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