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