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