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