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 =========================================================================*/
28 #include "vvSlicerManagerCommand.h"
29 #include "vvGlyphSource.h"
30 #include "vvGlyph2D.h"
31 #include "vvImageMapToWLColors.h"
33 #include <vtkTextProperty.h>
34 #include <vtkTextActor.h>
35 #include <vtkTextSource.h>
36 #include <vtkActor2D.h>
37 #include <vtkCursor2D.h>
38 #include <vtkPolyDataMapper2D.h>
39 #include <vtkProperty2D.h>
40 #include <vtkCornerAnnotation.h>
41 #include <vtkImageMapToWindowLevelColors.h>
42 #include <vtkImageData.h>
43 #include <vtkImageActor.h>
44 #include <vtkToolkits.h>
45 #include <vtkObjectFactory.h>
46 #include <vtkPointData.h>
47 #include <vtkDataArray.h>
48 #include <vtkFloatArray.h>
49 #include <vtkClipPolyData.h>
50 #include <vtkGlyph3D.h>
52 #include <vtkCursor3D.h>
53 #include <vtkProperty.h>
55 #include <vtkLightCollection.h>
56 #include <vtkScalarBarActor.h>
57 #include <vtkLookupTable.h>
59 #include <vtkRenderer.h>
60 #include <vtkRendererCollection.h>
61 #include <vtkRenderWindow.h>
62 #include <vtkRenderWindowInteractor.h>
63 #include <vtkCamera.h>
64 #include <vtkCallbackCommand.h>
65 #include <vtkCommand.h>
66 #include <vtkPolyDataMapper.h>
69 #include <vtkExtractVOI.h>
70 #include <vtkSphereSource.h>
71 #include <vtkCutter.h>
73 #include <vtkAssignAttribute.h>
75 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
76 vtkStandardNewMacro(vvSlicer);
83 mCurrent[0] = -VTK_DOUBLE_MAX;
84 mCurrent[1] = -VTK_DOUBLE_MAX;
85 mCurrent[2] = -VTK_DOUBLE_MAX;
87 mCursor[0] = -VTK_DOUBLE_MAX;
88 mCursor[1] = -VTK_DOUBLE_MAX;
89 mCursor[2] = -VTK_DOUBLE_MAX;
90 mCursor[3] = -VTK_DOUBLE_MAX;
96 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
97 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
98 text += "0,1,2,3,4,5 : preset windowing\n";
99 text += "6,7,8,9 : preset colormap\n";
100 text += "r : reset view\n";
101 text += "l : reload image\n";
102 text += "f : fly to mouse position\n";
103 text += "g : go to cross hair position\n\n";
104 text += "Up,down : change slice\n";
105 text += "Left,right : change tenporal slice\n\n";
106 text += "Scrollbar (or w/x) : zoom in/out\n";
107 text += "left button : synchronize all views\n";
108 text += "middle button : grab image\n";
109 text += "right button : change windowing\n";
111 crossCursor = vtkCursor2D::New();
112 crossCursor->AllOff();
113 crossCursor->AxesOn();
114 crossCursor->SetTranslationMode(1);
115 crossCursor->SetRadius(2);
117 pdm = vtkPolyDataMapper2D::New();
118 pdm->SetInput(crossCursor->GetOutput());
120 pdmA = vtkActor2D::New();
121 pdmA->SetMapper(pdm);
122 pdmA->GetProperty()->SetColor(255,10,212);
123 pdmA->SetVisibility(0);
124 pdmA->SetPickable(0);
126 ca = vtkCornerAnnotation::New();
127 ca->GetTextProperty()->SetColor(255,10,212);
128 ca->SetVisibility(1);
136 legend = vtkSmartPointer<vtkScalarBarActor>::New();
137 //legend->SetTitle("test!");
138 legend->SetPosition(0.82,0.18);
139 legend->SetWidth(0.1);
140 legend->SetVisibility(0);
141 legend->SetLabelFormat("%.1f");
142 this->GetRenderer()->AddActor(legend);
144 this->WindowLevel->Delete();
145 this->WindowLevel = vvImageMapToWLColors::New();
146 this->InstallPipeline();
149 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
150 return mOverlayMapper.GetPointer();
152 vtkImageActor* vvSlicer::GetOverlayActor() {
153 return mOverlayActor.GetPointer();
155 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
156 return mFusionMapper.GetPointer();
159 vtkImageActor* vvSlicer::GetFusionActor() {
160 return mFusionActor.GetPointer();
162 vtkActor* vvSlicer::GetVFActor() {
163 return mVFActor.GetPointer();
165 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
166 return ca.GetPointer();
169 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
172 mSurfaceCutActors.push_back(new vvMeshActor());
174 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
176 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
177 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
178 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
183 void vvSlicer::ToggleContourSuperposition()
185 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
186 i!=mSurfaceCutActors.end();i++)
187 (*i)->ToggleSuperposition();
190 void vvSlicer::SetCursorColor(int r,int g, int b)
192 pdmA->GetProperty()->SetColor(r,g,b);
194 void vvSlicer::SetCursorVisibility(bool s)
196 pdmA->SetVisibility(s);
198 bool vvSlicer::GetCursorVisibility()
200 return pdmA->GetVisibility();
203 vvSlicer::~vvSlicer()
205 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
206 i!=mSurfaceCutActors.end();i++)
210 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
218 void vvSlicer::SetImage(vvImage::Pointer image)
220 if (image->GetVTKImages().size())
223 this->Superclass::SetInput(image->GetVTKImages()[0]);
224 this->UpdateDisplayExtent();
226 ca->SetText(0,mFileName.c_str());
230 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
232 if (overlay->GetVTKImages().size())
237 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
238 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
242 mOverlayActor = vtkImageActor::New();
243 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
244 mOverlayActor->SetPickable(0);
245 mOverlayActor->SetVisibility(false);
246 mOverlayActor->SetOpacity(0.5);
247 this->UpdateDisplayExtent();
250 //stupid but necessary : the Overlay need to be rendered before fusion
253 this->GetRenderer()->RemoveActor(mFusionActor);
254 this->GetRenderer()->AddActor(mOverlayActor);
255 this->GetRenderer()->AddActor(mFusionActor);
258 this->GetRenderer()->AddActor(mOverlayActor);
261 SetTSlice(mCurrentTSlice);
265 void vvSlicer::SetFusion(vvImage::Pointer fusion)
267 if (fusion->GetVTKImages().size())
272 mFusionMapper = vtkImageMapToWindowLevelColors::New();
273 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);
287 SetTSlice(mCurrentTSlice);
291 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
293 if (actor_type == "vector")
295 this->mVFActor->SetVisibility(vis);
297 if (actor_type == "overlay")
299 this->mOverlayActor->SetVisibility(vis);
301 if (actor_type == "fusion")
303 this->mFusionActor->SetVisibility(vis);
305 if (actor_type == "contour")
306 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
307 UpdateDisplayExtent();
310 void vvSlicer::SetVF(vvImage::Pointer vf)
312 if (vf->GetVTKImages().size())
318 mAAFilter=vtkAssignAttribute::New();
319 mVOIFilter = vtkExtractVOI::New();
320 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
322 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
323 mAAFilter->SetInput(mVOIFilter->GetOutput());
324 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
325 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
328 mArrow = vvGlyphSource::New();
329 mArrow->SetGlyphTypeToSpecificArrow();
330 mArrow->SetScale(mScale);
333 // Glyph the gradient vector (with arrows)
335 mGlyphFilter = vvGlyph2D::New();
336 mGlyphFilter->SetInput(mAAFilter->GetOutput());
337 mGlyphFilter->SetSource(mArrow->GetOutput());
338 mGlyphFilter->ScalingOn();
339 mGlyphFilter->SetScaleModeToScaleByVector();
340 mGlyphFilter->OrientOn();
341 mGlyphFilter->SetVectorModeToUseVector();
342 mGlyphFilter->SetColorModeToColorByVector();
345 mVFMapper = vtkPolyDataMapper::New();
346 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
347 mVFMapper->SetInput(mGlyphFilter->GetOutput());
348 mVFMapper->ImmediateModeRenderingOn();
351 mVFActor = vtkActor::New();
352 mVFActor->SetMapper(mVFMapper);
353 mVFActor->SetPickable(0);
354 this->UpdateDisplayExtent();
355 this->GetRenderer()->AddActor(mVFActor);
358 SetTSlice(mCurrentTSlice);
362 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
364 mLandmarks = landmarks;
369 mCross = vtkCursor3D::New();
370 mCross->SetFocalPoint(0.0,0.0,0.0);
371 mCross->SetModelBounds(-10,10,-10,10,-10,10);
376 mLandGlyph = vtkGlyph3D::New();
377 mLandGlyph->SetSource(mCross->GetOutput());
378 mLandGlyph->SetInput(landmarks->GetOutput());
379 //mLandGlyph->SetIndexModeToScalar();
380 mLandGlyph->SetRange(0,1);
381 mLandGlyph->ScalingOff();
383 mLandGlyph->SetColorModeToColorByScalar();
386 mClipBox = vtkBox::New();
388 mLandClipper = vtkClipPolyData::New();
389 mLandClipper->InsideOutOn();
390 mLandClipper->SetInput(mLandGlyph->GetOutput());
391 mLandClipper->SetClipFunction(mClipBox);
394 mLandMapper = vtkPolyDataMapper::New();
395 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
396 //mLandMapper->ScalarVisibilityOff();
399 mLandActor = vtkActor::New();
400 mLandActor->SetMapper(mLandMapper);
401 mLandActor->GetProperty()->SetColor(255,10,212);
402 mLandActor->SetPickable(0);
403 mLandActor->SetVisibility(true);
404 this->UpdateDisplayExtent();
405 this->GetRenderer()->AddActor(mLandActor);
409 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
411 if (actor_type == "vector")
421 if (actor_type == "overlay")
423 Renderer->RemoveActor(mOverlayActor);
425 mOverlayActor = NULL;
426 mOverlayMapper = NULL;
428 if (actor_type == "fusion")
430 Renderer->RemoveActor(mFusionActor);
433 mFusionMapper = NULL;
435 if (actor_type == "contour")
437 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
438 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
442 void vvSlicer::SetVFSubSampling(int sub)
446 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
449 UpdateDisplayExtent();
453 void vvSlicer::SetVFScale(int scale)
457 mArrow->SetScale(mScale);
458 UpdateDisplayExtent();
462 void vvSlicer::SetVFLog(int log)
467 mGlyphFilter->SetUseLog(mVFLog);
468 mGlyphFilter->Modified();
470 UpdateDisplayExtent();
474 void vvSlicer::SetTSlice(int t)
478 else if ((unsigned int)t >= mImage->GetVTKImages().size())
479 t = mImage->GetVTKImages().size() -1;
481 this->SetInput(mImage->GetVTKImages()[t]);
482 if (mVF && mVFActor->GetVisibility())
484 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
485 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
487 if (mOverlay && mOverlayActor->GetVisibility())
489 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
490 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
492 if (mFusion && mFusionActor->GetVisibility())
494 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
495 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
497 if (mSurfaceCutActors.size() > 0)
498 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
499 i!=mSurfaceCutActors.end();i++)
500 (*i)->SetTimeSlice(mCurrentTSlice);
501 UpdateDisplayExtent();
504 int vvSlicer::GetTSlice()
506 return mCurrentTSlice;
509 void vvSlicer::SetSliceOrientation(int orientation)
511 //if 2D image, force to watch in Axial View
513 this->GetInput()->GetWholeExtent(extent);
514 if (extent[5]-extent[4] <= 2)
517 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
518 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
520 vtkErrorMacro("Error - invalid slice orientation " << orientation);
524 this->SliceOrientation = orientation;
527 int *range = this->GetSliceRange();
529 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
531 this->UpdateOrientation();
532 this->UpdateDisplayExtent();
534 if (this->Renderer && this->GetInput())
536 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
537 this->Renderer->ResetCamera();
538 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
544 //----------------------------------------------------------------------------
545 void vvSlicer::UpdateDisplayExtent()
547 vtkImageData *input = this->GetInput();
548 if (!input || !this->ImageActor)
552 input->UpdateInformation();
553 int *w_ext = input->GetWholeExtent();
555 switch (this->SliceOrientation)
557 case vtkImageViewer2::SLICE_ORIENTATION_XY:
558 this->ImageActor->SetDisplayExtent(
559 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
560 if (mVF && mVFActor->GetVisibility())
563 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
564 mVOIFilter->SetVOI(vfExtent);
565 mGlyphFilter->SetOrientation(1,1,0);
567 // put the vector field between the image and the camera
568 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
569 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
571 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
573 if (mOverlay && mOverlayActor->GetVisibility())
576 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
577 mOverlayActor->SetDisplayExtent(overExtent);
578 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
579 mOverlayActor->SetPosition(0,0,1);
581 mOverlayActor->SetPosition(0,0,-1);
583 if (mFusion && mFusionActor->GetVisibility())
586 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
587 mFusionActor->SetDisplayExtent(fusExtent);
588 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
589 mFusionActor->SetPosition(0,0,1.5);
591 mFusionActor->SetPosition(0,0,-1.5);
598 bounds[0] = ImageActor->GetBounds()[0];
599 bounds[1] = ImageActor->GetBounds()[1];
600 bounds[2] = ImageActor->GetBounds()[2];
601 bounds[3] = ImageActor->GetBounds()[3];
602 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
603 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
604 mClipBox->SetBounds(bounds);
607 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
608 mLandActor->SetPosition(0,0,1.5);
610 mLandActor->SetPosition(0,0,-1.5);
614 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
615 this->ImageActor->SetDisplayExtent(
616 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
617 if (mVF && mVFActor->GetVisibility())
620 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
621 mVOIFilter->SetVOI(vfExtent);
622 mGlyphFilter->SetOrientation(1,0,1);
624 // put the vector field between the image aSpacingnd the camera
625 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
626 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
628 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
630 if (mOverlay && mOverlayActor->GetVisibility())
633 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
634 mOverlayActor->SetDisplayExtent(overExtent);
635 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
636 mOverlayActor->SetPosition(0,1,0);
638 mOverlayActor->SetPosition(0,-1,0);
640 if (mFusion && mFusionActor->GetVisibility())
643 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
644 mFusionActor->SetDisplayExtent(fusExtent);
645 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
646 mFusionActor->SetPosition(0,1.5,0);
648 mFusionActor->SetPosition(0,-1.5,0);
655 bounds[0] = ImageActor->GetBounds()[0];
656 bounds[1] = ImageActor->GetBounds()[1];
657 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
658 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
659 bounds[4] = ImageActor->GetBounds()[4];
660 bounds[5] = ImageActor->GetBounds()[5];
661 mClipBox->SetBounds(bounds);
664 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
665 mLandActor->SetPosition(0,1.5,0);
667 mLandActor->SetPosition(0,-1.5,0);
671 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
672 this->ImageActor->SetDisplayExtent(
673 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
674 if (mVF && mVFActor->GetVisibility())
677 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
678 mVOIFilter->SetVOI(vfExtent);
679 mGlyphFilter->SetOrientation(0,1,1);
681 // put the vector field between the image and the camera
682 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
683 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
685 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
687 if (mOverlay && mOverlayActor->GetVisibility())
690 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
691 mOverlayActor->SetDisplayExtent(overExtent);
692 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
693 mOverlayActor->SetPosition(1,0,0);
695 mOverlayActor->SetPosition(-1,0,0);
697 if (mFusion && mFusionActor->GetVisibility())
700 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
701 mFusionActor->SetDisplayExtent(fusExtent);
702 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
703 mFusionActor->SetPosition(1.5,0,0);
705 mFusionActor->SetPosition(-1.5,0,0);
712 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
713 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
714 bounds[2] = ImageActor->GetBounds()[2];
715 bounds[3] = ImageActor->GetBounds()[3];
716 bounds[4] = ImageActor->GetBounds()[4];
717 bounds[5] = ImageActor->GetBounds()[5];
718 mClipBox->SetBounds(bounds);
721 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
722 mLandActor->SetPosition(1.5,0,0);
724 mLandActor->SetPosition(-1.5,0,0);
729 // Figure out the correct clipping range
733 if (this->InteractorStyle &&
734 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
736 this->Renderer->ResetCameraClippingRange();
740 vtkCamera *cam = this->Renderer->GetActiveCamera();
744 this->ImageActor->GetBounds(bounds);
745 double spos = (double)bounds[this->SliceOrientation * 2];
746 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
747 double range = fabs(spos - cpos);
748 double *spacing = input->GetSpacing();
750 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
751 cam->SetClippingRange(
752 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
758 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
760 vtkImageData* image=this->GetInput();
761 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
762 mVF->GetSpacing()[0];
763 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
764 mVF->GetSpacing()[0];
765 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
766 mVF->GetSpacing()[1];
767 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
768 mVF->GetSpacing()[1];
769 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
770 mVF->GetSpacing()[2];
771 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
772 mVF->GetSpacing()[2];
774 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
777 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
779 vtkImageData* image=this->GetInput();
780 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
781 mOverlay->GetSpacing()[0];
782 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
783 mOverlay->GetSpacing()[0];
784 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
785 mOverlay->GetSpacing()[1];
786 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
787 mOverlay->GetSpacing()[1];
788 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
789 mOverlay->GetSpacing()[2];
790 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
791 mOverlay->GetSpacing()[2];
792 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
795 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
797 vtkImageData* image=this->GetInput();
798 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
799 mFusion->GetSpacing()[0];
800 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
801 mFusion->GetSpacing()[0];
802 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
803 mFusion->GetSpacing()[1];
804 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
805 mFusion->GetSpacing()[1];
806 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
807 mFusion->GetSpacing()[2];
808 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
809 mFusion->GetSpacing()[2];
810 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
813 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
818 //2D overlay on 3D image specific case
819 if (refExtent[4] == refExtent[5])
822 extent[4] = refExtent[4];
823 extent[5] = refExtent[5];
826 for (int i = 0; i < maxBound; i = i+2)
828 //if we are totally outside the image
829 if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
834 //crop to the limit of the image
835 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
836 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
837 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
838 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
841 for (int i = 0; i < maxBound; i = i+2)
843 extent[i] = refExtent[i];
844 extent[i+1] = refExtent[i];
848 void vvSlicer::UpdateOrientation()
850 // Set the camera position
851 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
854 switch (this->SliceOrientation)
856 case vtkImageViewer2::SLICE_ORIENTATION_XY:
857 cam->SetFocalPoint(0,0,0);
858 cam->SetPosition(0,0,-1); // -1 if medical ?
859 cam->SetViewUp(0,-1,0);
862 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
863 cam->SetFocalPoint(0,0,0);
864 cam->SetPosition(0,-1,0); // 1 if medical ?
865 cam->SetViewUp(0,0,1);
868 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
869 cam->SetFocalPoint(0,0,0);
870 cam->SetPosition(-1,0,0); // -1 if medical ?
871 cam->SetViewUp(0,0,1);
877 void vvSlicer::SetOpacity(double s)
879 this->GetImageActor()->SetOpacity(s);
882 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
884 this->Superclass::SetRenderWindow(rw);
885 this->SetupInteractor(rw->GetInteractor());
886 ca->SetImageActor(this->GetImageActor());
887 ca->SetWindowLevel(this->GetWindowLevel());
888 ca->SetText(2, "<slice>");
889 ca->SetText(3, "<window>\n<level>");
901 crossCursor->SetModelBounds(bounds);
902 this->GetRenderer()->AddActor(pdmA);
903 this->GetRenderer()->AddActor(ca);
904 this->GetRenderer()->ResetCamera();
906 //this is just a mapping between the labeling of the orientations presented to the user and
907 //the one used by vtk
908 SetSliceOrientation(2-(orientation%3));
912 void vvSlicer::ResetCamera()
914 if (this->GetInput())
916 double* input_bounds=this->GetInput()->GetBounds();
917 double bmax=input_bounds[1]-input_bounds[0];
918 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
919 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
920 this->GetRenderer()->ResetCamera();
921 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
925 void vvSlicer::SetDisplayMode(bool i)
927 this->GetImageActor()->SetVisibility(i);
928 this->GetAnnotation()->SetVisibility(i);
929 this->GetRenderer()->SetDraw(i);
931 mLandActor->SetVisibility(i);
932 pdmA->SetVisibility(i);
934 UpdateDisplayExtent();
937 void vvSlicer::FlipHorizontalView()
939 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
942 double *position = cam->GetPosition();
943 switch (this->SliceOrientation)
945 case vtkImageViewer2::SLICE_ORIENTATION_XY:
946 cam->SetPosition(position[0],position[1],-position[2]);
949 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
950 cam->SetPosition(position[0],-position[1],position[2]);
953 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
954 cam->SetPosition(-position[0],position[1],position[2]);
957 this->Renderer->ResetCameraClippingRange();
958 this->UpdateDisplayExtent();
962 void vvSlicer::FlipVerticalView()
964 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
967 FlipHorizontalView();
968 double *viewup = cam->GetViewUp();
969 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
970 this->UpdateDisplayExtent();
974 void vvSlicer::Render()
976 if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
979 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
980 legend->SetVisibility(1);
982 else legend->SetVisibility(0);
984 if (ca->GetVisibility())
986 std::string worldPos = "";
987 std::stringstream world1;
988 std::stringstream world2;
989 std::stringstream world3;
990 world1 << (int)mCurrent[0];
991 world2 << (int)mCurrent[1];
992 world3 << (int)mCurrent[2];
993 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
994 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
995 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
997 if (pdmA->GetVisibility())
999 double x = mCursor[0];
1000 double y = mCursor[1];
1001 double z = mCursor[2];
1002 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1003 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1004 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1006 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1007 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1008 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1009 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1010 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1011 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1013 vtkRenderer * renderer = this->Renderer;
1015 renderer->WorldToView(x,y,z);
1016 renderer->ViewToNormalizedViewport(x,y,z);
1017 renderer->NormalizedViewportToViewport(x,y);
1018 renderer->ViewportToNormalizedDisplay(x,y);
1019 renderer->NormalizedDisplayToDisplay(x,y);
1020 crossCursor->SetFocalPoint(x,y,z);
1023 crossCursor->SetFocalPoint(-1,-1,z);
1026 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1027 X <= this->GetInput()->GetWholeExtent()[1] &&
1028 Y >= this->GetInput()->GetWholeExtent()[2] &&
1029 Y <= this->GetInput()->GetWholeExtent()[3] &&
1030 Z >= this->GetInput()->GetWholeExtent()[4] &&
1031 Z <= this->GetInput()->GetWholeExtent()[5])
1033 std::stringstream pixel1;
1034 std::stringstream pixel2;
1035 std::stringstream pixel3;
1036 std::stringstream temps;
1040 temps << mCurrentTSlice;
1041 double value = this->GetInput()->GetScalarComponentAsDouble(
1046 std::stringstream val;
1048 worldPos += "data value : " + val.str();
1049 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1050 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1052 ca->SetText(1,worldPos.c_str());
1054 if (mOverlay && mOverlayActor->GetVisibility())
1056 mOverlayMapper->SetWindow(this->GetColorWindow());
1057 mOverlayMapper->SetLevel(this->GetColorLevel());
1058 mOverlayMapper->Update();
1062 //this->Superclass::Render();
1063 this->GetRenderWindow()->Render();
1066 void vvSlicer::UpdateCursorPosition()
1068 if (this->GetImageActor()->GetVisibility())
1070 pdmA->SetVisibility(true);
1071 mCursor[0] = mCurrent[0];
1072 mCursor[1] = mCurrent[1];
1073 mCursor[2] = mCurrent[2];
1074 mCursor[3] = mCurrentTSlice;
1078 void vvSlicer::UpdateLandmarks()
1080 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1081 if (pd->GetPoints())
1083 mLandGlyph->SetRange(0,1);
1084 mLandGlyph->Modified();
1085 mLandGlyph->Update();
1087 mClipBox->Modified();
1088 mLandClipper->Update();
1089 mLandMapper->Update();
1094 //----------------------------------------------------------------------------
1095 void vvSlicer::SetSlice(int slice)
1097 int *range = this->GetSliceRange();
1100 if (slice < range[0])
1104 else if (slice > range[1])
1110 if (this->Slice == slice)
1115 this->Slice = slice;
1118 this->UpdateDisplayExtent();
1122 void vvSlicer::SetContourSlice()
1124 if (mSurfaceCutActors.size() > 0)
1125 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1126 i!=mSurfaceCutActors.end();i++)
1127 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1128 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1131 void vvSlicer::ForceUpdateDisplayExtent()
1133 this->UpdateDisplayExtent();
1136 int* vvSlicer::GetDisplayExtent()
1138 return this->GetImageActor()->GetDisplayExtent();
1141 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1143 this->Superclass::PrintSelf(os, indent);