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