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