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 
19 #include <vtkObjectFactory.h>
21 #include <vtkTextureObject.h>
22 #include <vtk_glew.h>
23 #include <vtkMath.h>
24 
25 
26 //----------------------------------------------------------------------------
28 {
29 public:
30 
31  static vtkOpenGLVolumeRGBTable* New();
32 
33  // Activate texture.
34  //--------------------------------------------------------------------------
35  void Activate()
36  {
37  if (!this->TextureObject)
38  {
39  return;
40  }
41  this->TextureObject->Activate();
42  }
43 
44  // Deactivate texture.
45  //--------------------------------------------------------------------------
46  void Deactivate()
47  {
48  if (!this->TextureObject)
49  {
50  return;
51  }
52  this->TextureObject->Deactivate();
53  }
54 
55  // Update color transfer function texture.
56  //--------------------------------------------------------------------------
58  double range[2],
59  int filterValue,
60  vtkOpenGLRenderWindow* renWin)
61  {
62  bool needUpdate = false;
63 
64  if (!this->TextureObject)
65  {
67  }
68 
69  this->TextureObject->SetContext(renWin);
70 
71  if (range[0] != this->LastRange[0] || range[1] != this->LastRange[1])
72  {
73  this->LastRange[0] = range[0];
74  this->LastRange[1] = range[1];
75  needUpdate = true;
76  }
77 
78  if (scalarRGB->GetMTime() > this->BuildTime ||
79  this->TextureObject->GetMTime() > this->BuildTime ||
80  needUpdate || !this->TextureObject->GetHandle())
81  {
82  int const idealW = scalarRGB->EstimateMinNumberOfSamples(this->LastRange[0],
83  this->LastRange[1]);
84  int const newWidth = this->GetMaximumSupportedTextureWidth(renWin, idealW);
85 
86  if(this->Table == nullptr || this->TextureWidth != newWidth)
87  {
88  this->TextureWidth = newWidth;
89  delete [] this->Table;
90  this->Table = new float[this->TextureWidth *
92  }
93 
94  scalarRGB->GetTable(this->LastRange[0], this->LastRange[1],
95  this->TextureWidth, this->Table);
98  this->TextureObject->SetMagnificationFilter(filterValue);
99  this->TextureObject->SetMinificationFilter(filterValue);
102  VTK_FLOAT,
103  this->Table);
104  this->LastInterpolation = filterValue;
105  this->BuildTime.Modified();
106  }
107 
108  if (this->LastInterpolation != filterValue)
109  {
110  this->LastInterpolation = filterValue;
111  this->TextureObject->SetMagnificationFilter(filterValue);
112  this->TextureObject->SetMinificationFilter(filterValue);
113  }
114  }
115 
116  //--------------------------------------------------------------------------
118  int idealWidth)
119  {
120  if (!this->TextureObject)
121  {
122  vtkErrorMacro("vtkTextureObject not initialized!");
123  return -1;
124  }
125 
126  // Try to match the next power of two.
127  idealWidth = vtkMath::NearestPowerOfTwo(idealWidth);
128  int const maxWidth = this->TextureObject->GetMaximumTextureSize(renWin);
129  if (maxWidth < 0)
130  {
131  vtkErrorMacro("Failed to query max texture size! using default 1024.");
132  return 1024;
133  }
134 
135  if (maxWidth >= idealWidth)
136  {
137  idealWidth = vtkMath::Max(1024, idealWidth);
138  return idealWidth;
139  }
140 
141  vtkWarningMacro("This OpenGL implementation does not support the required "
142  "texture size of " << idealWidth << ", falling back to maximum allowed, "
143  << maxWidth << "." << "This may cause an incorrect color table mapping.");
144 
145  return maxWidth;
146  }
147 
148  // Get the texture unit
149  //--------------------------------------------------------------------------
150  int GetTextureUnit(void)
151  {
152  if (!this->TextureObject)
153  {
154  return -1;
155  }
156  return this->TextureObject->GetTextureUnit();
157  }
158 
159  //--------------------------------------------------------------------------
161  {
162  if (this->TextureObject)
163  {
165  this->TextureObject->Delete();
166  this->TextureObject = nullptr;
167  }
168  }
169 
170 protected:
171 
172  //--------------------------------------------------------------------------
174  {
175  this->TextureWidth = 1024;
176  this->NumberOfColorComponents = 3;
177  this->TextureObject = nullptr;
178  this->LastInterpolation = -1;
179  this->LastRange[0] = this->LastRange[1] = 0;
180  this->Table = nullptr;
181  }
182 
183  //--------------------------------------------------------------------------
185  {
186  if (this->TextureObject)
187  {
188  this->TextureObject->Delete();
189  this->TextureObject = nullptr;
190  }
191 
192  delete[] this->Table;
193  }
194 
195 
198 
200 
202  double LastRange[2];
203  float* Table;
205 
206 private:
208  = delete;
210  = delete;
211 };
212 
214 
215 
218 {
219 public:
220  //--------------------------------------------------------------------------
221  vtkOpenGLVolumeRGBTables(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 private:
271  std::vector<vtkOpenGLVolumeRGBTable*> Tables;
272 
273  vtkOpenGLVolumeRGBTables() = delete;
274 
275  vtkOpenGLVolumeRGBTables(const vtkOpenGLVolumeRGBTables &other) = delete;
276 
277  vtkOpenGLVolumeRGBTables &operator=(const vtkOpenGLVolumeRGBTables &other) = delete;
278 };
279 
280 #endif // vtkOpenGLVolumeRGBTable_h
281 // 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 SetContext(vtkRenderWindow *)
Get/Set the context.
vtkStandardNewMacro(vtkOpenGLVolumeRGBTable)
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 Deactivate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
void Update(vtkColorTransferFunction *scalarRGB, double range[2], int filterValue, vtkOpenGLRenderWindow *renWin)
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:1273
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.
void Activate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
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.
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:
vtkOpenGLVolumeRGBTables(unsigned int numberOfTables)
static T Max(const T &a, const T &b)
Returns the maximum of the two arugments provided.
Definition: vtkMath.h:1311
virtual void Delete()
Delete a VTK object.
virtual void SetMagnificationFilter(int)
Magnification filter mode.