1 /*=========================================================================
4 Module: $RCSfile: vvSlicer.cxx,v $
6 Date: $Date: 2010/01/06 13:31:58 $
7 Version: $Revision: 1.1 $
8 Author : Pierre Seroul (pierre.seroul@gmail.com)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
31 #include "vvSlicerManagerCommand.h"
32 #include "vvGlyphSource.h"
33 #include "vvGlyph2D.h"
34 #include "vvImageMapToWLColors.h"
36 #include <vtkTextProperty.h>
37 #include <vtkTextActor.h>
38 #include <vtkTextSource.h>
39 #include <vtkActor2D.h>
40 #include <vtkCursor2D.h>
41 #include <vtkPolyDataMapper2D.h>
42 #include <vtkProperty2D.h>
43 #include <vtkCornerAnnotation.h>
44 #include <vtkImageMapToWindowLevelColors.h>
45 #include <vtkImageData.h>
46 #include <vtkImageActor.h>
47 #include <vtkToolkits.h>
48 #include <vtkObjectFactory.h>
49 #include <vtkPointData.h>
50 #include <vtkDataArray.h>
51 #include <vtkFloatArray.h>
52 #include <vtkClipPolyData.h>
53 #include <vtkGlyph3D.h>
55 #include <vtkCursor3D.h>
56 #include <vtkProperty.h>
58 #include <vtkLightCollection.h>
59 #include <vtkScalarBarActor.h>
60 #include <vtkLookupTable.h>
62 #include <vtkRenderer.h>
63 #include <vtkRendererCollection.h>
64 #include <vtkRenderWindow.h>
65 #include <vtkRenderWindowInteractor.h>
66 #include <vtkCamera.h>
67 #include <vtkCallbackCommand.h>
68 #include <vtkCommand.h>
69 #include <vtkPolyDataMapper.h>
72 #include <vtkExtractVOI.h>
73 #include <vtkSphereSource.h>
74 #include <vtkCutter.h>
76 #include <vtkAssignAttribute.h>
78 vtkCxxRevisionMacro(vvSlicer, "$Revision: 1.1 $");
79 vtkStandardNewMacro(vvSlicer);
86 mCurrent[0] = -VTK_DOUBLE_MAX;
87 mCurrent[1] = -VTK_DOUBLE_MAX;
88 mCurrent[2] = -VTK_DOUBLE_MAX;
90 mCursor[0] = -VTK_DOUBLE_MAX;
91 mCursor[1] = -VTK_DOUBLE_MAX;
92 mCursor[2] = -VTK_DOUBLE_MAX;
93 mCursor[3] = -VTK_DOUBLE_MAX;
99 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
100 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
101 text += "0,1,2,3,4,5 : preset windowing\n";
102 text += "6,7,8,9 : preset colormap\n";
103 text += "r : reset view\n";
104 text += "l : reload image\n";
105 text += "f : fly to mouse position\n";
106 text += "g : go to cross hair position\n\n";
107 text += "Up,down : change slice\n";
108 text += "Left,right : change tenporal slice\n\n";
109 text += "Scrollbar (or w/x) : zoom in/out\n";
110 text += "left button : synchronize all views\n";
111 text += "middle button : grab image\n";
112 text += "right button : change windowing\n";
114 crossCursor = vtkCursor2D::New();
115 crossCursor->AllOff();
116 crossCursor->AxesOn();
117 crossCursor->SetTranslationMode(1);
118 crossCursor->SetRadius(2);
120 pdm = vtkPolyDataMapper2D::New();
121 pdm->SetInput(crossCursor->GetOutput());
123 pdmA = vtkActor2D::New();
124 pdmA->SetMapper(pdm);
125 pdmA->GetProperty()->SetColor(255,10,212);
126 pdmA->SetVisibility(0);
127 pdmA->SetPickable(0);
129 ca = vtkCornerAnnotation::New();
130 ca->GetTextProperty()->SetColor(255,10,212);
131 ca->SetVisibility(1);
139 legend = vtkSmartPointer<vtkScalarBarActor>::New();
140 //legend->SetTitle("test!");
141 legend->SetPosition(0.82,0.18);
142 legend->SetWidth(0.1);
143 legend->SetVisibility(0);
144 legend->SetLabelFormat("%.1f");
145 this->GetRenderer()->AddActor(legend);
147 this->WindowLevel->Delete();
148 this->WindowLevel = vvImageMapToWLColors::New();
149 this->InstallPipeline();
152 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
153 return mOverlayMapper.GetPointer();
155 vtkImageActor* vvSlicer::GetOverlayActor() {
156 return mOverlayActor.GetPointer();
158 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
159 return mFusionMapper.GetPointer();
162 vtkImageActor* vvSlicer::GetFusionActor() {
163 return mFusionActor.GetPointer();
165 vtkActor* vvSlicer::GetVFActor() {
166 return mVFActor.GetPointer();
168 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
169 return ca.GetPointer();
172 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
175 mSurfaceCutActors.push_back(new vvMeshActor());
177 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
179 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
180 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
181 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
186 void vvSlicer::ToggleContourSuperposition()
188 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
189 i!=mSurfaceCutActors.end();i++)
190 (*i)->ToggleSuperposition();
193 void vvSlicer::SetCursorColor(int r,int g, int b)
195 pdmA->GetProperty()->SetColor(r,g,b);
197 void vvSlicer::SetCursorVisibility(bool s)
199 pdmA->SetVisibility(s);
201 bool vvSlicer::GetCursorVisibility()
203 return pdmA->GetVisibility();
206 vvSlicer::~vvSlicer()
208 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
209 i!=mSurfaceCutActors.end();i++)
213 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
221 void vvSlicer::SetImage(vvImage::Pointer image)
223 if (image->GetVTKImages().size())
226 this->Superclass::SetInput(image->GetVTKImages()[0]);
227 this->UpdateDisplayExtent();
229 ca->SetText(0,mFileName.c_str());
233 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
235 if (overlay->GetVTKImages().size())
240 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
241 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
244 mOverlayActor = vtkImageActor::New();
245 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
246 mOverlayActor->SetPickable(0);
247 mOverlayActor->SetVisibility(false);
248 mOverlayActor->SetOpacity(0.5);
249 this->UpdateDisplayExtent();
251 //stupid but necessary : the Overlay need to be render before fusion
254 this->GetRenderer()->RemoveActor(mFusionActor);
255 this->GetRenderer()->AddActor(mOverlayActor);
256 this->GetRenderer()->AddActor(mFusionActor);
259 this->GetRenderer()->AddActor(mOverlayActor);
262 SetTSlice(mCurrentTSlice);
266 void vvSlicer::SetFusion(vvImage::Pointer fusion)
268 if (fusion->GetVTKImages().size())
273 mFusionMapper = vtkImageMapToWindowLevelColors::New();
274 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
277 mFusionActor = vtkImageActor::New();
278 mFusionActor->SetInput(mFusionMapper->GetOutput());
279 mFusionActor->SetPickable(0);
280 mFusionActor->SetVisibility(false);
281 mFusionActor->SetOpacity(0.7);
282 this->UpdateDisplayExtent();
283 this->GetRenderer()->AddActor(mFusionActor);
286 SetTSlice(mCurrentTSlice);
290 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
292 if (actor_type == "vector")
294 this->mVFActor->SetVisibility(vis);
296 if (actor_type == "overlay")
298 this->mOverlayActor->SetVisibility(vis);
300 if (actor_type == "fusion")
302 this->mFusionActor->SetVisibility(vis);
304 if (actor_type == "contour")
305 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
306 UpdateDisplayExtent();
309 void vvSlicer::SetVF(vvImage::Pointer vf)
311 if (vf->GetVTKImages().size())
317 mAAFilter=vtkAssignAttribute::New();
318 mVOIFilter = vtkExtractVOI::New();
319 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
321 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
322 mAAFilter->SetInput(mVOIFilter->GetOutput());
323 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
324 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
327 mArrow = vvGlyphSource::New();
328 mArrow->SetGlyphTypeToSpecificArrow();
329 mArrow->SetScale(mScale);
332 // Glyph the gradient vector (with arrows)
334 mGlyphFilter = vvGlyph2D::New();
335 mGlyphFilter->SetInput(mAAFilter->GetOutput());
336 mGlyphFilter->SetSource(mArrow->GetOutput());
337 mGlyphFilter->ScalingOn();
338 mGlyphFilter->SetScaleModeToScaleByVector();
339 mGlyphFilter->OrientOn();
340 mGlyphFilter->SetVectorModeToUseVector();
341 mGlyphFilter->SetColorModeToColorByVector();
344 mVFMapper = vtkPolyDataMapper::New();
345 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
346 mVFMapper->SetInput(mGlyphFilter->GetOutput());
347 mVFMapper->ImmediateModeRenderingOn();
350 mVFActor = vtkActor::New();
351 mVFActor->SetMapper(mVFMapper);
352 mVFActor->SetPickable(0);
353 this->UpdateDisplayExtent();
354 this->GetRenderer()->AddActor(mVFActor);
357 SetTSlice(mCurrentTSlice);
361 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
363 mLandmarks = landmarks;
368 mCross = vtkCursor3D::New();
369 mCross->SetFocalPoint(0.0,0.0,0.0);
370 mCross->SetModelBounds(-10,10,-10,10,-10,10);
375 mLandGlyph = vtkGlyph3D::New();
376 mLandGlyph->SetSource(mCross->GetOutput());
377 mLandGlyph->SetInput(landmarks->GetOutput());
378 //mLandGlyph->SetIndexModeToScalar();
379 mLandGlyph->SetRange(0,1);
380 mLandGlyph->ScalingOff();
382 mLandGlyph->SetColorModeToColorByScalar();
385 mClipBox = vtkBox::New();
387 mLandClipper = vtkClipPolyData::New();
388 mLandClipper->InsideOutOn();
389 mLandClipper->SetInput(mLandGlyph->GetOutput());
390 mLandClipper->SetClipFunction(mClipBox);
393 mLandMapper = vtkPolyDataMapper::New();
394 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
395 //mLandMapper->ScalarVisibilityOff();
398 mLandActor = vtkActor::New();
399 mLandActor->SetMapper(mLandMapper);
400 mLandActor->GetProperty()->SetColor(255,10,212);
401 mLandActor->SetPickable(0);
402 mLandActor->SetVisibility(true);
403 this->UpdateDisplayExtent();
404 this->GetRenderer()->AddActor(mLandActor);
408 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
410 if (actor_type == "vector")
420 if (actor_type == "overlay")
422 Renderer->RemoveActor(mOverlayActor);
424 mOverlayActor = NULL;
425 mOverlayMapper = NULL;
427 if (actor_type == "fusion")
429 Renderer->RemoveActor(mFusionActor);
432 mFusionMapper = NULL;
434 if (actor_type == "contour")
436 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
437 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
441 void vvSlicer::SetVFSubSampling(int sub)
445 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
448 UpdateDisplayExtent();
452 void vvSlicer::SetVFScale(int scale)
456 mArrow->SetScale(mScale);
457 UpdateDisplayExtent();
461 void vvSlicer::SetVFLog(int log)
466 mGlyphFilter->SetUseLog(mVFLog);
467 mGlyphFilter->Modified();
469 UpdateDisplayExtent();
473 void vvSlicer::SetTSlice(int t)
477 else if ((unsigned int)t >= mImage->GetVTKImages().size())
478 t = mImage->GetVTKImages().size() -1;
480 this->SetInput(mImage->GetVTKImages()[t]);
481 if (mVF && mVFActor->GetVisibility())
483 if (mVF->GetVTKImages().size() == 1)
485 mVOIFilter->SetInput(mVF->GetVTKImages()[0]);
487 else if (mVF->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
489 Renderer->RemoveActor(mVFActor);
493 if (!Renderer->HasViewProp(mVFActor))
495 Renderer->AddActor(mVFActor);
497 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
500 if (mOverlay && mOverlayActor->GetVisibility())
502 if (mOverlay->GetVTKImages().size() == 1)
504 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[0]);
506 else if (mOverlay->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
508 Renderer->RemoveActor(mOverlayActor);
512 if (!Renderer->HasViewProp(mOverlayActor))
514 Renderer->AddActor(mOverlayActor);
516 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
519 if (mFusion && mFusionActor->GetVisibility())
521 if (mFusion->GetVTKImages().size() == 1)
523 mFusionMapper->SetInput(mFusion->GetVTKImages()[0]);
525 else if (mFusion->GetVTKImages().size() <= (unsigned int)mCurrentTSlice)
527 Renderer->RemoveActor(mFusionActor);
531 if (!Renderer->HasViewProp(mFusionActor))
533 Renderer->AddActor(mFusionActor);
535 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
538 if (mSurfaceCutActors.size() > 0)
539 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
540 i!=mSurfaceCutActors.end();i++)
541 (*i)->SetTimeSlice(mCurrentTSlice);
542 UpdateDisplayExtent();
545 int vvSlicer::GetTSlice()
547 return mCurrentTSlice;
550 void vvSlicer::SetSliceOrientation(int orientation)
552 //if 2D image, force to watch in Axial View
554 this->GetInput()->GetWholeExtent(extent);
555 if (extent[5]-extent[4] <= 2)
558 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
559 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
561 vtkErrorMacro("Error - invalid slice orientation " << orientation);
565 this->SliceOrientation = orientation;
568 int *range = this->GetSliceRange();
570 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
572 this->UpdateOrientation();
573 this->UpdateDisplayExtent();
575 if (this->Renderer && this->GetInput())
577 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
578 this->Renderer->ResetCamera();
579 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
585 //----------------------------------------------------------------------------
586 void vvSlicer::UpdateDisplayExtent()
588 vtkImageData *input = this->GetInput();
589 if (!input || !this->ImageActor)
593 input->UpdateInformation();
594 int *w_ext = input->GetWholeExtent();
596 switch (this->SliceOrientation)
598 case vtkImageViewer2::SLICE_ORIENTATION_XY:
599 this->ImageActor->SetDisplayExtent(
600 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
601 if (mVF && mVFActor->GetVisibility())
604 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
605 mVOIFilter->SetVOI(vfExtent);
606 mGlyphFilter->SetOrientation(1,1,0);
608 // put the vector field between the image and the camera
609 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
610 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
612 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
614 if (mOverlay && mOverlayActor->GetVisibility())
617 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
618 mOverlayActor->SetDisplayExtent(overExtent);
619 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
620 mOverlayActor->SetPosition(0,0,1);
622 mOverlayActor->SetPosition(0,0,-1);
624 if (mFusion && mFusionActor->GetVisibility())
627 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
628 mFusionActor->SetDisplayExtent(fusExtent);
629 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
630 mFusionActor->SetPosition(0,0,1.5);
632 mFusionActor->SetPosition(0,0,-1.5);
639 bounds[0] = ImageActor->GetBounds()[0];
640 bounds[1] = ImageActor->GetBounds()[1];
641 bounds[2] = ImageActor->GetBounds()[2];
642 bounds[3] = ImageActor->GetBounds()[3];
643 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
644 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
645 mClipBox->SetBounds(bounds);
648 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
649 mLandActor->SetPosition(0,0,1.5);
651 mLandActor->SetPosition(0,0,-1.5);
655 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
656 this->ImageActor->SetDisplayExtent(
657 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
658 if (mVF && mVFActor->GetVisibility())
661 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
662 mVOIFilter->SetVOI(vfExtent);
663 mGlyphFilter->SetOrientation(1,0,1);
665 // put the vector field between the image aSpacingnd the camera
666 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
667 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
669 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
671 if (mOverlay && mOverlayActor->GetVisibility())
674 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
675 mOverlayActor->SetDisplayExtent(overExtent);
676 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
677 mOverlayActor->SetPosition(0,1,0);
679 mOverlayActor->SetPosition(0,-1,0);
681 if (mFusion && mFusionActor->GetVisibility())
684 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
685 mFusionActor->SetDisplayExtent(fusExtent);
686 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
687 mFusionActor->SetPosition(0,1.5,0);
689 mFusionActor->SetPosition(0,-1.5,0);
696 bounds[0] = ImageActor->GetBounds()[0];
697 bounds[1] = ImageActor->GetBounds()[1];
698 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
699 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
700 bounds[4] = ImageActor->GetBounds()[4];
701 bounds[5] = ImageActor->GetBounds()[5];
702 mClipBox->SetBounds(bounds);
705 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
706 mLandActor->SetPosition(0,1.5,0);
708 mLandActor->SetPosition(0,-1.5,0);
712 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
713 this->ImageActor->SetDisplayExtent(
714 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
715 if (mVF && mVFActor->GetVisibility())
718 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
719 mVOIFilter->SetVOI(vfExtent);
720 mGlyphFilter->SetOrientation(0,1,1);
722 // put the vector field between the image and the camera
723 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
724 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
726 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
728 if (mOverlay && mOverlayActor->GetVisibility())
731 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
732 mOverlayActor->SetDisplayExtent(overExtent);
733 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
734 mOverlayActor->SetPosition(1,0,0);
736 mOverlayActor->SetPosition(-1,0,0);
738 if (mFusion && mFusionActor->GetVisibility())
741 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
742 mFusionActor->SetDisplayExtent(fusExtent);
743 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
744 mFusionActor->SetPosition(1.5,0,0);
746 mFusionActor->SetPosition(-1.5,0,0);
753 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
754 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
755 bounds[2] = ImageActor->GetBounds()[2];
756 bounds[3] = ImageActor->GetBounds()[3];
757 bounds[4] = ImageActor->GetBounds()[4];
758 bounds[5] = ImageActor->GetBounds()[5];
759 mClipBox->SetBounds(bounds);
762 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
763 mLandActor->SetPosition(1.5,0,0);
765 mLandActor->SetPosition(-1.5,0,0);
770 // Figure out the correct clipping range
774 if (this->InteractorStyle &&
775 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
777 this->Renderer->ResetCameraClippingRange();
781 vtkCamera *cam = this->Renderer->GetActiveCamera();
785 this->ImageActor->GetBounds(bounds);
786 double spos = (double)bounds[this->SliceOrientation * 2];
787 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
788 double range = fabs(spos - cpos);
789 double *spacing = input->GetSpacing();
791 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
792 cam->SetClippingRange(
793 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
799 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
801 vtkImageData* image=this->GetInput();
802 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
803 mVF->GetSpacing()[0];
804 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
805 mVF->GetSpacing()[0];
806 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
807 mVF->GetSpacing()[1];
808 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
809 mVF->GetSpacing()[1];
810 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
811 mVF->GetSpacing()[2];
812 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
813 mVF->GetSpacing()[2];
815 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
818 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
820 vtkImageData* image=this->GetInput();
821 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
822 mOverlay->GetSpacing()[0];
823 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
824 mOverlay->GetSpacing()[0];
825 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
826 mOverlay->GetSpacing()[1];
827 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
828 mOverlay->GetSpacing()[1];
829 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
830 mOverlay->GetSpacing()[2];
831 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
832 mOverlay->GetSpacing()[2];
833 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
836 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
838 vtkImageData* image=this->GetInput();
839 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
840 mFusion->GetSpacing()[0];
841 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
842 mFusion->GetSpacing()[0];
843 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
844 mFusion->GetSpacing()[1];
845 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
846 mFusion->GetSpacing()[1];
847 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
848 mFusion->GetSpacing()[2];
849 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
850 mFusion->GetSpacing()[2];
851 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
854 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
859 //2D overlay on 3D image specific case
860 if (refExtent[4] == refExtent[5])
863 extent[4] = refExtent[4];
864 extent[5] = refExtent[5];
867 for (int i = 0; i < maxBound; i = i+2)
869 //if we are totally outside the image
870 if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
875 //crop to the limit of the image
876 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
877 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
878 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
879 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
882 for (int i = 0; i < maxBound; i = i+2)
884 extent[i] = refExtent[i];
885 extent[i+1] = refExtent[i];
889 void vvSlicer::UpdateOrientation()
891 // Set the camera position
892 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
895 switch (this->SliceOrientation)
897 case vtkImageViewer2::SLICE_ORIENTATION_XY:
898 cam->SetFocalPoint(0,0,0);
899 cam->SetPosition(0,0,-1); // -1 if medical ?
900 cam->SetViewUp(0,-1,0);
903 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
904 cam->SetFocalPoint(0,0,0);
905 cam->SetPosition(0,-1,0); // 1 if medical ?
906 cam->SetViewUp(0,0,1);
909 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
910 cam->SetFocalPoint(0,0,0);
911 cam->SetPosition(-1,0,0); // -1 if medical ?
912 cam->SetViewUp(0,0,1);
918 void vvSlicer::SetOpacity(double s)
920 this->GetImageActor()->SetOpacity(s);
923 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
925 this->Superclass::SetRenderWindow(rw);
926 this->SetupInteractor(rw->GetInteractor());
927 ca->SetImageActor(this->GetImageActor());
928 ca->SetWindowLevel(this->GetWindowLevel());
929 ca->SetText(2, "<slice>");
930 ca->SetText(3, "<window>\n<level>");
942 crossCursor->SetModelBounds(bounds);
943 this->GetRenderer()->AddActor(pdmA);
944 this->GetRenderer()->AddActor(ca);
945 this->GetRenderer()->ResetCamera();
947 //this is just a mapping between the labeling of the orientations presented to the user and
948 //the one used by vtk
949 SetSliceOrientation(2-(orientation%3));
953 void vvSlicer::ResetCamera()
955 if (this->GetInput())
957 double* input_bounds=this->GetInput()->GetBounds();
958 double bmax=input_bounds[1]-input_bounds[0];
959 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
960 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
961 this->GetRenderer()->ResetCamera();
962 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
966 void vvSlicer::SetDisplayMode(bool i)
968 this->GetImageActor()->SetVisibility(i);
969 this->GetAnnotation()->SetVisibility(i);
970 this->GetRenderer()->SetDraw(i);
972 mLandActor->SetVisibility(i);
973 pdmA->SetVisibility(i);
975 UpdateDisplayExtent();
978 void vvSlicer::FlipHorizontalView()
980 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
983 double *position = cam->GetPosition();
984 switch (this->SliceOrientation)
986 case vtkImageViewer2::SLICE_ORIENTATION_XY:
987 cam->SetPosition(position[0],position[1],-position[2]);
990 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
991 cam->SetPosition(position[0],-position[1],position[2]);
994 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
995 cam->SetPosition(-position[0],position[1],position[2]);
998 this->Renderer->ResetCameraClippingRange();
999 this->UpdateDisplayExtent();
1003 void vvSlicer::FlipVerticalView()
1005 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1008 FlipHorizontalView();
1009 double *viewup = cam->GetViewUp();
1010 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1011 this->UpdateDisplayExtent();
1015 void vvSlicer::Render()
1017 if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
1020 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1021 legend->SetVisibility(1);
1023 else legend->SetVisibility(0);
1025 if (ca->GetVisibility())
1027 std::string worldPos = "";
1028 std::stringstream world1;
1029 std::stringstream world2;
1030 std::stringstream world3;
1031 world1 << (int)mCurrent[0];
1032 world2 << (int)mCurrent[1];
1033 world3 << (int)mCurrent[2];
1034 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1035 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1036 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1038 if (pdmA->GetVisibility())
1040 double x = mCursor[0];
1041 double y = mCursor[1];
1042 double z = mCursor[2];
1043 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1044 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1045 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1047 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1048 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1049 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1050 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1051 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1052 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1054 vtkRenderer * renderer = this->Renderer;
1056 renderer->WorldToView(x,y,z);
1057 renderer->ViewToNormalizedViewport(x,y,z);
1058 renderer->NormalizedViewportToViewport(x,y);
1059 renderer->ViewportToNormalizedDisplay(x,y);
1060 renderer->NormalizedDisplayToDisplay(x,y);
1061 crossCursor->SetFocalPoint(x,y,z);
1064 crossCursor->SetFocalPoint(-1,-1,z);
1067 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1068 X <= this->GetInput()->GetWholeExtent()[1] &&
1069 Y >= this->GetInput()->GetWholeExtent()[2] &&
1070 Y <= this->GetInput()->GetWholeExtent()[3] &&
1071 Z >= this->GetInput()->GetWholeExtent()[4] &&
1072 Z <= this->GetInput()->GetWholeExtent()[5])
1074 std::stringstream pixel1;
1075 std::stringstream pixel2;
1076 std::stringstream pixel3;
1077 std::stringstream temps;
1081 temps << mCurrentTSlice;
1082 double value = this->GetInput()->GetScalarComponentAsDouble(
1087 std::stringstream val;
1089 worldPos += "data value : " + val.str();
1090 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1091 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1093 ca->SetText(1,worldPos.c_str());
1095 if (mOverlay && mOverlayActor->GetVisibility())
1097 mOverlayMapper->SetWindow(this->GetColorWindow());
1098 mOverlayMapper->SetLevel(this->GetColorLevel());
1099 mOverlayMapper->Update();
1103 //this->Superclass::Render();
1104 this->GetRenderWindow()->Render();
1107 void vvSlicer::UpdateCursorPosition()
1109 if (this->GetImageActor()->GetVisibility())
1111 pdmA->SetVisibility(true);
1112 mCursor[0] = mCurrent[0];
1113 mCursor[1] = mCurrent[1];
1114 mCursor[2] = mCurrent[2];
1115 mCursor[3] = mCurrentTSlice;
1119 void vvSlicer::UpdateLandmarks()
1121 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1122 if (pd->GetPoints())
1124 mLandGlyph->SetRange(0,1);
1125 mLandGlyph->Modified();
1126 mLandGlyph->Update();
1128 mClipBox->Modified();
1129 mLandClipper->Update();
1130 mLandMapper->Update();
1135 //----------------------------------------------------------------------------
1136 void vvSlicer::SetSlice(int slice)
1138 int *range = this->GetSliceRange();
1141 if (slice < range[0])
1145 else if (slice > range[1])
1151 if (this->Slice == slice)
1156 this->Slice = slice;
1159 this->UpdateDisplayExtent();
1163 void vvSlicer::SetContourSlice()
1165 if (mSurfaceCutActors.size() > 0)
1166 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1167 i!=mSurfaceCutActors.end();i++)
1168 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1169 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1172 void vvSlicer::ForceUpdateDisplayExtent()
1174 this->UpdateDisplayExtent();
1177 int* vvSlicer::GetDisplayExtent()
1179 return this->GetImageActor()->GetDisplayExtent();
1182 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1184 this->Superclass::PrintSelf(os, indent);