VTK  9.0.1
vtkVolumeMask.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVolumeMask.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 vtkVolumeMask_h
17 #define vtkVolumeMask_h
18 
19 #include <vtkDataArray.h>
20 #include <vtkImageData.h>
21 #include <vtkOpenGLRenderWindow.h>
22 #include <vtkRenderWindow.h>
23 #include <vtkRenderer.h>
24 #include <vtkTextureObject.h>
25 
26 #include <map> // STL required
27 
28 //----------------------------------------------------------------------------
30 {
31 public:
32  //--------------------------------------------------------------------------
34  {
35  this->Texture = nullptr;
36  this->Loaded = false;
37  this->LoadedExtent[0] = VTK_INT_MAX;
38  this->LoadedExtent[1] = VTK_INT_MIN;
39  this->LoadedExtent[2] = VTK_INT_MAX;
40  this->LoadedExtent[3] = VTK_INT_MIN;
41  this->LoadedExtent[4] = VTK_INT_MAX;
42  this->LoadedExtent[5] = VTK_INT_MIN;
43  }
44 
45  //--------------------------------------------------------------------------
47  {
48  if (this->Texture)
49  {
50  this->Texture->Delete();
51  this->Texture = nullptr;
52  }
53  }
54 
55  //--------------------------------------------------------------------------
56  vtkTimeStamp GetBuildTime() { return this->BuildTime; }
57 
58  //--------------------------------------------------------------------------
59  void Activate() { this->Texture->Activate(); }
60 
61  //--------------------------------------------------------------------------
62  void Deactivate() { this->Texture->Deactivate(); }
63 
64  //--------------------------------------------------------------------------
65  void Update(vtkRenderer* ren, vtkImageData* input, int cellFlag, int textureExtent[6],
66  int scalarMode, int arrayAccessMode, int arrayId, const char* arrayName,
67  vtkIdType maxMemoryInBytes)
68  {
69  bool needUpdate = false;
70  bool modified = false;
71 
72  if (!this->Texture)
73  {
75  needUpdate = true;
76  }
77 
79  auto ostate = renWin->GetState();
80  this->Texture->SetContext(renWin);
81 
82  if (!this->Texture->GetHandle())
83  {
84  needUpdate = true;
85  }
86 
87  int obsolete = needUpdate || !this->Loaded || input->GetMTime() > this->BuildTime;
88  if (!obsolete)
89  {
90  obsolete = cellFlag != this->LoadedCellFlag;
91  int i = 0;
92  while (!obsolete && i < 6)
93  {
94  obsolete = obsolete || this->LoadedExtent[i] > textureExtent[i];
95  ++i;
96  obsolete = obsolete || this->LoadedExtent[i] < textureExtent[i];
97  ++i;
98  }
99  }
100 
101  if (obsolete)
102  {
103  this->Loaded = false;
104  int dim[3];
105  input->GetDimensions(dim);
106 
108  input, scalarMode, arrayAccessMode, arrayId, arrayName, this->LoadedCellFlag);
109 
110  // DON'T USE GetScalarType() or GetNumberOfScalarComponents() on
111  // ImageData as it deals only with point data...
112  int scalarType = scalars->GetDataType();
113  if (scalarType != VTK_UNSIGNED_CHAR)
114  {
115  cout << "Mask should be VTK_UNSIGNED_CHAR." << endl;
116  }
117  if (scalars->GetNumberOfComponents() != 1)
118  {
119  cout << "Mask should be a one-component scalar field." << endl;
120  }
121 
122  GLint internalFormat = GL_R8;
123  GLenum format = GL_RED;
124  GLenum type = GL_UNSIGNED_BYTE;
125 
126  // Enough memory?
127  int textureSize[3];
128  int i = 0;
129  while (i < 3)
130  {
131  textureSize[i] = textureExtent[2 * i + 1] - textureExtent[2 * i] + 1;
132  ++i;
133  }
134 
135  GLint width;
136  glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &width);
137  this->Loaded = textureSize[0] <= width && textureSize[1] <= width && textureSize[2] <= width;
138  if (this->Loaded)
139  {
140  // so far, so good but some cards don't report allocation error
141  this->Loaded = textureSize[0] * textureSize[1] * textureSize[2] *
142  vtkAbstractArray::GetDataTypeSize(scalarType) * scalars->GetNumberOfComponents() <=
143  maxMemoryInBytes;
144  if (this->Loaded)
145  {
146  ostate->vtkglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
147 
148  if (!(textureExtent[1] - textureExtent[0] + cellFlag == dim[0]))
149  {
150  ostate->vtkglPixelStorei(GL_UNPACK_ROW_LENGTH, dim[0] - cellFlag);
151  }
152  if (!(textureExtent[3] - textureExtent[2] + cellFlag == dim[1]))
153  {
154  ostate->vtkglPixelStorei(GL_UNPACK_IMAGE_HEIGHT, dim[1] - cellFlag);
155  }
156  void* dataPtr = scalars->GetVoidPointer(
157  ((textureExtent[4] * (dim[1] - cellFlag) + textureExtent[2]) * (dim[0] - cellFlag) +
158  textureExtent[0]) *
159  scalars->GetNumberOfComponents());
160 
161  this->Texture->SetDataType(type);
162  this->Texture->SetFormat(format);
163  this->Texture->SetInternalFormat(internalFormat);
164  this->Texture->Create3DFromRaw(
165  textureSize[0], textureSize[1], textureSize[2], 1, scalarType, dataPtr);
171  this->Texture->SetBorderColor(0.0f, 0.0f, 0.0f, 0.0f);
172 
173  // Restore the default values.
174  ostate->vtkglPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
175  ostate->vtkglPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
176 
177  this->LoadedCellFlag = cellFlag;
178  i = 0;
179  while (i < 6)
180  {
181  this->LoadedExtent[i] = textureExtent[i];
182  ++i;
183  }
184 
185  double spacing[3];
186  double origin[3];
187  input->GetSpacing(spacing);
188  input->GetOrigin(origin);
189  int swapBounds[3];
190  swapBounds[0] = (spacing[0] < 0);
191  swapBounds[1] = (spacing[1] < 0);
192  swapBounds[2] = (spacing[2] < 0);
193 
194  if (!this->LoadedCellFlag) // loaded extents represent points
195  {
196  // slabsPoints[i]=(slabsDataSet[i] - origin[i/2]) / spacing[i/2];
197  // in general, x=o+i*spacing.
198  // if spacing is positive min extent match the min of the
199  // bounding box
200  // and the max extent match the max of the bounding box
201  // if spacing is negative min extent match the max of the
202  // bounding box
203  // and the max extent match the min of the bounding box
204 
205  // if spacing is negative, we may have to rethink the equation
206  // between real point and texture coordinate...
207  this->LoadedBounds[0] =
208  origin[0] + static_cast<double>(this->LoadedExtent[0 + swapBounds[0]]) * spacing[0];
209  this->LoadedBounds[2] =
210  origin[1] + static_cast<double>(this->LoadedExtent[2 + swapBounds[1]]) * spacing[1];
211  this->LoadedBounds[4] =
212  origin[2] + static_cast<double>(this->LoadedExtent[4 + swapBounds[2]]) * spacing[2];
213  this->LoadedBounds[1] =
214  origin[0] + static_cast<double>(this->LoadedExtent[1 - swapBounds[0]]) * spacing[0];
215  this->LoadedBounds[3] =
216  origin[1] + static_cast<double>(this->LoadedExtent[3 - swapBounds[1]]) * spacing[1];
217  this->LoadedBounds[5] =
218  origin[2] + static_cast<double>(this->LoadedExtent[5 - swapBounds[2]]) * spacing[2];
219  }
220  else // loaded extents represent cells
221  {
222  int wholeTextureExtent[6];
223  input->GetExtent(wholeTextureExtent);
224  i = 1;
225  while (i < 6)
226  {
227  wholeTextureExtent[i]--;
228  i += 2;
229  }
230 
231  i = 0;
232  while (i < 3)
233  {
234  if (this->LoadedExtent[2 * i] == wholeTextureExtent[2 * i])
235  {
236  this->LoadedBounds[2 * i + swapBounds[i]] = origin[i];
237  }
238  else
239  {
240  this->LoadedBounds[2 * i + swapBounds[i]] =
241  origin[i] + (static_cast<double>(this->LoadedExtent[2 * i]) + 0.5) * spacing[i];
242  }
243 
244  if (this->LoadedExtent[2 * i + 1] == wholeTextureExtent[2 * i + 1])
245  {
246  this->LoadedBounds[2 * i + 1 - swapBounds[i]] = origin[i] +
247  (static_cast<double>(this->LoadedExtent[2 * i + 1]) + 1.0) * spacing[i];
248  }
249  else
250  {
251  this->LoadedBounds[2 * i + 1 - swapBounds[i]] = origin[i] +
252  (static_cast<double>(this->LoadedExtent[2 * i + 1]) + 0.5) * spacing[i];
253  }
254  ++i;
255  }
256  }
257  modified = true;
258  }
259  }
260  }
261 
262  if (modified)
263  {
264  this->BuildTime.Modified();
265  }
266  }
267 
268  //--------------------------------------------------------------------------
269  double* GetLoadedBounds() { return this->LoadedBounds; }
270 
271  //--------------------------------------------------------------------------
273 
274  //--------------------------------------------------------------------------
275  int GetLoadedCellFlag() { return this->LoadedCellFlag; }
276 
277  //--------------------------------------------------------------------------
278  bool IsLoaded() { return this->Loaded; }
279 
280  // Get the texture unit
281  //--------------------------------------------------------------------------
282  int GetTextureUnit(void)
283  {
284  if (!this->Texture)
285  {
286  return -1;
287  }
288  return this->Texture->GetTextureUnit();
289  }
290 
291  //--------------------------------------------------------------------------
293  {
294  if (this->Texture)
295  {
296  this->Texture->ReleaseGraphicsResources(window);
297  this->Texture->Delete();
298  this->Texture = nullptr;
299  }
300  }
301 
302 protected:
305 
306  double LoadedBounds[6];
308 
310  bool Loaded;
311 };
312 
313 //----------------------------------------------------------------------------
315 {
316 public:
317  std::map<vtkImageData*, vtkVolumeMask*> Map;
319 
320 private:
322  vtkMapMaskTextureId& operator=(const vtkMapMaskTextureId& other);
323 };
324 
325 #endif // vtkVolumeMask_h
326 // VTK-HeaderTest-Exclude: vtkVolumeMask.h
vtkVolumeMask::~vtkVolumeMask
~vtkVolumeMask()
Definition: vtkVolumeMask.h:46
vtkImageData.h
vtkVolumeMask::GetLoadedCellFlag
int GetLoadedCellFlag()
Definition: vtkVolumeMask.h:275
vtkVolumeMask::vtkVolumeMask
vtkVolumeMask()
Definition: vtkVolumeMask.h:33
VTK_INT_MIN
#define VTK_INT_MIN
Definition: vtkType.h:154
vtkTextureObject::SetBorderColor
virtual void SetBorderColor(float, float, float, float)
Border Color (RGBA).
vtkVolumeMask::Texture
vtkTextureObject * Texture
Definition: vtkVolumeMask.h:303
VTK_INT_MAX
#define VTK_INT_MAX
Definition: vtkType.h:155
vtkMapMaskTextureId::vtkMapMaskTextureId
vtkMapMaskTextureId()
Definition: vtkVolumeMask.h:318
vtkVolumeMask::Activate
void Activate()
Definition: vtkVolumeMask.h:59
vtkAbstractMapper::GetScalars
static vtkDataArray * GetScalars(vtkDataSet *input, int scalarMode, int arrayAccessMode, int arrayId, const char *arrayName, int &cellFlag)
Internal helper function for getting the active scalars.
vtkTextureObject::Create3DFromRaw
bool Create3DFromRaw(unsigned int width, unsigned int height, unsigned int depth, int numComps, int dataType, void *data)
Create a 3D texture from client memory numComps must be in [1-4].
vtkMapMaskTextureId
Definition: vtkVolumeMask.h:314
vtkTextureObject::SetMinificationFilter
virtual void SetMinificationFilter(int)
vtkVolumeMask::Loaded
bool Loaded
Definition: vtkVolumeMask.h:310
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkIdType
int vtkIdType
Definition: vtkType.h:338
vtkTextureObject::SetWrapS
virtual void SetWrapS(int)
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkTextureObject::SetFormat
void SetFormat(unsigned int glFormat)
vtkAbstractArray::GetDataType
virtual int GetDataType() const =0
Return the underlying data type.
vtkRenderWindow.h
vtkTextureObject::SetContext
void SetContext(vtkOpenGLRenderWindow *)
Get/Set the context.
vtkRenderer.h
vtkTextureObject::SetInternalFormat
void SetInternalFormat(unsigned int glInternalFormat)
vtkObjectBase::Delete
virtual void Delete()
Delete a VTK object.
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
vtkImageData::GetDimensions
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
vtkTextureObject::GetTextureUnit
int GetTextureUnit()
Return the texture unit used for this texture.
vtkTextureObject::Activate
virtual void Activate()
Activate and Bind the texture.
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
vtkAbstractArray::GetVoidPointer
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
vtkTextureObject::Nearest
@ Nearest
Definition: vtkTextureObject.h:74
vtkImageData::GetExtent
virtual int * GetExtent()
vtkTextureObject::SetWrapT
virtual void SetWrapT(int)
vtkTimeStamp::Modified
void Modified()
Set this objects time to the current time.
vtkOpenGLRenderWindow::SafeDownCast
static vtkOpenGLRenderWindow * SafeDownCast(vtkObjectBase *o)
vtkVolumeMask::LoadedExtent
vtkIdType LoadedExtent[6]
Definition: vtkVolumeMask.h:307
vtkDataSet::GetMTime
vtkMTimeType GetMTime() override
Datasets are composite objects and need to check each part for MTime THIS METHOD IS THREAD SAFE.
vtkAbstractArray::GetNumberOfComponents
int GetNumberOfComponents() const
Definition: vtkAbstractArray.h:127
vtkVolumeMask::GetLoadedExtent
vtkIdType * GetLoadedExtent()
Definition: vtkVolumeMask.h:272
vtkTextureObject::Deactivate
void Deactivate()
Deactivate and UnBind the texture.
vtkImageData
topologically and geometrically regular array of data
Definition: vtkImageData.h:41
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:40
vtkTextureObject.h
vtkTextureObject::New
static vtkTextureObject * New()
VTK_UNSIGNED_CHAR
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:45
vtkOpenGLRenderWindow::GetState
virtual vtkOpenGLState * GetState()
Definition: vtkOpenGLRenderWindow.h:404
vtkTextureObject::ReleaseGraphicsResources
virtual void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind the texture.
vtkVolumeMask::GetLoadedBounds
double * GetLoadedBounds()
Definition: vtkVolumeMask.h:269
vtkVolumeMask::BuildTime
vtkTimeStamp BuildTime
Definition: vtkVolumeMask.h:304
vtkTextureObject::SetMagnificationFilter
virtual void SetMagnificationFilter(int)
vtkTextureObject::SetDataType
void SetDataType(unsigned int glType)
vtkX3D::spacing
@ spacing
Definition: vtkX3D.h:487
vtkVolumeMask
Definition: vtkVolumeMask.h:29
vtkImageData::GetOrigin
virtual double * GetOrigin()
Set/Get the origin of the dataset.
vtkDataArray.h
vtkVolumeMask::GetTextureUnit
int GetTextureUnit(void)
Definition: vtkVolumeMask.h:282
vtkTextureObject::SetWrapR
virtual void SetWrapR(int)
vtkTextureObject::GetHandle
virtual unsigned int GetHandle()
Returns the OpenGL handle.
vtkImageData::GetSpacing
virtual double * GetSpacing()
Set the spacing (width,height,length) of the cubical cells that compose the data set.
vtkRenderer
abstract specification for renderers
Definition: vtkRenderer.h:67
vtkAbstractArray::GetDataTypeSize
virtual int GetDataTypeSize() const =0
Return the size of the underlying data type.
vtkVolumeMask::GetBuildTime
vtkTimeStamp GetBuildTime()
Definition: vtkVolumeMask.h:56
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:51
vtkVolumeMask::LoadedBounds
double LoadedBounds[6]
Definition: vtkVolumeMask.h:306
vtkVolumeMask::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *window)
Definition: vtkVolumeMask.h:292
vtkTextureObject::ClampToEdge
@ ClampToEdge
Definition: vtkTextureObject.h:60
vtkRenderer::GetRenderWindow
vtkRenderWindow * GetRenderWindow()
Definition: vtkRenderer.h:423
vtkVolumeMask::LoadedCellFlag
int LoadedCellFlag
Definition: vtkVolumeMask.h:309
vtkVolumeMask::Deactivate
void Deactivate()
Definition: vtkVolumeMask.h:62
vtkMapMaskTextureId::Map
std::map< vtkImageData *, vtkVolumeMask * > Map
Definition: vtkVolumeMask.h:317
vtkVolumeMask::IsLoaded
bool IsLoaded()
Definition: vtkVolumeMask.h:278
vtkVolumeMask::Update
void Update(vtkRenderer *ren, vtkImageData *input, int cellFlag, int textureExtent[6], int scalarMode, int arrayAccessMode, int arrayId, const char *arrayName, vtkIdType maxMemoryInBytes)
Definition: vtkVolumeMask.h:65
vtkOpenGLRenderWindow.h