1 /*=========================================================================
4 Module: $RCSfile: vvSlicer.cxx,v $
6 Date: $Date: 2010/01/26 15:04:33 $
7 Version: $Revision: 1.2 $
8 Author : Pierre Seroul (pierre.seroul@gmail.com)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
31 #include "vvSlicerManagerCommand.h"
32 #include "vvGlyphSource.h"
33 #include "vvGlyph2D.h"
34 #include "vvImageMapToWLColors.h"
36 #include <vtkTextProperty.h>
37 #include <vtkTextActor.h>
38 #include <vtkTextSource.h>
39 #include <vtkActor2D.h>
40 #include <vtkCursor2D.h>
41 #include <vtkPolyDataMapper2D.h>
42 #include <vtkProperty2D.h>
43 #include <vtkCornerAnnotation.h>
44 #include <vtkImageMapToWindowLevelColors.h>
45 #include <vtkImageData.h>
46 #include <vtkImageActor.h>
47 #include <vtkToolkits.h>
48 #include <vtkObjectFactory.h>
49 #include <vtkPointData.h>
50 #include <vtkDataArray.h>
51 #include <vtkFloatArray.h>
52 #include <vtkClipPolyData.h>
53 #include <vtkGlyph3D.h>
55 #include <vtkCursor3D.h>
56 #include <vtkProperty.h>
58 #include <vtkLightCollection.h>
59 #include <vtkScalarBarActor.h>
60 #include <vtkLookupTable.h>
62 #include <vtkRenderer.h>
63 #include <vtkRendererCollection.h>
64 #include <vtkRenderWindow.h>
65 #include <vtkRenderWindowInteractor.h>
66 #include <vtkCamera.h>
67 #include <vtkCallbackCommand.h>
68 #include <vtkCommand.h>
69 #include <vtkPolyDataMapper.h>
72 #include <vtkExtractVOI.h>
73 #include <vtkSphereSource.h>
74 #include <vtkCutter.h>
76 #include <vtkAssignAttribute.h>
78 vtkCxxRevisionMacro(vvSlicer, "$Revision: 1.2 $");
79 vtkStandardNewMacro(vvSlicer);
86 mCurrent[0] = -VTK_DOUBLE_MAX;
87 mCurrent[1] = -VTK_DOUBLE_MAX;
88 mCurrent[2] = -VTK_DOUBLE_MAX;
90 mCursor[0] = -VTK_DOUBLE_MAX;
91 mCursor[1] = -VTK_DOUBLE_MAX;
92 mCursor[2] = -VTK_DOUBLE_MAX;
93 mCursor[3] = -VTK_DOUBLE_MAX;
99 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
100 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
101 text += "0,1,2,3,4,5 : preset windowing\n";
102 text += "6,7,8,9 : preset colormap\n";
103 text += "r : reset view\n";
104 text += "l : reload image\n";
105 text += "f : fly to mouse position\n";
106 text += "g : go to cross hair position\n\n";
107 text += "Up,down : change slice\n";
108 text += "Left,right : change tenporal slice\n\n";
109 text += "Scrollbar (or w/x) : zoom in/out\n";
110 text += "left button : synchronize all views\n";
111 text += "middle button : grab image\n";
112 text += "right button : change windowing\n";
114 crossCursor = vtkCursor2D::New();
115 crossCursor->AllOff();
116 crossCursor->AxesOn();
117 crossCursor->SetTranslationMode(1);
118 crossCursor->SetRadius(2);
120 pdm = vtkPolyDataMapper2D::New();
121 pdm->SetInput(crossCursor->GetOutput());
123 pdmA = vtkActor2D::New();
124 pdmA->SetMapper(pdm);
125 pdmA->GetProperty()->SetColor(255,10,212);
126 pdmA->SetVisibility(0);
127 pdmA->SetPickable(0);
129 ca = vtkCornerAnnotation::New();
130 ca->GetTextProperty()->SetColor(255,10,212);
131 ca->SetVisibility(1);
139 legend = vtkSmartPointer<vtkScalarBarActor>::New();
140 //legend->SetTitle("test!");
141 legend->SetPosition(0.82,0.18);
142 legend->SetWidth(0.1);
143 legend->SetVisibility(0);
144 legend->SetLabelFormat("%.1f");
145 this->GetRenderer()->AddActor(legend);
147 this->WindowLevel->Delete();
148 this->WindowLevel = vvImageMapToWLColors::New();
149 this->InstallPipeline();
152 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper() {
153 return mOverlayMapper.GetPointer();
155 vtkImageActor* vvSlicer::GetOverlayActor() {
156 return mOverlayActor.GetPointer();
158 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper() {
159 return mFusionMapper.GetPointer();
162 vtkImageActor* vvSlicer::GetFusionActor() {
163 return mFusionActor.GetPointer();
165 vtkActor* vvSlicer::GetVFActor() {
166 return mVFActor.GetPointer();
168 vtkCornerAnnotation* vvSlicer::GetAnnotation() {
169 return ca.GetPointer();
172 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
175 mSurfaceCutActors.push_back(new vvMeshActor());
177 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
179 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
180 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
181 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
186 void vvSlicer::ToggleContourSuperposition()
188 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
189 i!=mSurfaceCutActors.end();i++)
190 (*i)->ToggleSuperposition();
193 void vvSlicer::SetCursorColor(int r,int g, int b)
195 pdmA->GetProperty()->SetColor(r,g,b);
197 void vvSlicer::SetCursorVisibility(bool s)
199 pdmA->SetVisibility(s);
201 bool vvSlicer::GetCursorVisibility()
203 return pdmA->GetVisibility();
206 vvSlicer::~vvSlicer()
208 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
209 i!=mSurfaceCutActors.end();i++)
213 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
221 void vvSlicer::SetImage(vvImage::Pointer image)
223 if (image->GetVTKImages().size())
226 this->Superclass::SetInput(image->GetVTKImages()[0]);
227 this->UpdateDisplayExtent();
229 ca->SetText(0,mFileName.c_str());
233 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
235 if (overlay->GetVTKImages().size())
240 mOverlayMapper = vtkImageMapToWindowLevelColors::New();
241 mOverlayMapper->SetInput(overlay->GetVTKImages()[0]);
244 mOverlayActor = vtkImageActor::New();
245 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
246 mOverlayActor->SetPickable(0);
247 mOverlayActor->SetVisibility(false);
248 mOverlayActor->SetOpacity(0.5);
249 this->UpdateDisplayExtent();
251 //stupid but necessary : the Overlay need to be render before fusion
254 this->GetRenderer()->RemoveActor(mFusionActor);
255 this->GetRenderer()->AddActor(mOverlayActor);
256 this->GetRenderer()->AddActor(mFusionActor);
259 this->GetRenderer()->AddActor(mOverlayActor);
262 SetTSlice(mCurrentTSlice);
266 void vvSlicer::SetFusion(vvImage::Pointer fusion)
268 if (fusion->GetVTKImages().size())
273 mFusionMapper = vtkImageMapToWindowLevelColors::New();
274 mFusionMapper->SetInput(fusion->GetVTKImages()[0]);
277 mFusionActor = vtkImageActor::New();
278 mFusionActor->SetInput(mFusionMapper->GetOutput());
279 mFusionActor->SetPickable(0);
280 mFusionActor->SetVisibility(false);
281 mFusionActor->SetOpacity(0.7);
282 this->UpdateDisplayExtent();
283 this->GetRenderer()->AddActor(mFusionActor);
286 SetTSlice(mCurrentTSlice);
290 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
292 if (actor_type == "vector")
294 this->mVFActor->SetVisibility(vis);
296 if (actor_type == "overlay")
298 this->mOverlayActor->SetVisibility(vis);
300 if (actor_type == "fusion")
302 this->mFusionActor->SetVisibility(vis);
304 if (actor_type == "contour")
305 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
306 UpdateDisplayExtent();
309 void vvSlicer::SetVF(vvImage::Pointer vf)
311 if (vf->GetVTKImages().size())
317 mAAFilter=vtkAssignAttribute::New();
318 mVOIFilter = vtkExtractVOI::New();
319 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
321 mVOIFilter->SetInput(vf->GetVTKImages()[0]);
322 mAAFilter->SetInput(mVOIFilter->GetOutput());
323 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
324 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
327 mArrow = vvGlyphSource::New();
328 mArrow->SetGlyphTypeToSpecificArrow();
329 mArrow->SetScale(mScale);
332 // Glyph the gradient vector (with arrows)
334 mGlyphFilter = vvGlyph2D::New();
335 mGlyphFilter->SetInput(mAAFilter->GetOutput());
336 mGlyphFilter->SetSource(mArrow->GetOutput());
337 mGlyphFilter->ScalingOn();
338 mGlyphFilter->SetScaleModeToScaleByVector();
339 mGlyphFilter->OrientOn();
340 mGlyphFilter->SetVectorModeToUseVector();
341 mGlyphFilter->SetColorModeToColorByVector();
344 mVFMapper = vtkPolyDataMapper::New();
345 //mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
346 mVFMapper->SetInput(mGlyphFilter->GetOutput());
347 mVFMapper->ImmediateModeRenderingOn();
350 mVFActor = vtkActor::New();
351 mVFActor->SetMapper(mVFMapper);
352 mVFActor->SetPickable(0);
353 this->UpdateDisplayExtent();
354 this->GetRenderer()->AddActor(mVFActor);
357 SetTSlice(mCurrentTSlice);
361 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
363 mLandmarks = landmarks;
368 mCross = vtkCursor3D::New();
369 mCross->SetFocalPoint(0.0,0.0,0.0);
370 mCross->SetModelBounds(-10,10,-10,10,-10,10);
375 mLandGlyph = vtkGlyph3D::New();
376 mLandGlyph->SetSource(mCross->GetOutput());
377 mLandGlyph->SetInput(landmarks->GetOutput());
378 //mLandGlyph->SetIndexModeToScalar();
379 mLandGlyph->SetRange(0,1);
380 mLandGlyph->ScalingOff();
382 mLandGlyph->SetColorModeToColorByScalar();
385 mClipBox = vtkBox::New();
387 mLandClipper = vtkClipPolyData::New();
388 mLandClipper->InsideOutOn();
389 mLandClipper->SetInput(mLandGlyph->GetOutput());
390 mLandClipper->SetClipFunction(mClipBox);
393 mLandMapper = vtkPolyDataMapper::New();
394 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
395 //mLandMapper->ScalarVisibilityOff();
398 mLandActor = vtkActor::New();
399 mLandActor->SetMapper(mLandMapper);
400 mLandActor->GetProperty()->SetColor(255,10,212);
401 mLandActor->SetPickable(0);
402 mLandActor->SetVisibility(true);
403 this->UpdateDisplayExtent();
404 this->GetRenderer()->AddActor(mLandActor);
408 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
410 if (actor_type == "vector")
420 if (actor_type == "overlay")
422 Renderer->RemoveActor(mOverlayActor);
424 mOverlayActor = NULL;
425 mOverlayMapper = NULL;
427 if (actor_type == "fusion")
429 Renderer->RemoveActor(mFusionActor);
432 mFusionMapper = NULL;
434 if (actor_type == "contour")
436 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
437 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
441 void vvSlicer::SetVFSubSampling(int sub)
445 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
448 UpdateDisplayExtent();
452 void vvSlicer::SetVFScale(int scale)
456 mArrow->SetScale(mScale);
457 UpdateDisplayExtent();
461 void vvSlicer::SetVFLog(int log)
466 mGlyphFilter->SetUseLog(mVFLog);
467 mGlyphFilter->Modified();
469 UpdateDisplayExtent();
473 void vvSlicer::SetTSlice(int t)
477 else if ((unsigned int)t >= mImage->GetVTKImages().size())
478 t = mImage->GetVTKImages().size() -1;
480 this->SetInput(mImage->GetVTKImages()[t]);
481 if (mVF && mVFActor->GetVisibility())
483 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
484 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
486 if (mOverlay && mOverlayActor->GetVisibility())
488 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
489 mOverlayMapper->SetInput(mOverlay->GetVTKImages()[mCurrentTSlice]);
491 if (mFusion && mFusionActor->GetVisibility())
493 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
494 mFusionMapper->SetInput(mFusion->GetVTKImages()[mCurrentTSlice]);
496 if (mSurfaceCutActors.size() > 0)
497 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
498 i!=mSurfaceCutActors.end();i++)
499 (*i)->SetTimeSlice(mCurrentTSlice);
500 UpdateDisplayExtent();
503 int vvSlicer::GetTSlice()
505 return mCurrentTSlice;
508 void vvSlicer::SetSliceOrientation(int orientation)
510 //if 2D image, force to watch in Axial View
512 this->GetInput()->GetWholeExtent(extent);
513 if (extent[5]-extent[4] <= 2)
516 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
517 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY)
519 vtkErrorMacro("Error - invalid slice orientation " << orientation);
523 this->SliceOrientation = orientation;
526 int *range = this->GetSliceRange();
528 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
530 this->UpdateOrientation();
531 this->UpdateDisplayExtent();
533 if (this->Renderer && this->GetInput())
535 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
536 this->Renderer->ResetCamera();
537 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
543 //----------------------------------------------------------------------------
544 void vvSlicer::UpdateDisplayExtent()
546 vtkImageData *input = this->GetInput();
547 if (!input || !this->ImageActor)
551 input->UpdateInformation();
552 int *w_ext = input->GetWholeExtent();
554 switch (this->SliceOrientation)
556 case vtkImageViewer2::SLICE_ORIENTATION_XY:
557 this->ImageActor->SetDisplayExtent(
558 w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice);
559 if (mVF && mVFActor->GetVisibility())
562 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,vfExtent);
563 mVOIFilter->SetVOI(vfExtent);
564 mGlyphFilter->SetOrientation(1,1,0);
566 // put the vector field between the image and the camera
567 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
568 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[5]+2);
570 mVFActor->SetPosition(0,0,ImageActor->GetBounds()[4]-2);
572 if (mOverlay && mOverlayActor->GetVisibility())
575 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,overExtent);
576 mOverlayActor->SetDisplayExtent(overExtent);
577 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
578 mOverlayActor->SetPosition(0,0,1);
580 mOverlayActor->SetPosition(0,0,-1);
582 if (mFusion && mFusionActor->GetVisibility())
585 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], this->Slice, this->Slice,fusExtent);
586 mFusionActor->SetDisplayExtent(fusExtent);
587 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
588 mFusionActor->SetPosition(0,0,1.5);
590 mFusionActor->SetPosition(0,0,-1.5);
597 bounds[0] = ImageActor->GetBounds()[0];
598 bounds[1] = ImageActor->GetBounds()[1];
599 bounds[2] = ImageActor->GetBounds()[2];
600 bounds[3] = ImageActor->GetBounds()[3];
601 bounds[4] = ImageActor->GetBounds()[4]-(0.9/this->GetInput()->GetSpacing()[2]);
602 bounds[5] = ImageActor->GetBounds()[5]+(0.9/this->GetInput()->GetSpacing()[2]);
603 mClipBox->SetBounds(bounds);
606 if (Renderer->GetActiveCamera()->GetPosition()[2] > this->Slice)
607 mLandActor->SetPosition(0,0,1.5);
609 mLandActor->SetPosition(0,0,-1.5);
613 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
614 this->ImageActor->SetDisplayExtent(
615 w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5]);
616 if (mVF && mVFActor->GetVisibility())
619 ComputeVFDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],vfExtent);
620 mVOIFilter->SetVOI(vfExtent);
621 mGlyphFilter->SetOrientation(1,0,1);
623 // put the vector field between the image aSpacingnd the camera
624 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
625 mVFActor->SetPosition(0,ImageActor->GetBounds()[3]+2,0);
627 mVFActor->SetPosition(0,ImageActor->GetBounds()[2]-2,0);
629 if (mOverlay && mOverlayActor->GetVisibility())
632 ComputeOverlayDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],overExtent);
633 mOverlayActor->SetDisplayExtent(overExtent);
634 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
635 mOverlayActor->SetPosition(0,1,0);
637 mOverlayActor->SetPosition(0,-1,0);
639 if (mFusion && mFusionActor->GetVisibility())
642 ComputeFusionDisplayedExtent(w_ext[0], w_ext[1], this->Slice, this->Slice, w_ext[4], w_ext[5],fusExtent);
643 mFusionActor->SetDisplayExtent(fusExtent);
644 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
645 mFusionActor->SetPosition(0,1.5,0);
647 mFusionActor->SetPosition(0,-1.5,0);
654 bounds[0] = ImageActor->GetBounds()[0];
655 bounds[1] = ImageActor->GetBounds()[1];
656 bounds[2] = ImageActor->GetBounds()[2]-(0.5/this->GetInput()->GetSpacing()[1]);
657 bounds[3] = ImageActor->GetBounds()[3]+(0.5/this->GetInput()->GetSpacing()[1]);
658 bounds[4] = ImageActor->GetBounds()[4];
659 bounds[5] = ImageActor->GetBounds()[5];
660 mClipBox->SetBounds(bounds);
663 if (Renderer->GetActiveCamera()->GetPosition()[1] > this->Slice)
664 mLandActor->SetPosition(0,1.5,0);
666 mLandActor->SetPosition(0,-1.5,0);
670 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
671 this->ImageActor->SetDisplayExtent(
672 this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
673 if (mVF && mVFActor->GetVisibility())
676 ComputeVFDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],vfExtent);
677 mVOIFilter->SetVOI(vfExtent);
678 mGlyphFilter->SetOrientation(0,1,1);
680 // put the vector field between the image and the camera
681 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
682 mVFActor->SetPosition(ImageActor->GetBounds()[1]+2,0,0);
684 mVFActor->SetPosition(ImageActor->GetBounds()[0]-2,0,0);
686 if (mOverlay && mOverlayActor->GetVisibility())
689 ComputeOverlayDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],overExtent);
690 mOverlayActor->SetDisplayExtent(overExtent);
691 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
692 mOverlayActor->SetPosition(1,0,0);
694 mOverlayActor->SetPosition(-1,0,0);
696 if (mFusion && mFusionActor->GetVisibility())
699 ComputeFusionDisplayedExtent(this->Slice, this->Slice, w_ext[2], w_ext[3], w_ext[4], w_ext[5],fusExtent);
700 mFusionActor->SetDisplayExtent(fusExtent);
701 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
702 mFusionActor->SetPosition(1.5,0,0);
704 mFusionActor->SetPosition(-1.5,0,0);
711 bounds[0] = ImageActor->GetBounds()[0]-(0.5/this->GetInput()->GetSpacing()[0]);
712 bounds[1] = ImageActor->GetBounds()[1]+(0.5/this->GetInput()->GetSpacing()[0]);
713 bounds[2] = ImageActor->GetBounds()[2];
714 bounds[3] = ImageActor->GetBounds()[3];
715 bounds[4] = ImageActor->GetBounds()[4];
716 bounds[5] = ImageActor->GetBounds()[5];
717 mClipBox->SetBounds(bounds);
720 if (Renderer->GetActiveCamera()->GetPosition()[0] > this->Slice)
721 mLandActor->SetPosition(1.5,0,0);
723 mLandActor->SetPosition(-1.5,0,0);
728 // Figure out the correct clipping range
732 if (this->InteractorStyle &&
733 this->InteractorStyle->GetAutoAdjustCameraClippingRange())
735 this->Renderer->ResetCameraClippingRange();
739 vtkCamera *cam = this->Renderer->GetActiveCamera();
743 this->ImageActor->GetBounds(bounds);
744 double spos = (double)bounds[this->SliceOrientation * 2];
745 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
746 double range = fabs(spos - cpos);
747 double *spacing = input->GetSpacing();
749 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
750 cam->SetClippingRange(
751 range - avg_spacing * 3.0, range + avg_spacing * 3.0);
757 void vvSlicer::ComputeVFDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int vfExtent[6])
759 vtkImageData* image=this->GetInput();
760 vfExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
761 mVF->GetSpacing()[0];
762 vfExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mVF->GetOrigin()[0]) /
763 mVF->GetSpacing()[0];
764 vfExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
765 mVF->GetSpacing()[1];
766 vfExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mVF->GetOrigin()[1]) /
767 mVF->GetSpacing()[1];
768 vfExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
769 mVF->GetSpacing()[2];
770 vfExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mVF->GetOrigin()[2]) /
771 mVF->GetSpacing()[2];
773 ClipDisplayedExtent(vfExtent,mVOIFilter->GetInput()->GetWholeExtent());
776 void vvSlicer::ComputeOverlayDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int overExtent[6])
778 vtkImageData* image=this->GetInput();
779 overExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
780 mOverlay->GetSpacing()[0];
781 overExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mOverlay->GetOrigin()[0]) /
782 mOverlay->GetSpacing()[0];
783 overExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
784 mOverlay->GetSpacing()[1];
785 overExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mOverlay->GetOrigin()[1]) /
786 mOverlay->GetSpacing()[1];
787 overExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
788 mOverlay->GetSpacing()[2];
789 overExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mOverlay->GetOrigin()[2]) /
790 mOverlay->GetSpacing()[2];
791 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
794 void vvSlicer::ComputeFusionDisplayedExtent(int x1,int x2,int y1,int y2,int z1,int z2,int fusExtent[6])
796 vtkImageData* image=this->GetInput();
797 fusExtent[0] = (( image->GetOrigin()[0] + x1*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
798 mFusion->GetSpacing()[0];
799 fusExtent[1] = (( image->GetOrigin()[0] + x2*image->GetSpacing()[0] ) - mFusion->GetOrigin()[0]) /
800 mFusion->GetSpacing()[0];
801 fusExtent[2] = (( image->GetOrigin()[1] + y1*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
802 mFusion->GetSpacing()[1];
803 fusExtent[3] = (( image->GetOrigin()[1] + y2*image->GetSpacing()[1] ) - mFusion->GetOrigin()[1]) /
804 mFusion->GetSpacing()[1];
805 fusExtent[4] = (( image->GetOrigin()[2] + z1*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
806 mFusion->GetSpacing()[2];
807 fusExtent[5] = (( image->GetOrigin()[2] + z2*image->GetSpacing()[2] ) - mFusion->GetOrigin()[2]) /
808 mFusion->GetSpacing()[2];
809 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
812 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
817 //2D overlay on 3D image specific case
818 if (refExtent[4] == refExtent[5])
821 extent[4] = refExtent[4];
822 extent[5] = refExtent[5];
825 for (int i = 0; i < maxBound; i = i+2)
827 //if we are totally outside the image
828 if ( extent[i] > refExtent[i+1] or extent[i+1] < refExtent[i] )
833 //crop to the limit of the image
834 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
835 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
836 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
837 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
840 for (int i = 0; i < maxBound; i = i+2)
842 extent[i] = refExtent[i];
843 extent[i+1] = refExtent[i];
847 void vvSlicer::UpdateOrientation()
849 // Set the camera position
850 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
853 switch (this->SliceOrientation)
855 case vtkImageViewer2::SLICE_ORIENTATION_XY:
856 cam->SetFocalPoint(0,0,0);
857 cam->SetPosition(0,0,-1); // -1 if medical ?
858 cam->SetViewUp(0,-1,0);
861 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
862 cam->SetFocalPoint(0,0,0);
863 cam->SetPosition(0,-1,0); // 1 if medical ?
864 cam->SetViewUp(0,0,1);
867 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
868 cam->SetFocalPoint(0,0,0);
869 cam->SetPosition(-1,0,0); // -1 if medical ?
870 cam->SetViewUp(0,0,1);
876 void vvSlicer::SetOpacity(double s)
878 this->GetImageActor()->SetOpacity(s);
881 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
883 this->Superclass::SetRenderWindow(rw);
884 this->SetupInteractor(rw->GetInteractor());
885 ca->SetImageActor(this->GetImageActor());
886 ca->SetWindowLevel(this->GetWindowLevel());
887 ca->SetText(2, "<slice>");
888 ca->SetText(3, "<window>\n<level>");
900 crossCursor->SetModelBounds(bounds);
901 this->GetRenderer()->AddActor(pdmA);
902 this->GetRenderer()->AddActor(ca);
903 this->GetRenderer()->ResetCamera();
905 //this is just a mapping between the labeling of the orientations presented to the user and
906 //the one used by vtk
907 SetSliceOrientation(2-(orientation%3));
911 void vvSlicer::ResetCamera()
913 if (this->GetInput())
915 double* input_bounds=this->GetInput()->GetBounds();
916 double bmax=input_bounds[1]-input_bounds[0];
917 if (bmax < input_bounds[3]-input_bounds[2]) bmax=input_bounds[3]-input_bounds[2];
918 if (bmax < input_bounds[5]-input_bounds[4]) bmax=input_bounds[5]-input_bounds[4];
919 this->GetRenderer()->ResetCamera();
920 this->GetRenderer()->GetActiveCamera()->SetParallelScale(bmax/2);
924 void vvSlicer::SetDisplayMode(bool i)
926 this->GetImageActor()->SetVisibility(i);
927 this->GetAnnotation()->SetVisibility(i);
928 this->GetRenderer()->SetDraw(i);
930 mLandActor->SetVisibility(i);
931 pdmA->SetVisibility(i);
933 UpdateDisplayExtent();
936 void vvSlicer::FlipHorizontalView()
938 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
941 double *position = cam->GetPosition();
942 switch (this->SliceOrientation)
944 case vtkImageViewer2::SLICE_ORIENTATION_XY:
945 cam->SetPosition(position[0],position[1],-position[2]);
948 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
949 cam->SetPosition(position[0],-position[1],position[2]);
952 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
953 cam->SetPosition(-position[0],position[1],position[2]);
956 this->Renderer->ResetCameraClippingRange();
957 this->UpdateDisplayExtent();
961 void vvSlicer::FlipVerticalView()
963 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
966 FlipHorizontalView();
967 double *viewup = cam->GetViewUp();
968 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
969 this->UpdateDisplayExtent();
973 void vvSlicer::Render()
975 if (this->GetWindowLevel()->GetLookupTable() and not this->mOverlay and not
978 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
979 legend->SetVisibility(1);
981 else legend->SetVisibility(0);
983 if (ca->GetVisibility())
985 std::string worldPos = "";
986 std::stringstream world1;
987 std::stringstream world2;
988 std::stringstream world3;
989 world1 << (int)mCurrent[0];
990 world2 << (int)mCurrent[1];
991 world3 << (int)mCurrent[2];
992 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
993 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
994 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
996 if (pdmA->GetVisibility())
998 double x = mCursor[0];
999 double y = mCursor[1];
1000 double z = mCursor[2];
1001 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1002 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1003 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1005 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1006 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1007 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1008 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1009 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1010 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 )
1012 vtkRenderer * renderer = this->Renderer;
1014 renderer->WorldToView(x,y,z);
1015 renderer->ViewToNormalizedViewport(x,y,z);
1016 renderer->NormalizedViewportToViewport(x,y);
1017 renderer->ViewportToNormalizedDisplay(x,y);
1018 renderer->NormalizedDisplayToDisplay(x,y);
1019 crossCursor->SetFocalPoint(x,y,z);
1022 crossCursor->SetFocalPoint(-1,-1,z);
1025 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1026 X <= this->GetInput()->GetWholeExtent()[1] &&
1027 Y >= this->GetInput()->GetWholeExtent()[2] &&
1028 Y <= this->GetInput()->GetWholeExtent()[3] &&
1029 Z >= this->GetInput()->GetWholeExtent()[4] &&
1030 Z <= this->GetInput()->GetWholeExtent()[5])
1032 std::stringstream pixel1;
1033 std::stringstream pixel2;
1034 std::stringstream pixel3;
1035 std::stringstream temps;
1039 temps << mCurrentTSlice;
1040 double value = this->GetInput()->GetScalarComponentAsDouble(
1045 std::stringstream val;
1047 worldPos += "data value : " + val.str();
1048 worldPos += "\n mm : " + world1.str() + " " + world2.str() + " " + world3.str() + " " + temps.str();
1049 worldPos += "\n pixel : " + pixel1.str() + " " + pixel2.str() + " " + pixel3.str() + " " + temps.str();
1051 ca->SetText(1,worldPos.c_str());
1053 if (mOverlay && mOverlayActor->GetVisibility())
1055 mOverlayMapper->SetWindow(this->GetColorWindow());
1056 mOverlayMapper->SetLevel(this->GetColorLevel());
1057 mOverlayMapper->Update();
1061 //this->Superclass::Render();
1062 this->GetRenderWindow()->Render();
1065 void vvSlicer::UpdateCursorPosition()
1067 if (this->GetImageActor()->GetVisibility())
1069 pdmA->SetVisibility(true);
1070 mCursor[0] = mCurrent[0];
1071 mCursor[1] = mCurrent[1];
1072 mCursor[2] = mCurrent[2];
1073 mCursor[3] = mCurrentTSlice;
1077 void vvSlicer::UpdateLandmarks()
1079 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1080 if (pd->GetPoints())
1082 mLandGlyph->SetRange(0,1);
1083 mLandGlyph->Modified();
1084 mLandGlyph->Update();
1086 mClipBox->Modified();
1087 mLandClipper->Update();
1088 mLandMapper->Update();
1093 //----------------------------------------------------------------------------
1094 void vvSlicer::SetSlice(int slice)
1096 int *range = this->GetSliceRange();
1099 if (slice < range[0])
1103 else if (slice > range[1])
1109 if (this->Slice == slice)
1114 this->Slice = slice;
1117 this->UpdateDisplayExtent();
1121 void vvSlicer::SetContourSlice()
1123 if (mSurfaceCutActors.size() > 0)
1124 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1125 i!=mSurfaceCutActors.end();i++)
1126 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1127 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1130 void vvSlicer::ForceUpdateDisplayExtent()
1132 this->UpdateDisplayExtent();
1135 int* vvSlicer::GetDisplayExtent()
1137 return this->GetImageActor()->GetDisplayExtent();
1140 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1142 this->Superclass::PrintSelf(os, indent);