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