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