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