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