VTK
vtkOpenGLVolumeRGBTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLVolumeRGBTable.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 vtkOpenGLVolumeRGBTable_h
17 #define vtkOpenGLVolumeRGBTable_h
18 #ifndef __VTK_WRAP__
19 
20 #include <vtkObjectFactory.h>
22 #include <vtkTextureObject.h>
23 #include <vtk_glew.h>
24 #include <vtkMath.h>
25 
26 
27 //----------------------------------------------------------------------------
29 {
30 public:
31 
32  static vtkOpenGLVolumeRGBTable* New();
33 
34  // Activate texture.
35  //--------------------------------------------------------------------------
36  void Activate()
37  {
38  if (!this->TextureObject)
39  {
40  return;
41  }
42  this->TextureObject->Activate();
43  }
44 
45  // Deactivate texture.
46  //--------------------------------------------------------------------------
47  void Deactivate()
48  {
49  if (!this->TextureObject)
50  {
51  return;
52  }
53  this->TextureObject->Deactivate();
54  }
55 
56  // Update color transfer function texture.
57  //--------------------------------------------------------------------------
59  double range[2],
60  int filterValue,
61  vtkOpenGLRenderWindow* renWin)
62  {
63  bool needUpdate = false;
64 
65  if (!this->TextureObject)
66  {
68  }
69 
70  this->TextureObject->SetContext(renWin);
71 
72  if (range[0] != this->LastRange[0] || range[1] != this->LastRange[1])
73  {
74  this->LastRange[0] = range[0];
75  this->LastRange[1] = range[1];
76  needUpdate = true;
77  }
78 
79  if (scalarRGB->GetMTime() > this->BuildTime ||
80  this->TextureObject->GetMTime() > this->BuildTime ||
81  needUpdate || !this->TextureObject->GetHandle())
82  {
83  int const idealW = scalarRGB->EstimateMinNumberOfSamples(this->LastRange[0],
84  this->LastRange[1]);
85  int const newWidth = this->GetMaximumSupportedTextureWidth(renWin, idealW);
86 
87  if(this->Table == nullptr || this->TextureWidth != newWidth)
88  {
89  this->TextureWidth = newWidth;
90  delete [] this->Table;
91  this->Table = new float[this->TextureWidth *
93  }
94 
95  scalarRGB->GetTable(this->LastRange[0], this->LastRange[1],
96  this->TextureWidth, this->Table);
99  this->TextureObject->SetMagnificationFilter(filterValue);
100  this->TextureObject->SetMinificationFilter(filterValue);
103  VTK_FLOAT,
104  this->Table);
105  this->LastInterpolation = filterValue;
106  this->BuildTime.Modified();
107  }
108 
109  if (this->LastInterpolation != filterValue)
110  {
111  this->LastInterpolation = filterValue;
112  this->TextureObject->SetMagnificationFilter(filterValue);
113  this->TextureObject->SetMinificationFilter(filterValue);
114  }
115  }
116 
117  //--------------------------------------------------------------------------
119  int idealWidth)
120  {
121  if (!this->TextureObject)
122  {
123  vtkErrorMacro("vtkTextureObject not initialized!");
124  return -1;
125  }
126 
127  // Try to match the next power of two.
128  idealWidth = vtkMath::NearestPowerOfTwo(idealWidth);
129  int const maxWidth = this->TextureObject->GetMaximumTextureSize(renWin);
130  if (maxWidth < 0)
131  {
132  vtkErrorMacro("Failed to query max texture size! using default 1024.");
133  return 1024;
134  }
135 
136  if (maxWidth >= idealWidth)
137  {
138  idealWidth = vtkMath::Max(1024, idealWidth);
139  return idealWidth;
140  }
141 
142  vtkWarningMacro("This OpenGL implementation does not support the required "
143  "texture size of " << idealWidth << ", falling back to maximum allowed, "
144  << maxWidth << "." << "This may cause an incorrect color table mapping.");
145 
146  return maxWidth;
147  }
148 
149  // Get the texture unit
150  //--------------------------------------------------------------------------
151  int GetTextureUnit(void)
152  {
153  if (!this->TextureObject)
154  {
155  return -1;
156  }
157  return this->TextureObject->GetTextureUnit();
158  }
159 
160  //--------------------------------------------------------------------------
162  {
163  if (this->TextureObject)
164  {
166  this->TextureObject->Delete();
167  this->TextureObject = nullptr;
168  }
169  }
170 
171 protected:
172 
173  //--------------------------------------------------------------------------
175  {
176  this->TextureWidth = 1024;
177  this->NumberOfColorComponents = 3;
178  this->TextureObject = nullptr;
179  this->LastInterpolation = -1;
180  this->LastRange[0] = this->LastRange[1] = 0;
181  this->Table = nullptr;
182  }
183 
184  //--------------------------------------------------------------------------
186  {
187  if (this->TextureObject)
188  {
189  this->TextureObject->Delete();
190  this->TextureObject = nullptr;
191  }
192 
193  delete[] this->Table;
194  }
195 
196 
199 
201 
203  double LastRange[2];
204  float* Table;
206 
207 private:
209  = delete;
211  = delete;
212 };
213 
216 {
217 public:
218  static vtkOpenGLVolumeRGBTables* New();
219 
220  //--------------------------------------------------------------------------
221  void Create(unsigned int numberOfTables)
222  {
223  this->Tables.reserve(static_cast<size_t>(numberOfTables));
224 
225  for (unsigned int i = 0; i < numberOfTables; i++)
226  {
228  this->Tables.push_back(table);
229  }
230  }
231 
232  //--------------------------------------------------------------------------
234  {
235  size_t const size = this->Tables.size();
236  for (size_t i = 0; i < size; i++)
237  {
238  this->Tables[i]->Delete();
239  }
240  }
241 
242  // brief Get opacity table at a given index.
243  //--------------------------------------------------------------------------
245  {
246  if (i >= this->Tables.size())
247  {
248  return nullptr;
249  }
250  return this->Tables[i];
251  }
252 
253  // Get number of opacity tables.
254  //--------------------------------------------------------------------------
256  {
257  return this->Tables.size();
258  }
259 
260  //--------------------------------------------------------------------------
262  {
263  size_t const size = this->Tables.size();
264  for (size_t i = 0; i < size; ++i)
265  {
266  this->Tables[i]->ReleaseGraphicsResources(window);
267  }
268  }
269 
270 protected:
271  vtkOpenGLVolumeRGBTables() = default;
272 
273 private:
274  std::vector<vtkOpenGLVolumeRGBTable*> Tables;
275 
277  const vtkOpenGLVolumeRGBTables &other) = delete;
278 
279  vtkOpenGLVolumeRGBTables &operator=(
280  const vtkOpenGLVolumeRGBTables &other) = delete;
281 };
282 
283 #endif
284 #endif // vtkOpenGLVolumeRGBTable_h
285 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeRGBTable.h
OpenGL rendering window.
void GetTable(double x1, double x2, int n, double *table)
Fills in a table of n colors mapped from values mapped with even spacing between x1 and x2...
int GetMaximumSupportedTextureWidth(vtkOpenGLRenderWindow *renWin, int idealWidth)
abstract base class for most VTK objects
Definition: vtkObject.h:59
void ReleaseGraphicsResources(vtkWindow *window)
record modification and/or execution time
Definition: vtkTimeStamp.h:35
void Modified()
Set this objects time to the current time.
void Update(vtkColorTransferFunction *scalarRGB, double range[2], int filterValue, vtkOpenGLRenderWindow *renWin)
void Activate()
Activate and Bind the texture.
void Create(unsigned int numberOfTables)
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
#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.
vtkOpenGLVolumeRGBTable * GetTable(unsigned int i)
int EstimateMinNumberOfSamples(double const &x1, double const &x2)
Estimates the minimum size of a table such that it would correctly sample this function.
abstracts an OpenGL texture object.
static int GetMaximumTextureSize(vtkOpenGLRenderWindow *context)
Query and return maximum texture size (dimension) supported by the OpenGL driver for a particular con...
Defines a transfer function for mapping a property to an RGB color value.
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:
void ReleaseGraphicsResources(vtkWindow *window)
static vtkOpenGLVolumeRGBTable * New()
int GetTextureUnit()
Return the texture unit used for this texture.
virtual void SetWrapT(int)
Wrap mode for the first texture coordinate "t" Valid values are:
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.