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