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 //FIXME: this function leaks memory, we should fix it someday :)
410 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
412 if (actor_type == "vector")
414 Renderer->RemoveActor(mVFActor);
423 if (actor_type == "overlay")
425 Renderer->RemoveActor(mOverlayActor);
427 mOverlayActor = NULL;
428 mOverlayMapper = NULL;
430 if (actor_type == "fusion")
432 Renderer->RemoveActor(mFusionActor);
435 mFusionMapper = NULL;
437 if (actor_type == "contour")
439 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
440 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
444 void vvSlicer::SetVFSubSampling(int sub)
448 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
451 UpdateDisplayExtent();
455 void vvSlicer::SetVFScale(int scale)
459 mArrow->SetScale(mScale);
460 UpdateDisplayExtent();
464 void vvSlicer::SetVFLog(int log)
469 mGlyphFilter->SetUseLog(mVFLog);
470 mGlyphFilter->Modified();
472 UpdateDisplayExtent();
476 void vvSlicer::SetTSlice(int t)
480 else if ((unsigned int)t >= mImage->GetVTKImages().size())
481 t = mImage->GetVTKImages().size() -1;
483 this->SetInput(mImage->GetVTKImages()[t]);
484 if (mVF && mVFActor->GetVisibility())
486 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
487 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
489 if (mOverlay && mOverlayActor->GetVisibility())
491 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
492 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
494 if (mFusion && mFusionActor->GetVisibility())
496 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
497 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
499 if (mSurfaceCutActors.size() > 0)
500 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
501 i!=mSurfaceCutActors.end();i++)
502 (*i)->SetTimeSlice(mCurrentTSlice);
503 UpdateDisplayExtent();
506 int vvSlicer::GetTSlice()
508 return mCurrentTSlice;
511 void vvSlicer::SetSliceOrientation(int orientation)
513 //if 2D image, force to watch in Axial View
515 this->GetInput()->GetWholeExtent(extent);
516 if (extent[5]-extent[4] <= 2)
519 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
520 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
522 vtkErrorMacro("Error - invalid slice orientation " << orientation);
526 this->SliceOrientation = orientation;
529 int *range = this->GetSliceRange();
531 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
533 this->UpdateOrientation();
534 this->UpdateDisplayExtent();
536 if (this->Renderer && this->GetInput())
538 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
539 this->Renderer->ResetCamera();
540 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
546 //----------------------------------------------------------------------------
547 void vvSlicer::UpdateDisplayExtent()
549 vtkImageData *input = this->GetInput();
550 if (!input || !this->ImageActor)
554 input->UpdateInformation();
555 int *w_ext = input->GetWholeExtent();
557 switch (this->SliceOrientation)
559 case vtkImageViewer2::SLICE_ORIENTATION_XY:
560 this->ImageActor->SetDisplayExtent(
561 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
562 if (mVF && mVFActor->GetVisibility())
565 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
566 mVOIFilter->SetVOI(vfExtent);
567 mGlyphFilter->SetOrientation(1,1,0);
569 // put the vector field between the image and the camera
570 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
571 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
573 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
575 if (mOverlay && mOverlayActor->GetVisibility())
578 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
579 mOverlayActor->SetDisplayExtent(overExtent);
580 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
581 mOverlayActor->SetPosition(0,0,1);
583 mOverlayActor->SetPosition(0,0,-1);
585 if (mFusion && mFusionActor->GetVisibility())
588 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
589 mFusionActor->SetDisplayExtent(fusExtent);
590 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
591 mFusionActor->SetPosition(0,0,1.5);
593 mFusionActor->SetPosition(0,0,-1.5);
600 bounds[0] = ImageActor->GetBounds()[0];
601 bounds[1] = ImageActor->GetBounds()[1];
602 bounds[2] = ImageActor->GetBounds()[2];
603 bounds[3] = ImageActor->GetBounds()[3];
604 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
605 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
606 mClipBox->SetBounds(bounds);
609 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
610 mLandActor->SetPosition(0,0,1.5);
612 mLandActor->SetPosition(0,0,-1.5);
616 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
617 this->ImageActor->SetDisplayExtent(
618 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
619 if (mVF && mVFActor->GetVisibility())
622 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
623 mVOIFilter->SetVOI(vfExtent);
624 mGlyphFilter->SetOrientation(1,0,1);
626 // put the vector field between the image aSpacingnd the camera
627 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
628 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
630 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
632 if (mOverlay && mOverlayActor->GetVisibility())
635 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
636 mOverlayActor->SetDisplayExtent(overExtent);
637 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
638 mOverlayActor->SetPosition(0,1,0);
640 mOverlayActor->SetPosition(0,-1,0);
642 if (mFusion && mFusionActor->GetVisibility())
645 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
646 mFusionActor->SetDisplayExtent(fusExtent);
647 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
648 mFusionActor->SetPosition(0,1.5,0);
650 mFusionActor->SetPosition(0,-1.5,0);
657 bounds[0] = ImageActor->GetBounds()[0];
658 bounds[1] = ImageActor->GetBounds()[1];
659 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
660 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
661 bounds[4] = ImageActor->GetBounds()[4];
662 bounds[5] = ImageActor->GetBounds()[5];
663 mClipBox->SetBounds(bounds);
666 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
667 mLandActor->SetPosition(0,1.5,0);
669 mLandActor->SetPosition(0,-1.5,0);
673 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
674 this->ImageActor->SetDisplayExtent(
675 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
676 if (mVF && mVFActor->GetVisibility())
679 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
680 mVOIFilter->SetVOI(vfExtent);
681 mGlyphFilter->SetOrientation(0,1,1);
683 // put the vector field between the image and the camera
684 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
685 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
687 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
689 if (mOverlay && mOverlayActor->GetVisibility())
692 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
693 mOverlayActor->SetDisplayExtent(overExtent);
694 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
695 mOverlayActor->SetPosition(1,0,0);
697 mOverlayActor->SetPosition(-1,0,0);
699 if (mFusion && mFusionActor->GetVisibility())
702 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
703 mFusionActor->SetDisplayExtent(fusExtent);
704 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
705 mFusionActor->SetPosition(1.5,0,0);
707 mFusionActor->SetPosition(-1.5,0,0);
714 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
715 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
716 bounds[2] = ImageActor->GetBounds()[2];
717 bounds[3] = ImageActor->GetBounds()[3];
718 bounds[4] = ImageActor->GetBounds()[4];
719 bounds[5] = ImageActor->GetBounds()[5];
720 mClipBox->SetBounds(bounds);
723 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
724 mLandActor->SetPosition(1.5,0,0);
726 mLandActor->SetPosition(-1.5,0,0);
731 // Figure out the correct clipping range
735 if (this->InteractorStyle &&
736 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
738 this->Renderer->ResetCameraClippingRange();
742 vtkCamera *cam = this->Renderer->GetActiveCamera();
746 this->ImageActor->GetBounds(bounds);
747 double spos = (double)bounds[this->SliceOrientation * 2];
748 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
749 double range = fabs(spos - cpos);
750 double *spacing = input->GetSpacing();
752 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
753 cam->SetClippingRange(
754 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
760 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
762 vtkImageData* image=this->GetInput();
763 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
764 mVF->GetSpacing()[0];
765 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
766 mVF->GetSpacing()[0];
767 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
768 mVF->GetSpacing()[1];
769 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
770 mVF->GetSpacing()[1];
771 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
772 mVF->GetSpacing()[2];
773 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
774 mVF->GetSpacing()[2];
776 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
779 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
781 vtkImageData* image=this->GetInput();
782 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
783 mOverlay->GetSpacing()[0];
784 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
785 mOverlay->GetSpacing()[0];
786 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
787 mOverlay->GetSpacing()[1];
788 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
789 mOverlay->GetSpacing()[1];
790 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
791 mOverlay->GetSpacing()[2];
792 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
793 mOverlay->GetSpacing()[2];
794 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
797 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
799 vtkImageData* image=this->GetInput();
800 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
801 mFusion->GetSpacing()[0];
802 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
803 mFusion->GetSpacing()[0];
804 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
805 mFusion->GetSpacing()[1];
806 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
807 mFusion->GetSpacing()[1];
808 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
809 mFusion->GetSpacing()[2];
810 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
811 mFusion->GetSpacing()[2];
812 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
815 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
820 //2D overlay on 3D image specific case
821 if (refExtent[4] == refExtent[5])
824 extent[4] = refExtent[4];
825 extent[5] = refExtent[5];
828 for (int i = 0; i < maxBound; i = i+2)
830 //if we are totally outside the image
831 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] )
836 //crop to the limit of the image
837 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
838 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
839 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
840 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
843 for (int i = 0; i < maxBound; i = i+2)
845 extent[i] = refExtent[i];
846 extent[i+1] = refExtent[i];
850 void vvSlicer::UpdateOrientation()
852 // Set the camera position
853 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
856 switch (this->SliceOrientation)
858 case vtkImageViewer2::SLICE_ORIENTATION_XY:
859 cam->SetFocalPoint(0,0,0);
860 cam->SetPosition(0,0,-1); // -1 if medical ?
861 cam->SetViewUp(0,-1,0);
864 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
865 cam->SetFocalPoint(0,0,0);
866 cam->SetPosition(0,-1,0); // 1 if medical ?
867 cam->SetViewUp(0,0,1);
870 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
871 cam->SetFocalPoint(0,0,0);
872 cam->SetPosition(-1,0,0); // -1 if medical ?
873 cam->SetViewUp(0,0,1);
879 void vvSlicer::SetOpacity(double s)
881 this->GetImageActor()->SetOpacity(s);
884 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
886 this->Superclass::SetRenderWindow(rw);
887 this->SetupInteractor(rw->GetInteractor());
888 ca->SetImageActor(this->GetImageActor());
889 ca->SetWindowLevel(this->GetWindowLevel());
890 ca->SetText(2, "<slice>");
891 ca->SetText(3, "<window>\n<level>");
903 crossCursor->SetModelBounds(bounds);
904 this->GetRenderer()->AddActor(pdmA);
905 this->GetRenderer()->AddActor(ca);
906 this->GetRenderer()->ResetCamera();
908 //this is just a mapping between the labeling of the orientations presented to the user and
909 //the one used by vtk
910 SetSliceOrientation(2-(orientation%3));
914 void vvSlicer::ResetCamera()
916 if (this->GetInput())
918 double* input_bounds=this->GetInput()->GetBounds();
919 double bmax=input_bounds[1]-input_bounds[0];
920 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
921 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
922 this->GetRenderer()->ResetCamera();
923 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
927 void vvSlicer::SetDisplayMode(bool i)
929 this->GetImageActor()->SetVisibility(i);
930 this->GetAnnotation()->SetVisibility(i);
931 this->GetRenderer()->SetDraw(i);
933 mLandActor->SetVisibility(i);
934 pdmA->SetVisibility(i);
936 UpdateDisplayExtent();
939 void vvSlicer::FlipHorizontalView()
941 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
944 double *position = cam->GetPosition();
945 switch (this->SliceOrientation)
947 case vtkImageViewer2::SLICE_ORIENTATION_XY:
948 cam->SetPosition(position[0],position[1],-position[2]);
951 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
952 cam->SetPosition(position[0],-position[1],position[2]);
955 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
956 cam->SetPosition(-position[0],position[1],position[2]);
959 this->Renderer->ResetCameraClippingRange();
960 this->UpdateDisplayExtent();
964 void vvSlicer::FlipVerticalView()
966 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
969 FlipHorizontalView();
970 double *viewup = cam->GetViewUp();
971 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
972 this->UpdateDisplayExtent();
976 void vvSlicer::Render()
978 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion)
980 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
981 legend->SetVisibility(1);
983 else legend->SetVisibility(0);
985 if (ca->GetVisibility())
987 std::string worldPos = "";
988 std::stringstream world1;
989 std::stringstream world2;
990 std::stringstream world3;
991 world1 << (int)mCurrent[0];
992 world2 << (int)mCurrent[1];
993 world3 << (int)mCurrent[2];
994 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
995 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
996 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
998 if (pdmA->GetVisibility())
1000 double x = mCursor[0];
1001 double y = mCursor[1];
1002 double z = mCursor[2];
1003 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1004 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1005 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1007 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1008 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1009 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1010 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1011 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1012 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1014 vtkRenderer * renderer = this->Renderer;
1016 renderer->WorldToView(x,y,z);
1017 renderer->ViewToNormalizedViewport(x,y,z);
1018 renderer->NormalizedViewportToViewport(x,y);
1019 renderer->ViewportToNormalizedDisplay(x,y);
1020 renderer->NormalizedDisplayToDisplay(x,y);
1021 crossCursor->SetFocalPoint(x,y,z);
1024 crossCursor->SetFocalPoint(-1,-1,z);
1027 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1028 X <= this->GetInput()->GetWholeExtent()[1] &&
1029 Y >= this->GetInput()->GetWholeExtent()[2] &&
1030 Y <= this->GetInput()->GetWholeExtent()[3] &&
1031 Z >= this->GetInput()->GetWholeExtent()[4] &&
1032 Z <= this->GetInput()->GetWholeExtent()[5])
1034 std::stringstream pixel1;
1035 std::stringstream pixel2;
1036 std::stringstream pixel3;
1037 std::stringstream temps;
1041 temps << mCurrentTSlice;
1042 double value = this->GetInput()->GetScalarComponentAsDouble(
1047 std::stringstream val;
1049 worldPos += "data value : " + val.str();
1050 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1051 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1053 ca->SetText(1,worldPos.c_str());
1055 if (mOverlay && mOverlayActor->GetVisibility())
1057 mOverlayMapper->SetWindow(this->GetColorWindow());
1058 mOverlayMapper->SetLevel(this->GetColorLevel());
1059 mOverlayMapper->Update();
1063 //this->Superclass::Render();
1064 this->GetRenderWindow()->Render();
1067 void vvSlicer::UpdateCursorPosition()
1069 if (this->GetImageActor()->GetVisibility())
1071 pdmA->SetVisibility(true);
1072 mCursor[0] = mCurrent[0];
1073 mCursor[1] = mCurrent[1];
1074 mCursor[2] = mCurrent[2];
1075 mCursor[3] = mCurrentTSlice;
1079 void vvSlicer::UpdateLandmarks()
1081 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1082 if (pd->GetPoints())
1084 mLandGlyph->SetRange(0,1);
1085 mLandGlyph->Modified();
1086 mLandGlyph->Update();
1088 mClipBox->Modified();
1089 mLandClipper->Update();
1090 mLandMapper->Update();
1095 //----------------------------------------------------------------------------
1096 void vvSlicer::SetSlice(int slice)
1098 int *range = this->GetSliceRange();
1101 if (slice < range[0])
1105 else if (slice > range[1])
1111 if (this->Slice == slice)
1116 this->Slice = slice;
1119 this->UpdateDisplayExtent();
1123 void vvSlicer::SetContourSlice()
1125 if (mSurfaceCutActors.size() > 0)
1126 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1127 i!=mSurfaceCutActors.end();i++)
1128 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1129 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1132 void vvSlicer::ForceUpdateDisplayExtent()
1134 this->UpdateDisplayExtent();
1137 int* vvSlicer::GetDisplayExtent()
1139 return this->GetImageActor()->GetDisplayExtent();
1142 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1144 this->Superclass::PrintSelf(os, indent);