]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
Debug slowness of scalar range computation
[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 <QMessageBox>
20 #include <QString>
21
22 #include "vvSlicer.h"
23 #include "vvImage.h"
24 #include "vvSlicerManagerCommand.h"
25 #include "vvGlyphSource.h"
26 #include "vvGlyph2D.h"
27
28 #include <vtkVersion.h>
29 #include <vtkExtentTranslator.h>
30 #include <vtkAlgorithm.h>
31 #include <vtkExecutive.h>
32 #include <vtkStreamingDemandDrivenPipeline.h>
33 #include <vtkInformation.h>
34 #include <vtkTextProperty.h>
35 #include <vtkTextActor.h>
36 #include <vtkTextSource.h>
37 #include <vtkActor2D.h>
38 #include <vtkCursor2D.h>
39 #include <vtkPolyDataMapper2D.h>
40 #include <vtkProperty2D.h>
41 #include <vtkCornerAnnotation.h>
42 #include <vtkImageMapToWindowLevelColors.h>
43 #include <vtkImageData.h>
44 #include <vtkImageActor.h>
45 #include <vvBlendImageActor.h>
46 #include <vtkToolkits.h>
47 #include <vtkObjectFactory.h>
48 #include <vtkPointData.h>
49 #include <vtkDataArray.h>
50 #include <vtkFloatArray.h>
51 #include <vtkClipPolyData.h>
52 #include <vtkActor2DCollection.h>
53 #include <vtkGlyph3D.h>
54 #include <vtkMath.h>
55 #include <vtkCursor3D.h>
56 #include <vtkProperty.h>
57 #include <vtkLight.h>
58 #include <vtkLightCollection.h>
59 #include <vtkScalarBarActor.h>
60 #include <vtkLookupTable.h>
61
62 #include <vtkRenderer.h>
63 #include <vtkRendererCollection.h>
64 #include <vtkRenderWindow.h>
65 #include <vtkRenderWindowInteractor.h>
66 #include <vtkCamera.h>
67 #include <vtkCallbackCommand.h>
68 #include <vtkCommand.h>
69 #include <vtkPolyDataMapper.h>
70 #include <vtkBox.h>
71
72 #include <vtkExtractVOI.h>
73 #include <vtkSphereSource.h>
74 #include <vtkCutter.h>
75 #include <vtkAssignAttribute.h>
76 #include <vtkImageAccumulate.h>
77 #include <vtkImageReslice.h>
78 #include <vtkOpenGLImageSliceMapper.h>
79 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
80 #  include <vtkImageMapper3D.h>
81 #  include <vtkImageSliceMapper.h>
82 #endif
83
84 vtkStandardNewMacro(vvSlicer);
85 static void copyExtent(int* in, int* to){
86  for(int i=0; i<6; ++i) 
87  {
88         to[i]=in[i];
89  } 
90 }
91 //------------------------------------------------------------------------------
92 vvSlicer::vvSlicer()
93
94         mFusionSequenceCode = -1;
95   this->UnInstallPipeline();
96   mImage = NULL;
97   mReducedExtent = new int[6];
98   mRegisterExtent = NULL;
99   mCurrentTSlice = 0;
100   mCurrentFusionTSlice = 0;
101   mCurrentOverlayTSlice = 0;
102   mUseReducedExtent = false;
103
104   mCurrent[0] = -VTK_DOUBLE_MAX;
105   mCurrent[1] = -VTK_DOUBLE_MAX;
106   mCurrent[2] = -VTK_DOUBLE_MAX;
107
108   mCursor[0] = 0;//-VTK_DOUBLE_MAX;
109   mCursor[1] = 0;//-VTK_DOUBLE_MAX;
110   mCursor[2] = 0;//-VTK_DOUBLE_MAX;
111   mCursor[3] = 0;//-VTK_DOUBLE_MAX;
112
113   mSubSampling = 5;
114   mScale = 1;
115   mVFLog = 0;
116   mVFWidth = 1;
117   mVFColor[0] = 0;
118   mVFColor[1] = 1;
119   mVFColor[2] = 0;
120
121   crossCursor = vtkSmartPointer<vtkCursor2D>::New();
122   crossCursor->AllOff();
123   crossCursor->AxesOn();
124   crossCursor->SetTranslationMode(1);
125   crossCursor->SetRadius(2);
126
127   pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
128 #if VTK_MAJOR_VERSION <= 5
129   pdm->SetInput(crossCursor->GetOutput());
130 #else
131   pdm->SetInputConnection(crossCursor->GetOutputPort(0));
132 #endif
133
134   pdmA = vtkSmartPointer<vtkActor2D>::New();
135   pdmA->SetMapper(pdm);
136   pdmA->GetProperty()->SetColor(255,10,212);
137   pdmA->SetVisibility(0);
138   pdmA->SetPickable(0);
139
140   ca = vtkSmartPointer<vtkCornerAnnotation>::New();
141   ca->GetTextProperty()->SetColor(255,10,212);
142   ca->SetVisibility(1);
143   mFileName = "";
144
145   mVF = NULL;
146   mOverlay = NULL;
147   mFusion = NULL;
148   mLandmarks = NULL;
149
150   legend = vtkSmartPointer<vtkScalarBarActor>::New();
151   //legend->SetTitle("test!");
152   legend->SetPosition(0.82,0.18);
153   legend->SetWidth(0.1);
154   legend->SetVisibility(0);
155   legend->SetLabelFormat("%.1f");
156   this->GetRenderer()->AddActor(legend);
157   showFusionLegend = false;
158
159   this->InstallPipeline();
160
161   mLinkOverlayWindowLevel = true;
162   mImageVisibility = true;
163
164 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
165   this->GetImageActor()->GetMapper()->BorderOn();
166 #endif
167
168   mSlicingTransform = vtkSmartPointer<vtkTransform>::New();
169   mConcatenatedTransform = vtkSmartPointer<vtkTransform>::New();
170   mConcatenatedFusionTransform = vtkSmartPointer<vtkTransform>::New();
171   mConcatenatedOverlayTransform = vtkSmartPointer<vtkTransform>::New();
172   mFirstSetSliceOrientation = true;
173 }
174 //------------------------------------------------------------------------------
175
176
177 //------------------------------------------------------------------------------
178 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
179
180   return mOverlayMapper.GetPointer();
181 }
182 //------------------------------------------------------------------------------
183
184
185 //------------------------------------------------------------------------------
186 vvBlendImageActor* vvSlicer::GetOverlayActor()
187
188   return mOverlayActor.GetPointer();
189 }
190 //------------------------------------------------------------------------------
191
192
193 //------------------------------------------------------------------------------
194 vtkImageMapToColors* vvSlicer::GetFusionMapper()
195
196   return mFusionMapper.GetPointer();
197 }
198 //------------------------------------------------------------------------------
199
200
201 //------------------------------------------------------------------------------
202 vtkImageActor* vvSlicer::GetFusionActor()
203
204   return mFusionActor.GetPointer();
205 }
206 //------------------------------------------------------------------------------
207
208
209 //------------------------------------------------------------------------------
210 vtkActor* vvSlicer::GetVFActor()
211
212   return mVFActor.GetPointer();
213 }
214 //------------------------------------------------------------------------------
215
216
217 //------------------------------------------------------------------------------
218 vtkCornerAnnotation* vvSlicer::GetAnnotation()
219
220   return ca.GetPointer();
221 }
222 //------------------------------------------------------------------------------
223
224
225 //------------------------------------------------------------------------------
226 void vvSlicer::EnableReducedExtent(bool b)
227
228   mUseReducedExtent = b;
229 }
230 //------------------------------------------------------------------------------
231
232
233 //------------------------------------------------------------------------------
234 void vvSlicer::SetReducedExtent(int * ext)
235
236   copyExtent(ext, mReducedExtent);
237 }
238 //------------------------------------------------------------------------------
239
240
241 //------------------------------------------------------------------------------
242 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
243
244
245   mSurfaceCutActors.push_back(new vvMeshActor());
246   if (propagate)
247     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
248   else
249     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
250   mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
251   this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
252
253   SetContourSlice();
254 }
255 //------------------------------------------------------------------------------
256
257
258 //------------------------------------------------------------------------------
259 void vvSlicer::ToggleContourSuperposition()
260
261   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
262        i!=mSurfaceCutActors.end(); i++)
263     (*i)->ToggleSuperposition();
264 }
265 //------------------------------------------------------------------------------
266
267
268 //------------------------------------------------------------------------------
269 void vvSlicer::SetCursorColor(int r,int g, int b)
270
271   pdmA->GetProperty()->SetColor(r,g,b);
272 }
273 //------------------------------------------------------------------------------
274
275
276 //------------------------------------------------------------------------------
277 void vvSlicer::SetCursorVisibility(bool s)
278
279   pdmA->SetVisibility(s);
280 }
281 //------------------------------------------------------------------------------
282
283
284 //------------------------------------------------------------------------------
285 bool vvSlicer::GetCursorVisibility()
286
287   return pdmA->GetVisibility();
288 }
289 //------------------------------------------------------------------------------
290
291
292 //------------------------------------------------------------------------------
293 void vvSlicer::SetCornerAnnotationVisibility(bool s)
294
295   ca->SetVisibility(s);
296 }
297 //------------------------------------------------------------------------------
298
299
300 //------------------------------------------------------------------------------
301 bool vvSlicer::GetCornerAnnotationVisibility()
302
303   return ca->GetVisibility();
304 }
305 //------------------------------------------------------------------------------
306
307
308 //------------------------------------------------------------------------------
309 vvSlicer::~vvSlicer()
310
311   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
312        i!=mSurfaceCutActors.end(); i++)
313     delete (*i);
314   delete [] mReducedExtent;
315 }
316 //------------------------------------------------------------------------------
317
318 //------------------------------------------------------------------------------
319 double* vvSlicer::GetCurrentPosition()
320
321   return mCurrentBeforeSlicingTransform;
322 }
323 //------------------------------------------------------------------------------
324
325 //------------------------------------------------------------------------------
326 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
327
328   mCurrentBeforeSlicingTransform[0]=x;
329   mCurrentBeforeSlicingTransform[1]=y;
330   mCurrentBeforeSlicingTransform[2]=z;
331   mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
332   if (t>=0) SetTSlice(t);
333 }
334 //------------------------------------------------------------------------------
335
336
337 //------------------------------------------------------------------------------
338 void vvSlicer::SetImage(vvImage::Pointer image)
339
340   if (image->GetVTKImages().size()) {
341     mImage = image;
342
343     if (!mImageReslice) {
344       mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
345       mImageReslice->SetInterpolationModeToLinear();
346       mImageReslice->AutoCropOutputOn();
347       mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
348     }
349
350     mConcatenatedTransform->Identity();
351     mConcatenatedTransform->Concatenate(mImage->GetTransform()[0]);
352     mConcatenatedTransform->Concatenate(mSlicingTransform);
353     mImageReslice->SetResliceTransform(mConcatenatedTransform);
354 #if VTK_MAJOR_VERSION <= 5
355     mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
356 #else
357     mImageReslice->SetInputData(0, mImage->GetFirstVTKImageData());
358 #endif
359     mImageReslice->UpdateInformation();
360
361 #if VTK_MAJOR_VERSION <= 5
362     this->Superclass::SetInput(mImageReslice->GetOutput());
363 #else
364     this->Superclass::SetInputConnection(mImageReslice->GetOutputPort());
365 #endif
366
367     int extent[6];
368 #if VTK_MAJOR_VERSION <= 5
369     this->GetInput()->GetWholeExtent(extent);
370 #else
371     int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
372     copyExtent(ext, extent);
373 #endif
374
375     // Prevent crash when reload -> change slice if outside extent
376     if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
377       Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
378     }
379
380     // Make sure that the required part image has been computed
381     extent[SliceOrientation*2] = Slice;
382     extent[SliceOrientation*2+1] = Slice;    
383 #if VTK_MAJOR_VERSION <= 5
384     mImageReslice->GetOutput()->SetUpdateExtent(extent);
385     mImageReslice->GetOutput()->Update();
386 #else
387     mImageReslice->SetUpdateExtent(extent);
388     mImageReslice->Update();
389 #endif
390
391     this->UpdateDisplayExtent();
392
393     mCurrentTSlice = 0;
394     ca->SetText(0,mFileName.c_str());
395   }
396 }
397 //------------------------------------------------------------------------------
398
399
400 //------------------------------------------------------------------------------
401 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
402
403   if (overlay->GetVTKImages().size()) {
404     mOverlay = overlay;
405     mOverlayVisibility = true;
406     
407     if (!mOverlayReslice) {
408       mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
409       mOverlayReslice->SetInterpolationModeToLinear();
410       mOverlayReslice->AutoCropOutputOn();
411       mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
412     }
413
414     mConcatenatedOverlayTransform->Identity();
415     mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
416     mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
417     mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
418 #if VTK_MAJOR_VERSION <= 5
419     mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
420 #else
421     mOverlayReslice->SetInputData(0, mOverlay->GetFirstVTKImageData());
422 #endif
423     mOverlayReslice->Update();
424
425     if (!mOverlayMapper)
426       mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
427 #if VTK_MAJOR_VERSION <= 5
428     mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
429 #else
430     mOverlayMapper->SetInputConnection(mOverlayReslice->GetOutputPort(0));
431 #endif
432
433     if (!mOverlayActor) {
434       mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
435 #if VTK_MAJOR_VERSION <= 5
436       mOverlayActor->SetInput(mOverlayMapper->GetOutput());
437 #else
438       mOverlayActor->GetMapper()->SetInputConnection(mOverlayMapper->GetOutputPort());
439 #endif
440       mOverlayActor->SetPickable(0);
441       mOverlayActor->SetVisibility(true);
442       mOverlayActor->SetOpacity(0.5);
443 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
444       mOverlayActor->GetMapper()->BorderOn();
445 #endif
446       }
447
448     //stupid but necessary : the Overlay need to be rendered before fusion
449     if (mFusionActor) {
450       this->GetRenderer()->RemoveActor(mFusionActor);
451       this->GetRenderer()->AddActor(mOverlayActor);
452       this->GetRenderer()->AddActor(mFusionActor);
453     } else
454       this->GetRenderer()->AddActor(mOverlayActor);
455
456     //Synchronize orientation and slice
457     AdjustResliceToSliceOrientation(mOverlayReslice);
458     this->UpdateDisplayExtent();
459     this->SetTSlice(mCurrentTSlice);
460   }
461 }
462 //------------------------------------------------------------------------------
463
464
465 //------------------------------------------------------------------------------
466 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
467
468         mFusionSequenceCode = fusionSequenceCode;
469   if (fusion->GetVTKImages().size()) {
470     mFusion = fusion;
471     mFusionVisibility = true;
472
473     if (!mFusionReslice) {
474       mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
475       mFusionReslice->SetInterpolationModeToLinear();
476       mFusionReslice->AutoCropOutputOn();
477       mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
478     }
479
480     mConcatenatedFusionTransform->Identity();
481     mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
482     mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
483     mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
484 #if VTK_MAJOR_VERSION <= 5
485     mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
486 #else
487     mFusionReslice->SetInputData(0, mFusion->GetFirstVTKImageData());
488 #endif
489     mFusionReslice->Update();
490
491     if (!mFusionMapper)
492       mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
493
494     vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
495     lut->SetRange(0, 1);
496     lut->SetValueRange(0, 1);
497     lut->SetSaturationRange(0, 0);
498     lut->Build();
499     mFusionMapper->SetLookupTable(lut);
500 #if VTK_MAJOR_VERSION <= 5
501     mFusionMapper->SetInput(mFusionReslice->GetOutput());
502 #else
503     mFusionMapper->SetInputConnection(mFusionReslice->GetOutputPort(0));
504 #endif
505     
506     if (!mFusionActor) {
507       mFusionActor = vtkSmartPointer<vtkImageActor>::New();
508 #if VTK_MAJOR_VERSION <= 5
509       mFusionActor->SetInput(mFusionMapper->GetOutput());
510 #else
511       mFusionActor->GetMapper()->SetInputConnection(mFusionMapper->GetOutputPort());
512 #endif
513       mFusionActor->SetPickable(0);
514       mFusionActor->SetVisibility(true);
515       mFusionActor->SetOpacity(0.7);
516 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
517       mFusionActor->GetMapper()->BorderOn();
518 #endif
519
520       this->GetRenderer()->AddActor(mFusionActor);
521     }
522
523     //Synchronize orientation and slice
524     AdjustResliceToSliceOrientation(mFusionReslice);
525     this->UpdateDisplayExtent();
526     this->SetTSlice(mCurrentTSlice);
527   }
528 }
529 //------------------------------------------------------------------------------
530
531
532 //------------------------------------------------------------------------------
533 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
534
535   bool vis = false;
536   if (actor_type == "image")
537     vis = mImageVisibility;
538   else if (actor_type == "vector")
539     vis = mVFVisibility;
540   else if (actor_type == "overlay")
541     vis = mOverlayVisibility;
542   else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
543     vis = mFusionVisibility;
544   else if (actor_type == "contour")
545     vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
546   return vis;
547 }
548 //------------------------------------------------------------------------------
549
550 //------------------------------------------------------------------------------
551 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
552
553   if (actor_type == "image")
554     mImageVisibility = vis;
555   else if (actor_type == "vector")
556     mVFVisibility = vis;
557   else if (actor_type == "overlay")
558     mOverlayVisibility = vis;
559   else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
560     mFusionVisibility = vis;
561   else if (actor_type == "contour")
562     this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
563   UpdateDisplayExtent();
564 }
565 //------------------------------------------------------------------------------
566
567 //------------------------------------------------------------------------------
568 void vvSlicer::SetVF(vvImage::Pointer vf)
569
570   if (vf->GetVTKImages().size()) {
571     mVF = vf;
572     mVFVisibility = true;
573
574     if (!mAAFilter) {
575       mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
576       mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
577       mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
578     }
579 #if VTK_MAJOR_VERSION <= 5
580     mVOIFilter->SetInput(vf->GetFirstVTKImageData());
581     mAAFilter->SetInput(mVOIFilter->GetOutput());
582 #else
583     mVOIFilter->SetInputData(vf->GetFirstVTKImageData());
584     mAAFilter->SetInputConnection(mVOIFilter->GetOutputPort());
585 #endif
586     ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
587     mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
588
589     if (!mArrow)
590       mArrow = vtkSmartPointer<vvGlyphSource>::New();
591     mArrow->SetGlyphTypeToSpecificArrow();
592     mArrow->SetScale(mScale);
593     mArrow->FilledOff();
594
595     // Glyph the gradient vector (with arrows)
596     if (!mGlyphFilter)
597       mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
598 #if VTK_MAJOR_VERSION <= 5
599     mGlyphFilter->SetInput(mAAFilter->GetOutput());
600     mGlyphFilter->SetSource(mArrow->GetOutput());
601 #else
602     mGlyphFilter->SetInputConnection(mAAFilter->GetOutputPort());
603     mGlyphFilter->SetSourceConnection(mArrow->GetOutputPort());
604 #endif
605     mGlyphFilter->ScalingOn();
606     mGlyphFilter->SetScaleModeToScaleByVector();
607     mGlyphFilter->OrientOn();
608     mGlyphFilter->SetVectorModeToUseVector();
609     mGlyphFilter->SetColorModeToColorByVector();
610
611     if (!mVFColorLUT)
612       mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
613
614     double mVFColorHSV[3];
615     vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
616     mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
617     mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
618     mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
619
620     if (!mVFMapper)
621       mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
622 #if VTK_MAJOR_VERSION <= 5
623     mVFMapper->SetInput(mGlyphFilter->GetOutput());
624 #else
625     mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
626 #endif
627     mVFMapper->ImmediateModeRenderingOn();
628     mVFMapper->SetLookupTable(mVFColorLUT);
629
630     if (!mVFActor)
631       mVFActor = vtkSmartPointer<vtkActor>::New();
632     mVFActor->SetMapper(mVFMapper);
633     mVFActor->SetPickable(0);
634     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
635     this->UpdateDisplayExtent();
636     this->GetRenderer()->AddActor(mVFActor);
637
638     //Synchronize slice
639     SetTSlice(mCurrentTSlice);
640   }
641 }
642 //------------------------------------------------------------------------------
643
644
645 //------------------------------------------------------------------------------
646 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
647
648   mLandmarks = landmarks;
649   if (landmarks) {
650
651     if (!mCross)
652       mCross = vtkSmartPointer<vtkCursor3D>::New();
653         if (!mClipBox)
654       mClipBox = vtkSmartPointer<vtkBox>::New();
655     if (!mLandClipper)
656       mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
657     if (!mLandGlyph)
658       mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
659     if (!mLandMapper)
660       mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
661     if (!mLandActor)
662       mLandActor = vtkSmartPointer<vtkActor>::New();
663
664     mCross->SetFocalPoint(0.0,0.0,0.0);
665     mCross->SetModelBounds(-10,10,-10,10,-10,10);
666     mCross->AllOff();
667     mCross->AxesOn();
668
669     mLandClipper->SetClipFunction(mClipBox);
670     mLandClipper->InsideOutOn();
671 #if VTK_MAJOR_VERSION <= 5
672     mLandClipper->SetInput(mLandmarks->GetOutput());
673
674     mLandGlyph->SetSource(mCross->GetOutput());
675     mLandGlyph->SetInput(mLandClipper->GetOutput());
676 #else
677     mLandClipper->SetInputData(mLandmarks->GetOutput());
678
679     mLandGlyph->SetSourceConnection(mCross->GetOutputPort());
680     mLandGlyph->SetInputConnection(mLandClipper->GetOutputPort());
681 #endif
682     //mLandGlyph->SetIndexModeToScalar();
683     //mLandGlyph->SetRange(0,1);
684     //mLandGlyph->ScalingOff();
685
686     //mLandGlyph->SetColorModeToColorByScalar();
687     
688     mLandGlyph->SetScaleModeToDataScalingOff();
689     mLandGlyph->SetIndexModeToOff();
690
691     mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
692     //mLandMapper->ScalarVisibilityOff();
693
694     mLandActor->SetMapper(mLandMapper);
695     mLandActor->GetProperty()->SetOpacity(0.995);
696     mLandActor->GetProperty()->SetColor(255,10,212);
697     mLandActor->SetPickable(0);
698     mLandActor->SetVisibility(true);
699     this->UpdateDisplayExtent();
700     this->GetRenderer()->AddActor(mLandActor);
701   }
702 }
703 //------------------------------------------------------------------------------
704
705 //------------------------------------------------------------------------------
706 //FIXME: this function leaks memory, we should fix it someday :)
707 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
708
709   if (actor_type == "vector") {
710     Renderer->RemoveActor(mVFActor);
711     mGlyphFilter=NULL;
712     mVF = NULL;
713     mArrow = NULL;
714     mAAFilter=NULL;
715     mVOIFilter = NULL;
716     mVFMapper = NULL;
717     mVFActor = NULL;
718   }
719   if (actor_type == "overlay") {
720     Renderer->RemoveActor(mOverlayActor);
721     mOverlay = NULL;
722     mOverlayActor = NULL;
723     mOverlayMapper = NULL;
724   }
725   if ( (actor_type == "fusion") || (actor_type == "fusionSequence") ) {
726     Renderer->RemoveActor(mFusionActor);
727     mFusion = NULL;
728     mFusionActor = NULL;
729     mFusionMapper = NULL;
730   }
731   if (actor_type == "contour") {
732     Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
733     mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
734   }
735 }
736 //------------------------------------------------------------------------------
737
738
739 //------------------------------------------------------------------------------
740 void vvSlicer::SetVFSubSampling(int sub)
741
742   if (mVOIFilter) {
743     mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
744     mSubSampling = sub;
745   }
746   UpdateDisplayExtent();
747   Render();
748 }
749 //------------------------------------------------------------------------------
750
751
752 //------------------------------------------------------------------------------
753 void vvSlicer::SetVFScale(int scale)
754
755   mScale = scale;
756   if (mArrow)
757     mArrow->SetScale(mScale);
758   UpdateDisplayExtent();
759   Render();
760 }
761 //------------------------------------------------------------------------------
762
763 //------------------------------------------------------------------------------
764 void vvSlicer::SetVFWidth(int width)
765
766   mVFWidth = width;
767   if (mVFActor)
768     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
769   UpdateDisplayExtent();
770   Render();
771 }
772 //------------------------------------------------------------------------------
773
774
775 //------------------------------------------------------------------------------
776 void vvSlicer::SetVFLog(int log)
777
778   mVFLog = log;
779   if (mGlyphFilter) {
780     mGlyphFilter->SetUseLog(mVFLog);
781     mGlyphFilter->Modified();
782   }
783   UpdateDisplayExtent();
784   Render();
785 }
786 //------------------------------------------------------------------------------
787
788
789 //------------------------------------------------------------------------------
790 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
791
792         if (!updateLinkedImages) {
793                 mCurrentTSlice = t;
794 #if VTK_MAJOR_VERSION <= 5
795                 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
796 #else
797                 mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
798 #endif
799                 // Update transform
800                 mConcatenatedTransform->Identity();
801                 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
802                 mConcatenatedTransform->Concatenate(mSlicingTransform);
803                 UpdateDisplayExtent();
804                 return;
805         }
806   if (t < 0)
807     mCurrentTSlice = 0;
808   else if ((unsigned int)t >= mImage->GetVTKImages().size())
809     mCurrentTSlice = mImage->GetVTKImages().size() -1;
810   else
811     mCurrentTSlice = t;
812
813   // Update transform
814   mConcatenatedTransform->Identity();
815   mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
816   mConcatenatedTransform->Concatenate(mSlicingTransform);
817
818   // Update image data
819 #if VTK_MAJOR_VERSION <= 5
820    mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
821 #else
822   mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
823 #endif
824   if (mVF && mVFActor->GetVisibility()) {
825     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
826 #if VTK_MAJOR_VERSION <= 5
827       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
828 #else
829       mVOIFilter->SetInputData(mVF->GetVTKImages()[mCurrentTSlice]);
830 #endif
831   }
832   //update the overlay
833   if (mOverlay && mOverlayActor->GetVisibility()) {
834     if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
835       mCurrentOverlayTSlice = t;
836 #if VTK_MAJOR_VERSION <= 5
837       mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
838 #else
839       mOverlayReslice->SetInputData( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
840 #endif
841       // Update overlay transform
842       mConcatenatedOverlayTransform->Identity();
843       mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
844       mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
845     }
846   }
847   //update the fusion ; except in case this is a fusionSequence, in which case both 'times' should be independent.
848   if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode<0)) {
849     if (mFusion->GetVTKImages().size() > (unsigned int)t) {
850       mCurrentFusionTSlice = t;
851 #if VTK_MAJOR_VERSION <= 5
852       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
853 #else
854       mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
855 #endif
856
857       // Update fusion transform
858       mConcatenatedFusionTransform->Identity();
859       mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
860       mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
861     }
862   }
863   if (mSurfaceCutActors.size() > 0)
864     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
865          i!=mSurfaceCutActors.end(); i++)
866       (*i)->SetTimeSlice(mCurrentTSlice);
867   UpdateDisplayExtent();
868 }
869 //------------------------------------------------------------------------------
870
871
872 //------------------------------------------------------------------------------
873 void vvSlicer::SetFusionSequenceTSlice(int t)
874
875   if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode>=0)) {
876     if (mFusion->GetVTKImages().size() > (unsigned int)t) {
877       mCurrentFusionTSlice = t;
878 #if VTK_MAJOR_VERSION <= 5
879       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
880 #else
881       mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
882 #endif
883       // Update fusion transform
884       mConcatenatedFusionTransform->Identity();
885       mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]); //not really useful...
886       mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
887     }
888   }
889
890   UpdateDisplayExtent();
891 }
892 //------------------------------------------------------------------------------
893
894
895 //------------------------------------------------------------------------------
896 int vvSlicer::GetTSlice()
897
898   return mCurrentTSlice;
899 }
900 //------------------------------------------------------------------------------
901
902 //------------------------------------------------------------------------------
903 int vvSlicer::GetMaxCurrentTSlice()
904
905   int t = mCurrentTSlice;
906   if(mOverlay)
907     t = std::max(t, mCurrentOverlayTSlice);
908   if(mFusion&& (mFusionSequenceCode<0)) //ignore fusionSequence data: for these, the times are not to be related (this way)
909     t = std::max(t, mCurrentFusionTSlice);
910   return t;
911 }
912 //------------------------------------------------------------------------------
913
914 //------------------------------------------------------------------------------
915 int vvSlicer::GetFusionTSlice()
916
917   return mCurrentFusionTSlice;
918 }
919 //------------------------------------------------------------------------------
920
921 //------------------------------------------------------------------------------
922 int vvSlicer::GetOverlayTSlice()
923
924   return mCurrentOverlayTSlice;
925 }
926 //------------------------------------------------------------------------------
927
928 //------------------------------------------------------------------------------
929 void vvSlicer::SetSliceOrientation(int orientation)
930
931   //if 2D image, force to watch in Axial View
932   int extent[6];
933 #if VTK_MAJOR_VERSION <= 5
934     this->GetInput()->GetWholeExtent(extent);
935 #else
936     int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
937     copyExtent(ext, extent);
938 #endif
939
940   if (extent[5]-extent[4] <= 2)
941     orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
942
943   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
944       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
945     vtkErrorMacro("Error - invalid slice orientation " << orientation);
946     return;
947   }
948   
949   this->SliceOrientation = orientation;
950
951   if(mFusion)
952     AdjustResliceToSliceOrientation(mFusionReslice);
953
954   if(mOverlay)
955     AdjustResliceToSliceOrientation(mOverlayReslice);
956
957   // Update the viewer
958   
959   // Go to current cursor position
960   // double* cursorPos = GetCursorPosition();
961   // DDV(cursorPos, 3);
962   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
963
964   if (mFirstSetSliceOrientation) {
965     int *range = this->GetSliceRange();
966     if (range)
967       this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
968 #if VTK_MAJOR_VERSION <= 5
969       mFirstSetSliceOrientation = false;
970 #endif
971   }
972   else if (this->Renderer && this->GetInput()) {
973     double s = mCursor[orientation];
974     double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
975     this->Slice = static_cast<int>(sCursor);
976   }
977
978   this->UpdateOrientation();
979   
980   this->UpdateDisplayExtent();
981   
982   if (mFirstSetSliceOrientation) {
983     mFirstSetSliceOrientation = false;
984   }
985   
986   if (this->Renderer && this->GetInput()) {
987     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
988     this->Renderer->ResetCamera();
989     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
990   }
991
992   SetContourSlice();
993 }
994 //----------------------------------------------------------------------------
995
996 //------------------------------------------------------------------------------
997 // This function ensures that we sample the slices of a vtkImageReslice filter
998 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
999 // In other words, we change the grid of the reslice in the same way as the grid
1000 // of the displayed image in the slicing direction.
1001 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
1002
1003   // Reset autocrop and update output information
1004   reslice->SetOutputOriginToDefault();
1005   reslice->SetOutputSpacingToDefault();
1006 #if VTK_MAJOR_VERSION <= 5
1007   reslice->GetOutput()->UpdateInformation();
1008 #else
1009   reslice->UpdateInformation();
1010 #endif
1011
1012   // Ge new origin / spacing
1013   double origin[3];
1014   double spacing[3];
1015   reslice->GetOutput()->GetOrigin(origin);
1016   reslice->GetOutput()->GetSpacing(spacing);
1017
1018   // Use similar spacing as the image in the direction SliceOrientation
1019   spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1020
1021   // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
1022   // Step 1: from world coordinates to image coordinates
1023   origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1024   origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1025
1026   // Step 2: round to nearest grid positionInc. This has been validated as the only
1027   // way to have something consistent with the thickness of a 2D slice visible on the
1028   // other slices. The thickness is accounted for so if the 2D slice is to thin and
1029   // between two slices, one will never be able to see this 2D slice (bug #1883).
1030   origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
1031
1032   // Step 3: back to world coordinates
1033   origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1034   origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1035
1036   // Set new spacing and origin
1037   reslice->SetOutputOrigin(origin);
1038   reslice->SetOutputSpacing(spacing);
1039   reslice->UpdateInformation();
1040 }
1041 //------------------------------------------------------------------------------
1042
1043 //----------------------------------------------------------------------------
1044 int * vvSlicer::GetExtent()
1045
1046   int *w_ext;
1047   if (mUseReducedExtent) {
1048     w_ext = mReducedExtent;
1049   }
1050   else {
1051 #if VTK_MAJOR_VERSION <= 5
1052     w_ext = GetInput()->GetWholeExtent();
1053 #else
1054     w_ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1055 #endif
1056   }
1057   return w_ext;
1058 }
1059 //----------------------------------------------------------------------------
1060
1061
1062 //----------------------------------------------------------------------------
1063 int vvSlicer::GetOrientation()
1064
1065   return this->SliceOrientation;
1066 }
1067 //----------------------------------------------------------------------------
1068
1069
1070 //----------------------------------------------------------------------------
1071 void vvSlicer::UpdateDisplayExtent()
1072
1073   vtkImageData *input = this->GetInput();
1074   if (!input || !this->ImageActor) {
1075     return;
1076   }
1077
1078 #if VTK_MAJOR_VERSION <= 5
1079   input->UpdateInformation();
1080 #else
1081   mRegisterExtent = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1082 #endif
1083   this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
1084
1085   // Local copy of extent
1086   int w_ext[6];
1087   int* ext = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1088   copyExtent(ext, w_ext);
1089   if (mUseReducedExtent) {
1090         copyExtent(mReducedExtent, w_ext);
1091     }
1092   // Set slice value
1093
1094   w_ext[ this->SliceOrientation*2   ] = this->Slice;
1095   w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
1096   
1097   // Image actor
1098   this->ImageActor->SetVisibility(mImageVisibility);
1099   this->ImageActor->SetDisplayExtent(w_ext);
1100   
1101 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1102   // Fix for bug #1882
1103   dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
1104 #endif
1105
1106   // Overlay image actor
1107   if (mOverlay && mOverlayVisibility) {
1108     AdjustResliceToSliceOrientation(mOverlayReslice);
1109     int overExtent[6];
1110     this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mOverlayReslice->GetOutput(), overExtent);
1111 #if VTK_MAJOR_VERSION <= 5
1112     bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
1113 #else
1114     bool out = ClipDisplayedExtent(overExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1115 #endif
1116     mOverlayActor->SetVisibility(!out);
1117     mOverlayActor->SetDisplayExtent( overExtent );
1118 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1119     // Fix for bug #1882
1120     dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
1121 #endif
1122   }
1123   else if(mOverlay)
1124     mOverlayActor->SetVisibility(false);
1125
1126   // Fusion image actor
1127   if (mFusion && mFusionVisibility) {
1128     AdjustResliceToSliceOrientation(mFusionReslice);
1129     int fusExtent[6];
1130     this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mFusionReslice->GetOutput(), fusExtent);
1131 #if VTK_MAJOR_VERSION <= 5
1132     bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
1133 #else
1134     bool out = ClipDisplayedExtent(fusExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1135 #endif
1136     mFusionActor->SetVisibility(!out);
1137     mFusionActor->SetDisplayExtent( fusExtent );
1138 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1139     // Fix for bug #1882
1140     dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1141 #endif
1142   }
1143   else if(mFusion)
1144     mFusionActor->SetVisibility(false);
1145   // Vector field actor
1146   double* camera = Renderer->GetActiveCamera()->GetPosition();
1147   double* image_bounds = ImageActor->GetBounds();
1148   double position[3] = {0, 0, 0};
1149   position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2]; 
1150
1151   //print_vector<double, 6>("camera", camera);
1152   //print_vector<double, 6>("image_bounds", image_bounds);
1153   //print_vector<double, 3>("position", position);
1154
1155   // find where to place the VF actor. to deal with
1156   // z-buffer issues, the VF is placed right in front of the image,
1157   // subject to a small offset. the position actually depends on the
1158   // the location of the camera relative to the image. 
1159   double offset = 1;
1160   if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1161     offset = -1;
1162   
1163   if (mVF && mVFVisibility) {
1164     int vfExtent[6];
1165 #if VTK_MAJOR_VERSION <= 5
1166     mVF->GetVTKImages()[0]->UpdateInformation();
1167 #else
1168     //this->UpdateInformation();
1169 #endif
1170     this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mVF->GetVTKImages()[0], vfExtent);
1171 #if VTK_MAJOR_VERSION <= 5
1172     bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1173 #else
1174     bool out = ClipDisplayedExtent(vfExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1175 #endif
1176     mVFActor->SetVisibility(!out);
1177     mVOIFilter->SetVOI(vfExtent);
1178     int orientation[3] = {1,1,1};
1179     orientation[this->SliceOrientation] = 0;
1180     mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1181     position[this->SliceOrientation] += offset;
1182     mVFActor->SetPosition(position);
1183     mVFActor->GetProperty()->SetOpacity(0.995);
1184     mVFMapper->Update();
1185
1186   }
1187   else if(mVF)
1188     mVFActor->SetVisibility(false);
1189
1190   // Landmarks actor
1191   if (mLandActor) {
1192     if (mClipBox) {
1193       double bounds [6];
1194       for(unsigned int i=0; i<6; i++)
1195         bounds[i] = ImageActor->GetBounds()[i];
1196       bounds[ this->SliceOrientation*2   ] = ImageActor->GetBounds()[ this->SliceOrientation*2  ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1197       bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1198       mClipBox->SetBounds(bounds);
1199       UpdateLandmarks();
1200     }
1201     
1202     position[this->SliceOrientation] = offset;
1203     mLandActor->SetPosition(position);
1204   }
1205
1206   // Figure out the correct clipping range
1207   if (this->Renderer) {
1208     if (this->InteractorStyle &&
1209         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1210       this->Renderer->ResetCameraClippingRange();
1211     } else {
1212       vtkCamera *cam = this->Renderer->GetActiveCamera();
1213       if (cam) {
1214         double bounds[6];
1215         this->ImageActor->GetBounds(bounds);
1216         double spos = (double)bounds[this->SliceOrientation * 2];
1217         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1218         double range = fabs(spos - cpos);
1219         double *spacing = input->GetSpacing();
1220         double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1221         cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1222       }
1223     }
1224   }
1225 }
1226 //----------------------------------------------------------------------------
1227
1228 //----------------------------------------------------------------------------
1229 void vvSlicer::ConvertImageToImageDisplayExtent(vtkInformation *sourceImage, const int sourceExtent[6],
1230                                                 vtkImageData *targetImage, int targetExtent[6])
1231
1232   double dExtents[6];
1233   double *origin, *spacing;
1234   origin = sourceImage->Get(vtkDataObject::ORIGIN());
1235   spacing = sourceImage->Get(vtkDataObject::SPACING());
1236   for(unsigned int i=0; i<6; i++) {
1237     // From source voxel coordinates to world coordinates
1238     dExtents[i] = origin[i/2] + spacing[i/2] * sourceExtent[i];
1239
1240     // From world coordinates to floating point target voxel coordinates
1241     dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1242     
1243     // Round to current slice or larger extent
1244     if(i/2==this->GetOrientation())
1245       targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1246     else if(i%2==1)
1247       targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1248     else
1249       targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1250   }
1251 }
1252 //----------------------------------------------------------------------------
1253
1254 //----------------------------------------------------------------------------
1255 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1256
1257   bool out = false;
1258   int maxBound = 6;
1259
1260   for (int i = 0; i < maxBound; i = i+2) {
1261     //if we are totally outside the image
1262     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1263       out = true;
1264       break;
1265     }
1266     //crop to the limit of the image
1267     extent[i] = std::max(extent[i], refExtent[i]);
1268     extent[i] = std::min(extent[i], refExtent[i+1]);;
1269     extent[i+1] = std::max(extent[i+1], refExtent[i]);
1270     extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1271   }
1272   if (out)
1273     for (int i = 0; i < maxBound; i = i+2) {
1274       extent[i] = refExtent[i];
1275       extent[i+1] = refExtent[i];
1276     }
1277   return out;
1278 }
1279 //----------------------------------------------------------------------------
1280
1281
1282 //----------------------------------------------------------------------------
1283 void vvSlicer::UpdateOrientation()
1284
1285   // Set the camera position
1286   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1287   if (cam) {
1288     switch (this->SliceOrientation) {
1289     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1290       cam->SetFocalPoint(0,0,0);
1291       cam->SetPosition(0,0,-1); // -1 if medical ?
1292       cam->SetViewUp(0,-1,0);
1293       break;
1294
1295     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1296       cam->SetFocalPoint(0,0,0);
1297       cam->SetPosition(0,-1,0); // 1 if medical ?
1298       cam->SetViewUp(0,0,1);
1299       break;
1300
1301     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1302       cam->SetFocalPoint(0,0,0);
1303       cam->SetPosition(-1,0,0); // -1 if medical ?
1304       cam->SetViewUp(0,0,1);
1305       break;
1306     }
1307   }
1308 }
1309 //----------------------------------------------------------------------------
1310
1311
1312 //----------------------------------------------------------------------------
1313 void vvSlicer::SetOpacity(double s)
1314
1315   this->GetImageActor()->SetOpacity(s);
1316 }
1317 //----------------------------------------------------------------------------
1318
1319
1320 //----------------------------------------------------------------------------
1321 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1322
1323   this->Superclass::SetRenderWindow(rw);
1324   this->SetupInteractor(rw->GetInteractor());
1325   ca->SetImageActor(this->GetImageActor());
1326   ca->SetWindowLevel(this->GetWindowLevel());
1327   ca->SetText(3, "<window>\n<level>");
1328
1329   double bounds[6];
1330   double max = 65000;
1331
1332   bounds[0] = -max;
1333   bounds[1] = max;
1334   bounds[2] = -max;
1335   bounds[3] = max;
1336   bounds[4] = -max;
1337   bounds[5] = max;
1338   crossCursor->SetModelBounds(bounds);
1339
1340   this->GetRenderer()->AddActor(pdmA);
1341   this->GetRenderer()->AddActor(ca);
1342   this->GetRenderer()->ResetCamera();
1343
1344   //this is just a mapping between the labeling of the orientations presented to the user and
1345   //the one used by vtk
1346   SetSliceOrientation(2-(orientation%3));
1347   ResetCamera();
1348 }
1349 //----------------------------------------------------------------------------
1350
1351
1352 //----------------------------------------------------------------------------
1353 void vvSlicer::ResetCamera()
1354
1355   this->GetRenderer()->ResetCamera();
1356 }
1357 //----------------------------------------------------------------------------
1358
1359
1360 //----------------------------------------------------------------------------
1361 void vvSlicer::SetDisplayMode(bool i)
1362
1363         this->GetRenderer()->SetDraw(i);
1364         if (i) UpdateDisplayExtent();
1365 }
1366 //----------------------------------------------------------------------------
1367
1368
1369 //----------------------------------------------------------------------------
1370 void vvSlicer::FlipHorizontalView()
1371
1372   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1373   if (cam) {
1374     double *position = cam->GetPosition();
1375     double factor[3] = {1, 1, 1};
1376     factor[this->SliceOrientation] = -1;
1377     cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1378     
1379 /*    switch (this->SliceOrientation) {
1380     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1381       cam->SetPosition(position[0],position[1],-position[2]);
1382       break;
1383
1384     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1385       cam->SetPosition(position[0],-position[1],position[2]);
1386       break;
1387
1388     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1389       cam->SetPosition(-position[0],position[1],position[2]);
1390       break;
1391     }*/
1392
1393     this->Renderer->ResetCameraClippingRange();
1394     this->UpdateDisplayExtent();
1395   }
1396 }
1397 //----------------------------------------------------------------------------
1398
1399
1400 //----------------------------------------------------------------------------
1401 void vvSlicer::FlipVerticalView()
1402
1403   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1404   if (cam) {
1405     FlipHorizontalView();
1406     double *viewup = cam->GetViewUp();
1407     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1408     this->UpdateDisplayExtent();
1409   }
1410 }
1411 //----------------------------------------------------------------------------
1412
1413
1414 //----------------------------------------------------------------------------
1415 void vvSlicer::SetColorWindow(double window)
1416
1417   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1418   if ( LUT ) {
1419     double level = this->GetWindowLevel()->GetLevel();
1420     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1421     LUT->Build();
1422   }
1423   this->vtkImageViewer2::SetColorWindow(window);
1424 }
1425 //----------------------------------------------------------------------------
1426
1427 //----------------------------------------------------------------------------
1428 void vvSlicer::SetColorLevel(double level)
1429
1430   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1431   if ( LUT ) {
1432     double window = this->GetWindowLevel()->GetWindow();
1433     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1434     LUT->Build();
1435   }
1436   this->vtkImageViewer2::SetColorLevel(level);
1437 }
1438 //----------------------------------------------------------------------------
1439
1440 //----------------------------------------------------------------------------
1441 double vvSlicer::GetOverlayColorWindow()
1442
1443   if(mOverlayMapper)
1444     return mOverlayMapper->GetWindow();
1445   else
1446     return 0.;
1447 }
1448 //----------------------------------------------------------------------------
1449
1450 //----------------------------------------------------------------------------
1451 double vvSlicer::GetOverlayColorLevel()
1452
1453   if(mOverlayMapper)
1454     return mOverlayMapper->GetLevel();
1455   else
1456     return 0.;
1457 }
1458 //----------------------------------------------------------------------------
1459
1460 //----------------------------------------------------------------------------
1461 void vvSlicer::SetOverlayColorWindow(double window)
1462
1463   if(mOverlayMapper)
1464     mOverlayMapper->SetWindow(window);
1465 }
1466 //----------------------------------------------------------------------------
1467
1468 //----------------------------------------------------------------------------
1469 void vvSlicer::SetOverlayColorLevel(double level)
1470
1471   if(mOverlayMapper)
1472     mOverlayMapper->SetLevel(level);
1473 }
1474 //----------------------------------------------------------------------------
1475
1476 //----------------------------------------------------------------------------
1477 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1478 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1479
1480   //Get mouse pointer position in view coordinates
1481   double corner1[3];
1482   double corner2[3];
1483   for(int i=0; i<3; i++) {
1484     corner1[i] = mCurrent[i];
1485     corner2[i] = mCurrent[i];
1486   }
1487
1488   this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1489   this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1490
1491   // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1492   int w, h;
1493   this->Renderer->GetTiledSize(&w, &h);
1494   corner1[0] -= 0.2*h/(double)w;
1495   corner2[0] += 0.2*h/(double)w;
1496   corner1[1] -= 0.2;
1497   corner2[1] += 0.2;
1498   this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1499   this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1500
1501   //Convert to image pixel coordinates (rounded)
1502   transform->TransformPoint(corner1, corner1);
1503   transform->TransformPoint(corner2, corner2);
1504   int iLocalExtents[6];
1505   for(int i=0; i<3; i++) {
1506     corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1507     corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1508
1509     iLocalExtents[i*2  ] = lrint(corner1[i]);
1510     iLocalExtents[i*2+1] = lrint(corner2[i]);
1511
1512     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1513       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1514   }
1515
1516   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1517 #if VTK_MAJOR_VERSION <= 5
1518   voiFilter->SetInput(image);
1519 #else
1520   voiFilter->SetInputData(image);
1521 #endif
1522   voiFilter->SetVOI(iLocalExtents);
1523   voiFilter->Update();
1524   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1525     min = 0;
1526     max = 0;
1527     return;
1528   }
1529
1530   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1531 #if VTK_MAJOR_VERSION <= 5
1532   accFilter->SetInput(voiFilter->GetOutput());
1533 #else
1534   accFilter->SetInputConnection(voiFilter->GetOutputPort(0));
1535 #endif
1536   accFilter->Update();
1537
1538   min = *(accFilter->GetMin());
1539   max = *(accFilter->GetMax());
1540 }
1541 //----------------------------------------------------------------------------
1542
1543 //----------------------------------------------------------------------------
1544 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1545
1546   ix = lrint(X);
1547   iy = lrint(Y);
1548   iz = lrint(Z);
1549 #if VTK_MAJOR_VERSION <= 5
1550   if (ix < image->GetWholeExtent()[0] ||
1551       ix > image->GetWholeExtent()[1] ||
1552       iy < image->GetWholeExtent()[2] ||
1553       iy > image->GetWholeExtent()[3] ||
1554       iz < image->GetWholeExtent()[4] ||
1555       iz > image->GetWholeExtent()[5] )
1556     return std::numeric_limits<double>::quiet_NaN();
1557   image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1558   image->Update();
1559 #else
1560   if (ix < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0] ||
1561       ix > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1] ||
1562       iy < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2] ||
1563       iy > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3] ||
1564       iz < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4] ||
1565       iz > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5] )
1566     return std::numeric_limits<double>::quiet_NaN();
1567   //image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1568   //image->Update();
1569 #endif
1570
1571   return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1572 }
1573 //----------------------------------------------------------------------------
1574
1575 //----------------------------------------------------------------------------
1576 void vvSlicer::Render()
1577
1578   if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1579     legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1580     legend->UseOpacityOn();
1581     legend->SetVisibility(1);
1582   }
1583   else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay)  {
1584     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1585     legend->UseOpacityOff();
1586     legend->SetVisibility(1);
1587   } else legend->SetVisibility(0);
1588
1589   if (ca->GetVisibility()) {
1590
1591     std::stringstream worldPos(" ");
1592     double pt[3];
1593     mConcatenatedTransform->TransformPoint(mCurrent, pt);
1594     double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1595     double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1596     double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1597 #if VTK_MAJOR_VERSION <= 5
1598     if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1599         X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1600         Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1601         Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1602         Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1603         Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1604
1605       
1606       int ix, iy, iz;
1607       double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1608
1609       if(ImageActor->GetVisibility())
1610         worldPos << "data value : " << value << std::endl;
1611
1612       worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1613                           << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1614                           << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1615                           << mCurrentTSlice
1616                           << std::endl;
1617       worldPos << "pixel : " << ix << ' '
1618                              << iy << ' '
1619                              << iz << ' '
1620                              << mCurrentTSlice
1621                              << std::endl;
1622     }
1623 #else
1624     if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0]-0.5 &&
1625         X <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1]+0.5 &&
1626         Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2]-0.5 &&
1627         Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3]+0.5 &&
1628         Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4]-0.5 &&
1629         Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5]+0.5) {
1630
1631       int ix, iy, iz;
1632       double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1633
1634       if(ImageActor->GetVisibility())
1635         worldPos << "data value : " << value << std::endl;
1636
1637       worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1638                           << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1639                           << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1640                           << mCurrentTSlice
1641                           << std::endl;
1642       worldPos << "pixel : " << ix << ' '
1643                              << iy << ' '
1644                              << iz << ' '
1645                              << mCurrentTSlice
1646                              << std::endl;
1647     
1648     }
1649 #endif
1650     ca->SetText(1,worldPos.str().c_str());
1651
1652     std::stringstream slicePos;
1653     slicePos << "Slice: " << this->GetSlice();
1654     ca->SetText(2, slicePos.str().c_str());
1655   }
1656
1657   if (pdmA->GetVisibility()) {
1658     double x = mCursor[0];
1659     double y = mCursor[1];
1660     double z = mCursor[2];
1661     double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1662     double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1663     double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1664 #if VTK_MAJOR_VERSION <= 5    
1665     if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1666         xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1667         yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1668         yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1669         zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1670         zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1671       vtkRenderer * renderer = this->Renderer;
1672
1673       renderer->WorldToView(x,y,z);
1674       renderer->ViewToNormalizedViewport(x,y,z);
1675       renderer->NormalizedViewportToViewport(x,y);
1676       renderer->ViewportToNormalizedDisplay(x,y);
1677       renderer->NormalizedDisplayToDisplay(x,y);
1678       crossCursor->SetFocalPoint(x,y,z);
1679     } else
1680       crossCursor->SetFocalPoint(-1,-1,z);
1681     crossCursor->Update();
1682   }
1683 #else
1684     vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1685     try {
1686         mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1687     } catch (const std::bad_cast& e) {
1688                 std::cerr << e.what() << std::endl;
1689                 std::cerr << "Conversion error" << std::endl;
1690                 return;
1691         }
1692
1693     if (xCursor >= mapperOpenGL->GetCroppingRegion()[0]-0.5 &&
1694         xCursor < mapperOpenGL->GetCroppingRegion()[1]+0.5 &&
1695         yCursor >= mapperOpenGL->GetCroppingRegion()[2]-0.5 &&
1696         yCursor < mapperOpenGL->GetCroppingRegion()[3]+0.5 &&
1697         zCursor >= mapperOpenGL->GetCroppingRegion()[4]-0.5 &&
1698         zCursor < mapperOpenGL->GetCroppingRegion()[5]+0.5 ) {
1699       vtkRenderer * renderer = this->Renderer;
1700
1701       renderer->WorldToView(x,y,z);
1702       renderer->ViewToNormalizedViewport(x,y,z);
1703       renderer->NormalizedViewportToViewport(x,y);
1704       renderer->ViewportToNormalizedDisplay(x,y);
1705       renderer->NormalizedDisplayToDisplay(x,y);
1706       crossCursor->SetFocalPoint(x,y,z);
1707     } else
1708       crossCursor->SetFocalPoint(-1,-1,z);
1709     crossCursor->Update();
1710   }
1711 #endif
1712
1713   if (mOverlay && mOverlayActor->GetVisibility()) {
1714     if(mLinkOverlayWindowLevel) {
1715       mOverlayMapper->SetWindow(this->GetColorWindow());
1716       mOverlayMapper->SetLevel(this->GetColorLevel());
1717     }
1718 #if VTK_MAJOR_VERSION <= 5
1719     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1720 #else
1721     mOverlayMapper->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1722 #endif
1723     mOverlayMapper->Update();
1724   }
1725   if (mFusion && mFusionActor->GetVisibility()) {
1726 #if VTK_MAJOR_VERSION <= 5
1727     mFusionMapper->GetOutput()->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1728 #else
1729     mFusionMapper->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1730 #endif
1731     mFusionMapper->Update();
1732   }
1733   if (mLandMapper)
1734     UpdateLandmarks();
1735
1736     this->GetRenderWindow()->Render();
1737 }
1738 //----------------------------------------------------------------------------
1739
1740
1741 //----------------------------------------------------------------------------
1742 void vvSlicer::UpdateCursorPosition()
1743
1744   pdmA->SetVisibility(true);
1745   mCursor[0] = mCurrent[0];
1746   mCursor[1] = mCurrent[1];
1747   mCursor[2] = mCurrent[2];
1748   mCursor[3] = mCurrentTSlice;
1749 }
1750 //----------------------------------------------------------------------------
1751
1752
1753 //----------------------------------------------------------------------------
1754 void vvSlicer::UpdateLandmarks()
1755
1756   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1757   if (pd->GetPoints()) {
1758     //mLandGlyph->SetRange(0,1);
1759     //mLandGlyph->Modified();
1760     //mLandGlyph->Update();
1761
1762     mClipBox->Modified();
1763     mLandClipper->Update();
1764     mLandMapper->Update();
1765     //Let's add the captions
1766     //First remove all captions:
1767     for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1768         this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1769         //allActors2D->Remove (mLandLabelActors[i]);
1770     }
1771     mLandLabelActors.clear();
1772     //Next add the captions to the displayed points
1773     for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1774           double *position = mLandClipper->GetOutput()->GetPoint(id);
1775       vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1776       vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1777       label_actor->SetCaption(label);
1778       label_actor->SetAttachmentPoint(position);
1779       label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1780       label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1781       label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1782       label_actor->GetCaptionTextProperty()->SetBold(0);
1783       label_actor->GetCaptionTextProperty()->SetFontSize(6);
1784       label_actor->BorderOff();
1785       label_actor->LeaderOff();
1786       label_actor->ThreeDimensionalLeaderOff();
1787       mLandLabelActors.push_back(label_actor);
1788       this->Renderer->AddActor2D(mLandLabelActors[id]);
1789      }
1790   }
1791
1792 }
1793 //----------------------------------------------------------------------------
1794
1795
1796 //----------------------------------------------------------------------------
1797 void vvSlicer::SetSlice(int slice)
1798
1799   int *range = this->GetSliceRange();
1800   if (range) {
1801     if (slice < range[0]) {
1802       slice = range[0];
1803     } else if (slice > range[1]) {
1804       slice = range[1];
1805     }
1806   }
1807
1808   if (this->Slice == slice) {
1809     return;
1810   }
1811
1812   this->Slice = slice;
1813   SetContourSlice();
1814   this->Modified();
1815   this->UpdateDisplayExtent();
1816
1817   // Seems to work without this line
1818   //this->Render();
1819 }
1820 //----------------------------------------------------------------------------
1821
1822 //----------------------------------------------------------------------------
1823 int vvSlicer::GetTMax() 
1824
1825   int tmax = (int)mImage->GetVTKImages().size() - 1;
1826   if(mOverlay)
1827     tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1828   return tmax;
1829 }
1830 //----------------------------------------------------------------------------
1831
1832 //----------------------------------------------------------------------------
1833 void vvSlicer::SetContourSlice()
1834
1835   if (mSurfaceCutActors.size() > 0)
1836     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1837          i!=mSurfaceCutActors.end(); i++) {
1838          
1839       (*i)->SetSlicingOrientation(this->SliceOrientation);
1840       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1841                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1842     }
1843 }
1844 //----------------------------------------------------------------------------
1845
1846
1847 //----------------------------------------------------------------------------
1848 void vvSlicer::ForceUpdateDisplayExtent()
1849
1850   this->UpdateDisplayExtent();
1851 }
1852 //----------------------------------------------------------------------------
1853
1854
1855 //----------------------------------------------------------------------------
1856 int* vvSlicer::GetDisplayExtent()
1857
1858   return this->GetImageActor()->GetDisplayExtent();
1859 }
1860 //----------------------------------------------------------------------------
1861
1862
1863 //----------------------------------------------------------------------------
1864 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1865
1866   this->Superclass::PrintSelf(os, indent);
1867 }
1868 //----------------------------------------------------------------------------
1869
1870
1871 //----------------------------------------------------------------------------
1872 void vvSlicer::SetVFColor(double r, double g, double b)
1873
1874   double mVFColorHSV[3];
1875   mVFColor[0] = r;
1876   mVFColor[1] = g;
1877   mVFColor[2] = b;
1878
1879   vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1880   mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1881   mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1882   mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1883
1884   this->Render();
1885 }  
1886 //----------------------------------------------------------------------------
1887
1888
1889 //----------------------------------------------------------------------------
1890 void vvSlicer::SetRegisterExtent(int ext[6])
1891
1892     copyExtent(ext, mRegisterExtent);
1893 }
1894 //----------------------------------------------------------------------------
1895
1896
1897 //----------------------------------------------------------------------------
1898 void vvSlicer::GetRegisterExtent(int ext[6])
1899
1900     copyExtent(mRegisterExtent, ext);
1901 }
1902 //----------------------------------------------------------------------------
1903