VTK
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 <vtkRenderer.h>
23 #include <vtkRenderWindow.h>
24 #include <vtkTextureObject.h>
25 
26 #include <map> // STL required
27 
28 //----------------------------------------------------------------------------
30 {
31 public:
32  //--------------------------------------------------------------------------
34  {
35  this->Texture = NULL;
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 = 0;
52  }
53  }
54 
55  //--------------------------------------------------------------------------
57  {
58  return this->BuildTime;
59  }
60 
61  //--------------------------------------------------------------------------
62  void Activate()
63  {
64  this->Texture->Activate();
65  }
66 
67  //--------------------------------------------------------------------------
68  void Deactivate()
69  {
70  this->Texture->Deactivate();
71  }
72 
73  //--------------------------------------------------------------------------
74  void Update(vtkRenderer* ren,
75  vtkImageData *input,
76  int cellFlag,
77  int textureExtent[6],
78  int scalarMode,
79  int arrayAccessMode,
80  int arrayId,
81  const char* arrayName,
82  vtkIdType maxMemoryInBytes)
83  {
84  bool needUpdate = false;
85  bool modified = false;
86 
87  if (!this->Texture)
88  {
90  needUpdate = true;
91  }
92 
94  ren->GetRenderWindow()));
95 
96  if (!this->Texture->GetHandle())
97  {
98  needUpdate = true;
99  }
100 
101  int obsolete = needUpdate || !this->Loaded ||
102  input->GetMTime()>this->BuildTime;
103  if(!obsolete)
104  {
105  obsolete = cellFlag != this->LoadedCellFlag;
106  int i = 0;
107  while(!obsolete && i<6)
108  {
109  obsolete = obsolete || this->LoadedExtent[i]>textureExtent[i];
110  ++i;
111  obsolete = obsolete || this->LoadedExtent[i]<textureExtent[i];
112  ++i;
113  }
114  }
115 
116  if(obsolete)
117  {
118  this->Loaded = false;
119  int dim[3];
120  input->GetDimensions(dim);
121 
122  vtkDataArray *scalars =
123  vtkAbstractMapper::GetScalars(input,scalarMode,arrayAccessMode,
124  arrayId,arrayName,
125  this->LoadedCellFlag);
126 
127  // DONT USE GetScalarType() or GetNumberOfScalarComponents() on
128  // ImageData as it deals only with point data...
129  int scalarType = scalars->GetDataType();
130  if(scalarType != VTK_UNSIGNED_CHAR)
131  {
132  cout <<"Mask should be VTK_UNSIGNED_CHAR." << endl;
133  }
134  if(scalars->GetNumberOfComponents()!=1)
135  {
136  cout << "Mask should be a one-component scalar field." << endl;
137  }
138 
139  GLint internalFormat = GL_R8;
140  GLenum format = GL_RED;
141  GLenum type = GL_UNSIGNED_BYTE;
142 
143  // Enough memory?
144  int textureSize[3];
145  int i = 0;
146  while(i < 3)
147  {
148  textureSize[i] = textureExtent[2*i+1] - textureExtent[2*i] + 1;
149  ++i;
150  }
151 
152  GLint width;
153  glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &width);
154  this->Loaded = textureSize[0] <= width && textureSize[1] <= width &&
155  textureSize[2] <= width;
156  if(this->Loaded)
157  {
158  // so far, so good but some cards don't report allocation error
159  this->Loaded = textureSize[0] * textureSize[1]*
160  textureSize[2] *
162  scalars->GetNumberOfComponents() <=
163  maxMemoryInBytes;
164  if(this->Loaded)
165  {
166  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
167 
168  if(!(textureExtent[1]-textureExtent[0]+cellFlag==dim[0]))
169  {
170  glPixelStorei(GL_UNPACK_ROW_LENGTH,dim[0]-cellFlag);
171  }
172  if(!(textureExtent[3]-textureExtent[2]+cellFlag==dim[1]))
173  {
174  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
175  dim[1]-cellFlag);
176  }
177  void* dataPtr = scalars->GetVoidPointer(
178  ((textureExtent[4]*(dim[1]-cellFlag)+textureExtent[2]) *
179  (dim[0]-cellFlag)+textureExtent[0]) *
180  scalars->GetNumberOfComponents());
181 
182  this->Texture->SetDataType(type);
183  this->Texture->SetFormat(format);
184  this->Texture->SetInternalFormat(internalFormat);
185  this->Texture->Create3DFromRaw(
186  textureSize[0], textureSize[1], textureSize[2],
187  1, scalarType, dataPtr);
193  this->Texture->SetBorderColor(0.0f, 0.0f, 0.0f, 0.0f);
194 
195  // Restore the default values.
196  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
197  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
198 
199  this->LoadedCellFlag = cellFlag;
200  i = 0;
201  while(i < 6)
202  {
203  this->LoadedExtent[i] = textureExtent[i];
204  ++i;
205  }
206 
207  double spacing[3];
208  double origin[3];
209  input->GetSpacing(spacing);
210  input->GetOrigin(origin);
211  int swapBounds[3];
212  swapBounds[0] = (spacing[0] < 0);
213  swapBounds[1] = (spacing[1] < 0);
214  swapBounds[2] = (spacing[2] < 0);
215 
216  if(!this->LoadedCellFlag) // loaded extents represent points
217  {
218  // slabsPoints[i]=(slabsDataSet[i] - origin[i/2]) / spacing[i/2];
219  // in general, x=o+i*spacing.
220  // if spacing is positive min extent match the min of the
221  // bounding box
222  // and the max extent match the max of the bounding box
223  // if spacing is negative min extent match the max of the
224  // bounding box
225  // and the max extent match the min of the bounding box
226 
227  // if spacing is negative, we may have to rethink the equation
228  // between real point and texture coordinate...
229  this->LoadedBounds[0]=origin[0]+
230  static_cast<double>(this->LoadedExtent[0+swapBounds[0]])*spacing[0];
231  this->LoadedBounds[2]=origin[1]+
232  static_cast<double>(this->LoadedExtent[2+swapBounds[1]])*spacing[1];
233  this->LoadedBounds[4]=origin[2]+
234  static_cast<double>(this->LoadedExtent[4+swapBounds[2]])*spacing[2];
235  this->LoadedBounds[1]=origin[0]+
236  static_cast<double>(this->LoadedExtent[1-swapBounds[0]])*spacing[0];
237  this->LoadedBounds[3]=origin[1]+
238  static_cast<double>(this->LoadedExtent[3-swapBounds[1]])*spacing[1];
239  this->LoadedBounds[5]=origin[2]+
240  static_cast<double>(this->LoadedExtent[5-swapBounds[2]])*spacing[2];
241 
242  }
243  else // loaded extents represent cells
244  {
245  int wholeTextureExtent[6];
246  input->GetExtent(wholeTextureExtent);
247  i=1;
248  while(i<6)
249  {
250  wholeTextureExtent[i]--;
251  i+=2;
252  }
253 
254  i=0;
255  while(i<3)
256  {
257  if(this->LoadedExtent[2*i]==wholeTextureExtent[2*i])
258  {
259  this->LoadedBounds[2*i+swapBounds[i]]=origin[i];
260  }
261  else
262  {
263  this->LoadedBounds[2*i+swapBounds[i]]=origin[i]+
264  (static_cast<double>(this->LoadedExtent[2*i])+0.5)*spacing[i];
265  }
266 
267  if(this->LoadedExtent[2*i+1]==wholeTextureExtent[2*i+1])
268  {
269  this->LoadedBounds[2*i+1-swapBounds[i]]=origin[i]+
270  (static_cast<double>(this->LoadedExtent[2*i+1])+1.0)*spacing[i];
271  }
272  else
273  {
274  this->LoadedBounds[2*i+1-swapBounds[i]]=origin[i]+
275  (static_cast<double>(this->LoadedExtent[2*i+1])+0.5)*spacing[i];
276  }
277  ++i;
278  }
279  }
280  modified = true;
281  }
282  }
283  }
284 
285  if(modified)
286  {
287  this->BuildTime.Modified();
288  }
289  }
290 
291  //--------------------------------------------------------------------------
292  double* GetLoadedBounds()
293  {
294  return this->LoadedBounds;
295  }
296 
297  //--------------------------------------------------------------------------
299  {
300  return this->LoadedExtent;
301  }
302 
303  //--------------------------------------------------------------------------
305  {
306  return this->LoadedCellFlag;
307  }
308 
309  //--------------------------------------------------------------------------
310  bool IsLoaded()
311  {
312  return this->Loaded;
313  }
314 
315  // Get the texture unit
316  //--------------------------------------------------------------------------
317  int GetTextureUnit(void)
318  {
319  if (!this->Texture)
320  {
321  return -1;
322  }
323  return this->Texture->GetTextureUnit();
324  }
325 
326  //--------------------------------------------------------------------------
328  {
329  if (this->Texture)
330  {
331  this->Texture->ReleaseGraphicsResources(window);
332  this->Texture->Delete();
333  this->Texture = 0;
334  }
335  }
336 
337 
338 protected:
341 
342  double LoadedBounds[6];
344 
346  bool Loaded;
347 };
348 
349 //----------------------------------------------------------------------------
351 {
352 public:
353  std::map<vtkImageData *,vtkVolumeMask*> Map;
355  {
356  }
357 private:
359  vtkMapMaskTextureId &operator=(const vtkMapMaskTextureId &other);
360 };
361 
362 #endif // vtkVolumeMask_h
363 // VTK-HeaderTest-Exclude: vtkVolumeMask.h
virtual int GetDataTypeSize()=0
Return the size of the underlying data type.
virtual int GetDataType()=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:55
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:46
virtual double * GetOrigin()
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
virtual int * GetExtent()
virtual double * GetSpacing()
std::map< vtkImageData *, vtkVolumeMask * > Map
virtual void Delete()
Delete a VTK object.
static vtkOpenGLRenderWindow * SafeDownCast(vtkObjectBase *o)
abstract specification for renderers
Definition: vtkRenderer.h:64
vtkRenderWindow * GetRenderWindow()
Definition: vtkRenderer.h:418
abstracts an OpenGL texture object.
void Deactivate(unsigned int texUnit)
virtual void SetWrapT(int)
void SetInternalFormat(unsigned int glInternalFormat)
void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind 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).
void SetDataType(unsigned int glType)
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].
void Activate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
virtual void SetMinificationFilter(int)
virtual void SetWrapS(int)
void SetContext(vtkRenderWindow *)
Get/Set the context.
record modification and/or execution time
Definition: vtkTimeStamp.h:36
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:74
vtkTimeStamp BuildTime
void Deactivate()
Definition: vtkVolumeMask.h:68
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:516
@ spacing
Definition: vtkX3D.h:481
int vtkIdType
Definition: vtkType.h:287
#define VTK_INT_MIN
Definition: vtkType.h:152
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:51
#define VTK_INT_MAX
Definition: vtkType.h:153