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