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>
72 #include <vtkAssignAttribute.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 += "r : reset view\n";
102 text += "l : reload image\n";
103 text += "f : fly to mouse position\n";
104 text += "g : go to cross hair position\n\n";
105 text += "Up,down : change slice\n";
106 text += "Left,right : change tenporal slice\n\n";
107 text += "Scrollbar (or w/x) : zoom in/out\n";
108 text += "left button : synchronize all views\n";
109 text += "middle button : grab image\n";
110 text += "right button : change windowing\n";
112 crossCursor = vtkCursor2D::New();
113 crossCursor->AllOff();
114 crossCursor->AxesOn();
115 crossCursor->SetTranslationMode(1);
116 crossCursor->SetRadius(2);
118 pdm = vtkPolyDataMapper2D::New();
119 pdm->SetInput(crossCursor->GetOutput());
121 pdmA = vtkActor2D::New();
122 pdmA->SetMapper(pdm);
123 pdmA->GetProperty()->SetColor(255,10,212);
124 pdmA->SetVisibility(0);
125 pdmA->SetPickable(0);
127 ca = vtkCornerAnnotation::New();
128 ca->GetTextProperty()->SetColor(255,10,212);
129 ca->SetVisibility(1);
137 legend = vtkSmartPointer<vtkScalarBarActor>::New();
138 //legend->SetTitle("test!");
139 legend->SetPosition(0.82,0.18);
140 legend->SetWidth(0.1);
141 legend->SetVisibility(0);
142 legend->SetLabelFormat("%.1f");
143 this->GetRenderer()->AddActor(legend);
145 this->WindowLevel->Delete();
146 this->WindowLevel = vvImageMapToWLColors::New();
147 this->InstallPipeline();
149 //------------------------------------------------------------------------------
152 //------------------------------------------------------------------------------
153 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
154 return mOverlayMapper.GetPointer();
156 //------------------------------------------------------------------------------
159 //------------------------------------------------------------------------------
160 vtkImageActor* vvSlicer::GetOverlayActor() {
161 return mOverlayActor.GetPointer();
163 //------------------------------------------------------------------------------
166 //------------------------------------------------------------------------------
167 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
168 return mFusionMapper.GetPointer();
170 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 vtkImageActor* vvSlicer::GetFusionActor() {
175 return mFusionActor.GetPointer();
177 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 vtkActor* vvSlicer::GetVFActor() {
182 return mVFActor.GetPointer();
184 //------------------------------------------------------------------------------
187 //------------------------------------------------------------------------------
188 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
189 return ca.GetPointer();
191 //------------------------------------------------------------------------------
194 //------------------------------------------------------------------------------
195 void vvSlicer::EnableReducedExtent(bool b) {
196 mUseReducedExtent = b;
198 //------------------------------------------------------------------------------
201 //------------------------------------------------------------------------------
202 void vvSlicer::SetReducedExtent(int * ext) {
203 mReducedExtent = ext;
205 //------------------------------------------------------------------------------
208 //------------------------------------------------------------------------------
209 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
212 mSurfaceCutActors.push_back(new vvMeshActor());
214 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
216 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
217 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
218 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 void vvSlicer::ToggleContourSuperposition()
228 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
229 i!=mSurfaceCutActors.end();i++)
230 (*i)->ToggleSuperposition();
232 //------------------------------------------------------------------------------
235 //------------------------------------------------------------------------------
236 void vvSlicer::SetCursorColor(int r,int g, int b)
238 pdmA->GetProperty()->SetColor(r,g,b);
240 //------------------------------------------------------------------------------
243 //------------------------------------------------------------------------------
244 void vvSlicer::SetCursorVisibility(bool s)
246 pdmA->SetVisibility(s);
248 //------------------------------------------------------------------------------
251 //------------------------------------------------------------------------------
252 bool vvSlicer::GetCursorVisibility()
254 return pdmA->GetVisibility();
256 //------------------------------------------------------------------------------
259 //------------------------------------------------------------------------------
260 vvSlicer::~vvSlicer()
262 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
263 i!=mSurfaceCutActors.end();i++)
266 //------------------------------------------------------------------------------
269 //------------------------------------------------------------------------------
270 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
277 //------------------------------------------------------------------------------
280 //------------------------------------------------------------------------------
281 void vvSlicer::SetImage(vvImage::Pointer image)
283 if (image->GetVTKImages().size())
286 this->Superclass::SetInput(image->GetVTKImages()[0]);
287 this->UpdateDisplayExtent();
289 ca->SetText(0,mFileName.c_str());
292 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
298 if (overlay->GetVTKImages().size())
303 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
304 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
308 mOverlayActor = vtkImageActor::New();
309 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
310 mOverlayActor->SetPickable(0);
311 mOverlayActor->SetVisibility(false);
312 mOverlayActor->SetOpacity(0.5);
313 this->UpdateDisplayExtent();
316 //stupid but necessary : the Overlay need to be rendered before fusion
319 this->GetRenderer()->RemoveActor(mFusionActor);
320 this->GetRenderer()->AddActor(mOverlayActor);
321 this->GetRenderer()->AddActor(mFusionActor);
324 this->GetRenderer()->AddActor(mOverlayActor);
327 SetTSlice(mCurrentTSlice);
330 //------------------------------------------------------------------------------
333 //------------------------------------------------------------------------------
334 void vvSlicer::SetFusion(vvImage::Pointer fusion)
336 if (fusion->GetVTKImages().size())
341 mFusionMapper = vtkImageMapToWindowLevelColors::New();
342 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
346 mFusionActor = vtkImageActor::New();
347 mFusionActor->SetInput(mFusionMapper->GetOutput());
348 mFusionActor->SetPickable(0);
349 mFusionActor->SetVisibility(false);
350 mFusionActor->SetOpacity(0.7);
351 this->UpdateDisplayExtent();
352 this->GetRenderer()->AddActor(mFusionActor);
356 SetTSlice(mCurrentTSlice);
359 //------------------------------------------------------------------------------
362 //------------------------------------------------------------------------------
363 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
365 if (actor_type == "vector")
367 this->mVFActor->SetVisibility(vis);
369 if (actor_type == "overlay")
371 this->mOverlayActor->SetVisibility(vis);
373 if (actor_type == "fusion")
375 this->mFusionActor->SetVisibility(vis);
377 if (actor_type == "contour")
378 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
379 UpdateDisplayExtent();
381 //------------------------------------------------------------------------------
384 //------------------------------------------------------------------------------
385 void vvSlicer::SetVF(vvImage::Pointer vf)
387 if (vf->GetVTKImages().size())
393 mAAFilter=vtkAssignAttribute::New();
394 mVOIFilter = vtkExtractVOI::New();
395 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
397 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
398 mAAFilter->SetInput(mVOIFilter->GetOutput());
399 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
400 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
403 mArrow = vvGlyphSource::New();
404 mArrow->SetGlyphTypeToSpecificArrow();
405 mArrow->SetScale(mScale);
408 // Glyph the gradient vector (with arrows)
410 mGlyphFilter = vvGlyph2D::New();
411 mGlyphFilter->SetInput(mAAFilter->GetOutput());
412 mGlyphFilter->SetSource(mArrow->GetOutput());
413 mGlyphFilter->ScalingOn();
414 mGlyphFilter->SetScaleModeToScaleByVector();
415 mGlyphFilter->OrientOn();
416 mGlyphFilter->SetVectorModeToUseVector();
417 mGlyphFilter->SetColorModeToColorByVector();
420 mVFMapper = vtkPolyDataMapper::New();
421 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
422 mVFMapper->SetInput(mGlyphFilter->GetOutput());
423 mVFMapper->ImmediateModeRenderingOn();
426 mVFActor = vtkActor::New();
427 mVFActor->SetMapper(mVFMapper);
428 mVFActor->SetPickable(0);
429 this->UpdateDisplayExtent();
430 this->GetRenderer()->AddActor(mVFActor);
433 SetTSlice(mCurrentTSlice);
436 //------------------------------------------------------------------------------
439 //------------------------------------------------------------------------------
440 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
442 mLandmarks = landmarks;
447 mCross = vtkCursor3D::New();
448 mCross->SetFocalPoint(0.0,0.0,0.0);
449 mCross->SetModelBounds(-10,10,-10,10,-10,10);
454 mLandGlyph = vtkGlyph3D::New();
455 mLandGlyph->SetSource(mCross->GetOutput());
456 mLandGlyph->SetInput(landmarks->GetOutput());
457 //mLandGlyph->SetIndexModeToScalar();
458 mLandGlyph->SetRange(0,1);
459 mLandGlyph->ScalingOff();
461 mLandGlyph->SetColorModeToColorByScalar();
464 mClipBox = vtkBox::New();
466 mLandClipper = vtkClipPolyData::New();
467 mLandClipper->InsideOutOn();
468 mLandClipper->SetInput(mLandGlyph->GetOutput());
469 mLandClipper->SetClipFunction(mClipBox);
472 mLandMapper = vtkPolyDataMapper::New();
473 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
474 //mLandMapper->ScalarVisibilityOff();
477 mLandActor = vtkActor::New();
478 mLandActor->SetMapper(mLandMapper);
479 mLandActor->GetProperty()->SetColor(255,10,212);
480 mLandActor->SetPickable(0);
481 mLandActor->SetVisibility(true);
482 this->UpdateDisplayExtent();
483 this->GetRenderer()->AddActor(mLandActor);
486 //------------------------------------------------------------------------------
488 //------------------------------------------------------------------------------
489 //FIXME: this function leaks memory, we should fix it someday :)
490 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
492 if (actor_type == "vector")
494 Renderer->RemoveActor(mVFActor);
503 if (actor_type == "overlay")
505 Renderer->RemoveActor(mOverlayActor);
507 mOverlayActor = NULL;
508 mOverlayMapper = NULL;
510 if (actor_type == "fusion")
512 Renderer->RemoveActor(mFusionActor);
515 mFusionMapper = NULL;
517 if (actor_type == "contour")
519 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
520 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
523 //------------------------------------------------------------------------------
526 //------------------------------------------------------------------------------
527 void vvSlicer::SetVFSubSampling(int sub)
531 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
534 UpdateDisplayExtent();
537 //------------------------------------------------------------------------------
540 //------------------------------------------------------------------------------
541 void vvSlicer::SetVFScale(int scale)
545 mArrow->SetScale(mScale);
546 UpdateDisplayExtent();
549 //------------------------------------------------------------------------------
552 //------------------------------------------------------------------------------
553 void vvSlicer::SetVFLog(int log)
558 mGlyphFilter->SetUseLog(mVFLog);
559 mGlyphFilter->Modified();
561 UpdateDisplayExtent();
564 //------------------------------------------------------------------------------
567 //------------------------------------------------------------------------------
568 void vvSlicer::SetTSlice(int t)
572 else if ((unsigned int)t >= mImage->GetVTKImages().size())
573 t = mImage->GetVTKImages().size() -1;
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);
644 //----------------------------------------------------------------------------
645 void vvSlicer::UpdateDisplayExtent()
647 vtkImageData *input = this->GetInput();
648 if (!input || !this->ImageActor)
652 input->UpdateInformation();
653 int *w_ext;// = input->GetWholeExtent();
655 if (mUseReducedExtent) {
656 w_ext = mReducedExtent;
658 else w_ext = input->GetWholeExtent();
660 switch (this->SliceOrientation)
662 case vtkImageViewer2::SLICE_ORIENTATION_XY:
663 this->ImageActor->SetDisplayExtent(
664 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
665 if (mVF && mVFActor->GetVisibility())
668 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
669 mVOIFilter->SetVOI(vfExtent);
670 mGlyphFilter->SetOrientation(1,1,0);
672 // put the vector field between the image and the camera
673 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
674 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
676 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
678 if (mOverlay && mOverlayActor->GetVisibility())
681 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
682 mOverlayActor->SetDisplayExtent(overExtent);
683 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
684 mOverlayActor->SetPosition(0,0,1);
686 mOverlayActor->SetPosition(0,0,-1);
688 if (mFusion && mFusionActor->GetVisibility())
691 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
692 mFusionActor->SetDisplayExtent(fusExtent);
693 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
694 mFusionActor->SetPosition(0,0,1.5);
696 mFusionActor->SetPosition(0,0,-1.5);
703 bounds[0] = ImageActor->GetBounds()[0];
704 bounds[1] = ImageActor->GetBounds()[1];
705 bounds[2] = ImageActor->GetBounds()[2];
706 bounds[3] = ImageActor->GetBounds()[3];
707 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
708 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
709 mClipBox->SetBounds(bounds);
712 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
713 mLandActor->SetPosition(0,0,1.5);
715 mLandActor->SetPosition(0,0,-1.5);
719 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
720 this->ImageActor->SetDisplayExtent(
721 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
722 if (mVF && mVFActor->GetVisibility())
725 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
726 mVOIFilter->SetVOI(vfExtent);
727 mGlyphFilter->SetOrientation(1,0,1);
729 // put the vector field between the image aSpacingnd the camera
730 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
731 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
733 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
735 if (mOverlay && mOverlayActor->GetVisibility())
738 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
739 mOverlayActor->SetDisplayExtent(overExtent);
740 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
741 mOverlayActor->SetPosition(0,1,0);
743 mOverlayActor->SetPosition(0,-1,0);
745 if (mFusion && mFusionActor->GetVisibility())
748 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
749 mFusionActor->SetDisplayExtent(fusExtent);
750 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
751 mFusionActor->SetPosition(0,1.5,0);
753 mFusionActor->SetPosition(0,-1.5,0);
760 bounds[0] = ImageActor->GetBounds()[0];
761 bounds[1] = ImageActor->GetBounds()[1];
762 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
763 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
764 bounds[4] = ImageActor->GetBounds()[4];
765 bounds[5] = ImageActor->GetBounds()[5];
766 mClipBox->SetBounds(bounds);
769 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
770 mLandActor->SetPosition(0,1.5,0);
772 mLandActor->SetPosition(0,-1.5,0);
776 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
777 this->ImageActor->SetDisplayExtent(
778 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
779 if (mVF && mVFActor->GetVisibility())
782 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
783 mVOIFilter->SetVOI(vfExtent);
784 mGlyphFilter->SetOrientation(0,1,1);
786 // put the vector field between the image and the camera
787 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
788 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
790 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
792 if (mOverlay && mOverlayActor->GetVisibility())
795 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
796 mOverlayActor->SetDisplayExtent(overExtent);
797 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
798 mOverlayActor->SetPosition(1,0,0);
800 mOverlayActor->SetPosition(-1,0,0);
802 if (mFusion && mFusionActor->GetVisibility())
805 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
806 mFusionActor->SetDisplayExtent(fusExtent);
807 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
808 mFusionActor->SetPosition(1.5,0,0);
810 mFusionActor->SetPosition(-1.5,0,0);
817 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
818 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
819 bounds[2] = ImageActor->GetBounds()[2];
820 bounds[3] = ImageActor->GetBounds()[3];
821 bounds[4] = ImageActor->GetBounds()[4];
822 bounds[5] = ImageActor->GetBounds()[5];
823 mClipBox->SetBounds(bounds);
826 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
827 mLandActor->SetPosition(1.5,0,0);
829 mLandActor->SetPosition(-1.5,0,0);
834 // Figure out the correct clipping range
838 if (this->InteractorStyle &&
839 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
841 this->Renderer->ResetCameraClippingRange();
845 vtkCamera *cam = this->Renderer->GetActiveCamera();
849 this->ImageActor->GetBounds(bounds);
850 double spos = (double)bounds[this->SliceOrientation * 2];
851 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
852 double range = fabs(spos - cpos);
853 double *spacing = input->GetSpacing();
855 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
856 cam->SetClippingRange(
857 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
862 //----------------------------------------------------------------------------
865 //----------------------------------------------------------------------------
866 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
868 vtkImageData* image=this->GetInput();
869 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
870 mVF->GetSpacing()[0];
871 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
872 mVF->GetSpacing()[0];
873 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
874 mVF->GetSpacing()[1];
875 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
876 mVF->GetSpacing()[1];
877 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
878 mVF->GetSpacing()[2];
879 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
880 mVF->GetSpacing()[2];
882 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
884 //----------------------------------------------------------------------------
887 //----------------------------------------------------------------------------
888 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
890 vtkImageData* image=this->GetInput();
891 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
892 mOverlay->GetSpacing()[0];
893 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
894 mOverlay->GetSpacing()[0];
895 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
896 mOverlay->GetSpacing()[1];
897 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
898 mOverlay->GetSpacing()[1];
899 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
900 mOverlay->GetSpacing()[2];
901 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
902 mOverlay->GetSpacing()[2];
903 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
905 //----------------------------------------------------------------------------
908 //----------------------------------------------------------------------------
909 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
911 vtkImageData* image=this->GetInput();
912 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
913 mFusion->GetSpacing()[0];
914 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
915 mFusion->GetSpacing()[0];
916 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
917 mFusion->GetSpacing()[1];
918 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
919 mFusion->GetSpacing()[1];
920 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
921 mFusion->GetSpacing()[2];
922 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
923 mFusion->GetSpacing()[2];
924 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
926 //----------------------------------------------------------------------------
929 //----------------------------------------------------------------------------
930 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
935 //2D overlay on 3D image specific case
936 if (refExtent[4] == refExtent[5])
939 extent[4] = refExtent[4];
940 extent[5] = refExtent[5];
943 for (int i = 0; i < maxBound; i = i+2)
945 //if we are totally outside the image
946 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] )
951 //crop to the limit of the image
952 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
953 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
954 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
955 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
958 for (int i = 0; i < maxBound; i = i+2)
960 extent[i] = refExtent[i];
961 extent[i+1] = refExtent[i];
964 //----------------------------------------------------------------------------
967 //----------------------------------------------------------------------------
968 void vvSlicer::UpdateOrientation()
970 // Set the camera position
971 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
974 switch (this->SliceOrientation)
976 case vtkImageViewer2::SLICE_ORIENTATION_XY:
977 cam->SetFocalPoint(0,0,0);
978 cam->SetPosition(0,0,-1); // -1 if medical ?
979 cam->SetViewUp(0,-1,0);
982 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
983 cam->SetFocalPoint(0,0,0);
984 cam->SetPosition(0,-1,0); // 1 if medical ?
985 cam->SetViewUp(0,0,1);
988 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
989 cam->SetFocalPoint(0,0,0);
990 cam->SetPosition(-1,0,0); // -1 if medical ?
991 cam->SetViewUp(0,0,1);
996 //----------------------------------------------------------------------------
999 //----------------------------------------------------------------------------
1000 void vvSlicer::SetOpacity(double s)
1002 this->GetImageActor()->SetOpacity(s);
1004 //----------------------------------------------------------------------------
1007 //----------------------------------------------------------------------------
1008 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1010 this->Superclass::SetRenderWindow(rw);
1011 this->SetupInteractor(rw->GetInteractor());
1012 ca->SetImageActor(this->GetImageActor());
1013 ca->SetWindowLevel(this->GetWindowLevel());
1014 ca->SetText(2, "<slice>");
1015 ca->SetText(3, "<window>\n<level>");
1027 crossCursor->SetModelBounds(bounds);
1028 this->GetRenderer()->AddActor(pdmA);
1029 this->GetRenderer()->AddActor(ca);
1030 this->GetRenderer()->ResetCamera();
1032 //this is just a mapping between the labeling of the orientations presented to the user and
1033 //the one used by vtk
1034 SetSliceOrientation(2-(orientation%3));
1037 //----------------------------------------------------------------------------
1040 //----------------------------------------------------------------------------
1041 void vvSlicer::ResetCamera()
1043 if (this->GetInput())
1045 double* input_bounds=this->GetInput()->GetBounds();
1046 double bmax=input_bounds[1]-input_bounds[0];
1047 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
1048 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
1049 this->GetRenderer()->ResetCamera();
1050 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
1053 //----------------------------------------------------------------------------
1056 //----------------------------------------------------------------------------
1057 void vvSlicer::SetDisplayMode(bool i)
1059 this->GetImageActor()->SetVisibility(i);
1060 this->GetAnnotation()->SetVisibility(i);
1061 this->GetRenderer()->SetDraw(i);
1063 mLandActor->SetVisibility(i);
1064 pdmA->SetVisibility(i);
1066 UpdateDisplayExtent();
1068 //----------------------------------------------------------------------------
1071 //----------------------------------------------------------------------------
1072 void vvSlicer::FlipHorizontalView()
1074 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1077 double *position = cam->GetPosition();
1078 switch (this->SliceOrientation)
1080 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1081 cam->SetPosition(position[0],position[1],-position[2]);
1084 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1085 cam->SetPosition(position[0],-position[1],position[2]);
1088 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1089 cam->SetPosition(-position[0],position[1],position[2]);
1092 this->Renderer->ResetCameraClippingRange();
1093 this->UpdateDisplayExtent();
1096 //----------------------------------------------------------------------------
1099 //----------------------------------------------------------------------------
1100 void vvSlicer::FlipVerticalView()
1102 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1105 FlipHorizontalView();
1106 double *viewup = cam->GetViewUp();
1107 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1108 this->UpdateDisplayExtent();
1111 //----------------------------------------------------------------------------
1114 //----------------------------------------------------------------------------
1115 void vvSlicer::SetColorWindow(double window)
1117 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1120 double level = this->GetWindowLevel()->GetLevel();
1121 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1124 this->vtkImageViewer2::SetColorWindow(window);
1126 //----------------------------------------------------------------------------
1129 //----------------------------------------------------------------------------
1130 void vvSlicer::SetColorLevel(double level)
1132 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1135 double window = this->GetWindowLevel()->GetWindow();
1136 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1139 this->vtkImageViewer2::SetColorLevel(level);
1141 //----------------------------------------------------------------------------
1144 //----------------------------------------------------------------------------
1145 void vvSlicer::Render()
1147 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion)
1149 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1150 legend->SetVisibility(1);
1152 else legend->SetVisibility(0);
1154 if (ca->GetVisibility())
1156 std::string worldPos = "";
1157 std::stringstream world1;
1158 std::stringstream world2;
1159 std::stringstream world3;
1160 world1 << (int)mCurrent[0];
1161 world2 << (int)mCurrent[1];
1162 world3 << (int)mCurrent[2];
1163 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1164 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1165 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1167 if (pdmA->GetVisibility())
1169 double x = mCursor[0];
1170 double y = mCursor[1];
1171 double z = mCursor[2];
1172 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1173 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1174 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1176 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1177 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1178 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1179 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1180 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1181 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1183 vtkRenderer * renderer = this->Renderer;
1185 renderer->WorldToView(x,y,z);
1186 renderer->ViewToNormalizedViewport(x,y,z);
1187 renderer->NormalizedViewportToViewport(x,y);
1188 renderer->ViewportToNormalizedDisplay(x,y);
1189 renderer->NormalizedDisplayToDisplay(x,y);
1190 crossCursor->SetFocalPoint(x,y,z);
1193 crossCursor->SetFocalPoint(-1,-1,z);
1196 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1197 X <= this->GetInput()->GetWholeExtent()[1] &&
1198 Y >= this->GetInput()->GetWholeExtent()[2] &&
1199 Y <= this->GetInput()->GetWholeExtent()[3] &&
1200 Z >= this->GetInput()->GetWholeExtent()[4] &&
1201 Z <= this->GetInput()->GetWholeExtent()[5])
1203 std::stringstream pixel1;
1204 std::stringstream pixel2;
1205 std::stringstream pixel3;
1206 std::stringstream temps;
1210 temps << mCurrentTSlice;
1211 double value = this->GetInput()->GetScalarComponentAsDouble(
1216 std::stringstream val;
1218 worldPos += "data value : " + val.str();
1219 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " +
1220 world3.str() + " " + temps.str();
1221 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " +
1222 pixel3.str() + " " + temps.str();
1224 ca->SetText(1,worldPos.c_str());
1226 if (mOverlay && mOverlayActor->GetVisibility())
1228 mOverlayMapper->SetWindow(this->GetColorWindow());
1229 mOverlayMapper->SetLevel(this->GetColorLevel());
1230 mOverlayMapper->Update();
1234 //this->Superclass::Render();
1235 this->GetRenderWindow()->Render();
1237 //----------------------------------------------------------------------------
1240 //----------------------------------------------------------------------------
1241 void vvSlicer::UpdateCursorPosition()
1243 if (this->GetImageActor()->GetVisibility())
1245 pdmA->SetVisibility(true);
1246 mCursor[0] = mCurrent[0];
1247 mCursor[1] = mCurrent[1];
1248 mCursor[2] = mCurrent[2];
1249 mCursor[3] = mCurrentTSlice;
1252 //----------------------------------------------------------------------------
1255 //----------------------------------------------------------------------------
1256 void vvSlicer::UpdateLandmarks()
1258 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1259 if (pd->GetPoints())
1261 mLandGlyph->SetRange(0,1);
1262 mLandGlyph->Modified();
1263 mLandGlyph->Update();
1265 mClipBox->Modified();
1266 mLandClipper->Update();
1267 mLandMapper->Update();
1271 //----------------------------------------------------------------------------
1274 //----------------------------------------------------------------------------
1275 void vvSlicer::SetSlice(int slice)
1277 int *range = this->GetSliceRange();
1280 if (slice < range[0])
1284 else if (slice > range[1])
1290 if (this->Slice == slice)
1295 this->Slice = slice;
1298 this->UpdateDisplayExtent();
1301 //----------------------------------------------------------------------------
1304 //----------------------------------------------------------------------------
1305 void vvSlicer::SetContourSlice()
1307 if (mSurfaceCutActors.size() > 0)
1308 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1309 i!=mSurfaceCutActors.end();i++)
1310 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1311 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1313 //----------------------------------------------------------------------------
1316 //----------------------------------------------------------------------------
1317 void vvSlicer::ForceUpdateDisplayExtent()
1319 this->UpdateDisplayExtent();
1321 //----------------------------------------------------------------------------
1324 //----------------------------------------------------------------------------
1325 int* vvSlicer::GetDisplayExtent()
1327 return this->GetImageActor()->GetDisplayExtent();
1329 //----------------------------------------------------------------------------
1332 //----------------------------------------------------------------------------
1333 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1335 this->Superclass::PrintSelf(os, indent);
1337 //----------------------------------------------------------------------------