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