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