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 = new int[6];
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;
315 delete [] mRegisterExtent;
317 //------------------------------------------------------------------------------
319 //------------------------------------------------------------------------------
320 double* vvSlicer::GetCurrentPosition()
321 { //out << __func__ << endl;
322 return mCurrentBeforeSlicingTransform;
324 //------------------------------------------------------------------------------
326 //------------------------------------------------------------------------------
327 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
328 { //out << __func__ << endl;
329 mCurrentBeforeSlicingTransform[0]=x;
330 mCurrentBeforeSlicingTransform[1]=y;
331 mCurrentBeforeSlicingTransform[2]=z;
332 mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
333 if (t>=0) SetTSlice(t);
335 //------------------------------------------------------------------------------
338 //------------------------------------------------------------------------------
339 void vvSlicer::SetImage(vvImage::Pointer image)
340 { //out << __func__ << endl;
341 if (image->GetVTKImages().size()) {
344 if (!mImageReslice) {
345 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
346 mImageReslice->SetInterpolationModeToLinear();
347 mImageReslice->AutoCropOutputOn();
348 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
351 mConcatenatedTransform->Identity();
352 mConcatenatedTransform->Concatenate(mImage->GetTransform()[0]);
353 mConcatenatedTransform->Concatenate(mSlicingTransform);
354 mImageReslice->SetResliceTransform(mConcatenatedTransform);
355 #if VTK_MAJOR_VERSION <= 5
356 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
358 mImageReslice->SetInputData(0, mImage->GetFirstVTKImageData());
360 mImageReslice->UpdateInformation();
362 #if VTK_MAJOR_VERSION <= 5
363 this->Superclass::SetInput(mImageReslice->GetOutput());
365 this->Superclass::SetInputConnection(mImageReslice->GetOutputPort());
369 #if VTK_MAJOR_VERSION <= 5
370 this->GetInput()->GetWholeExtent(extent);
372 int* ext = mImageReslice->GetInputInformation()->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
373 copyExtent(ext, extent);
376 // Prevent crash when reload -> change slice if outside extent
377 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
378 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
381 // Make sure that the required part image has been computed
382 extent[SliceOrientation*2] = Slice;
383 extent[SliceOrientation*2+1] = Slice;
385 #if VTK_MAJOR_VERSION <= 5
386 mImageReslice->GetOutput()->SetUpdateExtent(extent);
387 mImageReslice->GetOutput()->Update();
389 mImageReslice->SetUpdateExtent(extent);
390 mImageReslice->Update();
393 this->UpdateDisplayExtent();
396 ca->SetText(0,mFileName.c_str());
399 //------------------------------------------------------------------------------
402 //------------------------------------------------------------------------------
403 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
404 { //out << __func__ << endl;
405 if (overlay->GetVTKImages().size()) {
407 mOverlayVisibility = true;
409 if (!mOverlayReslice) {
410 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
411 mOverlayReslice->SetInterpolationModeToLinear();
412 mOverlayReslice->AutoCropOutputOn();
413 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
416 mConcatenatedOverlayTransform->Identity();
417 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
418 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
419 mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
420 #if VTK_MAJOR_VERSION <= 5
421 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
423 mOverlayReslice->SetInputData(0, mOverlay->GetFirstVTKImageData());
425 mOverlayReslice->Update();
428 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
429 #if VTK_MAJOR_VERSION <= 5
430 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
432 mOverlayMapper->SetInputConnection(mOverlayReslice->GetOutputPort(0));
435 if (!mOverlayActor) {
436 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
437 #if VTK_MAJOR_VERSION <= 5
438 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
440 mOverlayActor->GetMapper()->SetInputConnection(mOverlayMapper->GetOutputPort());
442 mOverlayActor->SetPickable(0);
443 mOverlayActor->SetVisibility(true);
444 mOverlayActor->SetOpacity(0.5);
445 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
446 mOverlayActor->GetMapper()->BorderOn();
450 //stupid but necessary : the Overlay need to be rendered before fusion
452 this->GetRenderer()->RemoveActor(mFusionActor);
453 this->GetRenderer()->AddActor(mOverlayActor);
454 this->GetRenderer()->AddActor(mFusionActor);
456 this->GetRenderer()->AddActor(mOverlayActor);
458 //Synchronize orientation and slice
459 AdjustResliceToSliceOrientation(mOverlayReslice);
460 this->UpdateDisplayExtent();
461 this->SetTSlice(mCurrentTSlice);
464 //------------------------------------------------------------------------------
467 //------------------------------------------------------------------------------
468 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
469 { //out << __func__ << endl;
470 mFusionSequenceCode = fusionSequenceCode;
471 if (fusion->GetVTKImages().size()) {
473 mFusionVisibility = true;
475 if (!mFusionReslice) {
476 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
477 mFusionReslice->SetInterpolationModeToLinear();
478 mFusionReslice->AutoCropOutputOn();
479 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
482 mConcatenatedFusionTransform->Identity();
483 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
484 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
485 mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
486 #if VTK_MAJOR_VERSION <= 5
487 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
489 mFusionReslice->SetInputData(0, mFusion->GetFirstVTKImageData());
491 mFusionReslice->Update();
494 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
496 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
498 lut->SetValueRange(0, 1);
499 lut->SetSaturationRange(0, 0);
501 mFusionMapper->SetLookupTable(lut);
502 #if VTK_MAJOR_VERSION <= 5
503 mFusionMapper->SetInput(mFusionReslice->GetOutput());
505 mFusionMapper->SetInputConnection(mFusionReslice->GetOutputPort(0));
509 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
510 #if VTK_MAJOR_VERSION <= 5
511 mFusionActor->SetInput(mFusionMapper->GetOutput());
513 mFusionActor->GetMapper()->SetInputConnection(mFusionMapper->GetOutputPort());
515 mFusionActor->SetPickable(0);
516 mFusionActor->SetVisibility(true);
517 mFusionActor->SetOpacity(0.7);
518 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
519 mFusionActor->GetMapper()->BorderOn();
522 this->GetRenderer()->AddActor(mFusionActor);
525 //Synchronize orientation and slice
526 AdjustResliceToSliceOrientation(mFusionReslice);
527 this->UpdateDisplayExtent();
528 this->SetTSlice(mCurrentTSlice);
531 //------------------------------------------------------------------------------
534 //------------------------------------------------------------------------------
535 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
536 { //out << __func__ << endl;
538 if (actor_type == "image")
539 vis = mImageVisibility;
540 else if (actor_type == "vector")
542 else if (actor_type == "overlay")
543 vis = mOverlayVisibility;
544 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
545 vis = mFusionVisibility;
546 else if (actor_type == "contour")
547 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
550 //------------------------------------------------------------------------------
552 //------------------------------------------------------------------------------
553 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
554 { //out << __func__ << endl;
555 if (actor_type == "image")
556 mImageVisibility = vis;
557 else if (actor_type == "vector")
559 else if (actor_type == "overlay")
560 mOverlayVisibility = vis;
561 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
562 mFusionVisibility = vis;
563 else if (actor_type == "contour")
564 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
565 UpdateDisplayExtent();
567 //------------------------------------------------------------------------------
569 //------------------------------------------------------------------------------
570 void vvSlicer::SetVF(vvImage::Pointer vf)
571 { //out << __func__ << endl;
572 if (vf->GetVTKImages().size()) {
574 mVFVisibility = true;
577 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
578 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
579 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
581 #if VTK_MAJOR_VERSION <= 5
582 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
583 mAAFilter->SetInput(mVOIFilter->GetOutput());
585 mVOIFilter->SetInputData(vf->GetFirstVTKImageData());
586 mAAFilter->SetInputConnection(mVOIFilter->GetOutputPort());
588 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
589 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
592 mArrow = vtkSmartPointer<vvGlyphSource>::New();
593 mArrow->SetGlyphTypeToSpecificArrow();
594 mArrow->SetScale(mScale);
597 // Glyph the gradient vector (with arrows)
599 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
600 #if VTK_MAJOR_VERSION <= 5
601 mGlyphFilter->SetInput(mAAFilter->GetOutput());
602 mGlyphFilter->SetSource(mArrow->GetOutput());
604 mGlyphFilter->SetInputConnection(mAAFilter->GetOutputPort());
605 mGlyphFilter->SetSourceConnection(mArrow->GetOutputPort());
607 mGlyphFilter->ScalingOn();
608 mGlyphFilter->SetScaleModeToScaleByVector();
609 mGlyphFilter->OrientOn();
610 mGlyphFilter->SetVectorModeToUseVector();
611 mGlyphFilter->SetColorModeToColorByVector();
614 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
616 double mVFColorHSV[3];
617 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
618 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
619 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
620 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
623 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
624 #if VTK_MAJOR_VERSION <= 5
625 mVFMapper->SetInput(mGlyphFilter->GetOutput());
627 mVFMapper->SetInputConnection(mGlyphFilter->GetOutputPort());
629 mVFMapper->ImmediateModeRenderingOn();
630 mVFMapper->SetLookupTable(mVFColorLUT);
633 mVFActor = vtkSmartPointer<vtkActor>::New();
634 mVFActor->SetMapper(mVFMapper);
635 mVFActor->SetPickable(0);
636 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
637 this->UpdateDisplayExtent();
638 this->GetRenderer()->AddActor(mVFActor);
641 SetTSlice(mCurrentTSlice);
644 //------------------------------------------------------------------------------
647 //------------------------------------------------------------------------------
648 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
649 { //out << __func__ << endl;
650 mLandmarks = landmarks;
654 mCross = vtkSmartPointer<vtkCursor3D>::New();
656 mClipBox = vtkSmartPointer<vtkBox>::New();
658 mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
660 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
662 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
664 mLandActor = vtkSmartPointer<vtkActor>::New();
666 mCross->SetFocalPoint(0.0,0.0,0.0);
667 mCross->SetModelBounds(-10,10,-10,10,-10,10);
671 mLandClipper->SetClipFunction(mClipBox);
672 mLandClipper->InsideOutOn();
673 #if VTK_MAJOR_VERSION <= 5
674 mLandClipper->SetInput(mLandmarks->GetOutput());
676 mLandGlyph->SetSource(mCross->GetOutput());
677 mLandGlyph->SetInput(mLandClipper->GetOutput());
679 mLandClipper->SetInputData(mLandmarks->GetOutput());
681 mLandGlyph->SetSourceData(mCross->GetOutput());
682 mLandGlyph->SetInputData(mLandClipper->GetOutput());
684 //mLandGlyph->SetIndexModeToScalar();
685 //mLandGlyph->SetRange(0,1);
686 //mLandGlyph->ScalingOff();
688 //mLandGlyph->SetColorModeToColorByScalar();
690 mLandGlyph->SetScaleModeToDataScalingOff();
691 mLandGlyph->SetIndexModeToOff();
693 mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
694 //mLandMapper->ScalarVisibilityOff();
696 mLandActor->SetMapper(mLandMapper);
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;
1074 vtkImageData *input = this->GetInput();
1076 if (!input || !this->ImageActor) {
1080 #if VTK_MAJOR_VERSION <= 5
1081 input->UpdateInformation();
1083 this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
1085 // Local copy of extent
1087 int* ext = GetExtent();
1088 copyExtent(ext, w_ext);
1092 w_ext[ this->SliceOrientation*2 ] = this->Slice;
1093 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
1096 this->ImageActor->SetVisibility(mImageVisibility);
1097 this->ImageActor->SetDisplayExtent(w_ext);
1098 #if VTK_MAJOR_VERSION >= 6
1099 vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1101 mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1102 } catch (const std::bad_cast& e) {
1103 std::cerr << e.what() << std::endl;
1104 std::cerr << "Conversion error" << std::endl;
1107 if (mFirstSetSliceOrientation) {
1108 copyExtent(ext, mRegisterExtent);
1110 int w_croppingRegion[6];
1111 copyExtent(mRegisterExtent, w_croppingRegion);
1112 w_croppingRegion[ this->SliceOrientation*2 ] = this->Slice;
1113 w_croppingRegion[ this->SliceOrientation*2+1 ] = this->Slice;
1114 mapperOpenGL->SetCroppingRegion(w_croppingRegion);
1118 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1119 // Fix for bug #1882
1120 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
1123 // Overlay image actor
1124 if (mOverlay && mOverlayVisibility) {
1125 AdjustResliceToSliceOrientation(mOverlayReslice);
1127 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
1128 #if VTK_MAJOR_VERSION <= 5
1129 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
1131 bool out = ClipDisplayedExtent(overExtent, this->GetExtent());
1133 mOverlayActor->SetVisibility(!out);
1134 mOverlayActor->SetDisplayExtent( overExtent );
1135 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1136 // Fix for bug #1882
1137 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
1141 mOverlayActor->SetVisibility(false);
1143 // Fusion image actor
1144 if (mFusion && mFusionVisibility) {
1145 AdjustResliceToSliceOrientation(mFusionReslice);
1147 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
1148 #if VTK_MAJOR_VERSION <= 5
1149 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
1151 bool out = ClipDisplayedExtent(fusExtent, this->GetExtent());
1153 mFusionActor->SetVisibility(!out);
1154 mFusionActor->SetDisplayExtent( fusExtent );
1155 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
1156 // Fix for bug #1882
1157 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1161 mFusionActor->SetVisibility(false);
1162 // Vector field actor
1163 double* camera = Renderer->GetActiveCamera()->GetPosition();
1164 double* image_bounds = ImageActor->GetBounds();
1165 double position[3] = {0, 0, 0};
1166 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1168 //print_vector<double, 6>("camera", camera);
1169 //print_vector<double, 6>("image_bounds", image_bounds);
1170 //print_vector<double, 3>("position", position);
1172 // find where to place the VF actor. to deal with
1173 // z-buffer issues, the VF is placed right in front of the image,
1174 // subject to a small offset. the position actually depends on the
1175 // the location of the camera relative to the image.
1177 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1180 if (mVF && mVFVisibility) {
1182 #if VTK_MAJOR_VERSION <= 5
1183 mVF->GetVTKImages()[0]->UpdateInformation();
1185 //this->UpdateInformation();
1187 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
1188 #if VTK_MAJOR_VERSION <= 5
1189 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1191 bool out = ClipDisplayedExtent(vfExtent, this->GetExtent());
1193 mVFActor->SetVisibility(!out);
1194 mVOIFilter->SetVOI(vfExtent);
1195 int orientation[3] = {1,1,1};
1196 orientation[this->SliceOrientation] = 0;
1197 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1198 position[this->SliceOrientation] += offset;
1199 mVFActor->SetPosition(position);
1200 mVFActor->GetProperty()->SetOpacity(0.995);
1201 mVFMapper->Update();
1205 mVFActor->SetVisibility(false);
1211 for(unsigned int i=0; i<6; i++)
1212 bounds[i] = ImageActor->GetBounds()[i];
1213 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1214 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1215 mClipBox->SetBounds(bounds);
1219 position[this->SliceOrientation] = offset;
1220 mLandActor->SetPosition(position);
1223 // Figure out the correct clipping range
1224 if (this->Renderer) {
1225 if (this->InteractorStyle &&
1226 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1227 this->Renderer->ResetCameraClippingRange();
1229 vtkCamera *cam = this->Renderer->GetActiveCamera();
1232 this->ImageActor->GetBounds(bounds);
1233 double spos = (double)bounds[this->SliceOrientation * 2];
1234 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1235 double range = fabs(spos - cpos);
1236 double *spacing = input->GetSpacing();
1237 double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1238 cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1243 //----------------------------------------------------------------------------
1245 //----------------------------------------------------------------------------
1246 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
1247 vtkImageData *targetImage, int targetExtent[6])
1248 { //out << __func__ << endl;
1250 for(unsigned int i=0; i<6; i++) {
1251 // From source voxel coordinates to world coordinates
1252 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
1254 // From world coordinates to floating point target voxel coordinates
1255 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1257 // Round to current slice or larger extent
1258 if(i/2==this->GetOrientation())
1259 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1261 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1263 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1266 //----------------------------------------------------------------------------
1268 //----------------------------------------------------------------------------
1269 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1270 { //out << __func__ << endl;
1274 for (int i = 0; i < maxBound; i = i+2) {
1275 //if we are totally outside the image
1276 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1280 //crop to the limit of the image
1281 extent[i] = std::max(extent[i], refExtent[i]);
1282 extent[i] = std::min(extent[i], refExtent[i+1]);;
1283 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1284 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1287 for (int i = 0; i < maxBound; i = i+2) {
1288 extent[i] = refExtent[i];
1289 extent[i+1] = refExtent[i];
1293 //----------------------------------------------------------------------------
1296 //----------------------------------------------------------------------------
1297 void vvSlicer::UpdateOrientation()
1298 { //out << __func__ << endl;
1299 // Set the camera position
1300 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1302 switch (this->SliceOrientation) {
1303 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1304 cam->SetFocalPoint(0,0,0);
1305 cam->SetPosition(0,0,-1); // -1 if medical ?
1306 cam->SetViewUp(0,-1,0);
1309 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1310 cam->SetFocalPoint(0,0,0);
1311 cam->SetPosition(0,-1,0); // 1 if medical ?
1312 cam->SetViewUp(0,0,1);
1315 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1316 cam->SetFocalPoint(0,0,0);
1317 cam->SetPosition(-1,0,0); // -1 if medical ?
1318 cam->SetViewUp(0,0,1);
1323 //----------------------------------------------------------------------------
1326 //----------------------------------------------------------------------------
1327 void vvSlicer::SetOpacity(double s)
1328 { //out << __func__ << endl;
1329 this->GetImageActor()->SetOpacity(s);
1331 //----------------------------------------------------------------------------
1334 //----------------------------------------------------------------------------
1335 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1336 { //out << __func__ << endl;
1337 this->Superclass::SetRenderWindow(rw);
1338 this->SetupInteractor(rw->GetInteractor());
1339 ca->SetImageActor(this->GetImageActor());
1340 ca->SetWindowLevel(this->GetWindowLevel());
1341 ca->SetText(3, "<window>\n<level>");
1352 crossCursor->SetModelBounds(bounds);
1354 this->GetRenderer()->AddActor(pdmA);
1355 this->GetRenderer()->AddActor(ca);
1356 this->GetRenderer()->ResetCamera();
1358 //this is just a mapping between the labeling of the orientations presented to the user and
1359 //the one used by vtk
1360 SetSliceOrientation(2-(orientation%3));
1363 //----------------------------------------------------------------------------
1366 //----------------------------------------------------------------------------
1367 void vvSlicer::ResetCamera()
1368 { //out << __func__ << endl;
1369 this->GetRenderer()->ResetCamera();
1371 //----------------------------------------------------------------------------
1374 //----------------------------------------------------------------------------
1375 void vvSlicer::SetDisplayMode(bool i)
1376 { //out << __func__ << endl;
1377 this->GetRenderer()->SetDraw(i);
1378 if (i) UpdateDisplayExtent();
1380 //----------------------------------------------------------------------------
1383 //----------------------------------------------------------------------------
1384 void vvSlicer::FlipHorizontalView()
1385 { //out << __func__ << endl;
1386 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1388 double *position = cam->GetPosition();
1389 double factor[3] = {1, 1, 1};
1390 factor[this->SliceOrientation] = -1;
1391 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1393 /* switch (this->SliceOrientation) {
1394 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1395 cam->SetPosition(position[0],position[1],-position[2]);
1398 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1399 cam->SetPosition(position[0],-position[1],position[2]);
1402 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1403 cam->SetPosition(-position[0],position[1],position[2]);
1407 this->Renderer->ResetCameraClippingRange();
1408 this->UpdateDisplayExtent();
1411 //----------------------------------------------------------------------------
1414 //----------------------------------------------------------------------------
1415 void vvSlicer::FlipVerticalView()
1416 { //out << __func__ << endl;
1417 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1419 FlipHorizontalView();
1420 double *viewup = cam->GetViewUp();
1421 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1422 this->UpdateDisplayExtent();
1425 //----------------------------------------------------------------------------
1428 //----------------------------------------------------------------------------
1429 void vvSlicer::SetColorWindow(double window)
1430 { //out << __func__ << endl;
1431 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1433 double level = this->GetWindowLevel()->GetLevel();
1434 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1437 this->vtkImageViewer2::SetColorWindow(window);
1439 //----------------------------------------------------------------------------
1441 //----------------------------------------------------------------------------
1442 void vvSlicer::SetColorLevel(double level)
1443 { //out << __func__ << endl;
1444 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1446 double window = this->GetWindowLevel()->GetWindow();
1447 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1450 this->vtkImageViewer2::SetColorLevel(level);
1452 //----------------------------------------------------------------------------
1454 //----------------------------------------------------------------------------
1455 double vvSlicer::GetOverlayColorWindow()
1456 { //out << __func__ << endl;
1458 return mOverlayMapper->GetWindow();
1462 //----------------------------------------------------------------------------
1464 //----------------------------------------------------------------------------
1465 double vvSlicer::GetOverlayColorLevel()
1466 { //out << __func__ << endl;
1468 return mOverlayMapper->GetLevel();
1472 //----------------------------------------------------------------------------
1474 //----------------------------------------------------------------------------
1475 void vvSlicer::SetOverlayColorWindow(double window)
1476 { //out << __func__ << endl;
1478 mOverlayMapper->SetWindow(window);
1480 //----------------------------------------------------------------------------
1482 //----------------------------------------------------------------------------
1483 void vvSlicer::SetOverlayColorLevel(double level)
1484 { //out << __func__ << endl;
1486 mOverlayMapper->SetLevel(level);
1488 //----------------------------------------------------------------------------
1490 //----------------------------------------------------------------------------
1491 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1492 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1493 { //out << __func__ << endl;
1494 //Get mouse pointer position in view coordinates
1497 for(int i=0; i<3; i++) {
1498 corner1[i] = mCurrent[i];
1499 corner2[i] = mCurrent[i];
1502 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1503 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1505 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1507 this->Renderer->GetTiledSize(&w, &h);
1508 corner1[0] -= 0.2*h/(double)w;
1509 corner2[0] += 0.2*h/(double)w;
1512 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1513 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1515 //Convert to image pixel coordinates (rounded)
1516 transform->TransformPoint(corner1, corner1);
1517 transform->TransformPoint(corner2, corner2);
1518 int iLocalExtents[6];
1519 for(int i=0; i<3; i++) {
1520 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1521 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1523 iLocalExtents[i*2 ] = lrint(corner1[i]);
1524 iLocalExtents[i*2+1] = lrint(corner2[i]);
1526 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1527 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1530 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1531 #if VTK_MAJOR_VERSION <= 5
1532 voiFilter->SetInput(image);
1534 voiFilter->SetInputData(image);
1536 voiFilter->SetVOI(iLocalExtents);
1537 voiFilter->Update();
1538 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1544 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1545 #if VTK_MAJOR_VERSION <= 5
1546 accFilter->SetInput(voiFilter->GetOutput());
1548 accFilter->SetInputConnection(voiFilter->GetOutputPort(0));
1550 accFilter->Update();
1552 min = *(accFilter->GetMin());
1553 max = *(accFilter->GetMax());
1555 //----------------------------------------------------------------------------
1557 //----------------------------------------------------------------------------
1558 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1559 { //out << __func__ << endl;
1563 #if VTK_MAJOR_VERSION <= 5
1564 if (ix < image->GetWholeExtent()[0] ||
1565 ix > image->GetWholeExtent()[1] ||
1566 iy < image->GetWholeExtent()[2] ||
1567 iy > image->GetWholeExtent()[3] ||
1568 iz < image->GetWholeExtent()[4] ||
1569 iz > image->GetWholeExtent()[5] )
1570 return std::numeric_limits<double>::quiet_NaN();
1571 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1574 if (ix < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0] ||
1575 ix > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1] ||
1576 iy < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2] ||
1577 iy > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3] ||
1578 iz < image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4] ||
1579 iz > image->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5] )
1580 return std::numeric_limits<double>::quiet_NaN();
1581 //image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1585 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1587 //----------------------------------------------------------------------------
1589 //----------------------------------------------------------------------------
1590 void vvSlicer::Render()
1591 { //out << __func__ << endl;
1593 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1594 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1595 legend->UseOpacityOn();
1596 legend->SetVisibility(1);
1598 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1599 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1600 legend->UseOpacityOff();
1601 legend->SetVisibility(1);
1602 } else legend->SetVisibility(0);
1604 if (ca->GetVisibility()) {
1605 std::stringstream worldPos(" ");
1607 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1608 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1609 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1610 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1611 #if VTK_MAJOR_VERSION <= 5
1612 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1613 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1614 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1615 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1616 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1617 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1621 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1623 if(ImageActor->GetVisibility())
1624 worldPos << "data value : " << value << std::endl;
1626 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1627 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1628 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1631 worldPos << "pixel : " << ix << ' '
1638 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[0]-0.5 &&
1639 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[1]+0.5 &&
1640 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[2]-0.5 &&
1641 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[3]+0.5 &&
1642 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[4]-0.5 &&
1643 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetInformation()->Get(vtkDataObject::DATA_EXTENT())[5]+0.5) {
1646 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1648 if(ImageActor->GetVisibility())
1649 worldPos << "data value : " << value << std::endl;
1651 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1652 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1653 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1656 worldPos << "pixel : " << ix << ' '
1664 ca->SetText(1,worldPos.str().c_str());
1666 std::stringstream slicePos;
1667 slicePos << "Slice: " << this->GetSlice();
1668 ca->SetText(2, slicePos.str().c_str());
1671 if (pdmA->GetVisibility()) {
1672 double x = mCursor[0];
1673 double y = mCursor[1];
1674 double z = mCursor[2];
1675 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1676 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1677 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1678 #if VTK_MAJOR_VERSION <= 5
1679 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1680 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1681 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1682 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1683 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1684 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1685 vtkRenderer * renderer = this->Renderer;
1687 renderer->WorldToView(x,y,z);
1688 renderer->ViewToNormalizedViewport(x,y,z);
1689 renderer->NormalizedViewportToViewport(x,y);
1690 renderer->ViewportToNormalizedDisplay(x,y);
1691 renderer->NormalizedDisplayToDisplay(x,y);
1692 crossCursor->SetFocalPoint(x,y,z);
1694 crossCursor->SetFocalPoint(-1,-1,z);
1695 crossCursor->Update();
1698 vtkSmartPointer<vtkOpenGLImageSliceMapper> mapperOpenGL= vtkSmartPointer<vtkOpenGLImageSliceMapper>::New();
1700 mapperOpenGL = dynamic_cast<vtkOpenGLImageSliceMapper*>(GetImageActor()->GetMapper());
1701 } catch (const std::bad_cast& e) {
1702 std::cerr << e.what() << std::endl;
1703 std::cerr << "Conversion error" << std::endl;
1706 if (xCursor >= mapperOpenGL->GetCroppingRegion()[0]-0.5 &&
1707 xCursor < mapperOpenGL->GetCroppingRegion()[1]+0.5 &&
1708 yCursor >= mapperOpenGL->GetCroppingRegion()[2]-0.5 &&
1709 yCursor < mapperOpenGL->GetCroppingRegion()[3]+0.5 &&
1710 zCursor >= mapperOpenGL->GetCroppingRegion()[4]-0.5 &&
1711 zCursor < mapperOpenGL->GetCroppingRegion()[5]+0.5 ) {
1712 vtkRenderer * renderer = this->Renderer;
1714 renderer->WorldToView(x,y,z);
1715 renderer->ViewToNormalizedViewport(x,y,z);
1716 renderer->NormalizedViewportToViewport(x,y);
1717 renderer->ViewportToNormalizedDisplay(x,y);
1718 renderer->NormalizedDisplayToDisplay(x,y);
1719 crossCursor->SetFocalPoint(x,y,z);
1721 crossCursor->SetFocalPoint(-1,-1,z);
1722 crossCursor->Update();
1726 if (mOverlay && mOverlayActor->GetVisibility()) {
1727 if(mLinkOverlayWindowLevel) {
1728 mOverlayMapper->SetWindow(this->GetColorWindow());
1729 mOverlayMapper->SetLevel(this->GetColorLevel());
1731 #if VTK_MAJOR_VERSION <= 5
1732 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1734 mOverlayMapper->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1736 mOverlayMapper->Update();
1738 if (mFusion && mFusionActor->GetVisibility()) {
1739 #if VTK_MAJOR_VERSION <= 5
1740 mFusionMapper->GetOutput()->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1742 mFusionMapper->SetUpdateExtent(mFusionActor->GetDisplayExtent());
1744 mFusionMapper->Update();
1749 this->GetRenderWindow()->Render();
1751 //----------------------------------------------------------------------------
1754 //----------------------------------------------------------------------------
1755 void vvSlicer::UpdateCursorPosition()
1756 { //out << __func__ << endl;
1757 pdmA->SetVisibility(true);
1758 mCursor[0] = mCurrent[0];
1759 mCursor[1] = mCurrent[1];
1760 mCursor[2] = mCurrent[2];
1761 mCursor[3] = mCurrentTSlice;
1763 //----------------------------------------------------------------------------
1766 //----------------------------------------------------------------------------
1767 void vvSlicer::UpdateLandmarks()
1768 { //out << __func__ << endl;
1769 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1770 if (pd->GetPoints()) {
1771 //mLandGlyph->SetRange(0,1);
1772 //mLandGlyph->Modified();
1773 //mLandGlyph->Update();
1775 mClipBox->Modified();
1776 mLandClipper->Update();
1777 mLandMapper->Update();
1778 //Let's add the captions
1779 //First remove all captions:
1780 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1781 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1782 //allActors2D->Remove (mLandLabelActors[i]);
1784 mLandLabelActors.clear();
1785 //Next add the captions to the displayed points
1786 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1787 double *position = mLandClipper->GetOutput()->GetPoint(id);
1788 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1789 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1790 label_actor->SetCaption(label);
1791 label_actor->SetAttachmentPoint(position);
1792 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1793 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1794 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1795 label_actor->GetCaptionTextProperty()->SetBold(0);
1796 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1797 label_actor->BorderOff();
1798 label_actor->LeaderOff();
1799 label_actor->ThreeDimensionalLeaderOff();
1800 mLandLabelActors.push_back(label_actor);
1801 this->Renderer->AddActor2D(mLandLabelActors[id]);
1806 //----------------------------------------------------------------------------
1809 //----------------------------------------------------------------------------
1810 void vvSlicer::SetSlice(int slice)
1811 { //out << __func__ << endl;
1812 int *range = this->GetSliceRange();
1814 if (slice < range[0]) {
1816 } else if (slice > range[1]) {
1821 if (this->Slice == slice) {
1825 this->Slice = slice;
1828 this->UpdateDisplayExtent();
1830 // Seems to work without this line
1833 //----------------------------------------------------------------------------
1835 //----------------------------------------------------------------------------
1836 int vvSlicer::GetTMax()
1837 { //out << __func__ << endl;
1838 int tmax = (int)mImage->GetVTKImages().size() - 1;
1840 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1843 //----------------------------------------------------------------------------
1845 //----------------------------------------------------------------------------
1846 void vvSlicer::SetContourSlice()
1847 { //out << __func__ << endl;
1848 if (mSurfaceCutActors.size() > 0)
1849 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1850 i!=mSurfaceCutActors.end(); i++) {
1852 (*i)->SetSlicingOrientation(this->SliceOrientation);
1853 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1854 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1857 //----------------------------------------------------------------------------
1860 //----------------------------------------------------------------------------
1861 void vvSlicer::ForceUpdateDisplayExtent()
1862 { //out << __func__ << endl;
1863 this->UpdateDisplayExtent();
1865 //----------------------------------------------------------------------------
1868 //----------------------------------------------------------------------------
1869 int* vvSlicer::GetDisplayExtent()
1870 { //out << __func__ << endl;
1871 return this->GetImageActor()->GetDisplayExtent();
1873 //----------------------------------------------------------------------------
1876 //----------------------------------------------------------------------------
1877 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1878 { //out << __func__ << endl;
1879 this->Superclass::PrintSelf(os, indent);
1881 //----------------------------------------------------------------------------
1884 //----------------------------------------------------------------------------
1885 void vvSlicer::SetVFColor(double r, double g, double b)
1886 { //out << __func__ << endl;
1887 double mVFColorHSV[3];
1892 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1893 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1894 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1895 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
1899 //----------------------------------------------------------------------------
1902 //----------------------------------------------------------------------------
1903 void vvSlicer::SetRegisterExtent(int ext[6])
1904 { //out << __func__ << endl;
1905 copyExtent(ext, mRegisterExtent);
1907 //----------------------------------------------------------------------------
1910 //----------------------------------------------------------------------------
1911 void vvSlicer::GetRegisterExtent(int ext[6])
1912 { //out << __func__ << endl;
1913 copyExtent(mRegisterExtent, ext);
1915 //----------------------------------------------------------------------------