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;
383 #if VTK_MAJOR_VERSION <= 5
384 mImageReslice->GetOutput()->SetUpdateExtent(extent);
385 mImageReslice->GetOutput()->Update();
387 mImageReslice->SetUpdateExtent(extent);
388 mImageReslice->Update();
391 this->UpdateDisplayExtent();
394 ca->SetText(0,mFileName.c_str());
397 //------------------------------------------------------------------------------
400 //------------------------------------------------------------------------------
401 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
403 if (overlay->GetVTKImages().size()) {
405 mOverlayVisibility = true;
407 if (!mOverlayReslice) {
408 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
409 mOverlayReslice->SetInterpolationModeToLinear();
410 mOverlayReslice->AutoCropOutputOn();
411 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
414 mConcatenatedOverlayTransform->Identity();
415 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
416 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
417 mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
418 #if VTK_MAJOR_VERSION <= 5
419 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
421 mOverlayReslice->SetInputData(0, mOverlay->GetFirstVTKImageData());
423 mOverlayReslice->Update();
426 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
427 #if VTK_MAJOR_VERSION <= 5
428 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
430 mOverlayMapper->SetInputConnection(mOverlayReslice->GetOutputPort(0));
433 if (!mOverlayActor) {
434 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
435 #if VTK_MAJOR_VERSION <= 5
436 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
438 mOverlayActor->GetMapper()->SetInputConnection(mOverlayMapper->GetOutputPort());
440 mOverlayActor->SetPickable(0);
441 mOverlayActor->SetVisibility(true);
442 mOverlayActor->SetOpacity(0.5);
443 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
444 mOverlayActor->GetMapper()->BorderOn();
448 //stupid but necessary : the Overlay need to be rendered before fusion
450 this->GetRenderer()->RemoveActor(mFusionActor);
451 this->GetRenderer()->AddActor(mOverlayActor);
452 this->GetRenderer()->AddActor(mFusionActor);
454 this->GetRenderer()->AddActor(mOverlayActor);
456 //Synchronize orientation and slice
457 AdjustResliceToSliceOrientation(mOverlayReslice);
458 this->UpdateDisplayExtent();
459 this->SetTSlice(mCurrentTSlice);
462 //------------------------------------------------------------------------------
465 //------------------------------------------------------------------------------
466 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
468 mFusionSequenceCode = fusionSequenceCode;
469 if (fusion->GetVTKImages().size()) {
471 mFusionVisibility = true;
473 if (!mFusionReslice) {
474 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
475 mFusionReslice->SetInterpolationModeToLinear();
476 mFusionReslice->AutoCropOutputOn();
477 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
480 mConcatenatedFusionTransform->Identity();
481 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
482 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
483 mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
484 #if VTK_MAJOR_VERSION <= 5
485 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
487 mFusionReslice->SetInputData(0, mFusion->GetFirstVTKImageData());
489 mFusionReslice->Update();
492 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
494 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
496 lut->SetValueRange(0, 1);
497 lut->SetSaturationRange(0, 0);
499 mFusionMapper->SetLookupTable(lut);
500 #if VTK_MAJOR_VERSION <= 5
501 mFusionMapper->SetInput(mFusionReslice->GetOutput());
503 mFusionMapper->SetInputConnection(mFusionReslice->GetOutputPort(0));
507 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
508 #if VTK_MAJOR_VERSION <= 5
509 mFusionActor->SetInput(mFusionMapper->GetOutput());
511 mFusionActor->GetMapper()->SetInputConnection(mFusionMapper->GetOutputPort());
513 mFusionActor->SetPickable(0);
514 mFusionActor->SetVisibility(true);
515 mFusionActor->SetOpacity(0.7);
516 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
517 mFusionActor->GetMapper()->BorderOn();
520 this->GetRenderer()->AddActor(mFusionActor);
523 //Synchronize orientation and slice
524 AdjustResliceToSliceOrientation(mFusionReslice);
525 this->UpdateDisplayExtent();
526 this->SetTSlice(mCurrentTSlice);
529 //------------------------------------------------------------------------------
532 //------------------------------------------------------------------------------
533 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
536 if (actor_type == "image")
537 vis = mImageVisibility;
538 else if (actor_type == "vector")
540 else if (actor_type == "overlay")
541 vis = mOverlayVisibility;
542 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
543 vis = mFusionVisibility;
544 else if (actor_type == "contour")
545 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
548 //------------------------------------------------------------------------------
550 //------------------------------------------------------------------------------
551 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
553 if (actor_type == "image")
554 mImageVisibility = vis;
555 else if (actor_type == "vector")
557 else if (actor_type == "overlay")
558 mOverlayVisibility = vis;
559 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
560 mFusionVisibility = vis;
561 else if (actor_type == "contour")
562 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
563 UpdateDisplayExtent();
565 //------------------------------------------------------------------------------
567 //------------------------------------------------------------------------------
568 void vvSlicer::SetVF(vvImage::Pointer vf)
570 if (vf->GetVTKImages().size()) {
572 mVFVisibility = true;
575 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
576 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
577 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
579 #if VTK_MAJOR_VERSION <= 5
580 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
581 mAAFilter->SetInput(mVOIFilter->GetOutput());
583 mVOIFilter->SetInputData(vf->GetFirstVTKImageData());
584 mAAFilter->SetInputConnection(mVOIFilter->GetOutputPort());
586 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
587 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
590 mArrow = vtkSmartPointer<vvGlyphSource>::New();
591 mArrow->SetGlyphTypeToSpecificArrow();
592 mArrow->SetScale(mScale);
595 // Glyph the gradient vector (with arrows)
597 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
598 #if VTK_MAJOR_VERSION <= 5
599 mGlyphFilter->SetInput(mAAFilter->GetOutput());
600 mGlyphFilter->SetSource(mArrow->GetOutput());
602 mGlyphFilter->SetInputConnection(mAAFilter->GetOutputPort());
603 mGlyphFilter->SetSourceConnection(mArrow->GetOutputPort());
605 mGlyphFilter->ScalingOn();
606 mGlyphFilter->SetScaleModeToScaleByVector();
607 mGlyphFilter->OrientOn();
608 mGlyphFilter->SetVectorModeToUseVector();
609 mGlyphFilter->SetColorModeToColorByVector();
612 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
614 double mVFColorHSV[3];
615 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
616 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
617 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
618 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
621 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
622 #if VTK_MAJOR_VERSION <= 5
623 mVFMapper->SetInput(mGlyphFilter->GetOutput());
625 mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
627 mVFMapper->ImmediateModeRenderingOn();
628 mVFMapper->SetLookupTable(mVFColorLUT);
631 mVFActor = vtkSmartPointer<vtkActor>::New();
632 mVFActor->SetMapper(mVFMapper);
633 mVFActor->SetPickable(0);
634 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
635 this->UpdateDisplayExtent();
636 this->GetRenderer()->AddActor(mVFActor);
639 SetTSlice(mCurrentTSlice);
642 //------------------------------------------------------------------------------
645 //------------------------------------------------------------------------------
646 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
648 mLandmarks = landmarks;
652 mCross = vtkSmartPointer<vtkCursor3D>::New();
654 mClipBox = vtkSmartPointer<vtkBox>::New();
656 mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
658 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
660 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
662 mLandActor = vtkSmartPointer<vtkActor>::New();
664 mCross->SetFocalPoint(0.0,0.0,0.0);
665 mCross->SetModelBounds(-10,10,-10,10,-10,10);
669 mLandClipper->SetClipFunction(mClipBox);
670 mLandClipper->InsideOutOn();
671 #if VTK_MAJOR_VERSION <= 5
672 mLandClipper->SetInput(mLandmarks->GetOutput());
674 mLandGlyph->SetSource(mCross->GetOutput());
675 mLandGlyph->SetInput(mLandClipper->GetOutput());
677 mLandClipper->SetInputData(mLandmarks->GetOutput());
679 mLandGlyph->SetSourceConnection(mCross->GetOutputPort());
680 mLandGlyph->SetInputConnection(mLandClipper->GetOutputPort());
682 //mLandGlyph->SetIndexModeToScalar();
683 //mLandGlyph->SetRange(0,1);
684 //mLandGlyph->ScalingOff();
686 //mLandGlyph->SetColorModeToColorByScalar();
688 mLandGlyph->SetScaleModeToDataScalingOff();
689 mLandGlyph->SetIndexModeToOff();
691 mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
692 //mLandMapper->ScalarVisibilityOff();
694 mLandActor->SetMapper(mLandMapper);
695 mLandActor->GetProperty()->SetOpacity(0.995);
696 mLandActor->GetProperty()->SetColor(255,10,212);
697 mLandActor->SetPickable(0);
698 mLandActor->SetVisibility(true);
699 this->UpdateDisplayExtent();
700 this->GetRenderer()->AddActor(mLandActor);
703 //------------------------------------------------------------------------------
705 //------------------------------------------------------------------------------
706 //FIXME: this function leaks memory, we should fix it someday :)
707 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
709 if (actor_type == "vector") {
710 Renderer->RemoveActor(mVFActor);
719 if (actor_type == "overlay") {
720 Renderer->RemoveActor(mOverlayActor);
722 mOverlayActor = NULL;
723 mOverlayMapper = NULL;
725 if ( (actor_type == "fusion") || (actor_type == "fusionSequence") ) {
726 Renderer->RemoveActor(mFusionActor);
729 mFusionMapper = NULL;
731 if (actor_type == "contour") {
732 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
733 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
736 //------------------------------------------------------------------------------
739 //------------------------------------------------------------------------------
740 void vvSlicer::SetVFSubSampling(int sub)
743 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
746 UpdateDisplayExtent();
749 //------------------------------------------------------------------------------
752 //------------------------------------------------------------------------------
753 void vvSlicer::SetVFScale(int scale)
757 mArrow->SetScale(mScale);
758 UpdateDisplayExtent();
761 //------------------------------------------------------------------------------
763 //------------------------------------------------------------------------------
764 void vvSlicer::SetVFWidth(int width)
768 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
769 UpdateDisplayExtent();
772 //------------------------------------------------------------------------------
775 //------------------------------------------------------------------------------
776 void vvSlicer::SetVFLog(int log)
780 mGlyphFilter->SetUseLog(mVFLog);
781 mGlyphFilter->Modified();
783 UpdateDisplayExtent();
786 //------------------------------------------------------------------------------
789 //------------------------------------------------------------------------------
790 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
792 if (!updateLinkedImages) {
794 #if VTK_MAJOR_VERSION <= 5
795 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
797 mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
800 mConcatenatedTransform->Identity();
801 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
802 mConcatenatedTransform->Concatenate(mSlicingTransform);
803 UpdateDisplayExtent();
808 else if ((unsigned int)t >= mImage->GetVTKImages().size())
809 mCurrentTSlice = mImage->GetVTKImages().size() -1;
814 mConcatenatedTransform->Identity();
815 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
816 mConcatenatedTransform->Concatenate(mSlicingTransform);
819 #if VTK_MAJOR_VERSION <= 5
820 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
822 mImageReslice->SetInputData( mImage->GetVTKImages()[mCurrentTSlice] );
824 if (mVF && mVFActor->GetVisibility()) {
825 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
826 #if VTK_MAJOR_VERSION <= 5
827 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
829 mVOIFilter->SetInputData(mVF->GetVTKImages()[mCurrentTSlice]);
833 if (mOverlay && mOverlayActor->GetVisibility()) {
834 if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
835 mCurrentOverlayTSlice = t;
836 #if VTK_MAJOR_VERSION <= 5
837 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
839 mOverlayReslice->SetInputData( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
841 // Update overlay transform
842 mConcatenatedOverlayTransform->Identity();
843 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
844 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
847 //update the fusion ; except in case this is a fusionSequence, in which case both 'times' should be independent.
848 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode<0)) {
849 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
850 mCurrentFusionTSlice = t;
851 #if VTK_MAJOR_VERSION <= 5
852 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
854 mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
857 // Update fusion transform
858 mConcatenatedFusionTransform->Identity();
859 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
860 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
863 if (mSurfaceCutActors.size() > 0)
864 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
865 i!=mSurfaceCutActors.end(); i++)
866 (*i)->SetTimeSlice(mCurrentTSlice);
867 UpdateDisplayExtent();
869 //------------------------------------------------------------------------------
872 //------------------------------------------------------------------------------
873 void vvSlicer::SetFusionSequenceTSlice(int t)
875 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode>=0)) {
876 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
877 mCurrentFusionTSlice = t;
878 #if VTK_MAJOR_VERSION <= 5
879 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
881 mFusionReslice->SetInputData( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
883 // Update fusion transform
884 mConcatenatedFusionTransform->Identity();
885 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]); //not really useful...
886 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
890 UpdateDisplayExtent();
892 //------------------------------------------------------------------------------
895 //------------------------------------------------------------------------------
896 int vvSlicer::GetTSlice()
898 return mCurrentTSlice;
900 //------------------------------------------------------------------------------
902 //------------------------------------------------------------------------------
903 int vvSlicer::GetMaxCurrentTSlice()
905 int t = mCurrentTSlice;
907 t = std::max(t, mCurrentOverlayTSlice);
908 if(mFusion&& (mFusionSequenceCode<0)) //ignore fusionSequence data: for these, the times are not to be related (this way)
909 t = std::max(t, mCurrentFusionTSlice);
912 //------------------------------------------------------------------------------
914 //------------------------------------------------------------------------------
915 int vvSlicer::GetFusionTSlice()
917 return mCurrentFusionTSlice;
919 //------------------------------------------------------------------------------
921 //------------------------------------------------------------------------------
922 int vvSlicer::GetOverlayTSlice()
924 return mCurrentOverlayTSlice;
926 //------------------------------------------------------------------------------
928 //------------------------------------------------------------------------------
929 void vvSlicer::SetSliceOrientation(int orientation)
931 //if 2D image, force to watch in Axial View
933 #if VTK_MAJOR_VERSION <= 5
934 this->GetInput()->GetWholeExtent(extent);
936 int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
937 copyExtent(ext, extent);
940 if (extent[5]-extent[4] <= 2)
941 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
943 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
944 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
945 vtkErrorMacro("Error - invalid slice orientation " << orientation);
949 this->SliceOrientation = orientation;
952 AdjustResliceToSliceOrientation(mFusionReslice);
955 AdjustResliceToSliceOrientation(mOverlayReslice);
959 // Go to current cursor position
960 // double* cursorPos = GetCursorPosition();
961 // DDV(cursorPos, 3);
962 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
964 if (mFirstSetSliceOrientation) {
965 int *range = this->GetSliceRange();
967 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
968 #if VTK_MAJOR_VERSION <= 5
969 mFirstSetSliceOrientation = false;
972 else if (this->Renderer && this->GetInput()) {
973 double s = mCursor[orientation];
974 double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
975 this->Slice = static_cast<int>(sCursor);
978 this->UpdateOrientation();
980 this->UpdateDisplayExtent();
982 if (mFirstSetSliceOrientation) {
983 mFirstSetSliceOrientation = false;
986 if (this->Renderer && this->GetInput()) {
987 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
988 this->Renderer->ResetCamera();
989 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
994 //----------------------------------------------------------------------------
996 //------------------------------------------------------------------------------
997 // This function ensures that we sample the slices of a vtkImageReslice filter
998 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
999 // In other words, we change the grid of the reslice in the same way as the grid
1000 // of the displayed image in the slicing direction.
1001 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
1003 // Reset autocrop and update output information
1004 reslice->SetOutputOriginToDefault();
1005 reslice->SetOutputSpacingToDefault();
1006 #if VTK_MAJOR_VERSION <= 5
1007 reslice->GetOutput()->UpdateInformation();
1009 reslice->UpdateInformation();
1012 // Ge new origin / spacing
1015 reslice->GetOutput()->GetOrigin(origin);
1016 reslice->GetOutput()->GetSpacing(spacing);
1018 // Use similar spacing as the image in the direction SliceOrientation
1019 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1021 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
1022 // Step 1: from world coordinates to image coordinates
1023 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1024 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1026 // Step 2: round to nearest grid positionInc. This has been validated as the only
1027 // way to have something consistent with the thickness of a 2D slice visible on the
1028 // other slices. The thickness is accounted for so if the 2D slice is to thin and
1029 // between two slices, one will never be able to see this 2D slice (bug #1883).
1030 origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
1032 // Step 3: back to world coordinates
1033 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
1034 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
1036 // Set new spacing and origin
1037 reslice->SetOutputOrigin(origin);
1038 reslice->SetOutputSpacing(spacing);
1039 reslice->UpdateInformation();
1041 //------------------------------------------------------------------------------
1043 //----------------------------------------------------------------------------
1044 int * vvSlicer::GetExtent()
1047 if (mUseReducedExtent) {
1048 w_ext = mReducedExtent;
1051 #if VTK_MAJOR_VERSION <= 5
1052 w_ext = GetInput()->GetWholeExtent();
1054 w_ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1059 //----------------------------------------------------------------------------
1062 //----------------------------------------------------------------------------
1063 int vvSlicer::GetOrientation()
1065 return this->SliceOrientation;
1067 //----------------------------------------------------------------------------
1070 //----------------------------------------------------------------------------
1071 void vvSlicer::UpdateDisplayExtent()
1073 emit UpdateDisplayExtentBegin(mSlicerNumber);
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);
1095 w_ext[ this->SliceOrientation*2 ] = this->Slice;
1096 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
1099 this->ImageActor->SetVisibility(mImageVisibility);
1100 this->ImageActor->SetDisplayExtent(w_ext);
1102 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1103 // Fix for bug #1882
1104 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
1107 // Overlay image actor
1108 if (mOverlay && mOverlayVisibility) {
1109 AdjustResliceToSliceOrientation(mOverlayReslice);
1111 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mOverlayReslice->GetOutput(), overExtent);
1112 #if VTK_MAJOR_VERSION <= 5
1113 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
1115 bool out = ClipDisplayedExtent(overExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1117 mOverlayActor->SetVisibility(!out);
1118 mOverlayActor->SetDisplayExtent( overExtent );
1119 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1120 // Fix for bug #1882
1121 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
1125 mOverlayActor->SetVisibility(false);
1127 // Fusion image actor
1128 if (mFusion && mFusionVisibility) {
1129 AdjustResliceToSliceOrientation(mFusionReslice);
1131 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mFusionReslice->GetOutput(), fusExtent);
1132 #if VTK_MAJOR_VERSION <= 5
1133 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
1135 bool out = ClipDisplayedExtent(fusExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1137 mFusionActor->SetVisibility(!out);
1138 mFusionActor->SetDisplayExtent( fusExtent );
1139 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1140 // Fix for bug #1882
1141 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1145 mFusionActor->SetVisibility(false);
1146 // Vector field actor
1147 double* camera = Renderer->GetActiveCamera()->GetPosition();
1148 double* image_bounds = ImageActor->GetBounds();
1149 double position[3] = {0, 0, 0};
1150 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1152 //print_vector<double, 6>("camera", camera);
1153 //print_vector<double, 6>("image_bounds", image_bounds);
1154 //print_vector<double, 3>("position", position);
1156 // find where to place the VF actor. to deal with
1157 // z-buffer issues, the VF is placed right in front of the image,
1158 // subject to a small offset. the position actually depends on the
1159 // the location of the camera relative to the image.
1161 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1164 if (mVF && mVFVisibility) {
1166 #if VTK_MAJOR_VERSION <= 5
1167 mVF->GetVTKImages()[0]->UpdateInformation();
1169 //this->UpdateInformation();
1171 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mVF->GetVTKImages()[0], vfExtent);
1172 #if VTK_MAJOR_VERSION <= 5
1173 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1175 bool out = ClipDisplayedExtent(vfExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1177 mVFActor->SetVisibility(!out);
1178 mVOIFilter->SetVOI(vfExtent);
1179 int orientation[3] = {1,1,1};
1180 orientation[this->SliceOrientation] = 0;
1181 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1182 position[this->SliceOrientation] += offset;
1183 mVFActor->SetPosition(position);
1184 mVFActor->GetProperty()->SetOpacity(0.995);
1185 mVFMapper->Update();
1189 mVFActor->SetVisibility(false);
1193 for(unsigned int i=0; i<6; i++)
1194 boundsT[i] = ImageActor->GetBounds()[i];
1195 boundsT[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1196 boundsT[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1205 position[this->SliceOrientation] = offset;
1206 mLandActor->SetPosition(position);
1209 // Figure out the correct clipping range
1210 if (this->Renderer) {
1211 if (this->InteractorStyle &&
1212 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1213 this->Renderer->ResetCameraClippingRange();
1215 vtkCamera *cam = this->Renderer->GetActiveCamera();
1218 this->ImageActor->GetBounds(bounds);
1219 double spos = (double)bounds[this->SliceOrientation * 2];
1220 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1221 double range = fabs(spos - cpos);
1222 double *spacing = input->GetSpacing();
1223 double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1224 cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1234 emit UpdateDisplayExtentEnd(mSlicerNumber);
1236 //----------------------------------------------------------------------------
1238 //----------------------------------------------------------------------------
1239 void vvSlicer::ConvertImageToImageDisplayExtent(vtkInformation *sourceImage, const int sourceExtent[6],
1240 vtkImageData *targetImage, int targetExtent[6])
1243 double *origin, *spacing;
1244 origin = sourceImage->Get(vtkDataObject::ORIGIN());
1245 spacing = sourceImage->Get(vtkDataObject::SPACING());
1246 for(unsigned int i=0; i<6; i++) {
1247 // From source voxel coordinates to world coordinates
1248 dExtents[i] = origin[i/2] + spacing[i/2] * sourceExtent[i];
1250 // From world coordinates to floating point target voxel coordinates
1251 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1253 // Round to current slice or larger extent
1254 if(i/2==this->GetOrientation())
1255 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1257 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1259 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1262 //----------------------------------------------------------------------------
1264 //----------------------------------------------------------------------------
1265 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1270 for (int i = 0; i < maxBound; i = i+2) {
1271 //if we are totally outside the image
1272 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1276 //crop to the limit of the image
1277 extent[i] = std::max(extent[i], refExtent[i]);
1278 extent[i] = std::min(extent[i], refExtent[i+1]);;
1279 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1280 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1283 for (int i = 0; i < maxBound; i = i+2) {
1284 extent[i] = refExtent[i];
1285 extent[i+1] = refExtent[i];
1289 //----------------------------------------------------------------------------
1292 //----------------------------------------------------------------------------
1293 void vvSlicer::UpdateOrientation()
1295 // Set the camera position
1296 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1298 switch (this->SliceOrientation) {
1299 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1300 cam->SetFocalPoint(0,0,0);
1301 cam->SetPosition(0,0,-1); // -1 if medical ?
1302 cam->SetViewUp(0,-1,0);
1305 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1306 cam->SetFocalPoint(0,0,0);
1307 cam->SetPosition(0,-1,0); // 1 if medical ?
1308 cam->SetViewUp(0,0,1);
1311 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1312 cam->SetFocalPoint(0,0,0);
1313 cam->SetPosition(-1,0,0); // -1 if medical ?
1314 cam->SetViewUp(0,0,1);
1319 //----------------------------------------------------------------------------
1322 //----------------------------------------------------------------------------
1323 void vvSlicer::SetOpacity(double s)
1325 this->GetImageActor()->SetOpacity(s);
1327 //----------------------------------------------------------------------------
1330 //----------------------------------------------------------------------------
1331 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1333 this->Superclass::SetRenderWindow(rw);
1334 this->SetupInteractor(rw->GetInteractor());
1335 ca->SetImageActor(this->GetImageActor());
1336 ca->SetWindowLevel(this->GetWindowLevel());
1337 ca->SetText(3, "<window>\n<level>");
1348 crossCursor->SetModelBounds(bounds);
1350 this->GetRenderer()->AddActor(pdmA);
1351 this->GetRenderer()->AddActor(ca);
1352 this->GetRenderer()->ResetCamera();
1354 //this is just a mapping between the labeling of the orientations presented to the user and
1355 //the one used by vtk
1356 SetSliceOrientation(2-(orientation%3));
1359 //----------------------------------------------------------------------------
1362 //----------------------------------------------------------------------------
1363 void vvSlicer::ResetCamera()
1365 this->GetRenderer()->ResetCamera();
1367 //----------------------------------------------------------------------------
1370 //----------------------------------------------------------------------------
1371 void vvSlicer::SetDisplayMode(bool i)
1373 this->GetRenderer()->SetDraw(i);
1374 if (i) UpdateDisplayExtent();
1376 //----------------------------------------------------------------------------
1379 //----------------------------------------------------------------------------
1380 void vvSlicer::FlipHorizontalView()
1382 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1384 double *position = cam->GetPosition();
1385 double factor[3] = {1, 1, 1};
1386 factor[this->SliceOrientation] = -1;
1387 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1389 /* switch (this->SliceOrientation) {
1390 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1391 cam->SetPosition(position[0],position[1],-position[2]);
1394 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1395 cam->SetPosition(position[0],-position[1],position[2]);
1398 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1399 cam->SetPosition(-position[0],position[1],position[2]);
1403 this->Renderer->ResetCameraClippingRange();
1404 this->UpdateDisplayExtent();
1407 //----------------------------------------------------------------------------
1410 //----------------------------------------------------------------------------
1411 void vvSlicer::FlipVerticalView()
1413 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1415 FlipHorizontalView();
1416 double *viewup = cam->GetViewUp();
1417 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1418 this->UpdateDisplayExtent();
1421 //----------------------------------------------------------------------------
1424 //----------------------------------------------------------------------------
1425 void vvSlicer::SetColorWindow(double window)
1427 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1429 double level = this->GetWindowLevel()->GetLevel();
1430 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1433 this->vtkImageViewer2::SetColorWindow(window);
1435 //----------------------------------------------------------------------------
1437 //----------------------------------------------------------------------------
1438 void vvSlicer::SetColorLevel(double level)
1440 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1442 double window = this->GetWindowLevel()->GetWindow();
1443 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1446 this->vtkImageViewer2::SetColorLevel(level);
1448 //----------------------------------------------------------------------------
1450 //----------------------------------------------------------------------------
1451 double vvSlicer::GetOverlayColorWindow()
1454 return mOverlayMapper->GetWindow();
1458 //----------------------------------------------------------------------------
1460 //----------------------------------------------------------------------------
1461 double vvSlicer::GetOverlayColorLevel()
1464 return mOverlayMapper->GetLevel();
1468 //----------------------------------------------------------------------------
1470 //----------------------------------------------------------------------------
1471 void vvSlicer::SetOverlayColorWindow(double window)
1474 mOverlayMapper->SetWindow(window);
1476 //----------------------------------------------------------------------------
1478 //----------------------------------------------------------------------------
1479 void vvSlicer::SetOverlayColorLevel(double level)
1482 mOverlayMapper->SetLevel(level);
1484 //----------------------------------------------------------------------------
1486 //----------------------------------------------------------------------------
1487 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1488 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1490 //Get mouse pointer position in view coordinates
1493 for(int i=0; i<3; i++) {
1494 corner1[i] = mCurrent[i];
1495 corner2[i] = mCurrent[i];
1498 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1499 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1501 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1503 this->Renderer->GetTiledSize(&w, &h);
1504 corner1[0] -= 0.2*h/(double)w;
1505 corner2[0] += 0.2*h/(double)w;
1508 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1509 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1511 //Convert to image pixel coordinates (rounded)
1512 transform->TransformPoint(corner1, corner1);
1513 transform->TransformPoint(corner2, corner2);
1514 int iLocalExtents[6];
1515 for(int i=0; i<3; i++) {
1516 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1517 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1519 iLocalExtents[i*2 ] = lrint(corner1[i]);
1520 iLocalExtents[i*2+1] = lrint(corner2[i]);
1522 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1523 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1526 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1527 #if VTK_MAJOR_VERSION <= 5
1528 voiFilter->SetInput(image);
1530 voiFilter->SetInputData(image);
1532 voiFilter->SetVOI(iLocalExtents);
1533 voiFilter->Update();
1534 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1540 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1541 #if VTK_MAJOR_VERSION <= 5
1542 accFilter->SetInput(voiFilter->GetOutput());
1544 accFilter->SetInputConnection(voiFilter->GetOutputPort(0));
1546 accFilter->Update();
1548 min = *(accFilter->GetMin());
1549 max = *(accFilter->GetMax());
1551 //----------------------------------------------------------------------------
1553 //----------------------------------------------------------------------------
1554 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1559 #if VTK_MAJOR_VERSION <= 5
1560 if (ix < image->GetWholeExtent()[0] ||
1561 ix > image->GetWholeExtent()[1] ||
1562 iy < image->GetWholeExtent()[2] ||
1563 iy > image->GetWholeExtent()[3] ||
1564 iz < image->GetWholeExtent()[4] ||
1565 iz > image->GetWholeExtent()[5] )
1566 return std::numeric_limits<double>::quiet_NaN();
1567 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1570 if (ix < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0] ||
1571 ix > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1] ||
1572 iy < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2] ||
1573 iy > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3] ||
1574 iz < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4] ||
1575 iz > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5] )
1576 return std::numeric_limits<double>::quiet_NaN();
1577 //image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1581 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1583 //----------------------------------------------------------------------------
1585 //----------------------------------------------------------------------------
1586 void vvSlicer::Render()
1588 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1589 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1590 legend->UseOpacityOn();
1591 legend->SetVisibility(1);
1593 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1594 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1595 legend->UseOpacityOff();
1596 legend->SetVisibility(1);
1597 } else legend->SetVisibility(0);
1599 if (ca->GetVisibility()) {
1601 std::stringstream worldPos(" ");
1603 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1604 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1605 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1606 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1607 #if VTK_MAJOR_VERSION <= 5
1608 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1609 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1610 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1611 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1612 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1613 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1617 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1619 if(ImageActor->GetVisibility())
1620 worldPos << "data value : " << value << std::endl;
1622 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1623 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1624 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1627 worldPos << "pixel : " << ix << ' '
1634 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0]-0.5 &&
1635 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1]+0.5 &&
1636 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2]-0.5 &&
1637 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3]+0.5 &&
1638 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4]-0.5 &&
1639 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5]+0.5) {
1642 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1644 if(ImageActor->GetVisibility())
1645 worldPos << "data value : " << value << std::endl;
1647 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1648 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1649 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1652 worldPos << "pixel : " << ix << ' '
1660 ca->SetText(1,worldPos.str().c_str());
1662 std::stringstream slicePos;
1663 slicePos << "Slice: " << this->GetSlice();
1664 ca->SetText(2, slicePos.str().c_str());
1667 if (pdmA->GetVisibility()) {
1668 double x = mCursor[0];
1669 double y = mCursor[1];
1670 double z = mCursor[2];
1671 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1672 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1673 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1674 #if VTK_MAJOR_VERSION <= 5
1675 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1676 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1677 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1678 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1679 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1680 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1681 vtkRenderer * renderer = this->Renderer;
1683 renderer->WorldToView(x,y,z);
1684 renderer->ViewToNormalizedViewport(x,y,z);
1685 renderer->NormalizedViewportToViewport(x,y);
1686 renderer->ViewportToNormalizedDisplay(x,y);
1687 renderer->NormalizedDisplayToDisplay(x,y);
1688 crossCursor->SetFocalPoint(x,y,z);
1690 crossCursor->SetFocalPoint(-1,-1,z);
1691 crossCursor->Update();
1694 vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1696 mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1697 } catch (const std::bad_cast& e) {
1698 std::cerr << e.what() << std::endl;
1699 std::cerr << "Conversion error" << std::endl;
1703 if (xCursor >= mapperOpenGL->GetCroppingRegion()[0]-0.5 &&
1704 xCursor < mapperOpenGL->GetCroppingRegion()[1]+0.5 &&
1705 yCursor >= mapperOpenGL->GetCroppingRegion()[2]-0.5 &&
1706 yCursor < mapperOpenGL->GetCroppingRegion()[3]+0.5 &&
1707 zCursor >= mapperOpenGL->GetCroppingRegion()[4]-0.5 &&
1708 zCursor < mapperOpenGL->GetCroppingRegion()[5]+0.5 ) {
1709 vtkRenderer * renderer = this->Renderer;
1711 renderer->WorldToView(x,y,z);
1712 renderer->ViewToNormalizedViewport(x,y,z);
1713 renderer->NormalizedViewportToViewport(x,y);
1714 renderer->ViewportToNormalizedDisplay(x,y);
1715 renderer->NormalizedDisplayToDisplay(x,y);
1716 crossCursor->SetFocalPoint(x,y,z);
1718 crossCursor->SetFocalPoint(-1,-1,z);
1719 crossCursor->Update();
1723 if (mOverlay && mOverlayActor->GetVisibility()) {
1724 if(mLinkOverlayWindowLevel) {
1725 mOverlayMapper->SetWindow(this->GetColorWindow());
1726 mOverlayMapper->SetLevel(this->GetColorLevel());
1728 #if VTK_MAJOR_VERSION <= 5
1729 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1731 mOverlayMapper->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1733 mOverlayMapper->Update();
1735 if (mFusion && mFusionActor->GetVisibility()) {
1736 #if VTK_MAJOR_VERSION <= 5
1737 mFusionMapper->GetOutput()->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1739 mFusionMapper->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1741 mFusionMapper->Update();
1748 this->GetRenderWindow()->Render();
1750 //----------------------------------------------------------------------------
1753 //----------------------------------------------------------------------------
1754 void vvSlicer::UpdateCursorPosition()
1756 pdmA->SetVisibility(true);
1757 mCursor[0] = mCurrent[0];
1758 mCursor[1] = mCurrent[1];
1759 mCursor[2] = mCurrent[2];
1760 mCursor[3] = mCurrentTSlice;
1762 //----------------------------------------------------------------------------
1765 //----------------------------------------------------------------------------
1766 void vvSlicer::RemoveLandmarks()
1768 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1769 if (pd->GetPoints()) {
1771 //First remove all captions:
1772 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1773 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1774 //allActors2D->Remove (mLandLabelActors[i]);
1776 mLandLabelActors.clear();
1779 //----------------------------------------------------------------------------
1782 //----------------------------------------------------------------------------
1783 void vvSlicer::DisplayLandmarks()
1787 for(unsigned int i=0; i<6; i++)
1788 bounds[i] = ImageActor->GetBounds()[i];
1789 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1790 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1791 mClipBox->SetBounds(bounds);
1794 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1795 if (pd->GetPoints()) {
1796 //mLandGlyph->SetRange(0,1);
1797 //mLandGlyph->Modified();
1798 //mLandGlyph->Update();
1800 mClipBox->Modified();
1801 mLandClipper->Update();
1802 mLandMapper->Update();
1803 //Next add the captions to the displayed points
1804 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1805 double *position = mLandClipper->GetOutput()->GetPoint(id);
1806 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1807 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1808 label_actor->SetCaption(label);
1809 label_actor->SetAttachmentPoint(position);
1810 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1811 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1812 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1813 label_actor->GetCaptionTextProperty()->SetBold(0);
1814 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1815 label_actor->BorderOff();
1816 label_actor->LeaderOff();
1817 label_actor->ThreeDimensionalLeaderOff();
1818 mLandLabelActors.push_back(label_actor);
1819 this->Renderer->AddActor2D(mLandLabelActors[id]);
1824 //----------------------------------------------------------------------------
1827 //----------------------------------------------------------------------------
1828 void vvSlicer::SetSlice(int slice)
1830 int *range = this->GetSliceRange();
1832 if (slice < range[0]) {
1834 } else if (slice > range[1]) {
1839 if (this->Slice == slice) {
1843 this->Slice = slice;
1846 this->UpdateDisplayExtent();
1848 // Seems to work without this line
1851 //----------------------------------------------------------------------------
1853 //----------------------------------------------------------------------------
1854 int vvSlicer::GetTMax()
1856 int tmax = (int)mImage->GetVTKImages().size() - 1;
1858 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1861 //----------------------------------------------------------------------------
1863 //----------------------------------------------------------------------------
1864 void vvSlicer::SetContourSlice()
1866 if (mSurfaceCutActors.size() > 0)
1867 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1868 i!=mSurfaceCutActors.end(); i++) {
1870 (*i)->SetSlicingOrientation(this->SliceOrientation);
1871 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1872 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1875 //----------------------------------------------------------------------------
1878 //----------------------------------------------------------------------------
1879 void vvSlicer::ForceUpdateDisplayExtent()
1881 this->UpdateDisplayExtent();
1883 //----------------------------------------------------------------------------
1886 //----------------------------------------------------------------------------
1887 int* vvSlicer::GetDisplayExtent()
1889 return this->GetImageActor()->GetDisplayExtent();
1891 //----------------------------------------------------------------------------
1894 //----------------------------------------------------------------------------
1895 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1897 this->Superclass::PrintSelf(os, indent);
1899 //----------------------------------------------------------------------------
1902 //----------------------------------------------------------------------------
1903 void vvSlicer::SetVFColor(double r, double g, double b)
1905 double mVFColorHSV[3];
1910 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1911 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1912 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1913 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1917 //----------------------------------------------------------------------------
1920 //----------------------------------------------------------------------------
1921 void vvSlicer::SetRegisterExtent(int ext[6])
1923 copyExtent(ext, mRegisterExtent);
1925 //----------------------------------------------------------------------------
1928 //----------------------------------------------------------------------------
1929 void vvSlicer::GetRegisterExtent(int ext[6])
1931 copyExtent(mRegisterExtent, ext);
1933 //----------------------------------------------------------------------------