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