Fraxinus  16.5.0-fx-rc7
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxDepthPeeling.h"
33 #include "cxLogger.h"
34 
43 vtkSmartPointer<vtkAppendPolyData> GenerateOverlappingBunchOfSpheres(int theta, int phi)
44 {
45  vtkSmartPointer<vtkAppendPolyData> appendData =
46  vtkSmartPointer<vtkAppendPolyData>::New();
47 
48  for (int i = 0; i < 5; i++)
49  {
50  vtkSmartPointer<vtkSphereSource> sphereSource =
51  vtkSmartPointer<vtkSphereSource>::New();
52  sphereSource->SetThetaResolution(theta);
53  sphereSource->SetPhiResolution(phi);
54  sphereSource->SetRadius(0.5); // all spheres except the center
55  // one should have radius = 0.5
56  switch (i)
57  {
58  case 0:
59  sphereSource->SetRadius(1);
60  sphereSource->SetCenter(0, 0, 0); break;
61  case 1:
62  sphereSource->SetCenter(1, 0, 0); break;
63  case 2:
64  sphereSource->SetCenter(-1, 0, 0); break;
65  case 3:
66  sphereSource->SetCenter(0, 1, 0); break;
67  case 4:
68  sphereSource->SetCenter(0, -1, 0); break;
69  }
70  sphereSource->Update();
71  appendData->AddInputConnection(sphereSource->GetOutputPort());
72  }
73 
74  return appendData;
75 }
76 
89  vtkSmartPointer<vtkRenderWindow> renderWindow,
90  vtkSmartPointer<vtkRenderer> renderer, int maxNoOfPeels,
91  double occlusionRatio)
92 {
93  if (!renderWindow || !renderer)
94  {
95  cx::reportWarning("Can't set depth peeling. No render / renderwindow");
96  return false;
97  }
98 
99  // 1. Use a render window with alpha bits (as initial value is 0 (false)):
100  renderWindow->SetAlphaBitPlanes(true);
101 
102  // 2. Force to not pick a framebuffer with a multisample buffer
103  // (as initial value is 8):
104  renderWindow->SetMultiSamples(0);
105 
106  // 3. Choose to use depth peeling (if supported) (initial value is 0 (false)):
107  renderer->SetUseDepthPeeling(true);
108 
109  // 4. Set depth peeling parameters
110  // - Set the maximum number of rendering passes (initial value is 4):
111  renderer->SetMaximumNumberOfPeels(maxNoOfPeels);
112  // - Set the occlusion ratio (initial value is 0.0, exact image):
113  renderer->SetOcclusionRatio(occlusionRatio);
114 
115  // Do a test render
116  renderWindow->Render();
117  // Check whether depth peeling was used
118  bool success = renderer->GetLastRenderingUsedDepthPeeling();
119 
120  return success;
121 }
122 
124  vtkSmartPointer<vtkRenderWindow> renderWindow,
125  vtkSmartPointer<vtkRenderer> renderer)
126 {
127  if (!renderWindow || !renderer)
128  {
129  cx::reportWarning("Can't turn off depth peeling. No render / renderwindow");
130  return false;
131  }
132 
133  // Set values back to defaults
134  // TODO: Save defaults (see IsDepthPeelingSupported())
135  renderWindow->SetAlphaBitPlanes(false);
136  renderWindow->SetMultiSamples(8);
137  renderer->SetUseDepthPeeling(false);
138  renderer->SetMaximumNumberOfPeels(4);
139  renderer->SetOcclusionRatio(0.0);
140 
141  return true;
142 }
143 
155 bool IsDepthPeelingSupported(vtkSmartPointer<vtkRenderWindow> renderWindow,
156  vtkSmartPointer<vtkRenderer> renderer, bool doItOffScreen)
157 {
158  if (!renderWindow || !renderer)
159  {
160  cx::reportWarning("Can't test depth peeling. No render / renderwindow");
161  return false;
162  }
163 
164  bool success = true;
165 
166  // Save original renderer / render window state
167  bool origOffScreenRendering = renderWindow->GetOffScreenRendering() == 1;
168  bool origAlphaBitPlanes = renderWindow->GetAlphaBitPlanes() == 1;
169  int origMultiSamples = renderWindow->GetMultiSamples();
170  bool origUseDepthPeeling = renderer->GetUseDepthPeeling() == 1;
171  int origMaxPeels = renderer->GetMaximumNumberOfPeels();
172  double origOcclusionRatio = renderer->GetOcclusionRatio();
173 
174  // Activate off screen rendering on demand
175  renderWindow->SetOffScreenRendering(doItOffScreen);
176 
177  // Setup environment for depth peeling (with some default parametrization)
178  success = success && SetupEnvironmentForDepthPeeling(renderWindow, renderer,
179  100, 0.1);
180 
181  // Do a test render
182  renderWindow->Render();
183 
184  // Check whether depth peeling was used
185  success = success && renderer->GetLastRenderingUsedDepthPeeling();
186 
187  // recover original state
188  renderWindow->SetOffScreenRendering(origOffScreenRendering);
189  renderWindow->SetAlphaBitPlanes(origAlphaBitPlanes);
190  renderWindow->SetMultiSamples(origMultiSamples);
191  renderer->SetUseDepthPeeling(origUseDepthPeeling);
192  renderer->SetMaximumNumberOfPeels(origMaxPeels);
193  renderer->SetOcclusionRatio(origOcclusionRatio);
194 
195  return success;
196 }
bool IsDepthPeelingSupported(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer, bool doItOffScreen)
vtkSmartPointer< vtkAppendPolyData > GenerateOverlappingBunchOfSpheres(int theta, int phi)
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
bool TurnOffDepthPeeling(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer)
Turn off depth peeling.
bool SetupEnvironmentForDepthPeeling(vtkSmartPointer< vtkRenderWindow > renderWindow, vtkSmartPointer< vtkRenderer > renderer, int maxNoOfPeels, double occlusionRatio)