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(2, "<slice>");
1174 ca->SetText(3, "<window>\n<level>");
1186 crossCursor->SetModelBounds(bounds);
1187 this->GetRenderer()->AddActor(pdmA);
1188 this->GetRenderer()->AddActor(ca);
1189 this->GetRenderer()->ResetCamera();
1191 //this is just a mapping between the labeling of the orientations presented to the user and
1192 //the one used by vtk
1193 SetSliceOrientation(2-(orientation%3));
1196 //----------------------------------------------------------------------------
1199 //----------------------------------------------------------------------------
1200 void vvSlicer::ResetCamera()
1202 this->GetRenderer()->ResetCamera();
1204 //----------------------------------------------------------------------------
1207 //----------------------------------------------------------------------------
1208 void vvSlicer::SetDisplayMode(bool i)
1210 this->GetRenderer()->SetDraw(i);
1211 if (i) UpdateDisplayExtent();
1213 //----------------------------------------------------------------------------
1216 //----------------------------------------------------------------------------
1217 void vvSlicer::FlipHorizontalView()
1219 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1221 double *position = cam->GetPosition();
1222 double factor[3] = {1, 1, 1};
1223 factor[this->SliceOrientation] = -1;
1224 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1226 /* switch (this->SliceOrientation) {
1227 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1228 cam->SetPosition(position[0],position[1],-position[2]);
1231 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1232 cam->SetPosition(position[0],-position[1],position[2]);
1235 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1236 cam->SetPosition(-position[0],position[1],position[2]);
1240 this->Renderer->ResetCameraClippingRange();
1241 this->UpdateDisplayExtent();
1244 //----------------------------------------------------------------------------
1247 //----------------------------------------------------------------------------
1248 void vvSlicer::FlipVerticalView()
1250 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1252 FlipHorizontalView();
1253 double *viewup = cam->GetViewUp();
1254 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1255 this->UpdateDisplayExtent();
1258 //----------------------------------------------------------------------------
1261 //----------------------------------------------------------------------------
1262 void vvSlicer::SetColorWindow(double window)
1264 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1266 double level = this->GetWindowLevel()->GetLevel();
1267 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1270 this->vtkImageViewer2::SetColorWindow(window);
1272 //----------------------------------------------------------------------------
1274 //----------------------------------------------------------------------------
1275 void vvSlicer::SetColorLevel(double level)
1277 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1279 double window = this->GetWindowLevel()->GetWindow();
1280 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1283 this->vtkImageViewer2::SetColorLevel(level);
1285 //----------------------------------------------------------------------------
1287 //----------------------------------------------------------------------------
1288 double vvSlicer::GetOverlayColorWindow()
1291 return mOverlayMapper->GetWindow();
1295 //----------------------------------------------------------------------------
1297 //----------------------------------------------------------------------------
1298 double vvSlicer::GetOverlayColorLevel()
1301 return mOverlayMapper->GetLevel();
1305 //----------------------------------------------------------------------------
1307 //----------------------------------------------------------------------------
1308 void vvSlicer::SetOverlayColorWindow(double window)
1310 mOverlayMapper->SetWindow(window);
1312 //----------------------------------------------------------------------------
1314 //----------------------------------------------------------------------------
1315 void vvSlicer::SetOverlayColorLevel(double level)
1317 mOverlayMapper->SetLevel(level);
1319 //----------------------------------------------------------------------------
1321 //----------------------------------------------------------------------------
1322 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1323 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1325 //Get mouse pointer position in view coordinates
1328 for(int i=0; i<3; i++) {
1329 corner1[i] = mCurrent[i];
1330 corner2[i] = mCurrent[i];
1333 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1334 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1336 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1338 this->Renderer->GetTiledSize(&w, &h);
1339 corner1[0] -= 0.2*h/(double)w;
1340 corner2[0] += 0.2*h/(double)w;
1343 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1344 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1346 //Convert to image pixel coordinates (rounded)
1347 transform->TransformPoint(corner1, corner1);
1348 transform->TransformPoint(corner2, corner2);
1349 int iLocalExtents[6];
1350 for(int i=0; i<3; i++) {
1351 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1352 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1354 iLocalExtents[i*2 ] = lrint(corner1[i]);
1355 iLocalExtents[i*2+1] = lrint(corner2[i]);
1357 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1358 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1361 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1362 voiFilter->SetInput(image);
1363 voiFilter->SetVOI(iLocalExtents);
1364 voiFilter->Update();
1365 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1371 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1372 accFilter->SetInput(voiFilter->GetOutput());
1373 accFilter->Update();
1375 min = *(accFilter->GetMin());
1376 max = *(accFilter->GetMax());
1378 //----------------------------------------------------------------------------
1380 //----------------------------------------------------------------------------
1381 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1387 if (ix < image->GetWholeExtent()[0] ||
1388 ix > image->GetWholeExtent()[1] ||
1389 iy < image->GetWholeExtent()[2] ||
1390 iy > image->GetWholeExtent()[3] ||
1391 iz < image->GetWholeExtent()[4] ||
1392 iz > image->GetWholeExtent()[5] )
1393 return std::numeric_limits<double>::quiet_NaN();
1395 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1397 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1399 //----------------------------------------------------------------------------
1401 //----------------------------------------------------------------------------
1402 void vvSlicer::Render()
1404 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1405 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1406 legend->UseOpacityOn();
1407 legend->SetVisibility(1);
1409 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1410 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1411 legend->UseOpacityOff();
1412 legend->SetVisibility(1);
1413 } else legend->SetVisibility(0);
1415 if (ca->GetVisibility()) {
1416 std::stringstream worldPos;
1418 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1419 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1420 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1421 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1423 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1424 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1425 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1426 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1427 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1428 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1432 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1434 if(ImageActor->GetVisibility())
1435 worldPos << "data value : " << value << std::endl;
1437 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1438 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1439 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1442 worldPos << "pixel : " << ix << ' '
1448 ca->SetText(1,worldPos.str().c_str());
1451 if (pdmA->GetVisibility()) {
1452 double x = mCursor[0];
1453 double y = mCursor[1];
1454 double z = mCursor[2];
1455 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1456 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1457 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1459 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1460 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1461 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1462 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1463 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1464 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1465 vtkRenderer * renderer = this->Renderer;
1467 renderer->WorldToView(x,y,z);
1468 renderer->ViewToNormalizedViewport(x,y,z);
1469 renderer->NormalizedViewportToViewport(x,y);
1470 renderer->ViewportToNormalizedDisplay(x,y);
1471 renderer->NormalizedDisplayToDisplay(x,y);
1472 crossCursor->SetFocalPoint(x,y,z);
1474 crossCursor->SetFocalPoint(-1,-1,z);
1478 if (mOverlay && mOverlayActor->GetVisibility()) {
1479 if(mLinkOverlayWindowLevel) {
1480 mOverlayMapper->SetWindow(this->GetColorWindow());
1481 mOverlayMapper->SetLevel(this->GetColorLevel());
1483 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1484 mOverlayMapper->GetOutput()->Update();
1485 mOverlayMapper->Update();
1490 this->GetRenderWindow()->Render();
1492 //----------------------------------------------------------------------------
1495 //----------------------------------------------------------------------------
1496 void vvSlicer::UpdateCursorPosition()
1498 pdmA->SetVisibility(true);
1499 mCursor[0] = mCurrent[0];
1500 mCursor[1] = mCurrent[1];
1501 mCursor[2] = mCurrent[2];
1502 mCursor[3] = mCurrentTSlice;
1504 //----------------------------------------------------------------------------
1507 //----------------------------------------------------------------------------
1508 void vvSlicer::UpdateLandmarks()
1510 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1511 if (pd->GetPoints()) {
1512 //mLandGlyph->SetRange(0,1);
1513 //mLandGlyph->Modified();
1514 //mLandGlyph->Update();
1516 mClipBox->Modified();
1517 mLandClipper->Update();
1518 mLandMapper->Update();
1519 //Let's add the captions
1520 //First remove all captions:
1521 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1522 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1523 //allActors2D->Remove (mLandLabelActors[i]);
1525 mLandLabelActors.clear();
1526 //Next add the captions to the displayed points
1527 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1528 double *position = mLandClipper->GetOutput()->GetPoint(id);
1529 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1530 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1531 label_actor->SetCaption(label);
1532 label_actor->SetAttachmentPoint(position);
1533 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1534 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1535 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1536 label_actor->GetCaptionTextProperty()->SetBold(0);
1537 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1538 label_actor->BorderOff();
1539 label_actor->LeaderOff();
1540 label_actor->ThreeDimensionalLeaderOff();
1541 mLandLabelActors.push_back(label_actor);
1542 this->Renderer->AddActor2D(mLandLabelActors[id]);
1547 //----------------------------------------------------------------------------
1550 //----------------------------------------------------------------------------
1551 void vvSlicer::SetSlice(int slice)
1553 int *range = this->GetSliceRange();
1555 if (slice < range[0]) {
1557 } else if (slice > range[1]) {
1562 if (this->Slice == slice) {
1566 this->Slice = slice;
1569 this->UpdateDisplayExtent();
1571 // Seems to work without this line
1574 //----------------------------------------------------------------------------
1576 //----------------------------------------------------------------------------
1577 int vvSlicer::GetTMax() {
1578 int tmax = (int)mImage->GetVTKImages().size() - 1;
1580 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1583 //----------------------------------------------------------------------------
1585 //----------------------------------------------------------------------------
1586 void vvSlicer::SetContourSlice()
1588 if (mSurfaceCutActors.size() > 0)
1589 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1590 i!=mSurfaceCutActors.end(); i++) {
1592 (*i)->SetSlicingOrientation(this->SliceOrientation);
1593 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1594 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1597 //----------------------------------------------------------------------------
1600 //----------------------------------------------------------------------------
1601 void vvSlicer::ForceUpdateDisplayExtent()
1603 this->UpdateDisplayExtent();
1605 //----------------------------------------------------------------------------
1608 //----------------------------------------------------------------------------
1609 int* vvSlicer::GetDisplayExtent()
1611 return this->GetImageActor()->GetDisplayExtent();
1613 //----------------------------------------------------------------------------
1616 //----------------------------------------------------------------------------
1617 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1619 this->Superclass::PrintSelf(os, indent);
1621 //----------------------------------------------------------------------------
1623 //----------------------------------------------------------------------------
1624 void vvSlicer::SetVFColor(double r, double g, double b)
1626 double mVFColorHSV[3];
1631 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1632 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1633 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1634 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);