]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[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()[0]);
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()[0]);
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()[0]);
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   // Update transform
702   mConcatenatedTransform->Identity();
703   mConcatenatedTransform->Concatenate(mImage->GetTransform()[t]);
704   mConcatenatedTransform->Concatenate(mSlicingTransform);
705
706   // Update image data
707   mCurrentTSlice = t;
708   mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
709   if (mVF && mVFActor->GetVisibility()) {
710     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
711       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
712   }
713   if (mOverlay && mOverlayActor->GetVisibility()) {
714     if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
715       mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
716   }
717   if (mFusion && mFusionActor->GetVisibility()) {
718     if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
719       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
720   }
721   if (mSurfaceCutActors.size() > 0)
722     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
723          i!=mSurfaceCutActors.end(); i++)
724       (*i)->SetTimeSlice(mCurrentTSlice);
725   UpdateDisplayExtent();
726 }
727 //------------------------------------------------------------------------------
728
729
730 //------------------------------------------------------------------------------
731 int vvSlicer::GetTSlice()
732 {
733   return mCurrentTSlice;
734 }
735 //------------------------------------------------------------------------------
736
737 //------------------------------------------------------------------------------
738 void vvSlicer::SetSliceOrientation(int orientation)
739 {
740   //if 2D image, force to watch in Axial View
741   int extent[6];
742   this->GetInput()->GetWholeExtent(extent);
743   if (extent[5]-extent[4] <= 2)
744     orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
745
746   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
747       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
748     vtkErrorMacro("Error - invalid slice orientation " << orientation);
749     return;
750   }
751   
752   this->SliceOrientation = orientation;
753
754   if(mFusion)
755     AdjustResliceToSliceOrientation(mFusionReslice);
756
757   if(mOverlay)
758     AdjustResliceToSliceOrientation(mOverlayReslice);
759
760   // Update the viewer
761   int *range = this->GetSliceRange();
762   if (range)
763     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
764
765   // Go to current cursor position
766   // double* cursorPos = GetCursorPosition();
767   // DDV(cursorPos, 3);
768   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
769
770   this->UpdateOrientation();
771   this->UpdateDisplayExtent();
772
773   if (this->Renderer && this->GetInput()) {
774     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
775     this->Renderer->ResetCamera();
776     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
777   }
778
779   SetContourSlice();
780 }
781 //----------------------------------------------------------------------------
782
783 //------------------------------------------------------------------------------
784 // This function ensures that we sample the slices of a vtkImageReslice filter
785 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
786 // In other words, we change the grid of the reslice in the same way as the grid
787 // of the displayed image in the slicing direction.
788 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
789 {
790   // Reset autocrop and update output information
791   reslice->SetOutputOriginToDefault();
792   reslice->SetOutputSpacingToDefault();
793   reslice->GetOutput()->UpdateInformation();
794
795   // Ge new origin / spacing
796   double origin[3];
797   double spacing[3];
798   reslice->GetOutput()->GetOrigin(origin);
799   reslice->GetOutput()->GetSpacing(spacing);
800
801   // Use similar spacing as the image in the direction SliceOrientation
802   spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
803
804   // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
805   // Step 1: from world coordinates to image coordinates
806   origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
807   origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
808   // Step 2: round to superior grid positionInc
809   origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
810   // Step 3: back to world coordinates
811   origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
812   origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
813
814   // Set new spacing and origin
815   reslice->SetOutputOrigin(origin);
816   reslice->SetOutputSpacing(spacing);
817   reslice->UpdateInformation();
818   reslice->GetOutput()->UpdateInformation();
819 }
820 //------------------------------------------------------------------------------
821
822 //----------------------------------------------------------------------------
823 int * vvSlicer::GetExtent(){
824   int *w_ext;
825   if (mUseReducedExtent) {
826     w_ext = mReducedExtent;
827   } else w_ext = GetInput()->GetWholeExtent();
828   return w_ext;
829 }
830 //----------------------------------------------------------------------------
831
832
833 //----------------------------------------------------------------------------
834 int vvSlicer::GetOrientation()
835 {
836   return this->SliceOrientation;
837 }
838 //----------------------------------------------------------------------------
839
840
841 //----------------------------------------------------------------------------
842 void vvSlicer::UpdateDisplayExtent()
843 {
844   vtkImageData *input = this->GetInput();
845   if (!input || !this->ImageActor) {
846     return;
847   }
848   input->UpdateInformation();
849
850   // Local copy of extent
851   int w_ext[6];
852   int* ext = GetExtent();
853   copyExtent(ext, w_ext);
854   // Set slice value
855   int s = this->Slice > ext[this->SliceOrientation*2+1] ? ext[this->SliceOrientation*2 + 1] : this->Slice;
856   w_ext[ this->SliceOrientation*2   ] = s;
857   w_ext[ this->SliceOrientation*2+1 ] = s;
858   
859   // Image actor
860   this->ImageActor->SetDisplayExtent(w_ext);
861   
862   // Overlay image actor
863   if (mOverlay && mOverlayActor->GetVisibility()) {
864     AdjustResliceToSliceOrientation(mOverlayReslice);
865     int overExtent[6];
866     this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
867     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
868     mOverlayActor->SetDisplayExtent( overExtent );
869   }
870
871   // Fusion image actor
872   if (mFusion && mFusionActor->GetVisibility()) {
873     AdjustResliceToSliceOrientation(mFusionReslice);
874     int fusExtent[6];
875     this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
876     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
877     mFusionActor->SetDisplayExtent(fusExtent);
878   }
879
880   // Vector field actor
881   double* camera = Renderer->GetActiveCamera()->GetPosition();
882   double* image_bounds = ImageActor->GetBounds();
883   double position[3] = {0, 0, 0};
884   position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2]; 
885
886   //print_vector<double, 6>("camera", camera);
887   //print_vector<double, 6>("image_bounds", image_bounds);
888   //print_vector<double, 3>("position", position);
889
890   // find where to place the VF actor. to deal with
891   // z-buffer issues, the VF is placed right in front of the image,
892   // subject to a small offset. the position actually depends on the
893   // the location of the camera relative to the image. 
894   double offset = 1;
895   if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
896     offset = -1;
897   
898   if (mVF && mVFActor->GetVisibility()) {
899     int vfExtent[6];
900     mVF->GetVTKImages()[0]->UpdateInformation();
901     this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
902     ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
903     mVOIFilter->SetVOI(vfExtent);
904     int orientation[3] = {1,1,1};
905     orientation[this->SliceOrientation] = 0;
906     mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
907     mVFMapper->Update();
908
909     position[this->SliceOrientation] += offset;
910     mVFActor->SetPosition(position);
911   }
912   
913   // Landmarks actor
914   if (mLandActor) {
915     if (mClipBox) {
916       double bounds [6];
917       for(unsigned int i=0; i<6; i++)
918         bounds[i] = ImageActor->GetBounds()[i];
919       bounds[ this->SliceOrientation*2   ] = ImageActor->GetBounds()[ this->SliceOrientation*2  ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
920       bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
921       mClipBox->SetBounds(bounds);
922       UpdateLandmarks();
923     }
924     
925     position[this->SliceOrientation] = offset;
926     mLandActor->SetPosition(position);
927   }
928
929   // Figure out the correct clipping range
930   if (this->Renderer) {
931     if (this->InteractorStyle &&
932         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
933       this->Renderer->ResetCameraClippingRange();
934     } else {
935       vtkCamera *cam = this->Renderer->GetActiveCamera();
936       if (cam) {
937         double bounds[6];
938         this->ImageActor->GetBounds(bounds);
939         double spos = (double)bounds[this->SliceOrientation * 2];
940         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
941         double range = fabs(spos - cpos);
942         double *spacing = input->GetSpacing();
943         double sumSpacing = spacing[0] + spacing[1] + spacing[2];
944         cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
945       }
946     }
947   }
948   
949 }
950 //----------------------------------------------------------------------------
951
952 //----------------------------------------------------------------------------
953 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
954                                                 vtkImageData *targetImage, int targetExtent[6])
955 {
956   double dExtents[6];
957   for(unsigned int i=0; i<6; i++) {
958     // From source voxel coordinates to world coordinates
959     dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
960
961     // From world coordinates to floating point target voxel coordinates
962     dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
963     
964     // Round to nearest
965     //targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
966     targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
967   }
968 }
969 //----------------------------------------------------------------------------
970
971 //----------------------------------------------------------------------------
972 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
973 {
974   bool out = false;
975   int maxBound = 6;
976
977   //2D overlay on 3D image specific case
978   if (refExtent[4] == refExtent[5]) {
979     maxBound = 4;
980     extent[4] = refExtent[4];
981     extent[5] = refExtent[5];
982   }
983
984   for (int i = 0; i < maxBound; i = i+2) {
985     //if we are totally outside the image
986     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
987       out = true;
988       break;
989     }
990     //crop to the limit of the image
991     extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
992     extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
993     extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
994     extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
995   }
996   if (out)
997     for (int i = 0; i < maxBound; i = i+2) {
998       extent[i] = refExtent[i];
999       extent[i+1] = refExtent[i];
1000     }
1001 }
1002 //----------------------------------------------------------------------------
1003
1004
1005 //----------------------------------------------------------------------------
1006 void vvSlicer::UpdateOrientation()
1007 {
1008   // Set the camera position
1009   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1010   if (cam) {
1011     switch (this->SliceOrientation) {
1012     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1013       cam->SetFocalPoint(0,0,0);
1014       cam->SetPosition(0,0,-1); // -1 if medical ?
1015       cam->SetViewUp(0,-1,0);
1016       break;
1017
1018     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1019       cam->SetFocalPoint(0,0,0);
1020       cam->SetPosition(0,-1,0); // 1 if medical ?
1021       cam->SetViewUp(0,0,1);
1022       break;
1023
1024     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1025       cam->SetFocalPoint(0,0,0);
1026       cam->SetPosition(-1,0,0); // -1 if medical ?
1027       cam->SetViewUp(0,0,1);
1028       break;
1029     }
1030   }
1031 }
1032 //----------------------------------------------------------------------------
1033
1034
1035 //----------------------------------------------------------------------------
1036 void vvSlicer::SetOpacity(double s)
1037 {
1038   this->GetImageActor()->SetOpacity(s);
1039 }
1040 //----------------------------------------------------------------------------
1041
1042
1043 //----------------------------------------------------------------------------
1044 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1045 {
1046   this->Superclass::SetRenderWindow(rw);
1047   this->SetupInteractor(rw->GetInteractor());
1048   ca->SetImageActor(this->GetImageActor());
1049   ca->SetWindowLevel(this->GetWindowLevel());
1050   ca->SetText(2, "<slice>");
1051   ca->SetText(3, "<window>\n<level>");
1052
1053   double bounds[6];
1054   double max = 65000;
1055
1056   bounds[0] = -max;
1057   bounds[1] = max;
1058   bounds[2] = -max;
1059   bounds[3] = max;
1060   bounds[4] = -max;
1061   bounds[5] = max;
1062
1063   crossCursor->SetModelBounds(bounds);
1064   this->GetRenderer()->AddActor(pdmA);
1065   this->GetRenderer()->AddActor(ca);
1066   this->GetRenderer()->ResetCamera();
1067
1068   //this is just a mapping between the labeling of the orientations presented to the user and
1069   //the one used by vtk
1070   SetSliceOrientation(2-(orientation%3));
1071   ResetCamera();
1072 }
1073 //----------------------------------------------------------------------------
1074
1075
1076 //----------------------------------------------------------------------------
1077 void vvSlicer::ResetCamera()
1078 {
1079   this->GetRenderer()->ResetCamera();
1080 }
1081 //----------------------------------------------------------------------------
1082
1083
1084 //----------------------------------------------------------------------------
1085 void vvSlicer::SetDisplayMode(bool i)
1086 {
1087   this->GetRenderer()->SetDraw(i);
1088   if (i)
1089     UpdateDisplayExtent();
1090 }
1091 //----------------------------------------------------------------------------
1092
1093
1094 //----------------------------------------------------------------------------
1095 void vvSlicer::FlipHorizontalView()
1096 {
1097   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1098   if (cam) {
1099     double *position = cam->GetPosition();
1100     double factor[3] = {1, 1, 1};
1101     factor[this->SliceOrientation] = -1;
1102     cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1103     
1104 /*    switch (this->SliceOrientation) {
1105     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1106       cam->SetPosition(position[0],position[1],-position[2]);
1107       break;
1108
1109     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1110       cam->SetPosition(position[0],-position[1],position[2]);
1111       break;
1112
1113     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1114       cam->SetPosition(-position[0],position[1],position[2]);
1115       break;
1116     }*/
1117
1118     this->Renderer->ResetCameraClippingRange();
1119     this->UpdateDisplayExtent();
1120   }
1121 }
1122 //----------------------------------------------------------------------------
1123
1124
1125 //----------------------------------------------------------------------------
1126 void vvSlicer::FlipVerticalView()
1127 {
1128   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1129   if (cam) {
1130     FlipHorizontalView();
1131     double *viewup = cam->GetViewUp();
1132     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1133     this->UpdateDisplayExtent();
1134   }
1135 }
1136 //----------------------------------------------------------------------------
1137
1138
1139 //----------------------------------------------------------------------------
1140 void vvSlicer::SetColorWindow(double window)
1141 {
1142   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1143   if ( LUT ) {
1144     double level = this->GetWindowLevel()->GetLevel();
1145     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1146     LUT->Build();
1147   }
1148   this->vtkImageViewer2::SetColorWindow(window);
1149 }
1150 //----------------------------------------------------------------------------
1151
1152 //----------------------------------------------------------------------------
1153 void vvSlicer::SetColorLevel(double level)
1154 {
1155   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1156   if ( LUT ) {
1157     double window = this->GetWindowLevel()->GetWindow();
1158     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1159     LUT->Build();
1160   }
1161   this->vtkImageViewer2::SetColorLevel(level);
1162 }
1163 //----------------------------------------------------------------------------
1164
1165 //----------------------------------------------------------------------------
1166 double vvSlicer::GetOverlayColorWindow()
1167 {
1168   if(mOverlayMapper)
1169     return mOverlayMapper->GetWindow();
1170   else
1171     return 0.;
1172 }
1173 //----------------------------------------------------------------------------
1174
1175 //----------------------------------------------------------------------------
1176 double vvSlicer::GetOverlayColorLevel()
1177 {
1178   if(mOverlayMapper)
1179     return mOverlayMapper->GetLevel();
1180   else
1181     return 0.;
1182 }
1183 //----------------------------------------------------------------------------
1184
1185 //----------------------------------------------------------------------------
1186 void vvSlicer::SetOverlayColorWindow(double window)
1187 {
1188   mOverlayMapper->SetWindow(window);
1189 }
1190 //----------------------------------------------------------------------------
1191
1192 //----------------------------------------------------------------------------
1193 void vvSlicer::SetOverlayColorLevel(double level)
1194 {
1195   mOverlayMapper->SetLevel(level);
1196 }
1197 //----------------------------------------------------------------------------
1198
1199 //----------------------------------------------------------------------------
1200 // Returns the min an the max value in a 41x41 region around the mouse pointer
1201 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1202 {
1203   //Get mouse pointer position in view coordinates
1204   double corner1[3];
1205   double corner2[3];
1206   for(int i=0; i<3; i++) {
1207     corner1[i] = mCurrent[i];
1208     corner2[i] = mCurrent[i];
1209   }
1210
1211   this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1212   this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1213
1214   // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1215   int w, h;
1216   this->Renderer->GetTiledSize(&w, &h);
1217   corner1[0] -= 0.2*h/(double)w;
1218   corner2[0] += 0.2*h/(double)w;
1219   corner1[1] -= 0.2;
1220   corner2[1] += 0.2;
1221   this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1222   this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1223
1224   //Convert to image pixel coordinates (rounded)
1225   transform->TransformPoint(corner1, corner1);
1226   transform->TransformPoint(corner2, corner2);
1227   int iLocalExtents[6];
1228   for(int i=0; i<3; i++) {
1229     corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1230     corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1231
1232     iLocalExtents[i*2  ] = lrint(corner1[i]);
1233     iLocalExtents[i*2+1] = lrint(corner2[i]);
1234
1235     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1236       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1237   }
1238
1239   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1240   voiFilter->SetInput(image);
1241   voiFilter->SetVOI(iLocalExtents);
1242   voiFilter->Update();
1243   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1244     min = 0;
1245     max = 0;
1246     return;
1247   }
1248
1249   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1250   accFilter->SetInput(voiFilter->GetOutput());
1251   accFilter->Update();
1252
1253   min = *(accFilter->GetMin());
1254   max = *(accFilter->GetMax());
1255 }
1256 //----------------------------------------------------------------------------
1257
1258 //----------------------------------------------------------------------------
1259 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1260 {
1261   ix = lrint(X);
1262   iy = lrint(Y);
1263   iz = lrint(Z);
1264   if (ix < image->GetWholeExtent()[0] ||
1265       ix > image->GetWholeExtent()[1] ||
1266       iy < image->GetWholeExtent()[2] ||
1267       iy > image->GetWholeExtent()[3] ||
1268       iz < image->GetWholeExtent()[4] ||
1269       iz > image->GetWholeExtent()[5] )
1270     return std::numeric_limits<double>::quiet_NaN();
1271
1272   image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1273   image->Update();
1274   return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1275 }
1276 //----------------------------------------------------------------------------
1277
1278 //----------------------------------------------------------------------------
1279 void vvSlicer::Render()
1280 {
1281   if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1282     legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1283     legend->UseOpacityOn();
1284     legend->SetVisibility(1);
1285   }
1286   else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay)  {
1287     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1288     legend->UseOpacityOff();
1289     legend->SetVisibility(1);
1290   } else legend->SetVisibility(0);
1291
1292   if (ca->GetVisibility()) {
1293     std::stringstream worldPos;
1294     double pt[3];
1295     mConcatenatedTransform->TransformPoint(mCurrent, pt);
1296     double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1297     double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1298     double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1299
1300     if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1301         X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1302         Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1303         Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1304         Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1305         Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1306
1307       
1308       int ix, iy, iz;
1309       double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1310
1311       if(ImageActor->GetVisibility())
1312         worldPos << "data value : " << value << std::endl;
1313
1314       worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1315                           << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1316                           << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1317                           << mCurrentTSlice
1318                           << std::endl;
1319       worldPos << "pixel : " << ix << ' '
1320                              << iy << ' '
1321                              << iz << ' '
1322                              << mCurrentTSlice
1323                              << std::endl;
1324     }
1325     ca->SetText(1,worldPos.str().c_str());
1326   }
1327
1328   if (pdmA->GetVisibility()) {
1329     double x = mCursor[0];
1330     double y = mCursor[1];
1331     double z = mCursor[2];
1332     double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1333     double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1334     double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1335
1336     if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1337         xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1338         yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1339         yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1340         zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1341         zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1342       vtkRenderer * renderer = this->Renderer;
1343
1344       renderer->WorldToView(x,y,z);
1345       renderer->ViewToNormalizedViewport(x,y,z);
1346       renderer->NormalizedViewportToViewport(x,y);
1347       renderer->ViewportToNormalizedDisplay(x,y);
1348       renderer->NormalizedDisplayToDisplay(x,y);
1349       crossCursor->SetFocalPoint(x,y,z);
1350     } else
1351       crossCursor->SetFocalPoint(-1,-1,z);
1352   }
1353
1354
1355   if (mOverlay && mOverlayActor->GetVisibility()) {
1356     if(mLinkOverlayWindowLevel) {
1357       mOverlayMapper->SetWindow(this->GetColorWindow());
1358       mOverlayMapper->SetLevel(this->GetColorLevel());
1359     }
1360     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1361     mOverlayMapper->GetOutput()->Update();
1362     mOverlayMapper->Update();
1363   }
1364   if (mLandMapper)
1365     UpdateLandmarks();
1366
1367   this->GetRenderWindow()->Render();
1368 }
1369 //----------------------------------------------------------------------------
1370
1371
1372 //----------------------------------------------------------------------------
1373 void vvSlicer::UpdateCursorPosition()
1374 {
1375   pdmA->SetVisibility(true);
1376   mCursor[0] = mCurrent[0];
1377   mCursor[1] = mCurrent[1];
1378   mCursor[2] = mCurrent[2];
1379   mCursor[3] = mCurrentTSlice;
1380 }
1381 //----------------------------------------------------------------------------
1382
1383
1384 //----------------------------------------------------------------------------
1385 void vvSlicer::UpdateLandmarks()
1386 {
1387   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1388   if (pd->GetPoints()) {
1389     mLandGlyph->SetRange(0,1);
1390     mLandGlyph->Modified();
1391     mLandGlyph->Update();
1392
1393     mClipBox->Modified();
1394     mLandClipper->Update();
1395     mLandMapper->Update();
1396   }
1397
1398 }
1399 //----------------------------------------------------------------------------
1400
1401
1402 //----------------------------------------------------------------------------
1403 void vvSlicer::SetSlice(int slice)
1404 {
1405   int *range = this->GetSliceRange();
1406   if (range) {
1407     if (slice < range[0]) {
1408       slice = range[0];
1409     } else if (slice > range[1]) {
1410       slice = range[1];
1411     }
1412   }
1413
1414   if (this->Slice == slice) {
1415     return;
1416   }
1417
1418   this->Slice = slice;
1419   SetContourSlice();
1420   this->Modified();
1421   this->UpdateDisplayExtent();
1422
1423   // Seems to work without this line
1424   //this->Render();
1425 }
1426 //----------------------------------------------------------------------------
1427
1428
1429 //----------------------------------------------------------------------------
1430 void vvSlicer::SetContourSlice()
1431 {
1432   if (mSurfaceCutActors.size() > 0)
1433     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1434          i!=mSurfaceCutActors.end(); i++) {
1435          
1436       (*i)->SetSlicingOrientation(this->SliceOrientation);
1437       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1438                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1439     }
1440 }
1441 //----------------------------------------------------------------------------
1442
1443
1444 //----------------------------------------------------------------------------
1445 void vvSlicer::ForceUpdateDisplayExtent()
1446 {
1447   this->UpdateDisplayExtent();
1448 }
1449 //----------------------------------------------------------------------------
1450
1451
1452 //----------------------------------------------------------------------------
1453 int* vvSlicer::GetDisplayExtent()
1454 {
1455   return this->GetImageActor()->GetDisplayExtent();
1456 }
1457 //----------------------------------------------------------------------------
1458
1459
1460 //----------------------------------------------------------------------------
1461 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1462 {
1463   this->Superclass::PrintSelf(os, indent);
1464 }
1465 //----------------------------------------------------------------------------
1466
1467 //----------------------------------------------------------------------------
1468 void vvSlicer::SetVFColor(double r, double g, double b)
1469 {
1470   double mVFColorHSV[3];
1471   mVFColor[0] = r;
1472   mVFColor[1] = g;
1473   mVFColor[2] = b;
1474
1475   vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1476   mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1477   mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1478   mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1479
1480   this->Render();
1481 }  
1482