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 
19 #include <vtkDataArray.h>
20 #include <vtkImageData.h>
21 #include <vtkImageResize.h>
22 #include <vtkMath.h>
23 #include <vtkNew.h>
24 #include <vtkObjectFactory.h>
25 #include <vtkPointData.h>
26 #include <vtkTextureObject.h>
27 #include <vtk_glew.h>
28 
29 
41 {
42 public:
43 
45 
46  //--------------------------------------------------------------------------
47  void Activate()
48  {
49  if (!this->TextureObject)
50  {
51  return;
52  }
53  this->TextureObject->Activate();
54  };
55 
56  //--------------------------------------------------------------------------
57  void Deactivate()
58  {
59  if (!this->TextureObject)
60  {
61  return;
62  }
63  this->TextureObject->Deactivate();
64  };
65 
66  //--------------------------------------------------------------------------
67  void Update(vtkImageData* transfer2D, int interpolation,
68  vtkOpenGLRenderWindow* renWin)
69  {
70  if (!this->TextureObject)
71  {
73  }
74  this->TextureObject->SetContext(renWin);
75 
76  // Reload texture
77  if (transfer2D->GetMTime() > this->BuildTime ||
78  this->TextureObject->GetMTime() > this->BuildTime ||
79  !this->TextureObject->GetHandle())
80  {
81  int* dims = transfer2D->GetDimensions();
82  int const width = this->GetMaximumSupportedTextureWidth(renWin, dims[0]);
83  int const height = this->GetMaximumSupportedTextureWidth(renWin, dims[1]);
84 
85  // Resample if there is a size restriction
86  void* data = transfer2D->GetPointData()->GetScalars()->GetVoidPointer(0);
87  if (dims[0] != width || dims[1] != height)
88  {
89  this->ResizeFilter->SetInputData(transfer2D);
91  this->ResizeFilter->SetOutputDimensions(width, height, 1);
92  this->ResizeFilter->Update();
93  data = this->ResizeFilter->GetOutput()->GetPointData()->GetScalars(
94  )->GetVoidPointer(0);
95  }
96 
99  this->TextureObject->SetMagnificationFilter(interpolation);
100  this->TextureObject->SetMinificationFilter(interpolation);
101  this->TextureObject->Create2DFromRaw(width, height, 4, VTK_FLOAT,
102  data);
103  this->LastInterpolation = interpolation;
104  this->BuildTime.Modified();
105  }
106 
107  // Update filtering
108  if (this->LastInterpolation != interpolation)
109  {
110  this->LastInterpolation = interpolation;
111  this->TextureObject->SetMagnificationFilter(interpolation);
112  this->TextureObject->SetMinificationFilter(interpolation);
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 256;
133  }
134 
135  if (maxWidth >= idealWidth)
136  {
137  idealWidth = vtkMath::Max(256, 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  //--------------------------------------------------------------------------
149  int GetTextureUnit(void)
150  {
151  if (!this->TextureObject)
152  {
153  return -1;
154  }
155  return this->TextureObject->GetTextureUnit();
156  }
157 
158  //--------------------------------------------------------------------------
160  {
161  if (this->TextureObject)
162  {
164  this->TextureObject->Delete();
165  this->TextureObject = nullptr;
166  }
167  }
168 
169 protected:
170 
171  //--------------------------------------------------------------------------
173  : vtkObject()
174  , TextureObject(nullptr)
175  , LastInterpolation(-1)
176  {
177  }
178 
179  //--------------------------------------------------------------------------
181  {
182  if (this->TextureObject)
183  {
184  this->TextureObject->Delete();
185  this->TextureObject = nullptr;
186  }
187  }
188 
193 
194 private:
196  = delete;
198  = delete;
199 };
200 
202 
204 
216 {
217 public:
218  //--------------------------------------------------------------------------
219  vtkOpenGLTransferFunctions2D(unsigned int numberOfTables)
220  {
221  this->Tables.reserve(static_cast<size_t>(numberOfTables));
222 
223  for (unsigned int i = 0; i < numberOfTables; i++)
224  {
226  this->Tables.push_back(table);
227  }
228  }
229 
230  //--------------------------------------------------------------------------
232  {
233  size_t const size = this->Tables.size();
234  for (size_t i = 0; i < size; i++)
235  {
236  this->Tables[i]->Delete();
237  }
238  }
239 
240  //--------------------------------------------------------------------------
242  {
243  if (i >= this->Tables.size())
244  {
245  return nullptr;
246  }
247  return this->Tables[i];
248  }
249 
250  //--------------------------------------------------------------------------
252  {
253  return this->Tables.size();
254  }
255 
256  //--------------------------------------------------------------------------
258  {
259  size_t const size = this->Tables.size();
260  for (size_t i = 0; i < size; ++i)
261  {
262  this->Tables[i]->ReleaseGraphicsResources(window);
263  }
264  }
265 
266 private:
267  vtkOpenGLTransferFunctions2D() = delete;
269  = delete;
271  = delete;
272 
273  std::vector<vtkOpenGLTransferFunction2D*> Tables;
274 };
275 
276 #endif // vtkOpenGLTransferFunction2D_h
277 // VTK-HeaderTest-Exclude: vtkOpenGLTransferFunction2D.h
OpenGL rendering window.
static vtkOpenGLTransferFunction2D * New()
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.
void SetContext(vtkRenderWindow *)
Get/Set the context.
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.
vtkStandardNewMacro(vtkOpenGLTransferFunction2D)
void Deactivate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
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'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:1273
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'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.
void Activate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
virtual void Update(int port)
Bring this algorithm'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 SetResizeMethodToOutputDimensions()
The resizing method to use.
static vtkTextureObject * New()
virtual void SetWrapS(int)
Wrap mode for the first texture coordinate "s" Valid values are:
vtkOpenGLTransferFunctions2D(unsigned int numberOfTables)
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 arugments provided.
Definition: vtkMath.h:1311
virtual void Delete()
Delete a VTK object.
virtual void SetMagnificationFilter(int)
Magnification filter mode.