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