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