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