Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkGPUVolumeMapper3D.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #define GPU_INFO MITK_INFO("mapper.vr")
18 #define GPU_WARN MITK_WARN("mapper.vr")
19 #define GPU_ERROR MITK_ERROR("mapper.vr")
20 
21 #include <vtkAutoInit.h>
22 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
23 
24 #include "mitkGPUVolumeMapper3D.h"
25 
26 #include "mitkDataNode.h"
27 
28 #include "mitkColorProperty.h"
29 #include "mitkColorProperty.h"
30 #include "mitkLevelWindow.h"
33 #include "mitkProperties.h"
34 #include "mitkRenderingManager.h"
37 #include "mitkVtkPropRenderer.h"
38 
39 #include <vtkActor.h>
40 #include <vtkProperty.h>
41 
42 #include <vtkAssembly.h>
43 #include <vtkColorTransferFunction.h>
44 #include <vtkFiniteDifferenceGradientEstimator.h>
45 #include <vtkImageData.h>
46 #include <vtkImageResample.h>
47 #include <vtkImageWriter.h>
48 #include <vtkImplicitPlaneWidget.h>
49 #include <vtkLODProp3D.h>
50 #include <vtkPiecewiseFunction.h>
51 #include <vtkPlane.h>
52 #include <vtkRenderWindow.h>
53 #include <vtkRenderWindowInteractor.h>
54 #include <vtkVolume.h>
55 #include <vtkVolumeProperty.h>
56 #include <vtkVolumeRayCastCompositeFunction.h>
57 #include <vtkVolumeRayCastMIPFunction.h>
58 #include <vtkVolumeTextureMapper2D.h>
59 
60 #include <vtkCubeSource.h>
61 #include <vtkPolyDataMapper.h>
62 
63 // Only with VTK 5.6 or above
64 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
65 
67 
68 #endif
69 
71 #include "vtkOpenGLGPUVolumeRayCastMapper.h"
72 
74 {
75  return static_cast<const mitk::Image *>(GetDataNode()->GetData());
76 }
77 
79 {
80  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
81 
83 
84  if (ls->m_gpuInitialized)
85  ls->m_MapperGPU->UpdateMTime();
86 }
87 
89 {
90  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
91 
92  if (ls->m_gpuInitialized)
93  return ls->m_gpuSupported;
94 
95  ls->m_VtkRenderWindow = renderer->GetVtkRenderer()->GetRenderWindow();
96 
97  GPU_INFO << "initializing gpu-slicing-vr (vtkMitkOpenGLVolumeTextureMapper3D)";
98 
100  ls->m_MapperGPU->SetUseCompressedTexture(false);
101  ls->m_MapperGPU->SetSampleDistance(1.0);
102 
104  ls->m_VolumePropertyGPU->ShadeOn();
105  ls->m_VolumePropertyGPU->SetAmbient(0.25f); // 0.05f
106  ls->m_VolumePropertyGPU->SetDiffuse(0.50f); // 0.45f
107  ls->m_VolumePropertyGPU->SetSpecular(0.40f); // 0.50f
108  ls->m_VolumePropertyGPU->SetSpecularPower(16.0f);
109  ls->m_VolumePropertyGPU->SetInterpolationTypeToLinear();
110 
112  ls->m_VolumeGPU->SetMapper(ls->m_MapperGPU);
113  ls->m_VolumeGPU->SetProperty(ls->m_VolumePropertyGPU);
114  ls->m_VolumeGPU->VisibilityOn();
115 
116  ls->m_MapperGPU->SetInputConnection(this->m_UnitSpacingImageFilter->GetOutputPort());
117 
118  ls->m_gpuSupported = ls->m_MapperGPU->IsRenderSupported(renderer->GetVtkRenderer(), ls->m_VolumePropertyGPU);
119 
120  ls->m_gpuInitialized = true;
121 
122  return ls->m_gpuSupported;
123 }
124 
126 {
127  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
128 
129  if (ls->m_cpuInitialized)
130  return;
131 
132  ls->m_VtkRenderWindow = renderer->GetVtkRenderer()->GetRenderWindow();
133 
135  int numThreads = ls->m_MapperCPU->GetNumberOfThreads();
136 
137  GPU_INFO << "initializing cpu-raycast-vr (vtkFixedPointVolumeRayCastMapper) (" << numThreads << " threads)";
138 
139  ls->m_MapperCPU->SetSampleDistance(1.0);
140  // ls->m_MapperCPU->LockSampleDistanceToInputSpacingOn();
141  ls->m_MapperCPU->SetImageSampleDistance(1.0);
142  ls->m_MapperCPU->IntermixIntersectingGeometryOn();
143  ls->m_MapperCPU->SetAutoAdjustSampleDistances(0);
144 
146  ls->m_VolumePropertyCPU->ShadeOn();
147  ls->m_VolumePropertyCPU->SetAmbient(0.10f); // 0.05f
148  ls->m_VolumePropertyCPU->SetDiffuse(0.50f); // 0.45f
149  ls->m_VolumePropertyCPU->SetSpecular(0.40f); // 0.50f
150  ls->m_VolumePropertyCPU->SetSpecularPower(16.0f);
151  ls->m_VolumePropertyCPU->SetInterpolationTypeToLinear();
152 
154  ls->m_VolumeCPU->SetMapper(ls->m_MapperCPU);
155  ls->m_VolumeCPU->SetProperty(ls->m_VolumePropertyCPU);
156  ls->m_VolumeCPU->VisibilityOn();
157 
158  ls->m_MapperCPU->SetInputConnection(m_UnitSpacingImageFilter->GetOutputPort()); // m_Resampler->GetOutput());
159 
160  ls->m_cpuInitialized = true;
161 }
162 
164 {
165  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
166 
167  if (ls->m_gpuInitialized)
168  {
169  GPU_INFO << "deinitializing gpu-slicing-vr";
170  // deinit renderwindow, this is needed to release the memory allocated on the gpu
171  // to prevent leaking memory on the gpu
172  ls->m_MapperGPU->ReleaseGraphicsResources(renderer->GetVtkRenderer()->GetVTKWindow());
173  ls->m_VolumePropertyGPU = NULL;
174  ls->m_MapperGPU = NULL;
175  ls->m_VolumeGPU = NULL;
176  ls->m_gpuInitialized = false;
177  }
178 }
179 
181 {
182  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
183 
184  if (!ls->m_cpuInitialized)
185  return;
186 
187  GPU_INFO << "deinitializing cpu-raycast-vr";
188 
189  ls->m_VolumePropertyCPU = NULL;
190  ls->m_MapperCPU = NULL;
191  ls->m_VolumeCPU = NULL;
192  ls->m_cpuInitialized = false;
193 }
194 
196 {
197  m_VolumeNULL = 0;
198  m_commonInitialized = false;
199 }
200 
202 {
203  DeinitCommon();
204 }
205 
207 {
208  if (m_commonInitialized)
209  return;
210 
211  m_UnitSpacingImageFilter = vtkSmartPointer<vtkImageChangeInformation>::New();
212  m_UnitSpacingImageFilter->SetOutputSpacing(1.0, 1.0, 1.0);
213 
214  CreateDefaultTransferFunctions();
215 
216  m_commonInitialized = true;
217 }
218 
220 {
221  if (!m_commonInitialized)
222  return;
223 
224  m_commonInitialized = false;
225 }
226 
228 {
229  if (!GetDataNode())
230  return false;
231 
232  DataNode *node = GetDataNode();
233 
234  bool visible = true;
235  node->GetVisibility(visible, renderer, "visible");
236 
237  if (!visible)
238  return false;
239 
240  bool value = false;
241  if (!node->GetBoolProperty("volumerendering", value, renderer))
242  return false;
243 
244  if (!value)
245  return false;
246 
247  mitk::Image *input = const_cast<mitk::Image *>(this->GetInput());
248 
249  if (!input || !input->IsInitialized())
250  return false;
251 
252  vtkImageData *inputData = input->GetVtkImageData(this->GetTimestep());
253 
254  if (inputData == NULL)
255  return false;
256 
257  return true;
258 }
259 
261 {
262 // Only with VTK 5.6 or above
263 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
264  if (IsRAYEnabled(renderer))
265  {
266  DeinitCPU(renderer);
267  DeinitGPU(renderer);
268  if (!InitRAY(renderer))
269  {
270  GPU_WARN << "hardware renderer can't initialize ... falling back to software renderer";
271  goto fallback;
272  }
273  }
274  else
275 #endif
276  if (IsGPUEnabled(renderer))
277  {
278  DeinitCPU(renderer);
279 // Only with VTK 5.6 or above
280 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
281  DeinitRAY(renderer);
282 #endif
283  if (!InitGPU(renderer))
284  {
285  GPU_WARN << "hardware renderer can't initialize ... falling back to software renderer";
286  goto fallback;
287  }
288  }
289  else
290  {
291  fallback:
292  DeinitGPU(renderer);
293 // Only with VTK 5.6 or above
294 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
295  DeinitRAY(renderer);
296 #endif
297  InitCPU(renderer);
298  }
299 }
300 
302 {
303  if (!IsRenderable(renderer))
304  {
305  if (!m_VolumeNULL)
306  {
307  m_VolumeNULL = vtkSmartPointer<vtkVolume>::New();
308  m_VolumeNULL->VisibilityOff();
309  }
310  return m_VolumeNULL;
311  }
312 
313  InitCommon();
314  InitVtkMapper(renderer);
315 
316  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
317 
318 // Only with VTK 5.6 or above
319 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
320  if (ls->m_rayInitialized)
321  return ls->m_VolumeRAY;
322 #endif
323 
324  if (ls->m_gpuInitialized)
325  return ls->m_VolumeGPU;
326 
327  return ls->m_VolumeCPU;
328 }
329 
331 {
332  if (!IsRenderable(renderer))
333  return;
334 
335  InitCommon();
336  InitVtkMapper(renderer);
337 
338  mitk::Image *input = const_cast<mitk::Image *>(this->GetInput());
339  vtkImageData *inputData = input->GetVtkImageData(this->GetTimestep());
340  m_UnitSpacingImageFilter->SetInputData(inputData);
341 
342  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
343 
344 // Only with VTK 5.6 or above
345 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
346  if (ls->m_rayInitialized)
347  {
348  GenerateDataRAY(renderer);
349  }
350  else
351 #endif
352 
353  if (ls->m_gpuInitialized)
354  {
355  GenerateDataGPU(renderer);
356  }
357  else
358  {
359  GenerateDataCPU(renderer);
360  }
361 
362  // UpdateTransferFunctions
363  UpdateTransferFunctions(renderer);
364 }
365 
367 {
368  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
369 
370  bool useCompression = false;
371  GetDataNode()->GetBoolProperty("volumerendering.gpu.usetexturecompression", useCompression, renderer);
372  ls->m_MapperGPU->SetUseCompressedTexture(useCompression);
373 
374  if (IsLODEnabled(renderer) && mitk::RenderingManager::GetInstance()->GetNextLOD(renderer) == 0)
375  ls->m_MapperGPU->SetSampleDistance(2.0);
376  else
377  ls->m_MapperGPU->SetSampleDistance(1.0);
378 
379  // Updating shadings
380  {
381  float value = 0;
382  if (GetDataNode()->GetFloatProperty("volumerendering.gpu.ambient", value, renderer))
383  ls->m_VolumePropertyGPU->SetAmbient(value);
384  if (GetDataNode()->GetFloatProperty("volumerendering.gpu.diffuse", value, renderer))
385  ls->m_VolumePropertyGPU->SetDiffuse(value);
386  if (GetDataNode()->GetFloatProperty("volumerendering.gpu.specular", value, renderer))
387  ls->m_VolumePropertyGPU->SetSpecular(value);
388  if (GetDataNode()->GetFloatProperty("volumerendering.gpu.specular.power", value, renderer))
389  ls->m_VolumePropertyGPU->SetSpecularPower(value);
390  }
391 }
392 
394 {
395  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
396 
397  int nextLod = mitk::RenderingManager::GetInstance()->GetNextLOD(renderer);
398 
399  if (IsLODEnabled(renderer) && nextLod == 0)
400  {
401  ls->m_MapperCPU->SetImageSampleDistance(3.5);
402  ls->m_MapperCPU->SetSampleDistance(1.25);
403  ls->m_VolumePropertyCPU->SetInterpolationTypeToNearest();
404  }
405  else
406  {
407  ls->m_MapperCPU->SetImageSampleDistance(1.0);
408  ls->m_MapperCPU->SetSampleDistance(1.0);
409  ls->m_VolumePropertyCPU->SetInterpolationTypeToLinear();
410  }
411 
412  // Check raycasting mode
413  if (IsMIPEnabled(renderer))
414  ls->m_MapperCPU->SetBlendModeToMaximumIntensity();
415  else
416  ls->m_MapperCPU->SetBlendModeToComposite();
417 
418  // Updating shadings
419  {
420  float value = 0;
421  if (GetDataNode()->GetFloatProperty("volumerendering.cpu.ambient", value, renderer))
422  ls->m_VolumePropertyCPU->SetAmbient(value);
423  if (GetDataNode()->GetFloatProperty("volumerendering.cpu.diffuse", value, renderer))
424  ls->m_VolumePropertyCPU->SetDiffuse(value);
425  if (GetDataNode()->GetFloatProperty("volumerendering.cpu.specular", value, renderer))
426  ls->m_VolumePropertyCPU->SetSpecular(value);
427  if (GetDataNode()->GetFloatProperty("volumerendering.cpu.specular.power", value, renderer))
428  ls->m_VolumePropertyCPU->SetSpecularPower(value);
429  }
430 }
431 
433 {
434  m_DefaultOpacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
435  m_DefaultOpacityTransferFunction->AddPoint(0.0, 0.0);
436  m_DefaultOpacityTransferFunction->AddPoint(255.0, 0.8);
437  m_DefaultOpacityTransferFunction->ClampingOn();
438 
439  m_DefaultGradientTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
440  m_DefaultGradientTransferFunction->AddPoint(0.0, 0.0);
441  m_DefaultGradientTransferFunction->AddPoint(255.0, 0.8);
442  m_DefaultGradientTransferFunction->ClampingOn();
443 
444  m_DefaultColorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
445  m_DefaultColorTransferFunction->AddRGBPoint(0.0, 0.0, 0.0, 0.0);
446  m_DefaultColorTransferFunction->AddRGBPoint(127.5, 1, 1, 0.0);
447  m_DefaultColorTransferFunction->AddRGBPoint(255.0, 0.8, 0.2, 0);
448  m_DefaultColorTransferFunction->ClampingOn();
449 
450  m_BinaryOpacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
451  m_BinaryOpacityTransferFunction->AddPoint(0, 0.0);
452  m_BinaryOpacityTransferFunction->AddPoint(1, 1.0);
453 
454  m_BinaryGradientTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
455  m_BinaryGradientTransferFunction->AddPoint(0.0, 1.0);
456 
457  m_BinaryColorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
458 }
459 
461 {
462  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
463 
464  vtkPiecewiseFunction *opacityTransferFunction = m_DefaultOpacityTransferFunction;
465  vtkPiecewiseFunction *gradientTransferFunction = m_DefaultGradientTransferFunction;
466  vtkColorTransferFunction *colorTransferFunction = m_DefaultColorTransferFunction;
467 
468  bool isBinary = false;
469 
470  GetDataNode()->GetBoolProperty("binary", isBinary, renderer);
471 
472  if (isBinary)
473  {
474  opacityTransferFunction = m_BinaryOpacityTransferFunction;
475  gradientTransferFunction = m_BinaryGradientTransferFunction;
476  colorTransferFunction = m_BinaryColorTransferFunction;
477 
478  colorTransferFunction->RemoveAllPoints();
479  float rgb[3];
480  if (!GetDataNode()->GetColor(rgb, renderer))
481  rgb[0] = rgb[1] = rgb[2] = 1;
482  colorTransferFunction->AddRGBPoint(0, rgb[0], rgb[1], rgb[2]);
483  colorTransferFunction->Modified();
484  }
485  else
486  {
487  mitk::TransferFunctionProperty *transferFunctionProp =
488  dynamic_cast<mitk::TransferFunctionProperty *>(this->GetDataNode()->GetProperty("TransferFunction", renderer));
489 
490  if (transferFunctionProp)
491  {
492  opacityTransferFunction = transferFunctionProp->GetValue()->GetScalarOpacityFunction();
493  gradientTransferFunction = transferFunctionProp->GetValue()->GetGradientOpacityFunction();
494  colorTransferFunction = transferFunctionProp->GetValue()->GetColorTransferFunction();
495  }
496  }
497 
498  if (ls->m_gpuInitialized)
499  {
500  ls->m_VolumePropertyGPU->SetColor(colorTransferFunction);
501  ls->m_VolumePropertyGPU->SetScalarOpacity(opacityTransferFunction);
502  ls->m_VolumePropertyGPU->SetGradientOpacity(gradientTransferFunction);
503  }
504 
505 // Only with VTK 5.6 or above
506 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
507 
508  if (ls->m_rayInitialized)
509  {
510  ls->m_VolumePropertyRAY->SetColor(colorTransferFunction);
511  ls->m_VolumePropertyRAY->SetScalarOpacity(opacityTransferFunction);
512  ls->m_VolumePropertyRAY->SetGradientOpacity(gradientTransferFunction);
513  }
514 
515 #endif
516 
517  if (ls->m_cpuInitialized)
518  {
519  ls->m_VolumePropertyCPU->SetColor(colorTransferFunction);
520  ls->m_VolumePropertyCPU->SetScalarOpacity(opacityTransferFunction);
521  ls->m_VolumePropertyCPU->SetGradientOpacity(gradientTransferFunction);
522  }
523 }
524 
525 void mitk::GPUVolumeMapper3D::ApplyProperties(vtkActor * /*actor*/, mitk::BaseRenderer * /*renderer*/)
526 {
527  // GPU_INFO << "ApplyProperties";
528 }
529 
531 {
532  // GPU_INFO << "SetDefaultProperties";
533 
534  node->AddProperty("volumerendering", mitk::BoolProperty::New(false), renderer, overwrite);
535  node->AddProperty("volumerendering.usemip", mitk::BoolProperty::New(false), renderer, overwrite);
536  node->AddProperty("volumerendering.uselod", mitk::BoolProperty::New(false), renderer, overwrite);
537 
538  node->AddProperty("volumerendering.cpu.ambient", mitk::FloatProperty::New(0.10f), renderer, overwrite);
539  node->AddProperty("volumerendering.cpu.diffuse", mitk::FloatProperty::New(0.50f), renderer, overwrite);
540  node->AddProperty("volumerendering.cpu.specular", mitk::FloatProperty::New(0.40f), renderer, overwrite);
541  node->AddProperty("volumerendering.cpu.specular.power", mitk::FloatProperty::New(16.0f), renderer, overwrite);
542  bool usegpu = true;
543 #ifdef __APPLE__
544  usegpu = false;
545  node->AddProperty("volumerendering.uselod", mitk::BoolProperty::New(true), renderer, overwrite);
546 #endif
547  node->AddProperty("volumerendering.usegpu", mitk::BoolProperty::New(usegpu), renderer, overwrite);
548 
549 // Only with VTK 5.6 or above
550 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
551 
552  node->AddProperty("volumerendering.useray", mitk::BoolProperty::New(false), renderer, overwrite);
553 
554  node->AddProperty("volumerendering.ray.ambient", mitk::FloatProperty::New(0.25f), renderer, overwrite);
555  node->AddProperty("volumerendering.ray.diffuse", mitk::FloatProperty::New(0.50f), renderer, overwrite);
556  node->AddProperty("volumerendering.ray.specular", mitk::FloatProperty::New(0.40f), renderer, overwrite);
557  node->AddProperty("volumerendering.ray.specular.power", mitk::FloatProperty::New(16.0f), renderer, overwrite);
558 
559 #endif
560  node->AddProperty("volumerendering.gpu.ambient", mitk::FloatProperty::New(0.25f), renderer, overwrite);
561  node->AddProperty("volumerendering.gpu.diffuse", mitk::FloatProperty::New(0.50f), renderer, overwrite);
562  node->AddProperty("volumerendering.gpu.specular", mitk::FloatProperty::New(0.40f), renderer, overwrite);
563  node->AddProperty("volumerendering.gpu.specular.power", mitk::FloatProperty::New(16.0f), renderer, overwrite);
564  node->AddProperty("volumerendering.gpu.usetexturecompression", mitk::BoolProperty::New(false), renderer, overwrite);
565  node->AddProperty("volumerendering.gpu.reducesliceartifacts", mitk::BoolProperty::New(false), renderer, overwrite);
566 
567  node->AddProperty("binary", mitk::BoolProperty::New(false), renderer, overwrite);
568 
569  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(node->GetData());
570  if (image.IsNotNull() && image->IsInitialized())
571  {
572  if ((overwrite) || (node->GetProperty("levelwindow", renderer) == NULL))
573  {
575  mitk::LevelWindow levelwindow;
576  levelwindow.SetAuto(image);
577  levWinProp->SetLevelWindow(levelwindow);
578  node->SetProperty("levelwindow", levWinProp, renderer);
579  }
580 
581  if ((overwrite) || (node->GetProperty("TransferFunction", renderer) == NULL))
582  {
583  // add a default transfer function
586  tfInit->SetTransferFunctionMode(0);
587  node->SetProperty("TransferFunction", mitk::TransferFunctionProperty::New(tf.GetPointer()));
588  }
589  }
590 
591  Superclass::SetDefaultProperties(node, renderer, overwrite);
592 }
593 
595 {
596  bool value = false;
597  return GetDataNode()->GetBoolProperty("volumerendering.uselod", value, renderer) && value;
598 }
599 
601 {
602  bool value = false;
603  return GetDataNode()->GetBoolProperty("volumerendering.usemip", value, renderer) && value;
604 }
605 
607 {
608  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
609  bool value = false;
610  return ls->m_gpuSupported && GetDataNode()->GetBoolProperty("volumerendering.usegpu", value, renderer) && value;
611 }
612 
613 // Only with VTK 5.6 or above
614 #if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION >= 6)))
615 
616 bool mitk::GPUVolumeMapper3D::InitRAY(mitk::BaseRenderer *renderer)
617 {
618  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
619 
620  if (ls->m_rayInitialized)
621  return ls->m_raySupported;
622 
623  ls->m_VtkRenderWindow = renderer->GetVtkRenderer()->GetRenderWindow();
624 
625  GPU_INFO << "initializing gpu-raycast-vr (vtkOpenGLGPUVolumeRayCastMapper)";
626 
628  ls->m_MapperRAY->SetAutoAdjustSampleDistances(0);
629  ls->m_MapperRAY->SetSampleDistance(1.0);
630 
631  ls->m_VolumePropertyRAY = vtkSmartPointer<vtkVolumeProperty>::New();
632  ls->m_VolumePropertyRAY->ShadeOn();
633  ls->m_VolumePropertyRAY->SetAmbient(0.25f); // 0.05f
634  ls->m_VolumePropertyRAY->SetDiffuse(0.50f); // 0.45f
635  ls->m_VolumePropertyRAY->SetSpecular(0.40f); // 0.50f
636  ls->m_VolumePropertyRAY->SetSpecularPower(16.0f);
637  ls->m_VolumePropertyRAY->SetInterpolationTypeToLinear();
638 
639  ls->m_VolumeRAY = vtkSmartPointer<vtkVolume>::New();
640  ls->m_VolumeRAY->SetMapper(ls->m_MapperRAY);
641  ls->m_VolumeRAY->SetProperty(ls->m_VolumePropertyRAY);
642  ls->m_VolumeRAY->VisibilityOn();
643 
644  ls->m_MapperRAY->SetInputConnection(this->m_UnitSpacingImageFilter->GetOutputPort());
645 
646  ls->m_raySupported = ls->m_MapperRAY->IsRenderSupported(renderer->GetRenderWindow(), ls->m_VolumePropertyRAY);
647 
648  ls->m_rayInitialized = true;
649  return ls->m_raySupported;
650 }
651 
652 void mitk::GPUVolumeMapper3D::DeinitRAY(mitk::BaseRenderer *renderer)
653 {
654  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
655 
656  if (ls->m_rayInitialized)
657  {
658  GPU_INFO << "deinitializing gpu-raycast-vr";
659 
660  ls->m_MapperRAY = NULL;
661  ls->m_VolumePropertyRAY = NULL;
662  // Here ReleaseGraphicsResources has to be called to avoid VTK error messages.
663  // This seems like a VTK bug, because ReleaseGraphicsResources() is ment for internal use,
664  // but you cannot just delete the object (last smartpointer reference) without getting the
665  // VTK error.
666  ls->m_VolumeRAY->ReleaseGraphicsResources(renderer->GetVtkRenderer()->GetRenderWindow());
667  ls->m_VolumeRAY = NULL;
668  ls->m_rayInitialized = false;
669  }
670 }
671 
672 void mitk::GPUVolumeMapper3D::GenerateDataRAY(mitk::BaseRenderer *renderer)
673 {
674  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
675 
676  if (IsLODEnabled(renderer) && mitk::RenderingManager::GetInstance()->GetNextLOD(renderer) == 0)
677  ls->m_MapperRAY->SetImageSampleDistance(4.0);
678  else
679  ls->m_MapperRAY->SetImageSampleDistance(1.0);
680 
681  // Check raycasting mode
682  if (IsMIPEnabled(renderer))
683  ls->m_MapperRAY->SetBlendModeToMaximumIntensity();
684  else
685  ls->m_MapperRAY->SetBlendModeToComposite();
686 
687  // Updating shadings
688  {
689  float value = 0;
690  if (GetDataNode()->GetFloatProperty("volumerendering.ray.ambient", value, renderer))
691  ls->m_VolumePropertyRAY->SetAmbient(value);
692  if (GetDataNode()->GetFloatProperty("volumerendering.ray.diffuse", value, renderer))
693  ls->m_VolumePropertyRAY->SetDiffuse(value);
694  if (GetDataNode()->GetFloatProperty("volumerendering.ray.specular", value, renderer))
695  ls->m_VolumePropertyRAY->SetSpecular(value);
696  if (GetDataNode()->GetFloatProperty("volumerendering.ray.specular.power", value, renderer))
697  ls->m_VolumePropertyRAY->SetSpecularPower(value);
698  }
699 }
700 
702 {
703  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
704  bool value = false;
705  return ls->m_raySupported && GetDataNode()->GetBoolProperty("volumerendering.useray", value, renderer) && value;
706 }
707 
708 #endif
virtual vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
The TransferFunctionProperty class Property class for the mitk::TransferFunction. ...
vtkSmartPointer< vtkMitkVolumeTextureMapper3D > m_MapperGPU
virtual mitk::TransferFunction::Pointer GetValue() const
virtual void ApplyProperties(vtkActor *actor, mitk::BaseRenderer *renderer) override
Apply color and opacity properties read from the PropertyList.
static Pointer New()
bool IsRAYEnabled(BaseRenderer *renderer=NULL)
vtkSmartPointer< vtkVolumeProperty > m_VolumePropertyGPU
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL)
Organizes the rendering process.
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
virtual vtkImageData * GetVtkImageData(int t=0, int n=0)
Get a volume at a specific time t of channel n as a vtkImageData.
Definition: mitkImage.cpp:221
vtkSmartPointer< vtkVolume > m_VolumeGPU
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
void InitVtkMapper(mitk::BaseRenderer *renderer)
virtual bool IsLODEnabled(BaseRenderer *renderer=NULL) const override
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
void DeinitGPU(mitk::BaseRenderer *renderer)
virtual void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
The LevelWindow class Class to store level/window values.
static Pointer New()
void GenerateDataGPU(mitk::BaseRenderer *renderer)
void InitCPU(mitk::BaseRenderer *renderer)
virtual void MitkRenderVolumetricGeometry(BaseRenderer *renderer)
Checks visibility and renders volumes.
void AddProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr, bool overwrite=false)
Add the property (instance of BaseProperty) if it does not exist (or always ifoverwrite istrue) with ...
virtual const mitk::Image * GetInput()
vtkSmartPointer< vtkVolumeProperty > m_VolumePropertyCPU
static RenderingManager * GetInstance()
bool IsRenderable(mitk::BaseRenderer *renderer)
Image class for storing images.
Definition: mitkImage.h:76
bool InitGPU(mitk::BaseRenderer *renderer)
#define GPU_WARN
void SetAuto(const Image *image, bool tryPicTags=true, bool guessByCentralSlice=true, unsigned selectedComponent=0)
sets level/window to optimize the contrast of the given Image
static Pointer New()
void GenerateDataCPU(mitk::BaseRenderer *renderer)
void UpdateTransferFunctions(mitk::BaseRenderer *renderer)
int GetNextLOD(BaseRenderer *renderer)
void DeinitCPU(mitk::BaseRenderer *renderer)
static Pointer New()
bool IsMIPEnabled(BaseRenderer *renderer=NULL)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
void SetProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr)
Set the property (instance of BaseProperty) with key propertyKey in the PropertyList of the renderer ...
virtual void MitkRenderVolumetricGeometry(mitk::BaseRenderer *renderer) override
Checks visibility and renders volumes.
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
#define GPU_INFO
virtual DataNode * GetDataNode() const
Get the DataNode containing the data to map. Method only returns valid DataNode Pointer if the mapper...
Definition: mitkMapper.cpp:36
vtkSmartPointer< vtkVolume > m_VolumeCPU
vtkRenderer * GetVtkRenderer() const
bool GetVisibility(bool &visible, const mitk::BaseRenderer *renderer, const char *propertyKey="visible") const
Convenience access method for visibility properties (instances of BoolProperty with property-key "vis...
Definition: mitkDataNode.h:413
bool IsGPUEnabled(BaseRenderer *renderer=NULL)
vtkRenderWindow * GetRenderWindow() const
Access the RenderWindow into which this renderer renders.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
vtkSmartPointer< vtkFixedPointVolumeRayCastMapper > m_MapperCPU
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.