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