1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
21 #include "vvSlicerManagerCommand.h"
22 #include "vvGlyphSource.h"
23 #include "vvGlyph2D.h"
24 #include "vvImageMapToWLColors.h"
26 #include <vtkTextProperty.h>
27 #include <vtkTextActor.h>
28 #include <vtkTextSource.h>
29 #include <vtkActor2D.h>
30 #include <vtkCursor2D.h>
31 #include <vtkPolyDataMapper2D.h>
32 #include <vtkProperty2D.h>
33 #include <vtkCornerAnnotation.h>
34 #include <vtkImageMapToWindowLevelColors.h>
35 #include <vtkImageData.h>
36 #include <vtkImageActor.h>
37 #include <vvBlendImageActor.h>
38 #include <vtkToolkits.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkPointData.h>
41 #include <vtkDataArray.h>
42 #include <vtkFloatArray.h>
43 #include <vtkClipPolyData.h>
44 #include <vtkGlyph3D.h>
46 #include <vtkCursor3D.h>
47 #include <vtkProperty.h>
49 #include <vtkLightCollection.h>
50 #include <vtkScalarBarActor.h>
51 #include <vtkLookupTable.h>
53 #include <vtkRenderer.h>
54 #include <vtkRendererCollection.h>
55 #include <vtkRenderWindow.h>
56 #include <vtkRenderWindowInteractor.h>
57 #include <vtkCamera.h>
58 #include <vtkCallbackCommand.h>
59 #include <vtkCommand.h>
60 #include <vtkPolyDataMapper.h>
63 #include <vtkExtractVOI.h>
64 #include <vtkSphereSource.h>
65 #include <vtkCutter.h>
66 #include <vtkAssignAttribute.h>
67 #include <vtkImageAccumulate.h>
68 #include <vtkImageReslice.h>
70 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
71 vtkStandardNewMacro(vvSlicer);
72 static void copyExtent(int* in, int* to){
73 for(int i=0; i<6; ++i) to[i]=in[i];
75 //------------------------------------------------------------------------------
78 this->UnInstallPipeline();
80 mReducedExtent = new int[6];
82 mUseReducedExtent = false;
84 mCurrent[0] = -VTK_DOUBLE_MAX;
85 mCurrent[1] = -VTK_DOUBLE_MAX;
86 mCurrent[2] = -VTK_DOUBLE_MAX;
88 mCursor[0] = -VTK_DOUBLE_MAX;
89 mCursor[1] = -VTK_DOUBLE_MAX;
90 mCursor[2] = -VTK_DOUBLE_MAX;
91 mCursor[3] = -VTK_DOUBLE_MAX;
101 std::string text = "F1 = sagital; F2 = coronal; F3 = axial\n";
102 text += "F5 = horizontal flip; F6 = vertical flip\n\n";
103 text += "0,1,2,3,4,5 : preset windowing\n";
104 text += "6,7,8,9 : preset colormap\n";
105 text += "z : local windowing\n";
106 text += "r : reset view\n";
107 text += "l : reload image\n";
108 text += "f : fly to mouse position\n";
109 text += "g : go to cross hair position\n\n";
110 text += "Up,down : change slice\n";
111 text += "Left,right : change tenporal slice\n\n";
112 text += "Scrollbar (or w/x) : zoom in/out\n";
113 text += "left button : synchronize all views\n";
114 text += "middle button : grab image\n";
115 text += "right button : change windowing\n";
117 crossCursor = vtkSmartPointer<vtkCursor2D>::New();
118 crossCursor->AllOff();
119 crossCursor->AxesOn();
120 crossCursor->SetTranslationMode(1);
121 crossCursor->SetRadius(2);
123 pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
124 pdm->SetInput(crossCursor->GetOutput());
126 pdmA = vtkSmartPointer<vtkActor2D>::New();
127 pdmA->SetMapper(pdm);
128 pdmA->GetProperty()->SetColor(255,10,212);
129 pdmA->SetVisibility(0);
130 pdmA->SetPickable(0);
132 ca = vtkSmartPointer<vtkCornerAnnotation>::New();
133 ca->GetTextProperty()->SetColor(255,10,212);
134 ca->SetVisibility(1);
142 legend = vtkSmartPointer<vtkScalarBarActor>::New();
143 //legend->SetTitle("test!");
144 legend->SetPosition(0.82,0.18);
145 legend->SetWidth(0.1);
146 legend->SetVisibility(0);
147 legend->SetLabelFormat("%.1f");
148 this->GetRenderer()->AddActor(legend);
150 this->WindowLevel->Delete();
151 this->WindowLevel = vvImageMapToWLColors::New();
153 this->InstallPipeline();
155 //------------------------------------------------------------------------------
158 //------------------------------------------------------------------------------
159 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
161 return mOverlayMapper.GetPointer();
163 //------------------------------------------------------------------------------
166 //------------------------------------------------------------------------------
167 vvBlendImageActor* vvSlicer::GetOverlayActor()
169 return mOverlayActor.GetPointer();
171 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 vtkImageMapToWindowLevelColors* vvSlicer::GetFusionMapper()
177 return mFusionMapper.GetPointer();
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 vtkImageActor* vvSlicer::GetFusionActor()
185 return mFusionActor.GetPointer();
187 //------------------------------------------------------------------------------
190 //------------------------------------------------------------------------------
191 vtkActor* vvSlicer::GetVFActor()
193 return mVFActor.GetPointer();
195 //------------------------------------------------------------------------------
198 //------------------------------------------------------------------------------
199 vtkCornerAnnotation* vvSlicer::GetAnnotation()
201 return ca.GetPointer();
203 //------------------------------------------------------------------------------
206 //------------------------------------------------------------------------------
207 void vvSlicer::EnableReducedExtent(bool b)
209 mUseReducedExtent = b;
211 //------------------------------------------------------------------------------
214 //------------------------------------------------------------------------------
215 void vvSlicer::SetReducedExtent(int * ext)
217 copyExtent(ext, mReducedExtent);
219 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
226 mSurfaceCutActors.push_back(new vvMeshActor());
228 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
230 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
231 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
232 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
236 //------------------------------------------------------------------------------
239 //------------------------------------------------------------------------------
240 void vvSlicer::ToggleContourSuperposition()
242 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
243 i!=mSurfaceCutActors.end(); i++)
244 (*i)->ToggleSuperposition();
246 //------------------------------------------------------------------------------
249 //------------------------------------------------------------------------------
250 void vvSlicer::SetCursorColor(int r,int g, int b)
252 pdmA->GetProperty()->SetColor(r,g,b);
254 //------------------------------------------------------------------------------
257 //------------------------------------------------------------------------------
258 void vvSlicer::SetCursorVisibility(bool s)
260 pdmA->SetVisibility(s);
262 //------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------
266 bool vvSlicer::GetCursorVisibility()
268 return pdmA->GetVisibility();
270 //------------------------------------------------------------------------------
273 //------------------------------------------------------------------------------
274 void vvSlicer::SetCornerAnnotationVisibility(bool s)
276 ca->SetVisibility(s);
278 //------------------------------------------------------------------------------
281 //------------------------------------------------------------------------------
282 bool vvSlicer::GetCornerAnnotationVisibility()
284 return ca->GetVisibility();
286 //------------------------------------------------------------------------------
289 //------------------------------------------------------------------------------
290 vvSlicer::~vvSlicer()
292 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
293 i!=mSurfaceCutActors.end(); i++)
295 delete [] mReducedExtent;
297 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
308 //------------------------------------------------------------------------------
311 //------------------------------------------------------------------------------
312 void vvSlicer::SetImage(vvImage::Pointer image)
314 if (image->GetVTKImages().size()) {
317 if (!mImageReslice) {
318 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
319 mImageReslice->SetInterpolationModeToLinear();
320 mImageReslice->AutoCropOutputOn();
321 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
323 mImageReslice->SetResliceTransform(mImage->GetTransform());
324 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
325 mImageReslice->UpdateInformation();
327 this->Superclass::SetInput(mImageReslice->GetOutput());
330 this->GetInput()->GetWholeExtent(extent);
332 // Prevent crash when reload -> change slice if outside extent
333 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
334 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
337 // Make sure that the required part image has been computed
338 extent[SliceOrientation*2] = Slice;
339 extent[SliceOrientation*2+1] = Slice;
340 mImageReslice->GetOutput()->SetUpdateExtent(extent);
341 mImageReslice->GetOutput()->Update();
343 this->UpdateDisplayExtent();
346 ca->SetText(0,mFileName.c_str());
349 //------------------------------------------------------------------------------
352 //------------------------------------------------------------------------------
353 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
355 if (overlay->GetVTKImages().size()) {
358 if (!mOverlayReslice) {
359 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
360 mOverlayReslice->SetInterpolationModeToLinear();
361 mOverlayReslice->AutoCropOutputOn();
362 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
364 mOverlayReslice->SetResliceTransform(mOverlay->GetTransform());
365 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
368 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
369 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
371 if (!mOverlayActor) {
372 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
373 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
374 mOverlayActor->SetPickable(0);
375 mOverlayActor->SetVisibility(false);
376 mOverlayActor->SetOpacity(0.5);
379 //stupid but necessary : the Overlay need to be rendered before fusion
381 this->GetRenderer()->RemoveActor(mFusionActor);
382 this->GetRenderer()->AddActor(mOverlayActor);
383 this->GetRenderer()->AddActor(mFusionActor);
385 this->GetRenderer()->AddActor(mOverlayActor);
387 //Synchronize orientation and slice
388 this->SetSliceOrientation(this->SliceOrientation);
389 this->SetTSlice(mCurrentTSlice);
392 //------------------------------------------------------------------------------
395 //------------------------------------------------------------------------------
396 void vvSlicer::SetFusion(vvImage::Pointer fusion)
398 if (fusion->GetVTKImages().size()) {
401 if (!mFusionReslice) {
402 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
403 mFusionReslice->SetInterpolationModeToLinear();
404 mFusionReslice->AutoCropOutputOn();
405 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
407 mFusionReslice->SetResliceTransform(mFusion->GetTransform());
408 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
411 mFusionMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
412 mFusionMapper->SetInput(mFusionReslice->GetOutput());
415 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
416 mFusionActor->SetInput(mFusionMapper->GetOutput());
417 mFusionActor->SetPickable(0);
418 mFusionActor->SetVisibility(false);
419 mFusionActor->SetOpacity(0.7);
420 this->GetRenderer()->AddActor(mFusionActor);
423 //Synchronize orientation and slice
424 this->SetSliceOrientation(this->SliceOrientation);
425 this->SetTSlice(mCurrentTSlice);
428 //------------------------------------------------------------------------------
431 //------------------------------------------------------------------------------
432 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
434 if (actor_type == "vector") {
435 this->mVFActor->SetVisibility(vis);
437 if (actor_type == "overlay") {
438 this->mOverlayActor->SetVisibility(vis);
440 if (actor_type == "fusion") {
441 this->mFusionActor->SetVisibility(vis);
443 if (actor_type == "contour")
444 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
445 UpdateDisplayExtent();
447 //------------------------------------------------------------------------------
450 //------------------------------------------------------------------------------
451 void vvSlicer::SetVF(vvImage::Pointer vf)
453 if (vf->GetVTKImages().size()) {
457 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
458 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
459 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
461 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
462 mAAFilter->SetInput(mVOIFilter->GetOutput());
463 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
464 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
467 mArrow = vtkSmartPointer<vvGlyphSource>::New();
468 mArrow->SetGlyphTypeToSpecificArrow();
469 mArrow->SetScale(mScale);
472 // Glyph the gradient vector (with arrows)
474 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
475 mGlyphFilter->SetInput(mAAFilter->GetOutput());
476 mGlyphFilter->SetSource(mArrow->GetOutput());
477 mGlyphFilter->ScalingOn();
478 mGlyphFilter->SetScaleModeToScaleByVector();
479 mGlyphFilter->OrientOn();
480 mGlyphFilter->SetVectorModeToUseVector();
481 mGlyphFilter->SetColorModeToColorByVector();
484 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
486 double mVFColorHSV[3];
487 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
488 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
489 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
490 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
493 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
494 mVFMapper->SetInput(mGlyphFilter->GetOutput());
495 mVFMapper->ImmediateModeRenderingOn();
496 mVFMapper->SetLookupTable(mVFColorLUT);
499 mVFActor = vtkSmartPointer<vtkActor>::New();
500 mVFActor->SetMapper(mVFMapper);
501 mVFActor->SetPickable(0);
502 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
503 this->UpdateDisplayExtent();
504 this->GetRenderer()->AddActor(mVFActor);
507 SetTSlice(mCurrentTSlice);
510 //------------------------------------------------------------------------------
513 //------------------------------------------------------------------------------
514 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
516 mLandmarks = landmarks;
520 mCross = vtkSmartPointer<vtkCursor3D>::New();
521 mCross->SetFocalPoint(0.0,0.0,0.0);
522 mCross->SetModelBounds(-10,10,-10,10,-10,10);
527 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
528 mLandGlyph->SetSource(mCross->GetOutput());
529 mLandGlyph->SetInput(landmarks->GetOutput());
530 //mLandGlyph->SetIndexModeToScalar();
531 mLandGlyph->SetRange(0,1);
532 mLandGlyph->ScalingOff();
534 mLandGlyph->SetColorModeToColorByScalar();
537 mClipBox = vtkSmartPointer<vtkBox>::New();
539 mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
540 mLandClipper->InsideOutOn();
541 mLandClipper->SetInput(mLandGlyph->GetOutput());
542 mLandClipper->SetClipFunction(mClipBox);
545 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
546 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
547 //mLandMapper->ScalarVisibilityOff();
550 mLandActor = vtkSmartPointer<vtkActor>::New();
551 mLandActor->SetMapper(mLandMapper);
552 mLandActor->GetProperty()->SetColor(255,10,212);
553 mLandActor->SetPickable(0);
554 mLandActor->SetVisibility(true);
555 this->UpdateDisplayExtent();
556 this->GetRenderer()->AddActor(mLandActor);
559 //------------------------------------------------------------------------------
561 //------------------------------------------------------------------------------
562 //FIXME: this function leaks memory, we should fix it someday :)
563 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
565 if (actor_type == "vector") {
566 Renderer->RemoveActor(mVFActor);
575 if (actor_type == "overlay") {
576 Renderer->RemoveActor(mOverlayActor);
578 mOverlayActor = NULL;
579 mOverlayMapper = NULL;
581 if (actor_type == "fusion") {
582 Renderer->RemoveActor(mFusionActor);
585 mFusionMapper = NULL;
587 if (actor_type == "contour") {
588 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
589 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
592 //------------------------------------------------------------------------------
595 //------------------------------------------------------------------------------
596 void vvSlicer::SetVFSubSampling(int sub)
599 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
602 UpdateDisplayExtent();
605 //------------------------------------------------------------------------------
608 //------------------------------------------------------------------------------
609 void vvSlicer::SetVFScale(int scale)
613 mArrow->SetScale(mScale);
614 UpdateDisplayExtent();
617 //------------------------------------------------------------------------------
619 //------------------------------------------------------------------------------
620 void vvSlicer::SetVFWidth(int width)
624 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
625 UpdateDisplayExtent();
628 //------------------------------------------------------------------------------
631 //------------------------------------------------------------------------------
632 void vvSlicer::SetVFLog(int log)
636 mGlyphFilter->SetUseLog(mVFLog);
637 mGlyphFilter->Modified();
639 UpdateDisplayExtent();
642 //------------------------------------------------------------------------------
645 //------------------------------------------------------------------------------
646 void vvSlicer::SetTSlice(int t)
650 else if ((unsigned int)t >= mImage->GetVTKImages().size())
651 t = mImage->GetVTKImages().size() -1;
653 if (mCurrentTSlice == t) return;
656 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
657 if (mVF && mVFActor->GetVisibility()) {
658 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
659 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
661 if (mOverlay && mOverlayActor->GetVisibility()) {
662 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
663 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
665 if (mFusion && mFusionActor->GetVisibility()) {
666 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
667 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
669 if (mSurfaceCutActors.size() > 0)
670 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
671 i!=mSurfaceCutActors.end(); i++)
672 (*i)->SetTimeSlice(mCurrentTSlice);
673 UpdateDisplayExtent();
675 //------------------------------------------------------------------------------
678 //------------------------------------------------------------------------------
679 int vvSlicer::GetTSlice()
681 return mCurrentTSlice;
683 //------------------------------------------------------------------------------
685 //------------------------------------------------------------------------------
686 void vvSlicer::SetSliceOrientation(int orientation)
688 //if 2D image, force to watch in Axial View
690 this->GetInput()->GetWholeExtent(extent);
691 if (extent[5]-extent[4] <= 2)
692 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
694 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
695 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
696 vtkErrorMacro("Error - invalid slice orientation " << orientation);
700 this->SliceOrientation = orientation;
703 AdjustResliceToSliceOrientation(mFusionReslice);
706 AdjustResliceToSliceOrientation(mOverlayReslice);
709 int *range = this->GetSliceRange();
711 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
713 // Go to current cursor position
714 // double* cursorPos = GetCursorPosition();
715 // DDV(cursorPos, 3);
716 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
718 this->UpdateOrientation();
719 this->UpdateDisplayExtent();
721 if (this->Renderer && this->GetInput()) {
722 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
723 this->Renderer->ResetCamera();
724 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
729 //----------------------------------------------------------------------------
731 //------------------------------------------------------------------------------
732 // This function ensures that we sample the slices of a vtkImageReslice filter
733 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
734 // In other words, we change the grid of the reslice in the same way as the grid
735 // of the displayed image in the slicing direction.
736 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
739 double origin[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
740 double spacing[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
741 reslice->SetOutputOrigin(origin);
742 reslice->SetOutputSpacing(spacing);
743 reslice->GetOutput()->UpdateInformation();
744 reslice->GetOutput()->GetOrigin(origin);
745 reslice->GetOutput()->GetSpacing(spacing);
747 // Use similar spacing as the image in the direction SliceOrientation
748 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
750 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
751 // Step 1: from world coordinates to image coordinates
752 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
753 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
754 // Step 2: round to superior grid positionInc
755 origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
756 // Step 3: back to world coordinates
757 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
758 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
760 // Set new spacing and origin
761 reslice->SetOutputOrigin(origin);
762 reslice->SetOutputSpacing(spacing);
763 reslice->UpdateInformation();
765 //------------------------------------------------------------------------------
767 //----------------------------------------------------------------------------
768 int * vvSlicer::GetExtent(){
770 if (mUseReducedExtent) {
771 w_ext = mReducedExtent;
772 } else w_ext = GetInput()->GetWholeExtent();
775 //----------------------------------------------------------------------------
778 //----------------------------------------------------------------------------
779 int vvSlicer::GetOrientation()
781 return this->SliceOrientation;
783 //----------------------------------------------------------------------------
786 //----------------------------------------------------------------------------
787 void vvSlicer::UpdateDisplayExtent()
789 vtkImageData *input = this->GetInput();
790 if (!input || !this->ImageActor) {
793 input->UpdateInformation();
795 // Local copy of extent
797 copyExtent(GetExtent(), w_ext);
799 w_ext[ this->SliceOrientation*2 ] = this->Slice;
800 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
803 this->ImageActor->SetDisplayExtent(w_ext);
805 // Overlay image actor
806 if (mOverlay && mOverlayActor->GetVisibility()) {
808 mOverlayReslice->GetOutput()->UpdateInformation();
809 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
810 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
811 mOverlayActor->SetDisplayExtent( overExtent );
814 // Fusion image actor
815 if (mFusion && mFusionActor->GetVisibility()) {
817 mFusionReslice->GetOutput()->UpdateInformation();
818 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
819 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
820 mFusionActor->SetDisplayExtent(fusExtent);
823 // Vector field actor
824 double* camera = Renderer->GetActiveCamera()->GetPosition();
825 double* image_bounds = ImageActor->GetBounds();
826 double position[3] = {0, 0, 0};
827 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
829 //print_vector<double, 6>("camera", camera);
830 //print_vector<double, 6>("image_bounds", image_bounds);
831 //print_vector<double, 3>("position", position);
833 // find where to place the VF actor. to deal with
834 // z-buffer issues, the VF is placed right in front of the image,
835 // subject to a small offset. the position actually depends on the
836 // the location of the camera relative to the image.
838 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
841 if (mVF && mVFActor->GetVisibility()) {
843 mVF->GetVTKImages()[0]->UpdateInformation();
844 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
845 ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
846 mVOIFilter->SetVOI(vfExtent);
847 int orientation[3] = {1,1,1};
848 orientation[this->SliceOrientation] = 0;
849 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
852 position[this->SliceOrientation] += offset;
853 mVFActor->SetPosition(position);
860 for(unsigned int i=0; i<6; i++)
861 bounds[i] = ImageActor->GetBounds()[i];
862 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
863 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
864 mClipBox->SetBounds(bounds);
868 position[this->SliceOrientation] = offset;
869 mLandActor->SetPosition(position);
872 // Figure out the correct clipping range
873 if (this->Renderer) {
874 if (this->InteractorStyle &&
875 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
876 this->Renderer->ResetCameraClippingRange();
878 vtkCamera *cam = this->Renderer->GetActiveCamera();
881 this->ImageActor->GetBounds(bounds);
882 double spos = (double)bounds[this->SliceOrientation * 2];
883 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
884 double range = fabs(spos - cpos);
885 double *spacing = input->GetSpacing();
887 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
888 cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
894 //----------------------------------------------------------------------------
896 //----------------------------------------------------------------------------
897 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
898 vtkImageData *targetImage, int targetExtent[6])
901 for(unsigned int i=0; i<6; i++) {
902 // From source voxel coordinates to world coordinates
903 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
905 // From world coordinates to floating point target voxel coordinates
906 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
909 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
912 //----------------------------------------------------------------------------
914 //----------------------------------------------------------------------------
915 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
920 //2D overlay on 3D image specific case
921 if (refExtent[4] == refExtent[5]) {
923 extent[4] = refExtent[4];
924 extent[5] = refExtent[5];
927 for (int i = 0; i < maxBound; i = i+2) {
928 //if we are totally outside the image
929 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
933 //crop to the limit of the image
934 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
935 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
936 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
937 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
940 for (int i = 0; i < maxBound; i = i+2) {
941 extent[i] = refExtent[i];
942 extent[i+1] = refExtent[i];
945 //----------------------------------------------------------------------------
948 //----------------------------------------------------------------------------
949 void vvSlicer::UpdateOrientation()
951 // Set the camera position
952 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
954 switch (this->SliceOrientation) {
955 case vtkImageViewer2::SLICE_ORIENTATION_XY:
956 cam->SetFocalPoint(0,0,0);
957 cam->SetPosition(0,0,-1); // -1 if medical ?
958 cam->SetViewUp(0,-1,0);
961 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
962 cam->SetFocalPoint(0,0,0);
963 cam->SetPosition(0,-1,0); // 1 if medical ?
964 cam->SetViewUp(0,0,1);
967 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
968 cam->SetFocalPoint(0,0,0);
969 cam->SetPosition(-1,0,0); // -1 if medical ?
970 cam->SetViewUp(0,0,1);
975 //----------------------------------------------------------------------------
978 //----------------------------------------------------------------------------
979 void vvSlicer::SetOpacity(double s)
981 this->GetImageActor()->SetOpacity(s);
983 //----------------------------------------------------------------------------
986 //----------------------------------------------------------------------------
987 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
989 this->Superclass::SetRenderWindow(rw);
990 this->SetupInteractor(rw->GetInteractor());
991 ca->SetImageActor(this->GetImageActor());
992 ca->SetWindowLevel(this->GetWindowLevel());
993 ca->SetText(2, "<slice>");
994 ca->SetText(3, "<window>\n<level>");
1006 crossCursor->SetModelBounds(bounds);
1007 this->GetRenderer()->AddActor(pdmA);
1008 this->GetRenderer()->AddActor(ca);
1009 this->GetRenderer()->ResetCamera();
1011 //this is just a mapping between the labeling of the orientations presented to the user and
1012 //the one used by vtk
1013 SetSliceOrientation(2-(orientation%3));
1016 //----------------------------------------------------------------------------
1019 //----------------------------------------------------------------------------
1020 void vvSlicer::ResetCamera()
1022 this->GetRenderer()->ResetCamera();
1024 //----------------------------------------------------------------------------
1027 //----------------------------------------------------------------------------
1028 void vvSlicer::SetDisplayMode(bool i)
1030 this->GetImageActor()->SetVisibility(i);
1031 this->GetAnnotation()->SetVisibility(i);
1032 this->GetRenderer()->SetDraw(i);
1034 mLandActor->SetVisibility(i);
1035 pdmA->SetVisibility(i);
1037 UpdateDisplayExtent();
1039 //----------------------------------------------------------------------------
1042 //----------------------------------------------------------------------------
1043 void vvSlicer::FlipHorizontalView()
1045 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1047 double *position = cam->GetPosition();
1048 switch (this->SliceOrientation) {
1049 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1050 cam->SetPosition(position[0],position[1],-position[2]);
1053 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1054 cam->SetPosition(position[0],-position[1],position[2]);
1057 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1058 cam->SetPosition(-position[0],position[1],position[2]);
1061 this->Renderer->ResetCameraClippingRange();
1062 this->UpdateDisplayExtent();
1065 //----------------------------------------------------------------------------
1068 //----------------------------------------------------------------------------
1069 void vvSlicer::FlipVerticalView()
1071 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1073 FlipHorizontalView();
1074 double *viewup = cam->GetViewUp();
1075 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1076 this->UpdateDisplayExtent();
1079 //----------------------------------------------------------------------------
1082 //----------------------------------------------------------------------------
1083 void vvSlicer::SetColorWindow(double window)
1085 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1087 double level = this->GetWindowLevel()->GetLevel();
1088 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1091 this->vtkImageViewer2::SetColorWindow(window);
1093 //----------------------------------------------------------------------------
1096 //----------------------------------------------------------------------------
1097 void vvSlicer::SetColorLevel(double level)
1099 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1101 double window = this->GetWindowLevel()->GetWindow();
1102 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1105 this->vtkImageViewer2::SetColorLevel(level);
1107 //----------------------------------------------------------------------------
1109 //----------------------------------------------------------------------------
1110 // Returns the min an the max value in a 41x41 region around the mouse pointer
1111 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1113 //Get mouse pointer position in view coordinates
1114 double fLocalExtents[6];
1115 for(int i=0; i<3; i++) {
1116 fLocalExtents[i*2 ] = mCurrent[i];
1117 fLocalExtents[i*2+1] = mCurrent[i];
1119 this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1120 this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1121 for(int i=0; i<3; i++) {
1122 if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1123 fLocalExtents[i*2 ] -= 0.2;
1124 fLocalExtents[i*2+1] += 0.2;
1127 this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1128 this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1130 //Convert to image pixel coordinates (rounded)
1131 int iLocalExtents[6];
1132 for(int i=0; i<3; i++) {
1133 fLocalExtents[i*2 ] = (fLocalExtents[i*2 ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1134 fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1136 iLocalExtents[i*2 ] = lrint(fLocalExtents[i*2 ]);
1137 iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1139 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1140 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1143 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1144 voiFilter->SetInput(this->GetInput());
1145 voiFilter->SetVOI(iLocalExtents);
1146 voiFilter->Update();
1147 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1153 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1154 accFilter->SetInput(voiFilter->GetOutput());
1155 accFilter->Update();
1157 min = *(accFilter->GetMin());
1158 max = *(accFilter->GetMax());
1160 //----------------------------------------------------------------------------
1162 //----------------------------------------------------------------------------
1163 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1168 if (ix < image->GetWholeExtent()[0] ||
1169 ix > image->GetWholeExtent()[1] ||
1170 iy < image->GetWholeExtent()[2] ||
1171 iy > image->GetWholeExtent()[3] ||
1172 iz < image->GetWholeExtent()[4] ||
1173 iz > image->GetWholeExtent()[5] )
1174 return std::numeric_limits<double>::quiet_NaN();
1176 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1178 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1180 //----------------------------------------------------------------------------
1182 //----------------------------------------------------------------------------
1183 void vvSlicer::Render()
1185 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1186 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1187 legend->SetVisibility(1);
1188 } else legend->SetVisibility(0);
1190 if (ca->GetVisibility()) {
1191 std::stringstream worldPos;
1192 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1193 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1194 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1196 // if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1197 // else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1];
1198 // if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1199 // else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3];
1200 // if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1201 // else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5];
1203 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1204 X <= this->GetInput()->GetWholeExtent()[1] &&
1205 Y >= this->GetInput()->GetWholeExtent()[2] &&
1206 Y <= this->GetInput()->GetWholeExtent()[3] &&
1207 Z >= this->GetInput()->GetWholeExtent()[4] &&
1208 Z <= this->GetInput()->GetWholeExtent()[5]) {
1212 double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1214 worldPos << "data value : " << value << std::endl;
1215 worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1216 << lrint(mCurrent[1]) << ' '
1217 << lrint(mCurrent[2]) << ' '
1220 worldPos << "pixel : " << ix << ' '
1226 ca->SetText(1,worldPos.str().c_str());
1229 if (pdmA->GetVisibility()) {
1230 double x = mCursor[0];
1231 double y = mCursor[1];
1232 double z = mCursor[2];
1233 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1234 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1235 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1237 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1238 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1239 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1240 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1241 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1242 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1243 vtkRenderer * renderer = this->Renderer;
1245 renderer->WorldToView(x,y,z);
1246 renderer->ViewToNormalizedViewport(x,y,z);
1247 renderer->NormalizedViewportToViewport(x,y);
1248 renderer->ViewportToNormalizedDisplay(x,y);
1249 renderer->NormalizedDisplayToDisplay(x,y);
1250 crossCursor->SetFocalPoint(x,y,z);
1252 crossCursor->SetFocalPoint(-1,-1,z);
1256 if (mOverlay && mOverlayActor->GetVisibility()) {
1257 mOverlayMapper->SetWindow(this->GetColorWindow());
1258 mOverlayMapper->SetLevel(this->GetColorLevel());
1259 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1260 mOverlayMapper->GetOutput()->Update();
1261 mOverlayMapper->Update();
1266 this->GetRenderWindow()->Render();
1268 //----------------------------------------------------------------------------
1271 //----------------------------------------------------------------------------
1272 void vvSlicer::UpdateCursorPosition()
1274 if (this->GetImageActor()->GetVisibility()) {
1275 pdmA->SetVisibility(true);
1276 mCursor[0] = mCurrent[0];
1277 mCursor[1] = mCurrent[1];
1278 mCursor[2] = mCurrent[2];
1279 mCursor[3] = mCurrentTSlice;
1282 //----------------------------------------------------------------------------
1285 //----------------------------------------------------------------------------
1286 void vvSlicer::UpdateLandmarks()
1288 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1289 if (pd->GetPoints()) {
1290 mLandGlyph->SetRange(0,1);
1291 mLandGlyph->Modified();
1292 mLandGlyph->Update();
1294 mClipBox->Modified();
1295 mLandClipper->Update();
1296 mLandMapper->Update();
1300 //----------------------------------------------------------------------------
1303 //----------------------------------------------------------------------------
1304 void vvSlicer::SetSlice(int slice)
1306 int *range = this->GetSliceRange();
1308 if (slice < range[0]) {
1310 } else if (slice > range[1]) {
1315 if (this->Slice == slice) {
1319 this->Slice = slice;
1322 this->UpdateDisplayExtent();
1324 // Seems to work without this line
1327 //----------------------------------------------------------------------------
1330 //----------------------------------------------------------------------------
1331 void vvSlicer::SetContourSlice()
1333 if (mSurfaceCutActors.size() > 0)
1334 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1335 i!=mSurfaceCutActors.end(); i++) {
1337 (*i)->SetSlicingOrientation(this->SliceOrientation);
1338 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1339 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1342 //----------------------------------------------------------------------------
1345 //----------------------------------------------------------------------------
1346 void vvSlicer::ForceUpdateDisplayExtent()
1348 this->UpdateDisplayExtent();
1350 //----------------------------------------------------------------------------
1353 //----------------------------------------------------------------------------
1354 int* vvSlicer::GetDisplayExtent()
1356 return this->GetImageActor()->GetDisplayExtent();
1358 //----------------------------------------------------------------------------
1361 //----------------------------------------------------------------------------
1362 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1364 this->Superclass::PrintSelf(os, indent);
1366 //----------------------------------------------------------------------------
1368 //----------------------------------------------------------------------------
1369 void vvSlicer::SetVFColor(double r, double g, double b)
1371 double mVFColorHSV[3];
1376 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1377 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1378 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1379 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);