]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
Merge branch 'master' of git://git.creatis.insa-lyon.fr/clitk
[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 "vvSlicer.h"
20 #include "vvImage.h"
21 #include "vvSlicerManagerCommand.h"
22 #include "vvGlyphSource.h"
23 #include "vvGlyph2D.h"
24 #include "vvImageMapToWLColors.h"
25
26 #include <vtkTextProperty.h>
27 #include <vtkTextActor.h>
28 #include <vtkTextSource.h>
29 #include <vtkActor2D.h>
30 #include <vtkCursor2D.h>
31 #include <vtkPolyDataMapper2D.h>
32 #include <vtkProperty2D.h>
33 #include <vtkCornerAnnotation.h>
34 #include <vtkImageMapToWindowLevelColors.h>
35 #include <vtkImageData.h>
36 #include <vtkImageActor.h>
37 #include <vvBlendImageActor.h>
38 #include <vtkToolkits.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkPointData.h>
41 #include <vtkDataArray.h>
42 #include <vtkFloatArray.h>
43 #include <vtkClipPolyData.h>
44 #include <vtkGlyph3D.h>
45 #include <vtkMath.h>
46 #include <vtkCursor3D.h>
47 #include <vtkProperty.h>
48 #include <vtkLight.h>
49 #include <vtkLightCollection.h>
50 #include <vtkScalarBarActor.h>
51 #include <vtkLookupTable.h>
52
53 #include <vtkRenderer.h>
54 #include <vtkRendererCollection.h>
55 #include <vtkRenderWindow.h>
56 #include <vtkRenderWindowInteractor.h>
57 #include <vtkCamera.h>
58 #include <vtkCallbackCommand.h>
59 #include <vtkCommand.h>
60 #include <vtkPolyDataMapper.h>
61 #include <vtkBox.h>
62
63 #include <vtkExtractVOI.h>
64 #include <vtkSphereSource.h>
65 #include <vtkCutter.h>
66 #include <vtkAssignAttribute.h>
67 #include <vtkImageAccumulate.h>
68 #include <vtkImageReslice.h>
69
70 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
71 vtkStandardNewMacro(vvSlicer);
72 static void copyExtent(int* in, int* to){
73  for(int i=0; i<6; ++i) to[i]=in[i]; 
74 }
75 //------------------------------------------------------------------------------
76 vvSlicer::vvSlicer()
77 {
78   this->UnInstallPipeline();
79   mImage = NULL;
80   mReducedExtent = new int[6];
81   mCurrentTSlice = 0;
82   mUseReducedExtent = false;
83
84   mCurrent[0] = -VTK_DOUBLE_MAX;
85   mCurrent[1] = -VTK_DOUBLE_MAX;
86   mCurrent[2] = -VTK_DOUBLE_MAX;
87
88   mCursor[0] = -VTK_DOUBLE_MAX;
89   mCursor[1] = -VTK_DOUBLE_MAX;
90   mCursor[2] = -VTK_DOUBLE_MAX;
91   mCursor[3] = -VTK_DOUBLE_MAX;
92
93   mSubSampling = 5;
94   mScale = 1;
95   mVFLog = 0;
96   mVFWidth = 1;
97   mVFColor[0] = 0;
98   mVFColor[1] = 1;
99   mVFColor[2] = 0;
100
101   crossCursor = vtkSmartPointer<vtkCursor2D>::New();
102   crossCursor->AllOff();
103   crossCursor->AxesOn();
104   crossCursor->SetTranslationMode(1);
105   crossCursor->SetRadius(2);
106
107   pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
108   pdm->SetInput(crossCursor->GetOutput());
109
110   pdmA = vtkSmartPointer<vtkActor2D>::New();
111   pdmA->SetMapper(pdm);
112   pdmA->GetProperty()->SetColor(255,10,212);
113   pdmA->SetVisibility(0);
114   pdmA->SetPickable(0);
115
116   ca = vtkSmartPointer<vtkCornerAnnotation>::New();
117   ca->GetTextProperty()->SetColor(255,10,212);
118   ca->SetVisibility(1);
119   mFileName = "";
120
121   mVF = NULL;
122   mOverlay = NULL;
123   mFusion = NULL;
124   mLandmarks = NULL;
125
126   legend = vtkSmartPointer<vtkScalarBarActor>::New();
127   //legend->SetTitle("test!");
128   legend->SetPosition(0.82,0.18);
129   legend->SetWidth(0.1);
130   legend->SetVisibility(0);
131   legend->SetLabelFormat("%.1f");
132   this->GetRenderer()->AddActor(legend);
133
134   this->WindowLevel->Delete();
135   this->WindowLevel = vvImageMapToWLColors::New();
136
137   this->InstallPipeline();
138
139   mLinkOverlayWindowLevel = true;
140 }
141 //------------------------------------------------------------------------------
142
143
144 //------------------------------------------------------------------------------
145 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
146 {
147   return mOverlayMapper.GetPointer();
148 }
149 //------------------------------------------------------------------------------
150
151
152 //------------------------------------------------------------------------------
153 vvBlendImageActor* vvSlicer::GetOverlayActor()
154 {
155   return mOverlayActor.GetPointer();
156 }
157 //------------------------------------------------------------------------------
158
159
160 //------------------------------------------------------------------------------
161 vtkImageMapToColors* vvSlicer::GetFusionMapper()
162 {
163   return mFusionMapper.GetPointer();
164 }
165 //------------------------------------------------------------------------------
166
167
168 //------------------------------------------------------------------------------
169 vtkImageActor* vvSlicer::GetFusionActor()
170 {
171   return mFusionActor.GetPointer();
172 }
173 //------------------------------------------------------------------------------
174
175
176 //------------------------------------------------------------------------------
177 vtkActor* vvSlicer::GetVFActor()
178 {
179   return mVFActor.GetPointer();
180 }
181 //------------------------------------------------------------------------------
182
183
184 //------------------------------------------------------------------------------
185 vtkCornerAnnotation* vvSlicer::GetAnnotation()
186 {
187   return ca.GetPointer();
188 }
189 //------------------------------------------------------------------------------
190
191
192 //------------------------------------------------------------------------------
193 void vvSlicer::EnableReducedExtent(bool b)
194 {
195   mUseReducedExtent = b;
196 }
197 //------------------------------------------------------------------------------
198
199
200 //------------------------------------------------------------------------------
201 void vvSlicer::SetReducedExtent(int * ext)
202 {
203   copyExtent(ext, mReducedExtent);
204 }
205 //------------------------------------------------------------------------------
206
207
208 //------------------------------------------------------------------------------
209 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
210 {
211
212   mSurfaceCutActors.push_back(new vvMeshActor());
213   if (propagate)
214     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
215   else
216     mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
217   mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
218   this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
219
220   SetContourSlice();
221 }
222 //------------------------------------------------------------------------------
223
224
225 //------------------------------------------------------------------------------
226 void vvSlicer::ToggleContourSuperposition()
227 {
228   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
229        i!=mSurfaceCutActors.end(); i++)
230     (*i)->ToggleSuperposition();
231 }
232 //------------------------------------------------------------------------------
233
234
235 //------------------------------------------------------------------------------
236 void vvSlicer::SetCursorColor(int r,int g, int b)
237 {
238   pdmA->GetProperty()->SetColor(r,g,b);
239 }
240 //------------------------------------------------------------------------------
241
242
243 //------------------------------------------------------------------------------
244 void vvSlicer::SetCursorVisibility(bool s)
245 {
246   pdmA->SetVisibility(s);
247 }
248 //------------------------------------------------------------------------------
249
250
251 //------------------------------------------------------------------------------
252 bool vvSlicer::GetCursorVisibility()
253 {
254   return pdmA->GetVisibility();
255 }
256 //------------------------------------------------------------------------------
257
258
259 //------------------------------------------------------------------------------
260 void vvSlicer::SetCornerAnnotationVisibility(bool s)
261 {
262   ca->SetVisibility(s);
263 }
264 //------------------------------------------------------------------------------
265
266
267 //------------------------------------------------------------------------------
268 bool vvSlicer::GetCornerAnnotationVisibility()
269 {
270   return ca->GetVisibility();
271 }
272 //------------------------------------------------------------------------------
273
274
275 //------------------------------------------------------------------------------
276 vvSlicer::~vvSlicer()
277 {
278   for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
279        i!=mSurfaceCutActors.end(); i++)
280     delete (*i);
281   delete [] mReducedExtent;
282 }
283 //------------------------------------------------------------------------------
284
285
286 //------------------------------------------------------------------------------
287 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
288 {
289   mCurrent[0] = x;
290   mCurrent[1] = y;
291   mCurrent[2] = z;
292   mCurrentTSlice = t;
293 }
294 //------------------------------------------------------------------------------
295
296
297 //------------------------------------------------------------------------------
298 void vvSlicer::SetImage(vvImage::Pointer image)
299 {
300   if (image->GetVTKImages().size()) {
301     mImage = image;
302
303     if (!mImageReslice) {
304       mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
305       mImageReslice->SetInterpolationModeToLinear();
306       mImageReslice->AutoCropOutputOn();
307       mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
308     }
309     mImageReslice->SetResliceTransform(mImage->GetTransform());
310     mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
311     mImageReslice->UpdateInformation();
312
313     this->Superclass::SetInput(mImageReslice->GetOutput());
314
315     int extent[6];
316     this->GetInput()->GetWholeExtent(extent);
317
318     // Prevent crash when reload -> change slice if outside extent
319     if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
320       Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
321     }
322
323     // Make sure that the required part image has been computed
324     extent[SliceOrientation*2] = Slice;
325     extent[SliceOrientation*2+1] = Slice;
326     mImageReslice->GetOutput()->SetUpdateExtent(extent);
327     mImageReslice->GetOutput()->Update();
328
329     this->UpdateDisplayExtent();
330
331     mCurrentTSlice = 0;
332     ca->SetText(0,mFileName.c_str());
333   }
334 }
335 //------------------------------------------------------------------------------
336
337
338 //------------------------------------------------------------------------------
339 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
340 {
341   if (overlay->GetVTKImages().size()) {
342     mOverlay = overlay;
343     
344     if (!mOverlayReslice) {
345       mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
346       mOverlayReslice->SetInterpolationModeToLinear();
347       mOverlayReslice->AutoCropOutputOn();
348       mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
349     }
350     mOverlayReslice->SetResliceTransform(mOverlay->GetTransform());
351     mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
352
353     if (!mOverlayMapper)
354       mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
355     mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
356
357     if (!mOverlayActor) {
358       mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
359       mOverlayActor->SetInput(mOverlayMapper->GetOutput());
360       mOverlayActor->SetPickable(0);
361       mOverlayActor->SetVisibility(true);
362       mOverlayActor->SetOpacity(0.5);
363     }
364
365     //stupid but necessary : the Overlay need to be rendered before fusion
366     if (mFusionActor) {
367       this->GetRenderer()->RemoveActor(mFusionActor);
368       this->GetRenderer()->AddActor(mOverlayActor);
369       this->GetRenderer()->AddActor(mFusionActor);
370     } else
371       this->GetRenderer()->AddActor(mOverlayActor);
372
373     //Synchronize orientation and slice
374     AdjustResliceToSliceOrientation(mOverlayReslice);
375     this->UpdateDisplayExtent();
376     this->SetTSlice(mCurrentTSlice);
377   }
378 }
379 //------------------------------------------------------------------------------
380
381
382 //------------------------------------------------------------------------------
383 void vvSlicer::SetFusion(vvImage::Pointer fusion)
384 {
385   if (fusion->GetVTKImages().size()) {
386     mFusion = fusion;
387
388     if (!mFusionReslice) {
389       mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
390       mFusionReslice->SetInterpolationModeToLinear();
391       mFusionReslice->AutoCropOutputOn();
392       mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
393     }
394     mFusionReslice->SetResliceTransform(mFusion->GetTransform());
395     mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
396
397     if (!mFusionMapper)
398       mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
399     
400     vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
401     lut->SetRange(0, 1);
402     lut->SetValueRange(0, 1);
403     lut->SetSaturationRange(0, 0);
404     lut->Build();
405     mFusionMapper->SetLookupTable(lut);
406     mFusionMapper->SetInput(mFusionReslice->GetOutput());
407
408     if (!mFusionActor) {
409       mFusionActor = vtkSmartPointer<vtkImageActor>::New();
410       mFusionActor->SetInput(mFusionMapper->GetOutput());
411       mFusionActor->SetPickable(0);
412       mFusionActor->SetVisibility(true);
413       mFusionActor->SetOpacity(0.7);
414       this->GetRenderer()->AddActor(mFusionActor);
415     }
416
417     //Synchronize orientation and slice
418     AdjustResliceToSliceOrientation(mFusionReslice);
419     this->UpdateDisplayExtent();
420     this->SetTSlice(mCurrentTSlice);
421   }
422 }
423 //------------------------------------------------------------------------------
424
425
426 //------------------------------------------------------------------------------
427 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
428 {
429   bool vis = false;
430   if (actor_type == "image") {
431     vis = this->ImageActor->GetVisibility();
432   }
433   else if (actor_type == "vector") {
434     vis = this->mVFActor->GetVisibility();
435   }
436   else if (actor_type == "overlay") {
437     vis = this->mOverlayActor->GetVisibility();
438   }
439   else if (actor_type == "fusion") {
440     vis = this->mFusionActor->GetVisibility();
441   }
442   else if (actor_type == "contour")
443     vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
444
445   return vis;
446 }
447 //------------------------------------------------------------------------------
448
449 //------------------------------------------------------------------------------
450 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
451 {
452   if (actor_type == "image") {
453     this->ImageActor->SetVisibility(vis);
454   }
455   else if (actor_type == "vector") {
456     this->mVFActor->SetVisibility(vis);
457   }
458   else if (actor_type == "overlay") {
459     this->mOverlayActor->SetVisibility(vis);
460   }
461   else if (actor_type == "fusion") {
462     this->mFusionActor->SetVisibility(vis);
463   }
464   else if (actor_type == "contour")
465     this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
466   UpdateDisplayExtent();
467 }
468 //------------------------------------------------------------------------------
469
470 //------------------------------------------------------------------------------
471 void vvSlicer::SetVF(vvImage::Pointer vf)
472 {
473   if (vf->GetVTKImages().size()) {
474     mVF = vf;
475
476     if (!mAAFilter) {
477       mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
478       mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
479       mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
480     }
481     mVOIFilter->SetInput(vf->GetFirstVTKImageData());
482     mAAFilter->SetInput(mVOIFilter->GetOutput());
483     ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
484     mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
485
486     if (!mArrow)
487       mArrow = vtkSmartPointer<vvGlyphSource>::New();
488     mArrow->SetGlyphTypeToSpecificArrow();
489     mArrow->SetScale(mScale);
490     mArrow->FilledOff();
491
492     // Glyph the gradient vector (with arrows)
493     if (!mGlyphFilter)
494       mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
495     mGlyphFilter->SetInput(mAAFilter->GetOutput());
496     mGlyphFilter->SetSource(mArrow->GetOutput());
497     mGlyphFilter->ScalingOn();
498     mGlyphFilter->SetScaleModeToScaleByVector();
499     mGlyphFilter->OrientOn();
500     mGlyphFilter->SetVectorModeToUseVector();
501     mGlyphFilter->SetColorModeToColorByVector();
502
503     if (!mVFColorLUT)
504       mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
505
506     double mVFColorHSV[3];
507     vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
508     mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
509     mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
510     mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
511
512     if (!mVFMapper)
513       mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
514     mVFMapper->SetInput(mGlyphFilter->GetOutput());
515     mVFMapper->ImmediateModeRenderingOn();
516     mVFMapper->SetLookupTable(mVFColorLUT);
517
518     if (!mVFActor)
519       mVFActor = vtkSmartPointer<vtkActor>::New();
520     mVFActor->SetMapper(mVFMapper);
521     mVFActor->SetPickable(0);
522     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
523     this->UpdateDisplayExtent();
524     this->GetRenderer()->AddActor(mVFActor);
525
526     //Synchronize slice
527     SetTSlice(mCurrentTSlice);
528   }
529 }
530 //------------------------------------------------------------------------------
531
532
533 //------------------------------------------------------------------------------
534 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
535 {
536   mLandmarks = landmarks;
537   if (landmarks) {
538
539     if (!mCross)
540       mCross = vtkSmartPointer<vtkCursor3D>::New();
541     mCross->SetFocalPoint(0.0,0.0,0.0);
542     mCross->SetModelBounds(-10,10,-10,10,-10,10);
543     mCross->AllOff();
544     mCross->AxesOn();
545
546     if (!mLandGlyph)
547       mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
548     mLandGlyph->SetSource(mCross->GetOutput());
549     mLandGlyph->SetInput(landmarks->GetOutput());
550     //mLandGlyph->SetIndexModeToScalar();
551     mLandGlyph->SetRange(0,1);
552     mLandGlyph->ScalingOff();
553
554     mLandGlyph->SetColorModeToColorByScalar();
555
556     if (!mClipBox)
557       mClipBox = vtkSmartPointer<vtkBox>::New();
558     if (!mLandClipper)
559       mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
560     mLandClipper->InsideOutOn();
561     mLandClipper->SetInput(mLandGlyph->GetOutput());
562     mLandClipper->SetClipFunction(mClipBox);
563
564     if (!mLandMapper)
565       mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
566     mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
567     //mLandMapper->ScalarVisibilityOff();
568
569     if (!mLandActor)
570       mLandActor = vtkSmartPointer<vtkActor>::New();
571     mLandActor->SetMapper(mLandMapper);
572     mLandActor->GetProperty()->SetColor(255,10,212);
573     mLandActor->SetPickable(0);
574     mLandActor->SetVisibility(true);
575     this->UpdateDisplayExtent();
576     this->GetRenderer()->AddActor(mLandActor);
577   }
578 }
579 //------------------------------------------------------------------------------
580
581 //------------------------------------------------------------------------------
582 //FIXME: this function leaks memory, we should fix it someday :)
583 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
584 {
585   if (actor_type == "vector") {
586     Renderer->RemoveActor(mVFActor);
587     mGlyphFilter=NULL;
588     mVF = NULL;
589     mArrow = NULL;
590     mAAFilter=NULL;
591     mVOIFilter = NULL;
592     mVFMapper = NULL;
593     mVFActor = NULL;
594   }
595   if (actor_type == "overlay") {
596     Renderer->RemoveActor(mOverlayActor);
597     mOverlay = NULL;
598     mOverlayActor = NULL;
599     mOverlayMapper = NULL;
600   }
601   if (actor_type == "fusion") {
602     Renderer->RemoveActor(mFusionActor);
603     mFusion = NULL;
604     mFusionActor = NULL;
605     mFusionMapper = NULL;
606   }
607   if (actor_type == "contour") {
608     Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
609     mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
610   }
611 }
612 //------------------------------------------------------------------------------
613
614
615 //------------------------------------------------------------------------------
616 void vvSlicer::SetVFSubSampling(int sub)
617 {
618   if (mVOIFilter) {
619     mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
620     mSubSampling = sub;
621   }
622   UpdateDisplayExtent();
623   Render();
624 }
625 //------------------------------------------------------------------------------
626
627
628 //------------------------------------------------------------------------------
629 void vvSlicer::SetVFScale(int scale)
630 {
631   mScale = scale;
632   if (mArrow)
633     mArrow->SetScale(mScale);
634   UpdateDisplayExtent();
635   Render();
636 }
637 //------------------------------------------------------------------------------
638
639 //------------------------------------------------------------------------------
640 void vvSlicer::SetVFWidth(int width)
641 {
642   mVFWidth = width;
643   if (mVFActor)
644     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
645   UpdateDisplayExtent();
646   Render();
647 }
648 //------------------------------------------------------------------------------
649
650
651 //------------------------------------------------------------------------------
652 void vvSlicer::SetVFLog(int log)
653 {
654   mVFLog = log;
655   if (mGlyphFilter) {
656     mGlyphFilter->SetUseLog(mVFLog);
657     mGlyphFilter->Modified();
658   }
659   UpdateDisplayExtent();
660   Render();
661 }
662 //------------------------------------------------------------------------------
663
664
665 //------------------------------------------------------------------------------
666 void vvSlicer::SetTSlice(int t)
667 {
668   if (t < 0)
669     t = 0;
670   else if ((unsigned int)t >= mImage->GetVTKImages().size())
671     t = mImage->GetVTKImages().size() -1;
672
673   if (mCurrentTSlice == t) return;
674
675   mCurrentTSlice = t;
676   mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
677   if (mVF && mVFActor->GetVisibility()) {
678     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
679       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
680   }
681   if (mOverlay && mOverlayActor->GetVisibility()) {
682     if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
683       mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
684   }
685   if (mFusion && mFusionActor->GetVisibility()) {
686     if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
687       mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
688   }
689   if (mSurfaceCutActors.size() > 0)
690     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
691          i!=mSurfaceCutActors.end(); i++)
692       (*i)->SetTimeSlice(mCurrentTSlice);
693   UpdateDisplayExtent();
694 }
695 //------------------------------------------------------------------------------
696
697
698 //------------------------------------------------------------------------------
699 int vvSlicer::GetTSlice()
700 {
701   return mCurrentTSlice;
702 }
703 //------------------------------------------------------------------------------
704
705 //------------------------------------------------------------------------------
706 void vvSlicer::SetSliceOrientation(int orientation)
707 {
708   //if 2D image, force to watch in Axial View
709   int extent[6];
710   this->GetInput()->GetWholeExtent(extent);
711   if (extent[5]-extent[4] <= 2)
712     orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
713
714   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
715       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
716     vtkErrorMacro("Error - invalid slice orientation " << orientation);
717     return;
718   }
719   
720   this->SliceOrientation = orientation;
721
722   if(mFusion)
723     AdjustResliceToSliceOrientation(mFusionReslice);
724
725   if(mOverlay)
726     AdjustResliceToSliceOrientation(mOverlayReslice);
727
728   // Update the viewer
729   int *range = this->GetSliceRange();
730   if (range)
731     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
732
733   // Go to current cursor position
734   // double* cursorPos = GetCursorPosition();
735   // DDV(cursorPos, 3);
736   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
737
738   this->UpdateOrientation();
739   this->UpdateDisplayExtent();
740
741   if (this->Renderer && this->GetInput()) {
742     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
743     this->Renderer->ResetCamera();
744     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
745   }
746
747   SetContourSlice();
748 }
749 //----------------------------------------------------------------------------
750
751 //------------------------------------------------------------------------------
752 // This function ensures that we sample the slices of a vtkImageReslice filter
753 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
754 // In other words, we change the grid of the reslice in the same way as the grid
755 // of the displayed image in the slicing direction.
756 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
757 {
758   // Reset autocrop and update output information
759   reslice->SetOutputOriginToDefault();
760   reslice->SetOutputSpacingToDefault();
761   reslice->GetOutput()->UpdateInformation();
762
763   // Ge new origin / spacing
764   double origin[3];
765   double spacing[3];
766   reslice->GetOutput()->GetOrigin(origin);
767   reslice->GetOutput()->GetSpacing(spacing);
768
769   // Use similar spacing as the image in the direction SliceOrientation
770   spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
771
772   // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
773   // Step 1: from world coordinates to image coordinates
774   origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
775   origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
776   // Step 2: round to superior grid positionInc
777   origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
778   // Step 3: back to world coordinates
779   origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
780   origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
781
782   // Set new spacing and origin
783   reslice->SetOutputOrigin(origin);
784   reslice->SetOutputSpacing(spacing);
785   reslice->UpdateInformation();
786   reslice->GetOutput()->UpdateInformation();
787 }
788 //------------------------------------------------------------------------------
789
790 //----------------------------------------------------------------------------
791 int * vvSlicer::GetExtent(){
792   int *w_ext;
793   if (mUseReducedExtent) {
794     w_ext = mReducedExtent;
795   } else w_ext = GetInput()->GetWholeExtent();
796   return w_ext;
797 }
798 //----------------------------------------------------------------------------
799
800
801 //----------------------------------------------------------------------------
802 int vvSlicer::GetOrientation()
803 {
804   return this->SliceOrientation;
805 }
806 //----------------------------------------------------------------------------
807
808
809 //----------------------------------------------------------------------------
810 void vvSlicer::UpdateDisplayExtent()
811 {
812   vtkImageData *input = this->GetInput();
813   if (!input || !this->ImageActor) {
814     return;
815   }
816   input->UpdateInformation();
817
818   // Local copy of extent
819   int w_ext[6];
820   int* ext = GetExtent();
821   copyExtent(ext, w_ext);
822   // Set slice value
823   int s = this->Slice > ext[this->SliceOrientation*2+1] ? ext[this->SliceOrientation*2 + 1] : this->Slice;
824   w_ext[ this->SliceOrientation*2   ] = s;
825   w_ext[ this->SliceOrientation*2+1 ] = s;
826   
827   // Image actor
828   this->ImageActor->SetDisplayExtent(w_ext);
829   
830   // Overlay image actor
831   if (mOverlay && mOverlayActor->GetVisibility()) {
832     AdjustResliceToSliceOrientation(mOverlayReslice);
833     int overExtent[6];
834     this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
835     ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
836     mOverlayActor->SetDisplayExtent( overExtent );
837   }
838
839   // Fusion image actor
840   if (mFusion && mFusionActor->GetVisibility()) {
841     AdjustResliceToSliceOrientation(mFusionReslice);
842     int fusExtent[6];
843     this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
844     ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
845     mFusionActor->SetDisplayExtent(fusExtent);
846   }
847
848   // Vector field actor
849   double* camera = Renderer->GetActiveCamera()->GetPosition();
850   double* image_bounds = ImageActor->GetBounds();
851   double position[3] = {0, 0, 0};
852   position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2]; 
853
854   //print_vector<double, 6>("camera", camera);
855   //print_vector<double, 6>("image_bounds", image_bounds);
856   //print_vector<double, 3>("position", position);
857
858   // find where to place the VF actor. to deal with
859   // z-buffer issues, the VF is placed right in front of the image,
860   // subject to a small offset. the position actually depends on the
861   // the location of the camera relative to the image. 
862   double offset = 1;
863   if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
864     offset = -1;
865   
866   if (mVF && mVFActor->GetVisibility()) {
867     int vfExtent[6];
868     mVF->GetVTKImages()[0]->UpdateInformation();
869     this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
870     ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
871     mVOIFilter->SetVOI(vfExtent);
872     int orientation[3] = {1,1,1};
873     orientation[this->SliceOrientation] = 0;
874     mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
875     mVFMapper->Update();
876
877     position[this->SliceOrientation] += offset;
878     mVFActor->SetPosition(position);
879   }
880   
881   // Landmarks actor
882   if (mLandActor) {
883     if (mClipBox) {
884       double bounds [6];
885       for(unsigned int i=0; i<6; i++)
886         bounds[i] = ImageActor->GetBounds()[i];
887       bounds[ this->SliceOrientation*2   ] = ImageActor->GetBounds()[ this->SliceOrientation*2  ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
888       bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
889       mClipBox->SetBounds(bounds);
890       UpdateLandmarks();
891     }
892     
893     position[this->SliceOrientation] = offset;
894     mLandActor->SetPosition(position);
895   }
896
897   // Figure out the correct clipping range
898   if (this->Renderer) {
899     if (this->InteractorStyle &&
900         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
901       this->Renderer->ResetCameraClippingRange();
902     } else {
903       vtkCamera *cam = this->Renderer->GetActiveCamera();
904       if (cam) {
905         double bounds[6];
906         this->ImageActor->GetBounds(bounds);
907         double spos = (double)bounds[this->SliceOrientation * 2];
908         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
909         double range = fabs(spos - cpos);
910         double *spacing = input->GetSpacing();
911         double avg_spacing =
912           ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
913         cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
914       }
915     }
916   }
917   
918 }
919 //----------------------------------------------------------------------------
920
921 //----------------------------------------------------------------------------
922 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
923                                                 vtkImageData *targetImage, int targetExtent[6])
924 {
925   double dExtents[6];
926   for(unsigned int i=0; i<6; i++) {
927     // From source voxel coordinates to world coordinates
928     dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
929
930     // From world coordinates to floating point target voxel coordinates
931     dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
932     
933     // Round to nearest
934     //targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
935     targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
936   }
937 }
938 //----------------------------------------------------------------------------
939
940 //----------------------------------------------------------------------------
941 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
942 {
943   bool out = false;
944   int maxBound = 6;
945
946   //2D overlay on 3D image specific case
947   if (refExtent[4] == refExtent[5]) {
948     maxBound = 4;
949     extent[4] = refExtent[4];
950     extent[5] = refExtent[5];
951   }
952
953   for (int i = 0; i < maxBound; i = i+2) {
954     //if we are totally outside the image
955     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
956       out = true;
957       break;
958     }
959     //crop to the limit of the image
960     extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
961     extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
962     extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
963     extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
964   }
965   if (out)
966     for (int i = 0; i < maxBound; i = i+2) {
967       extent[i] = refExtent[i];
968       extent[i+1] = refExtent[i];
969     }
970 }
971 //----------------------------------------------------------------------------
972
973
974 //----------------------------------------------------------------------------
975 void vvSlicer::UpdateOrientation()
976 {
977   // Set the camera position
978   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
979   if (cam) {
980     switch (this->SliceOrientation) {
981     case vtkImageViewer2::SLICE_ORIENTATION_XY:
982       cam->SetFocalPoint(0,0,0);
983       cam->SetPosition(0,0,-1); // -1 if medical ?
984       cam->SetViewUp(0,-1,0);
985       break;
986
987     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
988       cam->SetFocalPoint(0,0,0);
989       cam->SetPosition(0,-1,0); // 1 if medical ?
990       cam->SetViewUp(0,0,1);
991       break;
992
993     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
994       cam->SetFocalPoint(0,0,0);
995       cam->SetPosition(-1,0,0); // -1 if medical ?
996       cam->SetViewUp(0,0,1);
997       break;
998     }
999   }
1000 }
1001 //----------------------------------------------------------------------------
1002
1003
1004 //----------------------------------------------------------------------------
1005 void vvSlicer::SetOpacity(double s)
1006 {
1007   this->GetImageActor()->SetOpacity(s);
1008 }
1009 //----------------------------------------------------------------------------
1010
1011
1012 //----------------------------------------------------------------------------
1013 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1014 {
1015   this->Superclass::SetRenderWindow(rw);
1016   this->SetupInteractor(rw->GetInteractor());
1017   ca->SetImageActor(this->GetImageActor());
1018   ca->SetWindowLevel(this->GetWindowLevel());
1019   ca->SetText(2, "<slice>");
1020   ca->SetText(3, "<window>\n<level>");
1021
1022   double bounds[6];
1023   double max = 65000;
1024
1025   bounds[0] = -max;
1026   bounds[1] = max;
1027   bounds[2] = -max;
1028   bounds[3] = max;
1029   bounds[4] = -max;
1030   bounds[5] = max;
1031
1032   crossCursor->SetModelBounds(bounds);
1033   this->GetRenderer()->AddActor(pdmA);
1034   this->GetRenderer()->AddActor(ca);
1035   this->GetRenderer()->ResetCamera();
1036
1037   //this is just a mapping between the labeling of the orientations presented to the user and
1038   //the one used by vtk
1039   SetSliceOrientation(2-(orientation%3));
1040   ResetCamera();
1041 }
1042 //----------------------------------------------------------------------------
1043
1044
1045 //----------------------------------------------------------------------------
1046 void vvSlicer::ResetCamera()
1047 {
1048   this->GetRenderer()->ResetCamera();
1049 }
1050 //----------------------------------------------------------------------------
1051
1052
1053 //----------------------------------------------------------------------------
1054 void vvSlicer::SetDisplayMode(bool i)
1055 {
1056   this->GetRenderer()->SetDraw(i);
1057   if (i)
1058     UpdateDisplayExtent();
1059 }
1060 //----------------------------------------------------------------------------
1061
1062
1063 //----------------------------------------------------------------------------
1064 void vvSlicer::FlipHorizontalView()
1065 {
1066   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1067   if (cam) {
1068     double *position = cam->GetPosition();
1069     double factor[3] = {1, 1, 1};
1070     factor[this->SliceOrientation] = -1;
1071     cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1072     
1073 /*    switch (this->SliceOrientation) {
1074     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1075       cam->SetPosition(position[0],position[1],-position[2]);
1076       break;
1077
1078     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1079       cam->SetPosition(position[0],-position[1],position[2]);
1080       break;
1081
1082     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1083       cam->SetPosition(-position[0],position[1],position[2]);
1084       break;
1085     }*/
1086
1087     this->Renderer->ResetCameraClippingRange();
1088     this->UpdateDisplayExtent();
1089   }
1090 }
1091 //----------------------------------------------------------------------------
1092
1093
1094 //----------------------------------------------------------------------------
1095 void vvSlicer::FlipVerticalView()
1096 {
1097   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1098   if (cam) {
1099     FlipHorizontalView();
1100     double *viewup = cam->GetViewUp();
1101     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1102     this->UpdateDisplayExtent();
1103   }
1104 }
1105 //----------------------------------------------------------------------------
1106
1107
1108 //----------------------------------------------------------------------------
1109 void vvSlicer::SetColorWindow(double window)
1110 {
1111   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1112   if ( LUT ) {
1113     double level = this->GetWindowLevel()->GetLevel();
1114     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1115     LUT->Build();
1116   }
1117   this->vtkImageViewer2::SetColorWindow(window);
1118 }
1119 //----------------------------------------------------------------------------
1120
1121 //----------------------------------------------------------------------------
1122 void vvSlicer::SetColorLevel(double level)
1123 {
1124   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1125   if ( LUT ) {
1126     double window = this->GetWindowLevel()->GetWindow();
1127     LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1128     LUT->Build();
1129   }
1130   this->vtkImageViewer2::SetColorLevel(level);
1131 }
1132 //----------------------------------------------------------------------------
1133
1134 //----------------------------------------------------------------------------
1135 double vvSlicer::GetOverlayColorWindow()
1136 {
1137   if(mOverlayMapper)
1138     return mOverlayMapper->GetWindow();
1139   else
1140     return 0.;
1141 }
1142 //----------------------------------------------------------------------------
1143
1144 //----------------------------------------------------------------------------
1145 double vvSlicer::GetOverlayColorLevel()
1146 {
1147   if(mOverlayMapper)
1148     return mOverlayMapper->GetLevel();
1149   else
1150     return 0.;
1151 }
1152 //----------------------------------------------------------------------------
1153
1154 //----------------------------------------------------------------------------
1155 void vvSlicer::SetOverlayColorWindow(double window)
1156 {
1157   mOverlayMapper->SetWindow(window);
1158 }
1159 //----------------------------------------------------------------------------
1160
1161 //----------------------------------------------------------------------------
1162 void vvSlicer::SetOverlayColorLevel(double level)
1163 {
1164   mOverlayMapper->SetLevel(level);
1165 }
1166 //----------------------------------------------------------------------------
1167
1168 //----------------------------------------------------------------------------
1169 // Returns the min an the max value in a 41x41 region around the mouse pointer
1170 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image)
1171 {
1172   //Get mouse pointer position in view coordinates
1173   double fLocalExtents[6];
1174   for(int i=0; i<3; i++) {
1175     fLocalExtents[i*2  ] = mCurrent[i];
1176     fLocalExtents[i*2+1] = mCurrent[i];
1177   }
1178   this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1179   this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1180   for(int i=0; i<3; i++) {
1181     if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1182       fLocalExtents[i*2  ] -= 0.2;
1183       fLocalExtents[i*2+1] += 0.2;
1184     }
1185   }
1186   this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1187   this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1188
1189   //Convert to image pixel coordinates (rounded)
1190   int iLocalExtents[6];
1191   for(int i=0; i<3; i++) {
1192     fLocalExtents[i*2  ] = (fLocalExtents[i*2  ] - image->GetOrigin()[i])/image->GetSpacing()[i];
1193     fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - image->GetOrigin()[i])/image->GetSpacing()[i];
1194
1195     iLocalExtents[i*2  ] = lrint(fLocalExtents[i*2  ]);
1196     iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1197
1198     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1199       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1200   }
1201
1202   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1203   voiFilter->SetInput(image);
1204   voiFilter->SetVOI(iLocalExtents);
1205   voiFilter->Update();
1206   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1207     min = 0;
1208     max = 0;
1209     return;
1210   }
1211
1212   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1213   accFilter->SetInput(voiFilter->GetOutput());
1214   accFilter->Update();
1215
1216   min = *(accFilter->GetMin());
1217   max = *(accFilter->GetMax());
1218 }
1219 //----------------------------------------------------------------------------
1220
1221 //----------------------------------------------------------------------------
1222 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1223 {
1224   ix = lrint(X);
1225   iy = lrint(Y);
1226   iz = lrint(Z);
1227   if (ix < image->GetWholeExtent()[0] ||
1228       ix > image->GetWholeExtent()[1] ||
1229       iy < image->GetWholeExtent()[2] ||
1230       iy > image->GetWholeExtent()[3] ||
1231       iz < image->GetWholeExtent()[4] ||
1232       iz > image->GetWholeExtent()[5] )
1233     return std::numeric_limits<double>::quiet_NaN();
1234
1235   image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1236   image->Update();
1237   return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1238 }
1239 //----------------------------------------------------------------------------
1240
1241 //----------------------------------------------------------------------------
1242 void vvSlicer::Render()
1243 {
1244   if (this->mFusion) {
1245     legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1246     legend->UseOpacityOn();
1247     legend->SetVisibility(1);
1248   }
1249   else if (this->GetWindowLevel()->GetLookupTable()) {// && !this->mOverlay && !this->mFusion) {
1250     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1251     legend->UseOpacityOff();
1252     legend->SetVisibility(1);
1253   } else legend->SetVisibility(0);
1254
1255   if (ca->GetVisibility()) {
1256     std::stringstream worldPos;
1257     double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1258     double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1259     double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1260     
1261 //     if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1262 //     else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1]; 
1263 //     if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1264 //     else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3]; 
1265 //     if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1266 //     else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5]; 
1267
1268     if (X >= this->GetInput()->GetWholeExtent()[0] &&
1269         X <= this->GetInput()->GetWholeExtent()[1] &&
1270         Y >= this->GetInput()->GetWholeExtent()[2] &&
1271         Y <= this->GetInput()->GetWholeExtent()[3] &&
1272         Z >= this->GetInput()->GetWholeExtent()[4] &&
1273         Z <= this->GetInput()->GetWholeExtent()[5]) {
1274
1275       
1276       int ix, iy, iz;
1277       double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1278
1279       if(ImageActor->GetVisibility())
1280         worldPos << "data value : " << value << std::endl;
1281
1282       worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1283                           << lrint(mCurrent[1]) << ' '
1284                           << lrint(mCurrent[2]) << ' '
1285                           << mCurrentTSlice
1286                           << std::endl;
1287       worldPos << "pixel : " << ix << ' '
1288                              << iy << ' '
1289                              << iz << ' '
1290                              << mCurrentTSlice
1291                              << std::endl;
1292     }
1293     ca->SetText(1,worldPos.str().c_str());
1294   }
1295
1296   if (pdmA->GetVisibility()) {
1297     double x = mCursor[0];
1298     double y = mCursor[1];
1299     double z = mCursor[2];
1300     double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1301     double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1302     double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1303
1304     if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1305         xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1306         yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1307         yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1308         zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1309         zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1310       vtkRenderer * renderer = this->Renderer;
1311
1312       renderer->WorldToView(x,y,z);
1313       renderer->ViewToNormalizedViewport(x,y,z);
1314       renderer->NormalizedViewportToViewport(x,y);
1315       renderer->ViewportToNormalizedDisplay(x,y);
1316       renderer->NormalizedDisplayToDisplay(x,y);
1317       crossCursor->SetFocalPoint(x,y,z);
1318     } else
1319       crossCursor->SetFocalPoint(-1,-1,z);
1320   }
1321
1322
1323   if (mOverlay && mOverlayActor->GetVisibility()) {
1324     if(mLinkOverlayWindowLevel) {
1325       mOverlayMapper->SetWindow(this->GetColorWindow());
1326       mOverlayMapper->SetLevel(this->GetColorLevel());
1327     }
1328     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1329     mOverlayMapper->GetOutput()->Update();
1330     mOverlayMapper->Update();
1331   }
1332   if (mLandMapper)
1333     UpdateLandmarks();
1334
1335   this->GetRenderWindow()->Render();
1336 }
1337 //----------------------------------------------------------------------------
1338
1339
1340 //----------------------------------------------------------------------------
1341 void vvSlicer::UpdateCursorPosition()
1342 {
1343   pdmA->SetVisibility(true);
1344   mCursor[0] = mCurrent[0];
1345   mCursor[1] = mCurrent[1];
1346   mCursor[2] = mCurrent[2];
1347   mCursor[3] = mCurrentTSlice;
1348 }
1349 //----------------------------------------------------------------------------
1350
1351
1352 //----------------------------------------------------------------------------
1353 void vvSlicer::UpdateLandmarks()
1354 {
1355   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1356   if (pd->GetPoints()) {
1357     mLandGlyph->SetRange(0,1);
1358     mLandGlyph->Modified();
1359     mLandGlyph->Update();
1360
1361     mClipBox->Modified();
1362     mLandClipper->Update();
1363     mLandMapper->Update();
1364   }
1365
1366 }
1367 //----------------------------------------------------------------------------
1368
1369
1370 //----------------------------------------------------------------------------
1371 void vvSlicer::SetSlice(int slice)
1372 {
1373   int *range = this->GetSliceRange();
1374   if (range) {
1375     if (slice < range[0]) {
1376       slice = range[0];
1377     } else if (slice > range[1]) {
1378       slice = range[1];
1379     }
1380   }
1381
1382   if (this->Slice == slice) {
1383     return;
1384   }
1385
1386   this->Slice = slice;
1387   SetContourSlice();
1388   this->Modified();
1389   this->UpdateDisplayExtent();
1390
1391   // Seems to work without this line
1392   //this->Render();
1393 }
1394 //----------------------------------------------------------------------------
1395
1396
1397 //----------------------------------------------------------------------------
1398 void vvSlicer::SetContourSlice()
1399 {
1400   if (mSurfaceCutActors.size() > 0)
1401     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1402          i!=mSurfaceCutActors.end(); i++) {
1403          
1404       (*i)->SetSlicingOrientation(this->SliceOrientation);
1405       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1406                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1407     }
1408 }
1409 //----------------------------------------------------------------------------
1410
1411
1412 //----------------------------------------------------------------------------
1413 void vvSlicer::ForceUpdateDisplayExtent()
1414 {
1415   this->UpdateDisplayExtent();
1416 }
1417 //----------------------------------------------------------------------------
1418
1419
1420 //----------------------------------------------------------------------------
1421 int* vvSlicer::GetDisplayExtent()
1422 {
1423   return this->GetImageActor()->GetDisplayExtent();
1424 }
1425 //----------------------------------------------------------------------------
1426
1427
1428 //----------------------------------------------------------------------------
1429 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1430 {
1431   this->Superclass::PrintSelf(os, indent);
1432 }
1433 //----------------------------------------------------------------------------
1434
1435 //----------------------------------------------------------------------------
1436 void vvSlicer::SetVFColor(double r, double g, double b)
1437 {
1438   double mVFColorHSV[3];
1439   mVFColor[0] = r;
1440   mVFColor[1] = g;
1441   mVFColor[2] = b;
1442
1443   vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1444   mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1445   mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1446   mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1447
1448   this->Render();
1449 }  
1450