CustusX  20.03-rc1
An IGT application
cxDepthPeeling.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxDepthPeeling.h"
12 #include "cxLogger.h"
13 
22 vtkSmartPointer<vtkAppendPolyData> GenerateOverlappingBunchOfSpheres(int theta, int phi)
23 {
24  vtkSmartPointer<vtkAppendPolyData> appendData =
25  vtkSmartPointer<vtkAppendPolyData>::New();
26 
27  for (int i = 0; i < 5; i++)
28  {
29  vtkSmartPointer<vtkSphereSource> sphereSource =
30  vtkSmartPointer<vtkSphereSource>::New();
31  sphereSource->SetThetaResolution(theta);
32  sphereSource->SetPhiResolution(phi);
33  sphereSource->SetRadius(0.5); // all spheres except the center
34  // one should have radius = 0.5
35  switch (i)
36  {
37  case 0:
38  sphereSource->SetRadius(1);
39  sphereSource->SetCenter(0, 0, 0); break;
40  case 1:
41  sphereSource->SetCenter(1, 0, 0); break;
42  case 2:
43  sphereSource->SetCenter(-1, 0, 0); break;
44  case 3:
45  sphereSource->SetCenter(0, 1, 0); break;
46  case 4:
47  sphereSource->SetCenter(0, -1, 0); break;
48  }
49  sphereSource->Update();
50  appendData->AddInputConnection(sphereSource->GetOutputPort());
51  }
52 
53  return appendData;
54 }
55 
68  vtkSmartPointer<vtkRenderWindow> renderWindow,
69  vtkSmartPointer<vtkRenderer> renderer, int maxNoOfPeels,
70  double occlusionRatio)
71 {
72  if (!renderWindow || !renderer)
73  {
74  cx::reportWarning("Can't set depth peeling. No render / renderwindow");
75  return false;
76  }
77 
78  // 1. Use a render window with alpha bits (as initial value is 0 (false)):
79  renderWindow->SetAlphaBitPlanes(true);
80 
81  // 2. Force to not pick a framebuffer with a multisample buffer
82  // (as initial value is 8):
83  renderWindow->SetMultiSamples(0);
84 
85  // 3. Choose to use depth peeling (if supported) (initial value is 0 (false)):
86  renderer->SetUseDepthPeeling(true);
87 
88  // 4. Set depth peeling parameters
89  // - Set the maximum number of rendering passes (initial value is 4):
90  renderer->SetMaximumNumberOfPeels(maxNoOfPeels);
91  // - Set the occlusion ratio (initial value is 0.0, exact image):
92  renderer->SetOcclusionRatio(occlusionRatio);
93 
94  // Do a test render
95  renderWindow->Render();
96  // Check whether depth peeling was used
97  bool success = renderer->GetLastRenderingUsedDepthPeeling();
98 
99  return success;
100 }
101 
103  vtkSmartPointer<vtkRenderWindow> renderWindow,
104  vtkSmartPointer<vtkRenderer> renderer)
105 {
106  if (!renderWindow || !renderer)
107  {
108  cx::reportWarning("Can't turn off depth peeling. No render / renderwindow");
109  return false;
110  }
111 
112  // Set values back to defaults
113  // TODO: Save defaults (see IsDepthPeelingSupported())
114  renderWindow->SetAlphaBitPlanes(false);
115  renderWindow->SetMultiSamples(8);
116  renderer->SetUseDepthPeeling(false);
117  renderer->SetMaximumNumberOfPeels(4);
118  renderer->SetOcclusionRatio(0.0);
119 
120  return true;
121 }
122 
134 bool IsDepthPeelingSupported(vtkSmartPointer<vtkRenderWindow> renderWindow,
135  vtkSmartPointer<vtkRenderer> renderer, bool doItOffScreen)
136 {
137  if (!renderWindow || !renderer)
138  {
139  cx::reportWarning("Can't test depth peeling. No render / renderwindow");
140  return false;
141  }
142 
143  bool success = true;
144 
145  // Save original renderer / render window state
146  bool origOffScreenRendering = renderWindow->GetOffScreenRendering() == 1;
147  bool origAlphaBitPlanes = renderWindow->GetAlphaBitPlanes() == 1;
148  int origMultiSamples = renderWindow->GetMultiSamples();
149  bool origUseDepthPeeling = renderer->GetUseDepthPeeling() == 1;
150  int origMaxPeels = renderer->GetMaximumNumberOfPeels();
151  double origOcclusionRatio = renderer->GetOcclusionRatio();
152 
153  // Activate off screen rendering on demand
154  renderWindow->SetOffScreenRendering(doItOffScreen);
155 
156  // Setup environment for depth peeling (with some default parametrization)
157  success = success && SetupEnvironmentForDepthPeeling(renderWindow, renderer,
158  100, 0.1);
159 
160  // Do a test render
161  renderWindow->Render();
162 
163  // Check whether depth peeling was used
164  success = success && renderer->GetLastRenderingUsedDepthPeeling();
165 
166  // recover original state
167  renderWindow->SetOffScreenRendering(origOffScreenRendering);
168  renderWindow->SetAlphaBitPlanes(origAlphaBitPlanes);
169  renderWindow->SetMultiSamples(origMultiSamples);
170  renderer->SetUseDepthPeeling(origUseDepthPeeling);
171  renderer->SetMaximumNumberOfPeels(origMaxPeels);
172  renderer->SetOcclusionRatio(origOcclusionRatio);
173 
174  return success;
175 }
bool IsDepthPeelingSupported(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer, bool doItOffScreen)
vtkSmartPointer< vtkAppendPolyData > GenerateOverlappingBunchOfSpheres(int theta, int phi)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
bool TurnOffDepthPeeling(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer)
Turn off depth peeling.
bool SetupEnvironmentForDepthPeeling(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer, int maxNoOfPeels, double occlusionRatio)