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
vtkOpenGLTransferFunction2D::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *window)
Definition: vtkOpenGLTransferFunction2D.h:160
vtkImageData.h
vtkTextureObject::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind the texture.
vtkAlgorithm::Update
virtual void Update(int port)
Bring this algorithm's outputs up-to-date.
vtkOpenGLTransferFunctions2D::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *window)
Definition: vtkOpenGLTransferFunction2D.h:258
vtkMath::NearestPowerOfTwo
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1330
vtkTextureObject::SetMinificationFilter
virtual void SetMinificationFilter(int)
vtkOpenGLTransferFunction2D::New
static vtkOpenGLTransferFunction2D * New()
vtkMath.h
vtkX3D::data
Definition: vtkX3D.h:315
vtkMath::Max
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1368
vtkTextureObject::SetWrapS
virtual void SetWrapS(int)
vtkOpenGLTransferFunction2D::ResizeFilter
vtkNew< vtkImageResize > ResizeFilter
Definition: vtkOpenGLTransferFunction2D.h:190
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:35
vtkTextureObject::SetContext
void SetContext(vtkOpenGLRenderWindow *)
Get/Set the context.
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkOpenGLTransferFunction2D::GetMaximumSupportedTextureWidth
int GetMaximumSupportedTextureWidth(vtkOpenGLRenderWindow *renWin, int idealWidth)
Definition: vtkOpenGLTransferFunction2D.h:118
vtkTextureObject::GetMaximumTextureSize
static int GetMaximumTextureSize(vtkOpenGLRenderWindow *context)
Query and return maximum texture size (dimension) supported by the OpenGL driver for a particular con...
vtkOpenGLTransferFunction2D::LastInterpolation
int LastInterpolation
Definition: vtkOpenGLTransferFunction2D.h:192
vtkObjectFactory.h
vtkImageResize::SetOutputDimensions
virtual void SetOutputDimensions(int, int, int)
The desired output dimensions.
vtkObjectBase::Delete
virtual void Delete()
Delete a VTK object.
vtkImageResize.h
vtkImageData::GetDimensions
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
vtkTextureObject::GetTextureUnit
int GetTextureUnit()
Return the texture unit used for this texture.
vtkOpenGLTransferFunction2D::BuildTime
vtkTimeStamp BuildTime
Definition: vtkOpenGLTransferFunction2D.h:193
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
vtkOpenGLTransferFunction2D::~vtkOpenGLTransferFunction2D
~vtkOpenGLTransferFunction2D() override
Definition: vtkOpenGLTransferFunction2D.h:181
vtkAbstractArray::GetVoidPointer
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
vtkOpenGLTransferFunction2D::Update
void Update(vtkImageData *transfer2D, int interpolation, vtkOpenGLRenderWindow *renWin)
Definition: vtkOpenGLTransferFunction2D.h:68
vtkOpenGLTransferFunction2D::vtkOpenGLTransferFunction2D
vtkOpenGLTransferFunction2D()
Definition: vtkOpenGLTransferFunction2D.h:173
vtkImageAlgorithm::SetInputData
void SetInputData(vtkDataObject *)
Assign a data object as input.
vtkTextureObject::SetWrapT
virtual void SetWrapT(int)
vtkOpenGLTransferFunction2D::TextureObject
vtkTextureObject * TextureObject
Definition: vtkOpenGLTransferFunction2D.h:191
vtkTimeStamp::Modified
void Modified()
Set this objects time to the current time.
vtkX3D::height
Definition: vtkX3D.h:254
vtkOpenGLTransferFunctions2D::Create
void Create(unsigned int numberOfTables)
Definition: vtkOpenGLTransferFunction2D.h:220
vtkObject::GetMTime
virtual vtkMTimeType GetMTime()
Return this object's modified time.
vtkDataSet::GetMTime
vtkMTimeType GetMTime() override
Datasets are composite objects and need to check each part for MTime THIS METHOD IS THREAD SAFE.
vtkDataSet::GetPointData
vtkPointData * GetPointData()
Return a pointer to this dataset's point data.
Definition: vtkDataSet.h:261
vtkPointData.h
vtkImageAlgorithm::GetOutput
vtkImageData * GetOutput()
Get the output data object for a port on this algorithm.
vtkTextureObject::Deactivate
void Deactivate()
Deactivate and UnBind the texture.
vtkImageData
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:44
vtkTextureObject.h
vtkTextureObject::New
static vtkTextureObject * New()
VTK_FLOAT
#define VTK_FLOAT
Definition: vtkType.h:58
vtkImageResize::SetResizeMethodToOutputDimensions
void SetResizeMethodToOutputDimensions()
Definition: vtkImageResize.h:68
vtkX3D::size
Definition: vtkX3D.h:253
vtkOpenGLTransferFunction2D::Deactivate
void Deactivate()
Definition: vtkOpenGLTransferFunction2D.h:58
vtkNew< vtkImageResize >
vtkDataSetAttributes::GetScalars
vtkDataArray * GetScalars()
vtkTextureObject::SetMagnificationFilter
virtual void SetMagnificationFilter(int)
vtkOpenGLTransferFunctions2D::~vtkOpenGLTransferFunctions2D
~vtkOpenGLTransferFunctions2D()
Definition: vtkOpenGLTransferFunction2D.h:232
vtkOpenGLTransferFunctions2D::vtkOpenGLTransferFunctions2D
vtkOpenGLTransferFunctions2D()=default
vtkOpenGLTransferFunctions2D::New
static vtkOpenGLTransferFunctions2D * New()
vtkDataArray.h
vtkTextureObject::GetHandle
virtual unsigned int GetHandle()
Returns the OpenGL handle.
vtkNew.h
vtkTextureObject::ClampToEdge
Definition: vtkTextureObject.h:67
vtkOpenGLTransferFunction2D
2D Transfer function container.
Definition: vtkOpenGLTransferFunction2D.h:41
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:53
vtkOpenGLTransferFunction2D::GetTextureUnit
int GetTextureUnit(void)
Definition: vtkOpenGLTransferFunction2D.h:150
vtkOpenGLTransferFunctions2D::GetTable
vtkOpenGLTransferFunction2D * GetTable(unsigned int i)
Definition: vtkOpenGLTransferFunction2D.h:242
vtkOpenGLTransferFunction2D::Activate
void Activate()
Definition: vtkOpenGLTransferFunction2D.h:48
vtkTextureObject::Create2DFromRaw
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].
vtkOpenGLTransferFunctions2D::GetNumberOfTables
size_t GetNumberOfTables()
Definition: vtkOpenGLTransferFunction2D.h:252
vtkOpenGLTransferFunctions2D
Container for a set of TransferFunction2D instances.
Definition: vtkOpenGLTransferFunction2D.h:214
vtkTextureObject::Activate
void Activate()
Activate and Bind the texture.