VTK
vtkOpenGLVolumeGradientOpacityTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLVolumeGradientOpacityTable.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkOpenGLVolumeGradientOpacityTable_h
17 #define vtkOpenGLVolumeGradientOpacityTable_h
18 #ifndef __VTK_WRAP__
19 
20 #include <vector>
21 
22 #include <vtkObjectFactory.h>
23 #include <vtkPiecewiseFunction.h>
24 #include <vtkTextureObject.h>
25 #include <vtkVolumeMapper.h>
26 #include <vtkMath.h>
27 #include <vtk_glew.h>
28 
29 
30 //----------------------------------------------------------------------------
32 {
33 public:
34 
36 
37  // activate texture.
38  //--------------------------------------------------------------------------
39  void Activate()
40  {
41  if (!this->TextureObject)
42  {
43  return;
44  }
45  this->TextureObject->Activate();
46  }
47 
48  void Deactivate()
49  {
50  if (!this->TextureObject)
51  {
52  return;
53  }
54  this->TextureObject->Deactivate();
55  }
56 
57  // Update opacity transfer function texture.
58  //--------------------------------------------------------------------------
59  void Update(vtkPiecewiseFunction* gradientOpacity,
60  double sampleDistance,
61  double range[2],
62  double vtkNotUsed(unitDistance),
63  int filterValue,
64  vtkOpenGLRenderWindow* renWin)
65  {
66  bool needUpdate=false;
67 
68  if (!this->TextureObject)
69  {
71  }
72 
73  this->TextureObject->SetContext(renWin);
74 
75  if (this->LastRange[0] != range[0] ||
76  this->LastRange[1] != range[1])
77  {
78  this->LastRange[0] = range[0];
79  this->LastRange[1] = range[1];
80  needUpdate = true;
81  }
82 
83  if(gradientOpacity->GetMTime() > this->BuildTime ||
84  this->TextureObject->GetMTime() > this->BuildTime ||
85  this->LastSampleDistance != sampleDistance ||
86  needUpdate || !this->TextureObject->GetHandle())
87  {
88  int const idealW = gradientOpacity->EstimateMinNumberOfSamples(this->LastRange[0],
89  this->LastRange[1]);
90  int const newWidth = this->GetMaximumSupportedTextureWidth(renWin, idealW);
91 
92  if(this->Table == nullptr || this->TextureWidth != newWidth)
93  {
94  this->TextureWidth = newWidth;
95  delete [] this->Table;
96  this->Table = new float[this->TextureWidth];
97  }
98 
99  gradientOpacity->GetTable(0,
100  (this->LastRange[1] - this->LastRange[0]) * 0.25,
101  this->TextureWidth, this->Table);
102 
103  this->TextureObject->Create2DFromRaw(this->TextureWidth, 1, 1,
104  VTK_FLOAT,
105  this->Table);
106 
108  this->TextureObject->SetMagnificationFilter(filterValue);
109  this->TextureObject->SetMinificationFilter(filterValue);
110  this->BuildTime.Modified();
111  }
112 
113  if(this->LastInterpolation != filterValue)
114  {
115  this->LastInterpolation = filterValue;
116  this->TextureObject->SetMagnificationFilter(filterValue);
117  this->TextureObject->SetMinificationFilter(filterValue);
118  }
119  }
120 
121  //--------------------------------------------------------------------------
123  int idealWidth)
124  {
125  if (!this->TextureObject)
126  {
127  vtkErrorMacro("vtkTextureObject not initialized!");
128  return -1;
129  }
130 
131  // Try to match the next power of two.
132  idealWidth = vtkMath::NearestPowerOfTwo(idealWidth);
133  int const maxWidth = this->TextureObject->GetMaximumTextureSize(renWin);
134  if (maxWidth < 0)
135  {
136  vtkErrorMacro("Failed to query max texture size! using default 1024.");
137  return 1024;
138  }
139 
140  if (maxWidth >= idealWidth)
141  {
142  idealWidth = vtkMath::Max(1024, idealWidth);
143  return idealWidth;
144  }
145 
146  vtkWarningMacro("This OpenGL implementation does not support the required "
147  "texture size of " << idealWidth << ", falling back to maximum allowed, "
148  << maxWidth << "." << "This may cause an incorrect color table mapping.");
149 
150  return maxWidth;
151  }
152 
153  // Get the texture unit
154  //--------------------------------------------------------------------------
155  int GetTextureUnit(void)
156  {
157  if (!this->TextureObject)
158  {
159  return -1;
160  }
161  return this->TextureObject->GetTextureUnit();
162  }
163 
164  //--------------------------------------------------------------------------
166  {
167  if (this->TextureObject)
168  {
170  this->TextureObject->Delete();
171  this->TextureObject = nullptr;
172  }
173  }
174 
175 protected:
176  //--------------------------------------------------------------------------
178  {
179  this->TextureObject = nullptr;
180  this->TextureWidth = width;
181  this->LastSampleDistance = 1.0;
182  this->Table = nullptr;
183  this->LastInterpolation = -1;
184  this->LastRange[0] = this->LastRange[1] = 0.0;
185  }
186 
187  //--------------------------------------------------------------------------
189  {
190  if (this->TextureObject)
191  {
192  this->TextureObject->Delete();
193  this->TextureObject = nullptr;
194  }
195 
196  delete[] this->Table;
197  }
198 
201 
204  float* Table;
206  double LastRange[2];
207 
208 private:
210  const vtkOpenGLVolumeGradientOpacityTable &) = delete;
212  const vtkOpenGLVolumeGradientOpacityTable&) = delete;
213 };
214 
217 {
218 public:
220 
221  //--------------------------------------------------------------------------
222  void Create(unsigned int numberOfTables)
223  {
224  this->Tables.reserve(static_cast<size_t>(numberOfTables));
225 
226  for (unsigned int i = 0; i < numberOfTables; i++)
227  {
230  this->Tables.push_back(table);
231  }
232  }
233 
234  //--------------------------------------------------------------------------
236  {
237  size_t const size = this->Tables.size();
238  for (size_t i = 0; i < size; i++)
239  {
240  this->Tables[i]->Delete();
241  }
242  }
243 
244  // Get opacity table at a given index.
245  //--------------------------------------------------------------------------
247  {
248  if (i >= this->Tables.size())
249  {
250  return nullptr;
251  }
252  return this->Tables[i];
253  }
254 
255  // Get number of tables.
256  //--------------------------------------------------------------------------
258  {
259  return this->Tables.size();
260  }
261 
262  //--------------------------------------------------------------------------
264  {
265  size_t const size = this->Tables.size();
266  for (size_t i = 0; i < size; ++i)
267  {
268  this->Tables[i]->ReleaseGraphicsResources(window);
269  }
270  }
271 protected:
273 
274 private:
275  std::vector<vtkOpenGLVolumeGradientOpacityTable*> Tables;
276 
278  const vtkOpenGLVolumeGradientOpacityTables &other) = delete;
280  const vtkOpenGLVolumeGradientOpacityTables &other) = delete;
281 };
282 
283 #endif
284 #endif // vtkOpenGLVolumeGradientOpacityTable_h
285 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeGradientOpacityTable.h
OpenGL rendering window.
abstract base class for most VTK objects
Definition: vtkObject.h:59
Defines a 1D piecewise function.
static vtkOpenGLVolumeGradientOpacityTable * New()
record modification and/or execution time
Definition: vtkTimeStamp.h:35
void Modified()
Set this objects time to the current time.
int GetMaximumSupportedTextureWidth(vtkOpenGLRenderWindow *renWin, int idealWidth)
void GetTable(double x1, double x2, int size, float *table, int stride=1, int logIncrements=0)
Fills in an array of function values evaluated at regular intervals.
void Activate()
Activate and Bind the texture.
bool Create2DFromRaw(unsigned int width, unsigned int height, int numComps, int dataType, void *data)
Create a 2D texture from client memory numComps must be in [1-4].
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
void Update(vtkPiecewiseFunction *gradientOpacity, double sampleDistance, double range[2], double vtkNotUsed(unitDistance), int filterValue, vtkOpenGLRenderWindow *renWin)
#define VTK_FLOAT
Definition: vtkType.h:58
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1330
virtual void SetMinificationFilter(int)
Minification filter mode.
virtual vtkMTimeType GetMTime()
Return this object&#39;s modified time.
virtual unsigned int GetHandle()
Returns the OpenGL handle.
abstracts an OpenGL texture object.
int EstimateMinNumberOfSamples(double const &x1, double const &x2)
Estimates the minimum size of a table such that it would correctly sample this function.
static int GetMaximumTextureSize(vtkOpenGLRenderWindow *context)
Query and return maximum texture size (dimension) supported by the OpenGL driver for a particular con...
void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind the texture.
void Deactivate()
Deactivate and UnBind the texture.
static vtkTextureObject * New()
virtual void SetWrapS(int)
Wrap mode for the first texture coordinate "s" Valid values are:
vtkMTimeType GetMTime() override
Data objects are composite objects and need to check each part for MTime.
int GetTextureUnit()
Return the texture unit used for this texture.
vtkOpenGLVolumeGradientOpacityTable * GetTable(unsigned int i)
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1368
virtual void Delete()
Delete a VTK object.
void SetContext(vtkOpenGLRenderWindow *)
Get/Set the context.
virtual void SetMagnificationFilter(int)
Magnification filter mode.