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