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