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 //------------------------------------------------------------------------------
93 { //out << __func__ << endl;
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()
179 { //out << __func__ << endl;
180 return mOverlayMapper.GetPointer();
182 //------------------------------------------------------------------------------
185 //------------------------------------------------------------------------------
186 vvBlendImageActor* vvSlicer::GetOverlayActor()
187 { //out << __func__ << endl;
188 return mOverlayActor.GetPointer();
190 //------------------------------------------------------------------------------
193 //------------------------------------------------------------------------------
194 vtkImageMapToColors* vvSlicer::GetFusionMapper()
195 { //out << __func__ << endl;
196 return mFusionMapper.GetPointer();
198 //------------------------------------------------------------------------------
201 //------------------------------------------------------------------------------
202 vtkImageActor* vvSlicer::GetFusionActor()
203 { //out << __func__ << endl;
204 return mFusionActor.GetPointer();
206 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
210 vtkActor* vvSlicer::GetVFActor()
211 { //out << __func__ << endl;
212 return mVFActor.GetPointer();
214 //------------------------------------------------------------------------------
217 //------------------------------------------------------------------------------
218 vtkCornerAnnotation* vvSlicer::GetAnnotation()
219 { //out << __func__ << endl;
220 return ca.GetPointer();
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 void vvSlicer::EnableReducedExtent(bool b)
227 { //out << __func__ << endl;
228 mUseReducedExtent = b;
230 //------------------------------------------------------------------------------
233 //------------------------------------------------------------------------------
234 void vvSlicer::SetReducedExtent(int * ext)
235 { //out << __func__ << endl;
236 copyExtent(ext, mReducedExtent);
238 //------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------
242 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
243 { //out << __func__ << endl;
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()
260 { //out << __func__ << endl;
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)
270 { //out << __func__ << endl;
271 pdmA->GetProperty()->SetColor(r,g,b);
273 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 void vvSlicer::SetCursorVisibility(bool s)
278 { //out << __func__ << endl;
279 pdmA->SetVisibility(s);
281 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
285 bool vvSlicer::GetCursorVisibility()
286 { //out << __func__ << endl;
287 return pdmA->GetVisibility();
289 //------------------------------------------------------------------------------
292 //------------------------------------------------------------------------------
293 void vvSlicer::SetCornerAnnotationVisibility(bool s)
294 { //out << __func__ << endl;
295 ca->SetVisibility(s);
297 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 bool vvSlicer::GetCornerAnnotationVisibility()
302 { //out << __func__ << endl;
303 return ca->GetVisibility();
305 //------------------------------------------------------------------------------
308 //------------------------------------------------------------------------------
309 vvSlicer::~vvSlicer()
310 { //out << __func__ << endl;
311 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
312 i!=mSurfaceCutActors.end(); i++)
314 delete [] mReducedExtent;
316 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 double* vvSlicer::GetCurrentPosition()
320 { //out << __func__ << endl;
321 return mCurrentBeforeSlicingTransform;
323 //------------------------------------------------------------------------------
325 //------------------------------------------------------------------------------
326 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
327 { //out << __func__ << endl;
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)
339 { //out << __func__ << endl;
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)
403 { //out << __func__ << endl;
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)
468 { //out << __func__ << endl;
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)
535 { //out << __func__ << endl;
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)
553 { //out << __func__ << endl;
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)
570 { //out << __func__ << endl;
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)
648 { //out << __func__ << endl;
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)
709 { //out << __func__ << endl;
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)
742 { //out << __func__ << endl;
744 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
747 UpdateDisplayExtent();
750 //------------------------------------------------------------------------------
753 //------------------------------------------------------------------------------
754 void vvSlicer::SetVFScale(int scale)
755 { //out << __func__ << endl;
758 mArrow->SetScale(mScale);
759 UpdateDisplayExtent();
762 //------------------------------------------------------------------------------
764 //------------------------------------------------------------------------------
765 void vvSlicer::SetVFWidth(int width)
766 { //out << __func__ << endl;
769 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
770 UpdateDisplayExtent();
773 //------------------------------------------------------------------------------
776 //------------------------------------------------------------------------------
777 void vvSlicer::SetVFLog(int log)
778 { //out << __func__ << endl;
781 mGlyphFilter->SetUseLog(mVFLog);
782 mGlyphFilter->Modified();
784 UpdateDisplayExtent();
787 //------------------------------------------------------------------------------
790 //------------------------------------------------------------------------------
791 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
792 { //out << __func__ << endl;
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)
875 { //out << __func__ << endl;
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()
898 { //out << __func__ << endl;
899 return mCurrentTSlice;
901 //------------------------------------------------------------------------------
903 //------------------------------------------------------------------------------
904 int vvSlicer::GetMaxCurrentTSlice()
905 { //out << __func__ << endl;
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()
917 { //out << __func__ << endl;
918 return mCurrentFusionTSlice;
920 //------------------------------------------------------------------------------
922 //------------------------------------------------------------------------------
923 int vvSlicer::GetOverlayTSlice()
924 { //out << __func__ << endl;
925 return mCurrentOverlayTSlice;
927 //------------------------------------------------------------------------------
929 //------------------------------------------------------------------------------
930 void vvSlicer::SetSliceOrientation(int orientation)
931 { //out << __func__ << endl;
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)
1003 { //out << __func__ << endl;
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()
1046 { //out << __func__ << endl;
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()
1065 { //out << __func__ << endl;
1066 return this->SliceOrientation;
1068 //----------------------------------------------------------------------------
1071 //----------------------------------------------------------------------------
1072 void vvSlicer::UpdateDisplayExtent()
1073 { //out << __func__ << endl;
1075 vtkImageData *input = this->GetInput();
1076 if (!input || !this->ImageActor) {
1080 #if VTK_MAJOR_VERSION <= 5
1081 input->UpdateInformation();
1083 mRegisterExtent = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1085 this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
1087 // Local copy of extent
1089 int* ext = mImageReslice->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
1090 copyExtent(ext, w_ext);
1091 if (mUseReducedExtent) {
1092 copyExtent(mReducedExtent, w_ext);
1097 w_ext[ this->SliceOrientation*2 ] = this->Slice;
1098 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
1101 this->ImageActor->SetVisibility(mImageVisibility);
1102 this->ImageActor->SetDisplayExtent(w_ext);
1104 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1105 // Fix for bug #1882
1106 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
1109 // Overlay image actor
1110 if (mOverlay && mOverlayVisibility) {
1111 AdjustResliceToSliceOrientation(mOverlayReslice);
1113 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mOverlayReslice->GetOutput(), overExtent);
1114 #if VTK_MAJOR_VERSION <= 5
1115 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
1117 bool out = ClipDisplayedExtent(overExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1119 mOverlayActor->SetVisibility(!out);
1120 mOverlayActor->SetDisplayExtent( overExtent );
1121 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1122 // Fix for bug #1882
1123 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
1127 mOverlayActor->SetVisibility(false);
1129 // Fusion image actor
1130 if (mFusion && mFusionVisibility) {
1131 AdjustResliceToSliceOrientation(mFusionReslice);
1133 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mFusionReslice->GetOutput(), fusExtent);
1134 #if VTK_MAJOR_VERSION <= 5
1135 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
1137 bool out = ClipDisplayedExtent(fusExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1139 mFusionActor->SetVisibility(!out);
1140 mFusionActor->SetDisplayExtent( fusExtent );
1141 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1142 // Fix for bug #1882
1143 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1147 mFusionActor->SetVisibility(false);
1148 // Vector field actor
1149 double* camera = Renderer->GetActiveCamera()->GetPosition();
1150 double* image_bounds = ImageActor->GetBounds();
1151 double position[3] = {0, 0, 0};
1152 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1154 //print_vector<double, 6>("camera", camera);
1155 //print_vector<double, 6>("image_bounds", image_bounds);
1156 //print_vector<double, 3>("position", position);
1158 // find where to place the VF actor. to deal with
1159 // z-buffer issues, the VF is placed right in front of the image,
1160 // subject to a small offset. the position actually depends on the
1161 // the location of the camera relative to the image.
1163 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1166 if (mVF && mVFVisibility) {
1168 #if VTK_MAJOR_VERSION <= 5
1169 mVF->GetVTKImages()[0]->UpdateInformation();
1171 //this->UpdateInformation();
1173 this->ConvertImageToImageDisplayExtent(mImageReslice->GetOutputInformation(0), w_ext, mVF->GetVTKImages()[0], vfExtent);
1174 #if VTK_MAJOR_VERSION <= 5
1175 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1177 bool out = ClipDisplayedExtent(vfExtent, mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT()));
1179 mVFActor->SetVisibility(!out);
1180 mVOIFilter->SetVOI(vfExtent);
1181 int orientation[3] = {1,1,1};
1182 orientation[this->SliceOrientation] = 0;
1183 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1184 position[this->SliceOrientation] += offset;
1185 mVFActor->SetPosition(position);
1186 mVFActor->GetProperty()->SetOpacity(0.995);
1187 mVFMapper->Update();
1191 mVFActor->SetVisibility(false);
1197 for(unsigned int i=0; i<6; i++)
1198 bounds[i] = ImageActor->GetBounds()[i];
1199 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1200 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1201 mClipBox->SetBounds(bounds);
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);
1229 //----------------------------------------------------------------------------
1231 //----------------------------------------------------------------------------
1232 void vvSlicer::ConvertImageToImageDisplayExtent(vtkInformation *sourceImage, const int sourceExtent[6],
1233 vtkImageData *targetImage, int targetExtent[6])
1234 { //out << __func__ << endl;
1236 double *origin, *spacing;
1237 origin = sourceImage->Get(vtkDataObject::ORIGIN());
1238 spacing = sourceImage->Get(vtkDataObject::SPACING());
1239 for(unsigned int i=0; i<6; i++) {
1240 // From source voxel coordinates to world coordinates
1241 dExtents[i] = origin[i/2] + spacing[i/2] * sourceExtent[i];
1243 // From world coordinates to floating point target voxel coordinates
1244 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1246 // Round to current slice or larger extent
1247 if(i/2==this->GetOrientation())
1248 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1250 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1252 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1255 //----------------------------------------------------------------------------
1257 //----------------------------------------------------------------------------
1258 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1259 { //out << __func__ << endl;
1263 for (int i = 0; i < maxBound; i = i+2) {
1264 //if we are totally outside the image
1265 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1269 //crop to the limit of the image
1270 extent[i] = std::max(extent[i], refExtent[i]);
1271 extent[i] = std::min(extent[i], refExtent[i+1]);;
1272 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1273 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1276 for (int i = 0; i < maxBound; i = i+2) {
1277 extent[i] = refExtent[i];
1278 extent[i+1] = refExtent[i];
1282 //----------------------------------------------------------------------------
1285 //----------------------------------------------------------------------------
1286 void vvSlicer::UpdateOrientation()
1287 { //out << __func__ << endl;
1288 // Set the camera position
1289 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1291 switch (this->SliceOrientation) {
1292 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1293 cam->SetFocalPoint(0,0,0);
1294 cam->SetPosition(0,0,-1); // -1 if medical ?
1295 cam->SetViewUp(0,-1,0);
1298 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1299 cam->SetFocalPoint(0,0,0);
1300 cam->SetPosition(0,-1,0); // 1 if medical ?
1301 cam->SetViewUp(0,0,1);
1304 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1305 cam->SetFocalPoint(0,0,0);
1306 cam->SetPosition(-1,0,0); // -1 if medical ?
1307 cam->SetViewUp(0,0,1);
1312 //----------------------------------------------------------------------------
1315 //----------------------------------------------------------------------------
1316 void vvSlicer::SetOpacity(double s)
1317 { //out << __func__ << endl;
1318 this->GetImageActor()->SetOpacity(s);
1320 //----------------------------------------------------------------------------
1323 //----------------------------------------------------------------------------
1324 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1325 { //out << __func__ << endl;
1326 this->Superclass::SetRenderWindow(rw);
1327 this->SetupInteractor(rw->GetInteractor());
1328 ca->SetImageActor(this->GetImageActor());
1329 ca->SetWindowLevel(this->GetWindowLevel());
1330 ca->SetText(3, "<window>\n<level>");
1341 crossCursor->SetModelBounds(bounds);
1343 this->GetRenderer()->AddActor(pdmA);
1344 this->GetRenderer()->AddActor(ca);
1345 this->GetRenderer()->ResetCamera();
1347 //this is just a mapping between the labeling of the orientations presented to the user and
1348 //the one used by vtk
1349 SetSliceOrientation(2-(orientation%3));
1352 //----------------------------------------------------------------------------
1355 //----------------------------------------------------------------------------
1356 void vvSlicer::ResetCamera()
1357 { //out << __func__ << endl;
1358 this->GetRenderer()->ResetCamera();
1360 //----------------------------------------------------------------------------
1363 //----------------------------------------------------------------------------
1364 void vvSlicer::SetDisplayMode(bool i)
1365 { //out << __func__ << endl;
1366 this->GetRenderer()->SetDraw(i);
1367 if (i) UpdateDisplayExtent();
1369 //----------------------------------------------------------------------------
1372 //----------------------------------------------------------------------------
1373 void vvSlicer::FlipHorizontalView()
1374 { //out << __func__ << endl;
1375 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1377 double *position = cam->GetPosition();
1378 double factor[3] = {1, 1, 1};
1379 factor[this->SliceOrientation] = -1;
1380 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1382 /* switch (this->SliceOrientation) {
1383 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1384 cam->SetPosition(position[0],position[1],-position[2]);
1387 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1388 cam->SetPosition(position[0],-position[1],position[2]);
1391 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1392 cam->SetPosition(-position[0],position[1],position[2]);
1396 this->Renderer->ResetCameraClippingRange();
1397 this->UpdateDisplayExtent();
1400 //----------------------------------------------------------------------------
1403 //----------------------------------------------------------------------------
1404 void vvSlicer::FlipVerticalView()
1405 { //out << __func__ << endl;
1406 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1408 FlipHorizontalView();
1409 double *viewup = cam->GetViewUp();
1410 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1411 this->UpdateDisplayExtent();
1414 //----------------------------------------------------------------------------
1417 //----------------------------------------------------------------------------
1418 void vvSlicer::SetColorWindow(double window)
1419 { //out << __func__ << endl;
1420 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1422 double level = this->GetWindowLevel()->GetLevel();
1423 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1426 this->vtkImageViewer2::SetColorWindow(window);
1428 //----------------------------------------------------------------------------
1430 //----------------------------------------------------------------------------
1431 void vvSlicer::SetColorLevel(double level)
1432 { //out << __func__ << endl;
1433 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1435 double window = this->GetWindowLevel()->GetWindow();
1436 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1439 this->vtkImageViewer2::SetColorLevel(level);
1441 //----------------------------------------------------------------------------
1443 //----------------------------------------------------------------------------
1444 double vvSlicer::GetOverlayColorWindow()
1445 { //out << __func__ << endl;
1447 return mOverlayMapper->GetWindow();
1451 //----------------------------------------------------------------------------
1453 //----------------------------------------------------------------------------
1454 double vvSlicer::GetOverlayColorLevel()
1455 { //out << __func__ << endl;
1457 return mOverlayMapper->GetLevel();
1461 //----------------------------------------------------------------------------
1463 //----------------------------------------------------------------------------
1464 void vvSlicer::SetOverlayColorWindow(double window)
1465 { //out << __func__ << endl;
1467 mOverlayMapper->SetWindow(window);
1469 //----------------------------------------------------------------------------
1471 //----------------------------------------------------------------------------
1472 void vvSlicer::SetOverlayColorLevel(double level)
1473 { //out << __func__ << endl;
1475 mOverlayMapper->SetLevel(level);
1477 //----------------------------------------------------------------------------
1479 //----------------------------------------------------------------------------
1480 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1481 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1482 { //out << __func__ << endl;
1483 //Get mouse pointer position in view coordinates
1486 for(int i=0; i<3; i++) {
1487 corner1[i] = mCurrent[i];
1488 corner2[i] = mCurrent[i];
1491 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1492 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1494 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1496 this->Renderer->GetTiledSize(&w, &h);
1497 corner1[0] -= 0.2*h/(double)w;
1498 corner2[0] += 0.2*h/(double)w;
1501 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1502 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1504 //Convert to image pixel coordinates (rounded)
1505 transform->TransformPoint(corner1, corner1);
1506 transform->TransformPoint(corner2, corner2);
1507 int iLocalExtents[6];
1508 for(int i=0; i<3; i++) {
1509 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1510 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1512 iLocalExtents[i*2 ] = lrint(corner1[i]);
1513 iLocalExtents[i*2+1] = lrint(corner2[i]);
1515 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1516 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1519 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1520 #if VTK_MAJOR_VERSION <= 5
1521 voiFilter->SetInput(image);
1523 voiFilter->SetInputData(image);
1525 voiFilter->SetVOI(iLocalExtents);
1526 voiFilter->Update();
1527 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1533 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1534 #if VTK_MAJOR_VERSION <= 5
1535 accFilter->SetInput(voiFilter->GetOutput());
1537 accFilter->SetInputConnection(voiFilter->GetOutputPort(0));
1539 accFilter->Update();
1541 min = *(accFilter->GetMin());
1542 max = *(accFilter->GetMax());
1544 //----------------------------------------------------------------------------
1546 //----------------------------------------------------------------------------
1547 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1548 { //out << __func__ << endl;
1552 #if VTK_MAJOR_VERSION <= 5
1553 if (ix < image->GetWholeExtent()[0] ||
1554 ix > image->GetWholeExtent()[1] ||
1555 iy < image->GetWholeExtent()[2] ||
1556 iy > image->GetWholeExtent()[3] ||
1557 iz < image->GetWholeExtent()[4] ||
1558 iz > image->GetWholeExtent()[5] )
1559 return std::numeric_limits<double>::quiet_NaN();
1560 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1563 if (ix < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0] ||
1564 ix > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1] ||
1565 iy < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2] ||
1566 iy > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3] ||
1567 iz < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4] ||
1568 iz > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5] )
1569 return std::numeric_limits<double>::quiet_NaN();
1570 //image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1574 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1576 //----------------------------------------------------------------------------
1578 //----------------------------------------------------------------------------
1579 void vvSlicer::Render()
1580 { //out << __func__ << endl;
1581 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1582 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1583 legend->UseOpacityOn();
1584 legend->SetVisibility(1);
1586 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1587 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1588 legend->UseOpacityOff();
1589 legend->SetVisibility(1);
1590 } else legend->SetVisibility(0);
1592 if (ca->GetVisibility()) {
1594 std::stringstream worldPos(" ");
1596 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1597 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1598 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1599 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1600 #if VTK_MAJOR_VERSION <= 5
1601 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1602 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1603 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1604 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1605 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1606 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1610 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1612 if(ImageActor->GetVisibility())
1613 worldPos << "data value : " << value << std::endl;
1615 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1616 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1617 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1620 worldPos << "pixel : " << ix << ' '
1627 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0]-0.5 &&
1628 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1]+0.5 &&
1629 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2]-0.5 &&
1630 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3]+0.5 &&
1631 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4]-0.5 &&
1632 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5]+0.5) {
1635 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1637 if(ImageActor->GetVisibility())
1638 worldPos << "data value : " << value << std::endl;
1640 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1641 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1642 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1645 worldPos << "pixel : " << ix << ' '
1653 ca->SetText(1,worldPos.str().c_str());
1655 std::stringstream slicePos;
1656 slicePos << "Slice: " << this->GetSlice();
1657 ca->SetText(2, slicePos.str().c_str());
1660 if (pdmA->GetVisibility()) {
1661 double x = mCursor[0];
1662 double y = mCursor[1];
1663 double z = mCursor[2];
1664 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1665 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1666 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1667 #if VTK_MAJOR_VERSION <= 5
1668 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1669 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1670 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1671 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1672 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1673 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1674 vtkRenderer * renderer = this->Renderer;
1676 renderer->WorldToView(x,y,z);
1677 renderer->ViewToNormalizedViewport(x,y,z);
1678 renderer->NormalizedViewportToViewport(x,y);
1679 renderer->ViewportToNormalizedDisplay(x,y);
1680 renderer->NormalizedDisplayToDisplay(x,y);
1681 crossCursor->SetFocalPoint(x,y,z);
1683 crossCursor->SetFocalPoint(-1,-1,z);
1684 crossCursor->Update();
1687 vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1689 mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1690 } catch (const std::bad_cast& e) {
1691 std::cerr << e.what() << std::endl;
1692 std::cerr << "Conversion error" << std::endl;
1696 if (xCursor >= mapperOpenGL->GetCroppingRegion()[0]-0.5 &&
1697 xCursor < mapperOpenGL->GetCroppingRegion()[1]+0.5 &&
1698 yCursor >= mapperOpenGL->GetCroppingRegion()[2]-0.5 &&
1699 yCursor < mapperOpenGL->GetCroppingRegion()[3]+0.5 &&
1700 zCursor >= mapperOpenGL->GetCroppingRegion()[4]-0.5 &&
1701 zCursor < mapperOpenGL->GetCroppingRegion()[5]+0.5 ) {
1702 vtkRenderer * renderer = this->Renderer;
1704 renderer->WorldToView(x,y,z);
1705 renderer->ViewToNormalizedViewport(x,y,z);
1706 renderer->NormalizedViewportToViewport(x,y);
1707 renderer->ViewportToNormalizedDisplay(x,y);
1708 renderer->NormalizedDisplayToDisplay(x,y);
1709 crossCursor->SetFocalPoint(x,y,z);
1711 crossCursor->SetFocalPoint(-1,-1,z);
1712 crossCursor->Update();
1716 if (mOverlay && mOverlayActor->GetVisibility()) {
1717 if(mLinkOverlayWindowLevel) {
1718 mOverlayMapper->SetWindow(this->GetColorWindow());
1719 mOverlayMapper->SetLevel(this->GetColorLevel());
1721 #if VTK_MAJOR_VERSION <= 5
1722 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1724 mOverlayMapper->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1726 mOverlayMapper->Update();
1728 if (mFusion && mFusionActor->GetVisibility()) {
1729 #if VTK_MAJOR_VERSION <= 5
1730 mFusionMapper->GetOutput()->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1732 mFusionMapper->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1734 mFusionMapper->Update();
1739 this->GetRenderWindow()->Render();
1741 //----------------------------------------------------------------------------
1744 //----------------------------------------------------------------------------
1745 void vvSlicer::UpdateCursorPosition()
1746 { //out << __func__ << endl;
1747 pdmA->SetVisibility(true);
1748 mCursor[0] = mCurrent[0];
1749 mCursor[1] = mCurrent[1];
1750 mCursor[2] = mCurrent[2];
1751 mCursor[3] = mCurrentTSlice;
1753 //----------------------------------------------------------------------------
1756 //----------------------------------------------------------------------------
1757 void vvSlicer::UpdateLandmarks()
1758 { //out << __func__ << endl;
1759 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1760 if (pd->GetPoints()) {
1761 //mLandGlyph->SetRange(0,1);
1762 //mLandGlyph->Modified();
1763 //mLandGlyph->Update();
1765 mClipBox->Modified();
1766 mLandClipper->Update();
1767 mLandMapper->Update();
1768 //Let's add the captions
1769 //First remove all captions:
1770 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1771 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1772 //allActors2D->Remove (mLandLabelActors[i]);
1774 mLandLabelActors.clear();
1775 //Next add the captions to the displayed points
1776 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1777 double *position = mLandClipper->GetOutput()->GetPoint(id);
1778 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1779 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1780 label_actor->SetCaption(label);
1781 label_actor->SetAttachmentPoint(position);
1782 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1783 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1784 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1785 label_actor->GetCaptionTextProperty()->SetBold(0);
1786 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1787 label_actor->BorderOff();
1788 label_actor->LeaderOff();
1789 label_actor->ThreeDimensionalLeaderOff();
1790 mLandLabelActors.push_back(label_actor);
1791 this->Renderer->AddActor2D(mLandLabelActors[id]);
1796 //----------------------------------------------------------------------------
1799 //----------------------------------------------------------------------------
1800 void vvSlicer::SetSlice(int slice)
1801 { //out << __func__ << endl;
1802 int *range = this->GetSliceRange();
1804 if (slice < range[0]) {
1806 } else if (slice > range[1]) {
1811 if (this->Slice == slice) {
1815 this->Slice = slice;
1818 this->UpdateDisplayExtent();
1820 // Seems to work without this line
1823 //----------------------------------------------------------------------------
1825 //----------------------------------------------------------------------------
1826 int vvSlicer::GetTMax()
1827 { //out << __func__ << endl;
1828 int tmax = (int)mImage->GetVTKImages().size() - 1;
1830 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1833 //----------------------------------------------------------------------------
1835 //----------------------------------------------------------------------------
1836 void vvSlicer::SetContourSlice()
1837 { //out << __func__ << endl;
1838 if (mSurfaceCutActors.size() > 0)
1839 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1840 i!=mSurfaceCutActors.end(); i++) {
1842 (*i)->SetSlicingOrientation(this->SliceOrientation);
1843 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1844 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1847 //----------------------------------------------------------------------------
1850 //----------------------------------------------------------------------------
1851 void vvSlicer::ForceUpdateDisplayExtent()
1852 { //out << __func__ << endl;
1853 this->UpdateDisplayExtent();
1855 //----------------------------------------------------------------------------
1858 //----------------------------------------------------------------------------
1859 int* vvSlicer::GetDisplayExtent()
1860 { //out << __func__ << endl;
1861 return this->GetImageActor()->GetDisplayExtent();
1863 //----------------------------------------------------------------------------
1866 //----------------------------------------------------------------------------
1867 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1868 { //out << __func__ << endl;
1869 this->Superclass::PrintSelf(os, indent);
1871 //----------------------------------------------------------------------------
1874 //----------------------------------------------------------------------------
1875 void vvSlicer::SetVFColor(double r, double g, double b)
1876 { //out << __func__ << endl;
1877 double mVFColorHSV[3];
1882 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1883 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1884 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1885 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1889 //----------------------------------------------------------------------------
1892 //----------------------------------------------------------------------------
1893 void vvSlicer::SetRegisterExtent(int ext[6])
1894 { //out << __func__ << endl;
1895 copyExtent(ext, mRegisterExtent);
1897 //----------------------------------------------------------------------------
1900 //----------------------------------------------------------------------------
1901 void vvSlicer::GetRegisterExtent(int ext[6])
1902 { //out << __func__ << endl;
1903 copyExtent(mRegisterExtent, ext);
1905 //----------------------------------------------------------------------------