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 <vtkTextProperty.h>
29 #include <vtkTextActor.h>
30 #include <vtkTextSource.h>
31 #include <vtkActor2D.h>
32 #include <vtkCursor2D.h>
33 #include <vtkPolyDataMapper2D.h>
34 #include <vtkProperty2D.h>
35 #include <vtkCornerAnnotation.h>
36 #include <vtkImageMapToWindowLevelColors.h>
37 #include <vtkImageData.h>
38 #include <vtkImageActor.h>
39 #include <vvBlendImageActor.h>
40 #include <vtkToolkits.h>
41 #include <vtkObjectFactory.h>
42 #include <vtkPointData.h>
43 #include <vtkDataArray.h>
44 #include <vtkFloatArray.h>
45 #include <vtkClipPolyData.h>
46 #include <vtkActor2DCollection.h>
47 #include <vtkGlyph3D.h>
49 #include <vtkCursor3D.h>
50 #include <vtkProperty.h>
52 #include <vtkLightCollection.h>
53 #include <vtkScalarBarActor.h>
54 #include <vtkLookupTable.h>
56 #include <vtkRenderer.h>
57 #include <vtkRendererCollection.h>
58 #include <vtkRenderWindow.h>
59 #include <vtkRenderWindowInteractor.h>
60 #include <vtkCamera.h>
61 #include <vtkCallbackCommand.h>
62 #include <vtkCommand.h>
63 #include <vtkPolyDataMapper.h>
66 #include <vtkExtractVOI.h>
67 #include <vtkSphereSource.h>
68 #include <vtkCutter.h>
69 #include <vtkAssignAttribute.h>
70 #include <vtkImageAccumulate.h>
71 #include <vtkImageReslice.h>
72 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
73 # include <vtkImageMapper3D.h>
74 # include <vtkImageSliceMapper.h>
77 vtkCxxRevisionMacro(vvSlicer, "DummyRevision");
78 vtkStandardNewMacro(vvSlicer);
79 static void copyExtent(int* in, int* to){
80 for(int i=0; i<6; ++i) to[i]=in[i];
82 //------------------------------------------------------------------------------
85 mFusionSequenceCode = -1;
86 this->UnInstallPipeline();
88 mReducedExtent = new int[6];
90 mCurrentFusionTSlice = 0;
91 mCurrentOverlayTSlice = 0;
92 mUseReducedExtent = false;
94 mCurrent[0] = -VTK_DOUBLE_MAX;
95 mCurrent[1] = -VTK_DOUBLE_MAX;
96 mCurrent[2] = -VTK_DOUBLE_MAX;
98 mCursor[0] = 0;//-VTK_DOUBLE_MAX;
99 mCursor[1] = 0;//-VTK_DOUBLE_MAX;
100 mCursor[2] = 0;//-VTK_DOUBLE_MAX;
101 mCursor[3] = 0;//-VTK_DOUBLE_MAX;
111 crossCursor = vtkSmartPointer<vtkCursor2D>::New();
112 crossCursor->AllOff();
113 crossCursor->AxesOn();
114 crossCursor->SetTranslationMode(1);
115 crossCursor->SetRadius(2);
117 pdm = vtkSmartPointer<vtkPolyDataMapper2D>::New();
118 pdm->SetInput(crossCursor->GetOutput());
120 pdmA = vtkSmartPointer<vtkActor2D>::New();
121 pdmA->SetMapper(pdm);
122 pdmA->GetProperty()->SetColor(255,10,212);
123 pdmA->SetVisibility(0);
124 pdmA->SetPickable(0);
126 ca = vtkSmartPointer<vtkCornerAnnotation>::New();
127 ca->GetTextProperty()->SetColor(255,10,212);
128 ca->SetVisibility(1);
136 legend = vtkSmartPointer<vtkScalarBarActor>::New();
137 //legend->SetTitle("test!");
138 legend->SetPosition(0.82,0.18);
139 legend->SetWidth(0.1);
140 legend->SetVisibility(0);
141 legend->SetLabelFormat("%.1f");
142 this->GetRenderer()->AddActor(legend);
143 showFusionLegend = false;
145 this->InstallPipeline();
147 mLinkOverlayWindowLevel = true;
148 mImageVisibility = true;
150 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
151 this->GetImageActor()->GetMapper()->BorderOn();
154 mSlicingTransform = vtkSmartPointer<vtkTransform>::New();
155 mConcatenatedTransform = vtkSmartPointer<vtkTransform>::New();
156 mConcatenatedFusionTransform = vtkSmartPointer<vtkTransform>::New();
157 mConcatenatedOverlayTransform = vtkSmartPointer<vtkTransform>::New();
158 mFirstSetSliceOrientation = true;
160 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
166 return mOverlayMapper.GetPointer();
168 //------------------------------------------------------------------------------
171 //------------------------------------------------------------------------------
172 vvBlendImageActor* vvSlicer::GetOverlayActor()
174 return mOverlayActor.GetPointer();
176 //------------------------------------------------------------------------------
179 //------------------------------------------------------------------------------
180 vtkImageMapToColors* vvSlicer::GetFusionMapper()
182 return mFusionMapper.GetPointer();
184 //------------------------------------------------------------------------------
187 //------------------------------------------------------------------------------
188 vtkImageActor* vvSlicer::GetFusionActor()
190 return mFusionActor.GetPointer();
192 //------------------------------------------------------------------------------
195 //------------------------------------------------------------------------------
196 vtkActor* vvSlicer::GetVFActor()
198 return mVFActor.GetPointer();
200 //------------------------------------------------------------------------------
203 //------------------------------------------------------------------------------
204 vtkCornerAnnotation* vvSlicer::GetAnnotation()
206 return ca.GetPointer();
208 //------------------------------------------------------------------------------
211 //------------------------------------------------------------------------------
212 void vvSlicer::EnableReducedExtent(bool b)
214 mUseReducedExtent = b;
216 //------------------------------------------------------------------------------
219 //------------------------------------------------------------------------------
220 void vvSlicer::SetReducedExtent(int * ext)
222 copyExtent(ext, mReducedExtent);
224 //------------------------------------------------------------------------------
227 //------------------------------------------------------------------------------
228 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
231 mSurfaceCutActors.push_back(new vvMeshActor());
233 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
235 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
236 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
237 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
241 //------------------------------------------------------------------------------
244 //------------------------------------------------------------------------------
245 void vvSlicer::ToggleContourSuperposition()
247 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
248 i!=mSurfaceCutActors.end(); i++)
249 (*i)->ToggleSuperposition();
251 //------------------------------------------------------------------------------
254 //------------------------------------------------------------------------------
255 void vvSlicer::SetCursorColor(int r,int g, int b)
257 pdmA->GetProperty()->SetColor(r,g,b);
259 //------------------------------------------------------------------------------
262 //------------------------------------------------------------------------------
263 void vvSlicer::SetCursorVisibility(bool s)
265 pdmA->SetVisibility(s);
267 //------------------------------------------------------------------------------
270 //------------------------------------------------------------------------------
271 bool vvSlicer::GetCursorVisibility()
273 return pdmA->GetVisibility();
275 //------------------------------------------------------------------------------
278 //------------------------------------------------------------------------------
279 void vvSlicer::SetCornerAnnotationVisibility(bool s)
281 ca->SetVisibility(s);
283 //------------------------------------------------------------------------------
286 //------------------------------------------------------------------------------
287 bool vvSlicer::GetCornerAnnotationVisibility()
289 return ca->GetVisibility();
291 //------------------------------------------------------------------------------
294 //------------------------------------------------------------------------------
295 vvSlicer::~vvSlicer()
297 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
298 i!=mSurfaceCutActors.end(); i++)
300 delete [] mReducedExtent;
302 //------------------------------------------------------------------------------
304 //------------------------------------------------------------------------------
305 double* vvSlicer::GetCurrentPosition()
307 return mCurrentBeforeSlicingTransform;
309 //------------------------------------------------------------------------------
311 //------------------------------------------------------------------------------
312 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
314 mCurrentBeforeSlicingTransform[0]=x;
315 mCurrentBeforeSlicingTransform[1]=y;
316 mCurrentBeforeSlicingTransform[2]=z;
317 mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
318 if (t>=0) SetTSlice(t);
320 //------------------------------------------------------------------------------
323 //------------------------------------------------------------------------------
324 void vvSlicer::SetImage(vvImage::Pointer image)
326 if (image->GetVTKImages().size()) {
329 if (!mImageReslice) {
330 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
331 mImageReslice->SetInterpolationModeToLinear();
332 mImageReslice->AutoCropOutputOn();
333 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
336 mConcatenatedTransform->Identity();
337 mConcatenatedTransform->Concatenate(mImage->GetTransform()[0]);
338 mConcatenatedTransform->Concatenate(mSlicingTransform);
339 mImageReslice->SetResliceTransform(mConcatenatedTransform);
340 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
341 mImageReslice->UpdateInformation();
343 this->Superclass::SetInput(mImageReslice->GetOutput());
346 this->GetInput()->GetWholeExtent(extent);
348 // Prevent crash when reload -> change slice if outside extent
349 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
350 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
353 // Make sure that the required part image has been computed
354 extent[SliceOrientation*2] = Slice;
355 extent[SliceOrientation*2+1] = Slice;
356 mImageReslice->GetOutput()->SetUpdateExtent(extent);
357 mImageReslice->GetOutput()->Update();
359 this->UpdateDisplayExtent();
362 ca->SetText(0,mFileName.c_str());
365 //------------------------------------------------------------------------------
368 //------------------------------------------------------------------------------
369 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
371 if (overlay->GetVTKImages().size()) {
373 mOverlayVisibility = true;
375 if (!mOverlayReslice) {
376 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
377 mOverlayReslice->SetInterpolationModeToLinear();
378 mOverlayReslice->AutoCropOutputOn();
379 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
382 mConcatenatedOverlayTransform->Identity();
383 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
384 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
385 mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
386 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
387 mImageReslice->UpdateInformation();
390 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
391 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
393 if (!mOverlayActor) {
394 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
395 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
396 mOverlayActor->SetPickable(0);
397 mOverlayActor->SetVisibility(true);
398 mOverlayActor->SetOpacity(0.5);
399 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
400 mOverlayActor->GetMapper()->BorderOn();
404 //stupid but necessary : the Overlay need to be rendered before fusion
406 this->GetRenderer()->RemoveActor(mFusionActor);
407 this->GetRenderer()->AddActor(mOverlayActor);
408 this->GetRenderer()->AddActor(mFusionActor);
410 this->GetRenderer()->AddActor(mOverlayActor);
412 //Synchronize orientation and slice
413 AdjustResliceToSliceOrientation(mOverlayReslice);
414 this->UpdateDisplayExtent();
415 this->SetTSlice(mCurrentTSlice);
418 //------------------------------------------------------------------------------
421 //------------------------------------------------------------------------------
422 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
424 mFusionSequenceCode = fusionSequenceCode;
425 if (fusion->GetVTKImages().size()) {
427 mFusionVisibility = true;
429 if (!mFusionReslice) {
430 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
431 mFusionReslice->SetInterpolationModeToLinear();
432 mFusionReslice->AutoCropOutputOn();
433 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
436 mConcatenatedFusionTransform->Identity();
437 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
438 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
439 mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
440 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
441 mFusionReslice->UpdateInformation();
444 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
446 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
448 lut->SetValueRange(0, 1);
449 lut->SetSaturationRange(0, 0);
451 mFusionMapper->SetLookupTable(lut);
452 mFusionMapper->SetInput(mFusionReslice->GetOutput());
455 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
456 mFusionActor->SetInput(mFusionMapper->GetOutput());
457 mFusionActor->SetPickable(0);
458 mFusionActor->SetVisibility(true);
459 mFusionActor->SetOpacity(0.7);
460 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
461 mFusionActor->GetMapper()->BorderOn();
463 this->GetRenderer()->AddActor(mFusionActor);
466 //Synchronize orientation and slice
467 AdjustResliceToSliceOrientation(mFusionReslice);
468 this->UpdateDisplayExtent();
469 this->SetTSlice(mCurrentTSlice);
472 //------------------------------------------------------------------------------
475 //------------------------------------------------------------------------------
476 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
479 if (actor_type == "image")
480 vis = mImageVisibility;
481 else if (actor_type == "vector")
483 else if (actor_type == "overlay")
484 vis = mOverlayVisibility;
485 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
486 vis = mFusionVisibility;
487 else if (actor_type == "contour")
488 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
491 //------------------------------------------------------------------------------
493 //------------------------------------------------------------------------------
494 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
496 if (actor_type == "image")
497 mImageVisibility = vis;
498 else if (actor_type == "vector")
500 else if (actor_type == "overlay")
501 mOverlayVisibility = vis;
502 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
503 mFusionVisibility = vis;
504 else if (actor_type == "contour")
505 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
506 UpdateDisplayExtent();
508 //------------------------------------------------------------------------------
510 //------------------------------------------------------------------------------
511 void vvSlicer::SetVF(vvImage::Pointer vf)
513 if (vf->GetVTKImages().size()) {
515 mVFVisibility = true;
518 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
519 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
520 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
522 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
523 mAAFilter->SetInput(mVOIFilter->GetOutput());
524 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
525 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
528 mArrow = vtkSmartPointer<vvGlyphSource>::New();
529 mArrow->SetGlyphTypeToSpecificArrow();
530 mArrow->SetScale(mScale);
533 // Glyph the gradient vector (with arrows)
535 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
536 mGlyphFilter->SetInput(mAAFilter->GetOutput());
537 mGlyphFilter->SetSource(mArrow->GetOutput());
538 mGlyphFilter->ScalingOn();
539 mGlyphFilter->SetScaleModeToScaleByVector();
540 mGlyphFilter->OrientOn();
541 mGlyphFilter->SetVectorModeToUseVector();
542 mGlyphFilter->SetColorModeToColorByVector();
545 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
547 double mVFColorHSV[3];
548 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
549 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
550 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
551 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
554 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
555 mVFMapper->SetInput(mGlyphFilter->GetOutput());
556 mVFMapper->ImmediateModeRenderingOn();
557 mVFMapper->SetLookupTable(mVFColorLUT);
560 mVFActor = vtkSmartPointer<vtkActor>::New();
561 mVFActor->SetMapper(mVFMapper);
562 mVFActor->SetPickable(0);
563 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
564 this->UpdateDisplayExtent();
565 this->GetRenderer()->AddActor(mVFActor);
568 SetTSlice(mCurrentTSlice);
571 //------------------------------------------------------------------------------
574 //------------------------------------------------------------------------------
575 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
577 mLandmarks = landmarks;
581 mCross = vtkSmartPointer<vtkCursor3D>::New();
583 mClipBox = vtkSmartPointer<vtkBox>::New();
585 mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
587 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
589 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
591 mLandActor = vtkSmartPointer<vtkActor>::New();
593 mCross->SetFocalPoint(0.0,0.0,0.0);
594 mCross->SetModelBounds(-10,10,-10,10,-10,10);
598 mLandClipper->SetClipFunction(mClipBox);
599 mLandClipper->InsideOutOn();
600 mLandClipper->SetInput(mLandmarks->GetOutput());
602 mLandGlyph->SetSource(mCross->GetOutput());
603 mLandGlyph->SetInput(mLandClipper->GetOutput());
604 //mLandGlyph->SetIndexModeToScalar();
605 //mLandGlyph->SetRange(0,1);
606 //mLandGlyph->ScalingOff();
608 //mLandGlyph->SetColorModeToColorByScalar();
610 mLandGlyph->SetScaleModeToDataScalingOff();
611 mLandGlyph->SetIndexModeToOff();
613 mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
614 //mLandMapper->ScalarVisibilityOff();
616 mLandActor->SetMapper(mLandMapper);
617 mLandActor->GetProperty()->SetColor(255,10,212);
618 mLandActor->SetPickable(0);
619 mLandActor->SetVisibility(true);
620 this->UpdateDisplayExtent();
621 this->GetRenderer()->AddActor(mLandActor);
624 //------------------------------------------------------------------------------
626 //------------------------------------------------------------------------------
627 //FIXME: this function leaks memory, we should fix it someday :)
628 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
630 if (actor_type == "vector") {
631 Renderer->RemoveActor(mVFActor);
640 if (actor_type == "overlay") {
641 Renderer->RemoveActor(mOverlayActor);
643 mOverlayActor = NULL;
644 mOverlayMapper = NULL;
646 if ( (actor_type == "fusion") || (actor_type == "fusionSequence") ) {
647 Renderer->RemoveActor(mFusionActor);
650 mFusionMapper = NULL;
652 if (actor_type == "contour") {
653 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
654 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
657 //------------------------------------------------------------------------------
660 //------------------------------------------------------------------------------
661 void vvSlicer::SetVFSubSampling(int sub)
664 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
667 UpdateDisplayExtent();
670 //------------------------------------------------------------------------------
673 //------------------------------------------------------------------------------
674 void vvSlicer::SetVFScale(int scale)
678 mArrow->SetScale(mScale);
679 UpdateDisplayExtent();
682 //------------------------------------------------------------------------------
684 //------------------------------------------------------------------------------
685 void vvSlicer::SetVFWidth(int width)
689 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
690 UpdateDisplayExtent();
693 //------------------------------------------------------------------------------
696 //------------------------------------------------------------------------------
697 void vvSlicer::SetVFLog(int log)
701 mGlyphFilter->SetUseLog(mVFLog);
702 mGlyphFilter->Modified();
704 UpdateDisplayExtent();
707 //------------------------------------------------------------------------------
710 //------------------------------------------------------------------------------
711 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
713 if (!updateLinkedImages) {
715 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
717 mConcatenatedTransform->Identity();
718 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
719 mConcatenatedTransform->Concatenate(mSlicingTransform);
720 UpdateDisplayExtent();
726 else if ((unsigned int)t >= mImage->GetVTKImages().size())
727 mCurrentTSlice = mImage->GetVTKImages().size() -1;
732 mConcatenatedTransform->Identity();
733 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
734 mConcatenatedTransform->Concatenate(mSlicingTransform);
737 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
738 if (mVF && mVFActor->GetVisibility()) {
739 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
740 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
743 if (mOverlay && mOverlayActor->GetVisibility()) {
744 if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
745 mCurrentOverlayTSlice = t;
746 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
748 // Update overlay transform
749 mConcatenatedOverlayTransform->Identity();
750 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
751 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
754 //update the fusion ; except in case this is a fusionSequence, in which case both 'times' should be independent.
755 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode<0)) {
756 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
757 mCurrentFusionTSlice = t;
758 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
760 // Update fusion transform
761 mConcatenatedFusionTransform->Identity();
762 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
763 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
766 if (mSurfaceCutActors.size() > 0)
767 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
768 i!=mSurfaceCutActors.end(); i++)
769 (*i)->SetTimeSlice(mCurrentTSlice);
770 UpdateDisplayExtent();
772 //------------------------------------------------------------------------------
775 //------------------------------------------------------------------------------
776 void vvSlicer::SetFusionSequenceTSlice(int t)
778 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode>=0)) {
779 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
780 mCurrentFusionTSlice = t;
781 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
782 // Update fusion transform
783 mConcatenatedFusionTransform->Identity();
784 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]); //not really useful...
785 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
789 UpdateDisplayExtent();
791 //------------------------------------------------------------------------------
794 //------------------------------------------------------------------------------
795 int vvSlicer::GetTSlice()
797 return mCurrentTSlice;
799 //------------------------------------------------------------------------------
801 //------------------------------------------------------------------------------
802 int vvSlicer::GetMaxCurrentTSlice()
804 int t = mCurrentTSlice;
806 t = std::max(t, mCurrentOverlayTSlice);
807 if(mFusion&& (mFusionSequenceCode<0)) //ignore fusionSequence data: for these, the times are not to be related (this way)
808 t = std::max(t, mCurrentFusionTSlice);
811 //------------------------------------------------------------------------------
813 //------------------------------------------------------------------------------
814 int vvSlicer::GetFusionTSlice()
816 return mCurrentFusionTSlice;
818 //------------------------------------------------------------------------------
820 //------------------------------------------------------------------------------
821 int vvSlicer::GetOverlayTSlice()
823 return mCurrentOverlayTSlice;
825 //------------------------------------------------------------------------------
827 //------------------------------------------------------------------------------
828 void vvSlicer::SetSliceOrientation(int orientation)
830 //if 2D image, force to watch in Axial View
832 this->GetInput()->GetWholeExtent(extent);
833 if (extent[5]-extent[4] <= 2)
834 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
836 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
837 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
838 vtkErrorMacro("Error - invalid slice orientation " << orientation);
842 this->SliceOrientation = orientation;
845 AdjustResliceToSliceOrientation(mFusionReslice);
848 AdjustResliceToSliceOrientation(mOverlayReslice);
852 // Go to current cursor position
853 // double* cursorPos = GetCursorPosition();
854 // DDV(cursorPos, 3);
855 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
857 if (mFirstSetSliceOrientation) {
858 int *range = this->GetSliceRange();
860 this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
861 mFirstSetSliceOrientation = false;
863 else if (this->Renderer && this->GetInput()) {
864 double s = mCursor[orientation];
865 double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
866 this->Slice = static_cast<int>(sCursor);
869 this->UpdateOrientation();
870 this->UpdateDisplayExtent();
872 if (this->Renderer && this->GetInput()) {
873 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
874 this->Renderer->ResetCamera();
875 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
880 //----------------------------------------------------------------------------
882 //------------------------------------------------------------------------------
883 // This function ensures that we sample the slices of a vtkImageReslice filter
884 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
885 // In other words, we change the grid of the reslice in the same way as the grid
886 // of the displayed image in the slicing direction.
887 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
889 // Reset autocrop and update output information
890 reslice->SetOutputOriginToDefault();
891 reslice->SetOutputSpacingToDefault();
892 reslice->GetOutput()->UpdateInformation();
894 // Ge new origin / spacing
897 reslice->GetOutput()->GetOrigin(origin);
898 reslice->GetOutput()->GetSpacing(spacing);
900 // Use similar spacing as the image in the direction SliceOrientation
901 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
903 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
904 // Step 1: from world coordinates to image coordinates
905 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
906 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
908 // Step 2: round to nearest grid positionInc. This has been validated as the only
909 // way to have something consistent with the thickness of a 2D slice visible on the
910 // other slices. The thickness is accounted for so if the 2D slice is to thin and
911 // between two slices, one will never be able to see this 2D slice (bug #1883).
912 origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
914 // Step 3: back to world coordinates
915 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
916 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
918 // Set new spacing and origin
919 reslice->SetOutputOrigin(origin);
920 reslice->SetOutputSpacing(spacing);
921 reslice->UpdateInformation();
922 reslice->GetOutput()->UpdateInformation();
924 //------------------------------------------------------------------------------
926 //----------------------------------------------------------------------------
927 int * vvSlicer::GetExtent(){
929 if (mUseReducedExtent) {
930 w_ext = mReducedExtent;
931 } else w_ext = GetInput()->GetWholeExtent();
934 //----------------------------------------------------------------------------
937 //----------------------------------------------------------------------------
938 int vvSlicer::GetOrientation()
940 return this->SliceOrientation;
942 //----------------------------------------------------------------------------
945 //----------------------------------------------------------------------------
946 void vvSlicer::UpdateDisplayExtent()
948 vtkImageData *input = this->GetInput();
949 if (!input || !this->ImageActor) {
952 input->UpdateInformation();
953 this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
955 // Local copy of extent
957 int* ext = GetExtent();
958 copyExtent(ext, w_ext);
960 w_ext[ this->SliceOrientation*2 ] = this->Slice;
961 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
964 this->ImageActor->SetVisibility(mImageVisibility);
965 this->ImageActor->SetDisplayExtent(w_ext);
966 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
968 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
971 // Overlay image actor
972 if (mOverlay && mOverlayVisibility) {
973 AdjustResliceToSliceOrientation(mOverlayReslice);
975 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
976 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
977 mOverlayActor->SetVisibility(!out);
978 mOverlayActor->SetDisplayExtent( overExtent );
979 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
981 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
985 mOverlayActor->SetVisibility(false);
987 // Fusion image actor
988 if (mFusion && mFusionVisibility) {
989 AdjustResliceToSliceOrientation(mFusionReslice);
991 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
992 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
993 mFusionActor->SetVisibility(!out);
994 mFusionActor->SetDisplayExtent( fusExtent );
995 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
997 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
1001 mFusionActor->SetVisibility(false);
1003 // Vector field actor
1004 double* camera = Renderer->GetActiveCamera()->GetPosition();
1005 double* image_bounds = ImageActor->GetBounds();
1006 double position[3] = {0, 0, 0};
1007 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1009 //print_vector<double, 6>("camera", camera);
1010 //print_vector<double, 6>("image_bounds", image_bounds);
1011 //print_vector<double, 3>("position", position);
1013 // find where to place the VF actor. to deal with
1014 // z-buffer issues, the VF is placed right in front of the image,
1015 // subject to a small offset. the position actually depends on the
1016 // the location of the camera relative to the image.
1018 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1021 if (mVF && mVFVisibility) {
1023 mVF->GetVTKImages()[0]->UpdateInformation();
1024 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
1025 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1026 mVFActor->SetVisibility(!out);
1027 mVOIFilter->SetVOI(vfExtent);
1028 int orientation[3] = {1,1,1};
1029 orientation[this->SliceOrientation] = 0;
1030 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1031 mVFMapper->Update();
1033 position[this->SliceOrientation] += offset;
1034 mVFActor->SetPosition(position);
1037 mVFActor->SetVisibility(false);
1043 for(unsigned int i=0; i<6; i++)
1044 bounds[i] = ImageActor->GetBounds()[i];
1045 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1046 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1047 mClipBox->SetBounds(bounds);
1051 position[this->SliceOrientation] = offset;
1052 mLandActor->SetPosition(position);
1055 // Figure out the correct clipping range
1056 if (this->Renderer) {
1057 if (this->InteractorStyle &&
1058 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1059 this->Renderer->ResetCameraClippingRange();
1061 vtkCamera *cam = this->Renderer->GetActiveCamera();
1064 this->ImageActor->GetBounds(bounds);
1065 double spos = (double)bounds[this->SliceOrientation * 2];
1066 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1067 double range = fabs(spos - cpos);
1068 double *spacing = input->GetSpacing();
1069 double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1070 cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1075 //----------------------------------------------------------------------------
1077 //----------------------------------------------------------------------------
1078 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
1079 vtkImageData *targetImage, int targetExtent[6])
1082 for(unsigned int i=0; i<6; i++) {
1083 // From source voxel coordinates to world coordinates
1084 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
1086 // From world coordinates to floating point target voxel coordinates
1087 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1089 // Round to current slice or larger extent
1090 if(i/2==this->GetOrientation())
1091 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1093 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1095 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1098 //----------------------------------------------------------------------------
1100 //----------------------------------------------------------------------------
1101 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1106 for (int i = 0; i < maxBound; i = i+2) {
1107 //if we are totally outside the image
1108 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1112 //crop to the limit of the image
1113 extent[i] = std::max(extent[i], refExtent[i]);
1114 extent[i] = std::min(extent[i], refExtent[i+1]);;
1115 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1116 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1119 for (int i = 0; i < maxBound; i = i+2) {
1120 extent[i] = refExtent[i];
1121 extent[i+1] = refExtent[i];
1125 //----------------------------------------------------------------------------
1128 //----------------------------------------------------------------------------
1129 void vvSlicer::UpdateOrientation()
1131 // Set the camera position
1132 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1134 switch (this->SliceOrientation) {
1135 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1136 cam->SetFocalPoint(0,0,0);
1137 cam->SetPosition(0,0,-1); // -1 if medical ?
1138 cam->SetViewUp(0,-1,0);
1141 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1142 cam->SetFocalPoint(0,0,0);
1143 cam->SetPosition(0,-1,0); // 1 if medical ?
1144 cam->SetViewUp(0,0,1);
1147 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1148 cam->SetFocalPoint(0,0,0);
1149 cam->SetPosition(-1,0,0); // -1 if medical ?
1150 cam->SetViewUp(0,0,1);
1155 //----------------------------------------------------------------------------
1158 //----------------------------------------------------------------------------
1159 void vvSlicer::SetOpacity(double s)
1161 this->GetImageActor()->SetOpacity(s);
1163 //----------------------------------------------------------------------------
1166 //----------------------------------------------------------------------------
1167 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1169 this->Superclass::SetRenderWindow(rw);
1170 this->SetupInteractor(rw->GetInteractor());
1171 ca->SetImageActor(this->GetImageActor());
1172 ca->SetWindowLevel(this->GetWindowLevel());
1173 ca->SetText(3, "<window>\n<level>");
1185 crossCursor->SetModelBounds(bounds);
1186 this->GetRenderer()->AddActor(pdmA);
1187 this->GetRenderer()->AddActor(ca);
1188 this->GetRenderer()->ResetCamera();
1190 //this is just a mapping between the labeling of the orientations presented to the user and
1191 //the one used by vtk
1192 SetSliceOrientation(2-(orientation%3));
1195 //----------------------------------------------------------------------------
1198 //----------------------------------------------------------------------------
1199 void vvSlicer::ResetCamera()
1201 this->GetRenderer()->ResetCamera();
1203 //----------------------------------------------------------------------------
1206 //----------------------------------------------------------------------------
1207 void vvSlicer::SetDisplayMode(bool i)
1209 this->GetRenderer()->SetDraw(i);
1210 if (i) UpdateDisplayExtent();
1212 //----------------------------------------------------------------------------
1215 //----------------------------------------------------------------------------
1216 void vvSlicer::FlipHorizontalView()
1218 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1220 double *position = cam->GetPosition();
1221 double factor[3] = {1, 1, 1};
1222 factor[this->SliceOrientation] = -1;
1223 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1225 /* switch (this->SliceOrientation) {
1226 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1227 cam->SetPosition(position[0],position[1],-position[2]);
1230 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1231 cam->SetPosition(position[0],-position[1],position[2]);
1234 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1235 cam->SetPosition(-position[0],position[1],position[2]);
1239 this->Renderer->ResetCameraClippingRange();
1240 this->UpdateDisplayExtent();
1243 //----------------------------------------------------------------------------
1246 //----------------------------------------------------------------------------
1247 void vvSlicer::FlipVerticalView()
1249 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1251 FlipHorizontalView();
1252 double *viewup = cam->GetViewUp();
1253 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1254 this->UpdateDisplayExtent();
1257 //----------------------------------------------------------------------------
1260 //----------------------------------------------------------------------------
1261 void vvSlicer::SetColorWindow(double window)
1263 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1265 double level = this->GetWindowLevel()->GetLevel();
1266 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1269 this->vtkImageViewer2::SetColorWindow(window);
1271 //----------------------------------------------------------------------------
1273 //----------------------------------------------------------------------------
1274 void vvSlicer::SetColorLevel(double level)
1276 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1278 double window = this->GetWindowLevel()->GetWindow();
1279 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1282 this->vtkImageViewer2::SetColorLevel(level);
1284 //----------------------------------------------------------------------------
1286 //----------------------------------------------------------------------------
1287 double vvSlicer::GetOverlayColorWindow()
1290 return mOverlayMapper->GetWindow();
1294 //----------------------------------------------------------------------------
1296 //----------------------------------------------------------------------------
1297 double vvSlicer::GetOverlayColorLevel()
1300 return mOverlayMapper->GetLevel();
1304 //----------------------------------------------------------------------------
1306 //----------------------------------------------------------------------------
1307 void vvSlicer::SetOverlayColorWindow(double window)
1310 mOverlayMapper->SetWindow(window);
1312 //----------------------------------------------------------------------------
1314 //----------------------------------------------------------------------------
1315 void vvSlicer::SetOverlayColorLevel(double level)
1318 mOverlayMapper->SetLevel(level);
1320 //----------------------------------------------------------------------------
1322 //----------------------------------------------------------------------------
1323 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1324 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1326 //Get mouse pointer position in view coordinates
1329 for(int i=0; i<3; i++) {
1330 corner1[i] = mCurrent[i];
1331 corner2[i] = mCurrent[i];
1334 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1335 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1337 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1339 this->Renderer->GetTiledSize(&w, &h);
1340 corner1[0] -= 0.2*h/(double)w;
1341 corner2[0] += 0.2*h/(double)w;
1344 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1345 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1347 //Convert to image pixel coordinates (rounded)
1348 transform->TransformPoint(corner1, corner1);
1349 transform->TransformPoint(corner2, corner2);
1350 int iLocalExtents[6];
1351 for(int i=0; i<3; i++) {
1352 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1353 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1355 iLocalExtents[i*2 ] = lrint(corner1[i]);
1356 iLocalExtents[i*2+1] = lrint(corner2[i]);
1358 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1359 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1362 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1363 voiFilter->SetInput(image);
1364 voiFilter->SetVOI(iLocalExtents);
1365 voiFilter->Update();
1366 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1372 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1373 accFilter->SetInput(voiFilter->GetOutput());
1374 accFilter->Update();
1376 min = *(accFilter->GetMin());
1377 max = *(accFilter->GetMax());
1379 //----------------------------------------------------------------------------
1381 //----------------------------------------------------------------------------
1382 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1388 if (ix < image->GetWholeExtent()[0] ||
1389 ix > image->GetWholeExtent()[1] ||
1390 iy < image->GetWholeExtent()[2] ||
1391 iy > image->GetWholeExtent()[3] ||
1392 iz < image->GetWholeExtent()[4] ||
1393 iz > image->GetWholeExtent()[5] )
1394 return std::numeric_limits<double>::quiet_NaN();
1396 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1398 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1400 //----------------------------------------------------------------------------
1402 //----------------------------------------------------------------------------
1403 void vvSlicer::Render()
1405 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1406 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1407 legend->UseOpacityOn();
1408 legend->SetVisibility(1);
1410 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1411 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1412 legend->UseOpacityOff();
1413 legend->SetVisibility(1);
1414 } else legend->SetVisibility(0);
1416 if (ca->GetVisibility()) {
1417 std::stringstream worldPos;
1419 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1420 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1421 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1422 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1424 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1425 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1426 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1427 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1428 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1429 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1433 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1435 if(ImageActor->GetVisibility())
1436 worldPos << "data value : " << value << std::endl;
1438 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1439 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1440 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1443 worldPos << "pixel : " << ix << ' '
1449 ca->SetText(1,worldPos.str().c_str());
1451 std::stringstream slicePos;
1452 slicePos << "Slice: " << this->GetSlice();
1453 ca->SetText(2, slicePos.str().c_str());
1456 if (pdmA->GetVisibility()) {
1457 double x = mCursor[0];
1458 double y = mCursor[1];
1459 double z = mCursor[2];
1460 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1461 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1462 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1464 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1465 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1466 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1467 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1468 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1469 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1470 vtkRenderer * renderer = this->Renderer;
1472 renderer->WorldToView(x,y,z);
1473 renderer->ViewToNormalizedViewport(x,y,z);
1474 renderer->NormalizedViewportToViewport(x,y);
1475 renderer->ViewportToNormalizedDisplay(x,y);
1476 renderer->NormalizedDisplayToDisplay(x,y);
1477 crossCursor->SetFocalPoint(x,y,z);
1479 crossCursor->SetFocalPoint(-1,-1,z);
1483 if (mOverlay && mOverlayActor->GetVisibility()) {
1484 if(mLinkOverlayWindowLevel) {
1485 mOverlayMapper->SetWindow(this->GetColorWindow());
1486 mOverlayMapper->SetLevel(this->GetColorLevel());
1488 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1489 mOverlayMapper->GetOutput()->Update();
1490 mOverlayMapper->Update();
1495 this->GetRenderWindow()->Render();
1497 //----------------------------------------------------------------------------
1500 //----------------------------------------------------------------------------
1501 void vvSlicer::UpdateCursorPosition()
1503 pdmA->SetVisibility(true);
1504 mCursor[0] = mCurrent[0];
1505 mCursor[1] = mCurrent[1];
1506 mCursor[2] = mCurrent[2];
1507 mCursor[3] = mCurrentTSlice;
1509 //----------------------------------------------------------------------------
1512 //----------------------------------------------------------------------------
1513 void vvSlicer::UpdateLandmarks()
1515 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1516 if (pd->GetPoints()) {
1517 //mLandGlyph->SetRange(0,1);
1518 //mLandGlyph->Modified();
1519 //mLandGlyph->Update();
1521 mClipBox->Modified();
1522 mLandClipper->Update();
1523 mLandMapper->Update();
1524 //Let's add the captions
1525 //First remove all captions:
1526 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1527 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1528 //allActors2D->Remove (mLandLabelActors[i]);
1530 mLandLabelActors.clear();
1531 //Next add the captions to the displayed points
1532 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1533 double *position = mLandClipper->GetOutput()->GetPoint(id);
1534 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1535 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1536 label_actor->SetCaption(label);
1537 label_actor->SetAttachmentPoint(position);
1538 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1539 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1540 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1541 label_actor->GetCaptionTextProperty()->SetBold(0);
1542 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1543 label_actor->BorderOff();
1544 label_actor->LeaderOff();
1545 label_actor->ThreeDimensionalLeaderOff();
1546 mLandLabelActors.push_back(label_actor);
1547 this->Renderer->AddActor2D(mLandLabelActors[id]);
1552 //----------------------------------------------------------------------------
1555 //----------------------------------------------------------------------------
1556 void vvSlicer::SetSlice(int slice)
1558 int *range = this->GetSliceRange();
1560 if (slice < range[0]) {
1562 } else if (slice > range[1]) {
1567 if (this->Slice == slice) {
1571 this->Slice = slice;
1574 this->UpdateDisplayExtent();
1576 // Seems to work without this line
1579 //----------------------------------------------------------------------------
1581 //----------------------------------------------------------------------------
1582 int vvSlicer::GetTMax() {
1583 int tmax = (int)mImage->GetVTKImages().size() - 1;
1585 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1588 //----------------------------------------------------------------------------
1590 //----------------------------------------------------------------------------
1591 void vvSlicer::SetContourSlice()
1593 if (mSurfaceCutActors.size() > 0)
1594 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1595 i!=mSurfaceCutActors.end(); i++) {
1597 (*i)->SetSlicingOrientation(this->SliceOrientation);
1598 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1599 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1602 //----------------------------------------------------------------------------
1605 //----------------------------------------------------------------------------
1606 void vvSlicer::ForceUpdateDisplayExtent()
1608 this->UpdateDisplayExtent();
1610 //----------------------------------------------------------------------------
1613 //----------------------------------------------------------------------------
1614 int* vvSlicer::GetDisplayExtent()
1616 return this->GetImageActor()->GetDisplayExtent();
1618 //----------------------------------------------------------------------------
1621 //----------------------------------------------------------------------------
1622 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1624 this->Superclass::PrintSelf(os, indent);
1626 //----------------------------------------------------------------------------
1628 //----------------------------------------------------------------------------
1629 void vvSlicer::SetVFColor(double r, double g, double b)
1631 double mVFColorHSV[3];
1636 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1637 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1638 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1639 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);