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