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