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