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 this->SetSliceOrientation(this->SliceOrientation);
373 this->SetTSlice(mCurrentTSlice);
376 //------------------------------------------------------------------------------
379 //------------------------------------------------------------------------------
380 void vvSlicer::SetFusion(vvImage::Pointer fusion)
382 if (fusion->GetVTKImages().size()) {
385 if (!mFusionReslice) {
386 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
387 mFusionReslice->SetInterpolationModeToLinear();
388 mFusionReslice->AutoCropOutputOn();
389 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
391 mFusionReslice->SetResliceTransform(mFusion->GetTransform());
392 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
395 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
397 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
399 lut->SetValueRange(0, 1);
400 lut->SetSaturationRange(0, 0);
402 mFusionMapper->SetLookupTable(lut);
403 mFusionMapper->SetInput(mFusionReslice->GetOutput());
406 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
407 mFusionActor->SetInput(mFusionMapper->GetOutput());
408 mFusionActor->SetPickable(0);
409 mFusionActor->SetVisibility(true);
410 mFusionActor->SetOpacity(0.7);
411 this->GetRenderer()->AddActor(mFusionActor);
414 //Synchronize orientation and slice
415 this->SetSliceOrientation(this->SliceOrientation);
416 this->SetTSlice(mCurrentTSlice);
419 //------------------------------------------------------------------------------
422 //------------------------------------------------------------------------------
423 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
426 if (actor_type == "image") {
427 vis = this->ImageActor->GetVisibility();
429 else if (actor_type == "vector") {
430 vis = this->mVFActor->GetVisibility();
432 else if (actor_type == "overlay") {
433 vis = this->mOverlayActor->GetVisibility();
435 else if (actor_type == "fusion") {
436 vis = this->mFusionActor->GetVisibility();
438 else if (actor_type == "contour")
439 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
443 //------------------------------------------------------------------------------
445 //------------------------------------------------------------------------------
446 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
448 if (actor_type == "image") {
449 this->ImageActor->SetVisibility(vis);
451 else if (actor_type == "vector") {
452 this->mVFActor->SetVisibility(vis);
454 else if (actor_type == "overlay") {
455 this->mOverlayActor->SetVisibility(vis);
457 else if (actor_type == "fusion") {
458 this->mFusionActor->SetVisibility(vis);
460 else if (actor_type == "contour")
461 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
462 UpdateDisplayExtent();
464 //------------------------------------------------------------------------------
466 //------------------------------------------------------------------------------
467 void vvSlicer::SetVF(vvImage::Pointer vf)
469 if (vf->GetVTKImages().size()) {
473 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
474 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
475 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
477 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
478 mAAFilter->SetInput(mVOIFilter->GetOutput());
479 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
480 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
483 mArrow = vtkSmartPointer<vvGlyphSource>::New();
484 mArrow->SetGlyphTypeToSpecificArrow();
485 mArrow->SetScale(mScale);
488 // Glyph the gradient vector (with arrows)
490 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
491 mGlyphFilter->SetInput(mAAFilter->GetOutput());
492 mGlyphFilter->SetSource(mArrow->GetOutput());
493 mGlyphFilter->ScalingOn();
494 mGlyphFilter->SetScaleModeToScaleByVector();
495 mGlyphFilter->OrientOn();
496 mGlyphFilter->SetVectorModeToUseVector();
497 mGlyphFilter->SetColorModeToColorByVector();
500 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
502 double mVFColorHSV[3];
503 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
504 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
505 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
506 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
509 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
510 mVFMapper->SetInput(mGlyphFilter->GetOutput());
511 mVFMapper->ImmediateModeRenderingOn();
512 mVFMapper->SetLookupTable(mVFColorLUT);
515 mVFActor = vtkSmartPointer<vtkActor>::New();
516 mVFActor->SetMapper(mVFMapper);
517 mVFActor->SetPickable(0);
518 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
519 this->UpdateDisplayExtent();
520 this->GetRenderer()->AddActor(mVFActor);
523 SetTSlice(mCurrentTSlice);
526 //------------------------------------------------------------------------------
529 //------------------------------------------------------------------------------
530 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
532 mLandmarks = landmarks;
536 mCross = vtkSmartPointer<vtkCursor3D>::New();
537 mCross->SetFocalPoint(0.0,0.0,0.0);
538 mCross->SetModelBounds(-10,10,-10,10,-10,10);
543 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
544 mLandGlyph->SetSource(mCross->GetOutput());
545 mLandGlyph->SetInput(landmarks->GetOutput());
546 //mLandGlyph->SetIndexModeToScalar();
547 mLandGlyph->SetRange(0,1);
548 mLandGlyph->ScalingOff();
550 mLandGlyph->SetColorModeToColorByScalar();
553 mClipBox = vtkSmartPointer<vtkBox>::New();
555 mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
556 mLandClipper->InsideOutOn();
557 mLandClipper->SetInput(mLandGlyph->GetOutput());
558 mLandClipper->SetClipFunction(mClipBox);
561 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
562 mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
563 //mLandMapper->ScalarVisibilityOff();
566 mLandActor = vtkSmartPointer<vtkActor>::New();
567 mLandActor->SetMapper(mLandMapper);
568 mLandActor->GetProperty()->SetColor(255,10,212);
569 mLandActor->SetPickable(0);
570 mLandActor->SetVisibility(true);
571 this->UpdateDisplayExtent();
572 this->GetRenderer()->AddActor(mLandActor);
575 //------------------------------------------------------------------------------
577 //------------------------------------------------------------------------------
578 //FIXME: this function leaks memory, we should fix it someday :)
579 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
581 if (actor_type == "vector") {
582 Renderer->RemoveActor(mVFActor);
591 if (actor_type == "overlay") {
592 Renderer->RemoveActor(mOverlayActor);
594 mOverlayActor = NULL;
595 mOverlayMapper = NULL;
597 if (actor_type == "fusion") {
598 Renderer->RemoveActor(mFusionActor);
601 mFusionMapper = NULL;
603 if (actor_type == "contour") {
604 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
605 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
608 //------------------------------------------------------------------------------
611 //------------------------------------------------------------------------------
612 void vvSlicer::SetVFSubSampling(int sub)
615 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
618 UpdateDisplayExtent();
621 //------------------------------------------------------------------------------
624 //------------------------------------------------------------------------------
625 void vvSlicer::SetVFScale(int scale)
629 mArrow->SetScale(mScale);
630 UpdateDisplayExtent();
633 //------------------------------------------------------------------------------
635 //------------------------------------------------------------------------------
636 void vvSlicer::SetVFWidth(int width)
640 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
641 UpdateDisplayExtent();
644 //------------------------------------------------------------------------------
647 //------------------------------------------------------------------------------
648 void vvSlicer::SetVFLog(int log)
652 mGlyphFilter->SetUseLog(mVFLog);
653 mGlyphFilter->Modified();
655 UpdateDisplayExtent();
658 //------------------------------------------------------------------------------
661 //------------------------------------------------------------------------------
662 void vvSlicer::SetTSlice(int t)
666 else if ((unsigned int)t >= mImage->GetVTKImages().size())
667 t = mImage->GetVTKImages().size() -1;
669 if (mCurrentTSlice == t) return;
672 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
673 if (mVF && mVFActor->GetVisibility()) {
674 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
675 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
677 if (mOverlay && mOverlayActor->GetVisibility()) {
678 if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
679 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
681 if (mFusion && mFusionActor->GetVisibility()) {
682 if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
683 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
685 if (mSurfaceCutActors.size() > 0)
686 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
687 i!=mSurfaceCutActors.end(); i++)
688 (*i)->SetTimeSlice(mCurrentTSlice);
689 UpdateDisplayExtent();
691 //------------------------------------------------------------------------------
694 //------------------------------------------------------------------------------
695 int vvSlicer::GetTSlice()
697 return mCurrentTSlice;
699 //------------------------------------------------------------------------------
701 //------------------------------------------------------------------------------
702 void vvSlicer::SetSliceOrientation(int orientation)
704 //if 2D image, force to watch in Axial View
706 this->GetInput()->GetWholeExtent(extent);
707 if (extent[5]-extent[4] <= 2)
708 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
710 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
711 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
712 vtkErrorMacro("Error - invalid slice orientation " << orientation);
716 this->SliceOrientation = orientation;
719 AdjustResliceToSliceOrientation(mFusionReslice);
722 AdjustResliceToSliceOrientation(mOverlayReslice);
725 int *range = this->GetSliceRange();
727 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
729 // Go to current cursor position
730 // double* cursorPos = GetCursorPosition();
731 // DDV(cursorPos, 3);
732 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
734 this->UpdateOrientation();
735 this->UpdateDisplayExtent();
737 if (this->Renderer && this->GetInput()) {
738 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
739 this->Renderer->ResetCamera();
740 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
745 //----------------------------------------------------------------------------
747 //------------------------------------------------------------------------------
748 // This function ensures that we sample the slices of a vtkImageReslice filter
749 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
750 // In other words, we change the grid of the reslice in the same way as the grid
751 // of the displayed image in the slicing direction.
752 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
755 double origin[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
756 double spacing[3] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MAX, VTK_DOUBLE_MAX};
757 reslice->SetOutputOrigin(origin);
758 reslice->SetOutputSpacing(spacing);
759 reslice->GetOutput()->UpdateInformation();
760 reslice->GetOutput()->GetOrigin(origin);
761 reslice->GetOutput()->GetSpacing(spacing);
763 // Use similar spacing as the image in the direction SliceOrientation
764 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
766 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
767 // Step 1: from world coordinates to image coordinates
768 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
769 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
770 // Step 2: round to superior grid positionInc
771 origin[this->SliceOrientation] = itk::Math::Ceil<double>(origin[this->SliceOrientation]);
772 // Step 3: back to world coordinates
773 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
774 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
776 // Set new spacing and origin
777 reslice->SetOutputOrigin(origin);
778 reslice->SetOutputSpacing(spacing);
779 reslice->UpdateInformation();
781 //------------------------------------------------------------------------------
783 //----------------------------------------------------------------------------
784 int * vvSlicer::GetExtent(){
786 if (mUseReducedExtent) {
787 w_ext = mReducedExtent;
788 } else w_ext = GetInput()->GetWholeExtent();
791 //----------------------------------------------------------------------------
794 //----------------------------------------------------------------------------
795 int vvSlicer::GetOrientation()
797 return this->SliceOrientation;
799 //----------------------------------------------------------------------------
802 //----------------------------------------------------------------------------
803 void vvSlicer::UpdateDisplayExtent()
805 vtkImageData *input = this->GetInput();
806 if (!input || !this->ImageActor) {
809 input->UpdateInformation();
811 // Local copy of extent
813 copyExtent(GetExtent(), w_ext);
815 w_ext[ this->SliceOrientation*2 ] = this->Slice;
816 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
819 this->ImageActor->SetDisplayExtent(w_ext);
821 // Overlay image actor
822 if (mOverlay && mOverlayActor->GetVisibility()) {
823 AdjustResliceToSliceOrientation(mOverlayReslice);
825 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
826 ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
827 mOverlayActor->SetDisplayExtent( overExtent );
830 // Fusion image actor
831 if (mFusion && mFusionActor->GetVisibility()) {
832 AdjustResliceToSliceOrientation(mFusionReslice);
834 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
835 ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
836 mFusionActor->SetDisplayExtent(fusExtent);
839 // Vector field actor
840 double* camera = Renderer->GetActiveCamera()->GetPosition();
841 double* image_bounds = ImageActor->GetBounds();
842 double position[3] = {0, 0, 0};
843 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
845 //print_vector<double, 6>("camera", camera);
846 //print_vector<double, 6>("image_bounds", image_bounds);
847 //print_vector<double, 3>("position", position);
849 // find where to place the VF actor. to deal with
850 // z-buffer issues, the VF is placed right in front of the image,
851 // subject to a small offset. the position actually depends on the
852 // the location of the camera relative to the image.
854 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
857 if (mVF && mVFActor->GetVisibility()) {
859 mVF->GetVTKImages()[0]->UpdateInformation();
860 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
861 ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
862 mVOIFilter->SetVOI(vfExtent);
863 int orientation[3] = {1,1,1};
864 orientation[this->SliceOrientation] = 0;
865 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
868 position[this->SliceOrientation] += offset;
869 mVFActor->SetPosition(position);
876 for(unsigned int i=0; i<6; i++)
877 bounds[i] = ImageActor->GetBounds()[i];
878 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
879 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
880 mClipBox->SetBounds(bounds);
884 position[this->SliceOrientation] = offset;
885 mLandActor->SetPosition(position);
888 // Figure out the correct clipping range
889 if (this->Renderer) {
890 if (this->InteractorStyle &&
891 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
892 this->Renderer->ResetCameraClippingRange();
894 vtkCamera *cam = this->Renderer->GetActiveCamera();
897 this->ImageActor->GetBounds(bounds);
898 double spos = (double)bounds[this->SliceOrientation * 2];
899 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
900 double range = fabs(spos - cpos);
901 double *spacing = input->GetSpacing();
903 ((double)spacing[0] + (double)spacing[1] + (double)spacing[2]) / 3.0;
904 cam->SetClippingRange(range - avg_spacing * 3.0, range + avg_spacing * 3.0);
910 //----------------------------------------------------------------------------
912 //----------------------------------------------------------------------------
913 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
914 vtkImageData *targetImage, int targetExtent[6])
917 for(unsigned int i=0; i<6; i++) {
918 // From source voxel coordinates to world coordinates
919 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
921 // From world coordinates to floating point target voxel coordinates
922 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
925 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
928 //----------------------------------------------------------------------------
930 //----------------------------------------------------------------------------
931 void vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
936 //2D overlay on 3D image specific case
937 if (refExtent[4] == refExtent[5]) {
939 extent[4] = refExtent[4];
940 extent[5] = refExtent[5];
943 for (int i = 0; i < maxBound; i = i+2) {
944 //if we are totally outside the image
945 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
949 //crop to the limit of the image
950 extent[i] = (extent[i] > refExtent[i]) ? extent[i] : refExtent[i];
951 extent[i] = (extent[i] < refExtent[i+1]) ? extent[i] : refExtent[i+1];
952 extent[i+1] = (extent[i+1] > refExtent[i]) ? extent[i+1] : refExtent[i];
953 extent[i+1] = (extent[i+1] < refExtent[i+1]) ? extent[i+1] : refExtent[i+1];
956 for (int i = 0; i < maxBound; i = i+2) {
957 extent[i] = refExtent[i];
958 extent[i+1] = refExtent[i];
961 //----------------------------------------------------------------------------
964 //----------------------------------------------------------------------------
965 void vvSlicer::UpdateOrientation()
967 // Set the camera position
968 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
970 switch (this->SliceOrientation) {
971 case vtkImageViewer2::SLICE_ORIENTATION_XY:
972 cam->SetFocalPoint(0,0,0);
973 cam->SetPosition(0,0,-1); // -1 if medical ?
974 cam->SetViewUp(0,-1,0);
977 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
978 cam->SetFocalPoint(0,0,0);
979 cam->SetPosition(0,-1,0); // 1 if medical ?
980 cam->SetViewUp(0,0,1);
983 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
984 cam->SetFocalPoint(0,0,0);
985 cam->SetPosition(-1,0,0); // -1 if medical ?
986 cam->SetViewUp(0,0,1);
991 //----------------------------------------------------------------------------
994 //----------------------------------------------------------------------------
995 void vvSlicer::SetOpacity(double s)
997 this->GetImageActor()->SetOpacity(s);
999 //----------------------------------------------------------------------------
1002 //----------------------------------------------------------------------------
1003 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1005 this->Superclass::SetRenderWindow(rw);
1006 this->SetupInteractor(rw->GetInteractor());
1007 ca->SetImageActor(this->GetImageActor());
1008 ca->SetWindowLevel(this->GetWindowLevel());
1009 ca->SetText(2, "<slice>");
1010 ca->SetText(3, "<window>\n<level>");
1022 crossCursor->SetModelBounds(bounds);
1023 this->GetRenderer()->AddActor(pdmA);
1024 this->GetRenderer()->AddActor(ca);
1025 this->GetRenderer()->ResetCamera();
1027 //this is just a mapping between the labeling of the orientations presented to the user and
1028 //the one used by vtk
1029 SetSliceOrientation(2-(orientation%3));
1032 //----------------------------------------------------------------------------
1035 //----------------------------------------------------------------------------
1036 void vvSlicer::ResetCamera()
1038 this->GetRenderer()->ResetCamera();
1040 //----------------------------------------------------------------------------
1043 //----------------------------------------------------------------------------
1044 void vvSlicer::SetDisplayMode(bool i)
1046 this->GetRenderer()->SetDraw(i);
1048 UpdateDisplayExtent();
1050 //----------------------------------------------------------------------------
1053 //----------------------------------------------------------------------------
1054 void vvSlicer::FlipHorizontalView()
1056 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1058 double *position = cam->GetPosition();
1059 double factor[3] = {1, 1, 1};
1060 factor[this->SliceOrientation] = -1;
1061 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1063 /* switch (this->SliceOrientation) {
1064 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1065 cam->SetPosition(position[0],position[1],-position[2]);
1068 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1069 cam->SetPosition(position[0],-position[1],position[2]);
1072 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1073 cam->SetPosition(-position[0],position[1],position[2]);
1077 this->Renderer->ResetCameraClippingRange();
1078 this->UpdateDisplayExtent();
1081 //----------------------------------------------------------------------------
1084 //----------------------------------------------------------------------------
1085 void vvSlicer::FlipVerticalView()
1087 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1089 FlipHorizontalView();
1090 double *viewup = cam->GetViewUp();
1091 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1092 this->UpdateDisplayExtent();
1095 //----------------------------------------------------------------------------
1098 //----------------------------------------------------------------------------
1099 void vvSlicer::SetColorWindow(double window)
1101 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1103 double level = this->GetWindowLevel()->GetLevel();
1104 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1107 this->vtkImageViewer2::SetColorWindow(window);
1109 //----------------------------------------------------------------------------
1112 //----------------------------------------------------------------------------
1113 void vvSlicer::SetColorLevel(double level)
1115 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1117 double window = this->GetWindowLevel()->GetWindow();
1118 LUT->SetTableRange(level-fabs(window)/4,level+fabs(window)/4);
1121 this->vtkImageViewer2::SetColorLevel(level);
1123 //----------------------------------------------------------------------------
1125 //----------------------------------------------------------------------------
1126 // Returns the min an the max value in a 41x41 region around the mouse pointer
1127 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max)
1129 //Get mouse pointer position in view coordinates
1130 double fLocalExtents[6];
1131 for(int i=0; i<3; i++) {
1132 fLocalExtents[i*2 ] = mCurrent[i];
1133 fLocalExtents[i*2+1] = mCurrent[i];
1135 this->Renderer->WorldToView(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1136 this->Renderer->WorldToView(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1137 for(int i=0; i<3; i++) {
1138 if (i!=SliceOrientation) { //SR: assumes that SliceOrientation is valid in ViewCoordinates (???)
1139 fLocalExtents[i*2 ] -= 0.2;
1140 fLocalExtents[i*2+1] += 0.2;
1143 this->Renderer->ViewToWorld(fLocalExtents[0], fLocalExtents[2], fLocalExtents[4]);
1144 this->Renderer->ViewToWorld(fLocalExtents[1], fLocalExtents[3], fLocalExtents[5]);
1146 //Convert to image pixel coordinates (rounded)
1147 int iLocalExtents[6];
1148 for(int i=0; i<3; i++) {
1149 fLocalExtents[i*2 ] = (fLocalExtents[i*2 ] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1150 fLocalExtents[i*2+1] = (fLocalExtents[i*2+1] - this->GetInput()->GetOrigin()[i])/this->GetInput()->GetSpacing()[i];
1152 iLocalExtents[i*2 ] = lrint(fLocalExtents[i*2 ]);
1153 iLocalExtents[i*2+1] = lrint(fLocalExtents[i*2+1]);
1155 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1156 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1159 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1160 voiFilter->SetInput(this->GetInput());
1161 voiFilter->SetVOI(iLocalExtents);
1162 voiFilter->Update();
1163 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1169 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1170 accFilter->SetInput(voiFilter->GetOutput());
1171 accFilter->Update();
1173 min = *(accFilter->GetMin());
1174 max = *(accFilter->GetMax());
1176 //----------------------------------------------------------------------------
1178 //----------------------------------------------------------------------------
1179 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1184 if (ix < image->GetWholeExtent()[0] ||
1185 ix > image->GetWholeExtent()[1] ||
1186 iy < image->GetWholeExtent()[2] ||
1187 iy > image->GetWholeExtent()[3] ||
1188 iz < image->GetWholeExtent()[4] ||
1189 iz > image->GetWholeExtent()[5] )
1190 return std::numeric_limits<double>::quiet_NaN();
1192 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1194 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1196 //----------------------------------------------------------------------------
1198 //----------------------------------------------------------------------------
1199 void vvSlicer::Render()
1201 if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay && !this->mFusion) {
1202 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1203 legend->SetVisibility(1);
1204 } else legend->SetVisibility(0);
1206 if (ca->GetVisibility()) {
1207 std::stringstream worldPos;
1208 double X = (mCurrent[0] - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1209 double Y = (mCurrent[1] - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1210 double Z = (mCurrent[2] - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1212 // if (X < this->GetInput()->GetWholeExtent()[0]) X = this->GetInput()->GetWholeExtent()[0];
1213 // else if (X > this->GetInput()->GetWholeExtent()[1]) X = this->GetInput()->GetWholeExtent()[1];
1214 // if (Y < this->GetInput()->GetWholeExtent()[2]) Y = this->GetInput()->GetWholeExtent()[2];
1215 // else if (Y > this->GetInput()->GetWholeExtent()[3]) Y = this->GetInput()->GetWholeExtent()[3];
1216 // if (Z < this->GetInput()->GetWholeExtent()[4]) Z = this->GetInput()->GetWholeExtent()[4];
1217 // else if (Z > this->GetInput()->GetWholeExtent()[5]) Z = this->GetInput()->GetWholeExtent()[5];
1219 if (X >= this->GetInput()->GetWholeExtent()[0] &&
1220 X <= this->GetInput()->GetWholeExtent()[1] &&
1221 Y >= this->GetInput()->GetWholeExtent()[2] &&
1222 Y <= this->GetInput()->GetWholeExtent()[3] &&
1223 Z >= this->GetInput()->GetWholeExtent()[4] &&
1224 Z <= this->GetInput()->GetWholeExtent()[5]) {
1228 double value = this->GetScalarComponentAsDouble(this->GetInput(), X, Y, Z, ix, iy, iz);
1230 if(ImageActor->GetVisibility())
1231 worldPos << "data value : " << value << std::endl;
1233 worldPos << "mm : " << lrint(mCurrent[0]) << ' '
1234 << lrint(mCurrent[1]) << ' '
1235 << lrint(mCurrent[2]) << ' '
1238 worldPos << "pixel : " << ix << ' '
1244 ca->SetText(1,worldPos.str().c_str());
1247 if (pdmA->GetVisibility()) {
1248 double x = mCursor[0];
1249 double y = mCursor[1];
1250 double z = mCursor[2];
1251 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1252 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1253 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1255 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0] &&
1256 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+1 &&
1257 yCursor >= this->GetImageActor()->GetDisplayExtent()[2] &&
1258 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+1 &&
1259 zCursor >= this->GetImageActor()->GetDisplayExtent()[4] &&
1260 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+1 ) {
1261 vtkRenderer * renderer = this->Renderer;
1263 renderer->WorldToView(x,y,z);
1264 renderer->ViewToNormalizedViewport(x,y,z);
1265 renderer->NormalizedViewportToViewport(x,y);
1266 renderer->ViewportToNormalizedDisplay(x,y);
1267 renderer->NormalizedDisplayToDisplay(x,y);
1268 crossCursor->SetFocalPoint(x,y,z);
1270 crossCursor->SetFocalPoint(-1,-1,z);
1274 if (mOverlay && mOverlayActor->GetVisibility()) {
1275 mOverlayMapper->SetWindow(this->GetColorWindow());
1276 mOverlayMapper->SetLevel(this->GetColorLevel());
1277 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1278 mOverlayMapper->GetOutput()->Update();
1279 mOverlayMapper->Update();
1284 this->GetRenderWindow()->Render();
1286 //----------------------------------------------------------------------------
1289 //----------------------------------------------------------------------------
1290 void vvSlicer::UpdateCursorPosition()
1292 pdmA->SetVisibility(true);
1293 mCursor[0] = mCurrent[0];
1294 mCursor[1] = mCurrent[1];
1295 mCursor[2] = mCurrent[2];
1296 mCursor[3] = mCurrentTSlice;
1298 //----------------------------------------------------------------------------
1301 //----------------------------------------------------------------------------
1302 void vvSlicer::UpdateLandmarks()
1304 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1305 if (pd->GetPoints()) {
1306 mLandGlyph->SetRange(0,1);
1307 mLandGlyph->Modified();
1308 mLandGlyph->Update();
1310 mClipBox->Modified();
1311 mLandClipper->Update();
1312 mLandMapper->Update();
1316 //----------------------------------------------------------------------------
1319 //----------------------------------------------------------------------------
1320 void vvSlicer::SetSlice(int slice)
1322 int *range = this->GetSliceRange();
1324 if (slice < range[0]) {
1326 } else if (slice > range[1]) {
1331 if (this->Slice == slice) {
1335 this->Slice = slice;
1338 this->UpdateDisplayExtent();
1340 // Seems to work without this line
1343 //----------------------------------------------------------------------------
1346 //----------------------------------------------------------------------------
1347 void vvSlicer::SetContourSlice()
1349 if (mSurfaceCutActors.size() > 0)
1350 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1351 i!=mSurfaceCutActors.end(); i++) {
1353 (*i)->SetSlicingOrientation(this->SliceOrientation);
1354 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1355 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1358 //----------------------------------------------------------------------------
1361 //----------------------------------------------------------------------------
1362 void vvSlicer::ForceUpdateDisplayExtent()
1364 this->UpdateDisplayExtent();
1366 //----------------------------------------------------------------------------
1369 //----------------------------------------------------------------------------
1370 int* vvSlicer::GetDisplayExtent()
1372 return this->GetImageActor()->GetDisplayExtent();
1374 //----------------------------------------------------------------------------
1377 //----------------------------------------------------------------------------
1378 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1380 this->Superclass::PrintSelf(os, indent);
1382 //----------------------------------------------------------------------------
1384 //----------------------------------------------------------------------------
1385 void vvSlicer::SetVFColor(double r, double g, double b)
1387 double mVFColorHSV[3];
1392 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1393 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1394 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1395 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);