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, "$Revision: 1.3 $");
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]);
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();
248 //stupid but necessary : the Overlay need to be render before fusion
251 this->GetRenderer()->RemoveActor(mFusionActor);
252 this->GetRenderer()->AddActor(mOverlayActor);
253 this->GetRenderer()->AddActor(mFusionActor);
256 this->GetRenderer()->AddActor(mOverlayActor);
259 SetTSlice(mCurrentTSlice);
263 void vvSlicer::SetFusion(vvImage::Pointer fusion)
265 if (fusion->GetVTKImages().size())
270 mFusionMapper = vtkImageMapToWindowLevelColors::New();
271 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
274 mFusionActor = vtkImageActor::New();
275 mFusionActor->SetInput(mFusionMapper->GetOutput());
276 mFusionActor->SetPickable(0);
277 mFusionActor->SetVisibility(false);
278 mFusionActor->SetOpacity(0.7);
279 this->UpdateDisplayExtent();
280 this->GetRenderer()->AddActor(mFusionActor);
283 SetTSlice(mCurrentTSlice);
287 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
289 if (actor_type == "vector")
291 this->mVFActor->SetVisibility(vis);
293 if (actor_type == "overlay")
295 this->mOverlayActor->SetVisibility(vis);
297 if (actor_type == "fusion")
299 this->mFusionActor->SetVisibility(vis);
301 if (actor_type == "contour")
302 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
303 UpdateDisplayExtent();
306 void vvSlicer::SetVF(vvImage::Pointer vf)
308 if (vf->GetVTKImages().size())
314 mAAFilter=vtkAssignAttribute::New();
315 mVOIFilter = vtkExtractVOI::New();
316 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
318 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
319 mAAFilter->SetInput(mVOIFilter->GetOutput());
320 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
321 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
324 mArrow = vvGlyphSource::New();
325 mArrow->SetGlyphTypeToSpecificArrow();
326 mArrow->SetScale(mScale);
329 // Glyph the gradient vector (with arrows)
331 mGlyphFilter = vvGlyph2D::New();
332 mGlyphFilter->SetInput(mAAFilter->GetOutput());
333 mGlyphFilter->SetSource(mArrow->GetOutput());
334 mGlyphFilter->ScalingOn();
335 mGlyphFilter->SetScaleModeToScaleByVector();
336 mGlyphFilter->OrientOn();
337 mGlyphFilter->SetVectorModeToUseVector();
338 mGlyphFilter->SetColorModeToColorByVector();
341 mVFMapper = vtkPolyDataMapper::New();
342 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
343 mVFMapper->SetInput(mGlyphFilter->GetOutput());
344 mVFMapper->ImmediateModeRenderingOn();
347 mVFActor = vtkActor::New();
348 mVFActor->SetMapper(mVFMapper);
349 mVFActor->SetPickable(0);
350 this->UpdateDisplayExtent();
351 this->GetRenderer()->AddActor(mVFActor);
354 SetTSlice(mCurrentTSlice);
358 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
360 mLandmarks = landmarks;
365 mCross = vtkCursor3D::New();
366 mCross->SetFocalPoint(0.0,0.0,0.0);
367 mCross->SetModelBounds(-10,10,-10,10,-10,10);
372 mLandGlyph = vtkGlyph3D::New();
373 mLandGlyph->SetSource(mCross->GetOutput());
374 mLandGlyph->SetInput(landmarks->GetOutput());
375 //mLandGlyph->SetIndexModeToScalar();
376 mLandGlyph->SetRange(0,1);
377 mLandGlyph->ScalingOff();
379 mLandGlyph->SetColorModeToColorByScalar();
382 mClipBox = vtkBox::New();
384 mLandClipper = vtkClipPolyData::New();
385 mLandClipper->InsideOutOn();
386 mLandClipper->SetInput(mLandGlyph->GetOutput());
387 mLandClipper->SetClipFunction(mClipBox);
390 mLandMapper = vtkPolyDataMapper::New();
391 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
392 //mLandMapper->ScalarVisibilityOff();
395 mLandActor = vtkActor::New();
396 mLandActor->SetMapper(mLandMapper);
397 mLandActor->GetProperty()->SetColor(255,10,212);
398 mLandActor->SetPickable(0);
399 mLandActor->SetVisibility(true);
400 this->UpdateDisplayExtent();
401 this->GetRenderer()->AddActor(mLandActor);
405 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
407 if (actor_type == "vector")
417 if (actor_type == "overlay")
419 Renderer->RemoveActor(mOverlayActor);
421 mOverlayActor = NULL;
422 mOverlayMapper = NULL;
424 if (actor_type == "fusion")
426 Renderer->RemoveActor(mFusionActor);
429 mFusionMapper = NULL;
431 if (actor_type == "contour")
433 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
434 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
438 void vvSlicer::SetVFSubSampling(int sub)
442 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
445 UpdateDisplayExtent();
449 void vvSlicer::SetVFScale(int scale)
453 mArrow->SetScale(mScale);
454 UpdateDisplayExtent();
458 void vvSlicer::SetVFLog(int log)
463 mGlyphFilter->SetUseLog(mVFLog);
464 mGlyphFilter->Modified();
466 UpdateDisplayExtent();
470 void vvSlicer::SetTSlice(int t)
474 else if ((unsigned int)t >= mImage->GetVTKImages().size())
475 t = mImage->GetVTKImages().size() -1;
477 this->SetInput(mImage->GetVTKImages()[t]);
478 if (mVF && mVFActor->GetVisibility())
480 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
481 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
483 if (mOverlay && mOverlayActor->GetVisibility())
485 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
486 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
488 if (mFusion && mFusionActor->GetVisibility())
490 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
491 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
493 if (mSurfaceCutActors.size() > 0)
494 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
495 i!=mSurfaceCutActors.end();i++)
496 (*i)->SetTimeSlice(mCurrentTSlice);
497 UpdateDisplayExtent();
500 int vvSlicer::GetTSlice()
502 return mCurrentTSlice;
505 void vvSlicer::SetSliceOrientation(int orientation)
507 //if 2D image, force to watch in Axial View
509 this->GetInput()->GetWholeExtent(extent);
510 if (extent[5]-extent[4] <= 2)
513 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
514 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
516 vtkErrorMacro("Error - invalid slice orientation " << orientation);
520 this->SliceOrientation = orientation;
523 int *range = this->GetSliceRange();
525 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
527 this->UpdateOrientation();
528 this->UpdateDisplayExtent();
530 if (this->Renderer && this->GetInput())
532 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
533 this->Renderer->ResetCamera();
534 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
540 //----------------------------------------------------------------------------
541 void vvSlicer::UpdateDisplayExtent()
543 vtkImageData *input = this->GetInput();
544 if (!input || !this->ImageActor)
548 input->UpdateInformation();
549 int *w_ext = input->GetWholeExtent();
551 switch (this->SliceOrientation)
553 case vtkImageViewer2::SLICE_ORIENTATION_XY:
554 this->ImageActor->SetDisplayExtent(
555 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
556 if (mVF && mVFActor->GetVisibility())
559 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
560 mVOIFilter->SetVOI(vfExtent);
561 mGlyphFilter->SetOrientation(1,1,0);
563 // put the vector field between the image and the camera
564 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
565 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
567 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
569 if (mOverlay && mOverlayActor->GetVisibility())
572 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
573 mOverlayActor->SetDisplayExtent(overExtent);
574 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
575 mOverlayActor->SetPosition(0,0,1);
577 mOverlayActor->SetPosition(0,0,-1);
579 if (mFusion && mFusionActor->GetVisibility())
582 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
583 mFusionActor->SetDisplayExtent(fusExtent);
584 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
585 mFusionActor->SetPosition(0,0,1.5);
587 mFusionActor->SetPosition(0,0,-1.5);
594 bounds[0] = ImageActor->GetBounds()[0];
595 bounds[1] = ImageActor->GetBounds()[1];
596 bounds[2] = ImageActor->GetBounds()[2];
597 bounds[3] = ImageActor->GetBounds()[3];
598 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
599 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
600 mClipBox->SetBounds(bounds);
603 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
604 mLandActor->SetPosition(0,0,1.5);
606 mLandActor->SetPosition(0,0,-1.5);
610 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
611 this->ImageActor->SetDisplayExtent(
612 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
613 if (mVF && mVFActor->GetVisibility())
616 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
617 mVOIFilter->SetVOI(vfExtent);
618 mGlyphFilter->SetOrientation(1,0,1);
620 // put the vector field between the image aSpacingnd the camera
621 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
622 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
624 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
626 if (mOverlay && mOverlayActor->GetVisibility())
629 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
630 mOverlayActor->SetDisplayExtent(overExtent);
631 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
632 mOverlayActor->SetPosition(0,1,0);
634 mOverlayActor->SetPosition(0,-1,0);
636 if (mFusion && mFusionActor->GetVisibility())
639 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
640 mFusionActor->SetDisplayExtent(fusExtent);
641 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
642 mFusionActor->SetPosition(0,1.5,0);
644 mFusionActor->SetPosition(0,-1.5,0);
651 bounds[0] = ImageActor->GetBounds()[0];
652 bounds[1] = ImageActor->GetBounds()[1];
653 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
654 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
655 bounds[4] = ImageActor->GetBounds()[4];
656 bounds[5] = ImageActor->GetBounds()[5];
657 mClipBox->SetBounds(bounds);
660 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
661 mLandActor->SetPosition(0,1.5,0);
663 mLandActor->SetPosition(0,-1.5,0);
667 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
668 this->ImageActor->SetDisplayExtent(
669 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
670 if (mVF && mVFActor->GetVisibility())
673 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
674 mVOIFilter->SetVOI(vfExtent);
675 mGlyphFilter->SetOrientation(0,1,1);
677 // put the vector field between the image and the camera
678 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
679 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
681 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
683 if (mOverlay && mOverlayActor->GetVisibility())
686 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
687 mOverlayActor->SetDisplayExtent(overExtent);
688 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
689 mOverlayActor->SetPosition(1,0,0);
691 mOverlayActor->SetPosition(-1,0,0);
693 if (mFusion && mFusionActor->GetVisibility())
696 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
697 mFusionActor->SetDisplayExtent(fusExtent);
698 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
699 mFusionActor->SetPosition(1.5,0,0);
701 mFusionActor->SetPosition(-1.5,0,0);
708 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
709 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
710 bounds[2] = ImageActor->GetBounds()[2];
711 bounds[3] = ImageActor->GetBounds()[3];
712 bounds[4] = ImageActor->GetBounds()[4];
713 bounds[5] = ImageActor->GetBounds()[5];
714 mClipBox->SetBounds(bounds);
717 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
718 mLandActor->SetPosition(1.5,0,0);
720 mLandActor->SetPosition(-1.5,0,0);
725 // Figure out the correct clipping range
729 if (this->InteractorStyle &&
730 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
732 this->Renderer->ResetCameraClippingRange();
736 vtkCamera *cam = this->Renderer->GetActiveCamera();
740 this->ImageActor->GetBounds(bounds);
741 double spos = (double)bounds[this->SliceOrientation * 2];
742 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
743 double range = fabs(spos - cpos);
744 double *spacing = input->GetSpacing();
746 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
747 cam->SetClippingRange(
748 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
754 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
756 vtkImageData* image=this->GetInput();
757 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
758 mVF->GetSpacing()[0];
759 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
760 mVF->GetSpacing()[0];
761 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
762 mVF->GetSpacing()[1];
763 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
764 mVF->GetSpacing()[1];
765 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
766 mVF->GetSpacing()[2];
767 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
768 mVF->GetSpacing()[2];
770 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
773 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
775 vtkImageData* image=this->GetInput();
776 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
777 mOverlay->GetSpacing()[0];
778 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
779 mOverlay->GetSpacing()[0];
780 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
781 mOverlay->GetSpacing()[1];
782 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
783 mOverlay->GetSpacing()[1];
784 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
785 mOverlay->GetSpacing()[2];
786 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
787 mOverlay->GetSpacing()[2];
788 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
791 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
793 vtkImageData* image=this->GetInput();
794 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
795 mFusion->GetSpacing()[0];
796 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
797 mFusion->GetSpacing()[0];
798 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
799 mFusion->GetSpacing()[1];
800 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
801 mFusion->GetSpacing()[1];
802 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
803 mFusion->GetSpacing()[2];
804 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
805 mFusion->GetSpacing()[2];
806 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
809 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
814 //2D overlay on 3D image specific case
815 if (refExtent[4] == refExtent[5])
818 extent[4] = refExtent[4];
819 extent[5] = refExtent[5];
822 for (int i = 0; i < maxBound; i = i+2)
824 //if we are totally outside the image
825 if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
830 //crop to the limit of the image
831 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
832 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
833 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
834 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
837 for (int i = 0; i < maxBound; i = i+2)
839 extent[i] = refExtent[i];
840 extent[i+1] = refExtent[i];
844 void vvSlicer::UpdateOrientation()
846 // Set the camera position
847 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
850 switch (this->SliceOrientation)
852 case vtkImageViewer2::SLICE_ORIENTATION_XY:
853 cam->SetFocalPoint(0,0,0);
854 cam->SetPosition(0,0,-1); // -1 if medical ?
855 cam->SetViewUp(0,-1,0);
858 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
859 cam->SetFocalPoint(0,0,0);
860 cam->SetPosition(0,-1,0); // 1 if medical ?
861 cam->SetViewUp(0,0,1);
864 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
865 cam->SetFocalPoint(0,0,0);
866 cam->SetPosition(-1,0,0); // -1 if medical ?
867 cam->SetViewUp(0,0,1);
873 void vvSlicer::SetOpacity(double s)
875 this->GetImageActor()->SetOpacity(s);
878 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
880 this->Superclass::SetRenderWindow(rw);
881 this->SetupInteractor(rw->GetInteractor());
882 ca->SetImageActor(this->GetImageActor());
883 ca->SetWindowLevel(this->GetWindowLevel());
884 ca->SetText(2, "<slice>");
885 ca->SetText(3, "<window>\n<level>");
897 crossCursor->SetModelBounds(bounds);
898 this->GetRenderer()->AddActor(pdmA);
899 this->GetRenderer()->AddActor(ca);
900 this->GetRenderer()->ResetCamera();
902 //this is just a mapping between the labeling of the orientations presented to the user and
903 //the one used by vtk
904 SetSliceOrientation(2-(orientation%3));
908 void vvSlicer::ResetCamera()
910 if (this->GetInput())
912 double* input_bounds=this->GetInput()->GetBounds();
913 double bmax=input_bounds[1]-input_bounds[0];
914 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
915 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
916 this->GetRenderer()->ResetCamera();
917 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
921 void vvSlicer::SetDisplayMode(bool i)
923 this->GetImageActor()->SetVisibility(i);
924 this->GetAnnotation()->SetVisibility(i);
925 this->GetRenderer()->SetDraw(i);
927 mLandActor->SetVisibility(i);
928 pdmA->SetVisibility(i);
930 UpdateDisplayExtent();
933 void vvSlicer::FlipHorizontalView()
935 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
938 double *position = cam->GetPosition();
939 switch (this->SliceOrientation)
941 case vtkImageViewer2::SLICE_ORIENTATION_XY:
942 cam->SetPosition(position[0],position[1],-position[2]);
945 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
946 cam->SetPosition(position[0],-position[1],position[2]);
949 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
950 cam->SetPosition(-position[0],position[1],position[2]);
953 this->Renderer->ResetCameraClippingRange();
954 this->UpdateDisplayExtent();
958 void vvSlicer::FlipVerticalView()
960 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
963 FlipHorizontalView();
964 double *viewup = cam->GetViewUp();
965 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
966 this->UpdateDisplayExtent();
970 void vvSlicer::Render()
972 if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
975 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
976 legend->SetVisibility(1);
978 else legend->SetVisibility(0);
980 if (ca->GetVisibility())
982 std::string worldPos = "";
983 std::stringstream world1;
984 std::stringstream world2;
985 std::stringstream world3;
986 world1 << (int)mCurrent[0];
987 world2 << (int)mCurrent[1];
988 world3 << (int)mCurrent[2];
989 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
990 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
991 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
993 if (pdmA->GetVisibility())
995 double x = mCursor[0];
996 double y = mCursor[1];
997 double z = mCursor[2];
998 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
999 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1000 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1002 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1003 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1004 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1005 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1006 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1007 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1009 vtkRenderer * renderer = this->Renderer;
1011 renderer->WorldToView(x,y,z);
1012 renderer->ViewToNormalizedViewport(x,y,z);
1013 renderer->NormalizedViewportToViewport(x,y);
1014 renderer->ViewportToNormalizedDisplay(x,y);
1015 renderer->NormalizedDisplayToDisplay(x,y);
1016 crossCursor->SetFocalPoint(x,y,z);
1019 crossCursor->SetFocalPoint(-1,-1,z);
1022 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1023 X <= this->GetInput()->GetWholeExtent()[1] &&
1024 Y >= this->GetInput()->GetWholeExtent()[2] &&
1025 Y <= this->GetInput()->GetWholeExtent()[3] &&
1026 Z >= this->GetInput()->GetWholeExtent()[4] &&
1027 Z <= this->GetInput()->GetWholeExtent()[5])
1029 std::stringstream pixel1;
1030 std::stringstream pixel2;
1031 std::stringstream pixel3;
1032 std::stringstream temps;
1036 temps << mCurrentTSlice;
1037 double value = this->GetInput()->GetScalarComponentAsDouble(
1042 std::stringstream val;
1044 worldPos += "data value : " + val.str();
1045 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1046 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1048 ca->SetText(1,worldPos.c_str());
1050 if (mOverlay && mOverlayActor->GetVisibility())
1052 mOverlayMapper->SetWindow(this->GetColorWindow());
1053 mOverlayMapper->SetLevel(this->GetColorLevel());
1054 mOverlayMapper->Update();
1058 //this->Superclass::Render();
1059 this->GetRenderWindow()->Render();
1062 void vvSlicer::UpdateCursorPosition()
1064 if (this->GetImageActor()->GetVisibility())
1066 pdmA->SetVisibility(true);
1067 mCursor[0] = mCurrent[0];
1068 mCursor[1] = mCurrent[1];
1069 mCursor[2] = mCurrent[2];
1070 mCursor[3] = mCurrentTSlice;
1074 void vvSlicer::UpdateLandmarks()
1076 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1077 if (pd->GetPoints())
1079 mLandGlyph->SetRange(0,1);
1080 mLandGlyph->Modified();
1081 mLandGlyph->Update();
1083 mClipBox->Modified();
1084 mLandClipper->Update();
1085 mLandMapper->Update();
1090 //----------------------------------------------------------------------------
1091 void vvSlicer::SetSlice(int slice)
1093 int *range = this->GetSliceRange();
1096 if (slice < range[0])
1100 else if (slice > range[1])
1106 if (this->Slice == slice)
1111 this->Slice = slice;
1114 this->UpdateDisplayExtent();
1118 void vvSlicer::SetContourSlice()
1120 if (mSurfaceCutActors.size() > 0)
1121 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1122 i!=mSurfaceCutActors.end();i++)
1123 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1124 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1127 void vvSlicer::ForceUpdateDisplayExtent()
1129 this->UpdateDisplayExtent();
1132 int* vvSlicer::GetDisplayExtent()
1134 return this->GetImageActor()->GetDisplayExtent();
1137 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1139 this->Superclass::PrintSelf(os, indent);