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