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