]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
6100c56dfcc84aa5ad97ca19c980bad135cfd6c6
[clitk.git] / vv / vvSlicer.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 #include "vvSlicer.h"
20 #include "vvImage.h"
21 #include "vvSlicerManagerCommand.h"
22 #include "vvGlyphSource.h"
23 #include "vvGlyph2D.h"
24 #include "vvImageMapToWLColors.h"
25
26 #include <vtkTextProperty.h>
27 #include <vtkTextActor.h>
28 #include <vtkTextSource.h>
29 #include <vtkActor2D.h>
30 #include <vtkCursor2D.h>
31 #include <vtkPolyDataMapper2D.h>
32 #include <vtkProperty2D.h>
33 #include <vtkCornerAnnotation.h>
34 #include <vtkImageMapToWindowLevelColors.h>
35 #include <vtkImageData.h>
36 #include <vtkImageActor.h>
37 #include <vvBlendImageActor.h>
38 #include <vtkToolkits.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkPointData.h>
41 #include <vtkDataArray.h>
42 #include <vtkFloatArray.h>
43 #include <vtkClipPolyData.h>
44 #include <vtkGlyph3D.h>
45 #include <vtkMath.h>
46 #include <vtkCursor3D.h>
47 #include <vtkProperty.h>
48 #include <vtkLight.h>
49 #include <vtkLightCollection.h>
50 #include <vtkScalarBarActor.h>
51 #include <vtkLookupTable.h>
52
53 #include <vtkRenderer.h>
54 #include <vtkRendererCollection.h>
55 #include <vtkRenderWindow.h>
56 #include <vtkRenderWindowInteractor.h>
57 #include <vtkCamera.h>
58 #include <vtkCallbackCommand.h>
59 #include <vtkCommand.h>
60 #include <vtkPolyDataMapper.h>
61 #include <vtkBox.h>
62
63 #include <vtkExtractVOI.h>
64 #include <vtkSphereSource.h>
65 #include <vtkCutter.h>
66 #include <vtkAssignAttribute.h>
67 #include <vtkImageAccumulate.h>
68 #include <vtkImageReslice.h>
69
70 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
71 vtkStandardNewMacro(vvSlicer);
72 static void copyExtent(int* in, int* to){
73  for(int i=0; i<6; ++i) to[i]=in[i]; 
74 }
75 //------------------------------------------------------------------------------
76 vvSlicer::vvSlicer()
77 {
78   this->UnInstallPipeline();
79   mImage = NULL;
80   mReducedExtent = new int[6];
81   mCurrentTSlice = 0;
82   mUseReducedExtent = false;
83
84   mCurrent[0] = -VTK_DOUBLE_MAX;
85   mCurrent[1] = -VTK_DOUBLE_MAX;
86   mCurrent[2] = -VTK_DOUBLE_MAX;
87
88   mCursor[0] = -VTK_DOUBLE_MAX;
89   mCursor[1] = -VTK_DOUBLE_MAX;
90   mCursor[2] = -VTK_DOUBLE_MAX;
91   mCursor[3] = -VTK_DOUBLE_MAX;
92
93   mSubSampling = 5;
94   mScale = 1;
95   mVFLog = 0;
96   mVFWidth = 1;
97   mVFColor[0] = 0;
98   mVFColor[1] = 1;
99   mVFColor[2] = 0;
100
101   std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
102   text += "F5 = horizontal flip; F6 = vertical flip\n\n";
103   text += "0,1,2,3,4,5 : preset windowing\n";
104   text += "6,7,8,9 : preset colormap\n";
105   text += "z : local windowing\n";
106   text += "r : reset view\n";
107   text += "l : reload image\n";
108   text += "f : fly to mouse position\n";
109   text += "g : go to cross hair position\n\n";
110   text += "Up,down : change slice\n";
111   text += "Left,right : change tenporal slice\n\n";
112   text += "Scrollbar (or w/x) : zoom in/out\n";
113   text += "left button : synchronize all views\n";
114   text += "middle button : grab image\n";
115   text += "right button : change windowing\n";
116
117   crossCursor = vtkSmartPointer<vtkCursor2D>::New();
118   crossCursor->AllOff();
119   crossCursor->AxesOn();
120   crossCursor->SetTranslationMode(1);
121   crossCursor->SetRadius(2);
122
123   pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
124   pdm->SetInput(crossCursor->GetOutput());
125
126   pdmA = vtkSmartPointer<vtkActor2D>::New();
127   pdmA->SetMapper(pdm);
128   pdmA->GetProperty()->SetColor(255,10,212);
129   pdmA->SetVisibility(0);
130   pdmA->SetPickable(0);
131
132   ca = vtkSmartPointer<vtkCornerAnnotation>::New();
133   ca->GetTextProperty()->SetColor(255,10,212);
134   ca->SetVisibility(1);
135   mFileName = "";
136
137   mVF = NULL;
138   mOverlay = NULL;
139   mFusion = NULL;
140   mLandmarks = NULL;
141
142   legend = vtkSmartPointer<vtkScalarBarActor>::New();
143   //legend->SetTitle("test!");
144   legend->SetPosition(0.82,0.18);
145   legend->SetWidth(0.1);
146   legend->SetVisibility(0);
147   legend->SetLabelFormat("%.1f");
148   this->GetRenderer()->AddActor(legend);
149
150   this->WindowLevel->Delete();
151   this->WindowLevel = vvImageMapToWLColors::New();
152
153   this->InstallPipeline();
154 }
155 //------------------------------------------------------------------------------
156
157
158 //------------------------------------------------------------------------------
159 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
160 {
161   return mOverlayMapper.GetPointer();
162 }
163 //------------------------------------------------------------------------------
164
165
166 //------------------------------------------------------------------------------
167 vvBlendImageActor* vvSlicer::GetOverlayActor()
168 {
169   return mOverlayActor.GetPointer();
170 }
171 //------------------------------------------------------------------------------
172
173
174 //------------------------------------------------------------------------------
175 vtkImageMapToColors* vvSlicer::GetFusionMapper()
176 {
177   return mFusionMapper.GetPointer();
178 }
179 //------------------------------------------------------------------------------
180
181
182 //------------------------------------------------------------------------------
183 vtkImageActor* vvSlicer::GetFusionActor()
184 {
185   return mFusionActor.GetPointer();
186 }
187 //------------------------------------------------------------------------------
188
189
190 //------------------------------------------------------------------------------
191 vtkActor* vvSlicer::GetVFActor()
192 {
193   return mVFActor.GetPointer();
194 }
195 //------------------------------------------------------------------------------
196
197
198 //------------------------------------------------------------------------------
199 vtkCornerAnnotation* vvSlicer::GetAnnotation()
200 {
201   return ca.GetPointer();
202 }
203 //------------------------------------------------------------------------------
204
205
206 //------------------------------------------------------------------------------
207 void vvSlicer::EnableReducedExtent(bool b)
208 {
209   mUseReducedExtent = b;
210 }
211 //------------------------------------------------------------------------------
212
213
214 //------------------------------------------------------------------------------
215 void vvSlicer::SetReducedExtent(int * ext)
216 {
217   copyExtent(ext, mReducedExtent);
218 }
219 //------------------------------------------------------------------------------
220
221
222 //------------------------------------------------------------------------------
223 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
224 {
225
226   mSurfaceCutActors.push_back(new vvMeshActor());
227   if (propagate)
228     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
229   else
230     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
231   mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
232   this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
233
234   SetContourSlice();
235 }
236 //------------------------------------------------------------------------------
237
238
239 //------------------------------------------------------------------------------
240 void vvSlicer::ToggleContourSuperposition()
241 {
242   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
243        i!=mSurfaceCutActors.end(); i++)
244     (*i)->ToggleSuperposition();
245 }
246 //------------------------------------------------------------------------------
247
248
249 //------------------------------------------------------------------------------
250 void vvSlicer::SetCursorColor(int r,int g, int b)
251 {
252   pdmA->GetProperty()->SetColor(r,g,b);
253 }
254 //------------------------------------------------------------------------------
255
256
257 //------------------------------------------------------------------------------
258 void vvSlicer::SetCursorVisibility(bool s)
259 {
260   pdmA->SetVisibility(s);
261 }
262 //------------------------------------------------------------------------------
263
264
265 //------------------------------------------------------------------------------
266 bool vvSlicer::GetCursorVisibility()
267 {
268   return pdmA->GetVisibility();
269 }
270 //------------------------------------------------------------------------------
271
272
273 //------------------------------------------------------------------------------
274 void vvSlicer::SetCornerAnnotationVisibility(bool s)
275 {
276   ca->SetVisibility(s);
277 }
278 //------------------------------------------------------------------------------
279
280
281 //------------------------------------------------------------------------------
282 bool vvSlicer::GetCornerAnnotationVisibility()
283 {
284   return ca->GetVisibility();
285 }
286 //------------------------------------------------------------------------------
287
288
289 //------------------------------------------------------------------------------
290 vvSlicer::~vvSlicer()
291 {
292   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
293        i!=mSurfaceCutActors.end(); i++)
294     delete (*i);
295   delete [] mReducedExtent;
296 }
297 //------------------------------------------------------------------------------
298
299
300 //------------------------------------------------------------------------------
301 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
302 {
303   mCurrent[0] = x;
304   mCurrent[1] = y;
305   mCurrent[2] = z;
306   mCurrentTSlice = t;
307 }
308 //------------------------------------------------------------------------------
309
310
311 //------------------------------------------------------------------------------
312 void vvSlicer::SetImage(vvImage::Pointer image)
313 {
314   if (image->GetVTKImages().size()) {
315     mImage = image;
316
317     if (!mImageReslice) {
318       mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
319       mImageReslice->SetInterpolationModeToLinear();
320       mImageReslice->AutoCropOutputOn();
321       mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
322     }
323     mImageReslice->SetResliceTransform(mImage->GetTransform());
324     mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
325     mImageReslice->UpdateInformation();
326
327     this->Superclass::SetInput(mImageReslice->GetOutput());
328
329     int extent[6];
330     this->GetInput()->GetWholeExtent(extent);
331
332     // Prevent crash when reload -> change slice if outside extent
333     if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
334       Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
335     }
336
337     // Make sure that the required part image has been computed
338     extent[SliceOrientation*2] = Slice;
339     extent[SliceOrientation*2+1] = Slice;
340     mImageReslice->GetOutput()->SetUpdateExtent(extent);
341     mImageReslice->GetOutput()->Update();
342
343     this->UpdateDisplayExtent();
344
345     mCurrentTSlice = 0;
346     ca->SetText(0,mFileName.c_str());
347   }
348 }
349 //------------------------------------------------------------------------------
350
351
352 //------------------------------------------------------------------------------
353 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
354 {
355   if (overlay->GetVTKImages().size()) {
356     mOverlay = overlay;
357     
358     if (!mOverlayReslice) {
359       mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
360       mOverlayReslice->SetInterpolationModeToLinear();
361       mOverlayReslice->AutoCropOutputOn();
362       mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
363     }
364     mOverlayReslice->SetResliceTransform(mOverlay->GetTransform());
365     mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
366
367     if (!mOverlayMapper)
368       mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
369     mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
370
371     if (!mOverlayActor) {
372       mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
373       mOverlayActor->SetInput(mOverlayMapper->GetOutput());
374       mOverlayActor->SetPickable(0);
375       mOverlayActor->SetVisibility(true);
376       mOverlayActor->SetOpacity(0.5);
377     }
378
379     //stupid but necessary : the Overlay need to be rendered before fusion
380     if (mFusionActor) {
381       this->GetRenderer()->RemoveActor(mFusionActor);
382       this->GetRenderer()->AddActor(mOverlayActor);
383       this->GetRenderer()->AddActor(mFusionActor);
384     } else
385       this->GetRenderer()->AddActor(mOverlayActor);
386
387     //Synchronize orientation and slice
388     this->SetSliceOrientation(this->SliceOrientation);
389     this->SetTSlice(mCurrentTSlice);
390   }
391 }
392 //------------------------------------------------------------------------------
393
394
395 //------------------------------------------------------------------------------
396 void vvSlicer::SetFusion(vvImage::Pointer fusion)
397 {
398   if (fusion->GetVTKImages().size()) {
399     mFusion = fusion;
400
401     if (!mFusionReslice) {
402       mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
403       mFusionReslice->SetInterpolationModeToLinear();
404       mFusionReslice->AutoCropOutputOn();
405       mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
406     }
407     mFusionReslice->SetResliceTransform(mFusion->GetTransform());
408     mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
409
410     if (!mFusionMapper)
411       mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
412     
413     vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
414     lut->SetRange(0, 1);
415     lut->SetValueRange(0, 1);
416     lut->SetSaturationRange(0, 0);
417     lut->Build();
418     mFusionMapper->SetLookupTable(lut);
419     mFusionMapper->SetInput(mFusionReslice->GetOutput());
420
421     if (!mFusionActor) {
422       mFusionActor = vtkSmartPointer<vtkImageActor>::New();
423       mFusionActor->SetInput(mFusionMapper->GetOutput());
424       mFusionActor->SetPickable(0);
425       mFusionActor->SetVisibility(true);
426       mFusionActor->SetOpacity(0.7);
427       this->GetRenderer()->AddActor(mFusionActor);
428     }
429
430     //Synchronize orientation and slice
431     this->SetSliceOrientation(this->SliceOrientation);
432     this->SetTSlice(mCurrentTSlice);
433   }
434 }
435 //------------------------------------------------------------------------------
436
437
438 //------------------------------------------------------------------------------
439 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
440 {
441   bool vis = false;
442   if (actor_type == "image") {
443     vis = this->ImageActor->GetVisibility();
444   }
445   else if (actor_type == "vector") {
446     vis = this->mVFActor->GetVisibility();
447   }
448   else if (actor_type == "overlay") {
449     vis = this->mOverlayActor->GetVisibility();
450   }
451   else if (actor_type == "fusion") {
452     vis = this->mFusionActor->GetVisibility();
453   }
454   else if (actor_type == "contour")
455     vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
456
457   return vis;
458 }
459 //------------------------------------------------------------------------------
460
461 //------------------------------------------------------------------------------
462 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
463 {
464   if (actor_type == "image") {
465     this->ImageActor->SetVisibility(vis);
466   }
467   else if (actor_type == "vector") {
468     this->mVFActor->SetVisibility(vis);
469   }
470   else if (actor_type == "overlay") {
471     this->mOverlayActor->SetVisibility(vis);
472   }
473   else if (actor_type == "fusion") {
474     this->mFusionActor->SetVisibility(vis);
475   }
476   else if (actor_type == "contour")
477     this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
478   UpdateDisplayExtent();
479 }
480 //------------------------------------------------------------------------------
481
482 //------------------------------------------------------------------------------
483 void vvSlicer::SetVF(vvImage::Pointer vf)
484 {
485   if (vf->GetVTKImages().size()) {
486     mVF = vf;
487
488     if (!mAAFilter) {
489       mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
490       mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
491       mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
492     }
493     mVOIFilter->SetInput(vf->GetFirstVTKImageData());
494     mAAFilter->SetInput(mVOIFilter->GetOutput());
495     ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
496     mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
497
498     if (!mArrow)
499       mArrow = vtkSmartPointer<vvGlyphSource>::New();
500     mArrow->SetGlyphTypeToSpecificArrow();
501     mArrow->SetScale(mScale);
502     mArrow->FilledOff();
503
504     // Glyph the gradient vector (with arrows)
505     if (!mGlyphFilter)
506       mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
507     mGlyphFilter->SetInput(mAAFilter->GetOutput());
508     mGlyphFilter->SetSource(mArrow->GetOutput());
509     mGlyphFilter->ScalingOn();
510     mGlyphFilter->SetScaleModeToScaleByVector();
511     mGlyphFilter->OrientOn();
512     mGlyphFilter->SetVectorModeToUseVector();
513     mGlyphFilter->SetColorModeToColorByVector();
514
515     if (!mVFColorLUT)
516       mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
517
518     double mVFColorHSV[3];
519     vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
520     mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
521     mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
522     mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
523
524     if (!mVFMapper)
525       mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
526     mVFMapper->SetInput(mGlyphFilter->GetOutput());
527     mVFMapper->ImmediateModeRenderingOn();
528     mVFMapper->SetLookupTable(mVFColorLUT);
529
530     if (!mVFActor)
531       mVFActor = vtkSmartPointer<vtkActor>::New();
532     mVFActor->SetMapper(mVFMapper);
533     mVFActor->SetPickable(0);
534     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
535     this->UpdateDisplayExtent();
536     this->GetRenderer()->AddActor(mVFActor);
537
538     //Synchronize slice
539     SetTSlice(mCurrentTSlice);
540   }
541 }
542 //------------------------------------------------------------------------------
543
544
545 //------------------------------------------------------------------------------
546 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
547 {
548   mLandmarks = landmarks;
549   if (landmarks) {
550
551     if (!mCross)
552       mCross = vtkSmartPointer<vtkCursor3D>::New();
553     mCross->SetFocalPoint(0.0,0.0,0.0);
554     mCross->SetModelBounds(-10,10,-10,10,-10,10);
555     mCross->AllOff();
556     mCross->AxesOn();
557
558     if (!mLandGlyph)
559       mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
560     mLandGlyph->SetSource(mCross->GetOutput());
561     mLandGlyph->SetInput(landmarks->GetOutput());
562     //mLandGlyph->SetIndexModeToScalar();
563     mLandGlyph->SetRange(0,1);
564     mLandGlyph->ScalingOff();
565
566     mLandGlyph->SetColorModeToColorByScalar();
567
568     if (!mClipBox)
569       mClipBox = vtkSmartPointer<vtkBox>::New();
570     if (!mLandClipper)
571       mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
572     mLandClipper->InsideOutOn();
573     mLandClipper->SetInput(mLandGlyph->GetOutput());
574     mLandClipper->SetClipFunction(mClipBox);
575
576     if (!mLandMapper)
577       mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
578     mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
579     //mLandMapper->ScalarVisibilityOff();
580
581     if (!mLandActor)
582       mLandActor = vtkSmartPointer<vtkActor>::New();
583     mLandActor->SetMapper(mLandMapper);
584     mLandActor->GetProperty()->SetColor(255,10,212);
585     mLandActor->SetPickable(0);
586     mLandActor->SetVisibility(true);
587     this->UpdateDisplayExtent();
588     this->GetRenderer()->AddActor(mLandActor);
589   }
590 }
591 //------------------------------------------------------------------------------
592
593 //------------------------------------------------------------------------------
594 //FIXME: this function leaks memory, we should fix it someday :)
595 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
596 {
597   if (actor_type == "vector") {
598     Renderer->RemoveActor(mVFActor);
599     mGlyphFilter=NULL;
600     mVF = NULL;
601     mArrow = NULL;
602     mAAFilter=NULL;
603     mVOIFilter = NULL;
604     mVFMapper = NULL;
605     mVFActor = NULL;
606   }
607   if (actor_type == "overlay") {
608     Renderer->RemoveActor(mOverlayActor);
609     mOverlay = NULL;
610     mOverlayActor = NULL;
611     mOverlayMapper = NULL;
612   }
613   if (actor_type == "fusion") {
614     Renderer->RemoveActor(mFusionActor);
615     mFusion = NULL;
616     mFusionActor = NULL;
617     mFusionMapper = NULL;
618   }
619   if (actor_type == "contour") {
620     Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
621     mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
622   }
623 }
624 //------------------------------------------------------------------------------
625
626
627 //------------------------------------------------------------------------------
628 void vvSlicer::SetVFSubSampling(int sub)
629 {
630   if (mVOIFilter) {
631     mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
632     mSubSampling = sub;
633   }
634   UpdateDisplayExtent();
635   Render();
636 }
637 //------------------------------------------------------------------------------
638
639
640 //------------------------------------------------------------------------------
641 void vvSlicer::SetVFScale(int scale)
642 {
643   mScale = scale;
644   if (mArrow)
645     mArrow->SetScale(mScale);
646   UpdateDisplayExtent();
647   Render();
648 }
649 //------------------------------------------------------------------------------
650
651 //------------------------------------------------------------------------------
652 void vvSlicer::SetVFWidth(int width)
653 {
654   mVFWidth = width;
655   if (mVFActor)
656     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
657   UpdateDisplayExtent();
658   Render();
659 }
660 //------------------------------------------------------------------------------
661
662
663 //------------------------------------------------------------------------------
664 void vvSlicer::SetVFLog(int log)
665 {
666   mVFLog = log;
667   if (mGlyphFilter) {
668     mGlyphFilter->SetUseLog(mVFLog);
669     mGlyphFilter->Modified();
670   }
671   UpdateDisplayExtent();
672   Render();
673 }
674 //------------------------------------------------------------------------------
675
676
677 //------------------------------------------------------------------------------
678 void vvSlicer::SetTSlice(int t)
679 {
680   if (t < 0)
681     t = 0;
682   else if ((unsigned int)t >= mImage->GetVTKImages().size())
683     t = mImage->GetVTKImages().size() -1;
684
685   if (mCurrentTSlice == t) return;
686
687   mCurrentTSlice = t;
688   mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
689   if (mVF && mVFActor->GetVisibility()) {
690     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
691       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
692   }
693   if (mOverlay && mOverlayActor->GetVisibility()) {
694     if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
695       mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
696   }
697   if (mFusion && mFusionActor->GetVisibility()) {
698     if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
699       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
700   }
701   if (mSurfaceCutActors.size() > 0)
702     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
703          i!=mSurfaceCutActors.end(); i++)
704       (*i)->SetTimeSlice(mCurrentTSlice);
705   UpdateDisplayExtent();
706 }
707 //------------------------------------------------------------------------------
708
709
710 //------------------------------------------------------------------------------
711 int vvSlicer::GetTSlice()
712 {
713   return mCurrentTSlice;
714 }
715 //------------------------------------------------------------------------------
716
717 //------------------------------------------------------------------------------
718 void vvSlicer::SetSliceOrientation(int orientation)
719 {
720   //if 2D image, force to watch in Axial View
721   int extent[6];
722   this->GetInput()->GetWholeExtent(extent);
723   if (extent[5]-extent[4] <= 2)
724     orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
725
726   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
727       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
728     vtkErrorMacro("Error - invalid slice orientation " << orientation);
729     return;
730   }
731   
732   this->SliceOrientation = orientation;
733
734   if(mFusion)
735     AdjustResliceToSliceOrientation(mFusionReslice);
736
737   if(mOverlay)
738     AdjustResliceToSliceOrientation(mOverlayReslice);
739
740   // Update the viewer
741   int *range = this->GetSliceRange();
742   if (range)
743     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
744
745   // Go to current cursor position
746   // double* cursorPos = GetCursorPosition();
747   // DDV(cursorPos, 3);
748   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
749
750   this->UpdateOrientation();
751   this->UpdateDisplayExtent();
752
753   if (this->Renderer && this->GetInput()) {
754     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
755     this->Renderer->ResetCamera();
756     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
757   }
758
759   SetContourSlice();
760 }
761 //----------------------------------------------------------------------------
762
763 //------------------------------------------------------------------------------
764 // This function ensures that we sample the slices of a vtkImageReslice filter
765 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
766 // In other words, we change the grid of the reslice in the same way as the grid
767 // of the displayed image in the slicing direction.
768 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
769 {
770   // Reset autocrop
771   double origin[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
772   double spacing[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
773   reslice->SetOutputOrigin(origin);
774   reslice->SetOutputSpacing(spacing);
775   reslice->GetOutput()->UpdateInformation();
776   reslice->GetOutput()->GetOrigin(origin);
777   reslice->GetOutput()->GetSpacing(spacing);
778
779   // Use similar spacing as the image in the direction SliceOrientation
780   spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
781
782   // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
783   // Step 1: from world coordinates to image coordinates
784   origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
785   origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
786   // Step 2: round to superior grid positionInc
787   origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
788   // Step 3: back to world coordinates
789   origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
790   origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
791
792   // Set new spacing and origin
793   reslice->SetOutputOrigin(origin);
794   reslice->SetOutputSpacing(spacing);
795   reslice->UpdateInformation();
796 }
797 //------------------------------------------------------------------------------
798
799 //----------------------------------------------------------------------------
800 int * vvSlicer::GetExtent(){
801   int *w_ext;
802   if (mUseReducedExtent) {
803     w_ext = mReducedExtent;
804   } else w_ext = GetInput()->GetWholeExtent();
805   return w_ext;
806 }
807 //----------------------------------------------------------------------------
808
809
810 //----------------------------------------------------------------------------
811 int vvSlicer::GetOrientation()
812 {
813   return this->SliceOrientation;
814 }
815 //----------------------------------------------------------------------------
816
817
818 //----------------------------------------------------------------------------
819 void vvSlicer::UpdateDisplayExtent()
820 {
821   vtkImageData *input = this->GetInput();
822   if (!input || !this->ImageActor) {
823     return;
824   }
825   input->UpdateInformation();
826
827   // Local copy of extent
828   int w_ext[6];
829   copyExtent(GetExtent(), w_ext);
830   // Set slice value
831   w_ext[ this->SliceOrientation*2   ] = this->Slice;
832   w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
833   
834   // Image actor
835   this->ImageActor->SetDisplayExtent(w_ext);
836   
837   // Overlay image actor
838   if (mOverlay && mOverlayActor->GetVisibility()) {
839     AdjustResliceToSliceOrientation(mOverlayReslice);
840     int overExtent[6];
841     this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
842     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
843     mOverlayActor->SetDisplayExtent( overExtent );
844   }
845
846   // Fusion image actor
847   if (mFusion && mFusionActor->GetVisibility()) {
848     AdjustResliceToSliceOrientation(mFusionReslice);
849     int fusExtent[6];
850     this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
851     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
852     mFusionActor->SetDisplayExtent(fusExtent);
853   }
854
855   // Vector field actor
856   double* camera = Renderer->GetActiveCamera()->GetPosition();
857   double* image_bounds = ImageActor->GetBounds();
858   double position[3] = {0, 0, 0};
859   position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2]; 
860
861   //print_vector<double, 6>("camera", camera);
862   //print_vector<double, 6>("image_bounds", image_bounds);
863   //print_vector<double, 3>("position", position);
864
865   // find where to place the VF actor. to deal with
866   // z-buffer issues, the VF is placed right in front of the image,
867   // subject to a small offset. the position actually depends on the
868   // the location of the camera relative to the image. 
869   double offset = 1;
870   if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
871     offset = -1;
872   
873   if (mVF && mVFActor->GetVisibility()) {
874     int vfExtent[6];
875     mVF->GetVTKImages()[0]->UpdateInformation();
876     this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
877     ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
878     mVOIFilter->SetVOI(vfExtent);
879     int orientation[3] = {1,1,1};
880     orientation[this->SliceOrientation] = 0;
881     mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
882     mVFMapper->Update();
883
884     position[this->SliceOrientation] += offset;
885     mVFActor->SetPosition(position);
886   }
887   
888   // Landmarks actor
889   if (mLandActor) {
890     if (mClipBox) {
891       double bounds [6];
892       for(unsigned int i=0; i<6; i++)
893         bounds[i] = ImageActor->GetBounds()[i];
894       bounds[ this->SliceOrientation*2   ] = ImageActor->GetBounds()[ this->SliceOrientation*2  ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
895       bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
896       mClipBox->SetBounds(bounds);
897       UpdateLandmarks();
898     }
899     
900     position[this->SliceOrientation] = offset;
901     mLandActor->SetPosition(position);
902   }
903
904   // Figure out the correct clipping range
905   if (this->Renderer) {
906     if (this->InteractorStyle &&
907         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
908       this->Renderer->ResetCameraClippingRange();
909     } else {
910       vtkCamera *cam = this->Renderer->GetActiveCamera();
911       if (cam) {
912         double bounds[6];
913         this->ImageActor->GetBounds(bounds);
914         double spos = (double)bounds[this->SliceOrientation * 2];
915         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
916         double range = fabs(spos - cpos);
917         double *spacing = input->GetSpacing();
918         double avg_spacing =
919           ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
920         cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
921       }
922     }
923   }
924   
925 }
926 //----------------------------------------------------------------------------
927
928 //----------------------------------------------------------------------------
929 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
930                                                 vtkImageData *targetImage, int targetExtent[6])
931 {
932   double dExtents[6];
933   for(unsigned int i=0; i<6; i++) {
934     // From source voxel coordinates to world coordinates
935     dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
936
937     // From world coordinates to floating point target voxel coordinates
938     dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
939     
940     // Round to nearest
941     targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
942   }
943 }
944 //----------------------------------------------------------------------------
945
946 //----------------------------------------------------------------------------
947 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
948 {
949   bool out = false;
950   int maxBound = 6;
951
952   //2D overlay on 3D image specific case
953   if (refExtent[4] == refExtent[5]) {
954     maxBound = 4;
955     extent[4] = refExtent[4];
956     extent[5] = refExtent[5];
957   }
958
959   for (int i = 0; i < maxBound; i = i+2) {
960     //if we are totally outside the image
961     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
962       out = true;
963       break;
964     }
965     //crop to the limit of the image
966     extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
967     extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
968     extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
969     extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
970   }
971   if (out)
972     for (int i = 0; i < maxBound; i = i+2) {
973       extent[i] = refExtent[i];
974       extent[i+1] = refExtent[i];
975     }
976 }
977 //----------------------------------------------------------------------------
978
979
980 //----------------------------------------------------------------------------
981 void vvSlicer::UpdateOrientation()
982 {
983   // Set the camera position
984   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
985   if (cam) {
986     switch (this->SliceOrientation) {
987     case vtkImageViewer2::SLICE_ORIENTATION_XY:
988       cam->SetFocalPoint(0,0,0);
989       cam->SetPosition(0,0,-1); // -1 if medical ?
990       cam->SetViewUp(0,-1,0);
991       break;
992
993     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
994       cam->SetFocalPoint(0,0,0);
995       cam->SetPosition(0,-1,0); // 1 if medical ?
996       cam->SetViewUp(0,0,1);
997       break;
998
999     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1000       cam->SetFocalPoint(0,0,0);
1001       cam->SetPosition(-1,0,0); // -1 if medical ?
1002       cam->SetViewUp(0,0,1);
1003       break;
1004     }
1005   }
1006 }
1007 //----------------------------------------------------------------------------
1008
1009
1010 //----------------------------------------------------------------------------
1011 void vvSlicer::SetOpacity(double s)
1012 {
1013   this->GetImageActor()->SetOpacity(s);
1014 }
1015 //----------------------------------------------------------------------------
1016
1017
1018 //----------------------------------------------------------------------------
1019 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1020 {
1021   this->Superclass::SetRenderWindow(rw);
1022   this->SetupInteractor(rw->GetInteractor());
1023   ca->SetImageActor(this->GetImageActor());
1024   ca->SetWindowLevel(this->GetWindowLevel());
1025   ca->SetText(2, "<slice>");
1026   ca->SetText(3, "<window>\n<level>");
1027
1028   double bounds[6];
1029   double max = 65000;
1030
1031   bounds[0] = -max;
1032   bounds[1] = max;
1033   bounds[2] = -max;
1034   bounds[3] = max;
1035   bounds[4] = -max;
1036   bounds[5] = max;
1037
1038   crossCursor->SetModelBounds(bounds);
1039   this->GetRenderer()->AddActor(pdmA);
1040   this->GetRenderer()->AddActor(ca);
1041   this->GetRenderer()->ResetCamera();
1042
1043   //this is just a mapping between the labeling of the orientations presented to the user and
1044   //the one used by vtk
1045   SetSliceOrientation(2-(orientation%3));
1046   ResetCamera();
1047 }
1048 //----------------------------------------------------------------------------
1049
1050
1051 //----------------------------------------------------------------------------
1052 void vvSlicer::ResetCamera()
1053 {
1054   this->GetRenderer()->ResetCamera();
1055 }
1056 //----------------------------------------------------------------------------
1057
1058
1059 //----------------------------------------------------------------------------
1060 void vvSlicer::SetDisplayMode(bool i)
1061 {
1062   this->GetRenderer()->SetDraw(i);
1063   if (i)
1064     UpdateDisplayExtent();
1065 }
1066 //----------------------------------------------------------------------------
1067
1068
1069 //----------------------------------------------------------------------------
1070 void vvSlicer::FlipHorizontalView()
1071 {
1072   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1073   if (cam) {
1074     double *position = cam->GetPosition();
1075     double factor[3] = {1, 1, 1};
1076     factor[this->SliceOrientation] = -1;
1077     cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1078     
1079 /*    switch (this->SliceOrientation) {
1080     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1081       cam->SetPosition(position[0],position[1],-position[2]);
1082       break;
1083
1084     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1085       cam->SetPosition(position[0],-position[1],position[2]);
1086       break;
1087
1088     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1089       cam->SetPosition(-position[0],position[1],position[2]);
1090       break;
1091     }*/
1092
1093     this->Renderer->ResetCameraClippingRange();
1094     this->UpdateDisplayExtent();
1095   }
1096 }
1097 //----------------------------------------------------------------------------
1098
1099
1100 //----------------------------------------------------------------------------
1101 void vvSlicer::FlipVerticalView()
1102 {
1103   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1104   if (cam) {
1105     FlipHorizontalView();
1106     double *viewup = cam->GetViewUp();
1107     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1108     this->UpdateDisplayExtent();
1109   }
1110 }
1111 //----------------------------------------------------------------------------
1112
1113
1114 //----------------------------------------------------------------------------
1115 void vvSlicer::SetColorWindow(double window)
1116 {
1117   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1118   if ( LUT ) {
1119     double level = this->GetWindowLevel()->GetLevel();
1120     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1121     LUT->Build();
1122   }
1123   this->vtkImageViewer2::SetColorWindow(window);
1124 }
1125 //----------------------------------------------------------------------------
1126
1127
1128 //----------------------------------------------------------------------------
1129 void vvSlicer::SetColorLevel(double level)
1130 {
1131   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1132   if ( LUT ) {
1133     double window = this->GetWindowLevel()->GetWindow();
1134     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1135     LUT->Build();
1136   }
1137   this->vtkImageViewer2::SetColorLevel(level);
1138 }
1139 //----------------------------------------------------------------------------
1140
1141 //----------------------------------------------------------------------------
1142 // Returns the min an the max value in a 41x41 region around the mouse pointer
1143 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1144 {
1145   //Get mouse pointer position in view coordinates
1146   double fLocalExtents[6];
1147   for(int i=0; i<3; i++) {
1148     fLocalExtents[i*2  ] = mCurrent[i];
1149     fLocalExtents[i*2+1] = mCurrent[i];
1150   }
1151   this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1152   this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1153   for(int i=0; i<3; i++) {
1154     if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1155       fLocalExtents[i*2  ] -= 0.2;
1156       fLocalExtents[i*2+1] += 0.2;
1157     }
1158   }
1159   this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1160   this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1161
1162   //Convert to image pixel coordinates (rounded)
1163   int iLocalExtents[6];
1164   for(int i=0; i<3; i++) {
1165     fLocalExtents[i*2  ] = (fLocalExtents[i*2  ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1166     fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1167
1168     iLocalExtents[i*2  ] = lrint(fLocalExtents[i*2  ]);
1169     iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1170
1171     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1172       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1173   }
1174
1175   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1176   voiFilter->SetInput(this->GetInput());
1177   voiFilter->SetVOI(iLocalExtents);
1178   voiFilter->Update();
1179   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1180     min = 0;
1181     max = 0;
1182     return;
1183   }
1184
1185   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1186   accFilter->SetInput(voiFilter->GetOutput());
1187   accFilter->Update();
1188
1189   min = *(accFilter->GetMin());
1190   max = *(accFilter->GetMax());
1191 }
1192 //----------------------------------------------------------------------------
1193
1194 //----------------------------------------------------------------------------
1195 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1196 {
1197   ix = lrint(X);
1198   iy = lrint(Y);
1199   iz = lrint(Z);
1200   if (ix < image->GetWholeExtent()[0] ||
1201       ix > image->GetWholeExtent()[1] ||
1202       iy < image->GetWholeExtent()[2] ||
1203       iy > image->GetWholeExtent()[3] ||
1204       iz < image->GetWholeExtent()[4] ||
1205       iz > image->GetWholeExtent()[5] )
1206     return std::numeric_limits<double>::quiet_NaN();
1207
1208   image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1209   image->Update();
1210   return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1211 }
1212 //----------------------------------------------------------------------------
1213
1214 //----------------------------------------------------------------------------
1215 void vvSlicer::Render()
1216 {
1217   if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1218     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1219     legend->SetVisibility(1);
1220   } else legend->SetVisibility(0);
1221
1222   if (ca->GetVisibility()) {
1223     std::stringstream worldPos;
1224     double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1225     double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1226     double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1227     
1228 //     if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1229 //     else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1]; 
1230 //     if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1231 //     else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3]; 
1232 //     if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1233 //     else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5]; 
1234
1235     if (X >= this->GetInput()->GetWholeExtent()[0] &&
1236         X <= this->GetInput()->GetWholeExtent()[1] &&
1237         Y >= this->GetInput()->GetWholeExtent()[2] &&
1238         Y <= this->GetInput()->GetWholeExtent()[3] &&
1239         Z >= this->GetInput()->GetWholeExtent()[4] &&
1240         Z <= this->GetInput()->GetWholeExtent()[5]) {
1241
1242       
1243       int ix, iy, iz;
1244       double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1245
1246       if(ImageActor->GetVisibility())
1247         worldPos << "data value : " << value << std::endl;
1248
1249       worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1250                           << lrint(mCurrent[1]) << ' '
1251                           << lrint(mCurrent[2]) << ' '
1252                           << mCurrentTSlice
1253                           << std::endl;
1254       worldPos << "pixel : " << ix << ' '
1255                              << iy << ' '
1256                              << iz << ' '
1257                              << mCurrentTSlice
1258                              << std::endl;
1259     }
1260     ca->SetText(1,worldPos.str().c_str());
1261   }
1262
1263   if (pdmA->GetVisibility()) {
1264     double x = mCursor[0];
1265     double y = mCursor[1];
1266     double z = mCursor[2];
1267     double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1268     double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1269     double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1270
1271     if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1272         xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1273         yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1274         yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1275         zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1276         zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1277       vtkRenderer * renderer = this->Renderer;
1278
1279       renderer->WorldToView(x,y,z);
1280       renderer->ViewToNormalizedViewport(x,y,z);
1281       renderer->NormalizedViewportToViewport(x,y);
1282       renderer->ViewportToNormalizedDisplay(x,y);
1283       renderer->NormalizedDisplayToDisplay(x,y);
1284       crossCursor->SetFocalPoint(x,y,z);
1285     } else
1286       crossCursor->SetFocalPoint(-1,-1,z);
1287   }
1288
1289
1290   if (mOverlay && mOverlayActor->GetVisibility()) {
1291     mOverlayMapper->SetWindow(this->GetColorWindow());
1292     mOverlayMapper->SetLevel(this->GetColorLevel());
1293     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1294     mOverlayMapper->GetOutput()->Update();
1295     mOverlayMapper->Update();
1296   }
1297   if (mLandMapper)
1298     UpdateLandmarks();
1299
1300   this->GetRenderWindow()->Render();
1301 }
1302 //----------------------------------------------------------------------------
1303
1304
1305 //----------------------------------------------------------------------------
1306 void vvSlicer::UpdateCursorPosition()
1307 {
1308   pdmA->SetVisibility(true);
1309   mCursor[0] = mCurrent[0];
1310   mCursor[1] = mCurrent[1];
1311   mCursor[2] = mCurrent[2];
1312   mCursor[3] = mCurrentTSlice;
1313 }
1314 //----------------------------------------------------------------------------
1315
1316
1317 //----------------------------------------------------------------------------
1318 void vvSlicer::UpdateLandmarks()
1319 {
1320   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1321   if (pd->GetPoints()) {
1322     mLandGlyph->SetRange(0,1);
1323     mLandGlyph->Modified();
1324     mLandGlyph->Update();
1325
1326     mClipBox->Modified();
1327     mLandClipper->Update();
1328     mLandMapper->Update();
1329   }
1330
1331 }
1332 //----------------------------------------------------------------------------
1333
1334
1335 //----------------------------------------------------------------------------
1336 void vvSlicer::SetSlice(int slice)
1337 {
1338   int *range = this->GetSliceRange();
1339   if (range) {
1340     if (slice < range[0]) {
1341       slice = range[0];
1342     } else if (slice > range[1]) {
1343       slice = range[1];
1344     }
1345   }
1346
1347   if (this->Slice == slice) {
1348     return;
1349   }
1350
1351   this->Slice = slice;
1352   SetContourSlice();
1353   this->Modified();
1354   this->UpdateDisplayExtent();
1355
1356   // Seems to work without this line
1357   //this->Render();
1358 }
1359 //----------------------------------------------------------------------------
1360
1361
1362 //----------------------------------------------------------------------------
1363 void vvSlicer::SetContourSlice()
1364 {
1365   if (mSurfaceCutActors.size() > 0)
1366     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1367          i!=mSurfaceCutActors.end(); i++) {
1368          
1369       (*i)->SetSlicingOrientation(this->SliceOrientation);
1370       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1371                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1372     }
1373 }
1374 //----------------------------------------------------------------------------
1375
1376
1377 //----------------------------------------------------------------------------
1378 void vvSlicer::ForceUpdateDisplayExtent()
1379 {
1380   this->UpdateDisplayExtent();
1381 }
1382 //----------------------------------------------------------------------------
1383
1384
1385 //----------------------------------------------------------------------------
1386 int* vvSlicer::GetDisplayExtent()
1387 {
1388   return this->GetImageActor()->GetDisplayExtent();
1389 }
1390 //----------------------------------------------------------------------------
1391
1392
1393 //----------------------------------------------------------------------------
1394 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1395 {
1396   this->Superclass::PrintSelf(os, indent);
1397 }
1398 //----------------------------------------------------------------------------
1399
1400 //----------------------------------------------------------------------------
1401 void vvSlicer::SetVFColor(double r, double g, double b)
1402 {
1403   double mVFColorHSV[3];
1404   mVFColor[0] = r;
1405   mVFColor[1] = g;
1406   mVFColor[2] = b;
1407
1408   vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1409   mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1410   mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1411   mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1412
1413   this->Render();
1414 }  
1415