1 /*=========================================================================
5 Author : Pierre Seroul (pierre.seroul@gmail.com)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 =========================================================================*/
27 #include "vvSlicerManagerCommand.h"
28 #include "vvGlyphSource.h"
29 #include "vvGlyph2D.h"
30 #include "vvImageMapToWLColors.h"
32 #include <vtkTextProperty.h>
33 #include <vtkTextActor.h>
34 #include <vtkTextSource.h>
35 #include <vtkActor2D.h>
36 #include <vtkCursor2D.h>
37 #include <vtkPolyDataMapper2D.h>
38 #include <vtkProperty2D.h>
39 #include <vtkCornerAnnotation.h>
40 #include <vtkImageMapToWindowLevelColors.h>
41 #include <vtkImageData.h>
42 #include <vtkImageActor.h>
43 #include <vtkToolkits.h>
44 #include <vtkObjectFactory.h>
45 #include <vtkPointData.h>
46 #include <vtkDataArray.h>
47 #include <vtkFloatArray.h>
48 #include <vtkClipPolyData.h>
49 #include <vtkGlyph3D.h>
51 #include <vtkCursor3D.h>
52 #include <vtkProperty.h>
54 #include <vtkLightCollection.h>
55 #include <vtkScalarBarActor.h>
56 #include <vtkLookupTable.h>
58 #include <vtkRenderer.h>
59 #include <vtkRendererCollection.h>
60 #include <vtkRenderWindow.h>
61 #include <vtkRenderWindowInteractor.h>
62 #include <vtkCamera.h>
63 #include <vtkCallbackCommand.h>
64 #include <vtkCommand.h>
65 #include <vtkPolyDataMapper.h>
68 #include <vtkExtractVOI.h>
69 #include <vtkSphereSource.h>
70 #include <vtkCutter.h>
72 #include <vtkAssignAttribute.h>
74 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
75 vtkStandardNewMacro(vvSlicer);
82 mCurrent[0] = -VTK_DOUBLE_MAX;
83 mCurrent[1] = -VTK_DOUBLE_MAX;
84 mCurrent[2] = -VTK_DOUBLE_MAX;
86 mCursor[0] = -VTK_DOUBLE_MAX;
87 mCursor[1] = -VTK_DOUBLE_MAX;
88 mCursor[2] = -VTK_DOUBLE_MAX;
89 mCursor[3] = -VTK_DOUBLE_MAX;
95 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
96 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
97 text += "0,1,2,3,4,5 : preset windowing\n";
98 text += "6,7,8,9 : preset colormap\n";
99 text += "r : reset view\n";
100 text += "l : reload image\n";
101 text += "f : fly to mouse position\n";
102 text += "g : go to cross hair position\n\n";
103 text += "Up,down : change slice\n";
104 text += "Left,right : change tenporal slice\n\n";
105 text += "Scrollbar (or w/x) : zoom in/out\n";
106 text += "left button : synchronize all views\n";
107 text += "middle button : grab image\n";
108 text += "right button : change windowing\n";
110 crossCursor = vtkCursor2D::New();
111 crossCursor->AllOff();
112 crossCursor->AxesOn();
113 crossCursor->SetTranslationMode(1);
114 crossCursor->SetRadius(2);
116 pdm = vtkPolyDataMapper2D::New();
117 pdm->SetInput(crossCursor->GetOutput());
119 pdmA = vtkActor2D::New();
120 pdmA->SetMapper(pdm);
121 pdmA->GetProperty()->SetColor(255,10,212);
122 pdmA->SetVisibility(0);
123 pdmA->SetPickable(0);
125 ca = vtkCornerAnnotation::New();
126 ca->GetTextProperty()->SetColor(255,10,212);
127 ca->SetVisibility(1);
135 legend = vtkSmartPointer<vtkScalarBarActor>::New();
136 //legend->SetTitle("test!");
137 legend->SetPosition(0.82,0.18);
138 legend->SetWidth(0.1);
139 legend->SetVisibility(0);
140 legend->SetLabelFormat("%.1f");
141 this->GetRenderer()->AddActor(legend);
143 this->WindowLevel->Delete();
144 this->WindowLevel = vvImageMapToWLColors::New();
145 this->InstallPipeline();
148 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
149 return mOverlayMapper.GetPointer();
151 vtkImageActor* vvSlicer::GetOverlayActor() {
152 return mOverlayActor.GetPointer();
154 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
155 return mFusionMapper.GetPointer();
158 vtkImageActor* vvSlicer::GetFusionActor() {
159 return mFusionActor.GetPointer();
161 vtkActor* vvSlicer::GetVFActor() {
162 return mVFActor.GetPointer();
164 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
165 return ca.GetPointer();
168 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
171 mSurfaceCutActors.push_back(new vvMeshActor());
173 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
175 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
176 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
177 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
182 void vvSlicer::ToggleContourSuperposition()
184 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
185 i!=mSurfaceCutActors.end();i++)
186 (*i)->ToggleSuperposition();
189 void vvSlicer::SetCursorColor(int r,int g, int b)
191 pdmA->GetProperty()->SetColor(r,g,b);
193 void vvSlicer::SetCursorVisibility(bool s)
195 pdmA->SetVisibility(s);
197 bool vvSlicer::GetCursorVisibility()
199 return pdmA->GetVisibility();
202 vvSlicer::~vvSlicer()
204 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
205 i!=mSurfaceCutActors.end();i++)
209 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
217 void vvSlicer::SetImage(vvImage::Pointer image)
219 if (image->GetVTKImages().size())
222 this->Superclass::SetInput(image->GetVTKImages()[0]);
223 this->UpdateDisplayExtent();
225 ca->SetText(0,mFileName.c_str());
229 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
231 if (overlay->GetVTKImages().size())
236 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
237 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
241 mOverlayActor = vtkImageActor::New();
242 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
243 mOverlayActor->SetPickable(0);
244 mOverlayActor->SetVisibility(false);
245 mOverlayActor->SetOpacity(0.5);
246 this->UpdateDisplayExtent();
249 //stupid but necessary : the Overlay need to be rendered before fusion
252 this->GetRenderer()->RemoveActor(mFusionActor);
253 this->GetRenderer()->AddActor(mOverlayActor);
254 this->GetRenderer()->AddActor(mFusionActor);
257 this->GetRenderer()->AddActor(mOverlayActor);
260 SetTSlice(mCurrentTSlice);
264 void vvSlicer::SetFusion(vvImage::Pointer fusion)
266 if (fusion->GetVTKImages().size())
271 mFusionMapper = vtkImageMapToWindowLevelColors::New();
272 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
276 mFusionActor = vtkImageActor::New();
277 mFusionActor->SetInput(mFusionMapper->GetOutput());
278 mFusionActor->SetPickable(0);
279 mFusionActor->SetVisibility(false);
280 mFusionActor->SetOpacity(0.7);
281 this->UpdateDisplayExtent();
282 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 //FIXME: this function leaks memory, we should fix it someday :)
409 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
411 if (actor_type == "vector")
413 Renderer->RemoveActor(mVFActor);
422 if (actor_type == "overlay")
424 Renderer->RemoveActor(mOverlayActor);
426 mOverlayActor = NULL;
427 mOverlayMapper = NULL;
429 if (actor_type == "fusion")
431 Renderer->RemoveActor(mFusionActor);
434 mFusionMapper = NULL;
436 if (actor_type == "contour")
438 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
439 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
443 void vvSlicer::SetVFSubSampling(int sub)
447 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
450 UpdateDisplayExtent();
454 void vvSlicer::SetVFScale(int scale)
458 mArrow->SetScale(mScale);
459 UpdateDisplayExtent();
463 void vvSlicer::SetVFLog(int log)
468 mGlyphFilter->SetUseLog(mVFLog);
469 mGlyphFilter->Modified();
471 UpdateDisplayExtent();
475 void vvSlicer::SetTSlice(int t)
479 else if ((unsigned int)t >= mImage->GetVTKImages().size())
480 t = mImage->GetVTKImages().size() -1;
482 this->SetInput(mImage->GetVTKImages()[t]);
483 if (mVF && mVFActor->GetVisibility())
485 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
486 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
488 if (mOverlay && mOverlayActor->GetVisibility())
490 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
491 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
493 if (mFusion && mFusionActor->GetVisibility())
495 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
496 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
498 if (mSurfaceCutActors.size() > 0)
499 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
500 i!=mSurfaceCutActors.end();i++)
501 (*i)->SetTimeSlice(mCurrentTSlice);
502 UpdateDisplayExtent();
505 int vvSlicer::GetTSlice()
507 return mCurrentTSlice;
510 void vvSlicer::SetSliceOrientation(int orientation)
512 //if 2D image, force to watch in Axial View
514 this->GetInput()->GetWholeExtent(extent);
515 if (extent[5]-extent[4] <= 2)
518 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
519 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
521 vtkErrorMacro("Error - invalid slice orientation " << orientation);
525 this->SliceOrientation = orientation;
528 int *range = this->GetSliceRange();
530 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
532 this->UpdateOrientation();
533 this->UpdateDisplayExtent();
535 if (this->Renderer && this->GetInput())
537 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
538 this->Renderer->ResetCamera();
539 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
545 //----------------------------------------------------------------------------
546 void vvSlicer::UpdateDisplayExtent()
548 vtkImageData *input = this->GetInput();
549 if (!input || !this->ImageActor)
553 input->UpdateInformation();
554 int *w_ext = input->GetWholeExtent();
556 switch (this->SliceOrientation)
558 case vtkImageViewer2::SLICE_ORIENTATION_XY:
559 this->ImageActor->SetDisplayExtent(
560 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
561 if (mVF && mVFActor->GetVisibility())
564 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
565 mVOIFilter->SetVOI(vfExtent);
566 mGlyphFilter->SetOrientation(1,1,0);
568 // put the vector field between the image and the camera
569 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
570 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
572 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
574 if (mOverlay && mOverlayActor->GetVisibility())
577 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
578 mOverlayActor->SetDisplayExtent(overExtent);
579 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
580 mOverlayActor->SetPosition(0,0,1);
582 mOverlayActor->SetPosition(0,0,-1);
584 if (mFusion && mFusionActor->GetVisibility())
587 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
588 mFusionActor->SetDisplayExtent(fusExtent);
589 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
590 mFusionActor->SetPosition(0,0,1.5);
592 mFusionActor->SetPosition(0,0,-1.5);
599 bounds[0] = ImageActor->GetBounds()[0];
600 bounds[1] = ImageActor->GetBounds()[1];
601 bounds[2] = ImageActor->GetBounds()[2];
602 bounds[3] = ImageActor->GetBounds()[3];
603 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
604 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
605 mClipBox->SetBounds(bounds);
608 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
609 mLandActor->SetPosition(0,0,1.5);
611 mLandActor->SetPosition(0,0,-1.5);
615 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
616 this->ImageActor->SetDisplayExtent(
617 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
618 if (mVF && mVFActor->GetVisibility())
621 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
622 mVOIFilter->SetVOI(vfExtent);
623 mGlyphFilter->SetOrientation(1,0,1);
625 // put the vector field between the image aSpacingnd the camera
626 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
627 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
629 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
631 if (mOverlay && mOverlayActor->GetVisibility())
634 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
635 mOverlayActor->SetDisplayExtent(overExtent);
636 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
637 mOverlayActor->SetPosition(0,1,0);
639 mOverlayActor->SetPosition(0,-1,0);
641 if (mFusion && mFusionActor->GetVisibility())
644 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
645 mFusionActor->SetDisplayExtent(fusExtent);
646 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
647 mFusionActor->SetPosition(0,1.5,0);
649 mFusionActor->SetPosition(0,-1.5,0);
656 bounds[0] = ImageActor->GetBounds()[0];
657 bounds[1] = ImageActor->GetBounds()[1];
658 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
659 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
660 bounds[4] = ImageActor->GetBounds()[4];
661 bounds[5] = ImageActor->GetBounds()[5];
662 mClipBox->SetBounds(bounds);
665 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
666 mLandActor->SetPosition(0,1.5,0);
668 mLandActor->SetPosition(0,-1.5,0);
672 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
673 this->ImageActor->SetDisplayExtent(
674 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
675 if (mVF && mVFActor->GetVisibility())
678 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
679 mVOIFilter->SetVOI(vfExtent);
680 mGlyphFilter->SetOrientation(0,1,1);
682 // put the vector field between the image and the camera
683 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
684 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
686 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
688 if (mOverlay && mOverlayActor->GetVisibility())
691 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
692 mOverlayActor->SetDisplayExtent(overExtent);
693 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
694 mOverlayActor->SetPosition(1,0,0);
696 mOverlayActor->SetPosition(-1,0,0);
698 if (mFusion && mFusionActor->GetVisibility())
701 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
702 mFusionActor->SetDisplayExtent(fusExtent);
703 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
704 mFusionActor->SetPosition(1.5,0,0);
706 mFusionActor->SetPosition(-1.5,0,0);
713 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
714 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
715 bounds[2] = ImageActor->GetBounds()[2];
716 bounds[3] = ImageActor->GetBounds()[3];
717 bounds[4] = ImageActor->GetBounds()[4];
718 bounds[5] = ImageActor->GetBounds()[5];
719 mClipBox->SetBounds(bounds);
722 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
723 mLandActor->SetPosition(1.5,0,0);
725 mLandActor->SetPosition(-1.5,0,0);
730 // Figure out the correct clipping range
734 if (this->InteractorStyle &&
735 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
737 this->Renderer->ResetCameraClippingRange();
741 vtkCamera *cam = this->Renderer->GetActiveCamera();
745 this->ImageActor->GetBounds(bounds);
746 double spos = (double)bounds[this->SliceOrientation * 2];
747 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
748 double range = fabs(spos - cpos);
749 double *spacing = input->GetSpacing();
751 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
752 cam->SetClippingRange(
753 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
759 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
761 vtkImageData* image=this->GetInput();
762 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
763 mVF->GetSpacing()[0];
764 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
765 mVF->GetSpacing()[0];
766 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
767 mVF->GetSpacing()[1];
768 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
769 mVF->GetSpacing()[1];
770 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
771 mVF->GetSpacing()[2];
772 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
773 mVF->GetSpacing()[2];
775 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
778 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
780 vtkImageData* image=this->GetInput();
781 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
782 mOverlay->GetSpacing()[0];
783 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
784 mOverlay->GetSpacing()[0];
785 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
786 mOverlay->GetSpacing()[1];
787 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
788 mOverlay->GetSpacing()[1];
789 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
790 mOverlay->GetSpacing()[2];
791 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
792 mOverlay->GetSpacing()[2];
793 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
796 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
798 vtkImageData* image=this->GetInput();
799 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
800 mFusion->GetSpacing()[0];
801 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
802 mFusion->GetSpacing()[0];
803 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
804 mFusion->GetSpacing()[1];
805 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
806 mFusion->GetSpacing()[1];
807 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
808 mFusion->GetSpacing()[2];
809 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
810 mFusion->GetSpacing()[2];
811 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
814 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
819 //2D overlay on 3D image specific case
820 if (refExtent[4] == refExtent[5])
823 extent[4] = refExtent[4];
824 extent[5] = refExtent[5];
827 for (int i = 0; i < maxBound; i = i+2)
829 //if we are totally outside the image
830 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] )
835 //crop to the limit of the image
836 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
837 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
838 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
839 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
842 for (int i = 0; i < maxBound; i = i+2)
844 extent[i] = refExtent[i];
845 extent[i+1] = refExtent[i];
849 void vvSlicer::UpdateOrientation()
851 // Set the camera position
852 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
855 switch (this->SliceOrientation)
857 case vtkImageViewer2::SLICE_ORIENTATION_XY:
858 cam->SetFocalPoint(0,0,0);
859 cam->SetPosition(0,0,-1); // -1 if medical ?
860 cam->SetViewUp(0,-1,0);
863 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
864 cam->SetFocalPoint(0,0,0);
865 cam->SetPosition(0,-1,0); // 1 if medical ?
866 cam->SetViewUp(0,0,1);
869 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
870 cam->SetFocalPoint(0,0,0);
871 cam->SetPosition(-1,0,0); // -1 if medical ?
872 cam->SetViewUp(0,0,1);
878 void vvSlicer::SetOpacity(double s)
880 this->GetImageActor()->SetOpacity(s);
883 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
885 this->Superclass::SetRenderWindow(rw);
886 this->SetupInteractor(rw->GetInteractor());
887 ca->SetImageActor(this->GetImageActor());
888 ca->SetWindowLevel(this->GetWindowLevel());
889 ca->SetText(2, "<slice>");
890 ca->SetText(3, "<window>\n<level>");
902 crossCursor->SetModelBounds(bounds);
903 this->GetRenderer()->AddActor(pdmA);
904 this->GetRenderer()->AddActor(ca);
905 this->GetRenderer()->ResetCamera();
907 //this is just a mapping between the labeling of the orientations presented to the user and
908 //the one used by vtk
909 SetSliceOrientation(2-(orientation%3));
913 void vvSlicer::ResetCamera()
915 if (this->GetInput())
917 double* input_bounds=this->GetInput()->GetBounds();
918 double bmax=input_bounds[1]-input_bounds[0];
919 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
920 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
921 this->GetRenderer()->ResetCamera();
922 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
926 void vvSlicer::SetDisplayMode(bool i)
928 this->GetImageActor()->SetVisibility(i);
929 this->GetAnnotation()->SetVisibility(i);
930 this->GetRenderer()->SetDraw(i);
932 mLandActor->SetVisibility(i);
933 pdmA->SetVisibility(i);
935 UpdateDisplayExtent();
937 //----------------------------------------------------------------------------
940 //----------------------------------------------------------------------------
941 void vvSlicer::FlipHorizontalView()
943 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
946 double *position = cam->GetPosition();
947 switch (this->SliceOrientation)
949 case vtkImageViewer2::SLICE_ORIENTATION_XY:
950 cam->SetPosition(position[0],position[1],-position[2]);
953 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
954 cam->SetPosition(position[0],-position[1],position[2]);
957 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
958 cam->SetPosition(-position[0],position[1],position[2]);
961 this->Renderer->ResetCameraClippingRange();
962 this->UpdateDisplayExtent();
965 //----------------------------------------------------------------------------
968 //----------------------------------------------------------------------------
969 void vvSlicer::FlipVerticalView()
971 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
974 FlipHorizontalView();
975 double *viewup = cam->GetViewUp();
976 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
977 this->UpdateDisplayExtent();
980 //----------------------------------------------------------------------------
983 //----------------------------------------------------------------------------
984 void vvSlicer::SetColorWindow(double window)
986 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
989 double level = this->GetWindowLevel()->GetLevel();
990 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
993 this->vtkImageViewer2::SetColorWindow(window);
995 //----------------------------------------------------------------------------
998 //----------------------------------------------------------------------------
999 void vvSlicer::SetColorLevel(double level)
1001 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1004 double window = this->GetWindowLevel()->GetWindow();
1005 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1008 this->vtkImageViewer2::SetColorLevel(level);
1010 //----------------------------------------------------------------------------
1013 //----------------------------------------------------------------------------
1014 void vvSlicer::Render()
1016 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion)
1018 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1019 legend->SetVisibility(1);
1021 else legend->SetVisibility(0);
1023 if (ca->GetVisibility())
1025 std::string worldPos = "";
1026 std::stringstream world1;
1027 std::stringstream world2;
1028 std::stringstream world3;
1029 world1 << (int)mCurrent[0];
1030 world2 << (int)mCurrent[1];
1031 world3 << (int)mCurrent[2];
1032 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1033 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1034 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1036 if (pdmA->GetVisibility())
1038 double x = mCursor[0];
1039 double y = mCursor[1];
1040 double z = mCursor[2];
1041 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1042 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1043 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1045 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1046 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1047 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1048 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1049 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1050 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1052 vtkRenderer * renderer = this->Renderer;
1054 renderer->WorldToView(x,y,z);
1055 renderer->ViewToNormalizedViewport(x,y,z);
1056 renderer->NormalizedViewportToViewport(x,y);
1057 renderer->ViewportToNormalizedDisplay(x,y);
1058 renderer->NormalizedDisplayToDisplay(x,y);
1059 crossCursor->SetFocalPoint(x,y,z);
1062 crossCursor->SetFocalPoint(-1,-1,z);
1065 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1066 X <= this->GetInput()->GetWholeExtent()[1] &&
1067 Y >= this->GetInput()->GetWholeExtent()[2] &&
1068 Y <= this->GetInput()->GetWholeExtent()[3] &&
1069 Z >= this->GetInput()->GetWholeExtent()[4] &&
1070 Z <= this->GetInput()->GetWholeExtent()[5])
1072 std::stringstream pixel1;
1073 std::stringstream pixel2;
1074 std::stringstream pixel3;
1075 std::stringstream temps;
1079 temps << mCurrentTSlice;
1080 double value = this->GetInput()->GetScalarComponentAsDouble(
1085 std::stringstream val;
1087 worldPos += "data value : " + val.str();
1088 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1089 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1091 ca->SetText(1,worldPos.c_str());
1093 if (mOverlay && mOverlayActor->GetVisibility())
1095 mOverlayMapper->SetWindow(this->GetColorWindow());
1096 mOverlayMapper->SetLevel(this->GetColorLevel());
1097 mOverlayMapper->Update();
1101 //this->Superclass::Render();
1102 this->GetRenderWindow()->Render();
1104 //----------------------------------------------------------------------------
1107 //----------------------------------------------------------------------------
1108 void vvSlicer::UpdateCursorPosition()
1110 if (this->GetImageActor()->GetVisibility())
1112 pdmA->SetVisibility(true);
1113 mCursor[0] = mCurrent[0];
1114 mCursor[1] = mCurrent[1];
1115 mCursor[2] = mCurrent[2];
1116 mCursor[3] = mCurrentTSlice;
1119 //----------------------------------------------------------------------------
1121 //----------------------------------------------------------------------------
1122 void vvSlicer::UpdateLandmarks()
1124 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1125 if (pd->GetPoints())
1127 mLandGlyph->SetRange(0,1);
1128 mLandGlyph->Modified();
1129 mLandGlyph->Update();
1131 mClipBox->Modified();
1132 mLandClipper->Update();
1133 mLandMapper->Update();
1137 //----------------------------------------------------------------------------
1140 //----------------------------------------------------------------------------
1141 void vvSlicer::SetSlice(int slice)
1143 int *range = this->GetSliceRange();
1146 if (slice < range[0])
1150 else if (slice > range[1])
1156 if (this->Slice == slice)
1161 this->Slice = slice;
1164 this->UpdateDisplayExtent();
1167 //----------------------------------------------------------------------------
1170 //----------------------------------------------------------------------------
1171 void vvSlicer::SetContourSlice()
1173 if (mSurfaceCutActors.size() > 0)
1174 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1175 i!=mSurfaceCutActors.end();i++)
1176 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1177 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1179 //----------------------------------------------------------------------------
1182 //----------------------------------------------------------------------------
1183 void vvSlicer::ForceUpdateDisplayExtent()
1185 this->UpdateDisplayExtent();
1187 //----------------------------------------------------------------------------
1190 //----------------------------------------------------------------------------
1191 int* vvSlicer::GetDisplayExtent()
1193 return this->GetImageActor()->GetDisplayExtent();
1195 //----------------------------------------------------------------------------
1198 //----------------------------------------------------------------------------
1199 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1201 this->Superclass::PrintSelf(os, indent);
1203 //----------------------------------------------------------------------------