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 Bind()
63  {
64  this->Texture->Activate();
65  }
66 
67  //--------------------------------------------------------------------------
68  void Update(vtkRenderer* ren,
70  int cellFlag,
71  int textureExtent[6],
72  int scalarMode,
73  int arrayAccessMode,
74  int arrayId,
75  const char* arrayName,
76  vtkIdType maxMemoryInBytes)
77  {
78  bool needUpdate = false;
79  bool modified = false;
80 
81  if (!this->Texture)
82  {
84  needUpdate = true;
85  }
86 
88  ren->GetRenderWindow()));
89 
90  if (!this->Texture->GetHandle())
91  {
92  needUpdate = true;
93  }
94 
95  int obsolete = needUpdate || !this->Loaded ||
96  input->GetMTime()>this->BuildTime;
97  if(!obsolete)
98  {
99  obsolete = cellFlag != this->LoadedCellFlag;
100  int i = 0;
101  while(!obsolete && i<6)
102  {
103  obsolete = obsolete || this->LoadedExtent[i]>textureExtent[i];
104  ++i;
105  obsolete = obsolete || this->LoadedExtent[i]<textureExtent[i];
106  ++i;
107  }
108  }
109 
110  if(obsolete)
111  {
112  this->Loaded = false;
113  int dim[3];
114  input->GetDimensions(dim);
115 
116  vtkDataArray *scalars =
117  vtkAbstractMapper::GetScalars(input,scalarMode,arrayAccessMode,
118  arrayId,arrayName,
119  this->LoadedCellFlag);
120 
121  // DONT USE GetScalarType() or GetNumberOfScalarComponents() on
122  // ImageData as it deals only with point data...
123  int scalarType = scalars->GetDataType();
124  if(scalarType != VTK_UNSIGNED_CHAR)
125  {
126  cout <<"Mask should be VTK_UNSIGNED_CHAR." << endl;
127  }
128  if(scalars->GetNumberOfComponents()!=1)
129  {
130  cout << "Mask should be a one-component scalar field." << endl;
131  }
132 
133  GLint internalFormat = GL_ALPHA8;
134  GLenum format = GL_ALPHA;
135  GLenum type = GL_UNSIGNED_BYTE;
136 
137  // Enough memory?
138  int textureSize[3];
139  int i = 0;
140  while(i < 3)
141  {
142  textureSize[i] = textureExtent[2*i+1] - textureExtent[2*i] + 1;
143  ++i;
144  }
145 
146  GLint width;
147  glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &width);
148  this->Loaded = textureSize[0] <= width && textureSize[1] <= width &&
149  textureSize[2] <= width;
150  if(this->Loaded)
151  {
152  // so far, so good but some cards don't report allocation error
153  this->Loaded = textureSize[0] * textureSize[1]*
154  textureSize[2] *
156  scalars->GetNumberOfComponents() <=
157  maxMemoryInBytes;
158  if(this->Loaded)
159  {
160  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
161 
162  if(!(textureExtent[1]-textureExtent[0]+cellFlag==dim[0]))
163  {
164  glPixelStorei(GL_UNPACK_ROW_LENGTH,dim[0]-cellFlag);
165  }
166  if(!(textureExtent[3]-textureExtent[2]+cellFlag==dim[1]))
167  {
168  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT_EXT,
169  dim[1]-cellFlag);
170  }
171  void* dataPtr = scalars->GetVoidPointer(
172  ((textureExtent[4]*(dim[1]-cellFlag)+textureExtent[2]) *
173  (dim[0]-cellFlag)+textureExtent[0]) *
174  scalars->GetNumberOfComponents());
175 
176  this->Texture->SetDataType(type);
177  this->Texture->SetFormat(format);
178  this->Texture->SetInternalFormat(internalFormat);
179  this->Texture->Create3DFromRaw(
180  textureSize[0], textureSize[1], textureSize[2],
181  1, scalarType, dataPtr);
182  this->Texture->Activate();
188  this->Texture->SetBorderColor(0.0f, 0.0f, 0.0f, 0.0f);
189 
190  // Restore the default values.
191  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
192  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT_EXT, 0);
193 
194  this->LoadedCellFlag = cellFlag;
195  i = 0;
196  while(i < 6)
197  {
198  this->LoadedExtent[i] = textureExtent[i];
199  ++i;
200  }
201 
202  double spacing[3];
203  double origin[3];
204  input->GetSpacing(spacing);
205  input->GetOrigin(origin);
206  int swapBounds[3];
207  swapBounds[0] = (spacing[0] < 0);
208  swapBounds[1] = (spacing[1] < 0);
209  swapBounds[2] = (spacing[2] < 0);
210 
211  if(!this->LoadedCellFlag) // loaded extents represent points
212  {
213  // slabsPoints[i]=(slabsDataSet[i] - origin[i/2]) / spacing[i/2];
214  // in general, x=o+i*spacing.
215  // if spacing is positive min extent match the min of the
216  // bounding box
217  // and the max extent match the max of the bounding box
218  // if spacing is negative min extent match the max of the
219  // bounding box
220  // and the max extent match the min of the bounding box
221 
222  // if spacing is negative, we may have to rethink the equation
223  // between real point and texture coordinate...
224  this->LoadedBounds[0]=origin[0]+
225  static_cast<double>(this->LoadedExtent[0+swapBounds[0]])*spacing[0];
226  this->LoadedBounds[2]=origin[1]+
227  static_cast<double>(this->LoadedExtent[2+swapBounds[1]])*spacing[1];
228  this->LoadedBounds[4]=origin[2]+
229  static_cast<double>(this->LoadedExtent[4+swapBounds[2]])*spacing[2];
230  this->LoadedBounds[1]=origin[0]+
231  static_cast<double>(this->LoadedExtent[1-swapBounds[0]])*spacing[0];
232  this->LoadedBounds[3]=origin[1]+
233  static_cast<double>(this->LoadedExtent[3-swapBounds[1]])*spacing[1];
234  this->LoadedBounds[5]=origin[2]+
235  static_cast<double>(this->LoadedExtent[5-swapBounds[2]])*spacing[2];
236 
237  }
238  else // loaded extents represent cells
239  {
240  int wholeTextureExtent[6];
241  input->GetExtent(wholeTextureExtent);
242  i=1;
243  while(i<6)
244  {
245  wholeTextureExtent[i]--;
246  i+=2;
247  }
248 
249  i=0;
250  while(i<3)
251  {
252  if(this->LoadedExtent[2*i]==wholeTextureExtent[2*i])
253  {
254  this->LoadedBounds[2*i+swapBounds[i]]=origin[i];
255  }
256  else
257  {
258  this->LoadedBounds[2*i+swapBounds[i]]=origin[i]+
259  (static_cast<double>(this->LoadedExtent[2*i])+0.5)*spacing[i];
260  }
261 
262  if(this->LoadedExtent[2*i+1]==wholeTextureExtent[2*i+1])
263  {
264  this->LoadedBounds[2*i+1-swapBounds[i]]=origin[i]+
265  (static_cast<double>(this->LoadedExtent[2*i+1])+1.0)*spacing[i];
266  }
267  else
268  {
269  this->LoadedBounds[2*i+1-swapBounds[i]]=origin[i]+
270  (static_cast<double>(this->LoadedExtent[2*i+1])+0.5)*spacing[i];
271  }
272  ++i;
273  }
274  }
275  modified = true;
276  }
277  }
278  }
279 
280  if(modified)
281  {
282  this->BuildTime.Modified();
283  }
284  }
285 
286  //--------------------------------------------------------------------------
287  double* GetLoadedBounds()
288  {
289  return this->LoadedBounds;
290  }
291 
292  //--------------------------------------------------------------------------
294  {
295  return this->LoadedExtent;
296  }
297 
298  //--------------------------------------------------------------------------
300  {
301  return this->LoadedCellFlag;
302  }
303 
304  //--------------------------------------------------------------------------
305  bool IsLoaded()
306  {
307  return this->Loaded;
308  }
309 
310  // Get the texture unit
311  //--------------------------------------------------------------------------
312  int GetTextureUnit(void)
313  {
314  if (!this->Texture)
315  {
316  return -1;
317  }
318  return this->Texture->GetTextureUnit();
319  }
320 
321  //--------------------------------------------------------------------------
323  {
324  if (this->Texture)
325  {
326  this->Texture->ReleaseGraphicsResources(window);
327  this->Texture->Delete();
328  this->Texture = 0;
329  }
330  }
331 
332 
333 protected:
336 
337  double LoadedBounds[6];
339 
341  bool Loaded;
342 };
343 
344 //----------------------------------------------------------------------------
346 {
347 public:
348  std::map<vtkImageData *,vtkVolumeMask*> Map;
350  {
351  }
352 private:
354  vtkMapMaskTextureId &operator=(const vtkMapMaskTextureId &other);
355 };
356 
357 #endif // vtkVolumeMask_h_
358 // VTK-HeaderTest-Exclude: vtkVolumeMask.h
vtkIdType * GetLoadedExtent()
GLclampf f
Definition: vtkgl.h:14181
int GetTextureUnit(void)
GLuint GLuint GLsizei GLenum type
Definition: vtkgl.h:11315
void SetContext(vtkRenderWindow *)
vtkTimeStamp GetBuildTime()
Definition: vtkVolumeMask.h:56
GLenum GLenum GLenum input
Definition: vtkgl.h:15941
virtual int GetDataTypeSize()=0
static vtkOpenGLRenderWindow * SafeDownCast(vtkObjectBase *o)
#define VTK_INT_MAX
Definition: vtkType.h:132
record modification and/or execution time
Definition: vtkTimeStamp.h:34
static vtkDataArray * GetScalars(vtkDataSet *input, int scalarMode, int arrayAccessMode, int arrayId, const char *arrayName, int &cellFlag)
void Modified()
abstract specification for renderers
Definition: vtkRenderer.h:63
virtual int GetDataType()=0
virtual void SetWrapR(int)
void SetDataType(unsigned int glType)
virtual void SetBorderColor(float, float, float, float)
double * GetLoadedBounds()
int vtkIdType
Definition: vtkType.h:281
vtkTextureObject * Texture
vtkTimeStamp BuildTime
void ReleaseGraphicsResources(vtkWindow *window)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:33
unsigned long int GetMTime()
double LoadedBounds[6]
virtual int * GetDimensions()
virtual double * GetOrigin()
bool Create3DFromRaw(unsigned int width, unsigned int height, unsigned int depth, int numComps, int dataType, void *data)
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: vtkgl.h:11316
topologically and geometrically regular array of data
Definition: vtkImageData.h:44
virtual void SetMinificationFilter(int)
GLint GLint GLsizei width
Definition: vtkgl.h:11316
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:53
virtual double * GetSpacing()
virtual unsigned int GetHandle()
void Activate(unsigned int texUnit)
void SetInternalFormat(unsigned int glInternalFormat)
virtual int * GetExtent()
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:68
virtual void * GetVoidPointer(vtkIdType id)=0
abstracts an OpenGL texture object.
typedef GLint(APIENTRYP PFNGLGETATTRIBLOCATIONPROC)(GLuint program
int GetLoadedCellFlag()
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:28
typedef GLenum(APIENTRYP PFNGLCHECKFRAMEBUFFERSTATUSPROC)(GLenum target)
GLenum internalFormat
Definition: vtkgl.h:15195
void ReleaseGraphicsResources(vtkWindow *win)
static vtkTextureObject * New()
virtual void SetWrapS(int)
vtkRenderWindow * GetRenderWindow()
Definition: vtkRenderer.h:310
std::map< vtkImageData *, vtkVolumeMask * > Map
vtkIdType LoadedExtent[6]
virtual void SetWrapT(int)
void SetFormat(unsigned int glFormat)
#define VTK_INT_MIN
Definition: vtkType.h:131
virtual void Delete()
virtual void SetMagnificationFilter(int)