VTK
vtkOpenGLTransferFunction2D.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLTransferFunction2D.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 vtkOpenGLTransferFunction2D_h
17 #define vtkOpenGLTransferFunction2D_h
18 #ifndef __VTK_WRAP__
19 
20 #include <vtkDataArray.h>
21 #include <vtkImageData.h>
22 #include <vtkImageResize.h>
23 #include <vtkMath.h>
24 #include <vtkNew.h>
25 #include <vtkObjectFactory.h>
26 #include <vtkPointData.h>
27 #include <vtkTextureObject.h>
28 #include <vtk_glew.h>
29 
30 
42 {
43 public:
44 
46 
47  //--------------------------------------------------------------------------
48  void Activate()
49  {
50  if (!this->TextureObject)
51  {
52  return;
53  }
54  this->TextureObject->Activate();
55  };
56 
57  //--------------------------------------------------------------------------
58  void Deactivate()
59  {
60  if (!this->TextureObject)
61  {
62  return;
63  }
64  this->TextureObject->Deactivate();
65  };
66 
67  //--------------------------------------------------------------------------
68  void Update(vtkImageData* transfer2D, int interpolation,
69  vtkOpenGLRenderWindow* renWin)
70  {
71  if (!this->TextureObject)
72  {
74  }
75  this->TextureObject->SetContext(renWin);
76 
77  // Reload texture
78  if (transfer2D->GetMTime() > this->BuildTime ||
79  this->TextureObject->GetMTime() > this->BuildTime ||
80  !this->TextureObject->GetHandle())
81  {
82  int* dims = transfer2D->GetDimensions();
83  int const width = this->GetMaximumSupportedTextureWidth(renWin, dims[0]);
84  int const height = this->GetMaximumSupportedTextureWidth(renWin, dims[1]);
85 
86  // Resample if there is a size restriction
87  void* data = transfer2D->GetPointData()->GetScalars()->GetVoidPointer(0);
88  if (dims[0] != width || dims[1] != height)
89  {
90  this->ResizeFilter->SetInputData(transfer2D);
92  this->ResizeFilter->SetOutputDimensions(width, height, 1);
93  this->ResizeFilter->Update();
94  data = this->ResizeFilter->GetOutput()->GetPointData()->GetScalars(
95  )->GetVoidPointer(0);
96  }
97 
100  this->TextureObject->SetMagnificationFilter(interpolation);
101  this->TextureObject->SetMinificationFilter(interpolation);
102  this->TextureObject->Create2DFromRaw(width, height, 4, VTK_FLOAT,
103  data);
104  this->LastInterpolation = interpolation;
105  this->BuildTime.Modified();
106  }
107 
108  // Update filtering
109  if (this->LastInterpolation != interpolation)
110  {
111  this->LastInterpolation = interpolation;
112  this->TextureObject->SetMagnificationFilter(interpolation);
113  this->TextureObject->SetMinificationFilter(interpolation);
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 256;
134  }
135 
136  if (maxWidth >= idealWidth)
137  {
138  idealWidth = vtkMath::Max(256, 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  //--------------------------------------------------------------------------
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  : vtkObject()
175  , TextureObject(nullptr)
176  , LastInterpolation(-1)
177  {
178  }
179 
180  //--------------------------------------------------------------------------
182  {
183  if (this->TextureObject)
184  {
185  this->TextureObject->Delete();
186  this->TextureObject = nullptr;
187  }
188  }
189 
194 
195 private:
197  const vtkOpenGLTransferFunction2D&) = delete;
198  vtkOpenGLTransferFunction2D& operator=(
199  const vtkOpenGLTransferFunction2D&) = delete;
200 };
201 
203 
215 {
216 public:
218 
219  //--------------------------------------------------------------------------
220  void Create(unsigned int numberOfTables)
221  {
222  this->Tables.reserve(static_cast<size_t>(numberOfTables));
223 
224  for (unsigned int i = 0; i < numberOfTables; i++)
225  {
227  this->Tables.push_back(table);
228  }
229  }
230 
231  //--------------------------------------------------------------------------
233  {
234  size_t const size = this->Tables.size();
235  for (size_t i = 0; i < size; i++)
236  {
237  this->Tables[i]->Delete();
238  }
239  }
240 
241  //--------------------------------------------------------------------------
243  {
244  if (i >= this->Tables.size())
245  {
246  return nullptr;
247  }
248  return this->Tables[i];
249  }
250 
251  //--------------------------------------------------------------------------
253  {
254  return this->Tables.size();
255  }
256 
257  //--------------------------------------------------------------------------
259  {
260  size_t const size = this->Tables.size();
261  for (size_t i = 0; i < size; ++i)
262  {
263  this->Tables[i]->ReleaseGraphicsResources(window);
264  }
265  }
266 protected:
267  vtkOpenGLTransferFunctions2D() = default;
268 
269 private:
271  const vtkOpenGLTransferFunctions2D& other) = delete;
272  vtkOpenGLTransferFunctions2D& operator=(
273  const vtkOpenGLTransferFunctions2D& other) = delete;
274 
275  std::vector<vtkOpenGLTransferFunction2D*> Tables;
276 };
277 
278 #endif
279 #endif // vtkOpenGLTransferFunction2D_h
280 // VTK-HeaderTest-Exclude: vtkOpenGLTransferFunction2D.h
OpenGL rendering window.
static vtkOpenGLTransferFunction2D * New()
void Create(unsigned int numberOfTables)
int GetMaximumSupportedTextureWidth(vtkOpenGLRenderWindow *renWin, int idealWidth)
virtual void SetOutputDimensions(int, int, int)
The desired output dimensions.
abstract base class for most VTK objects
Definition: vtkObject.h:59
Container for a set of TransferFunction2D instances.
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
record modification and/or execution time
Definition: vtkTimeStamp.h:35
void Modified()
Set this objects time to the current time.
void Activate()
Activate and Bind the texture.
void SetInputData(vtkDataObject *)
Assign a data object as input.
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].
vtkImageData * GetOutput()
Get the output data object for a port on this algorithm.
vtkPointData * GetPointData()
Return a pointer to this dataset&#39;s point data.
Definition: vtkDataSet.h:261
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
#define VTK_FLOAT
Definition: vtkType.h:58
vtkOpenGLTransferFunction2D * GetTable(unsigned int i)
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1330
vtkDataArray * GetScalars()
Set/Get the scalar data.
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
2D Transfer function container.
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
void ReleaseGraphicsResources(vtkWindow *window)
virtual void SetMinificationFilter(int)
Minification filter mode.
virtual vtkMTimeType GetMTime()
Return this object&#39;s modified time.
void ReleaseGraphicsResources(vtkWindow *window)
vtkMTimeType GetMTime() override
Datasets are composite objects and need to check each part for MTime THIS METHOD IS THREAD SAFE...
virtual unsigned int GetHandle()
Returns the OpenGL handle.
virtual void Update(int port)
Bring this algorithm&#39;s outputs up-to-date.
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...
void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind the texture.
void Deactivate()
Deactivate and UnBind the texture.
void SetResizeMethodToOutputDimensions()
The resizing method to use.
static vtkTextureObject * New()
virtual void SetWrapS(int)
Wrap mode for the first texture coordinate "s" Valid values are:
void Update(vtkImageData *transfer2D, int interpolation, vtkOpenGLRenderWindow *renWin)
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.