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