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