1 /*=========================================================================
5 Author : Pierre Seroul (pierre.seroul@gmail.com)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 =========================================================================*/
27 #include "vvSlicerManagerCommand.h"
28 #include "vvGlyphSource.h"
29 #include "vvGlyph2D.h"
30 #include "vvImageMapToWLColors.h"
32 #include <vtkTextProperty.h>
33 #include <vtkTextActor.h>
34 #include <vtkTextSource.h>
35 #include <vtkActor2D.h>
36 #include <vtkCursor2D.h>
37 #include <vtkPolyDataMapper2D.h>
38 #include <vtkProperty2D.h>
39 #include <vtkCornerAnnotation.h>
40 #include <vtkImageMapToWindowLevelColors.h>
41 #include <vtkImageData.h>
42 #include <vtkImageActor.h>
43 #include <vtkToolkits.h>
44 #include <vtkObjectFactory.h>
45 #include <vtkPointData.h>
46 #include <vtkDataArray.h>
47 #include <vtkFloatArray.h>
48 #include <vtkClipPolyData.h>
49 #include <vtkGlyph3D.h>
51 #include <vtkCursor3D.h>
52 #include <vtkProperty.h>
54 #include <vtkLightCollection.h>
55 #include <vtkScalarBarActor.h>
56 #include <vtkLookupTable.h>
58 #include <vtkRenderer.h>
59 #include <vtkRendererCollection.h>
60 #include <vtkRenderWindow.h>
61 #include <vtkRenderWindowInteractor.h>
62 #include <vtkCamera.h>
63 #include <vtkCallbackCommand.h>
64 #include <vtkCommand.h>
65 #include <vtkPolyDataMapper.h>
68 #include <vtkExtractVOI.h>
69 #include <vtkSphereSource.h>
70 #include <vtkCutter.h>
71 #include <vtkAssignAttribute.h>
72 #include <vtkImageAccumulate.h>
74 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
75 vtkStandardNewMacro(vvSlicer);
77 //------------------------------------------------------------------------------
82 mUseReducedExtent = false;
84 mCurrent[0] = -VTK_DOUBLE_MAX;
85 mCurrent[1] = -VTK_DOUBLE_MAX;
86 mCurrent[2] = -VTK_DOUBLE_MAX;
88 mCursor[0] = -VTK_DOUBLE_MAX;
89 mCursor[1] = -VTK_DOUBLE_MAX;
90 mCursor[2] = -VTK_DOUBLE_MAX;
91 mCursor[3] = -VTK_DOUBLE_MAX;
97 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
98 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
99 text += "0,1,2,3,4,5 : preset windowing\n";
100 text += "6,7,8,9 : preset colormap\n";
101 text += "z : local windowing\n";
102 text += "r : reset view\n";
103 text += "l : reload image\n";
104 text += "f : fly to mouse position\n";
105 text += "g : go to cross hair position\n\n";
106 text += "Up,down : change slice\n";
107 text += "Left,right : change tenporal slice\n\n";
108 text += "Scrollbar (or w/x) : zoom in/out\n";
109 text += "left button : synchronize all views\n";
110 text += "middle button : grab image\n";
111 text += "right button : change windowing\n";
113 crossCursor = vtkCursor2D::New();
114 crossCursor->AllOff();
115 crossCursor->AxesOn();
116 crossCursor->SetTranslationMode(1);
117 crossCursor->SetRadius(2);
119 pdm = vtkPolyDataMapper2D::New();
120 pdm->SetInput(crossCursor->GetOutput());
122 pdmA = vtkActor2D::New();
123 pdmA->SetMapper(pdm);
124 pdmA->GetProperty()->SetColor(255,10,212);
125 pdmA->SetVisibility(0);
126 pdmA->SetPickable(0);
128 ca = vtkCornerAnnotation::New();
129 ca->GetTextProperty()->SetColor(255,10,212);
130 ca->SetVisibility(1);
138 legend = vtkSmartPointer<vtkScalarBarActor>::New();
139 //legend->SetTitle("test!");
140 legend->SetPosition(0.82,0.18);
141 legend->SetWidth(0.1);
142 legend->SetVisibility(0);
143 legend->SetLabelFormat("%.1f");
144 this->GetRenderer()->AddActor(legend);
146 this->WindowLevel->Delete();
147 this->WindowLevel = vvImageMapToWLColors::New();
148 this->InstallPipeline();
150 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
155 return mOverlayMapper.GetPointer();
157 //------------------------------------------------------------------------------
160 //------------------------------------------------------------------------------
161 vtkImageActor* vvSlicer::GetOverlayActor() {
162 return mOverlayActor.GetPointer();
164 //------------------------------------------------------------------------------
167 //------------------------------------------------------------------------------
168 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
169 return mFusionMapper.GetPointer();
171 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 vtkImageActor* vvSlicer::GetFusionActor() {
176 return mFusionActor.GetPointer();
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 vtkActor* vvSlicer::GetVFActor() {
183 return mVFActor.GetPointer();
185 //------------------------------------------------------------------------------
188 //------------------------------------------------------------------------------
189 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
190 return ca.GetPointer();
192 //------------------------------------------------------------------------------
195 //------------------------------------------------------------------------------
196 void vvSlicer::EnableReducedExtent(bool b) {
197 mUseReducedExtent = b;
199 //------------------------------------------------------------------------------
202 //------------------------------------------------------------------------------
203 void vvSlicer::SetReducedExtent(int * ext) {
204 mReducedExtent = ext;
206 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
210 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
213 mSurfaceCutActors.push_back(new vvMeshActor());
215 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
217 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
218 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
219 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
223 //------------------------------------------------------------------------------
226 //------------------------------------------------------------------------------
227 void vvSlicer::ToggleContourSuperposition()
229 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
230 i!=mSurfaceCutActors.end();i++)
231 (*i)->ToggleSuperposition();
233 //------------------------------------------------------------------------------
236 //------------------------------------------------------------------------------
237 void vvSlicer::SetCursorColor(int r,int g, int b)
239 pdmA->GetProperty()->SetColor(r,g,b);
241 //------------------------------------------------------------------------------
244 //------------------------------------------------------------------------------
245 void vvSlicer::SetCursorVisibility(bool s)
247 pdmA->SetVisibility(s);
249 //------------------------------------------------------------------------------
252 //------------------------------------------------------------------------------
253 bool vvSlicer::GetCursorVisibility()
255 return pdmA->GetVisibility();
257 //------------------------------------------------------------------------------
260 //------------------------------------------------------------------------------
261 vvSlicer::~vvSlicer()
263 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
264 i!=mSurfaceCutActors.end();i++)
267 //------------------------------------------------------------------------------
270 //------------------------------------------------------------------------------
271 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
278 //------------------------------------------------------------------------------
281 //------------------------------------------------------------------------------
282 void vvSlicer::SetImage(vvImage::Pointer image)
284 if (image->GetVTKImages().size())
287 this->Superclass::SetInput(image->GetVTKImages()[0]);
288 this->UpdateDisplayExtent();
290 ca->SetText(0,mFileName.c_str());
293 //------------------------------------------------------------------------------
296 //------------------------------------------------------------------------------
297 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
299 if (overlay->GetVTKImages().size())
304 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
305 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
309 mOverlayActor = vtkImageActor::New();
310 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
311 mOverlayActor->SetPickable(0);
312 mOverlayActor->SetVisibility(false);
313 mOverlayActor->SetOpacity(0.5);
314 this->UpdateDisplayExtent();
317 //stupid but necessary : the Overlay need to be rendered before fusion
320 this->GetRenderer()->RemoveActor(mFusionActor);
321 this->GetRenderer()->AddActor(mOverlayActor);
322 this->GetRenderer()->AddActor(mFusionActor);
325 this->GetRenderer()->AddActor(mOverlayActor);
328 SetTSlice(mCurrentTSlice);
331 //------------------------------------------------------------------------------
334 //------------------------------------------------------------------------------
335 void vvSlicer::SetFusion(vvImage::Pointer fusion)
337 if (fusion->GetVTKImages().size())
342 mFusionMapper = vtkImageMapToWindowLevelColors::New();
343 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
347 mFusionActor = vtkImageActor::New();
348 mFusionActor->SetInput(mFusionMapper->GetOutput());
349 mFusionActor->SetPickable(0);
350 mFusionActor->SetVisibility(false);
351 mFusionActor->SetOpacity(0.7);
352 this->UpdateDisplayExtent();
353 this->GetRenderer()->AddActor(mFusionActor);
357 SetTSlice(mCurrentTSlice);
360 //------------------------------------------------------------------------------
363 //------------------------------------------------------------------------------
364 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
366 if (actor_type == "vector")
368 this->mVFActor->SetVisibility(vis);
370 if (actor_type == "overlay")
372 this->mOverlayActor->SetVisibility(vis);
374 if (actor_type == "fusion")
376 this->mFusionActor->SetVisibility(vis);
378 if (actor_type == "contour")
379 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
380 UpdateDisplayExtent();
382 //------------------------------------------------------------------------------
385 //------------------------------------------------------------------------------
386 void vvSlicer::SetVF(vvImage::Pointer vf)
388 if (vf->GetVTKImages().size())
394 mAAFilter=vtkAssignAttribute::New();
395 mVOIFilter = vtkExtractVOI::New();
396 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
398 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
399 mAAFilter->SetInput(mVOIFilter->GetOutput());
400 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
401 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
404 mArrow = vvGlyphSource::New();
405 mArrow->SetGlyphTypeToSpecificArrow();
406 mArrow->SetScale(mScale);
409 // Glyph the gradient vector (with arrows)
411 mGlyphFilter = vvGlyph2D::New();
412 mGlyphFilter->SetInput(mAAFilter->GetOutput());
413 mGlyphFilter->SetSource(mArrow->GetOutput());
414 mGlyphFilter->ScalingOn();
415 mGlyphFilter->SetScaleModeToScaleByVector();
416 mGlyphFilter->OrientOn();
417 mGlyphFilter->SetVectorModeToUseVector();
418 mGlyphFilter->SetColorModeToColorByVector();
421 mVFMapper = vtkPolyDataMapper::New();
422 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
423 mVFMapper->SetInput(mGlyphFilter->GetOutput());
424 mVFMapper->ImmediateModeRenderingOn();
427 mVFActor = vtkActor::New();
428 mVFActor->SetMapper(mVFMapper);
429 mVFActor->SetPickable(0);
430 this->UpdateDisplayExtent();
431 this->GetRenderer()->AddActor(mVFActor);
434 SetTSlice(mCurrentTSlice);
437 //------------------------------------------------------------------------------
440 //------------------------------------------------------------------------------
441 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
443 mLandmarks = landmarks;
448 mCross = vtkCursor3D::New();
449 mCross->SetFocalPoint(0.0,0.0,0.0);
450 mCross->SetModelBounds(-10,10,-10,10,-10,10);
455 mLandGlyph = vtkGlyph3D::New();
456 mLandGlyph->SetSource(mCross->GetOutput());
457 mLandGlyph->SetInput(landmarks->GetOutput());
458 //mLandGlyph->SetIndexModeToScalar();
459 mLandGlyph->SetRange(0,1);
460 mLandGlyph->ScalingOff();
462 mLandGlyph->SetColorModeToColorByScalar();
465 mClipBox = vtkBox::New();
467 mLandClipper = vtkClipPolyData::New();
468 mLandClipper->InsideOutOn();
469 mLandClipper->SetInput(mLandGlyph->GetOutput());
470 mLandClipper->SetClipFunction(mClipBox);
473 mLandMapper = vtkPolyDataMapper::New();
474 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
475 //mLandMapper->ScalarVisibilityOff();
478 mLandActor = vtkActor::New();
479 mLandActor->SetMapper(mLandMapper);
480 mLandActor->GetProperty()->SetColor(255,10,212);
481 mLandActor->SetPickable(0);
482 mLandActor->SetVisibility(true);
483 this->UpdateDisplayExtent();
484 this->GetRenderer()->AddActor(mLandActor);
487 //------------------------------------------------------------------------------
489 //------------------------------------------------------------------------------
490 //FIXME: this function leaks memory, we should fix it someday :)
491 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
493 if (actor_type == "vector")
495 Renderer->RemoveActor(mVFActor);
504 if (actor_type == "overlay")
506 Renderer->RemoveActor(mOverlayActor);
508 mOverlayActor = NULL;
509 mOverlayMapper = NULL;
511 if (actor_type == "fusion")
513 Renderer->RemoveActor(mFusionActor);
516 mFusionMapper = NULL;
518 if (actor_type == "contour")
520 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
521 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
524 //------------------------------------------------------------------------------
527 //------------------------------------------------------------------------------
528 void vvSlicer::SetVFSubSampling(int sub)
532 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
535 UpdateDisplayExtent();
538 //------------------------------------------------------------------------------
541 //------------------------------------------------------------------------------
542 void vvSlicer::SetVFScale(int scale)
546 mArrow->SetScale(mScale);
547 UpdateDisplayExtent();
550 //------------------------------------------------------------------------------
553 //------------------------------------------------------------------------------
554 void vvSlicer::SetVFLog(int log)
559 mGlyphFilter->SetUseLog(mVFLog);
560 mGlyphFilter->Modified();
562 UpdateDisplayExtent();
565 //------------------------------------------------------------------------------
568 //------------------------------------------------------------------------------
569 void vvSlicer::SetTSlice(int t)
573 else if ((unsigned int)t >= mImage->GetVTKImages().size())
574 t = mImage->GetVTKImages().size() -1;
576 this->SetInput(mImage->GetVTKImages()[t]);
577 if (mVF && mVFActor->GetVisibility())
579 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
580 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
582 if (mOverlay && mOverlayActor->GetVisibility())
584 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
585 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
587 if (mFusion && mFusionActor->GetVisibility())
589 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
590 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
592 if (mSurfaceCutActors.size() > 0)
593 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
594 i!=mSurfaceCutActors.end();i++)
595 (*i)->SetTimeSlice(mCurrentTSlice);
596 UpdateDisplayExtent();
598 //------------------------------------------------------------------------------
601 //------------------------------------------------------------------------------
602 int vvSlicer::GetTSlice()
604 return mCurrentTSlice;
606 //------------------------------------------------------------------------------
609 //------------------------------------------------------------------------------
610 void vvSlicer::SetSliceOrientation(int orientation)
612 //if 2D image, force to watch in Axial View
614 this->GetInput()->GetWholeExtent(extent);
615 if (extent[5]-extent[4] <= 2)
618 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
619 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
621 vtkErrorMacro("Error - invalid slice orientation " << orientation);
625 this->SliceOrientation = orientation;
628 int *range = this->GetSliceRange();
630 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
632 this->UpdateOrientation();
633 this->UpdateDisplayExtent();
635 if (this->Renderer && this->GetInput())
637 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
638 this->Renderer->ResetCamera();
639 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
645 //----------------------------------------------------------------------------
646 void vvSlicer::UpdateDisplayExtent()
648 vtkImageData *input = this->GetInput();
649 if (!input || !this->ImageActor)
653 input->UpdateInformation();
654 int *w_ext;// = input->GetWholeExtent();
656 if (mUseReducedExtent) {
657 w_ext = mReducedExtent;
659 else w_ext = input->GetWholeExtent();
661 switch (this->SliceOrientation)
663 case vtkImageViewer2::SLICE_ORIENTATION_XY:
664 this->ImageActor->SetDisplayExtent(
665 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
666 if (mVF && mVFActor->GetVisibility())
669 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
670 mVOIFilter->SetVOI(vfExtent);
671 mGlyphFilter->SetOrientation(1,1,0);
673 // put the vector field between the image and the camera
674 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
675 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
677 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
679 if (mOverlay && mOverlayActor->GetVisibility())
682 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
683 mOverlayActor->SetDisplayExtent(overExtent);
684 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
685 mOverlayActor->SetPosition(0,0,1);
687 mOverlayActor->SetPosition(0,0,-1);
689 if (mFusion && mFusionActor->GetVisibility())
692 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
693 mFusionActor->SetDisplayExtent(fusExtent);
694 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
695 mFusionActor->SetPosition(0,0,1.5);
697 mFusionActor->SetPosition(0,0,-1.5);
704 bounds[0] = ImageActor->GetBounds()[0];
705 bounds[1] = ImageActor->GetBounds()[1];
706 bounds[2] = ImageActor->GetBounds()[2];
707 bounds[3] = ImageActor->GetBounds()[3];
708 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
709 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
710 mClipBox->SetBounds(bounds);
713 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
714 mLandActor->SetPosition(0,0,1.5);
716 mLandActor->SetPosition(0,0,-1.5);
720 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
721 this->ImageActor->SetDisplayExtent(
722 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
723 if (mVF && mVFActor->GetVisibility())
726 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
727 mVOIFilter->SetVOI(vfExtent);
728 mGlyphFilter->SetOrientation(1,0,1);
730 // put the vector field between the image aSpacingnd the camera
731 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
732 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
734 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
736 if (mOverlay && mOverlayActor->GetVisibility())
739 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
740 mOverlayActor->SetDisplayExtent(overExtent);
741 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
742 mOverlayActor->SetPosition(0,1,0);
744 mOverlayActor->SetPosition(0,-1,0);
746 if (mFusion && mFusionActor->GetVisibility())
749 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
750 mFusionActor->SetDisplayExtent(fusExtent);
751 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
752 mFusionActor->SetPosition(0,1.5,0);
754 mFusionActor->SetPosition(0,-1.5,0);
761 bounds[0] = ImageActor->GetBounds()[0];
762 bounds[1] = ImageActor->GetBounds()[1];
763 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
764 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
765 bounds[4] = ImageActor->GetBounds()[4];
766 bounds[5] = ImageActor->GetBounds()[5];
767 mClipBox->SetBounds(bounds);
770 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
771 mLandActor->SetPosition(0,1.5,0);
773 mLandActor->SetPosition(0,-1.5,0);
777 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
778 this->ImageActor->SetDisplayExtent(
779 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
780 if (mVF && mVFActor->GetVisibility())
783 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
784 mVOIFilter->SetVOI(vfExtent);
785 mGlyphFilter->SetOrientation(0,1,1);
787 // put the vector field between the image and the camera
788 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
789 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
791 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
793 if (mOverlay && mOverlayActor->GetVisibility())
796 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
797 mOverlayActor->SetDisplayExtent(overExtent);
798 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
799 mOverlayActor->SetPosition(1,0,0);
801 mOverlayActor->SetPosition(-1,0,0);
803 if (mFusion && mFusionActor->GetVisibility())
806 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
807 mFusionActor->SetDisplayExtent(fusExtent);
808 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
809 mFusionActor->SetPosition(1.5,0,0);
811 mFusionActor->SetPosition(-1.5,0,0);
818 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
819 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
820 bounds[2] = ImageActor->GetBounds()[2];
821 bounds[3] = ImageActor->GetBounds()[3];
822 bounds[4] = ImageActor->GetBounds()[4];
823 bounds[5] = ImageActor->GetBounds()[5];
824 mClipBox->SetBounds(bounds);
827 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
828 mLandActor->SetPosition(1.5,0,0);
830 mLandActor->SetPosition(-1.5,0,0);
835 // Figure out the correct clipping range
839 if (this->InteractorStyle &&
840 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
842 this->Renderer->ResetCameraClippingRange();
846 vtkCamera *cam = this->Renderer->GetActiveCamera();
850 this->ImageActor->GetBounds(bounds);
851 double spos = (double)bounds[this->SliceOrientation * 2];
852 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
853 double range = fabs(spos - cpos);
854 double *spacing = input->GetSpacing();
856 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
857 cam->SetClippingRange(
858 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
863 //----------------------------------------------------------------------------
866 //----------------------------------------------------------------------------
867 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
869 vtkImageData* image=this->GetInput();
870 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
871 mVF->GetSpacing()[0];
872 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
873 mVF->GetSpacing()[0];
874 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
875 mVF->GetSpacing()[1];
876 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
877 mVF->GetSpacing()[1];
878 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
879 mVF->GetSpacing()[2];
880 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
881 mVF->GetSpacing()[2];
883 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
885 //----------------------------------------------------------------------------
888 //----------------------------------------------------------------------------
889 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
891 vtkImageData* image=this->GetInput();
892 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
893 mOverlay->GetSpacing()[0];
894 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
895 mOverlay->GetSpacing()[0];
896 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
897 mOverlay->GetSpacing()[1];
898 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
899 mOverlay->GetSpacing()[1];
900 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
901 mOverlay->GetSpacing()[2];
902 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
903 mOverlay->GetSpacing()[2];
904 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
906 //----------------------------------------------------------------------------
909 //----------------------------------------------------------------------------
910 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
912 vtkImageData* image=this->GetInput();
913 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
914 mFusion->GetSpacing()[0];
915 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
916 mFusion->GetSpacing()[0];
917 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
918 mFusion->GetSpacing()[1];
919 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
920 mFusion->GetSpacing()[1];
921 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
922 mFusion->GetSpacing()[2];
923 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
924 mFusion->GetSpacing()[2];
925 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
927 //----------------------------------------------------------------------------
930 //----------------------------------------------------------------------------
931 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
936 //2D overlay on 3D image specific case
937 if (refExtent[4] == refExtent[5])
940 extent[4] = refExtent[4];
941 extent[5] = refExtent[5];
944 for (int i = 0; i < maxBound; i = i+2)
946 //if we are totally outside the image
947 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] )
952 //crop to the limit of the image
953 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
954 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
955 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
956 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
959 for (int i = 0; i < maxBound; i = i+2)
961 extent[i] = refExtent[i];
962 extent[i+1] = refExtent[i];
965 //----------------------------------------------------------------------------
968 //----------------------------------------------------------------------------
969 void vvSlicer::UpdateOrientation()
971 // Set the camera position
972 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
975 switch (this->SliceOrientation)
977 case vtkImageViewer2::SLICE_ORIENTATION_XY:
978 cam->SetFocalPoint(0,0,0);
979 cam->SetPosition(0,0,-1); // -1 if medical ?
980 cam->SetViewUp(0,-1,0);
983 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
984 cam->SetFocalPoint(0,0,0);
985 cam->SetPosition(0,-1,0); // 1 if medical ?
986 cam->SetViewUp(0,0,1);
989 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
990 cam->SetFocalPoint(0,0,0);
991 cam->SetPosition(-1,0,0); // -1 if medical ?
992 cam->SetViewUp(0,0,1);
997 //----------------------------------------------------------------------------
1000 //----------------------------------------------------------------------------
1001 void vvSlicer::SetOpacity(double s)
1003 this->GetImageActor()->SetOpacity(s);
1005 //----------------------------------------------------------------------------
1008 //----------------------------------------------------------------------------
1009 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1011 this->Superclass::SetRenderWindow(rw);
1012 this->SetupInteractor(rw->GetInteractor());
1013 ca->SetImageActor(this->GetImageActor());
1014 ca->SetWindowLevel(this->GetWindowLevel());
1015 ca->SetText(2, "<slice>");
1016 ca->SetText(3, "<window>\n<level>");
1028 crossCursor->SetModelBounds(bounds);
1029 this->GetRenderer()->AddActor(pdmA);
1030 this->GetRenderer()->AddActor(ca);
1031 this->GetRenderer()->ResetCamera();
1033 //this is just a mapping between the labeling of the orientations presented to the user and
1034 //the one used by vtk
1035 SetSliceOrientation(2-(orientation%3));
1038 //----------------------------------------------------------------------------
1041 //----------------------------------------------------------------------------
1042 void vvSlicer::ResetCamera()
1044 if (this->GetInput())
1046 double* input_bounds=this->GetInput()->GetBounds();
1047 double bmax=input_bounds[1]-input_bounds[0];
1048 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
1049 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
1050 this->GetRenderer()->ResetCamera();
1051 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
1054 //----------------------------------------------------------------------------
1057 //----------------------------------------------------------------------------
1058 void vvSlicer::SetDisplayMode(bool i)
1060 this->GetImageActor()->SetVisibility(i);
1061 this->GetAnnotation()->SetVisibility(i);
1062 this->GetRenderer()->SetDraw(i);
1064 mLandActor->SetVisibility(i);
1065 pdmA->SetVisibility(i);
1067 UpdateDisplayExtent();
1069 //----------------------------------------------------------------------------
1072 //----------------------------------------------------------------------------
1073 void vvSlicer::FlipHorizontalView()
1075 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1078 double *position = cam->GetPosition();
1079 switch (this->SliceOrientation)
1081 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1082 cam->SetPosition(position[0],position[1],-position[2]);
1085 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1086 cam->SetPosition(position[0],-position[1],position[2]);
1089 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1090 cam->SetPosition(-position[0],position[1],position[2]);
1093 this->Renderer->ResetCameraClippingRange();
1094 this->UpdateDisplayExtent();
1097 //----------------------------------------------------------------------------
1100 //----------------------------------------------------------------------------
1101 void vvSlicer::FlipVerticalView()
1103 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1106 FlipHorizontalView();
1107 double *viewup = cam->GetViewUp();
1108 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1109 this->UpdateDisplayExtent();
1112 //----------------------------------------------------------------------------
1115 //----------------------------------------------------------------------------
1116 void vvSlicer::SetColorWindow(double window)
1118 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1121 double level = this->GetWindowLevel()->GetLevel();
1122 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1125 this->vtkImageViewer2::SetColorWindow(window);
1127 //----------------------------------------------------------------------------
1130 //----------------------------------------------------------------------------
1131 void vvSlicer::SetColorLevel(double level)
1133 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1136 double window = this->GetWindowLevel()->GetWindow();
1137 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1140 this->vtkImageViewer2::SetColorLevel(level);
1142 //----------------------------------------------------------------------------
1144 //----------------------------------------------------------------------------
1145 // Returns the min an the max value in a 41x41 region around the mouse pointer
1146 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1148 //Get mouse pointer position in view coordinates
1149 double fLocalExtents[6];
1150 for(int i=0; i<3; i++)
1152 fLocalExtents[i*2 ] = mCurrent[i];
1153 fLocalExtents[i*2+1] = mCurrent[i];
1155 this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1156 this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1157 for(int i=0; i<3; i++)
1159 if (i!=SliceOrientation) //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1161 fLocalExtents[i*2 ] -= 0.2;
1162 fLocalExtents[i*2+1] += 0.2;
1165 this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1166 this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1168 //Convert to image pixel coordinates (rounded)
1169 int iLocalExtents[6];
1170 for(int i=0; i<3; i++)
1172 fLocalExtents[i*2 ] = (fLocalExtents[i*2 ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1173 fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1175 iLocalExtents[i*2 ] = lrint(fLocalExtents[i*2 ]);
1176 iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1178 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1179 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1182 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkExtractVOI::New();
1183 voiFilter->SetInput(this->GetInput());
1184 voiFilter->SetVOI(iLocalExtents);
1186 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkImageAccumulate::New();
1187 accFilter->SetInput(voiFilter->GetOutput());
1188 accFilter->Update();
1190 min = *(accFilter->GetMin());
1191 max = *(accFilter->GetMax());
1193 //----------------------------------------------------------------------------
1195 //----------------------------------------------------------------------------
1196 void vvSlicer::Render()
1198 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion)
1200 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1201 legend->SetVisibility(1);
1203 else legend->SetVisibility(0);
1205 if (ca->GetVisibility())
1207 std::string worldPos = "";
1208 std::stringstream world1;
1209 std::stringstream world2;
1210 std::stringstream world3;
1211 world1 << (int)mCurrent[0];
1212 world2 << (int)mCurrent[1];
1213 world3 << (int)mCurrent[2];
1214 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1215 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1216 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1218 if (pdmA->GetVisibility())
1220 double x = mCursor[0];
1221 double y = mCursor[1];
1222 double z = mCursor[2];
1223 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1224 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1225 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1227 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1228 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1229 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1230 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1231 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1232 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1234 vtkRenderer * renderer = this->Renderer;
1236 renderer->WorldToView(x,y,z);
1237 renderer->ViewToNormalizedViewport(x,y,z);
1238 renderer->NormalizedViewportToViewport(x,y);
1239 renderer->ViewportToNormalizedDisplay(x,y);
1240 renderer->NormalizedDisplayToDisplay(x,y);
1241 crossCursor->SetFocalPoint(x,y,z);
1244 crossCursor->SetFocalPoint(-1,-1,z);
1247 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1248 X <= this->GetInput()->GetWholeExtent()[1] &&
1249 Y >= this->GetInput()->GetWholeExtent()[2] &&
1250 Y <= this->GetInput()->GetWholeExtent()[3] &&
1251 Z >= this->GetInput()->GetWholeExtent()[4] &&
1252 Z <= this->GetInput()->GetWholeExtent()[5])
1254 std::stringstream pixel1;
1255 std::stringstream pixel2;
1256 std::stringstream pixel3;
1257 std::stringstream temps;
1261 temps << mCurrentTSlice;
1262 double value = this->GetInput()->GetScalarComponentAsDouble(
1267 std::stringstream val;
1269 worldPos += "data value : " + val.str();
1270 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " +
1271 world3.str() + " " + temps.str();
1272 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " +
1273 pixel3.str() + " " + temps.str();
1275 ca->SetText(1,worldPos.c_str());
1277 if (mOverlay && mOverlayActor->GetVisibility())
1279 mOverlayMapper->SetWindow(this->GetColorWindow());
1280 mOverlayMapper->SetLevel(this->GetColorLevel());
1281 mOverlayMapper->Update();
1285 //this->Superclass::Render();
1286 this->GetRenderWindow()->Render();
1288 //----------------------------------------------------------------------------
1291 //----------------------------------------------------------------------------
1292 void vvSlicer::UpdateCursorPosition()
1294 if (this->GetImageActor()->GetVisibility())
1296 pdmA->SetVisibility(true);
1297 mCursor[0] = mCurrent[0];
1298 mCursor[1] = mCurrent[1];
1299 mCursor[2] = mCurrent[2];
1300 mCursor[3] = mCurrentTSlice;
1303 //----------------------------------------------------------------------------
1306 //----------------------------------------------------------------------------
1307 void vvSlicer::UpdateLandmarks()
1309 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1310 if (pd->GetPoints())
1312 mLandGlyph->SetRange(0,1);
1313 mLandGlyph->Modified();
1314 mLandGlyph->Update();
1316 mClipBox->Modified();
1317 mLandClipper->Update();
1318 mLandMapper->Update();
1322 //----------------------------------------------------------------------------
1325 //----------------------------------------------------------------------------
1326 void vvSlicer::SetSlice(int slice)
1328 int *range = this->GetSliceRange();
1331 if (slice < range[0])
1335 else if (slice > range[1])
1341 if (this->Slice == slice)
1346 this->Slice = slice;
1349 this->UpdateDisplayExtent();
1352 //----------------------------------------------------------------------------
1355 //----------------------------------------------------------------------------
1356 void vvSlicer::SetContourSlice()
1358 if (mSurfaceCutActors.size() > 0)
1359 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1360 i!=mSurfaceCutActors.end();i++)
1361 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1362 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1364 //----------------------------------------------------------------------------
1367 //----------------------------------------------------------------------------
1368 void vvSlicer::ForceUpdateDisplayExtent()
1370 this->UpdateDisplayExtent();
1372 //----------------------------------------------------------------------------
1375 //----------------------------------------------------------------------------
1376 int* vvSlicer::GetDisplayExtent()
1378 return this->GetImageActor()->GetDisplayExtent();
1380 //----------------------------------------------------------------------------
1383 //----------------------------------------------------------------------------
1384 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1386 this->Superclass::PrintSelf(os, indent);
1388 //----------------------------------------------------------------------------