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();
159 //------------------------------------------------------------------------------
162 //------------------------------------------------------------------------------
163 vtkImageMapToWindowLevelColors* vvSlicer::GetOverlayMapper()
165 return mOverlayMapper.GetPointer();
167 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 vvBlendImageActor* vvSlicer::GetOverlayActor()
173 return mOverlayActor.GetPointer();
175 //------------------------------------------------------------------------------
178 //------------------------------------------------------------------------------
179 vtkImageMapToColors* vvSlicer::GetFusionMapper()
181 return mFusionMapper.GetPointer();
183 //------------------------------------------------------------------------------
186 //------------------------------------------------------------------------------
187 vtkImageActor* vvSlicer::GetFusionActor()
189 return mFusionActor.GetPointer();
191 //------------------------------------------------------------------------------
194 //------------------------------------------------------------------------------
195 vtkActor* vvSlicer::GetVFActor()
197 return mVFActor.GetPointer();
199 //------------------------------------------------------------------------------
202 //------------------------------------------------------------------------------
203 vtkCornerAnnotation* vvSlicer::GetAnnotation()
205 return ca.GetPointer();
207 //------------------------------------------------------------------------------
210 //------------------------------------------------------------------------------
211 void vvSlicer::EnableReducedExtent(bool b)
213 mUseReducedExtent = b;
215 //------------------------------------------------------------------------------
218 //------------------------------------------------------------------------------
219 void vvSlicer::SetReducedExtent(int * ext)
221 copyExtent(ext, mReducedExtent);
223 //------------------------------------------------------------------------------
226 //------------------------------------------------------------------------------
227 void vvSlicer::AddContour(vvMesh::Pointer contour,bool propagate)
230 mSurfaceCutActors.push_back(new vvMeshActor());
232 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice,mVF);
234 mSurfaceCutActors.back()->Init(contour,mCurrentTSlice);
235 mSurfaceCutActors.back()->SetSlicingOrientation(SliceOrientation);
236 this->GetRenderer()->AddActor(mSurfaceCutActors.back()->GetActor());
240 //------------------------------------------------------------------------------
243 //------------------------------------------------------------------------------
244 void vvSlicer::ToggleContourSuperposition()
246 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
247 i!=mSurfaceCutActors.end(); i++)
248 (*i)->ToggleSuperposition();
250 //------------------------------------------------------------------------------
253 //------------------------------------------------------------------------------
254 void vvSlicer::SetCursorColor(int r,int g, int b)
256 pdmA->GetProperty()->SetColor(r,g,b);
258 //------------------------------------------------------------------------------
261 //------------------------------------------------------------------------------
262 void vvSlicer::SetCursorVisibility(bool s)
264 pdmA->SetVisibility(s);
266 //------------------------------------------------------------------------------
269 //------------------------------------------------------------------------------
270 bool vvSlicer::GetCursorVisibility()
272 return pdmA->GetVisibility();
274 //------------------------------------------------------------------------------
277 //------------------------------------------------------------------------------
278 void vvSlicer::SetCornerAnnotationVisibility(bool s)
280 ca->SetVisibility(s);
282 //------------------------------------------------------------------------------
285 //------------------------------------------------------------------------------
286 bool vvSlicer::GetCornerAnnotationVisibility()
288 return ca->GetVisibility();
290 //------------------------------------------------------------------------------
293 //------------------------------------------------------------------------------
294 vvSlicer::~vvSlicer()
296 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
297 i!=mSurfaceCutActors.end(); i++)
299 delete [] mReducedExtent;
301 //------------------------------------------------------------------------------
303 //------------------------------------------------------------------------------
304 double* vvSlicer::GetCurrentPosition()
306 return mCurrentBeforeSlicingTransform;
308 //------------------------------------------------------------------------------
310 //------------------------------------------------------------------------------
311 void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
313 mCurrentBeforeSlicingTransform[0]=x;
314 mCurrentBeforeSlicingTransform[1]=y;
315 mCurrentBeforeSlicingTransform[2]=z;
316 mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
317 if (t>=0) SetTSlice(t);
319 //------------------------------------------------------------------------------
322 //------------------------------------------------------------------------------
323 void vvSlicer::SetImage(vvImage::Pointer image)
325 if (image->GetVTKImages().size()) {
328 if (!mImageReslice) {
329 mImageReslice = vtkSmartPointer<vtkImageReslice>::New();
330 mImageReslice->SetInterpolationModeToLinear();
331 mImageReslice->AutoCropOutputOn();
332 mImageReslice->SetBackgroundColor(-1000,-1000,-1000,1);
335 mConcatenatedTransform->Identity();
336 mConcatenatedTransform->Concatenate(mImage->GetTransform()[0]);
337 mConcatenatedTransform->Concatenate(mSlicingTransform);
338 mImageReslice->SetResliceTransform(mConcatenatedTransform);
339 mImageReslice->SetInput(0, mImage->GetFirstVTKImageData());
340 mImageReslice->UpdateInformation();
342 this->Superclass::SetInput(mImageReslice->GetOutput());
345 this->GetInput()->GetWholeExtent(extent);
347 // Prevent crash when reload -> change slice if outside extent
348 if (Slice < extent[SliceOrientation*2] || Slice>=extent[SliceOrientation*2+1]) {
349 Slice = (extent[SliceOrientation*2+1]+extent[SliceOrientation*2])/2.0;
352 // Make sure that the required part image has been computed
353 extent[SliceOrientation*2] = Slice;
354 extent[SliceOrientation*2+1] = Slice;
355 mImageReslice->GetOutput()->SetUpdateExtent(extent);
356 mImageReslice->GetOutput()->Update();
358 this->UpdateDisplayExtent();
361 ca->SetText(0,mFileName.c_str());
364 //------------------------------------------------------------------------------
367 //------------------------------------------------------------------------------
368 void vvSlicer::SetOverlay(vvImage::Pointer overlay)
370 if (overlay->GetVTKImages().size()) {
372 mOverlayVisibility = true;
374 if (!mOverlayReslice) {
375 mOverlayReslice = vtkSmartPointer<vtkImageReslice>::New();
376 mOverlayReslice->SetInterpolationModeToLinear();
377 mOverlayReslice->AutoCropOutputOn();
378 mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
381 mConcatenatedOverlayTransform->Identity();
382 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
383 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
384 mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
385 mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
386 mImageReslice->UpdateInformation();
389 mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
390 mOverlayMapper->SetInput(mOverlayReslice->GetOutput());
392 if (!mOverlayActor) {
393 mOverlayActor = vtkSmartPointer<vvBlendImageActor>::New();
394 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
395 mOverlayActor->SetPickable(0);
396 mOverlayActor->SetVisibility(true);
397 mOverlayActor->SetOpacity(0.5);
398 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
399 mOverlayActor->GetMapper()->BorderOn();
403 //stupid but necessary : the Overlay need to be rendered before fusion
405 this->GetRenderer()->RemoveActor(mFusionActor);
406 this->GetRenderer()->AddActor(mOverlayActor);
407 this->GetRenderer()->AddActor(mFusionActor);
409 this->GetRenderer()->AddActor(mOverlayActor);
411 //Synchronize orientation and slice
412 AdjustResliceToSliceOrientation(mOverlayReslice);
413 this->UpdateDisplayExtent();
414 this->SetTSlice(mCurrentTSlice);
417 //------------------------------------------------------------------------------
420 //------------------------------------------------------------------------------
421 void vvSlicer::SetFusion(vvImage::Pointer fusion, int fusionSequenceCode)
423 mFusionSequenceCode = fusionSequenceCode;
424 if (fusion->GetVTKImages().size()) {
426 mFusionVisibility = true;
428 if (!mFusionReslice) {
429 mFusionReslice = vtkSmartPointer<vtkImageReslice>::New();
430 mFusionReslice->SetInterpolationModeToLinear();
431 mFusionReslice->AutoCropOutputOn();
432 mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
435 mConcatenatedFusionTransform->Identity();
436 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
437 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
438 mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
439 mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
440 mFusionReslice->UpdateInformation();
443 mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
445 vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
447 lut->SetValueRange(0, 1);
448 lut->SetSaturationRange(0, 0);
450 mFusionMapper->SetLookupTable(lut);
451 mFusionMapper->SetInput(mFusionReslice->GetOutput());
454 mFusionActor = vtkSmartPointer<vtkImageActor>::New();
455 mFusionActor->SetInput(mFusionMapper->GetOutput());
456 mFusionActor->SetPickable(0);
457 mFusionActor->SetVisibility(true);
458 mFusionActor->SetOpacity(0.7);
459 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
460 mFusionActor->GetMapper()->BorderOn();
462 this->GetRenderer()->AddActor(mFusionActor);
465 //Synchronize orientation and slice
466 AdjustResliceToSliceOrientation(mFusionReslice);
467 this->UpdateDisplayExtent();
468 this->SetTSlice(mCurrentTSlice);
471 //------------------------------------------------------------------------------
474 //------------------------------------------------------------------------------
475 bool vvSlicer::GetActorVisibility(const std::string& actor_type, int overlay_index)
478 if (actor_type == "image")
479 vis = mImageVisibility;
480 else if (actor_type == "vector")
482 else if (actor_type == "overlay")
483 vis = mOverlayVisibility;
484 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
485 vis = mFusionVisibility;
486 else if (actor_type == "contour")
487 vis = this->mSurfaceCutActors[overlay_index]->GetActor()->GetVisibility();
490 //------------------------------------------------------------------------------
492 //------------------------------------------------------------------------------
493 void vvSlicer::SetActorVisibility(const std::string& actor_type, int overlay_index ,bool vis)
495 if (actor_type == "image")
496 mImageVisibility = vis;
497 else if (actor_type == "vector")
499 else if (actor_type == "overlay")
500 mOverlayVisibility = vis;
501 else if ( (actor_type == "fusion") || (actor_type == "fusionSequence") )
502 mFusionVisibility = vis;
503 else if (actor_type == "contour")
504 this->mSurfaceCutActors[overlay_index]->GetActor()->SetVisibility(vis);
505 UpdateDisplayExtent();
507 //------------------------------------------------------------------------------
509 //------------------------------------------------------------------------------
510 void vvSlicer::SetVF(vvImage::Pointer vf)
512 if (vf->GetVTKImages().size()) {
514 mVFVisibility = true;
517 mAAFilter= vtkSmartPointer<vtkAssignAttribute>::New();
518 mVOIFilter = vtkSmartPointer<vtkExtractVOI>::New();
519 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
521 mVOIFilter->SetInput(vf->GetFirstVTKImageData());
522 mAAFilter->SetInput(mVOIFilter->GetOutput());
523 ///This tells VTK to use the scalar (pixel) data of the image to draw the little arrows
524 mAAFilter->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA);
527 mArrow = vtkSmartPointer<vvGlyphSource>::New();
528 mArrow->SetGlyphTypeToSpecificArrow();
529 mArrow->SetScale(mScale);
532 // Glyph the gradient vector (with arrows)
534 mGlyphFilter = vtkSmartPointer<vvGlyph2D>::New();
535 mGlyphFilter->SetInput(mAAFilter->GetOutput());
536 mGlyphFilter->SetSource(mArrow->GetOutput());
537 mGlyphFilter->ScalingOn();
538 mGlyphFilter->SetScaleModeToScaleByVector();
539 mGlyphFilter->OrientOn();
540 mGlyphFilter->SetVectorModeToUseVector();
541 mGlyphFilter->SetColorModeToColorByVector();
544 mVFColorLUT = vtkSmartPointer<vtkLookupTable>::New();
546 double mVFColorHSV[3];
547 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
548 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
549 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
550 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);
553 mVFMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
554 mVFMapper->SetInput(mGlyphFilter->GetOutput());
555 mVFMapper->ImmediateModeRenderingOn();
556 mVFMapper->SetLookupTable(mVFColorLUT);
559 mVFActor = vtkSmartPointer<vtkActor>::New();
560 mVFActor->SetMapper(mVFMapper);
561 mVFActor->SetPickable(0);
562 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
563 this->UpdateDisplayExtent();
564 this->GetRenderer()->AddActor(mVFActor);
567 SetTSlice(mCurrentTSlice);
570 //------------------------------------------------------------------------------
573 //------------------------------------------------------------------------------
574 void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
576 mLandmarks = landmarks;
580 mCross = vtkSmartPointer<vtkCursor3D>::New();
582 mClipBox = vtkSmartPointer<vtkBox>::New();
584 mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
586 mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
588 mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
590 mLandActor = vtkSmartPointer<vtkActor>::New();
592 mCross->SetFocalPoint(0.0,0.0,0.0);
593 mCross->SetModelBounds(-10,10,-10,10,-10,10);
597 mLandClipper->SetClipFunction(mClipBox);
598 mLandClipper->InsideOutOn();
599 mLandClipper->SetInput(mLandmarks->GetOutput());
601 mLandGlyph->SetSource(mCross->GetOutput());
602 mLandGlyph->SetInput(mLandClipper->GetOutput());
603 //mLandGlyph->SetIndexModeToScalar();
604 //mLandGlyph->SetRange(0,1);
605 //mLandGlyph->ScalingOff();
607 //mLandGlyph->SetColorModeToColorByScalar();
609 mLandGlyph->SetScaleModeToDataScalingOff();
610 mLandGlyph->SetIndexModeToOff();
612 mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
613 //mLandMapper->ScalarVisibilityOff();
615 mLandActor->SetMapper(mLandMapper);
616 mLandActor->GetProperty()->SetColor(255,10,212);
617 mLandActor->SetPickable(0);
618 mLandActor->SetVisibility(true);
619 this->UpdateDisplayExtent();
620 this->GetRenderer()->AddActor(mLandActor);
623 //------------------------------------------------------------------------------
625 //------------------------------------------------------------------------------
626 //FIXME: this function leaks memory, we should fix it someday :)
627 void vvSlicer::RemoveActor(const std::string& actor_type, int overlay_index)
629 if (actor_type == "vector") {
630 Renderer->RemoveActor(mVFActor);
639 if (actor_type == "overlay") {
640 Renderer->RemoveActor(mOverlayActor);
642 mOverlayActor = NULL;
643 mOverlayMapper = NULL;
645 if ( (actor_type == "fusion") || (actor_type == "fusionSequence") ) {
646 Renderer->RemoveActor(mFusionActor);
649 mFusionMapper = NULL;
651 if (actor_type == "contour") {
652 Renderer->RemoveActor(this->mSurfaceCutActors[overlay_index]->GetActor());
653 mSurfaceCutActors.erase(mSurfaceCutActors.begin()+overlay_index);
656 //------------------------------------------------------------------------------
659 //------------------------------------------------------------------------------
660 void vvSlicer::SetVFSubSampling(int sub)
663 mVOIFilter->SetSampleRate(mSubSampling,mSubSampling,mSubSampling);
666 UpdateDisplayExtent();
669 //------------------------------------------------------------------------------
672 //------------------------------------------------------------------------------
673 void vvSlicer::SetVFScale(int scale)
677 mArrow->SetScale(mScale);
678 UpdateDisplayExtent();
681 //------------------------------------------------------------------------------
683 //------------------------------------------------------------------------------
684 void vvSlicer::SetVFWidth(int width)
688 mVFActor->GetProperty()->SetLineWidth(mVFWidth);
689 UpdateDisplayExtent();
692 //------------------------------------------------------------------------------
695 //------------------------------------------------------------------------------
696 void vvSlicer::SetVFLog(int log)
700 mGlyphFilter->SetUseLog(mVFLog);
701 mGlyphFilter->Modified();
703 UpdateDisplayExtent();
706 //------------------------------------------------------------------------------
709 //------------------------------------------------------------------------------
710 void vvSlicer::SetTSlice(int t, bool updateLinkedImages)
712 if (!updateLinkedImages) {
714 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
716 mConcatenatedTransform->Identity();
717 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
718 mConcatenatedTransform->Concatenate(mSlicingTransform);
719 UpdateDisplayExtent();
725 else if ((unsigned int)t >= mImage->GetVTKImages().size())
726 mCurrentTSlice = mImage->GetVTKImages().size() -1;
731 mConcatenatedTransform->Identity();
732 mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
733 mConcatenatedTransform->Concatenate(mSlicingTransform);
736 mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
737 if (mVF && mVFActor->GetVisibility()) {
738 if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
739 mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
742 if (mOverlay && mOverlayActor->GetVisibility()) {
743 if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
744 mCurrentOverlayTSlice = t;
745 mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
747 // Update overlay transform
748 mConcatenatedOverlayTransform->Identity();
749 mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
750 mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
753 //update the fusion ; except in case this is a fusionSequence, in which case both 'times' should be independent.
754 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode<0)) {
755 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
756 mCurrentFusionTSlice = t;
757 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
759 // Update fusion transform
760 mConcatenatedFusionTransform->Identity();
761 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
762 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
765 if (mSurfaceCutActors.size() > 0)
766 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
767 i!=mSurfaceCutActors.end(); i++)
768 (*i)->SetTimeSlice(mCurrentTSlice);
769 UpdateDisplayExtent();
771 //------------------------------------------------------------------------------
774 //------------------------------------------------------------------------------
775 void vvSlicer::SetFusionSequenceTSlice(int t)
777 if (mFusion && mFusionActor->GetVisibility() && (mFusionSequenceCode>=0)) {
778 if (mFusion->GetVTKImages().size() > (unsigned int)t) {
779 mCurrentFusionTSlice = t;
780 mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice] );
781 // Update fusion transform
782 mConcatenatedFusionTransform->Identity();
783 mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]); //not really useful...
784 mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
788 UpdateDisplayExtent();
790 //------------------------------------------------------------------------------
793 //------------------------------------------------------------------------------
794 int vvSlicer::GetTSlice()
796 return mCurrentTSlice;
798 //------------------------------------------------------------------------------
800 //------------------------------------------------------------------------------
801 int vvSlicer::GetMaxCurrentTSlice()
803 int t = mCurrentTSlice;
805 t = std::max(t, mCurrentOverlayTSlice);
806 if(mFusion&& (mFusionSequenceCode<0)) //ignore fusionSequence data: for these, the times are not to be related (this way)
807 t = std::max(t, mCurrentFusionTSlice);
810 //------------------------------------------------------------------------------
812 //------------------------------------------------------------------------------
813 int vvSlicer::GetFusionTSlice()
815 return mCurrentFusionTSlice;
817 //------------------------------------------------------------------------------
819 //------------------------------------------------------------------------------
820 int vvSlicer::GetOverlayTSlice()
822 return mCurrentOverlayTSlice;
824 //------------------------------------------------------------------------------
826 //------------------------------------------------------------------------------
827 void vvSlicer::SetSliceOrientation(int orientation)
829 //if 2D image, force to watch in Axial View
831 this->GetInput()->GetWholeExtent(extent);
832 if (extent[5]-extent[4] <= 2)
833 orientation = vtkImageViewer2::SLICE_ORIENTATION_XY;
835 if (orientation < vtkImageViewer2::SLICE_ORIENTATION_YZ ||
836 orientation > vtkImageViewer2::SLICE_ORIENTATION_XY) {
837 vtkErrorMacro("Error - invalid slice orientation " << orientation);
841 this->SliceOrientation = orientation;
844 AdjustResliceToSliceOrientation(mFusionReslice);
847 AdjustResliceToSliceOrientation(mOverlayReslice);
851 // Go to current cursor position
852 // double* cursorPos = GetCursorPosition();
853 // DDV(cursorPos, 3);
854 // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
856 if (this->Renderer && this->GetInput()) {
857 double s = mCursor[orientation];
858 double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
859 this->Slice = static_cast<int>(sCursor);
862 // int *range = this->GetSliceRange();
864 // this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
866 this->UpdateOrientation();
867 this->UpdateDisplayExtent();
869 if (this->Renderer && this->GetInput()) {
870 double scale = this->Renderer->GetActiveCamera()->GetParallelScale();
871 this->Renderer->ResetCamera();
872 this->Renderer->GetActiveCamera()->SetParallelScale(scale);
877 //----------------------------------------------------------------------------
879 //------------------------------------------------------------------------------
880 // This function ensures that we sample the slices of a vtkImageReslice filter
881 // in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
882 // In other words, we change the grid of the reslice in the same way as the grid
883 // of the displayed image in the slicing direction.
884 void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
886 // Reset autocrop and update output information
887 reslice->SetOutputOriginToDefault();
888 reslice->SetOutputSpacingToDefault();
889 reslice->GetOutput()->UpdateInformation();
891 // Ge new origin / spacing
894 reslice->GetOutput()->GetOrigin(origin);
895 reslice->GetOutput()->GetSpacing(spacing);
897 // Use similar spacing as the image in the direction SliceOrientation
898 spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
900 // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
901 // Step 1: from world coordinates to image coordinates
902 origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
903 origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
905 // Step 2: round to nearest grid positionInc. This has been validated as the only
906 // way to have something consistent with the thickness of a 2D slice visible on the
907 // other slices. The thickness is accounted for so if the 2D slice is to thin and
908 // between two slices, one will never be able to see this 2D slice (bug #1883).
909 origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
911 // Step 3: back to world coordinates
912 origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
913 origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
915 // Set new spacing and origin
916 reslice->SetOutputOrigin(origin);
917 reslice->SetOutputSpacing(spacing);
918 reslice->UpdateInformation();
919 reslice->GetOutput()->UpdateInformation();
921 //------------------------------------------------------------------------------
923 //----------------------------------------------------------------------------
924 int * vvSlicer::GetExtent(){
926 if (mUseReducedExtent) {
927 w_ext = mReducedExtent;
928 } else w_ext = GetInput()->GetWholeExtent();
931 //----------------------------------------------------------------------------
934 //----------------------------------------------------------------------------
935 int vvSlicer::GetOrientation()
937 return this->SliceOrientation;
939 //----------------------------------------------------------------------------
942 //----------------------------------------------------------------------------
943 void vvSlicer::UpdateDisplayExtent()
945 vtkImageData *input = this->GetInput();
946 if (!input || !this->ImageActor) {
949 input->UpdateInformation();
950 this->SetSlice( this->GetSlice() ); //SR: make sure the update let the slice in extents
952 // Local copy of extent
954 int* ext = GetExtent();
955 copyExtent(ext, w_ext);
957 w_ext[ this->SliceOrientation*2 ] = this->Slice;
958 w_ext[ this->SliceOrientation*2+1 ] = this->Slice;
961 this->ImageActor->SetVisibility(mImageVisibility);
962 this->ImageActor->SetDisplayExtent(w_ext);
963 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
965 dynamic_cast<vtkImageSliceMapper *>(this->ImageActor->GetMapper())->SetOrientation(this->GetOrientation());
968 // Overlay image actor
969 if (mOverlay && mOverlayVisibility) {
970 AdjustResliceToSliceOrientation(mOverlayReslice);
972 this->ConvertImageToImageDisplayExtent(input, w_ext, mOverlayReslice->GetOutput(), overExtent);
973 bool out = ClipDisplayedExtent(overExtent, mOverlayMapper->GetInput()->GetWholeExtent());
974 mOverlayActor->SetVisibility(!out);
975 mOverlayActor->SetDisplayExtent( overExtent );
976 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
978 dynamic_cast<vtkImageSliceMapper *>(mOverlayActor->GetMapper())->SetOrientation(this->GetOrientation());
982 mOverlayActor->SetVisibility(false);
984 // Fusion image actor
985 if (mFusion && mFusionVisibility) {
986 AdjustResliceToSliceOrientation(mFusionReslice);
988 this->ConvertImageToImageDisplayExtent(input, w_ext, mFusionReslice->GetOutput(), fusExtent);
989 bool out = ClipDisplayedExtent(fusExtent, mFusionMapper->GetInput()->GetWholeExtent());
990 mFusionActor->SetVisibility(!out);
991 mFusionActor->SetDisplayExtent( fusExtent );
992 #if VTK_MAJOR_VERSION >= 6 || (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 10)
994 dynamic_cast<vtkImageSliceMapper *>(mFusionActor->GetMapper())->SetOrientation(this->GetOrientation());
998 mFusionActor->SetVisibility(false);
1000 // Vector field actor
1001 double* camera = Renderer->GetActiveCamera()->GetPosition();
1002 double* image_bounds = ImageActor->GetBounds();
1003 double position[3] = {0, 0, 0};
1004 position[this->SliceOrientation] = image_bounds[this->SliceOrientation*2];
1006 //print_vector<double, 6>("camera", camera);
1007 //print_vector<double, 6>("image_bounds", image_bounds);
1008 //print_vector<double, 3>("position", position);
1010 // find where to place the VF actor. to deal with
1011 // z-buffer issues, the VF is placed right in front of the image,
1012 // subject to a small offset. the position actually depends on the
1013 // the location of the camera relative to the image.
1015 if (camera[this->SliceOrientation] < image_bounds[this->SliceOrientation*2])
1018 if (mVF && mVFVisibility) {
1020 mVF->GetVTKImages()[0]->UpdateInformation();
1021 this->ConvertImageToImageDisplayExtent(input, w_ext, mVF->GetVTKImages()[0], vfExtent);
1022 bool out = ClipDisplayedExtent(vfExtent, mVOIFilter->GetInput()->GetWholeExtent());
1023 mVFActor->SetVisibility(!out);
1024 mVOIFilter->SetVOI(vfExtent);
1025 int orientation[3] = {1,1,1};
1026 orientation[this->SliceOrientation] = 0;
1027 mGlyphFilter->SetOrientation(orientation[0], orientation[1], orientation[2]);
1028 mVFMapper->Update();
1030 position[this->SliceOrientation] += offset;
1031 mVFActor->SetPosition(position);
1034 mVFActor->SetVisibility(false);
1040 for(unsigned int i=0; i<6; i++)
1041 bounds[i] = ImageActor->GetBounds()[i];
1042 bounds[ this->SliceOrientation*2 ] = ImageActor->GetBounds()[ this->SliceOrientation*2 ]-fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1043 bounds[ this->SliceOrientation*2+1 ] = ImageActor->GetBounds()[ this->SliceOrientation*2+1 ]+fabs(this->GetInput()->GetSpacing()[this->SliceOrientation]);
1044 mClipBox->SetBounds(bounds);
1048 position[this->SliceOrientation] = offset;
1049 mLandActor->SetPosition(position);
1052 // Figure out the correct clipping range
1053 if (this->Renderer) {
1054 if (this->InteractorStyle &&
1055 this->InteractorStyle->GetAutoAdjustCameraClippingRange()) {
1056 this->Renderer->ResetCameraClippingRange();
1058 vtkCamera *cam = this->Renderer->GetActiveCamera();
1061 this->ImageActor->GetBounds(bounds);
1062 double spos = (double)bounds[this->SliceOrientation * 2];
1063 double cpos = (double)cam->GetPosition()[this->SliceOrientation];
1064 double range = fabs(spos - cpos);
1065 double *spacing = input->GetSpacing();
1066 double sumSpacing = spacing[0] + spacing[1] + spacing[2];
1067 cam->SetClippingRange(range - sumSpacing, range + sumSpacing);
1072 //----------------------------------------------------------------------------
1074 //----------------------------------------------------------------------------
1075 void vvSlicer::ConvertImageToImageDisplayExtent(vtkImageData *sourceImage, const int sourceExtent[6],
1076 vtkImageData *targetImage, int targetExtent[6])
1079 for(unsigned int i=0; i<6; i++) {
1080 // From source voxel coordinates to world coordinates
1081 dExtents[i] = sourceImage->GetOrigin()[i/2] + sourceImage->GetSpacing()[i/2] * sourceExtent[i];
1083 // From world coordinates to floating point target voxel coordinates
1084 dExtents[i] = (dExtents[i]- targetImage->GetOrigin()[i/2]) / targetImage->GetSpacing()[i/2];
1086 // Round to current slice or larger extent
1087 if(i/2==this->GetOrientation())
1088 targetExtent[i] = itk::Math::Round<double>(dExtents[i]);
1090 targetExtent[i] = itk::Math::Ceil<double>(dExtents[i]);
1092 targetExtent[i] = itk::Math::Floor<double>(dExtents[i]);
1095 //----------------------------------------------------------------------------
1097 //----------------------------------------------------------------------------
1098 bool vvSlicer::ClipDisplayedExtent(int extent[6], int refExtent[6])
1103 for (int i = 0; i < maxBound; i = i+2) {
1104 //if we are totally outside the image
1105 if ( extent[i] > refExtent[i+1] || extent[i+1] < refExtent[i] ) {
1109 //crop to the limit of the image
1110 extent[i] = std::max(extent[i], refExtent[i]);
1111 extent[i] = std::min(extent[i], refExtent[i+1]);;
1112 extent[i+1] = std::max(extent[i+1], refExtent[i]);
1113 extent[i+1] = std::min(extent[i+1], refExtent[i+1]);;
1116 for (int i = 0; i < maxBound; i = i+2) {
1117 extent[i] = refExtent[i];
1118 extent[i+1] = refExtent[i];
1122 //----------------------------------------------------------------------------
1125 //----------------------------------------------------------------------------
1126 void vvSlicer::UpdateOrientation()
1128 // Set the camera position
1129 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1131 switch (this->SliceOrientation) {
1132 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1133 cam->SetFocalPoint(0,0,0);
1134 cam->SetPosition(0,0,-1); // -1 if medical ?
1135 cam->SetViewUp(0,-1,0);
1138 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1139 cam->SetFocalPoint(0,0,0);
1140 cam->SetPosition(0,-1,0); // 1 if medical ?
1141 cam->SetViewUp(0,0,1);
1144 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1145 cam->SetFocalPoint(0,0,0);
1146 cam->SetPosition(-1,0,0); // -1 if medical ?
1147 cam->SetViewUp(0,0,1);
1152 //----------------------------------------------------------------------------
1155 //----------------------------------------------------------------------------
1156 void vvSlicer::SetOpacity(double s)
1158 this->GetImageActor()->SetOpacity(s);
1160 //----------------------------------------------------------------------------
1163 //----------------------------------------------------------------------------
1164 void vvSlicer::SetRenderWindow(int orientation, vtkRenderWindow * rw)
1166 this->Superclass::SetRenderWindow(rw);
1167 this->SetupInteractor(rw->GetInteractor());
1168 ca->SetImageActor(this->GetImageActor());
1169 ca->SetWindowLevel(this->GetWindowLevel());
1170 ca->SetText(2, "<slice>");
1171 ca->SetText(3, "<window>\n<level>");
1183 crossCursor->SetModelBounds(bounds);
1184 this->GetRenderer()->AddActor(pdmA);
1185 this->GetRenderer()->AddActor(ca);
1186 this->GetRenderer()->ResetCamera();
1188 //this is just a mapping between the labeling of the orientations presented to the user and
1189 //the one used by vtk
1190 SetSliceOrientation(2-(orientation%3));
1193 //----------------------------------------------------------------------------
1196 //----------------------------------------------------------------------------
1197 void vvSlicer::ResetCamera()
1199 this->GetRenderer()->ResetCamera();
1201 //----------------------------------------------------------------------------
1204 //----------------------------------------------------------------------------
1205 void vvSlicer::SetDisplayMode(bool i)
1207 this->GetRenderer()->SetDraw(i);
1208 if (i) UpdateDisplayExtent();
1210 //----------------------------------------------------------------------------
1213 //----------------------------------------------------------------------------
1214 void vvSlicer::FlipHorizontalView()
1216 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1218 double *position = cam->GetPosition();
1219 double factor[3] = {1, 1, 1};
1220 factor[this->SliceOrientation] = -1;
1221 cam->SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2]);
1223 /* switch (this->SliceOrientation) {
1224 case vtkImageViewer2::SLICE_ORIENTATION_XY:
1225 cam->SetPosition(position[0],position[1],-position[2]);
1228 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
1229 cam->SetPosition(position[0],-position[1],position[2]);
1232 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
1233 cam->SetPosition(-position[0],position[1],position[2]);
1237 this->Renderer->ResetCameraClippingRange();
1238 this->UpdateDisplayExtent();
1241 //----------------------------------------------------------------------------
1244 //----------------------------------------------------------------------------
1245 void vvSlicer::FlipVerticalView()
1247 vtkCamera *cam = this->Renderer ? this->Renderer->GetActiveCamera() : NULL;
1249 FlipHorizontalView();
1250 double *viewup = cam->GetViewUp();
1251 cam->SetViewUp(-viewup[0],-viewup[1],-viewup[2]);
1252 this->UpdateDisplayExtent();
1255 //----------------------------------------------------------------------------
1258 //----------------------------------------------------------------------------
1259 void vvSlicer::SetColorWindow(double window)
1261 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1263 double level = this->GetWindowLevel()->GetLevel();
1264 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1267 this->vtkImageViewer2::SetColorWindow(window);
1269 //----------------------------------------------------------------------------
1271 //----------------------------------------------------------------------------
1272 void vvSlicer::SetColorLevel(double level)
1274 vtkLookupTable* LUT = static_cast<vtkLookupTable*>(this->GetWindowLevel()->GetLookupTable());
1276 double window = this->GetWindowLevel()->GetWindow();
1277 LUT->SetTableRange(level-fabs(window)/2,level+fabs(window)/2);
1280 this->vtkImageViewer2::SetColorLevel(level);
1282 //----------------------------------------------------------------------------
1284 //----------------------------------------------------------------------------
1285 double vvSlicer::GetOverlayColorWindow()
1288 return mOverlayMapper->GetWindow();
1292 //----------------------------------------------------------------------------
1294 //----------------------------------------------------------------------------
1295 double vvSlicer::GetOverlayColorLevel()
1298 return mOverlayMapper->GetLevel();
1302 //----------------------------------------------------------------------------
1304 //----------------------------------------------------------------------------
1305 void vvSlicer::SetOverlayColorWindow(double window)
1307 mOverlayMapper->SetWindow(window);
1309 //----------------------------------------------------------------------------
1311 //----------------------------------------------------------------------------
1312 void vvSlicer::SetOverlayColorLevel(double level)
1314 mOverlayMapper->SetLevel(level);
1316 //----------------------------------------------------------------------------
1318 //----------------------------------------------------------------------------
1319 // Returns the min an the max value in a 20%x20% region around the mouse pointer
1320 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
1322 //Get mouse pointer position in view coordinates
1325 for(int i=0; i<3; i++) {
1326 corner1[i] = mCurrent[i];
1327 corner2[i] = mCurrent[i];
1330 this->Renderer->WorldToView(corner1[0], corner1[1], corner1[2]);
1331 this->Renderer->WorldToView(corner2[0], corner2[1], corner2[2]);
1333 // In view coordinates, x is the slicer width and y is the slicer height are the in-plane axis
1335 this->Renderer->GetTiledSize(&w, &h);
1336 corner1[0] -= 0.2*h/(double)w;
1337 corner2[0] += 0.2*h/(double)w;
1340 this->Renderer->ViewToWorld(corner1[0], corner1[1], corner1[2]);
1341 this->Renderer->ViewToWorld(corner2[0], corner2[1], corner2[2]);
1343 //Convert to image pixel coordinates (rounded)
1344 transform->TransformPoint(corner1, corner1);
1345 transform->TransformPoint(corner2, corner2);
1346 int iLocalExtents[6];
1347 for(int i=0; i<3; i++) {
1348 corner1[i] = (corner1[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1349 corner2[i] = (corner2[i] - image->GetOrigin()[i])/image->GetSpacing()[i];
1351 iLocalExtents[i*2 ] = lrint(corner1[i]);
1352 iLocalExtents[i*2+1] = lrint(corner2[i]);
1354 if(iLocalExtents[i*2 ]>iLocalExtents[i*2+1])
1355 std::swap(iLocalExtents[i*2], iLocalExtents[i*2+1]);
1358 vtkSmartPointer<vtkExtractVOI> voiFilter = vtkSmartPointer<vtkExtractVOI>::New();
1359 voiFilter->SetInput(image);
1360 voiFilter->SetVOI(iLocalExtents);
1361 voiFilter->Update();
1362 if (!voiFilter->GetOutput()->GetNumberOfPoints()) {
1368 vtkSmartPointer<vtkImageAccumulate> accFilter = vtkSmartPointer<vtkImageAccumulate>::New();
1369 accFilter->SetInput(voiFilter->GetOutput());
1370 accFilter->Update();
1372 min = *(accFilter->GetMin());
1373 max = *(accFilter->GetMax());
1375 //----------------------------------------------------------------------------
1377 //----------------------------------------------------------------------------
1378 double vvSlicer::GetScalarComponentAsDouble(vtkImageData *image, double X, double Y, double Z, int &ix, int &iy, int &iz, int component)
1384 if (ix < image->GetWholeExtent()[0] ||
1385 ix > image->GetWholeExtent()[1] ||
1386 iy < image->GetWholeExtent()[2] ||
1387 iy > image->GetWholeExtent()[3] ||
1388 iz < image->GetWholeExtent()[4] ||
1389 iz > image->GetWholeExtent()[5] )
1390 return std::numeric_limits<double>::quiet_NaN();
1392 image->SetUpdateExtent(ix, ix, iy, iy, iz, iz);
1394 return image->GetScalarComponentAsDouble(ix, iy, iz, component);
1396 //----------------------------------------------------------------------------
1398 //----------------------------------------------------------------------------
1399 void vvSlicer::Render()
1401 if (this->mFusion && mFusionActor->GetVisibility() && showFusionLegend) {
1402 legend->SetLookupTable(this->GetFusionMapper()->GetLookupTable());
1403 legend->UseOpacityOn();
1404 legend->SetVisibility(1);
1406 else if (this->GetWindowLevel()->GetLookupTable() && !this->mOverlay) {
1407 legend->SetLookupTable(this->GetWindowLevel()->GetLookupTable());
1408 legend->UseOpacityOff();
1409 legend->SetVisibility(1);
1410 } else legend->SetVisibility(0);
1412 if (ca->GetVisibility()) {
1413 std::stringstream worldPos;
1415 mConcatenatedTransform->TransformPoint(mCurrent, pt);
1416 double X = (pt[0] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[0])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[0];
1417 double Y = (pt[1] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[1])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[1];
1418 double Z = (pt[2] - mImage->GetVTKImages()[mCurrentTSlice]->GetOrigin()[2])/mImage->GetVTKImages()[mCurrentTSlice]->GetSpacing()[2];
1420 if (X >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[0]-0.5 &&
1421 X <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[1]+0.5 &&
1422 Y >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[2]-0.5 &&
1423 Y <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[3]+0.5 &&
1424 Z >= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[4]-0.5 &&
1425 Z <= mImage->GetVTKImages()[mCurrentTSlice]->GetWholeExtent()[5]+0.5) {
1429 double value = this->GetScalarComponentAsDouble(mImage->GetVTKImages()[mCurrentTSlice], X, Y, Z, ix, iy, iz);
1431 if(ImageActor->GetVisibility())
1432 worldPos << "data value : " << value << std::endl;
1434 worldPos << "mm : " << lrint(mCurrentBeforeSlicingTransform[0]) << ' '
1435 << lrint(mCurrentBeforeSlicingTransform[1]) << ' '
1436 << lrint(mCurrentBeforeSlicingTransform[2]) << ' '
1439 worldPos << "pixel : " << ix << ' '
1445 ca->SetText(1,worldPos.str().c_str());
1448 if (pdmA->GetVisibility()) {
1449 double x = mCursor[0];
1450 double y = mCursor[1];
1451 double z = mCursor[2];
1452 double xCursor = (x - this->GetInput()->GetOrigin()[0])/this->GetInput()->GetSpacing()[0];
1453 double yCursor = (y - this->GetInput()->GetOrigin()[1])/this->GetInput()->GetSpacing()[1];
1454 double zCursor = (z - this->GetInput()->GetOrigin()[2])/this->GetInput()->GetSpacing()[2];
1456 if (xCursor >= this->GetImageActor()->GetDisplayExtent()[0]-0.5 &&
1457 xCursor < this->GetImageActor()->GetDisplayExtent()[1]+0.5 &&
1458 yCursor >= this->GetImageActor()->GetDisplayExtent()[2]-0.5 &&
1459 yCursor < this->GetImageActor()->GetDisplayExtent()[3]+0.5 &&
1460 zCursor >= this->GetImageActor()->GetDisplayExtent()[4]-0.5 &&
1461 zCursor < this->GetImageActor()->GetDisplayExtent()[5]+0.5 ) {
1462 vtkRenderer * renderer = this->Renderer;
1464 renderer->WorldToView(x,y,z);
1465 renderer->ViewToNormalizedViewport(x,y,z);
1466 renderer->NormalizedViewportToViewport(x,y);
1467 renderer->ViewportToNormalizedDisplay(x,y);
1468 renderer->NormalizedDisplayToDisplay(x,y);
1469 crossCursor->SetFocalPoint(x,y,z);
1471 crossCursor->SetFocalPoint(-1,-1,z);
1475 if (mOverlay && mOverlayActor->GetVisibility()) {
1476 if(mLinkOverlayWindowLevel) {
1477 mOverlayMapper->SetWindow(this->GetColorWindow());
1478 mOverlayMapper->SetLevel(this->GetColorLevel());
1480 mOverlayMapper->GetOutput()->SetUpdateExtent(mOverlayActor->GetDisplayExtent());
1481 mOverlayMapper->GetOutput()->Update();
1482 mOverlayMapper->Update();
1487 this->GetRenderWindow()->Render();
1489 //----------------------------------------------------------------------------
1492 //----------------------------------------------------------------------------
1493 void vvSlicer::UpdateCursorPosition()
1495 pdmA->SetVisibility(true);
1496 mCursor[0] = mCurrent[0];
1497 mCursor[1] = mCurrent[1];
1498 mCursor[2] = mCurrent[2];
1499 mCursor[3] = mCurrentTSlice;
1501 //----------------------------------------------------------------------------
1504 //----------------------------------------------------------------------------
1505 void vvSlicer::UpdateLandmarks()
1507 vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
1508 if (pd->GetPoints()) {
1509 //mLandGlyph->SetRange(0,1);
1510 //mLandGlyph->Modified();
1511 //mLandGlyph->Update();
1513 mClipBox->Modified();
1514 mLandClipper->Update();
1515 mLandMapper->Update();
1516 //Let's add the captions
1517 //First remove all captions:
1518 for(unsigned int i=0;i<mLandLabelActors.size();i++) {
1519 this->Renderer->RemoveActor2D(mLandLabelActors[i]);
1520 //allActors2D->Remove (mLandLabelActors[i]);
1522 mLandLabelActors.clear();
1523 //Next add the captions to the displayed points
1524 for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
1525 double *position = mLandClipper->GetOutput()->GetPoint(id);
1526 vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
1527 vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
1528 label_actor->SetCaption(label);
1529 label_actor->SetAttachmentPoint(position);
1530 label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
1531 label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
1532 label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
1533 label_actor->GetCaptionTextProperty()->SetBold(0);
1534 label_actor->GetCaptionTextProperty()->SetFontSize(6);
1535 label_actor->BorderOff();
1536 label_actor->LeaderOff();
1537 label_actor->ThreeDimensionalLeaderOff();
1538 mLandLabelActors.push_back(label_actor);
1539 this->Renderer->AddActor2D(mLandLabelActors[id]);
1544 //----------------------------------------------------------------------------
1547 //----------------------------------------------------------------------------
1548 void vvSlicer::SetSlice(int slice)
1550 int *range = this->GetSliceRange();
1552 if (slice < range[0]) {
1554 } else if (slice > range[1]) {
1559 if (this->Slice == slice) {
1563 this->Slice = slice;
1566 this->UpdateDisplayExtent();
1568 // Seems to work without this line
1571 //----------------------------------------------------------------------------
1573 //----------------------------------------------------------------------------
1574 int vvSlicer::GetTMax() {
1575 int tmax = (int)mImage->GetVTKImages().size() - 1;
1577 tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
1580 //----------------------------------------------------------------------------
1582 //----------------------------------------------------------------------------
1583 void vvSlicer::SetContourSlice()
1585 if (mSurfaceCutActors.size() > 0)
1586 for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
1587 i!=mSurfaceCutActors.end(); i++) {
1589 (*i)->SetSlicingOrientation(this->SliceOrientation);
1590 (*i)->SetCutSlice((this->Slice)*this->GetImage()->GetSpacing()[this->SliceOrientation]+
1591 this->GetImage()->GetOrigin()[this->SliceOrientation]);
1594 //----------------------------------------------------------------------------
1597 //----------------------------------------------------------------------------
1598 void vvSlicer::ForceUpdateDisplayExtent()
1600 this->UpdateDisplayExtent();
1602 //----------------------------------------------------------------------------
1605 //----------------------------------------------------------------------------
1606 int* vvSlicer::GetDisplayExtent()
1608 return this->GetImageActor()->GetDisplayExtent();
1610 //----------------------------------------------------------------------------
1613 //----------------------------------------------------------------------------
1614 void vvSlicer::PrintSelf(ostream& os, vtkIndent indent)
1616 this->Superclass::PrintSelf(os, indent);
1618 //----------------------------------------------------------------------------
1620 //----------------------------------------------------------------------------
1621 void vvSlicer::SetVFColor(double r, double g, double b)
1623 double mVFColorHSV[3];
1628 vtkMath::RGBToHSV(mVFColor, mVFColorHSV);
1629 mVFColorLUT->SetHueRange(mVFColorHSV[0], mVFColorHSV[0]);
1630 mVFColorLUT->SetSaturationRange(mVFColorHSV[1],mVFColorHSV[1]);
1631 mVFColorLUT->SetValueRange(mVFColorHSV[2], mVFColorHSV[2]);