34 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
36 #include "vtkCamera.h"
37 #include "vtkCellData.h"
38 #include "vtkCommand.h"
39 #include "vtkDataArray.h"
40 #include "vtkGPUInfo.h"
41 #include "vtkGPUInfoList.h"
42 #include "vtkImageData.h"
43 #include "vtkImageResample.h"
44 #include "vtkMultiThreader.h"
45 #include "vtkPointData.h"
46 #include "vtkRenderWindow.h"
47 #include "vtkRenderer.h"
48 #include "vtkRendererCollection.h"
49 #include "vtkTimerLog.h"
50 #include "vtkVolume.h"
51 #include "vtkVolumeProperty.h"
58 vtkMitkGPUVolumeRayCastMapper::vtkMitkGPUVolumeRayCastMapper()
60 this->AutoAdjustSampleDistances = 1;
61 this->ImageSampleDistance = 1.0;
62 this->MinimumImageSampleDistance = 1.0;
63 this->MaximumImageSampleDistance = 10.0;
64 this->SampleDistance = 1.0;
65 this->SmallVolumeRender = 0;
66 this->BigTimeToDraw = 0.0;
67 this->SmallTimeToDraw = 0.0;
68 this->FinalColorWindow = 1.0;
69 this->FinalColorLevel = 0.5;
70 this->GeneratingCanonicalView = 0;
71 this->CanonicalViewImageData =
nullptr;
73 this->MaskInput =
nullptr;
74 this->MaskBlendFactor = 1.0f;
77 this->ClippedCroppingRegionPlanes[0] = VTK_DOUBLE_MAX;
78 this->ClippedCroppingRegionPlanes[1] = VTK_DOUBLE_MIN;
79 this->ClippedCroppingRegionPlanes[2] = VTK_DOUBLE_MAX;
80 this->ClippedCroppingRegionPlanes[3] = VTK_DOUBLE_MIN;
81 this->ClippedCroppingRegionPlanes[4] = VTK_DOUBLE_MAX;
82 this->ClippedCroppingRegionPlanes[5] = VTK_DOUBLE_MIN;
84 this->MaxMemoryInBytes = 0;
87 if (l->GetNumberOfGPUs() > 0)
89 vtkGPUInfo *
info = l->GetGPUInfo(0);
90 this->MaxMemoryInBytes = info->GetDedicatedVideoMemory();
91 if (this->MaxMemoryInBytes == 0)
93 this->MaxMemoryInBytes = info->GetDedicatedSystemMemory();
99 if (this->MaxMemoryInBytes == 0)
101 this->MaxMemoryInBytes = 128 * 1024 * 1024;
104 this->MaxMemoryFraction = 0.75;
106 this->ReportProgress =
true;
108 this->TransformedInput =
nullptr;
109 this->LastInput =
nullptr;
113 vtkMitkGPUVolumeRayCastMapper::~vtkMitkGPUVolumeRayCastMapper()
115 this->SetMaskInput(
nullptr);
116 this->SetTransformedInput(
nullptr);
117 this->LastInput =
nullptr;
131 void vtkMitkGPUVolumeRayCastMapper::Render(vtkRenderer *ren, vtkVolume *vol)
135 if (this->GeneratingCanonicalView)
137 this->CanonicalViewRender(ren, vol);
142 this->InvokeEvent(vtkCommand::VolumeMapperRenderStartEvent,
nullptr);
150 if (this->ValidateRender(ren, vol))
153 this->GPURender(ren, vol);
158 double t = timer->GetElapsedTime();
162 this->TimeToDraw = t;
165 if (vol->GetAllocatedRenderTime() < 1.0)
167 this->SmallTimeToDraw = t;
171 this->BigTimeToDraw = t;
175 this->InvokeEvent(vtkCommand::VolumeMapperRenderEndEvent,
nullptr);
182 void vtkMitkGPUVolumeRayCastMapper::CanonicalViewRender(vtkRenderer *ren, vtkVolume *vol)
185 if (this->ValidateRender(ren, vol))
188 this->GPURender(ren, vol);
201 int vtkMitkGPUVolumeRayCastMapper::ValidateRender(vtkRenderer *ren, vtkVolume *vol)
210 vtkErrorMacro(
"Renderer cannot be null.");
214 if (goodSoFar && !vol)
217 vtkErrorMacro(
"Volume cannot be null.");
232 if (goodSoFar && vol->GetProperty()->GetColorChannels() != 3)
243 if (goodSoFar && this->Cropping && (this->CroppingRegionPlanes[0] >= this->CroppingRegionPlanes[1] ||
244 this->CroppingRegionPlanes[2] >= this->CroppingRegionPlanes[3] ||
245 this->CroppingRegionPlanes[4] >= this->CroppingRegionPlanes[5]))
252 vtkImageData *input = this->GetInput();
255 if (input != this->LastInput || input->GetMTime() > this->TransformedInput->GetMTime())
257 this->LastInput = input;
260 if (!this->TransformedInput)
263 this->SetTransformedInput(clone);
268 clone = this->TransformedInput;
271 clone->ShallowCopy(input);
279 int extents[6], real_extents[6];
280 clone->GetExtent(extents);
281 clone->GetExtent(real_extents);
284 double origin[3], spacing[3];
285 clone->GetOrigin(origin);
286 clone->GetSpacing(spacing);
288 for (
int cc = 0; cc < 3; cc++)
291 origin[cc] = origin[cc] + extents[2 * cc] * spacing[cc];
292 extents[2 * cc + 1] -= extents[2 * cc];
293 extents[2 * cc] -= extents[2 * cc];
296 clone->SetOrigin(origin);
297 clone->SetExtent(extents);
300 if (goodSoFar && !this->TransformedInput)
302 vtkErrorMacro(
"Input is NULL but is required");
309 vtkDataArray *scalars =
nullptr;
318 scalars = this->GetScalars(
319 this->TransformedInput, this->ScalarMode, this->ArrayAccessMode, this->ArrayId, this->ArrayName, this->CellFlag);
324 vtkErrorMacro(
"No scalars found on input.");
328 else if (this->CellFlag == 2)
330 vtkErrorMacro(
"Only point or cell scalar support - found field scalars instead.");
339 switch (scalars->GetDataType())
342 vtkErrorMacro(<<
"scalar of type VTK_CHAR is not supported "
343 <<
"because this type is platform dependent. "
344 <<
"Use VTK_SIGNED_CHAR or VTK_UNSIGNED_CHAR instead.");
348 vtkErrorMacro(
"scalar of type VTK_BIT is not supported by this mapper.");
352 vtkErrorMacro(
"scalar of type VTK_ID_TYPE is not supported by this mapper.");
356 vtkErrorMacro(
"scalar of type VTK_STRING is not supported by this mapper.");
368 if (this->BlendMode != vtkVolumeMapper::COMPOSITE_BLEND &&
369 this->BlendMode != vtkVolumeMapper::MAXIMUM_INTENSITY_BLEND &&
370 this->BlendMode != vtkVolumeMapper::MINIMUM_INTENSITY_BLEND)
373 vtkErrorMacro(<<
"Selected blend mode not supported. "
374 <<
"Only Composite and MIP and MinIP modes "
375 <<
"are supported by the current implementation.");
381 int numberOfComponents = 0;
384 numberOfComponents = scalars->GetNumberOfComponents();
385 if (!(numberOfComponents == 1 || (numberOfComponents == 4 && vol->GetProperty()->GetIndependentComponents() == 0)))
388 vtkErrorMacro(<<
"Only one component scalars, or four "
389 <<
"component with non-independent components, "
390 <<
"are supported by this mapper.");
395 if (goodSoFar && numberOfComponents == 4 && scalars->GetDataType() != VTK_UNSIGNED_CHAR)
398 vtkErrorMacro(
"Only unsigned char is supported for 4-component scalars!");
410 void vtkMitkGPUVolumeRayCastMapper::SetCellFlag(
int cellFlag)
412 this->CellFlag = cellFlag;
416 void vtkMitkGPUVolumeRayCastMapper::CreateCanonicalView(vtkRenderer *ren,
419 int vtkNotUsed(blend_mode),
420 double viewDirection[3],
423 this->GeneratingCanonicalView = 1;
424 int oldSwap = ren->GetRenderWindow()->GetSwapBuffers();
425 ren->GetRenderWindow()->SwapBuffersOff();
428 image->GetDimensions(dim);
429 int *size = ren->GetRenderWindow()->GetSize();
432 bigImage->SetDimensions(size[0], size[1], 1);
433 bigImage->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
435 this->CanonicalViewImageData = bigImage;
438 scale[0] = dim[0] /
static_cast<double>(size[0]);
439 scale[1] = dim[1] /
static_cast<double>(size[1]);
443 vtkRendererCollection *renderers = ren->GetRenderWindow()->GetRenderers();
444 int numberOfRenderers = renderers->GetNumberOfItems();
446 auto rendererVisibilities =
new bool[numberOfRenderers];
447 renderers->InitTraversal();
449 while (i < numberOfRenderers)
451 vtkRenderer *r = renderers->GetNextItem();
452 rendererVisibilities[i] = r->GetDraw() == 1;
463 vtkPropCollection *props = ren->GetViewProps();
464 int numberOfProps = props->GetNumberOfItems();
466 auto propVisibilities =
new bool[numberOfProps];
467 props->InitTraversal();
469 while (i < numberOfProps)
471 vtkProp *p = props->GetNextProp();
472 propVisibilities[i] = p->GetVisibility() == 1;
475 p->SetVisibility(
false);
480 vtkCamera *savedCamera = ren->GetActiveCamera();
481 savedCamera->Modified();
485 double *center = volume->GetCenter();
487 volume->GetBounds(bounds);
489 sqrt((bounds[1] - bounds[0]) * (bounds[1] - bounds[0]) + (bounds[3] - bounds[2]) * (bounds[3] - bounds[2]) +
490 (bounds[5] - bounds[4]) * (bounds[5] - bounds[4]));
493 d = bounds[1] - bounds[0];
496 canonicalViewCamera->SetFocalPoint(center);
497 canonicalViewCamera->ParallelProjectionOn();
498 canonicalViewCamera->SetPosition(
499 center[0] - d * viewDirection[0], center[1] - d * viewDirection[1], center[2] - d * viewDirection[2]);
500 canonicalViewCamera->SetViewUp(viewUp);
501 canonicalViewCamera->SetParallelScale(d / 2);
503 ren->SetActiveCamera(canonicalViewCamera);
504 ren->GetRenderWindow()->Render();
506 ren->SetActiveCamera(savedCamera);
507 canonicalViewCamera->Delete();
511 resample->SetInputData(bigImage);
512 resample->SetAxisMagnificationFactor(0, scale[0]);
513 resample->SetAxisMagnificationFactor(1, scale[1]);
514 resample->SetAxisMagnificationFactor(2, 1);
515 resample->UpdateWholeExtent();
518 image->DeepCopy(resample->GetOutput());
524 props->InitTraversal();
526 while (i < numberOfProps)
528 vtkProp *p = props->GetNextProp();
529 p->SetVisibility(propVisibilities[i]);
533 delete[] propVisibilities;
536 renderers->InitTraversal();
538 while (i < numberOfRenderers)
540 vtkRenderer *r = renderers->GetNextItem();
541 r->SetDraw(rendererVisibilities[i]);
545 delete[] rendererVisibilities;
547 ren->GetRenderWindow()->SetSwapBuffers(oldSwap);
548 this->CanonicalViewImageData =
nullptr;
549 this->GeneratingCanonicalView = 0;
554 void vtkMitkGPUVolumeRayCastMapper::PrintSelf(ostream &os, vtkIndent indent)
556 this->Superclass::PrintSelf(os, indent);
558 os << indent <<
"AutoAdjustSampleDistances: " << this->AutoAdjustSampleDistances << endl;
559 os << indent <<
"MinimumImageSampleDistance: " << this->MinimumImageSampleDistance << endl;
560 os << indent <<
"MaximumImageSampleDistance: " << this->MaximumImageSampleDistance << endl;
561 os << indent <<
"ImageSampleDistance: " << this->ImageSampleDistance << endl;
562 os << indent <<
"SampleDistance: " << this->SampleDistance << endl;
563 os << indent <<
"FinalColorWindow: " << this->FinalColorWindow << endl;
564 os << indent <<
"FinalColorLevel: " << this->FinalColorLevel << endl;
565 os << indent <<
"MaskInput: " << this->MaskInput << endl;
566 os << indent <<
"MaskBlendFactor: " << this->MaskBlendFactor << endl;
567 os << indent <<
"MaxMemoryInBytes: " << this->MaxMemoryInBytes << endl;
568 os << indent <<
"MaxMemoryFraction: " << this->MaxMemoryFraction << endl;
569 os << indent <<
"ReportProgress: " << this->ReportProgress << endl;
584 void vtkMitkGPUVolumeRayCastMapper::ClipCroppingRegionPlanes()
586 assert(
"pre: volume_exists" && this->GetInput() !=
nullptr);
587 assert(
"pre: valid_cropping" && this->Cropping && this->CroppingRegionPlanes[0] < this->CroppingRegionPlanes[1] &&
588 this->CroppingRegionPlanes[2] < this->CroppingRegionPlanes[3] &&
589 this->CroppingRegionPlanes[4] < this->CroppingRegionPlanes[5]);
601 this->GetInput()->GetBounds(volBounds);
607 if (this->CroppingRegionPlanes[i] < volBounds[i])
609 this->ClippedCroppingRegionPlanes[i] = volBounds[i];
613 this->ClippedCroppingRegionPlanes[i] = this->CroppingRegionPlanes[i];
617 if (this->CroppingRegionPlanes[i] > volBounds[i])
619 this->ClippedCroppingRegionPlanes[i] = volBounds[i];
623 this->ClippedCroppingRegionPlanes[i] = this->CroppingRegionPlanes[i];
static void info(const char *fmt,...)
vtkInstantiatorNewMacro(vtkMitkVolumeTextureMapper3D)
static void clone(T *&dst, S *src, int n)
vtkCxxSetObjectMacro(vtkXMLMaterialParser, Material, vtkXMLMaterial)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.