1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
21 #include "vvSlicerManagerCommand.h"
22 #include "vvGlyphSource.h"
23 #include "vvGlyph2D.h"
24 #include "vvImageMapToWLColors.h"
26 #include <vtkTextProperty.h>
27 #include <vtkTextActor.h>
28 #include <vtkTextSource.h>
29 #include <vtkActor2D.h>
30 #include <vtkCursor2D.h>
31 #include <vtkPolyDataMapper2D.h>
32 #include <vtkProperty2D.h>
33 #include <vtkCornerAnnotation.h>
34 #include <vtkImageMapToWindowLevelColors.h>
35 #include <vtkImageData.h>
36 #include <vtkImageActor.h>
37 #include <vtkToolkits.h>
38 #include <vtkObjectFactory.h>
39 #include <vtkPointData.h>
40 #include <vtkDataArray.h>
41 #include <vtkFloatArray.h>
42 #include <vtkClipPolyData.h>
43 #include <vtkGlyph3D.h>
45 #include <vtkCursor3D.h>
46 #include <vtkProperty.h>
48 #include <vtkLightCollection.h>
49 #include <vtkScalarBarActor.h>
50 #include <vtkLookupTable.h>
52 #include <vtkRenderer.h>
53 #include <vtkRendererCollection.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkRenderWindowInteractor.h>
56 #include <vtkCamera.h>
57 #include <vtkCallbackCommand.h>
58 #include <vtkCommand.h>
59 #include <vtkPolyDataMapper.h>
62 #include <vtkExtractVOI.h>
63 #include <vtkSphereSource.h>
64 #include <vtkCutter.h>
65 #include <vtkAssignAttribute.h>
66 #include <vtkImageAccumulate.h>
67 #include <vtkImageReslice.h>
69 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
70 vtkStandardNewMacro(vvSlicer);
72 //------------------------------------------------------------------------------
77 mUseReducedExtent = false;
79 mCurrent[0] = -VTK_DOUBLE_MAX;
80 mCurrent[1] = -VTK_DOUBLE_MAX;
81 mCurrent[2] = -VTK_DOUBLE_MAX;
83 mCursor[0] = -VTK_DOUBLE_MAX;
84 mCursor[1] = -VTK_DOUBLE_MAX;
85 mCursor[2] = -VTK_DOUBLE_MAX;
86 mCursor[3] = -VTK_DOUBLE_MAX;
92 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
93 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
94 text += "0,1,2,3,4,5 : preset windowing\n";
95 text += "6,7,8,9 : preset colormap\n";
96 text += "z : local windowing\n";
97 text += "r : reset view\n";
98 text += "l : reload image\n";
99 text += "f : fly to mouse position\n";
100 text += "g : go to cross hair position\n\n";
101 text += "Up,down : change slice\n";
102 text += "Left,right : change tenporal slice\n\n";
103 text += "Scrollbar (or w/x) : zoom in/out\n";
104 text += "left button : synchronize all views\n";
105 text += "middle button : grab image\n";
106 text += "right button : change windowing\n";
108 crossCursor = vtkCursor2D::New();
109 crossCursor->AllOff();
110 crossCursor->AxesOn();
111 crossCursor->SetTranslationMode(1);
112 crossCursor->SetRadius(2);
114 pdm = vtkPolyDataMapper2D::New();
115 pdm->SetInput(crossCursor->GetOutput());
117 pdmA = vtkActor2D::New();
118 pdmA->SetMapper(pdm);
119 pdmA->GetProperty()->SetColor(255,10,212);
120 pdmA->SetVisibility(0);
121 pdmA->SetPickable(0);
123 ca = vtkCornerAnnotation::New();
124 ca->GetTextProperty()->SetColor(255,10,212);
125 ca->SetVisibility(1);
133 legend = vtkSmartPointer<vtkScalarBarActor>::New();
134 //legend->SetTitle("test!");
135 legend->SetPosition(0.82,0.18);
136 legend->SetWidth(0.1);
137 legend->SetVisibility(0);
138 legend->SetLabelFormat("%.1f");
139 this->GetRenderer()->AddActor(legend);
141 this->WindowLevel->Delete();
142 this->WindowLevel = vvImageMapToWLColors::New();
143 this->InstallPipeline();
146 //------------------------------------------------------------------------------
149 //------------------------------------------------------------------------------
150 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
151 return mOverlayMapper.GetPointer();
153 //------------------------------------------------------------------------------
156 //------------------------------------------------------------------------------
157 vtkImageActor* vvSlicer::GetOverlayActor() {
158 return mOverlayActor.GetPointer();
160 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
165 return mFusionMapper.GetPointer();
167 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 vtkImageActor* vvSlicer::GetFusionActor() {
172 return mFusionActor.GetPointer();
174 //------------------------------------------------------------------------------
177 //------------------------------------------------------------------------------
178 vtkActor* vvSlicer::GetVFActor() {
179 return mVFActor.GetPointer();
181 //------------------------------------------------------------------------------
184 //------------------------------------------------------------------------------
185 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
186 return ca.GetPointer();
188 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 void vvSlicer::EnableReducedExtent(bool b) {
193 mUseReducedExtent = b;
195 //------------------------------------------------------------------------------
198 //------------------------------------------------------------------------------
199 void vvSlicer::SetReducedExtent(int * ext) {
200 mReducedExtent = ext;
202 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
206 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
209 mSurfaceCutActors.push_back(new vvMeshActor());
211 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
213 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
214 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
215 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
219 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 void vvSlicer::ToggleContourSuperposition()
225 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
226 i!=mSurfaceCutActors.end();i++)
227 (*i)->ToggleSuperposition();
229 //------------------------------------------------------------------------------
232 //------------------------------------------------------------------------------
233 void vvSlicer::SetCursorColor(int r,int g, int b)
235 pdmA->GetProperty()->SetColor(r,g,b);
237 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 void vvSlicer::SetCursorVisibility(bool s)
243 pdmA->SetVisibility(s);
245 //------------------------------------------------------------------------------
248 //------------------------------------------------------------------------------
249 bool vvSlicer::GetCursorVisibility()
251 return pdmA->GetVisibility();
253 //------------------------------------------------------------------------------
256 //------------------------------------------------------------------------------
257 vvSlicer::~vvSlicer()
259 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
260 i!=mSurfaceCutActors.end();i++)
263 //------------------------------------------------------------------------------
266 //------------------------------------------------------------------------------
267 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
274 //------------------------------------------------------------------------------
277 //------------------------------------------------------------------------------
278 void vvSlicer::SetImage(vvImage::Pointer image)
280 if (image->GetVTKImages().size())
283 this->Superclass::SetInput(image->GetVTKImages()[0]);
284 this->UpdateDisplayExtent();
286 ca->SetText(0,mFileName.c_str());
289 //------------------------------------------------------------------------------
292 //------------------------------------------------------------------------------
293 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
295 if (overlay->GetVTKImages().size())
300 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
301 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
305 mOverlayActor = vtkImageActor::New();
306 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
307 mOverlayActor->SetPickable(0);
308 mOverlayActor->SetVisibility(false);
309 mOverlayActor->SetOpacity(0.5);
310 this->UpdateDisplayExtent();
313 //stupid but necessary : the Overlay need to be rendered before fusion
316 this->GetRenderer()->RemoveActor(mFusionActor);
317 this->GetRenderer()->AddActor(mOverlayActor);
318 this->GetRenderer()->AddActor(mFusionActor);
321 this->GetRenderer()->AddActor(mOverlayActor);
324 SetTSlice(mCurrentTSlice);
327 //------------------------------------------------------------------------------
330 //------------------------------------------------------------------------------
331 void vvSlicer::SetFusion(vvImage::Pointer fusion)
333 if (fusion->GetVTKImages().size())
338 mFusionMapper = vtkImageMapToWindowLevelColors::New();
339 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
343 mFusionActor = vtkImageActor::New();
344 mFusionActor->SetInput(mFusionMapper->GetOutput());
345 mFusionActor->SetPickable(0);
346 mFusionActor->SetVisibility(false);
347 mFusionActor->SetOpacity(0.7);
348 this->UpdateDisplayExtent();
349 this->GetRenderer()->AddActor(mFusionActor);
353 SetTSlice(mCurrentTSlice);
356 //------------------------------------------------------------------------------
359 //------------------------------------------------------------------------------
360 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
362 if (actor_type == "vector")
364 this->mVFActor->SetVisibility(vis);
366 if (actor_type == "overlay")
368 this->mOverlayActor->SetVisibility(vis);
370 if (actor_type == "fusion")
372 this->mFusionActor->SetVisibility(vis);
374 if (actor_type == "contour")
375 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
376 UpdateDisplayExtent();
378 //------------------------------------------------------------------------------
381 //------------------------------------------------------------------------------
382 void vvSlicer::SetVF(vvImage::Pointer vf)
384 if (vf->GetVTKImages().size())
390 mAAFilter=vtkAssignAttribute::New();
391 mVOIFilter = vtkExtractVOI::New();
392 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
394 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
395 mAAFilter->SetInput(mVOIFilter->GetOutput());
396 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
397 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
400 mArrow = vvGlyphSource::New();
401 mArrow->SetGlyphTypeToSpecificArrow();
402 mArrow->SetScale(mScale);
405 // Glyph the gradient vector (with arrows)
407 mGlyphFilter = vvGlyph2D::New();
408 mGlyphFilter->SetInput(mAAFilter->GetOutput());
409 mGlyphFilter->SetSource(mArrow->GetOutput());
410 mGlyphFilter->ScalingOn();
411 mGlyphFilter->SetScaleModeToScaleByVector();
412 mGlyphFilter->OrientOn();
413 mGlyphFilter->SetVectorModeToUseVector();
414 mGlyphFilter->SetColorModeToColorByVector();
417 mVFMapper = vtkPolyDataMapper::New();
418 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
419 mVFMapper->SetInput(mGlyphFilter->GetOutput());
420 mVFMapper->ImmediateModeRenderingOn();
423 mVFActor = vtkActor::New();
424 mVFActor->SetMapper(mVFMapper);
425 mVFActor->SetPickable(0);
426 this->UpdateDisplayExtent();
427 this->GetRenderer()->AddActor(mVFActor);
430 SetTSlice(mCurrentTSlice);
433 //------------------------------------------------------------------------------
436 //------------------------------------------------------------------------------
437 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
439 mLandmarks = landmarks;
444 mCross = vtkCursor3D::New();
445 mCross->SetFocalPoint(0.0,0.0,0.0);
446 mCross->SetModelBounds(-10,10,-10,10,-10,10);
451 mLandGlyph = vtkGlyph3D::New();
452 mLandGlyph->SetSource(mCross->GetOutput());
453 mLandGlyph->SetInput(landmarks->GetOutput());
454 //mLandGlyph->SetIndexModeToScalar();
455 mLandGlyph->SetRange(0,1);
456 mLandGlyph->ScalingOff();
458 mLandGlyph->SetColorModeToColorByScalar();
461 mClipBox = vtkBox::New();
463 mLandClipper = vtkClipPolyData::New();
464 mLandClipper->InsideOutOn();
465 mLandClipper->SetInput(mLandGlyph->GetOutput());
466 mLandClipper->SetClipFunction(mClipBox);
469 mLandMapper = vtkPolyDataMapper::New();
470 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
471 //mLandMapper->ScalarVisibilityOff();
474 mLandActor = vtkActor::New();
475 mLandActor->SetMapper(mLandMapper);
476 mLandActor->GetProperty()->SetColor(255,10,212);
477 mLandActor->SetPickable(0);
478 mLandActor->SetVisibility(true);
479 this->UpdateDisplayExtent();
480 this->GetRenderer()->AddActor(mLandActor);
483 //------------------------------------------------------------------------------
485 //------------------------------------------------------------------------------
486 //FIXME: this function leaks memory, we should fix it someday :)
487 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
489 if (actor_type == "vector")
491 Renderer->RemoveActor(mVFActor);
500 if (actor_type == "overlay")
502 Renderer->RemoveActor(mOverlayActor);
504 mOverlayActor = NULL;
505 mOverlayMapper = NULL;
507 if (actor_type == "fusion")
509 Renderer->RemoveActor(mFusionActor);
512 mFusionMapper = NULL;
514 if (actor_type == "contour")
516 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
517 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
520 //------------------------------------------------------------------------------
523 //------------------------------------------------------------------------------
524 void vvSlicer::SetVFSubSampling(int sub)
528 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
531 UpdateDisplayExtent();
534 //------------------------------------------------------------------------------
537 //------------------------------------------------------------------------------
538 void vvSlicer::SetVFScale(int scale)
542 mArrow->SetScale(mScale);
543 UpdateDisplayExtent();
546 //------------------------------------------------------------------------------
549 //------------------------------------------------------------------------------
550 void vvSlicer::SetVFLog(int log)
555 mGlyphFilter->SetUseLog(mVFLog);
556 mGlyphFilter->Modified();
558 UpdateDisplayExtent();
561 //------------------------------------------------------------------------------
564 //------------------------------------------------------------------------------
565 void vvSlicer::SetTSlice(int t)
569 else if ((unsigned int)t >= mImage->GetVTKImages().size())
570 t = mImage->GetVTKImages().size() -1;
572 if (mCurrentTSlice == t) return;
575 this->SetInput(mImage->GetVTKImages()[t]);
576 if (mVF && mVFActor->GetVisibility())
578 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
579 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
581 if (mOverlay && mOverlayActor->GetVisibility())
583 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
584 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
586 if (mFusion && mFusionActor->GetVisibility())
588 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
589 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
591 if (mSurfaceCutActors.size() > 0)
592 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
593 i!=mSurfaceCutActors.end();i++)
594 (*i)->SetTimeSlice(mCurrentTSlice);
595 UpdateDisplayExtent();
597 //------------------------------------------------------------------------------
600 //------------------------------------------------------------------------------
601 int vvSlicer::GetTSlice()
603 return mCurrentTSlice;
605 //------------------------------------------------------------------------------
608 //------------------------------------------------------------------------------
609 void vvSlicer::SetSliceOrientation(int orientation)
611 //if 2D image, force to watch in Axial View
613 this->GetInput()->GetWholeExtent(extent);
614 if (extent[5]-extent[4] <= 2)
617 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
618 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
620 vtkErrorMacro("Error - invalid slice orientation " << orientation);
624 this->SliceOrientation = orientation;
627 int *range = this->GetSliceRange();
629 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
631 this->UpdateOrientation();
632 this->UpdateDisplayExtent();
634 if (this->Renderer && this->GetInput())
636 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
637 this->Renderer->ResetCamera();
638 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
643 //----------------------------------------------------------------------------
646 //----------------------------------------------------------------------------
647 int * vvSlicer::GetExtent() {
649 if (mUseReducedExtent) {
650 w_ext = mReducedExtent;
652 else w_ext = GetInput()->GetWholeExtent();
655 //----------------------------------------------------------------------------
658 //----------------------------------------------------------------------------
659 int vvSlicer::GetOrientation() {
660 return this->SliceOrientation;
662 //----------------------------------------------------------------------------
665 //----------------------------------------------------------------------------
666 void vvSlicer::UpdateDisplayExtent()
668 vtkImageData *input = this->GetInput();
669 if (!input || !this->ImageActor)
673 input->UpdateInformation();
674 int *w_ext;// = input->GetWholeExtent();
676 if (mUseReducedExtent) {
677 w_ext = mReducedExtent;
679 else w_ext = input->GetWholeExtent();
681 switch (this->SliceOrientation)
683 case vtkImageViewer2::SLICE_ORIENTATION_XY:
684 this->ImageActor->SetDisplayExtent(
685 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
686 if (mVF && mVFActor->GetVisibility())
689 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
690 mVOIFilter->SetVOI(vfExtent);
691 mGlyphFilter->SetOrientation(1,1,0);
693 // put the vector field between the image and the camera
694 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
695 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
697 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
699 if (mOverlay && mOverlayActor->GetVisibility())
702 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
703 mOverlayActor->SetDisplayExtent(overExtent);
704 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
705 mOverlayActor->SetPosition(0,0,1);
707 mOverlayActor->SetPosition(0,0,-1);
709 if (mFusion && mFusionActor->GetVisibility())
712 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
713 mFusionActor->SetDisplayExtent(fusExtent);
714 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
715 mFusionActor->SetPosition(0,0,1.5);
717 mFusionActor->SetPosition(0,0,-1.5);
724 bounds[0] = ImageActor->GetBounds()[0];
725 bounds[1] = ImageActor->GetBounds()[1];
726 bounds[2] = ImageActor->GetBounds()[2];
727 bounds[3] = ImageActor->GetBounds()[3];
728 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
729 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
730 mClipBox->SetBounds(bounds);
733 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
734 mLandActor->SetPosition(0,0,1.5);
736 mLandActor->SetPosition(0,0,-1.5);
740 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
741 this->ImageActor->SetDisplayExtent(
742 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
743 if (mVF && mVFActor->GetVisibility())
746 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
747 mVOIFilter->SetVOI(vfExtent);
748 mGlyphFilter->SetOrientation(1,0,1);
750 // put the vector field between the image aSpacingnd the camera
751 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
752 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
754 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
756 if (mOverlay && mOverlayActor->GetVisibility())
759 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
760 mOverlayActor->SetDisplayExtent(overExtent);
761 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
762 mOverlayActor->SetPosition(0,1,0);
764 mOverlayActor->SetPosition(0,-1,0);
766 if (mFusion && mFusionActor->GetVisibility())
769 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
770 mFusionActor->SetDisplayExtent(fusExtent);
771 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
772 mFusionActor->SetPosition(0,1.5,0);
774 mFusionActor->SetPosition(0,-1.5,0);
781 bounds[0] = ImageActor->GetBounds()[0];
782 bounds[1] = ImageActor->GetBounds()[1];
783 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
784 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
785 bounds[4] = ImageActor->GetBounds()[4];
786 bounds[5] = ImageActor->GetBounds()[5];
787 mClipBox->SetBounds(bounds);
790 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
791 mLandActor->SetPosition(0,1.5,0);
793 mLandActor->SetPosition(0,-1.5,0);
797 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
798 this->ImageActor->SetDisplayExtent(
799 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
800 if (mVF && mVFActor->GetVisibility())
803 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
804 mVOIFilter->SetVOI(vfExtent);
805 mGlyphFilter->SetOrientation(0,1,1);
807 // put the vector field between the image and the camera
808 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
809 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
811 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
813 if (mOverlay && mOverlayActor->GetVisibility())
816 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
817 mOverlayActor->SetDisplayExtent(overExtent);
818 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
819 mOverlayActor->SetPosition(1,0,0);
821 mOverlayActor->SetPosition(-1,0,0);
823 if (mFusion && mFusionActor->GetVisibility())
826 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
827 mFusionActor->SetDisplayExtent(fusExtent);
828 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
829 mFusionActor->SetPosition(1.5,0,0);
831 mFusionActor->SetPosition(-1.5,0,0);
838 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
839 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
840 bounds[2] = ImageActor->GetBounds()[2];
841 bounds[3] = ImageActor->GetBounds()[3];
842 bounds[4] = ImageActor->GetBounds()[4];
843 bounds[5] = ImageActor->GetBounds()[5];
844 mClipBox->SetBounds(bounds);
847 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
848 mLandActor->SetPosition(1.5,0,0);
850 mLandActor->SetPosition(-1.5,0,0);
855 // Figure out the correct clipping range
859 if (this->InteractorStyle &&
860 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
862 this->Renderer->ResetCameraClippingRange();
866 vtkCamera *cam = this->Renderer->GetActiveCamera();
870 this->ImageActor->GetBounds(bounds);
871 double spos = (double)bounds[this->SliceOrientation * 2];
872 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
873 double range = fabs(spos - cpos);
874 double *spacing = input->GetSpacing();
876 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
877 cam->SetClippingRange(
878 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
883 //----------------------------------------------------------------------------
886 //----------------------------------------------------------------------------
887 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
889 vtkImageData* image=this->GetInput();
890 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
891 mVF->GetSpacing()[0];
892 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
893 mVF->GetSpacing()[0];
894 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
895 mVF->GetSpacing()[1];
896 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
897 mVF->GetSpacing()[1];
898 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
899 mVF->GetSpacing()[2];
900 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
901 mVF->GetSpacing()[2];
903 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
905 //----------------------------------------------------------------------------
908 //----------------------------------------------------------------------------
909 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
911 vtkImageData* image=this->GetInput();
912 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
913 mOverlay->GetSpacing()[0];
914 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
915 mOverlay->GetSpacing()[0];
916 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
917 mOverlay->GetSpacing()[1];
918 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
919 mOverlay->GetSpacing()[1];
920 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
921 mOverlay->GetSpacing()[2];
922 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
923 mOverlay->GetSpacing()[2];
924 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
926 //----------------------------------------------------------------------------
929 //----------------------------------------------------------------------------
930 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
932 vtkImageData* image=this->GetInput();
933 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
934 mFusion->GetSpacing()[0];
935 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
936 mFusion->GetSpacing()[0];
937 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
938 mFusion->GetSpacing()[1];
939 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
940 mFusion->GetSpacing()[1];
941 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
942 mFusion->GetSpacing()[2];
943 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
944 mFusion->GetSpacing()[2];
945 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
947 //----------------------------------------------------------------------------
950 //----------------------------------------------------------------------------
951 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
956 //2D overlay on 3D image specific case
957 if (refExtent[4] == refExtent[5])
960 extent[4] = refExtent[4];
961 extent[5] = refExtent[5];
964 for (int i = 0; i < maxBound; i = i+2)
966 //if we are totally outside the image
967 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] )
972 //crop to the limit of the image
973 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
974 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
975 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
976 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
979 for (int i = 0; i < maxBound; i = i+2)
981 extent[i] = refExtent[i];
982 extent[i+1] = refExtent[i];
985 //----------------------------------------------------------------------------
988 //----------------------------------------------------------------------------
989 void vvSlicer::UpdateOrientation()
991 // Set the camera position
992 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
995 switch (this->SliceOrientation)
997 case vtkImageViewer2::SLICE_ORIENTATION_XY:
998 cam->SetFocalPoint(0,0,0);
999 cam->SetPosition(0,0,-1); // -1 if medical ?
1000 cam->SetViewUp(0,-1,0);
1003 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1004 cam->SetFocalPoint(0,0,0);
1005 cam->SetPosition(0,-1,0); // 1 if medical ?
1006 cam->SetViewUp(0,0,1);
1009 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1010 cam->SetFocalPoint(0,0,0);
1011 cam->SetPosition(-1,0,0); // -1 if medical ?
1012 cam->SetViewUp(0,0,1);
1017 //----------------------------------------------------------------------------
1020 //----------------------------------------------------------------------------
1021 void vvSlicer::SetOpacity(double s)
1023 this->GetImageActor()->SetOpacity(s);
1025 //----------------------------------------------------------------------------
1028 //----------------------------------------------------------------------------
1029 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1031 this->Superclass::SetRenderWindow(rw);
1032 this->SetupInteractor(rw->GetInteractor());
1033 ca->SetImageActor(this->GetImageActor());
1034 ca->SetWindowLevel(this->GetWindowLevel());
1035 ca->SetText(2, "<slice>");
1036 ca->SetText(3, "<window>\n<level>");
1048 crossCursor->SetModelBounds(bounds);
1049 this->GetRenderer()->AddActor(pdmA);
1050 this->GetRenderer()->AddActor(ca);
1051 this->GetRenderer()->ResetCamera();
1053 //this is just a mapping between the labeling of the orientations presented to the user and
1054 //the one used by vtk
1055 SetSliceOrientation(2-(orientation%3));
1058 //----------------------------------------------------------------------------
1061 //----------------------------------------------------------------------------
1062 void vvSlicer::ResetCamera()
1064 if (this->GetInput())
1066 double* input_bounds=this->GetInput()->GetBounds();
1067 double bmax=input_bounds[1]-input_bounds[0];
1068 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
1069 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
1070 this->GetRenderer()->ResetCamera();
1071 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
1074 //----------------------------------------------------------------------------
1077 //----------------------------------------------------------------------------
1078 void vvSlicer::SetDisplayMode(bool i)
1080 this->GetImageActor()->SetVisibility(i);
1081 this->GetAnnotation()->SetVisibility(i);
1082 this->GetRenderer()->SetDraw(i);
1084 mLandActor->SetVisibility(i);
1085 pdmA->SetVisibility(i);
1087 UpdateDisplayExtent();
1089 //----------------------------------------------------------------------------
1092 //----------------------------------------------------------------------------
1093 void vvSlicer::FlipHorizontalView()
1095 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1098 double *position = cam->GetPosition();
1099 switch (this->SliceOrientation)
1101 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1102 cam->SetPosition(position[0],position[1],-position[2]);
1105 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1106 cam->SetPosition(position[0],-position[1],position[2]);
1109 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1110 cam->SetPosition(-position[0],position[1],position[2]);
1113 this->Renderer->ResetCameraClippingRange();
1114 this->UpdateDisplayExtent();
1117 //----------------------------------------------------------------------------
1120 //----------------------------------------------------------------------------
1121 void vvSlicer::FlipVerticalView()
1123 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1126 FlipHorizontalView();
1127 double *viewup = cam->GetViewUp();
1128 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1129 this->UpdateDisplayExtent();
1132 //----------------------------------------------------------------------------
1135 //----------------------------------------------------------------------------
1136 void vvSlicer::SetColorWindow(double window)
1138 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1141 double level = this->GetWindowLevel()->GetLevel();
1142 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1145 this->vtkImageViewer2::SetColorWindow(window);
1147 //----------------------------------------------------------------------------
1150 //----------------------------------------------------------------------------
1151 void vvSlicer::SetColorLevel(double level)
1153 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1156 double window = this->GetWindowLevel()->GetWindow();
1157 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1160 this->vtkImageViewer2::SetColorLevel(level);
1162 //----------------------------------------------------------------------------
1164 //----------------------------------------------------------------------------
1165 // Returns the min an the max value in a 41x41 region around the mouse pointer
1166 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1168 //Get mouse pointer position in view coordinates
1169 double fLocalExtents[6];
1170 for(int i=0; i<3; i++)
1172 fLocalExtents[i*2 ] = mCurrent[i];
1173 fLocalExtents[i*2+1] = mCurrent[i];
1175 this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1176 this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1177 for(int i=0; i<3; i++)
1179 if (i!=SliceOrientation) //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1181 fLocalExtents[i*2 ] -= 0.2;
1182 fLocalExtents[i*2+1] += 0.2;
1185 this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1186 this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1188 //Convert to image pixel coordinates (rounded)
1189 int iLocalExtents[6];
1190 for(int i=0; i<3; i++)
1192 fLocalExtents[i*2 ] = (fLocalExtents[i*2 ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1193 fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1195 iLocalExtents[i*2 ] = lrint(fLocalExtents[i*2 ]);
1196 iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1198 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1199 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1202 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkExtractVOI::New();
1203 voiFilter->SetInput(this->GetInput());
1204 voiFilter->SetVOI(iLocalExtents);
1205 voiFilter->Update();
1206 if (!voiFilter->GetOutput()->GetNumberOfPoints())
1213 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkImageAccumulate::New();
1214 accFilter->SetInput(voiFilter->GetOutput());
1215 accFilter->Update();
1217 min = *(accFilter->GetMin());
1218 max = *(accFilter->GetMax());
1220 //----------------------------------------------------------------------------
1222 //----------------------------------------------------------------------------
1223 void vvSlicer::Render()
1226 //DD(SliceOrientation);
1227 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion)
1229 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1230 legend->SetVisibility(1);
1232 else legend->SetVisibility(0);
1234 if (ca->GetVisibility())
1236 std::string worldPos = "";
1237 std::stringstream world1;
1238 std::stringstream world2;
1239 std::stringstream world3;
1240 world1 << (int)mCurrent[0];
1241 world2 << (int)mCurrent[1];
1242 world3 << (int)mCurrent[2];
1243 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1244 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1245 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1247 if (pdmA->GetVisibility())
1249 double x = mCursor[0];
1250 double y = mCursor[1];
1251 double z = mCursor[2];
1252 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1253 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1254 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1256 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1257 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1258 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1259 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1260 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1261 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1263 vtkRenderer * renderer = this->Renderer;
1265 renderer->WorldToView(x,y,z);
1266 renderer->ViewToNormalizedViewport(x,y,z);
1267 renderer->NormalizedViewportToViewport(x,y);
1268 renderer->ViewportToNormalizedDisplay(x,y);
1269 renderer->NormalizedDisplayToDisplay(x,y);
1270 crossCursor->SetFocalPoint(x,y,z);
1273 crossCursor->SetFocalPoint(-1,-1,z);
1276 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1277 X <= this->GetInput()->GetWholeExtent()[1] &&
1278 Y >= this->GetInput()->GetWholeExtent()[2] &&
1279 Y <= this->GetInput()->GetWholeExtent()[3] &&
1280 Z >= this->GetInput()->GetWholeExtent()[4] &&
1281 Z <= this->GetInput()->GetWholeExtent()[5])
1283 std::stringstream pixel1;
1284 std::stringstream pixel2;
1285 std::stringstream pixel3;
1286 std::stringstream temps;
1290 temps << mCurrentTSlice;
1291 double value = this->GetInput()->GetScalarComponentAsDouble(lrint(X),
1295 std::stringstream val;
1297 worldPos += "data value : " + val.str();
1298 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " +
1299 world3.str() + " " + temps.str();
1300 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " +
1301 pixel3.str() + " " + temps.str();
1303 ca->SetText(1,worldPos.c_str());
1305 if (mOverlay && mOverlayActor->GetVisibility())
1307 mOverlayMapper->SetWindow(this->GetColorWindow());
1308 mOverlayMapper->SetLevel(this->GetColorLevel());
1309 mOverlayMapper->Update();
1313 //this->Superclass::Render();
1314 this->GetRenderWindow()->Render();
1316 //----------------------------------------------------------------------------
1319 //----------------------------------------------------------------------------
1320 void vvSlicer::UpdateCursorPosition()
1322 if (this->GetImageActor()->GetVisibility())
1324 pdmA->SetVisibility(true);
1325 mCursor[0] = mCurrent[0];
1326 mCursor[1] = mCurrent[1];
1327 mCursor[2] = mCurrent[2];
1328 mCursor[3] = mCurrentTSlice;
1331 //----------------------------------------------------------------------------
1334 //----------------------------------------------------------------------------
1335 void vvSlicer::UpdateLandmarks()
1337 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1338 if (pd->GetPoints())
1340 mLandGlyph->SetRange(0,1);
1341 mLandGlyph->Modified();
1342 mLandGlyph->Update();
1344 mClipBox->Modified();
1345 mLandClipper->Update();
1346 mLandMapper->Update();
1350 //----------------------------------------------------------------------------
1353 //----------------------------------------------------------------------------
1354 void vvSlicer::SetSlice(int slice)
1356 int *range = this->GetSliceRange();
1359 if (slice < range[0])
1363 else if (slice > range[1])
1369 if (this->Slice == slice)
1374 this->Slice = slice;
1377 this->UpdateDisplayExtent();
1379 // DD("SetSlice de slicer = Render");
1383 //----------------------------------------------------------------------------
1386 //----------------------------------------------------------------------------
1387 void vvSlicer::SetContourSlice()
1389 if (mSurfaceCutActors.size() > 0)
1390 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1391 i!=mSurfaceCutActors.end();i++)
1392 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1393 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1395 //----------------------------------------------------------------------------
1398 //----------------------------------------------------------------------------
1399 void vvSlicer::ForceUpdateDisplayExtent()
1401 this->UpdateDisplayExtent();
1403 //----------------------------------------------------------------------------
1406 //----------------------------------------------------------------------------
1407 int* vvSlicer::GetDisplayExtent()
1409 return this->GetImageActor()->GetDisplayExtent();
1411 //----------------------------------------------------------------------------
1414 //----------------------------------------------------------------------------
1415 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1417 this->Superclass::PrintSelf(os, indent);
1419 //----------------------------------------------------------------------------