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