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 ===========================================================================**/
19 #include <QMessageBox>
24 #include "vvSlicerManagerCommand.h"
25 #include "vvGlyphSource.h"
26 #include "vvGlyph2D.h"
28 #include <vtkVersion.h>
29 #include <vtkExtentTranslator.h>
30 #include <vtkAlgorithm.h>
31 #include <vtkExecutive.h>
32 #include <vtkStreamingDemandDrivenPipeline.h>
33 #include <vtkInformation.h>
34 #include <vtkTextProperty.h>
35 #include <vtkTextActor.h>
36 #include <vtkTextSource.h>
37 #include <vtkActor2D.h>
38 #include <vtkCursor2D.h>
39 #include <vtkPolyDataMapper2D.h>
40 #include <vtkProperty2D.h>
41 #include <vtkCornerAnnotation.h>
42 #include <vtkImageMapToWindowLevelColors.h>
43 #include <vtkImageData.h>
44 #include <vtkImageActor.h>
45 #include <vvBlendImageActor.h>
46 #include <vtkToolkits.h>
47 #include <vtkObjectFactory.h>
48 #include <vtkPointData.h>
49 #include <vtkDataArray.h>
50 #include <vtkFloatArray.h>
51 #include <vtkClipPolyData.h>
52 #include <vtkActor2DCollection.h>
53 #include <vtkGlyph3D.h>
55 #include <vtkCursor3D.h>
56 #include <vtkProperty.h>
58 #include <vtkLightCollection.h>
59 #include <vtkScalarBarActor.h>
60 #include <vtkLookupTable.h>
62 #include <vtkRenderer.h>
63 #include <vtkRendererCollection.h>
64 #include <vtkRenderWindow.h>
65 #include <vtkRenderWindowInteractor.h>
66 #include <vtkCamera.h>
67 #include <vtkCallbackCommand.h>
68 #include <vtkCommand.h>
69 #include <vtkPolyDataMapper.h>
72 #include <vtkExtractVOI.h>
73 #include <vtkSphereSource.h>
74 #include <vtkCutter.h>
75 #include <vtkAssignAttribute.h>
76 #include <vtkImageAccumulate.h>
77 #include <vtkImageReslice.h>
78 #include <vtkOpenGLImageSliceMapper.h>
79 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
80 # include <vtkImageMapper3D.h>
81 # include <vtkImageSliceMapper.h>
84 vtkStandardNewMacro(vvSlicer);
85 static void copyExtent(int* in, int* to){
86 for(int i=0; i<6; ++i)
91 //------------------------------------------------------------------------------
94 mFusionSequenceCode = -1;
95 this->UnInstallPipeline();
97 mReducedExtent = new int[6];
98 mRegisterExtent = NULL;
100 mCurrentFusionTSlice = 0;
101 mCurrentOverlayTSlice = 0;
102 mUseReducedExtent = false;
104 mCurrent[0] = -VTK_DOUBLE_MAX;
105 mCurrent[1] = -VTK_DOUBLE_MAX;
106 mCurrent[2] = -VTK_DOUBLE_MAX;
108 mCursor[0] = 0;//-VTK_DOUBLE_MAX;
109 mCursor[1] = 0;//-VTK_DOUBLE_MAX;
110 mCursor[2] = 0;//-VTK_DOUBLE_MAX;
111 mCursor[3] = 0;//-VTK_DOUBLE_MAX;
121 crossCursor = vtkSmartPointer<vtkCursor2D>::New();
122 crossCursor->AllOff();
123 crossCursor->AxesOn();
124 crossCursor->SetTranslationMode(1);
125 crossCursor->SetRadius(2);
127 pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
128 #if VTK_MAJOR_VERSION <= 5
129 pdm->SetInput(crossCursor->GetOutput());
131 pdm->SetInputConnection(crossCursor->GetOutputPort(0));
134 pdmA = vtkSmartPointer<vtkActor2D>::New();
135 pdmA->SetMapper(pdm);
136 pdmA->GetProperty()->SetColor(255,10,212);
137 pdmA->SetVisibility(0);
138 pdmA->SetPickable(0);
140 ca = vtkSmartPointer<vtkCornerAnnotation>::New();
141 ca->GetTextProperty()->SetColor(255,10,212);
142 ca->SetVisibility(1);
150 legend = vtkSmartPointer<vtkScalarBarActor>::New();
151 //legend->SetTitle("test!");
152 legend->SetPosition(0.82,0.18);
153 legend->SetWidth(0.1);
154 legend->SetVisibility(0);
155 legend->SetLabelFormat("%.1f");
156 this->GetRenderer()->AddActor(legend);
157 showFusionLegend = false;
159 this->InstallPipeline();
161 mLinkOverlayWindowLevel = true;
162 mImageVisibility = true;
164 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
165 this->GetImageActor()->GetMapper()->BorderOn();
168 mSlicingTransform = vtkSmartPointer<vtkTransform>::New();
169 mConcatenatedTransform = vtkSmartPointer<vtkTransform>::New();
170 mConcatenatedFusionTransform = vtkSmartPointer<vtkTransform>::New();
171 mConcatenatedOverlayTransform = vtkSmartPointer<vtkTransform>::New();
172 mFirstSetSliceOrientation = true;
174 //------------------------------------------------------------------------------
177 //------------------------------------------------------------------------------
178 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
180 return mOverlayMapper.GetPointer();
182 //------------------------------------------------------------------------------
185 //------------------------------------------------------------------------------
186 vvBlendImageActor* vvSlicer::GetOverlayActor()
188 return mOverlayActor.GetPointer();
190 //------------------------------------------------------------------------------
193 //------------------------------------------------------------------------------
194 vtkImageMapToColors* vvSlicer::GetFusionMapper()
196 return mFusionMapper.GetPointer();
198 //------------------------------------------------------------------------------
201 //------------------------------------------------------------------------------
202 vtkImageActor* vvSlicer::GetFusionActor()
204 return mFusionActor.GetPointer();
206 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
210 vtkActor* vvSlicer::GetVFActor()
212 return mVFActor.GetPointer();
214 //------------------------------------------------------------------------------
217 //------------------------------------------------------------------------------
218 vtkCornerAnnotation* vvSlicer::GetAnnotation()
220 return ca.GetPointer();
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 void vvSlicer::EnableReducedExtent(bool b)
228 mUseReducedExtent = b;
230 //------------------------------------------------------------------------------
233 //------------------------------------------------------------------------------
234 void vvSlicer::SetReducedExtent(int * ext)
236 copyExtent(ext, mReducedExtent);
238 //------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------
242 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
245 mSurfaceCutActors.push_back(new vvMeshActor());
247 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
249 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
250 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
251 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
255 //------------------------------------------------------------------------------
258 //------------------------------------------------------------------------------
259 void vvSlicer::ToggleContourSuperposition()
261 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
262 i!=mSurfaceCutActors.end(); i++)
263 (*i)->ToggleSuperposition();
265 //------------------------------------------------------------------------------
268 //------------------------------------------------------------------------------
269 void vvSlicer::SetCursorColor(int r,int g, int b)
271 pdmA->GetProperty()->SetColor(r,g,b);
273 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 void vvSlicer::SetCursorVisibility(bool s)
279 pdmA->SetVisibility(s);
281 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
285 bool vvSlicer::GetCursorVisibility()
287 return pdmA->GetVisibility();
289 //------------------------------------------------------------------------------
292 //------------------------------------------------------------------------------
293 void vvSlicer::SetCornerAnnotationVisibility(bool s)
295 ca->SetVisibility(s);
297 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 bool vvSlicer::GetCornerAnnotationVisibility()
303 return ca->GetVisibility();
305 //------------------------------------------------------------------------------
308 //------------------------------------------------------------------------------
309 vvSlicer::~vvSlicer()
311 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
312 i!=mSurfaceCutActors.end(); i++)
314 delete [] mReducedExtent;
316 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 double* vvSlicer::GetCurrentPosition()
321 return mCurrentBeforeSlicingTransform;
323 //------------------------------------------------------------------------------
325 //------------------------------------------------------------------------------
326 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
328 mCurrentBeforeSlicingTransform[0]=x;
329 mCurrentBeforeSlicingTransform[1]=y;
330 mCurrentBeforeSlicingTransform[2]=z;
331 mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
332 if (t>=0) SetTSlice(t);
334 //------------------------------------------------------------------------------
337 //------------------------------------------------------------------------------
338 void vvSlicer::SetImage(vvImage::Pointer image)
340 if (image->GetVTKImages().size()) {
343 if (!mImageReslice) {
344 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
345 mImageReslice->SetInterpolationModeToLinear();
346 mImageReslice->AutoCropOutputOn();
347 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
350 mConcatenatedTransform->Identity();
351 mConcatenatedTransform->Concatenate(mImage->GetTransform()[0]);
352 mConcatenatedTransform->Concatenate(mSlicingTransform);
353 mImageReslice->SetResliceTransform(mConcatenatedTransform);
354 #if VTK_MAJOR_VERSION <= 5
355 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
357 mImageReslice->SetInputData(0, mImage->GetFirstVTKImageData());
359 mImageReslice->UpdateInformation();
361 #if VTK_MAJOR_VERSION <= 5
362 this->Superclass::SetInput(mImageReslice->GetOutput());
364 this->Superclass::SetInputConnection(mImageReslice->GetOutputPort());
368 #if VTK_MAJOR_VERSION <= 5
369 this->GetInput()->GetWholeExtent(extent);
371 int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
372 copyExtent(ext, extent);
375 // Prevent crash when reload -> change slice if outside extent
376 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
377 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
380 // Make sure that the required part image has been computed
381 extent[SliceOrientation*2] = Slice;
382 extent[SliceOrientation*2+1] = Slice;
384 #if VTK_MAJOR_VERSION <= 5
385 mImageReslice->GetOutput()->SetUpdateExtent(extent);
386 mImageReslice->GetOutput()->Update();
388 mImageReslice->SetUpdateExtent(extent);
389 mImageReslice->Update();
392 this->UpdateDisplayExtent();
395 ca->SetText(0,mFileName.c_str());
398 //------------------------------------------------------------------------------
401 //------------------------------------------------------------------------------
402 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
404 if (overlay->GetVTKImages().size()) {
406 mOverlayVisibility = true;
408 if (!mOverlayReslice) {
409 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
410 mOverlayReslice->SetInterpolationModeToLinear();
411 mOverlayReslice->AutoCropOutputOn();
412 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
415 mConcatenatedOverlayTransform->Identity();
416 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
417 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
418 mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
419 #if VTK_MAJOR_VERSION <= 5
420 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
422 mOverlayReslice->SetInputData(0, mOverlay->GetFirstVTKImageData());
424 mOverlayReslice->Update();
427 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
428 #if VTK_MAJOR_VERSION <= 5
429 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
431 mOverlayMapper->SetInputConnection(mOverlayReslice->GetOutputPort(0));
434 if (!mOverlayActor) {
435 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
436 #if VTK_MAJOR_VERSION <= 5
437 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
439 mOverlayActor->GetMapper()->SetInputConnection(mOverlayMapper->GetOutputPort());
441 mOverlayActor->SetPickable(0);
442 mOverlayActor->SetVisibility(true);
443 mOverlayActor->SetOpacity(0.5);
444 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
445 mOverlayActor->GetMapper()->BorderOn();
449 //stupid but necessary : the Overlay need to be rendered before fusion
451 this->GetRenderer()->RemoveActor(mFusionActor);
452 this->GetRenderer()->AddActor(mOverlayActor);
453 this->GetRenderer()->AddActor(mFusionActor);
455 this->GetRenderer()->AddActor(mOverlayActor);
457 //Synchronize orientation and slice
458 AdjustResliceToSliceOrientation(mOverlayReslice);
459 this->UpdateDisplayExtent();
460 this->SetTSlice(mCurrentTSlice);
463 //------------------------------------------------------------------------------
466 //------------------------------------------------------------------------------
467 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
469 mFusionSequenceCode = fusionSequenceCode;
470 if (fusion->GetVTKImages().size()) {
472 mFusionVisibility = true;
474 if (!mFusionReslice) {
475 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
476 mFusionReslice->SetInterpolationModeToLinear();
477 mFusionReslice->AutoCropOutputOn();
478 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
481 mConcatenatedFusionTransform->Identity();
482 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
483 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
484 mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
485 #if VTK_MAJOR_VERSION <= 5
486 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
488 mFusionReslice->SetInputData(0, mFusion->GetFirstVTKImageData());
490 mFusionReslice->Update();
493 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
495 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
497 lut->SetValueRange(0, 1);
498 lut->SetSaturationRange(0, 0);
500 mFusionMapper->SetLookupTable(lut);
501 #if VTK_MAJOR_VERSION <= 5
502 mFusionMapper->SetInput(mFusionReslice->GetOutput());
504 mFusionMapper->SetInputConnection(mFusionReslice->GetOutputPort(0));
508 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
509 #if VTK_MAJOR_VERSION <= 5
510 mFusionActor->SetInput(mFusionMapper->GetOutput());
512 mFusionActor->GetMapper()->SetInputConnection(mFusionMapper->GetOutputPort());
514 mFusionActor->SetPickable(0);
515 mFusionActor->SetVisibility(true);
516 mFusionActor->SetOpacity(0.7);
517 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
518 mFusionActor->GetMapper()->BorderOn();
521 this->GetRenderer()->AddActor(mFusionActor);
524 //Synchronize orientation and slice
525 AdjustResliceToSliceOrientation(mFusionReslice);
526 this->UpdateDisplayExtent();
527 this->SetTSlice(mCurrentTSlice);
530 //------------------------------------------------------------------------------
533 //------------------------------------------------------------------------------
534 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
537 if (actor_type == "image")
538 vis = mImageVisibility;
539 else if (actor_type == "vector")
541 else if (actor_type == "overlay")
542 vis = mOverlayVisibility;
543 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
544 vis = mFusionVisibility;
545 else if (actor_type == "contour")
546 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
549 //------------------------------------------------------------------------------
551 //------------------------------------------------------------------------------
552 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
554 if (actor_type == "image")
555 mImageVisibility = vis;
556 else if (actor_type == "vector")
558 else if (actor_type == "overlay")
559 mOverlayVisibility = vis;
560 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
561 mFusionVisibility = vis;
562 else if (actor_type == "contour")
563 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
564 UpdateDisplayExtent();
566 //------------------------------------------------------------------------------
568 //------------------------------------------------------------------------------
569 void vvSlicer::SetVF(vvImage::Pointer vf)
571 if (vf->GetVTKImages().size()) {
573 mVFVisibility = true;
576 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
577 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
578 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
580 #if VTK_MAJOR_VERSION <= 5
581 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
582 mAAFilter->SetInput(mVOIFilter->GetOutput());
584 mVOIFilter->SetInputData(vf->GetFirstVTKImageData());
585 mAAFilter->SetInputConnection(mVOIFilter->GetOutputPort());
587 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
588 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
591 mArrow = vtkSmartPointer<vvGlyphSource>::New();
592 mArrow->SetGlyphTypeToSpecificArrow();
593 mArrow->SetScale(mScale);
596 // Glyph the gradient vector (with arrows)
598 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
599 #if VTK_MAJOR_VERSION <= 5
600 mGlyphFilter->SetInput(mAAFilter->GetOutput());
601 mGlyphFilter->SetSource(mArrow->GetOutput());
603 mGlyphFilter->SetInputConnection(mAAFilter->GetOutputPort());
604 mGlyphFilter->SetSourceConnection(mArrow->GetOutputPort());
606 mGlyphFilter->ScalingOn();
607 mGlyphFilter->SetScaleModeToScaleByVector();
608 mGlyphFilter->OrientOn();
609 mGlyphFilter->SetVectorModeToUseVector();
610 mGlyphFilter->SetColorModeToColorByVector();
613 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
615 double mVFColorHSV[3];
616 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
617 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
618 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
619 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
622 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
623 #if VTK_MAJOR_VERSION <= 5
624 mVFMapper->SetInput(mGlyphFilter->GetOutput());
626 mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
628 mVFMapper->ImmediateModeRenderingOn();
629 mVFMapper->SetLookupTable(mVFColorLUT);
632 mVFActor = vtkSmartPointer<vtkActor>::New();
633 mVFActor->SetMapper(mVFMapper);
634 mVFActor->SetPickable(0);
635 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
636 this->UpdateDisplayExtent();
637 this->GetRenderer()->AddActor(mVFActor);
640 SetTSlice(mCurrentTSlice);
643 //------------------------------------------------------------------------------
646 //------------------------------------------------------------------------------
647 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
649 mLandmarks = landmarks;
653 mCross = vtkSmartPointer<vtkCursor3D>::New();
655 mClipBox = vtkSmartPointer<vtkBox>::New();
657 mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
659 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
661 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
663 mLandActor = vtkSmartPointer<vtkActor>::New();
665 mCross->SetFocalPoint(0.0,0.0,0.0);
666 mCross->SetModelBounds(-10,10,-10,10,-10,10);
670 mLandClipper->SetClipFunction(mClipBox);
671 mLandClipper->InsideOutOn();
672 #if VTK_MAJOR_VERSION <= 5
673 mLandClipper->SetInput(mLandmarks->GetOutput());
675 mLandGlyph->SetSource(mCross->GetOutput());
676 mLandGlyph->SetInput(mLandClipper->GetOutput());
678 mLandClipper->SetInputData(mLandmarks->GetOutput());
680 mLandGlyph->SetSourceConnection(mCross->GetOutputPort());
681 mLandGlyph->SetInputConnection(mLandClipper->GetOutputPort());
683 //mLandGlyph->SetIndexModeToScalar();
684 //mLandGlyph->SetRange(0,1);
685 //mLandGlyph->ScalingOff();
687 //mLandGlyph->SetColorModeToColorByScalar();
689 mLandGlyph->SetScaleModeToDataScalingOff();
690 mLandGlyph->SetIndexModeToOff();
692 mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
693 //mLandMapper->ScalarVisibilityOff();
695 mLandActor->SetMapper(mLandMapper);
696 mLandActor->GetProperty()->SetOpacity(0.995);
697 mLandActor->GetProperty()->SetColor(255,10,212);
698 mLandActor->SetPickable(0);
699 mLandActor->SetVisibility(true);
700 this->UpdateDisplayExtent();
701 this->GetRenderer()->AddActor(mLandActor);
704 //------------------------------------------------------------------------------
706 //------------------------------------------------------------------------------
707 //FIXME: this function leaks memory, we should fix it someday :)
708 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
710 if (actor_type == "vector") {
711 Renderer->RemoveActor(mVFActor);
720 if (actor_type == "overlay") {
721 Renderer->RemoveActor(mOverlayActor);
723 mOverlayActor = NULL;
724 mOverlayMapper = NULL;
726 if ( (actor_type == "fusion") || (actor_type == "fusionSequence") ) {
727 Renderer->RemoveActor(mFusionActor);
730 mFusionMapper = NULL;
732 if (actor_type == "contour") {
733 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
734 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
737 //------------------------------------------------------------------------------
740 //------------------------------------------------------------------------------
741 void vvSlicer::SetVFSubSampling(int sub)
744 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
747 UpdateDisplayExtent();
750 //------------------------------------------------------------------------------
753 //------------------------------------------------------------------------------
754 void vvSlicer::SetVFScale(int scale)
758 mArrow->SetScale(mScale);
759 UpdateDisplayExtent();
762 //------------------------------------------------------------------------------
764 //------------------------------------------------------------------------------
765 void vvSlicer::SetVFWidth(int width)
769 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
770 UpdateDisplayExtent();
773 //------------------------------------------------------------------------------
776 //------------------------------------------------------------------------------
777 void vvSlicer::SetVFLog(int log)
781 mGlyphFilter->SetUseLog(mVFLog);
782 mGlyphFilter->Modified();
784 UpdateDisplayExtent();
787 //------------------------------------------------------------------------------
790 //------------------------------------------------------------------------------
791 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
793 if (!updateLinkedImages) {
795 #if VTK_MAJOR_VERSION <= 5
796 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
798 mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
801 mConcatenatedTransform->Identity();
802 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
803 mConcatenatedTransform->Concatenate(mSlicingTransform);
804 UpdateDisplayExtent();
809 else if ((unsigned int)t >= mImage->GetVTKImages().size())
810 mCurrentTSlice = mImage->GetVTKImages().size() -1;
815 mConcatenatedTransform->Identity();
816 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
817 mConcatenatedTransform->Concatenate(mSlicingTransform);
820 #if VTK_MAJOR_VERSION <= 5
821 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
823 mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
825 if (mVF && mVFActor->GetVisibility()) {
826 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
827 #if VTK_MAJOR_VERSION <= 5
828 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
830 mVOIFilter->SetInputData(mVF->GetVTKImages()[mCurrentTSlice]);
834 if (mOverlay && mOverlayActor->GetVisibility()) {
835 if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
836 mCurrentOverlayTSlice = t;
837 #if VTK_MAJOR_VERSION <= 5
838 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
840 mOverlayReslice->SetInputData( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
842 // Update overlay transform
843 mConcatenatedOverlayTransform->Identity();
844 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
845 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
848 //update the fusion ; except in case this is a fusionSequence, in which case both 'times' should be independent.
849 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode<0)) {
850 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
851 mCurrentFusionTSlice = t;
852 #if VTK_MAJOR_VERSION <= 5
853 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
855 mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
858 // Update fusion transform
859 mConcatenatedFusionTransform->Identity();
860 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
861 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
864 if (mSurfaceCutActors.size() > 0)
865 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
866 i!=mSurfaceCutActors.end(); i++)
867 (*i)->SetTimeSlice(mCurrentTSlice);
868 UpdateDisplayExtent();
870 //------------------------------------------------------------------------------
873 //------------------------------------------------------------------------------
874 void vvSlicer::SetFusionSequenceTSlice(int t)
876 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode>=0)) {
877 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
878 mCurrentFusionTSlice = t;
879 #if VTK_MAJOR_VERSION <= 5
880 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
882 mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
884 // Update fusion transform
885 mConcatenatedFusionTransform->Identity();
886 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]); //not really useful...
887 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
891 UpdateDisplayExtent();
893 //------------------------------------------------------------------------------
896 //------------------------------------------------------------------------------
897 int vvSlicer::GetTSlice()
899 return mCurrentTSlice;
901 //------------------------------------------------------------------------------
903 //------------------------------------------------------------------------------
904 int vvSlicer::GetMaxCurrentTSlice()
906 int t = mCurrentTSlice;
908 t = std::max(t, mCurrentOverlayTSlice);
909 if(mFusion&& (mFusionSequenceCode<0)) //ignore fusionSequence data: for these, the times are not to be related (this way)
910 t = std::max(t, mCurrentFusionTSlice);
913 //------------------------------------------------------------------------------
915 //------------------------------------------------------------------------------
916 int vvSlicer::GetFusionTSlice()
918 return mCurrentFusionTSlice;
920 //------------------------------------------------------------------------------
922 //------------------------------------------------------------------------------
923 int vvSlicer::GetOverlayTSlice()
925 return mCurrentOverlayTSlice;
927 //------------------------------------------------------------------------------
929 //------------------------------------------------------------------------------
930 void vvSlicer::SetSliceOrientation(int orientation)
932 //if 2D image, force to watch in Axial View
934 #if VTK_MAJOR_VERSION <= 5
935 this->GetInput()->GetWholeExtent(extent);
937 int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
938 copyExtent(ext, extent);
941 if (extent[5]-extent[4] <= 2)
942 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
944 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
945 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
946 vtkErrorMacro("Error - invalid slice orientation " << orientation);
950 this->SliceOrientation = orientation;
953 AdjustResliceToSliceOrientation(mFusionReslice);
956 AdjustResliceToSliceOrientation(mOverlayReslice);
960 // Go to current cursor position
961 // double* cursorPos = GetCursorPosition();
962 // DDV(cursorPos, 3);
963 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
965 if (mFirstSetSliceOrientation) {
966 int *range = this->GetSliceRange();
968 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
969 #if VTK_MAJOR_VERSION <= 5
970 mFirstSetSliceOrientation = false;
973 else if (this->Renderer && this->GetInput()) {
974 double s = mCursor[orientation];
975 double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
976 this->Slice = static_cast<int>(sCursor);
979 this->UpdateOrientation();
981 this->UpdateDisplayExtent();
983 if (mFirstSetSliceOrientation) {
984 mFirstSetSliceOrientation = false;
987 if (this->Renderer && this->GetInput()) {
988 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
989 this->Renderer->ResetCamera();
990 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
995 //----------------------------------------------------------------------------
997 //------------------------------------------------------------------------------
998 // This function ensures that we sample the slices of a vtkImageReslice filter
999 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
1000 // In other words, we change the grid of the reslice in the same way as the grid
1001 // of the displayed image in the slicing direction.
1002 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
1004 // Reset autocrop and update output information
1005 reslice->SetOutputOriginToDefault();
1006 reslice->SetOutputSpacingToDefault();
1007 #if VTK_MAJOR_VERSION <= 5
1008 reslice->GetOutput()->UpdateInformation();
1010 reslice->UpdateInformation();
1013 // Ge new origin / spacing
1016 reslice->GetOutput()->GetOrigin(origin);
1017 reslice->GetOutput()->GetSpacing(spacing);
1019 // Use similar spacing as the image in the direction SliceOrientation
1020 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1022 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
1023 // Step 1: from world coordinates to image coordinates
1024 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1025 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1027 // Step 2: round to nearest grid positionInc. This has been validated as the only
1028 // way to have something consistent with the thickness of a 2D slice visible on the
1029 // other slices. The thickness is accounted for so if the 2D slice is to thin and
1030 // between two slices, one will never be able to see this 2D slice (bug #1883).
1031 origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
1033 // Step 3: back to world coordinates
1034 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1035 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1037 // Set new spacing and origin
1038 reslice->SetOutputOrigin(origin);
1039 reslice->SetOutputSpacing(spacing);
1040 reslice->UpdateInformation();
1042 //------------------------------------------------------------------------------
1044 //----------------------------------------------------------------------------
1045 int * vvSlicer::GetExtent()
1048 if (mUseReducedExtent) {
1049 w_ext = mReducedExtent;
1052 #if VTK_MAJOR_VERSION <= 5
1053 w_ext = GetInput()->GetWholeExtent();
1055 w_ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1060 //----------------------------------------------------------------------------
1063 //----------------------------------------------------------------------------
1064 int vvSlicer::GetOrientation()
1066 return this->SliceOrientation;
1068 //----------------------------------------------------------------------------
1071 //----------------------------------------------------------------------------
1072 void vvSlicer::UpdateDisplayExtent()
1074 vtkImageData *input = this->GetInput();
1075 if (!input || !this->ImageActor) {
1079 #if VTK_MAJOR_VERSION <= 5
1080 input->UpdateInformation();
1082 mRegisterExtent = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1084 this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
1086 // Local copy of extent
1088 int* ext = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1089 copyExtent(ext, w_ext);
1090 if (mUseReducedExtent) {
1091 copyExtent(mReducedExtent, w_ext);
1093 cout << mUseReducedExtent<< " " << w_ext[0] << " " << w_ext[1] << " " << w_ext[2] << " " << w_ext[3] << " " << w_ext[4] << " " << w_ext[5] << endl;
1096 w_ext[ this->SliceOrientation*2 ] = this->Slice;
1097 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
1100 this->ImageActor->SetVisibility(mImageVisibility);
1101 this->ImageActor->SetDisplayExtent(w_ext);
1103 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1104 // Fix for bug #1882
1105 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
1108 // Overlay image actor
1109 if (mOverlay && mOverlayVisibility) {
1110 AdjustResliceToSliceOrientation(mOverlayReslice);
1112 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mOverlayReslice->GetOutput(), overExtent);
1113 #if VTK_MAJOR_VERSION <= 5
1114 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
1116 bool out = ClipDisplayedExtent(overExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1118 mOverlayActor->SetVisibility(!out);
1119 mOverlayActor->SetDisplayExtent( overExtent );
1120 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1121 // Fix for bug #1882
1122 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
1126 mOverlayActor->SetVisibility(false);
1128 // Fusion image actor
1129 if (mFusion && mFusionVisibility) {
1130 AdjustResliceToSliceOrientation(mFusionReslice);
1132 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mFusionReslice->GetOutput(), fusExtent);
1133 #if VTK_MAJOR_VERSION <= 5
1134 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
1136 bool out = ClipDisplayedExtent(fusExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1138 mFusionActor->SetVisibility(!out);
1139 mFusionActor->SetDisplayExtent( fusExtent );
1140 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1141 // Fix for bug #1882
1142 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1146 mFusionActor->SetVisibility(false);
1147 // Vector field actor
1148 double* camera = Renderer->GetActiveCamera()->GetPosition();
1149 double* image_bounds = ImageActor->GetBounds();
1150 double position[3] = {0, 0, 0};
1151 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1153 //print_vector<double, 6>("camera", camera);
1154 //print_vector<double, 6>("image_bounds", image_bounds);
1155 //print_vector<double, 3>("position", position);
1157 // find where to place the VF actor. to deal with
1158 // z-buffer issues, the VF is placed right in front of the image,
1159 // subject to a small offset. the position actually depends on the
1160 // the location of the camera relative to the image.
1162 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1165 if (mVF && mVFVisibility) {
1167 #if VTK_MAJOR_VERSION <= 5
1168 mVF->GetVTKImages()[0]->UpdateInformation();
1170 //this->UpdateInformation();
1172 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mVF->GetVTKImages()[0], vfExtent);
1173 #if VTK_MAJOR_VERSION <= 5
1174 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1176 bool out = ClipDisplayedExtent(vfExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1178 mVFActor->SetVisibility(!out);
1179 mVOIFilter->SetVOI(vfExtent);
1180 int orientation[3] = {1,1,1};
1181 orientation[this->SliceOrientation] = 0;
1182 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1183 position[this->SliceOrientation] += offset;
1184 mVFActor->SetPosition(position);
1185 mVFActor->GetProperty()->SetOpacity(0.995);
1186 mVFMapper->Update();
1190 mVFActor->SetVisibility(false);
1196 for(unsigned int i=0; i<6; i++)
1197 bounds[i] = ImageActor->GetBounds()[i];
1198 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1199 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1200 mClipBox->SetBounds(bounds);
1204 position[this->SliceOrientation] = offset;
1205 mLandActor->SetPosition(position);
1208 // Figure out the correct clipping range
1209 if (this->Renderer) {
1210 if (this->InteractorStyle &&
1211 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1212 this->Renderer->ResetCameraClippingRange();
1214 vtkCamera *cam = this->Renderer->GetActiveCamera();
1217 this->ImageActor->GetBounds(bounds);
1218 double spos = (double)bounds[this->SliceOrientation * 2];
1219 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1220 double range = fabs(spos - cpos);
1221 double *spacing = input->GetSpacing();
1222 double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1223 cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1228 //----------------------------------------------------------------------------
1230 //----------------------------------------------------------------------------
1231 void vvSlicer::ConvertImageToImageDisplayExtent(vtkInformation *sourceImage, const int sourceExtent[6],
1232 vtkImageData *targetImage, int targetExtent[6])
1235 double *origin, *spacing;
1236 origin = sourceImage->Get(vtkDataObject::ORIGIN());
1237 spacing = sourceImage->Get(vtkDataObject::SPACING());
1238 for(unsigned int i=0; i<6; i++) {
1239 // From source voxel coordinates to world coordinates
1240 dExtents[i] = origin[i/2] + spacing[i/2] * sourceExtent[i];
1242 // From world coordinates to floating point target voxel coordinates
1243 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1245 // Round to current slice or larger extent
1246 if(i/2==this->GetOrientation())
1247 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1249 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1251 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1254 //----------------------------------------------------------------------------
1256 //----------------------------------------------------------------------------
1257 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1262 for (int i = 0; i < maxBound; i = i+2) {
1263 //if we are totally outside the image
1264 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1268 //crop to the limit of the image
1269 extent[i] = std::max(extent[i], refExtent[i]);
1270 extent[i] = std::min(extent[i], refExtent[i+1]);;
1271 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1272 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1275 for (int i = 0; i < maxBound; i = i+2) {
1276 extent[i] = refExtent[i];
1277 extent[i+1] = refExtent[i];
1281 //----------------------------------------------------------------------------
1284 //----------------------------------------------------------------------------
1285 void vvSlicer::UpdateOrientation()
1287 // Set the camera position
1288 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1290 switch (this->SliceOrientation) {
1291 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1292 cam->SetFocalPoint(0,0,0);
1293 cam->SetPosition(0,0,-1); // -1 if medical ?
1294 cam->SetViewUp(0,-1,0);
1297 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1298 cam->SetFocalPoint(0,0,0);
1299 cam->SetPosition(0,-1,0); // 1 if medical ?
1300 cam->SetViewUp(0,0,1);
1303 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1304 cam->SetFocalPoint(0,0,0);
1305 cam->SetPosition(-1,0,0); // -1 if medical ?
1306 cam->SetViewUp(0,0,1);
1311 //----------------------------------------------------------------------------
1314 //----------------------------------------------------------------------------
1315 void vvSlicer::SetOpacity(double s)
1317 this->GetImageActor()->SetOpacity(s);
1319 //----------------------------------------------------------------------------
1322 //----------------------------------------------------------------------------
1323 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1325 this->Superclass::SetRenderWindow(rw);
1326 this->SetupInteractor(rw->GetInteractor());
1327 ca->SetImageActor(this->GetImageActor());
1328 ca->SetWindowLevel(this->GetWindowLevel());
1329 ca->SetText(3, "<window>\n<level>");
1340 crossCursor->SetModelBounds(bounds);
1342 this->GetRenderer()->AddActor(pdmA);
1343 this->GetRenderer()->AddActor(ca);
1344 this->GetRenderer()->ResetCamera();
1346 //this is just a mapping between the labeling of the orientations presented to the user and
1347 //the one used by vtk
1348 SetSliceOrientation(2-(orientation%3));
1351 //----------------------------------------------------------------------------
1354 //----------------------------------------------------------------------------
1355 void vvSlicer::ResetCamera()
1357 this->GetRenderer()->ResetCamera();
1359 //----------------------------------------------------------------------------
1362 //----------------------------------------------------------------------------
1363 void vvSlicer::SetDisplayMode(bool i)
1365 this->GetRenderer()->SetDraw(i);
1366 if (i) UpdateDisplayExtent();
1368 //----------------------------------------------------------------------------
1371 //----------------------------------------------------------------------------
1372 void vvSlicer::FlipHorizontalView()
1374 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1376 double *position = cam->GetPosition();
1377 double factor[3] = {1, 1, 1};
1378 factor[this->SliceOrientation] = -1;
1379 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1381 /* switch (this->SliceOrientation) {
1382 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1383 cam->SetPosition(position[0],position[1],-position[2]);
1386 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1387 cam->SetPosition(position[0],-position[1],position[2]);
1390 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1391 cam->SetPosition(-position[0],position[1],position[2]);
1395 this->Renderer->ResetCameraClippingRange();
1396 this->UpdateDisplayExtent();
1399 //----------------------------------------------------------------------------
1402 //----------------------------------------------------------------------------
1403 void vvSlicer::FlipVerticalView()
1405 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1407 FlipHorizontalView();
1408 double *viewup = cam->GetViewUp();
1409 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1410 this->UpdateDisplayExtent();
1413 //----------------------------------------------------------------------------
1416 //----------------------------------------------------------------------------
1417 void vvSlicer::SetColorWindow(double window)
1419 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1421 double level = this->GetWindowLevel()->GetLevel();
1422 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1425 this->vtkImageViewer2::SetColorWindow(window);
1427 //----------------------------------------------------------------------------
1429 //----------------------------------------------------------------------------
1430 void vvSlicer::SetColorLevel(double level)
1432 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1434 double window = this->GetWindowLevel()->GetWindow();
1435 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1438 this->vtkImageViewer2::SetColorLevel(level);
1440 //----------------------------------------------------------------------------
1442 //----------------------------------------------------------------------------
1443 double vvSlicer::GetOverlayColorWindow()
1446 return mOverlayMapper->GetWindow();
1450 //----------------------------------------------------------------------------
1452 //----------------------------------------------------------------------------
1453 double vvSlicer::GetOverlayColorLevel()
1456 return mOverlayMapper->GetLevel();
1460 //----------------------------------------------------------------------------
1462 //----------------------------------------------------------------------------
1463 void vvSlicer::SetOverlayColorWindow(double window)
1466 mOverlayMapper->SetWindow(window);
1468 //----------------------------------------------------------------------------
1470 //----------------------------------------------------------------------------
1471 void vvSlicer::SetOverlayColorLevel(double level)
1474 mOverlayMapper->SetLevel(level);
1476 //----------------------------------------------------------------------------
1478 //----------------------------------------------------------------------------
1479 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1480 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1482 //Get mouse pointer position in view coordinates
1485 for(int i=0; i<3; i++) {
1486 corner1[i] = mCurrent[i];
1487 corner2[i] = mCurrent[i];
1490 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1491 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1493 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1495 this->Renderer->GetTiledSize(&w, &h);
1496 corner1[0] -= 0.2*h/(double)w;
1497 corner2[0] += 0.2*h/(double)w;
1500 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1501 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1503 //Convert to image pixel coordinates (rounded)
1504 transform->TransformPoint(corner1, corner1);
1505 transform->TransformPoint(corner2, corner2);
1506 int iLocalExtents[6];
1507 for(int i=0; i<3; i++) {
1508 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1509 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1511 iLocalExtents[i*2 ] = lrint(corner1[i]);
1512 iLocalExtents[i*2+1] = lrint(corner2[i]);
1514 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1515 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1518 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1519 #if VTK_MAJOR_VERSION <= 5
1520 voiFilter->SetInput(image);
1522 voiFilter->SetInputData(image);
1524 voiFilter->SetVOI(iLocalExtents);
1525 voiFilter->Update();
1526 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1532 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1533 #if VTK_MAJOR_VERSION <= 5
1534 accFilter->SetInput(voiFilter->GetOutput());
1536 accFilter->SetInputConnection(voiFilter->GetOutputPort(0));
1538 accFilter->Update();
1540 min = *(accFilter->GetMin());
1541 max = *(accFilter->GetMax());
1543 //----------------------------------------------------------------------------
1545 //----------------------------------------------------------------------------
1546 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1551 #if VTK_MAJOR_VERSION <= 5
1552 if (ix < image->GetWholeExtent()[0] ||
1553 ix > image->GetWholeExtent()[1] ||
1554 iy < image->GetWholeExtent()[2] ||
1555 iy > image->GetWholeExtent()[3] ||
1556 iz < image->GetWholeExtent()[4] ||
1557 iz > image->GetWholeExtent()[5] )
1558 return std::numeric_limits<double>::quiet_NaN();
1559 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1562 if (ix < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0] ||
1563 ix > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1] ||
1564 iy < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2] ||
1565 iy > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3] ||
1566 iz < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4] ||
1567 iz > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5] )
1568 return std::numeric_limits<double>::quiet_NaN();
1569 //image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1573 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1575 //----------------------------------------------------------------------------
1577 //----------------------------------------------------------------------------
1578 void vvSlicer::Render()
1580 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1581 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1582 legend->UseOpacityOn();
1583 legend->SetVisibility(1);
1585 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1586 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1587 legend->UseOpacityOff();
1588 legend->SetVisibility(1);
1589 } else legend->SetVisibility(0);
1591 if (ca->GetVisibility()) {
1593 std::stringstream worldPos(" ");
1595 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1596 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1597 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1598 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1599 #if VTK_MAJOR_VERSION <= 5
1600 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1601 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1602 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1603 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1604 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1605 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1609 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1611 if(ImageActor->GetVisibility())
1612 worldPos << "data value : " << value << std::endl;
1614 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1615 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1616 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1619 worldPos << "pixel : " << ix << ' '
1626 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0]-0.5 &&
1627 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1]+0.5 &&
1628 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2]-0.5 &&
1629 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3]+0.5 &&
1630 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4]-0.5 &&
1631 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5]+0.5) {
1634 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1636 if(ImageActor->GetVisibility())
1637 worldPos << "data value : " << value << std::endl;
1639 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1640 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1641 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1644 worldPos << "pixel : " << ix << ' '
1652 ca->SetText(1,worldPos.str().c_str());
1654 std::stringstream slicePos;
1655 slicePos << "Slice: " << this->GetSlice();
1656 ca->SetText(2, slicePos.str().c_str());
1659 if (pdmA->GetVisibility()) {
1660 double x = mCursor[0];
1661 double y = mCursor[1];
1662 double z = mCursor[2];
1663 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1664 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1665 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1666 #if VTK_MAJOR_VERSION <= 5
1667 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1668 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1669 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1670 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1671 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1672 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1673 vtkRenderer * renderer = this->Renderer;
1675 renderer->WorldToView(x,y,z);
1676 renderer->ViewToNormalizedViewport(x,y,z);
1677 renderer->NormalizedViewportToViewport(x,y);
1678 renderer->ViewportToNormalizedDisplay(x,y);
1679 renderer->NormalizedDisplayToDisplay(x,y);
1680 crossCursor->SetFocalPoint(x,y,z);
1682 crossCursor->SetFocalPoint(-1,-1,z);
1683 crossCursor->Update();
1686 vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1688 mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1689 } catch (const std::bad_cast& e) {
1690 std::cerr << e.what() << std::endl;
1691 std::cerr << "Conversion error" << std::endl;
1695 if (xCursor >= mapperOpenGL->GetCroppingRegion()[0]-0.5 &&
1696 xCursor < mapperOpenGL->GetCroppingRegion()[1]+0.5 &&
1697 yCursor >= mapperOpenGL->GetCroppingRegion()[2]-0.5 &&
1698 yCursor < mapperOpenGL->GetCroppingRegion()[3]+0.5 &&
1699 zCursor >= mapperOpenGL->GetCroppingRegion()[4]-0.5 &&
1700 zCursor < mapperOpenGL->GetCroppingRegion()[5]+0.5 ) {
1701 vtkRenderer * renderer = this->Renderer;
1703 renderer->WorldToView(x,y,z);
1704 renderer->ViewToNormalizedViewport(x,y,z);
1705 renderer->NormalizedViewportToViewport(x,y);
1706 renderer->ViewportToNormalizedDisplay(x,y);
1707 renderer->NormalizedDisplayToDisplay(x,y);
1708 crossCursor->SetFocalPoint(x,y,z);
1710 crossCursor->SetFocalPoint(-1,-1,z);
1711 crossCursor->Update();
1715 if (mOverlay && mOverlayActor->GetVisibility()) {
1716 if(mLinkOverlayWindowLevel) {
1717 mOverlayMapper->SetWindow(this->GetColorWindow());
1718 mOverlayMapper->SetLevel(this->GetColorLevel());
1720 #if VTK_MAJOR_VERSION <= 5
1721 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1723 mOverlayMapper->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1725 mOverlayMapper->Update();
1727 if (mFusion && mFusionActor->GetVisibility()) {
1728 #if VTK_MAJOR_VERSION <= 5
1729 mFusionMapper->GetOutput()->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1731 mFusionMapper->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1733 mFusionMapper->Update();
1738 this->GetRenderWindow()->Render();
1740 //----------------------------------------------------------------------------
1743 //----------------------------------------------------------------------------
1744 void vvSlicer::UpdateCursorPosition()
1746 pdmA->SetVisibility(true);
1747 mCursor[0] = mCurrent[0];
1748 mCursor[1] = mCurrent[1];
1749 mCursor[2] = mCurrent[2];
1750 mCursor[3] = mCurrentTSlice;
1752 //----------------------------------------------------------------------------
1755 //----------------------------------------------------------------------------
1756 void vvSlicer::UpdateLandmarks()
1758 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1759 if (pd->GetPoints()) {
1760 //mLandGlyph->SetRange(0,1);
1761 //mLandGlyph->Modified();
1762 //mLandGlyph->Update();
1764 mClipBox->Modified();
1765 mLandClipper->Update();
1766 mLandMapper->Update();
1767 //Let's add the captions
1768 //First remove all captions:
1769 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1770 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1771 //allActors2D->Remove (mLandLabelActors[i]);
1773 mLandLabelActors.clear();
1774 //Next add the captions to the displayed points
1775 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1776 double *position = mLandClipper->GetOutput()->GetPoint(id);
1777 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1778 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1779 label_actor->SetCaption(label);
1780 label_actor->SetAttachmentPoint(position);
1781 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1782 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1783 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1784 label_actor->GetCaptionTextProperty()->SetBold(0);
1785 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1786 label_actor->BorderOff();
1787 label_actor->LeaderOff();
1788 label_actor->ThreeDimensionalLeaderOff();
1789 mLandLabelActors.push_back(label_actor);
1790 this->Renderer->AddActor2D(mLandLabelActors[id]);
1795 //----------------------------------------------------------------------------
1798 //----------------------------------------------------------------------------
1799 void vvSlicer::SetSlice(int slice)
1801 int *range = this->GetSliceRange();
1803 if (slice < range[0]) {
1805 } else if (slice > range[1]) {
1810 if (this->Slice == slice) {
1814 this->Slice = slice;
1817 this->UpdateDisplayExtent();
1819 // Seems to work without this line
1822 //----------------------------------------------------------------------------
1824 //----------------------------------------------------------------------------
1825 int vvSlicer::GetTMax()
1827 int tmax = (int)mImage->GetVTKImages().size() - 1;
1829 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1832 //----------------------------------------------------------------------------
1834 //----------------------------------------------------------------------------
1835 void vvSlicer::SetContourSlice()
1837 if (mSurfaceCutActors.size() > 0)
1838 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1839 i!=mSurfaceCutActors.end(); i++) {
1841 (*i)->SetSlicingOrientation(this->SliceOrientation);
1842 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1843 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1846 //----------------------------------------------------------------------------
1849 //----------------------------------------------------------------------------
1850 void vvSlicer::ForceUpdateDisplayExtent()
1852 this->UpdateDisplayExtent();
1854 //----------------------------------------------------------------------------
1857 //----------------------------------------------------------------------------
1858 int* vvSlicer::GetDisplayExtent()
1860 return this->GetImageActor()->GetDisplayExtent();
1862 //----------------------------------------------------------------------------
1865 //----------------------------------------------------------------------------
1866 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1868 this->Superclass::PrintSelf(os, indent);
1870 //----------------------------------------------------------------------------
1873 //----------------------------------------------------------------------------
1874 void vvSlicer::SetVFColor(double r, double g, double b)
1876 double mVFColorHSV[3];
1881 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1882 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1883 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1884 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1888 //----------------------------------------------------------------------------
1891 //----------------------------------------------------------------------------
1892 void vvSlicer::SetRegisterExtent(int ext[6])
1894 copyExtent(ext, mRegisterExtent);
1896 //----------------------------------------------------------------------------
1899 //----------------------------------------------------------------------------
1900 void vvSlicer::GetRegisterExtent(int ext[6])
1902 copyExtent(mRegisterExtent, ext);
1904 //----------------------------------------------------------------------------