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