]> Creatis software - clitk.git/blob - vv/vvSlicer.cxx
add tooltip for MiB mebibytes / MB megabytes
[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->GetTransformedVTKImages()[0]);
294
295     int extent[6];
296     this->GetInput()->GetWholeExtent(extent);
297
298     // Prevent crash when reload -> change slice if outside extent
299     if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
300       Slice = (extent[SliceOrientation*2+1]-extent[SliceOrientation*2])/2.0;
301     }
302
303     // Make sure that the required part image has been computed
304     extent[SliceOrientation*2] = Slice;
305     extent[SliceOrientation*2+1] = Slice;
306     image->GetTransformedVTKImages()[0]->SetUpdateExtent(extent);
307     image->GetTransformedVTKImages()[0]->Update();
308
309     this->UpdateDisplayExtent();
310
311     mCurrentTSlice = 0;
312     ca->SetText(0,mFileName.c_str());
313   }
314 }
315 //------------------------------------------------------------------------------
316
317
318 //------------------------------------------------------------------------------
319 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
320 {
321   if (overlay->GetVTKImages().size()) {
322     mOverlay = overlay;
323
324     if (!mOverlayMapper)
325       mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
326     mOverlayMapper->SetInput(overlay->GetTransformedVTKImages()[0]);
327
328     if (!mOverlayActor) {
329       mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
330       mOverlayActor->SetInput(mOverlayMapper->GetOutput());
331       mOverlayActor->SetPickable(0);
332       mOverlayActor->SetVisibility(false);
333       mOverlayActor->SetOpacity(0.5);
334       this->UpdateDisplayExtent();
335     }
336
337     //stupid but necessary : the Overlay need to be rendered before fusion
338     if (mFusionActor) {
339       this->GetRenderer()->RemoveActor(mFusionActor);
340       this->GetRenderer()->AddActor(mOverlayActor);
341       this->GetRenderer()->AddActor(mFusionActor);
342     } else
343       this->GetRenderer()->AddActor(mOverlayActor);
344
345     //Synchronize slice
346     SetTSlice(mCurrentTSlice);
347   }
348 }
349 //------------------------------------------------------------------------------
350
351
352 //------------------------------------------------------------------------------
353 void vvSlicer::SetFusion(vvImage::Pointer fusion)
354 {
355   if (fusion->GetVTKImages().size()) {
356     mFusion = fusion;
357
358     if (!mFusionMapper)
359       mFusionMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
360     mFusionMapper->SetInput(fusion->GetTransformedVTKImages()[0]);
361
362     if (!mFusionActor) {
363       mFusionActor = vtkSmartPointer<vtkImageActor>::New();
364       mFusionActor->SetInput(mFusionMapper->GetOutput());
365       mFusionActor->SetPickable(0);
366       mFusionActor->SetVisibility(false);
367       mFusionActor->SetOpacity(0.7);
368       this->UpdateDisplayExtent();
369       this->GetRenderer()->AddActor(mFusionActor);
370     }
371
372     //Synchronize slice
373     SetTSlice(mCurrentTSlice);
374   }
375 }
376 //------------------------------------------------------------------------------
377
378
379 //------------------------------------------------------------------------------
380 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
381 {
382   if (actor_type == "vector") {
383     this->mVFActor->SetVisibility(vis);
384   }
385   if (actor_type == "overlay") {
386     this->mOverlayActor->SetVisibility(vis);
387   }
388   if (actor_type == "fusion") {
389     this->mFusionActor->SetVisibility(vis);
390   }
391   if (actor_type == "contour")
392     this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
393   UpdateDisplayExtent();
394 }
395 //------------------------------------------------------------------------------
396
397
398 //------------------------------------------------------------------------------
399 void vvSlicer::SetVF(vvImage::Pointer vf)
400 {
401   if (vf->GetVTKImages().size()) {
402     mVF = vf;
403
404     if (!mAAFilter) {
405       mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
406       mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
407       mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
408     }
409     mVOIFilter->SetInput(vf->GetTransformedVTKImages()[0]);
410     mAAFilter->SetInput(mVOIFilter->GetOutput());
411     ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
412     mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
413
414     if (!mArrow)
415       mArrow = vtkSmartPointer<vvGlyphSource>::New();
416     mArrow->SetGlyphTypeToSpecificArrow();
417     mArrow->SetScale(mScale);
418     mArrow->FilledOff();
419
420     // Glyph the gradient vector (with arrows)
421     if (!mGlyphFilter)
422       mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
423     mGlyphFilter->SetInput(mAAFilter->GetOutput());
424     mGlyphFilter->SetSource(mArrow->GetOutput());
425     mGlyphFilter->ScalingOn();
426     mGlyphFilter->SetScaleModeToScaleByVector();
427     mGlyphFilter->OrientOn();
428     mGlyphFilter->SetVectorModeToUseVector();
429     mGlyphFilter->SetColorModeToColorByVector();
430
431     if (!mVFMapper)
432       mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
433     //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
434     mVFMapper->SetInput(mGlyphFilter->GetOutput());
435     mVFMapper->ImmediateModeRenderingOn();
436
437     if (!mVFActor)
438       mVFActor = vtkSmartPointer<vtkActor>::New();
439     mVFActor->SetMapper(mVFMapper);
440     mVFActor->SetPickable(0);
441     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
442     this->UpdateDisplayExtent();
443     this->GetRenderer()->AddActor(mVFActor);
444
445     //Synchronize slice
446     SetTSlice(mCurrentTSlice);
447   }
448 }
449 //------------------------------------------------------------------------------
450
451
452 //------------------------------------------------------------------------------
453 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
454 {
455   mLandmarks = landmarks;
456   if (landmarks) {
457
458     if (!mCross)
459       mCross = vtkSmartPointer<vtkCursor3D>::New();
460     mCross->SetFocalPoint(0.0,0.0,0.0);
461     mCross->SetModelBounds(-10,10,-10,10,-10,10);
462     mCross->AllOff();
463     mCross->AxesOn();
464
465     if (!mLandGlyph)
466       mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
467     mLandGlyph->SetSource(mCross->GetOutput());
468     mLandGlyph->SetInput(landmarks->GetOutput());
469     //mLandGlyph->SetIndexModeToScalar();
470     mLandGlyph->SetRange(0,1);
471     mLandGlyph->ScalingOff();
472
473     mLandGlyph->SetColorModeToColorByScalar();
474
475     if (!mClipBox)
476       mClipBox = vtkSmartPointer<vtkBox>::New();
477     if (!mLandClipper)
478       mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
479     mLandClipper->InsideOutOn();
480     mLandClipper->SetInput(mLandGlyph->GetOutput());
481     mLandClipper->SetClipFunction(mClipBox);
482
483     if (!mLandMapper)
484       mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
485     mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
486     //mLandMapper->ScalarVisibilityOff();
487
488     if (!mLandActor)
489       mLandActor = vtkSmartPointer<vtkActor>::New();
490     mLandActor->SetMapper(mLandMapper);
491     mLandActor->GetProperty()->SetColor(255,10,212);
492     mLandActor->SetPickable(0);
493     mLandActor->SetVisibility(true);
494     this->UpdateDisplayExtent();
495     this->GetRenderer()->AddActor(mLandActor);
496   }
497 }
498 //------------------------------------------------------------------------------
499
500 //------------------------------------------------------------------------------
501 //FIXME: this function leaks memory, we should fix it someday :)
502 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
503 {
504   if (actor_type == "vector") {
505     Renderer->RemoveActor(mVFActor);
506     mGlyphFilter=NULL;
507     mVF = NULL;
508     mArrow = NULL;
509     mAAFilter=NULL;
510     mVOIFilter = NULL;
511     mVFMapper = NULL;
512     mVFActor = NULL;
513   }
514   if (actor_type == "overlay") {
515     Renderer->RemoveActor(mOverlayActor);
516     mOverlay = NULL;
517     mOverlayActor = NULL;
518     mOverlayMapper = NULL;
519   }
520   if (actor_type == "fusion") {
521     Renderer->RemoveActor(mFusionActor);
522     mFusion = NULL;
523     mFusionActor = NULL;
524     mFusionMapper = NULL;
525   }
526   if (actor_type == "contour") {
527     Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
528     mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
529   }
530 }
531 //------------------------------------------------------------------------------
532
533
534 //------------------------------------------------------------------------------
535 void vvSlicer::SetVFSubSampling(int sub)
536 {
537   if (mVOIFilter) {
538     mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
539     mSubSampling = sub;
540   }
541   UpdateDisplayExtent();
542   Render();
543 }
544 //------------------------------------------------------------------------------
545
546
547 //------------------------------------------------------------------------------
548 void vvSlicer::SetVFScale(int scale)
549 {
550   mScale = scale;
551   if (mArrow)
552     mArrow->SetScale(mScale);
553   UpdateDisplayExtent();
554   Render();
555 }
556 //------------------------------------------------------------------------------
557
558 //------------------------------------------------------------------------------
559 void vvSlicer::SetVFWidth(int width)
560 {
561   mVFWidth = width;
562   if (mVFActor)
563     mVFActor->GetProperty()->SetLineWidth(mVFWidth);
564   UpdateDisplayExtent();
565   Render();
566 }
567 //------------------------------------------------------------------------------
568
569
570 //------------------------------------------------------------------------------
571 void vvSlicer::SetVFLog(int log)
572 {
573   mVFLog = log;
574   if (mGlyphFilter) {
575     mGlyphFilter->SetUseLog(mVFLog);
576     mGlyphFilter->Modified();
577   }
578   UpdateDisplayExtent();
579   Render();
580 }
581 //------------------------------------------------------------------------------
582
583
584 //------------------------------------------------------------------------------
585 void vvSlicer::SetTSlice(int t)
586 {
587   if (t < 0)
588     t = 0;
589   else if ((unsigned int)t >= mImage->GetVTKImages().size())
590     t = mImage->GetVTKImages().size() -1;
591
592   if (mCurrentTSlice == t) return;
593
594   mCurrentTSlice = t;
595   this->SetInput(mImage->GetTransformedVTKImages()[t]);
596   if (mVF && mVFActor->GetVisibility()) {
597     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
598       mVOIFilter->SetInput(mVF->GetTransformedVTKImages()[mCurrentTSlice]);
599   }
600   if (mOverlay && mOverlayActor->GetVisibility()) {
601     if (mOverlay->GetTransformedVTKImages().size() > (unsigned int)mCurrentTSlice)
602       mOverlayMapper->SetInput(mOverlay->GetTransformedVTKImages()[mCurrentTSlice]);
603   }
604   if (mFusion && mFusionActor->GetVisibility()) {
605     if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
606       mFusionMapper->SetInput(mFusion->GetTransformedVTKImages()[mCurrentTSlice]);
607   }
608   if (mSurfaceCutActors.size() > 0)
609     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
610          i!=mSurfaceCutActors.end(); i++)
611       (*i)->SetTimeSlice(mCurrentTSlice);
612   UpdateDisplayExtent();
613 }
614 //------------------------------------------------------------------------------
615
616
617 //------------------------------------------------------------------------------
618 int vvSlicer::GetTSlice()
619 {
620   return mCurrentTSlice;
621 }
622 //------------------------------------------------------------------------------
623
624
625 //------------------------------------------------------------------------------
626 void vvSlicer::SetSliceOrientation(int orientation)
627 {
628   //if 2D image, force to watch in Axial View
629   int extent[6];
630   this->GetInput()->GetWholeExtent(extent);
631   if (extent[5]-extent[4] <= 2)
632     orientation=2;
633
634   if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
635       orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
636     vtkErrorMacro("Error - invalid slice orientation " << orientation);
637     return;
638   }
639
640   this->SliceOrientation = orientation;
641
642   // Update the viewer
643   int *range = this->GetSliceRange();
644   if (range)
645     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
646
647   // Go to current cursor position
648   // double* cursorPos = GetCursorPosition();
649   // DDV(cursorPos, 3);
650   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
651
652   this->UpdateOrientation();
653   this->UpdateDisplayExtent();
654
655   if (this->Renderer && this->GetInput()) {
656     double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
657     this->Renderer->ResetCamera();
658     this->Renderer->GetActiveCamera()->SetParallelScale(scale);
659   }
660
661   SetContourSlice();
662 }
663 //----------------------------------------------------------------------------
664
665
666 //----------------------------------------------------------------------------
667 int * vvSlicer::GetExtent()
668 {
669   int *w_ext;
670   if (mUseReducedExtent) {
671     w_ext = mReducedExtent;
672   } else w_ext = GetInput()->GetWholeExtent();
673   return w_ext;
674 }
675 //----------------------------------------------------------------------------
676
677
678 //----------------------------------------------------------------------------
679 int vvSlicer::GetOrientation()
680 {
681   return this->SliceOrientation;
682 }
683 //----------------------------------------------------------------------------
684
685
686 //----------------------------------------------------------------------------
687 void vvSlicer::UpdateDisplayExtent()
688 {
689   vtkImageData *input = this->GetInput();
690   if (!input || !this->ImageActor) {
691     return;
692   }
693   input->UpdateInformation();
694   int *w_ext;// = input->GetWholeExtent();
695
696   if (mUseReducedExtent) {
697     w_ext = mReducedExtent;
698   } else w_ext = input->GetWholeExtent();
699
700   switch (this->SliceOrientation) {
701   case vtkImageViewer2::SLICE_ORIENTATION_XY:
702     this->ImageActor->SetDisplayExtent(
703                                        w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
704     if (mVF && mVFActor->GetVisibility()) {
705       int vfExtent[6];
706       ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
707       mVOIFilter->SetVOI(vfExtent);
708       mGlyphFilter->SetOrientation(1,1,0);
709       mVFMapper->Update();
710       // put the vector field between the image and the camera
711       if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
712         mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
713       else
714         mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
715     }
716     if (mOverlay && mOverlayActor->GetVisibility()) {
717       int overExtent[6];
718       ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
719       mOverlayActor->SetDisplayExtent(overExtent);
720       if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
721         mOverlayActor->SetPosition(0,0,1);
722       else
723         mOverlayActor->SetPosition(0,0,-1);
724     }
725     if (mFusion && mFusionActor->GetVisibility()) {
726       int fusExtent[6];
727       ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
728       mFusionActor->SetDisplayExtent(fusExtent);
729       if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
730         mFusionActor->SetPosition(0,0,1.5);
731       else
732         mFusionActor->SetPosition(0,0,-1.5);
733     }
734     if (mLandActor) {
735       if (mClipBox) {
736         double bounds [6];
737         bounds[0] = ImageActor->GetBounds()[0];
738         bounds[1] = ImageActor->GetBounds()[1];
739         bounds[2] = ImageActor->GetBounds()[2];
740         bounds[3] = ImageActor->GetBounds()[3];
741         bounds[4] = ImageActor->GetBounds()[4]-fabs(0.5/this->GetInput()->GetSpacing()[2]);
742         bounds[5] = ImageActor->GetBounds()[5]+fabs(0.5/this->GetInput()->GetSpacing()[2]);
743         mClipBox->SetBounds(bounds);
744         UpdateLandmarks();
745       }
746       // DD(mLandActor->GetPosition()[2]);
747       // DD(Renderer->GetActiveCamera()->GetPosition()[2]);
748       mLandActor->SetPosition(0,0,-1.5);
749       /*
750       if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
751         mLandActor->SetPosition(0,0,1.5);
752       else
753         mLandActor->SetPosition(0,0,-1.5);
754       */
755     }
756     break;
757
758   case vtkImageViewer2::SLICE_ORIENTATION_XZ:
759     this->ImageActor->SetDisplayExtent(
760                                        w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
761     if (mVF && mVFActor->GetVisibility()) {
762       int vfExtent[6];
763       ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
764       mVOIFilter->SetVOI(vfExtent);
765       mGlyphFilter->SetOrientation(1,0,1);
766       mVFMapper->Update();
767       // put the vector field between the image aSpacingnd the camera
768       if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
769         mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
770       else
771         mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
772     }
773     if (mOverlay && mOverlayActor->GetVisibility()) {
774       int overExtent[6];
775       ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
776       mOverlayActor->SetDisplayExtent(overExtent);
777       if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
778         mOverlayActor->SetPosition(0,1,0);
779       else
780         mOverlayActor->SetPosition(0,-1,0);
781     }
782     if (mFusion && mFusionActor->GetVisibility()) {
783       int fusExtent[6];
784       ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
785       mFusionActor->SetDisplayExtent(fusExtent);
786       if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
787         mFusionActor->SetPosition(0,1.5,0);
788       else
789         mFusionActor->SetPosition(0,-1.5,0);
790     }
791     if (mLandActor) {
792       if (mClipBox) {
793         double bounds [6];
794         bounds[0] = ImageActor->GetBounds()[0];
795         bounds[1] = ImageActor->GetBounds()[1];
796         bounds[2] = ImageActor->GetBounds()[2]-fabs(0.5/this->GetInput()->GetSpacing()[1]);
797         bounds[3] = ImageActor->GetBounds()[3]+fabs(0.5/this->GetInput()->GetSpacing()[1]);
798         bounds[4] = ImageActor->GetBounds()[4];
799         bounds[5] = ImageActor->GetBounds()[5];
800         mClipBox->SetBounds(bounds);
801         UpdateLandmarks();
802       }
803       //      DD(mLandActor->GetPosition()[1]);
804       //DD(Renderer->GetActiveCamera()->GetPosition()[1]);
805       if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
806         mLandActor->SetPosition(0,1.5,0);
807       else
808         mLandActor->SetPosition(0,-1.5,0);
809     }
810     break;
811
812   case vtkImageViewer2::SLICE_ORIENTATION_YZ:
813     this->ImageActor->SetDisplayExtent(
814                                        this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
815     if (mVF && mVFActor->GetVisibility()) {
816       int vfExtent[6];
817       ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
818       mVOIFilter->SetVOI(vfExtent);
819       mGlyphFilter->SetOrientation(0,1,1);
820       mVFMapper->Update();
821       // put the vector field between the image and the camera
822       if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
823         mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
824       else
825         mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
826     }
827     if (mOverlay && mOverlayActor->GetVisibility()) {
828       int overExtent[6];
829       ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
830       mOverlayActor->SetDisplayExtent(overExtent);
831       if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
832         mOverlayActor->SetPosition(1,0,0);
833       else
834         mOverlayActor->SetPosition(-1,0,0);
835     }
836     if (mFusion && mFusionActor->GetVisibility()) {
837       int fusExtent[6];
838       ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
839       mFusionActor->SetDisplayExtent(fusExtent);
840       if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
841         mFusionActor->SetPosition(1.5,0,0);
842       else
843         mFusionActor->SetPosition(-1.5,0,0);
844     }
845     if (mLandActor) {
846       if (mClipBox) {
847         double bounds [6];
848         bounds[0] = ImageActor->GetBounds()[0]-fabs(0.5/this->GetInput()->GetSpacing()[0]);
849         bounds[1] = ImageActor->GetBounds()[1]+fabs(0.5/this->GetInput()->GetSpacing()[0]);
850         bounds[2] = ImageActor->GetBounds()[2];
851         bounds[3] = ImageActor->GetBounds()[3];
852         bounds[4] = ImageActor->GetBounds()[4];
853         bounds[5] = ImageActor->GetBounds()[5];
854         mClipBox->SetBounds(bounds);
855         UpdateLandmarks();
856       }
857       //      DD(mLandActor->GetPosition()[1]);
858       //      DD(Renderer->GetActiveCamera()->GetPosition()[1]);
859       if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
860         mLandActor->SetPosition(1.5,0,0);
861       else
862         mLandActor->SetPosition(-1.5,0,0);
863     }
864     break;
865   }
866
867   // Figure out the correct clipping range
868
869   if (this->Renderer) {
870     if (this->InteractorStyle &&
871         this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
872       this->Renderer->ResetCameraClippingRange();
873     } else {
874       vtkCamera *cam = this->Renderer->GetActiveCamera();
875       if (cam) {
876         double bounds[6];
877         this->ImageActor->GetBounds(bounds);
878         double spos = (double)bounds[this->SliceOrientation * 2];
879         double cpos = (double)cam->GetPosition()[this->SliceOrientation];
880         double range = fabs(spos - cpos);
881         double *spacing = input->GetSpacing();
882         double avg_spacing =
883           ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
884         cam->SetClippingRange(
885                               range - avg_spacing * 3.0, range + avg_spacing * 3.0);
886       }
887     }
888   }
889 }
890 //----------------------------------------------------------------------------
891
892
893 //----------------------------------------------------------------------------
894 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
895 {
896   vtkImageData* image=this->GetInput();
897   vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
898     mVF->GetSpacing()[0];
899   vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
900     mVF->GetSpacing()[0];
901   vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
902     mVF->GetSpacing()[1];
903   vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
904     mVF->GetSpacing()[1];
905   vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
906     mVF->GetSpacing()[2];
907   vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
908     mVF->GetSpacing()[2];
909
910   ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
911 }
912 //----------------------------------------------------------------------------
913
914
915 //----------------------------------------------------------------------------
916 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
917 {
918   vtkImageData* image=this->GetInput();
919   overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
920     mOverlay->GetSpacing()[0];
921   overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
922     mOverlay->GetSpacing()[0];
923   overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
924     mOverlay->GetSpacing()[1];
925   overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
926     mOverlay->GetSpacing()[1];
927   overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
928     mOverlay->GetSpacing()[2];
929   overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
930     mOverlay->GetSpacing()[2];
931   ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
932 }
933 //----------------------------------------------------------------------------
934
935
936 //----------------------------------------------------------------------------
937 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
938 {
939   vtkImageData* image=this->GetInput();
940   fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
941     mFusion->GetSpacing()[0];
942   fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
943     mFusion->GetSpacing()[0];
944   fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
945     mFusion->GetSpacing()[1];
946   fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
947     mFusion->GetSpacing()[1];
948   fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
949     mFusion->GetSpacing()[2];
950   fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
951     mFusion->GetSpacing()[2];
952   ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
953 }
954 //----------------------------------------------------------------------------
955
956
957 //----------------------------------------------------------------------------
958 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
959 {
960   bool out = false;
961   int maxBound = 6;
962
963   //2D overlay on 3D image specific case
964   if (refExtent[4] == refExtent[5]) {
965     maxBound = 4;
966     extent[4] = refExtent[4];
967     extent[5] = refExtent[5];
968   }
969
970   for (int i = 0; i < maxBound; i = i+2) {
971     //if we are totally outside the image
972     if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
973       out = true;
974       break;
975     }
976     //crop to the limit of the image
977     extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
978     extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
979     extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
980     extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
981   }
982   if (out)
983     for (int i = 0; i < maxBound; i = i+2) {
984       extent[i] = refExtent[i];
985       extent[i+1] = refExtent[i];
986     }
987 }
988 //----------------------------------------------------------------------------
989
990
991 //----------------------------------------------------------------------------
992 void vvSlicer::UpdateOrientation()
993 {
994   // Set the camera position
995   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
996   if (cam) {
997     switch (this->SliceOrientation) {
998     case vtkImageViewer2::SLICE_ORIENTATION_XY:
999       cam->SetFocalPoint(0,0,0);
1000       cam->SetPosition(0,0,-1); // -1 if medical ?
1001       cam->SetViewUp(0,-1,0);
1002       break;
1003
1004     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1005       cam->SetFocalPoint(0,0,0);
1006       cam->SetPosition(0,-1,0); // 1 if medical ?
1007       cam->SetViewUp(0,0,1);
1008       break;
1009
1010     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1011       cam->SetFocalPoint(0,0,0);
1012       cam->SetPosition(-1,0,0); // -1 if medical ?
1013       cam->SetViewUp(0,0,1);
1014       break;
1015     }
1016   }
1017 }
1018 //----------------------------------------------------------------------------
1019
1020
1021 //----------------------------------------------------------------------------
1022 void vvSlicer::SetOpacity(double s)
1023 {
1024   this->GetImageActor()->SetOpacity(s);
1025 }
1026 //----------------------------------------------------------------------------
1027
1028
1029 //----------------------------------------------------------------------------
1030 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1031 {
1032   this->Superclass::SetRenderWindow(rw);
1033   this->SetupInteractor(rw->GetInteractor());
1034   ca->SetImageActor(this->GetImageActor());
1035   ca->SetWindowLevel(this->GetWindowLevel());
1036   ca->SetText(2, "<slice>");
1037   ca->SetText(3, "<window>\n<level>");
1038
1039   double bounds[6];
1040   double max = 65000;
1041
1042   bounds[0] = -max;
1043   bounds[1] = max;
1044   bounds[2] = -max;
1045   bounds[3] = max;
1046   bounds[4] = -max;
1047   bounds[5] = max;
1048
1049   crossCursor->SetModelBounds(bounds);
1050   this->GetRenderer()->AddActor(pdmA);
1051   this->GetRenderer()->AddActor(ca);
1052   this->GetRenderer()->ResetCamera();
1053
1054   //this is just a mapping between the labeling of the orientations presented to the user and
1055   //the one used by vtk
1056   SetSliceOrientation(2-(orientation%3));
1057   ResetCamera();
1058 }
1059 //----------------------------------------------------------------------------
1060
1061
1062 //----------------------------------------------------------------------------
1063 void vvSlicer::ResetCamera()
1064 {
1065   if (this->GetInput()) {
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     double *position = cam->GetPosition();
1098     switch (this->SliceOrientation) {
1099     case vtkImageViewer2::SLICE_ORIENTATION_XY:
1100       cam->SetPosition(position[0],position[1],-position[2]);
1101       break;
1102
1103     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1104       cam->SetPosition(position[0],-position[1],position[2]);
1105       break;
1106
1107     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1108       cam->SetPosition(-position[0],position[1],position[2]);
1109       break;
1110     }
1111     this->Renderer->ResetCameraClippingRange();
1112     this->UpdateDisplayExtent();
1113   }
1114 }
1115 //----------------------------------------------------------------------------
1116
1117
1118 //----------------------------------------------------------------------------
1119 void vvSlicer::FlipVerticalView()
1120 {
1121   vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1122   if (cam) {
1123     FlipHorizontalView();
1124     double *viewup = cam->GetViewUp();
1125     cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1126     this->UpdateDisplayExtent();
1127   }
1128 }
1129 //----------------------------------------------------------------------------
1130
1131
1132 //----------------------------------------------------------------------------
1133 void vvSlicer::SetColorWindow(double window)
1134 {
1135   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1136   if ( LUT ) {
1137     double level = this->GetWindowLevel()->GetLevel();
1138     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1139     LUT->Build();
1140   }
1141   this->vtkImageViewer2::SetColorWindow(window);
1142 }
1143 //----------------------------------------------------------------------------
1144
1145
1146 //----------------------------------------------------------------------------
1147 void vvSlicer::SetColorLevel(double level)
1148 {
1149   vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1150   if ( LUT ) {
1151     double window = this->GetWindowLevel()->GetWindow();
1152     LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1153     LUT->Build();
1154   }
1155   this->vtkImageViewer2::SetColorLevel(level);
1156 }
1157 //----------------------------------------------------------------------------
1158
1159 //----------------------------------------------------------------------------
1160 // Returns the min an the max value in a 41x41 region around the mouse pointer
1161 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1162 {
1163   //Get mouse pointer position in view coordinates
1164   double fLocalExtents[6];
1165   for(int i=0; i<3; i++) {
1166     fLocalExtents[i*2  ] = mCurrent[i];
1167     fLocalExtents[i*2+1] = mCurrent[i];
1168   }
1169   this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1170   this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1171   for(int i=0; i<3; i++) {
1172     if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1173       fLocalExtents[i*2  ] -= 0.2;
1174       fLocalExtents[i*2+1] += 0.2;
1175     }
1176   }
1177   this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1178   this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1179
1180   //Convert to image pixel coordinates (rounded)
1181   int iLocalExtents[6];
1182   for(int i=0; i<3; i++) {
1183     fLocalExtents[i*2  ] = (fLocalExtents[i*2  ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1184     fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1185
1186     iLocalExtents[i*2  ] = lrint(fLocalExtents[i*2  ]);
1187     iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1188
1189     if(iLocalExtents[i*2  ]>iLocalExtents[i*2+1])
1190       std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1191   }
1192
1193   vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1194   voiFilter->SetInput(this->GetInput());
1195   voiFilter->SetVOI(iLocalExtents);
1196   voiFilter->Update();
1197   if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1198     min = 0;
1199     max = 0;
1200     return;
1201   }
1202
1203   vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1204   accFilter->SetInput(voiFilter->GetOutput());
1205   accFilter->Update();
1206
1207   min = *(accFilter->GetMin());
1208   max = *(accFilter->GetMax());
1209 }
1210 //----------------------------------------------------------------------------
1211
1212 //----------------------------------------------------------------------------
1213 void vvSlicer::Render()
1214 {
1215   if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1216     legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1217     legend->SetVisibility(1);
1218   } else legend->SetVisibility(0);
1219
1220   if (ca->GetVisibility()) {
1221     std::string worldPos = "";
1222     std::stringstream world1;
1223     std::stringstream world2;
1224     std::stringstream world3;
1225     world1 << (int)mCurrent[0];
1226     world2 << (int)mCurrent[1];
1227     world3 << (int)mCurrent[2];
1228     double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1229     double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1230     double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1231
1232     if (pdmA->GetVisibility()) {
1233       double x = mCursor[0];
1234       double y = mCursor[1];
1235       double z = mCursor[2];
1236       double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1237       double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1238       double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1239
1240       if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1241           xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1242           yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1243           yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1244           zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1245           zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1246         vtkRenderer * renderer = this->Renderer;
1247
1248         renderer->WorldToView(x,y,z);
1249         renderer->ViewToNormalizedViewport(x,y,z);
1250         renderer->NormalizedViewportToViewport(x,y);
1251         renderer->ViewportToNormalizedDisplay(x,y);
1252         renderer->NormalizedDisplayToDisplay(x,y);
1253         crossCursor->SetFocalPoint(x,y,z);
1254       } else
1255         crossCursor->SetFocalPoint(-1,-1,z);
1256     }
1257
1258     if (X >= this->GetInput()->GetWholeExtent()[0] &&
1259         X <= this->GetInput()->GetWholeExtent()[1] &&
1260         Y >= this->GetInput()->GetWholeExtent()[2] &&
1261         Y <= this->GetInput()->GetWholeExtent()[3] &&
1262         Z >= this->GetInput()->GetWholeExtent()[4] &&
1263         Z <= this->GetInput()->GetWholeExtent()[5]) {
1264       int ix = lrint(X);
1265       int iy = lrint(Y);
1266       int iz = lrint(Z);
1267       std::stringstream pixel1;
1268       std::stringstream pixel2;
1269       std::stringstream pixel3;
1270       std::stringstream temps;
1271       pixel1 << ix;
1272       pixel2 << iy;
1273       pixel3 << iz;
1274       temps << mCurrentTSlice;
1275       this->GetInput()->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1276       this->GetInput()->Update();
1277       double value = this->GetInput()->GetScalarComponentAsDouble(ix, iy, iz, 0);
1278
1279       std::stringstream val;
1280       val << value;
1281       worldPos += "data value : " + val.str();
1282       worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " +
1283         world3.str() + " " + temps.str();
1284       worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " +
1285         pixel3.str() + " " + temps.str();
1286     }
1287     ca->SetText(1,worldPos.c_str());
1288   }
1289   if (mOverlay && mOverlayActor->GetVisibility()) {
1290     mOverlayMapper->SetWindow(this->GetColorWindow());
1291     mOverlayMapper->SetLevel(this->GetColorLevel());
1292     mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1293     mOverlayMapper->GetOutput()->Update();
1294     mOverlayMapper->Update();
1295   }
1296   if (mLandMapper)
1297     UpdateLandmarks();
1298   //this->Superclass::Render();
1299   this->GetRenderWindow()->Render();
1300 }
1301 //----------------------------------------------------------------------------
1302
1303
1304 //----------------------------------------------------------------------------
1305 void vvSlicer::UpdateCursorPosition()
1306 {
1307   if (this->GetImageActor()->GetVisibility()) {
1308     pdmA->SetVisibility(true);
1309     mCursor[0] = mCurrent[0];
1310     mCursor[1] = mCurrent[1];
1311     mCursor[2] = mCurrent[2];
1312     mCursor[3] = mCurrentTSlice;
1313   }
1314 }
1315 //----------------------------------------------------------------------------
1316
1317
1318 //----------------------------------------------------------------------------
1319 void vvSlicer::UpdateLandmarks()
1320 {
1321   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1322   if (pd->GetPoints()) {
1323     mLandGlyph->SetRange(0,1);
1324     mLandGlyph->Modified();
1325     mLandGlyph->Update();
1326
1327     mClipBox->Modified();
1328     mLandClipper->Update();
1329     mLandMapper->Update();
1330   }
1331
1332 }
1333 //----------------------------------------------------------------------------
1334
1335
1336 //----------------------------------------------------------------------------
1337 void vvSlicer::SetSlice(int slice)
1338 {
1339   int *range = this->GetSliceRange();
1340   if (range) {
1341     if (slice < range[0]) {
1342       slice = range[0];
1343     } else if (slice > range[1]) {
1344       slice = range[1];
1345     }
1346   }
1347
1348   if (this->Slice == slice) {
1349     return;
1350   }
1351
1352   this->Slice = slice;
1353   SetContourSlice();
1354   this->Modified();
1355   this->UpdateDisplayExtent();
1356
1357   // Seems to work without this line
1358   //  this->Render();
1359 }
1360 //----------------------------------------------------------------------------
1361
1362
1363 //----------------------------------------------------------------------------
1364 void vvSlicer::SetContourSlice()
1365 {
1366   if (mSurfaceCutActors.size() > 0)
1367     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1368          i!=mSurfaceCutActors.end(); i++)
1369       (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1370                         this->GetImage()->GetOrigin()[this->SliceOrientation]);
1371 }
1372 //----------------------------------------------------------------------------
1373
1374
1375 //----------------------------------------------------------------------------
1376 void vvSlicer::ForceUpdateDisplayExtent()
1377 {
1378   this->UpdateDisplayExtent();
1379 }
1380 //----------------------------------------------------------------------------
1381
1382
1383 //----------------------------------------------------------------------------
1384 int* vvSlicer::GetDisplayExtent()
1385 {
1386   return this->GetImageActor()->GetDisplayExtent();
1387 }
1388 //----------------------------------------------------------------------------
1389
1390
1391 //----------------------------------------------------------------------------
1392 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1393 {
1394   this->Superclass::PrintSelf(os, indent);
1395 }
1396 //----------------------------------------------------------------------------
1397
1398
1399
1400
1401
1402
1403