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