]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
Merge branch 'master' of /home/dsarrut/clitk3.server
[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 vtkImageMapToWindowLevelColors* 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(false);
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<vtkImageMapToWindowLevelColors>::New();
412     mFusionMapper->SetInput(mFusionReslice->GetOutput());
413
414     if (!mFusionActor) {
415       mFusionActor = vtkSmartPointer<vtkImageActor>::New();
416       mFusionActor->SetInput(mFusionMapper->GetOutput());
417       mFusionActor->SetPickable(0);
418       mFusionActor->SetVisibility(false);
419       mFusionActor->SetOpacity(0.7);
420       this->GetRenderer()->AddActor(mFusionActor);
421     }
422
423     //Synchronize orientation and slice
424     this->SetSliceOrientation(this->SliceOrientation);
425     this->SetTSlice(mCurrentTSlice);
426   }
427 }
428 //------------------------------------------------------------------------------
429
430
431 //------------------------------------------------------------------------------
432 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
433 {
434   if (actor_type == "vector") {
435     this->mVFActor->SetVisibility(vis);
436   }
437   if (actor_type == "overlay") {
438     this->mOverlayActor->SetVisibility(vis);
439   }
440   if (actor_type == "fusion") {
441     this->mFusionActor->SetVisibility(vis);
442   }
443   if (actor_type == "contour")
444     this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
445   UpdateDisplayExtent();
446 }
447 //------------------------------------------------------------------------------
448
449
450 //------------------------------------------------------------------------------
451 void vvSlicer::SetVF(vvImage::Pointer vf)
452 {
453   if (vf->GetVTKImages().size()) {
454     mVF = vf;
455
456     if (!mAAFilter) {
457       mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
458       mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
459       mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
460     }
461     mVOIFilter->SetInput(vf->GetFirstVTKImageData());
462     mAAFilter->SetInput(mVOIFilter->GetOutput());
463     ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
464     mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
465
466     if (!mArrow)
467       mArrow = vtkSmartPointer<vvGlyphSource>::New();
468     mArrow->SetGlyphTypeToSpecificArrow();
469     mArrow->SetScale(mScale);
470     mArrow->FilledOff();
471
472     // Glyph the gradient vector (with arrows)
473     if (!mGlyphFilter)
474       mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
475     mGlyphFilter->SetInput(mAAFilter->GetOutput());
476     mGlyphFilter->SetSource(mArrow->GetOutput());
477     mGlyphFilter->ScalingOn();
478     mGlyphFilter->SetScaleModeToScaleByVector();
479     mGlyphFilter->OrientOn();
480     mGlyphFilter->SetVectorModeToUseVector();
481     mGlyphFilter->SetColorModeToColorByVector();
482
483     if (!mVFColorLUT)
484       mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
485
486     double mVFColorHSV[3];
487     vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
488     mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
489     mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
490     mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
491
492     if (!mVFMapper)
493       mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
494     mVFMapper->SetInput(mGlyphFilter->GetOutput());
495     mVFMapper->ImmediateModeRenderingOn();
496     mVFMapper->SetLookupTable(mVFColorLUT);
497
498     if (!mVFActor)
499       mVFActor = vtkSmartPointer<vtkActor>::New();
500     mVFActor->SetMapper(mVFMapper);
501     mVFActor->SetPickable(0);
502     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
503     this->UpdateDisplayExtent();
504     this->GetRenderer()->AddActor(mVFActor);
505
506     //Synchronize slice
507     SetTSlice(mCurrentTSlice);
508   }
509 }
510 //------------------------------------------------------------------------------
511
512
513 //------------------------------------------------------------------------------
514 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
515 {
516   mLandmarks = landmarks;
517   if (landmarks) {
518
519     if (!mCross)
520       mCross = vtkSmartPointer<vtkCursor3D>::New();
521     mCross->SetFocalPoint(0.0,0.0,0.0);
522     mCross->SetModelBounds(-10,10,-10,10,-10,10);
523     mCross->AllOff();
524     mCross->AxesOn();
525
526     if (!mLandGlyph)
527       mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
528     mLandGlyph->SetSource(mCross->GetOutput());
529     mLandGlyph->SetInput(landmarks->GetOutput());
530     //mLandGlyph->SetIndexModeToScalar();
531     mLandGlyph->SetRange(0,1);
532     mLandGlyph->ScalingOff();
533
534     mLandGlyph->SetColorModeToColorByScalar();
535
536     if (!mClipBox)
537       mClipBox = vtkSmartPointer<vtkBox>::New();
538     if (!mLandClipper)
539       mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
540     mLandClipper->InsideOutOn();
541     mLandClipper->SetInput(mLandGlyph->GetOutput());
542     mLandClipper->SetClipFunction(mClipBox);
543
544     if (!mLandMapper)
545       mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
546     mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
547     //mLandMapper->ScalarVisibilityOff();
548
549     if (!mLandActor)
550       mLandActor = vtkSmartPointer<vtkActor>::New();
551     mLandActor->SetMapper(mLandMapper);
552     mLandActor->GetProperty()->SetColor(255,10,212);
553     mLandActor->SetPickable(0);
554     mLandActor->SetVisibility(true);
555     this->UpdateDisplayExtent();
556     this->GetRenderer()->AddActor(mLandActor);
557   }
558 }
559 //------------------------------------------------------------------------------
560
561 //------------------------------------------------------------------------------
562 //FIXME: this function leaks memory, we should fix it someday :)
563 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
564 {
565   if (actor_type == "vector") {
566     Renderer->RemoveActor(mVFActor);
567     mGlyphFilter=NULL;
568     mVF = NULL;
569     mArrow = NULL;
570     mAAFilter=NULL;
571     mVOIFilter = NULL;
572     mVFMapper = NULL;
573     mVFActor = NULL;
574   }
575   if (actor_type == "overlay") {
576     Renderer->RemoveActor(mOverlayActor);
577     mOverlay = NULL;
578     mOverlayActor = NULL;
579     mOverlayMapper = NULL;
580   }
581   if (actor_type == "fusion") {
582     Renderer->RemoveActor(mFusionActor);
583     mFusion = NULL;
584     mFusionActor = NULL;
585     mFusionMapper = NULL;
586   }
587   if (actor_type == "contour") {
588     Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
589     mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
590   }
591 }
592 //------------------------------------------------------------------------------
593
594
595 //------------------------------------------------------------------------------
596 void vvSlicer::SetVFSubSampling(int sub)
597 {
598   if (mVOIFilter) {
599     mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
600     mSubSampling = sub;
601   }
602   UpdateDisplayExtent();
603   Render();
604 }
605 //------------------------------------------------------------------------------
606
607
608 //------------------------------------------------------------------------------
609 void vvSlicer::SetVFScale(int scale)
610 {
611   mScale = scale;
612   if (mArrow)
613     mArrow->SetScale(mScale);
614   UpdateDisplayExtent();
615   Render();
616 }
617 //------------------------------------------------------------------------------
618
619 //------------------------------------------------------------------------------
620 void vvSlicer::SetVFWidth(int width)
621 {
622   mVFWidth = width;
623   if (mVFActor)
624     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
625   UpdateDisplayExtent();
626   Render();
627 }
628 //------------------------------------------------------------------------------
629
630
631 //------------------------------------------------------------------------------
632 void vvSlicer::SetVFLog(int log)
633 {
634   mVFLog = log;
635   if (mGlyphFilter) {
636     mGlyphFilter->SetUseLog(mVFLog);
637     mGlyphFilter->Modified();
638   }
639   UpdateDisplayExtent();
640   Render();
641 }
642 //------------------------------------------------------------------------------
643
644
645 //------------------------------------------------------------------------------
646 void vvSlicer::SetTSlice(int t)
647 {
648   if (t < 0)
649     t = 0;
650   else if ((unsigned int)t >= mImage->GetVTKImages().size())
651     t = mImage->GetVTKImages().size() -1;
652
653   if (mCurrentTSlice == t) return;
654
655   mCurrentTSlice = t;
656   mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
657   if (mVF && mVFActor->GetVisibility()) {
658     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
659       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
660   }
661   if (mOverlay && mOverlayActor->GetVisibility()) {
662     if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
663       mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
664   }
665   if (mFusion && mFusionActor->GetVisibility()) {
666     if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
667       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
668   }
669   if (mSurfaceCutActors.size() > 0)
670     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
671          i!=mSurfaceCutActors.end(); i++)
672       (*i)->SetTimeSlice(mCurrentTSlice);
673   UpdateDisplayExtent();
674 }
675 //------------------------------------------------------------------------------
676
677
678 //------------------------------------------------------------------------------
679 int vvSlicer::GetTSlice()
680 {
681   return mCurrentTSlice;
682 }
683 //------------------------------------------------------------------------------
684
685 //------------------------------------------------------------------------------
686 void vvSlicer::SetSliceOrientation(int orientation)
687 {
688   //if 2D image, force to watch in Axial View
689   int extent[6];
690   this->GetInput()->GetWholeExtent(extent);
691   if (extent[5]-extent[4] <= 2)
692     orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
693
694   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
695       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
696     vtkErrorMacro("Error - invalid slice orientation " << orientation);
697     return;
698   }
699   
700   this->SliceOrientation = orientation;
701
702   if(mFusion)
703     AdjustResliceToSliceOrientation(mFusionReslice);
704
705   if(mOverlay)
706     AdjustResliceToSliceOrientation(mOverlayReslice);
707
708   // Update the viewer
709   int *range = this->GetSliceRange();
710   if (range)
711     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
712
713   // Go to current cursor position
714   // double* cursorPos = GetCursorPosition();
715   // DDV(cursorPos, 3);
716   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
717
718   this->UpdateOrientation();
719   this->UpdateDisplayExtent();
720
721   if (this->Renderer && this->GetInput()) {
722     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
723     this->Renderer->ResetCamera();
724     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
725   }
726
727   SetContourSlice();
728 }
729 //----------------------------------------------------------------------------
730
731 //------------------------------------------------------------------------------
732 // This function ensures that we sample the slices of a vtkImageReslice filter
733 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
734 // In other words, we change the grid of the reslice in the same way as the grid
735 // of the displayed image in the slicing direction.
736 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
737 {
738   // Reset autocrop
739   double origin[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
740   double spacing[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
741   reslice->SetOutputOrigin(origin);
742   reslice->SetOutputSpacing(spacing);
743   reslice->GetOutput()->UpdateInformation();
744   reslice->GetOutput()->GetOrigin(origin);
745   reslice->GetOutput()->GetSpacing(spacing);
746
747   // Use similar spacing as the image in the direction SliceOrientation
748   spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
749
750   // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
751   // Step 1: from world coordinates to image coordinates
752   origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
753   origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
754   // Step 2: round to superior grid positionInc
755   origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
756   // Step 3: back to world coordinates
757   origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
758   origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
759
760   // Set new spacing and origin
761   reslice->SetOutputOrigin(origin);
762   reslice->SetOutputSpacing(spacing);
763   reslice->UpdateInformation();
764 }
765 //------------------------------------------------------------------------------
766
767 //----------------------------------------------------------------------------
768 int * vvSlicer::GetExtent(){
769   int *w_ext;
770   if (mUseReducedExtent) {
771     w_ext = mReducedExtent;
772   } else w_ext = GetInput()->GetWholeExtent();
773   return w_ext;
774 }
775 //----------------------------------------------------------------------------
776
777
778 //----------------------------------------------------------------------------
779 int vvSlicer::GetOrientation()
780 {
781   return this->SliceOrientation;
782 }
783 //----------------------------------------------------------------------------
784
785
786 //----------------------------------------------------------------------------
787 void vvSlicer::UpdateDisplayExtent()
788 {
789   vtkImageData *input = this->GetInput();
790   if (!input || !this->ImageActor) {
791     return;
792   }
793   input->UpdateInformation();
794
795   // Local copy of extent
796   int w_ext[6];
797   copyExtent(GetExtent(), w_ext);
798   // Set slice value
799   w_ext[ this->SliceOrientation*2   ] = this->Slice;
800   w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
801   
802   // Image actor
803   this->ImageActor->SetDisplayExtent(w_ext);
804   
805   // Overlay image actor
806   if (mOverlay && mOverlayActor->GetVisibility()) {
807     int overExtent[6];
808     mOverlayReslice->GetOutput()->UpdateInformation();
809     this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
810     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
811     mOverlayActor->SetDisplayExtent( overExtent );
812   }
813
814   // Fusion image actor
815   if (mFusion && mFusionActor->GetVisibility()) {
816     int fusExtent[6];
817     mFusionReslice->GetOutput()->UpdateInformation();
818     this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
819     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
820     mFusionActor->SetDisplayExtent(fusExtent);
821   }
822
823   // Vector field actor
824   double* camera = Renderer->GetActiveCamera()->GetPosition();
825   double* image_bounds = ImageActor->GetBounds();
826   double position[3] = {0, 0, 0};
827   position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2]; 
828
829   //print_vector<double, 6>("camera", camera);
830   //print_vector<double, 6>("image_bounds", image_bounds);
831   //print_vector<double, 3>("position", position);
832
833   // find where to place the VF actor. to deal with
834   // z-buffer issues, the VF is placed right in front of the image,
835   // subject to a small offset. the position actually depends on the
836   // the location of the camera relative to the image. 
837   double offset = 1;
838   if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
839     offset = -1;
840   
841   if (mVF && mVFActor->GetVisibility()) {
842     int vfExtent[6];
843     mVF->GetVTKImages()[0]->UpdateInformation();
844     this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
845     ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
846     mVOIFilter->SetVOI(vfExtent);
847     int orientation[3] = {1,1,1};
848     orientation[this->SliceOrientation] = 0;
849     mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
850     mVFMapper->Update();
851
852     position[this->SliceOrientation] += offset;
853     mVFActor->SetPosition(position);
854   }
855   
856   // Landmarks actor
857   if (mLandActor) {
858     if (mClipBox) {
859       double bounds [6];
860       for(unsigned int i=0; i<6; i++)
861         bounds[i] = ImageActor->GetBounds()[i];
862       bounds[ this->SliceOrientation*2   ] = ImageActor->GetBounds()[ this->SliceOrientation*2  ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
863       bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
864       mClipBox->SetBounds(bounds);
865       UpdateLandmarks();
866     }
867     
868     position[this->SliceOrientation] = offset;
869     mLandActor->SetPosition(position);
870   }
871
872   // Figure out the correct clipping range
873   if (this->Renderer) {
874     if (this->InteractorStyle &&
875         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
876       this->Renderer->ResetCameraClippingRange();
877     } else {
878       vtkCamera *cam = this->Renderer->GetActiveCamera();
879       if (cam) {
880         double bounds[6];
881         this->ImageActor->GetBounds(bounds);
882         double spos = (double)bounds[this->SliceOrientation * 2];
883         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
884         double range = fabs(spos - cpos);
885         double *spacing = input->GetSpacing();
886         double avg_spacing =
887           ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
888         cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
889       }
890     }
891   }
892   
893 }
894 //----------------------------------------------------------------------------
895
896 //----------------------------------------------------------------------------
897 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
898                                                 vtkImageData *targetImage, int targetExtent[6])
899 {
900   double dExtents[6];
901   for(unsigned int i=0; i<6; i++) {
902     // From source voxel coordinates to world coordinates
903     dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
904
905     // From world coordinates to floating point target voxel coordinates
906     dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
907     
908     // Round to nearest
909     targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
910   }
911 }
912 //----------------------------------------------------------------------------
913
914 //----------------------------------------------------------------------------
915 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
916 {
917   bool out = false;
918   int maxBound = 6;
919
920   //2D overlay on 3D image specific case
921   if (refExtent[4] == refExtent[5]) {
922     maxBound = 4;
923     extent[4] = refExtent[4];
924     extent[5] = refExtent[5];
925   }
926
927   for (int i = 0; i < maxBound; i = i+2) {
928     //if we are totally outside the image
929     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
930       out = true;
931       break;
932     }
933     //crop to the limit of the image
934     extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
935     extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
936     extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
937     extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
938   }
939   if (out)
940     for (int i = 0; i < maxBound; i = i+2) {
941       extent[i] = refExtent[i];
942       extent[i+1] = refExtent[i];
943     }
944 }
945 //----------------------------------------------------------------------------
946
947
948 //----------------------------------------------------------------------------
949 void vvSlicer::UpdateOrientation()
950 {
951   // Set the camera position
952   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
953   if (cam) {
954     switch (this->SliceOrientation) {
955     case vtkImageViewer2::SLICE_ORIENTATION_XY:
956       cam->SetFocalPoint(0,0,0);
957       cam->SetPosition(0,0,-1); // -1 if medical ?
958       cam->SetViewUp(0,-1,0);
959       break;
960
961     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
962       cam->SetFocalPoint(0,0,0);
963       cam->SetPosition(0,-1,0); // 1 if medical ?
964       cam->SetViewUp(0,0,1);
965       break;
966
967     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
968       cam->SetFocalPoint(0,0,0);
969       cam->SetPosition(-1,0,0); // -1 if medical ?
970       cam->SetViewUp(0,0,1);
971       break;
972     }
973   }
974 }
975 //----------------------------------------------------------------------------
976
977
978 //----------------------------------------------------------------------------
979 void vvSlicer::SetOpacity(double s)
980 {
981   this->GetImageActor()->SetOpacity(s);
982 }
983 //----------------------------------------------------------------------------
984
985
986 //----------------------------------------------------------------------------
987 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
988 {
989   this->Superclass::SetRenderWindow(rw);
990   this->SetupInteractor(rw->GetInteractor());
991   ca->SetImageActor(this->GetImageActor());
992   ca->SetWindowLevel(this->GetWindowLevel());
993   ca->SetText(2, "<slice>");
994   ca->SetText(3, "<window>\n<level>");
995
996   double bounds[6];
997   double max = 65000;
998
999   bounds[0] = -max;
1000   bounds[1] = max;
1001   bounds[2] = -max;
1002   bounds[3] = max;
1003   bounds[4] = -max;
1004   bounds[5] = max;
1005
1006   crossCursor->SetModelBounds(bounds);
1007   this->GetRenderer()->AddActor(pdmA);
1008   this->GetRenderer()->AddActor(ca);
1009   this->GetRenderer()->ResetCamera();
1010
1011   //this is just a mapping between the labeling of the orientations presented to the user and
1012   //the one used by vtk
1013   SetSliceOrientation(2-(orientation%3));
1014   ResetCamera();
1015 }
1016 //----------------------------------------------------------------------------
1017
1018
1019 //----------------------------------------------------------------------------
1020 void vvSlicer::ResetCamera()
1021 {
1022   this->GetRenderer()->ResetCamera();
1023 }
1024 //----------------------------------------------------------------------------
1025
1026
1027 //----------------------------------------------------------------------------
1028 void vvSlicer::SetDisplayMode(bool i)
1029 {
1030   this->GetImageActor()->SetVisibility(i);
1031   this->GetAnnotation()->SetVisibility(i);
1032   this->GetRenderer()->SetDraw(i);
1033   if (mLandActor)
1034     mLandActor->SetVisibility(i);
1035   pdmA->SetVisibility(i);
1036   if (i)
1037     UpdateDisplayExtent();
1038 }
1039 //----------------------------------------------------------------------------
1040
1041
1042 //----------------------------------------------------------------------------
1043 void vvSlicer::FlipHorizontalView()
1044 {
1045   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1046   if (cam) {
1047     double *position = cam->GetPosition();
1048     switch (this->SliceOrientation) {
1049     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1050       cam->SetPosition(position[0],position[1],-position[2]);
1051       break;
1052
1053     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1054       cam->SetPosition(position[0],-position[1],position[2]);
1055       break;
1056
1057     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1058       cam->SetPosition(-position[0],position[1],position[2]);
1059       break;
1060     }
1061     this->Renderer->ResetCameraClippingRange();
1062     this->UpdateDisplayExtent();
1063   }
1064 }
1065 //----------------------------------------------------------------------------
1066
1067
1068 //----------------------------------------------------------------------------
1069 void vvSlicer::FlipVerticalView()
1070 {
1071   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1072   if (cam) {
1073     FlipHorizontalView();
1074     double *viewup = cam->GetViewUp();
1075     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1076     this->UpdateDisplayExtent();
1077   }
1078 }
1079 //----------------------------------------------------------------------------
1080
1081
1082 //----------------------------------------------------------------------------
1083 void vvSlicer::SetColorWindow(double window)
1084 {
1085   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1086   if ( LUT ) {
1087     double level = this->GetWindowLevel()->GetLevel();
1088     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1089     LUT->Build();
1090   }
1091   this->vtkImageViewer2::SetColorWindow(window);
1092 }
1093 //----------------------------------------------------------------------------
1094
1095
1096 //----------------------------------------------------------------------------
1097 void vvSlicer::SetColorLevel(double level)
1098 {
1099   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1100   if ( LUT ) {
1101     double window = this->GetWindowLevel()->GetWindow();
1102     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1103     LUT->Build();
1104   }
1105   this->vtkImageViewer2::SetColorLevel(level);
1106 }
1107 //----------------------------------------------------------------------------
1108
1109 //----------------------------------------------------------------------------
1110 // Returns the min an the max value in a 41x41 region around the mouse pointer
1111 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1112 {
1113   //Get mouse pointer position in view coordinates
1114   double fLocalExtents[6];
1115   for(int i=0; i<3; i++) {
1116     fLocalExtents[i*2  ] = mCurrent[i];
1117     fLocalExtents[i*2+1] = mCurrent[i];
1118   }
1119   this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1120   this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1121   for(int i=0; i<3; i++) {
1122     if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1123       fLocalExtents[i*2  ] -= 0.2;
1124       fLocalExtents[i*2+1] += 0.2;
1125     }
1126   }
1127   this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1128   this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1129
1130   //Convert to image pixel coordinates (rounded)
1131   int iLocalExtents[6];
1132   for(int i=0; i<3; i++) {
1133     fLocalExtents[i*2  ] = (fLocalExtents[i*2  ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1134     fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1135
1136     iLocalExtents[i*2  ] = lrint(fLocalExtents[i*2  ]);
1137     iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1138
1139     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1140       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1141   }
1142
1143   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1144   voiFilter->SetInput(this->GetInput());
1145   voiFilter->SetVOI(iLocalExtents);
1146   voiFilter->Update();
1147   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1148     min = 0;
1149     max = 0;
1150     return;
1151   }
1152
1153   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1154   accFilter->SetInput(voiFilter->GetOutput());
1155   accFilter->Update();
1156
1157   min = *(accFilter->GetMin());
1158   max = *(accFilter->GetMax());
1159 }
1160 //----------------------------------------------------------------------------
1161
1162 //----------------------------------------------------------------------------
1163 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1164 {
1165   ix = lrint(X);
1166   iy = lrint(Y);
1167   iz = lrint(Z);
1168   if (ix < image->GetWholeExtent()[0] ||
1169       ix > image->GetWholeExtent()[1] ||
1170       iy < image->GetWholeExtent()[2] ||
1171       iy > image->GetWholeExtent()[3] ||
1172       iz < image->GetWholeExtent()[4] ||
1173       iz > image->GetWholeExtent()[5] )
1174     return std::numeric_limits<double>::quiet_NaN();
1175
1176   image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1177   image->Update();
1178   return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1179 }
1180 //----------------------------------------------------------------------------
1181
1182 //----------------------------------------------------------------------------
1183 void vvSlicer::Render()
1184 {
1185   if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1186     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1187     legend->SetVisibility(1);
1188   } else legend->SetVisibility(0);
1189
1190   if (ca->GetVisibility()) {
1191     std::stringstream worldPos;
1192     double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1193     double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1194     double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1195     
1196 //     if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1197 //     else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1]; 
1198 //     if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1199 //     else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3]; 
1200 //     if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1201 //     else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5]; 
1202
1203     if (X >= this->GetInput()->GetWholeExtent()[0] &&
1204         X <= this->GetInput()->GetWholeExtent()[1] &&
1205         Y >= this->GetInput()->GetWholeExtent()[2] &&
1206         Y <= this->GetInput()->GetWholeExtent()[3] &&
1207         Z >= this->GetInput()->GetWholeExtent()[4] &&
1208         Z <= this->GetInput()->GetWholeExtent()[5]) {
1209
1210       
1211       int ix, iy, iz;
1212       double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1213
1214       worldPos << "data value : " << value << std::endl;
1215       worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1216                           << lrint(mCurrent[1]) << ' '
1217                           << lrint(mCurrent[2]) << ' '
1218                           << mCurrentTSlice
1219                           << std::endl;
1220       worldPos << "pixel : " << ix << ' '
1221                              << iy << ' '
1222                              << iz << ' '
1223                              << mCurrentTSlice
1224                              << std::endl;
1225     }
1226     ca->SetText(1,worldPos.str().c_str());
1227   }
1228
1229   if (pdmA->GetVisibility()) {
1230     double x = mCursor[0];
1231     double y = mCursor[1];
1232     double z = mCursor[2];
1233     double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1234     double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1235     double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1236
1237     if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1238         xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1239         yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1240         yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1241         zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1242         zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1243       vtkRenderer * renderer = this->Renderer;
1244
1245       renderer->WorldToView(x,y,z);
1246       renderer->ViewToNormalizedViewport(x,y,z);
1247       renderer->NormalizedViewportToViewport(x,y);
1248       renderer->ViewportToNormalizedDisplay(x,y);
1249       renderer->NormalizedDisplayToDisplay(x,y);
1250       crossCursor->SetFocalPoint(x,y,z);
1251     } else
1252       crossCursor->SetFocalPoint(-1,-1,z);
1253   }
1254
1255
1256   if (mOverlay && mOverlayActor->GetVisibility()) {
1257     mOverlayMapper->SetWindow(this->GetColorWindow());
1258     mOverlayMapper->SetLevel(this->GetColorLevel());
1259     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1260     mOverlayMapper->GetOutput()->Update();
1261     mOverlayMapper->Update();
1262   }
1263   if (mLandMapper)
1264     UpdateLandmarks();
1265
1266   this->GetRenderWindow()->Render();
1267 }
1268 //----------------------------------------------------------------------------
1269
1270
1271 //----------------------------------------------------------------------------
1272 void vvSlicer::UpdateCursorPosition()
1273 {
1274   if (this->GetImageActor()->GetVisibility()) {
1275     pdmA->SetVisibility(true);
1276     mCursor[0] = mCurrent[0];
1277     mCursor[1] = mCurrent[1];
1278     mCursor[2] = mCurrent[2];
1279     mCursor[3] = mCurrentTSlice;
1280   }
1281 }
1282 //----------------------------------------------------------------------------
1283
1284
1285 //----------------------------------------------------------------------------
1286 void vvSlicer::UpdateLandmarks()
1287 {
1288   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1289   if (pd->GetPoints()) {
1290     mLandGlyph->SetRange(0,1);
1291     mLandGlyph->Modified();
1292     mLandGlyph->Update();
1293
1294     mClipBox->Modified();
1295     mLandClipper->Update();
1296     mLandMapper->Update();
1297   }
1298
1299 }
1300 //----------------------------------------------------------------------------
1301
1302
1303 //----------------------------------------------------------------------------
1304 void vvSlicer::SetSlice(int slice)
1305 {
1306   int *range = this->GetSliceRange();
1307   if (range) {
1308     if (slice < range[0]) {
1309       slice = range[0];
1310     } else if (slice > range[1]) {
1311       slice = range[1];
1312     }
1313   }
1314
1315   if (this->Slice == slice) {
1316     return;
1317   }
1318
1319   this->Slice = slice;
1320   SetContourSlice();
1321   this->Modified();
1322   this->UpdateDisplayExtent();
1323
1324   // Seems to work without this line
1325   //this->Render();
1326 }
1327 //----------------------------------------------------------------------------
1328
1329
1330 //----------------------------------------------------------------------------
1331 void vvSlicer::SetContourSlice()
1332 {
1333   if (mSurfaceCutActors.size() > 0)
1334     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1335          i!=mSurfaceCutActors.end(); i++) {
1336          
1337       (*i)->SetSlicingOrientation(this->SliceOrientation);
1338       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1339                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1340     }
1341 }
1342 //----------------------------------------------------------------------------
1343
1344
1345 //----------------------------------------------------------------------------
1346 void vvSlicer::ForceUpdateDisplayExtent()
1347 {
1348   this->UpdateDisplayExtent();
1349 }
1350 //----------------------------------------------------------------------------
1351
1352
1353 //----------------------------------------------------------------------------
1354 int* vvSlicer::GetDisplayExtent()
1355 {
1356   return this->GetImageActor()->GetDisplayExtent();
1357 }
1358 //----------------------------------------------------------------------------
1359
1360
1361 //----------------------------------------------------------------------------
1362 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1363 {
1364   this->Superclass::PrintSelf(os, indent);
1365 }
1366 //----------------------------------------------------------------------------
1367
1368 //----------------------------------------------------------------------------
1369 void vvSlicer::SetVFColor(double r, double g, double b)
1370 {
1371   double mVFColorHSV[3];
1372   mVFColor[0] = r;
1373   mVFColor[1] = g;
1374   mVFColor[2] = b;
1375
1376   vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1377   mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1378   mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1379   mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1380
1381   this->Render();
1382 }  
1383