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 crossCursor = vtkSmartPointer<vtkCursor2D>::New();
102 crossCursor->AllOff();
103 crossCursor->AxesOn();
104 crossCursor->SetTranslationMode(1);
105 crossCursor->SetRadius(2);
107 pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
108 pdm->SetInput(crossCursor->GetOutput());
110 pdmA = vtkSmartPointer<vtkActor2D>::New();
111 pdmA->SetMapper(pdm);
112 pdmA->GetProperty()->SetColor(255,10,212);
113 pdmA->SetVisibility(0);
114 pdmA->SetPickable(0);
116 ca = vtkSmartPointer<vtkCornerAnnotation>::New();
117 ca->GetTextProperty()->SetColor(255,10,212);
118 ca->SetVisibility(1);
126 legend = vtkSmartPointer<vtkScalarBarActor>::New();
127 //legend->SetTitle("test!");
128 legend->SetPosition(0.82,0.18);
129 legend->SetWidth(0.1);
130 legend->SetVisibility(0);
131 legend->SetLabelFormat("%.1f");
132 this->GetRenderer()->AddActor(legend);
134 this->WindowLevel->Delete();
135 this->WindowLevel = vvImageMapToWLColors::New();
137 this->InstallPipeline();
139 //------------------------------------------------------------------------------
142 //------------------------------------------------------------------------------
143 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
145 return mOverlayMapper.GetPointer();
147 //------------------------------------------------------------------------------
150 //------------------------------------------------------------------------------
151 vvBlendImageActor* vvSlicer::GetOverlayActor()
153 return mOverlayActor.GetPointer();
155 //------------------------------------------------------------------------------
158 //------------------------------------------------------------------------------
159 vtkImageMapToColors* vvSlicer::GetFusionMapper()
161 return mFusionMapper.GetPointer();
163 //------------------------------------------------------------------------------
166 //------------------------------------------------------------------------------
167 vtkImageActor* vvSlicer::GetFusionActor()
169 return mFusionActor.GetPointer();
171 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 vtkActor* vvSlicer::GetVFActor()
177 return mVFActor.GetPointer();
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 vtkCornerAnnotation* vvSlicer::GetAnnotation()
185 return ca.GetPointer();
187 //------------------------------------------------------------------------------
190 //------------------------------------------------------------------------------
191 void vvSlicer::EnableReducedExtent(bool b)
193 mUseReducedExtent = b;
195 //------------------------------------------------------------------------------
198 //------------------------------------------------------------------------------
199 void vvSlicer::SetReducedExtent(int * ext)
201 copyExtent(ext, mReducedExtent);
203 //------------------------------------------------------------------------------
206 //------------------------------------------------------------------------------
207 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
210 mSurfaceCutActors.push_back(new vvMeshActor());
212 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
214 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
215 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
216 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
220 //------------------------------------------------------------------------------
223 //------------------------------------------------------------------------------
224 void vvSlicer::ToggleContourSuperposition()
226 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
227 i!=mSurfaceCutActors.end(); i++)
228 (*i)->ToggleSuperposition();
230 //------------------------------------------------------------------------------
233 //------------------------------------------------------------------------------
234 void vvSlicer::SetCursorColor(int r,int g, int b)
236 pdmA->GetProperty()->SetColor(r,g,b);
238 //------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------
242 void vvSlicer::SetCursorVisibility(bool s)
244 pdmA->SetVisibility(s);
246 //------------------------------------------------------------------------------
249 //------------------------------------------------------------------------------
250 bool vvSlicer::GetCursorVisibility()
252 return pdmA->GetVisibility();
254 //------------------------------------------------------------------------------
257 //------------------------------------------------------------------------------
258 void vvSlicer::SetCornerAnnotationVisibility(bool s)
260 ca->SetVisibility(s);
262 //------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------
266 bool vvSlicer::GetCornerAnnotationVisibility()
268 return ca->GetVisibility();
270 //------------------------------------------------------------------------------
273 //------------------------------------------------------------------------------
274 vvSlicer::~vvSlicer()
276 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
277 i!=mSurfaceCutActors.end(); i++)
279 delete [] mReducedExtent;
281 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
285 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
292 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 void vvSlicer::SetImage(vvImage::Pointer image)
298 if (image->GetVTKImages().size()) {
301 if (!mImageReslice) {
302 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
303 mImageReslice->SetInterpolationModeToLinear();
304 mImageReslice->AutoCropOutputOn();
305 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
307 mImageReslice->SetResliceTransform(mImage->GetTransform());
308 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
309 mImageReslice->UpdateInformation();
311 this->Superclass::SetInput(mImageReslice->GetOutput());
314 this->GetInput()->GetWholeExtent(extent);
316 // Prevent crash when reload -> change slice if outside extent
317 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
318 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
321 // Make sure that the required part image has been computed
322 extent[SliceOrientation*2] = Slice;
323 extent[SliceOrientation*2+1] = Slice;
324 mImageReslice->GetOutput()->SetUpdateExtent(extent);
325 mImageReslice->GetOutput()->Update();
327 this->UpdateDisplayExtent();
330 ca->SetText(0,mFileName.c_str());
333 //------------------------------------------------------------------------------
336 //------------------------------------------------------------------------------
337 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
339 if (overlay->GetVTKImages().size()) {
342 if (!mOverlayReslice) {
343 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
344 mOverlayReslice->SetInterpolationModeToLinear();
345 mOverlayReslice->AutoCropOutputOn();
346 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
348 mOverlayReslice->SetResliceTransform(mOverlay->GetTransform());
349 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
352 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
353 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
355 if (!mOverlayActor) {
356 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
357 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
358 mOverlayActor->SetPickable(0);
359 mOverlayActor->SetVisibility(true);
360 mOverlayActor->SetOpacity(0.5);
363 //stupid but necessary : the Overlay need to be rendered before fusion
365 this->GetRenderer()->RemoveActor(mFusionActor);
366 this->GetRenderer()->AddActor(mOverlayActor);
367 this->GetRenderer()->AddActor(mFusionActor);
369 this->GetRenderer()->AddActor(mOverlayActor);
371 //Synchronize orientation and slice
372 AdjustResliceToSliceOrientation(mOverlayReslice);
373 this->UpdateDisplayExtent();
374 this->SetTSlice(mCurrentTSlice);
377 //------------------------------------------------------------------------------
380 //------------------------------------------------------------------------------
381 void vvSlicer::SetFusion(vvImage::Pointer fusion)
383 if (fusion->GetVTKImages().size()) {
386 if (!mFusionReslice) {
387 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
388 mFusionReslice->SetInterpolationModeToLinear();
389 mFusionReslice->AutoCropOutputOn();
390 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
392 mFusionReslice->SetResliceTransform(mFusion->GetTransform());
393 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
396 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
398 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
400 lut->SetValueRange(0, 1);
401 lut->SetSaturationRange(0, 0);
403 mFusionMapper->SetLookupTable(lut);
404 mFusionMapper->SetInput(mFusionReslice->GetOutput());
407 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
408 mFusionActor->SetInput(mFusionMapper->GetOutput());
409 mFusionActor->SetPickable(0);
410 mFusionActor->SetVisibility(true);
411 mFusionActor->SetOpacity(0.7);
412 this->GetRenderer()->AddActor(mFusionActor);
415 //Synchronize orientation and slice
416 AdjustResliceToSliceOrientation(mFusionReslice);
417 this->UpdateDisplayExtent();
418 this->SetTSlice(mCurrentTSlice);
421 //------------------------------------------------------------------------------
424 //------------------------------------------------------------------------------
425 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
428 if (actor_type == "image") {
429 vis = this->ImageActor->GetVisibility();
431 else if (actor_type == "vector") {
432 vis = this->mVFActor->GetVisibility();
434 else if (actor_type == "overlay") {
435 vis = this->mOverlayActor->GetVisibility();
437 else if (actor_type == "fusion") {
438 vis = this->mFusionActor->GetVisibility();
440 else if (actor_type == "contour")
441 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
445 //------------------------------------------------------------------------------
447 //------------------------------------------------------------------------------
448 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
450 if (actor_type == "image") {
451 this->ImageActor->SetVisibility(vis);
453 else if (actor_type == "vector") {
454 this->mVFActor->SetVisibility(vis);
456 else if (actor_type == "overlay") {
457 this->mOverlayActor->SetVisibility(vis);
459 else if (actor_type == "fusion") {
460 this->mFusionActor->SetVisibility(vis);
462 else if (actor_type == "contour")
463 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
464 UpdateDisplayExtent();
466 //------------------------------------------------------------------------------
468 //------------------------------------------------------------------------------
469 void vvSlicer::SetVF(vvImage::Pointer vf)
471 if (vf->GetVTKImages().size()) {
475 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
476 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
477 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
479 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
480 mAAFilter->SetInput(mVOIFilter->GetOutput());
481 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
482 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
485 mArrow = vtkSmartPointer<vvGlyphSource>::New();
486 mArrow->SetGlyphTypeToSpecificArrow();
487 mArrow->SetScale(mScale);
490 // Glyph the gradient vector (with arrows)
492 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
493 mGlyphFilter->SetInput(mAAFilter->GetOutput());
494 mGlyphFilter->SetSource(mArrow->GetOutput());
495 mGlyphFilter->ScalingOn();
496 mGlyphFilter->SetScaleModeToScaleByVector();
497 mGlyphFilter->OrientOn();
498 mGlyphFilter->SetVectorModeToUseVector();
499 mGlyphFilter->SetColorModeToColorByVector();
502 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
504 double mVFColorHSV[3];
505 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
506 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
507 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
508 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
511 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
512 mVFMapper->SetInput(mGlyphFilter->GetOutput());
513 mVFMapper->ImmediateModeRenderingOn();
514 mVFMapper->SetLookupTable(mVFColorLUT);
517 mVFActor = vtkSmartPointer<vtkActor>::New();
518 mVFActor->SetMapper(mVFMapper);
519 mVFActor->SetPickable(0);
520 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
521 this->UpdateDisplayExtent();
522 this->GetRenderer()->AddActor(mVFActor);
525 SetTSlice(mCurrentTSlice);
528 //------------------------------------------------------------------------------
531 //------------------------------------------------------------------------------
532 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
534 mLandmarks = landmarks;
538 mCross = vtkSmartPointer<vtkCursor3D>::New();
539 mCross->SetFocalPoint(0.0,0.0,0.0);
540 mCross->SetModelBounds(-10,10,-10,10,-10,10);
545 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
546 mLandGlyph->SetSource(mCross->GetOutput());
547 mLandGlyph->SetInput(landmarks->GetOutput());
548 //mLandGlyph->SetIndexModeToScalar();
549 mLandGlyph->SetRange(0,1);
550 mLandGlyph->ScalingOff();
552 mLandGlyph->SetColorModeToColorByScalar();
555 mClipBox = vtkSmartPointer<vtkBox>::New();
557 mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
558 mLandClipper->InsideOutOn();
559 mLandClipper->SetInput(mLandGlyph->GetOutput());
560 mLandClipper->SetClipFunction(mClipBox);
563 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
564 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
565 //mLandMapper->ScalarVisibilityOff();
568 mLandActor = vtkSmartPointer<vtkActor>::New();
569 mLandActor->SetMapper(mLandMapper);
570 mLandActor->GetProperty()->SetColor(255,10,212);
571 mLandActor->SetPickable(0);
572 mLandActor->SetVisibility(true);
573 this->UpdateDisplayExtent();
574 this->GetRenderer()->AddActor(mLandActor);
577 //------------------------------------------------------------------------------
579 //------------------------------------------------------------------------------
580 //FIXME: this function leaks memory, we should fix it someday :)
581 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
583 if (actor_type == "vector") {
584 Renderer->RemoveActor(mVFActor);
593 if (actor_type == "overlay") {
594 Renderer->RemoveActor(mOverlayActor);
596 mOverlayActor = NULL;
597 mOverlayMapper = NULL;
599 if (actor_type == "fusion") {
600 Renderer->RemoveActor(mFusionActor);
603 mFusionMapper = NULL;
605 if (actor_type == "contour") {
606 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
607 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
610 //------------------------------------------------------------------------------
613 //------------------------------------------------------------------------------
614 void vvSlicer::SetVFSubSampling(int sub)
617 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
620 UpdateDisplayExtent();
623 //------------------------------------------------------------------------------
626 //------------------------------------------------------------------------------
627 void vvSlicer::SetVFScale(int scale)
631 mArrow->SetScale(mScale);
632 UpdateDisplayExtent();
635 //------------------------------------------------------------------------------
637 //------------------------------------------------------------------------------
638 void vvSlicer::SetVFWidth(int width)
642 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
643 UpdateDisplayExtent();
646 //------------------------------------------------------------------------------
649 //------------------------------------------------------------------------------
650 void vvSlicer::SetVFLog(int log)
654 mGlyphFilter->SetUseLog(mVFLog);
655 mGlyphFilter->Modified();
657 UpdateDisplayExtent();
660 //------------------------------------------------------------------------------
663 //------------------------------------------------------------------------------
664 void vvSlicer::SetTSlice(int t)
668 else if ((unsigned int)t >= mImage->GetVTKImages().size())
669 t = mImage->GetVTKImages().size() -1;
671 if (mCurrentTSlice == t) return;
674 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
675 if (mVF && mVFActor->GetVisibility()) {
676 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
677 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
679 if (mOverlay && mOverlayActor->GetVisibility()) {
680 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
681 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
683 if (mFusion && mFusionActor->GetVisibility()) {
684 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
685 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
687 if (mSurfaceCutActors.size() > 0)
688 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
689 i!=mSurfaceCutActors.end(); i++)
690 (*i)->SetTimeSlice(mCurrentTSlice);
691 UpdateDisplayExtent();
693 //------------------------------------------------------------------------------
696 //------------------------------------------------------------------------------
697 int vvSlicer::GetTSlice()
699 return mCurrentTSlice;
701 //------------------------------------------------------------------------------
703 //------------------------------------------------------------------------------
704 void vvSlicer::SetSliceOrientation(int orientation)
706 //if 2D image, force to watch in Axial View
708 this->GetInput()->GetWholeExtent(extent);
709 if (extent[5]-extent[4] <= 2)
710 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
712 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
713 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
714 vtkErrorMacro("Error - invalid slice orientation " << orientation);
718 this->SliceOrientation = orientation;
721 AdjustResliceToSliceOrientation(mFusionReslice);
724 AdjustResliceToSliceOrientation(mOverlayReslice);
727 int *range = this->GetSliceRange();
729 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
731 // Go to current cursor position
732 // double* cursorPos = GetCursorPosition();
733 // DDV(cursorPos, 3);
734 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
736 this->UpdateOrientation();
737 this->UpdateDisplayExtent();
739 if (this->Renderer && this->GetInput()) {
740 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
741 this->Renderer->ResetCamera();
742 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
747 //----------------------------------------------------------------------------
749 //------------------------------------------------------------------------------
750 // This function ensures that we sample the slices of a vtkImageReslice filter
751 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
752 // In other words, we change the grid of the reslice in the same way as the grid
753 // of the displayed image in the slicing direction.
754 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
757 double origin[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
758 double spacing[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
759 reslice->SetOutputOrigin(origin);
760 reslice->SetOutputSpacing(spacing);
761 reslice->GetOutput()->UpdateInformation();
762 reslice->GetOutput()->GetOrigin(origin);
763 reslice->GetOutput()->GetSpacing(spacing);
765 // Use similar spacing as the image in the direction SliceOrientation
766 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
768 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
769 // Step 1: from world coordinates to image coordinates
770 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
771 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
772 // Step 2: round to superior grid positionInc
773 origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
774 // Step 3: back to world coordinates
775 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
776 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
778 // Set new spacing and origin
779 reslice->SetOutputOrigin(origin);
780 reslice->SetOutputSpacing(spacing);
781 reslice->UpdateInformation();
782 reslice->GetOutput()->UpdateInformation();
784 //------------------------------------------------------------------------------
786 //----------------------------------------------------------------------------
787 int * vvSlicer::GetExtent(){
789 if (mUseReducedExtent) {
790 w_ext = mReducedExtent;
791 } else w_ext = GetInput()->GetWholeExtent();
794 //----------------------------------------------------------------------------
797 //----------------------------------------------------------------------------
798 int vvSlicer::GetOrientation()
800 return this->SliceOrientation;
802 //----------------------------------------------------------------------------
805 //----------------------------------------------------------------------------
806 void vvSlicer::UpdateDisplayExtent()
808 vtkImageData *input = this->GetInput();
809 if (!input || !this->ImageActor) {
812 input->UpdateInformation();
814 // Local copy of extent
816 copyExtent(GetExtent(), w_ext);
818 w_ext[ this->SliceOrientation*2 ] = this->Slice;
819 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
822 this->ImageActor->SetDisplayExtent(w_ext);
824 // Overlay image actor
825 if (mOverlay && mOverlayActor->GetVisibility()) {
826 AdjustResliceToSliceOrientation(mOverlayReslice);
828 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
829 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
830 mOverlayActor->SetDisplayExtent( overExtent );
833 // Fusion image actor
834 if (mFusion && mFusionActor->GetVisibility()) {
835 AdjustResliceToSliceOrientation(mFusionReslice);
837 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
838 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
839 mFusionActor->SetDisplayExtent(fusExtent);
842 // Vector field actor
843 double* camera = Renderer->GetActiveCamera()->GetPosition();
844 double* image_bounds = ImageActor->GetBounds();
845 double position[3] = {0, 0, 0};
846 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
848 //print_vector<double, 6>("camera", camera);
849 //print_vector<double, 6>("image_bounds", image_bounds);
850 //print_vector<double, 3>("position", position);
852 // find where to place the VF actor. to deal with
853 // z-buffer issues, the VF is placed right in front of the image,
854 // subject to a small offset. the position actually depends on the
855 // the location of the camera relative to the image.
857 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
860 if (mVF && mVFActor->GetVisibility()) {
862 mVF->GetVTKImages()[0]->UpdateInformation();
863 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
864 ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
865 mVOIFilter->SetVOI(vfExtent);
866 int orientation[3] = {1,1,1};
867 orientation[this->SliceOrientation] = 0;
868 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
871 position[this->SliceOrientation] += offset;
872 mVFActor->SetPosition(position);
879 for(unsigned int i=0; i<6; i++)
880 bounds[i] = ImageActor->GetBounds()[i];
881 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
882 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
883 mClipBox->SetBounds(bounds);
887 position[this->SliceOrientation] = offset;
888 mLandActor->SetPosition(position);
891 // Figure out the correct clipping range
892 if (this->Renderer) {
893 if (this->InteractorStyle &&
894 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
895 this->Renderer->ResetCameraClippingRange();
897 vtkCamera *cam = this->Renderer->GetActiveCamera();
900 this->ImageActor->GetBounds(bounds);
901 double spos = (double)bounds[this->SliceOrientation * 2];
902 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
903 double range = fabs(spos - cpos);
904 double *spacing = input->GetSpacing();
906 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
907 cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
913 //----------------------------------------------------------------------------
915 //----------------------------------------------------------------------------
916 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
917 vtkImageData *targetImage, int targetExtent[6])
920 for(unsigned int i=0; i<6; i++) {
921 // From source voxel coordinates to world coordinates
922 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
924 // From world coordinates to floating point target voxel coordinates
925 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
928 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
931 //----------------------------------------------------------------------------
933 //----------------------------------------------------------------------------
934 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
939 //2D overlay on 3D image specific case
940 if (refExtent[4] == refExtent[5]) {
942 extent[4] = refExtent[4];
943 extent[5] = refExtent[5];
946 for (int i = 0; i < maxBound; i = i+2) {
947 //if we are totally outside the image
948 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
952 //crop to the limit of the image
953 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
954 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
955 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
956 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
959 for (int i = 0; i < maxBound; i = i+2) {
960 extent[i] = refExtent[i];
961 extent[i+1] = refExtent[i];
964 //----------------------------------------------------------------------------
967 //----------------------------------------------------------------------------
968 void vvSlicer::UpdateOrientation()
970 // Set the camera position
971 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
973 switch (this->SliceOrientation) {
974 case vtkImageViewer2::SLICE_ORIENTATION_XY:
975 cam->SetFocalPoint(0,0,0);
976 cam->SetPosition(0,0,-1); // -1 if medical ?
977 cam->SetViewUp(0,-1,0);
980 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
981 cam->SetFocalPoint(0,0,0);
982 cam->SetPosition(0,-1,0); // 1 if medical ?
983 cam->SetViewUp(0,0,1);
986 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
987 cam->SetFocalPoint(0,0,0);
988 cam->SetPosition(-1,0,0); // -1 if medical ?
989 cam->SetViewUp(0,0,1);
994 //----------------------------------------------------------------------------
997 //----------------------------------------------------------------------------
998 void vvSlicer::SetOpacity(double s)
1000 this->GetImageActor()->SetOpacity(s);
1002 //----------------------------------------------------------------------------
1005 //----------------------------------------------------------------------------
1006 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1008 this->Superclass::SetRenderWindow(rw);
1009 this->SetupInteractor(rw->GetInteractor());
1010 ca->SetImageActor(this->GetImageActor());
1011 ca->SetWindowLevel(this->GetWindowLevel());
1012 ca->SetText(2, "<slice>");
1013 ca->SetText(3, "<window>\n<level>");
1025 crossCursor->SetModelBounds(bounds);
1026 this->GetRenderer()->AddActor(pdmA);
1027 this->GetRenderer()->AddActor(ca);
1028 this->GetRenderer()->ResetCamera();
1030 //this is just a mapping between the labeling of the orientations presented to the user and
1031 //the one used by vtk
1032 SetSliceOrientation(2-(orientation%3));
1035 //----------------------------------------------------------------------------
1038 //----------------------------------------------------------------------------
1039 void vvSlicer::ResetCamera()
1041 this->GetRenderer()->ResetCamera();
1043 //----------------------------------------------------------------------------
1046 //----------------------------------------------------------------------------
1047 void vvSlicer::SetDisplayMode(bool i)
1049 this->GetRenderer()->SetDraw(i);
1051 UpdateDisplayExtent();
1053 //----------------------------------------------------------------------------
1056 //----------------------------------------------------------------------------
1057 void vvSlicer::FlipHorizontalView()
1059 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1061 double *position = cam->GetPosition();
1062 double factor[3] = {1, 1, 1};
1063 factor[this->SliceOrientation] = -1;
1064 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1066 /* switch (this->SliceOrientation) {
1067 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1068 cam->SetPosition(position[0],position[1],-position[2]);
1071 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1072 cam->SetPosition(position[0],-position[1],position[2]);
1075 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1076 cam->SetPosition(-position[0],position[1],position[2]);
1080 this->Renderer->ResetCameraClippingRange();
1081 this->UpdateDisplayExtent();
1084 //----------------------------------------------------------------------------
1087 //----------------------------------------------------------------------------
1088 void vvSlicer::FlipVerticalView()
1090 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1092 FlipHorizontalView();
1093 double *viewup = cam->GetViewUp();
1094 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1095 this->UpdateDisplayExtent();
1098 //----------------------------------------------------------------------------
1101 //----------------------------------------------------------------------------
1102 void vvSlicer::SetColorWindow(double window)
1104 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1106 double level = this->GetWindowLevel()->GetLevel();
1107 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1110 this->vtkImageViewer2::SetColorWindow(window);
1112 //----------------------------------------------------------------------------
1115 //----------------------------------------------------------------------------
1116 void vvSlicer::SetColorLevel(double level)
1118 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1120 double window = this->GetWindowLevel()->GetWindow();
1121 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1124 this->vtkImageViewer2::SetColorLevel(level);
1126 //----------------------------------------------------------------------------
1128 //----------------------------------------------------------------------------
1129 // Returns the min an the max value in a 41x41 region around the mouse pointer
1130 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1132 //Get mouse pointer position in view coordinates
1133 double fLocalExtents[6];
1134 for(int i=0; i<3; i++) {
1135 fLocalExtents[i*2 ] = mCurrent[i];
1136 fLocalExtents[i*2+1] = mCurrent[i];
1138 this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1139 this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1140 for(int i=0; i<3; i++) {
1141 if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1142 fLocalExtents[i*2 ] -= 0.2;
1143 fLocalExtents[i*2+1] += 0.2;
1146 this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1147 this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1149 //Convert to image pixel coordinates (rounded)
1150 int iLocalExtents[6];
1151 for(int i=0; i<3; i++) {
1152 fLocalExtents[i*2 ] = (fLocalExtents[i*2 ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1153 fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1155 iLocalExtents[i*2 ] = lrint(fLocalExtents[i*2 ]);
1156 iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1158 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1159 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1162 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1163 voiFilter->SetInput(this->GetInput());
1164 voiFilter->SetVOI(iLocalExtents);
1165 voiFilter->Update();
1166 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1172 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1173 accFilter->SetInput(voiFilter->GetOutput());
1174 accFilter->Update();
1176 min = *(accFilter->GetMin());
1177 max = *(accFilter->GetMax());
1179 //----------------------------------------------------------------------------
1181 //----------------------------------------------------------------------------
1182 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1187 if (ix < image->GetWholeExtent()[0] ||
1188 ix > image->GetWholeExtent()[1] ||
1189 iy < image->GetWholeExtent()[2] ||
1190 iy > image->GetWholeExtent()[3] ||
1191 iz < image->GetWholeExtent()[4] ||
1192 iz > image->GetWholeExtent()[5] )
1193 return std::numeric_limits<double>::quiet_NaN();
1195 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1197 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1199 //----------------------------------------------------------------------------
1201 //----------------------------------------------------------------------------
1202 void vvSlicer::Render()
1204 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1205 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1206 legend->SetVisibility(1);
1207 } else legend->SetVisibility(0);
1209 if (ca->GetVisibility()) {
1210 std::stringstream worldPos;
1211 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1212 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1213 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1215 // if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1216 // else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1];
1217 // if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1218 // else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3];
1219 // if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1220 // else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5];
1222 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1223 X <= this->GetInput()->GetWholeExtent()[1] &&
1224 Y >= this->GetInput()->GetWholeExtent()[2] &&
1225 Y <= this->GetInput()->GetWholeExtent()[3] &&
1226 Z >= this->GetInput()->GetWholeExtent()[4] &&
1227 Z <= this->GetInput()->GetWholeExtent()[5]) {
1231 double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1233 if(ImageActor->GetVisibility())
1234 worldPos << "data value : " << value << std::endl;
1236 worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1237 << lrint(mCurrent[1]) << ' '
1238 << lrint(mCurrent[2]) << ' '
1241 worldPos << "pixel : " << ix << ' '
1247 ca->SetText(1,worldPos.str().c_str());
1250 if (pdmA->GetVisibility()) {
1251 double x = mCursor[0];
1252 double y = mCursor[1];
1253 double z = mCursor[2];
1254 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1255 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1256 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1258 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1259 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1260 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1261 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1262 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1263 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1264 vtkRenderer * renderer = this->Renderer;
1266 renderer->WorldToView(x,y,z);
1267 renderer->ViewToNormalizedViewport(x,y,z);
1268 renderer->NormalizedViewportToViewport(x,y);
1269 renderer->ViewportToNormalizedDisplay(x,y);
1270 renderer->NormalizedDisplayToDisplay(x,y);
1271 crossCursor->SetFocalPoint(x,y,z);
1273 crossCursor->SetFocalPoint(-1,-1,z);
1277 if (mOverlay && mOverlayActor->GetVisibility()) {
1278 mOverlayMapper->SetWindow(this->GetColorWindow());
1279 mOverlayMapper->SetLevel(this->GetColorLevel());
1280 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1281 mOverlayMapper->GetOutput()->Update();
1282 mOverlayMapper->Update();
1287 this->GetRenderWindow()->Render();
1289 //----------------------------------------------------------------------------
1292 //----------------------------------------------------------------------------
1293 void vvSlicer::UpdateCursorPosition()
1295 pdmA->SetVisibility(true);
1296 mCursor[0] = mCurrent[0];
1297 mCursor[1] = mCurrent[1];
1298 mCursor[2] = mCurrent[2];
1299 mCursor[3] = mCurrentTSlice;
1301 //----------------------------------------------------------------------------
1304 //----------------------------------------------------------------------------
1305 void vvSlicer::UpdateLandmarks()
1307 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1308 if (pd->GetPoints()) {
1309 mLandGlyph->SetRange(0,1);
1310 mLandGlyph->Modified();
1311 mLandGlyph->Update();
1313 mClipBox->Modified();
1314 mLandClipper->Update();
1315 mLandMapper->Update();
1319 //----------------------------------------------------------------------------
1322 //----------------------------------------------------------------------------
1323 void vvSlicer::SetSlice(int slice)
1325 int *range = this->GetSliceRange();
1327 if (slice < range[0]) {
1329 } else if (slice > range[1]) {
1334 if (this->Slice == slice) {
1338 this->Slice = slice;
1341 this->UpdateDisplayExtent();
1343 // Seems to work without this line
1346 //----------------------------------------------------------------------------
1349 //----------------------------------------------------------------------------
1350 void vvSlicer::SetContourSlice()
1352 if (mSurfaceCutActors.size() > 0)
1353 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1354 i!=mSurfaceCutActors.end(); i++) {
1356 (*i)->SetSlicingOrientation(this->SliceOrientation);
1357 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1358 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1361 //----------------------------------------------------------------------------
1364 //----------------------------------------------------------------------------
1365 void vvSlicer::ForceUpdateDisplayExtent()
1367 this->UpdateDisplayExtent();
1369 //----------------------------------------------------------------------------
1372 //----------------------------------------------------------------------------
1373 int* vvSlicer::GetDisplayExtent()
1375 return this->GetImageActor()->GetDisplayExtent();
1377 //----------------------------------------------------------------------------
1380 //----------------------------------------------------------------------------
1381 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1383 this->Superclass::PrintSelf(os, indent);
1385 //----------------------------------------------------------------------------
1387 //----------------------------------------------------------------------------
1388 void vvSlicer::SetVFColor(double r, double g, double b)
1390 double mVFColorHSV[3];
1395 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1396 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1397 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1398 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);