VTK  9.1.0
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>
22#include <vtkRenderWindow.h>
23#include <vtkRenderer.h>
24#include <vtkTextureObject.h>
25
26#include <map> // STL required
27
28//----------------------------------------------------------------------------
30{
31public:
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 //--------------------------------------------------------------------------
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] *
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);
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 //--------------------------------------------------------------------------
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
302protected:
305
306 double LoadedBounds[6];
308
310 bool Loaded;
311};
312
313//----------------------------------------------------------------------------
315{
316public:
317 std::map<vtkImageData*, vtkVolumeMask*> Map;
319
320private:
321 vtkMapMaskTextureId(const vtkMapMaskTextureId& other) = delete;
322 vtkMapMaskTextureId& operator=(const vtkMapMaskTextureId& other) = delete;
323};
324
325#endif // vtkVolumeMask_h
326// VTK-HeaderTest-Exclude: vtkVolumeMask.h
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
virtual int GetDataTypeSize() const =0
Return the size of the underlying data type.
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
virtual int GetDataType() const =0
Return the underlying data type.
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:159
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:157
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
virtual double * GetOrigin()
Set/Get the origin of the dataset.
virtual int * GetExtent()
Set/Get the extent.
virtual double * GetSpacing()
Set the spacing (width,height,length) of the cubical cells that compose the data set.
std::map< vtkImageData *, vtkVolumeMask * > Map
vtkMapMaskTextureId()=default
virtual void Delete()
Delete a VTK object.
OpenGL rendering window.
static vtkOpenGLRenderWindow * SafeDownCast(vtkObjectBase *o)
virtual vtkOpenGLState * GetState()
abstract specification for renderers
Definition: vtkRenderer.h:173
vtkRenderWindow * GetRenderWindow()
Definition: vtkRenderer.h:558
abstracts an OpenGL texture object.
void SetContext(vtkOpenGLRenderWindow *)
Get/Set the context.
virtual void SetWrapT(int)
Wrap mode for the first texture coordinate "t" Valid values are:
void SetInternalFormat(unsigned int glInternalFormat)
Get/Set internal format (OpenGL internal format) that should be used.
virtual void Activate()
Activate and Bind the texture.
virtual void SetMagnificationFilter(int)
Magnification filter mode.
virtual unsigned int GetHandle()
Returns the OpenGL handle.
int GetTextureUnit()
Return the texture unit used for this texture.
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)
Get the data type for the texture as GLenum type.
void Deactivate()
Deactivate and UnBind the texture.
virtual void SetWrapR(int)
Wrap mode for the first texture coordinate "r" Valid values are:
void SetFormat(unsigned int glFormat)
Get/Set format (OpenGL internal format) that should be used.
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)
Minification filter mode.
static vtkTextureObject * New()
virtual void SetWrapS(int)
Wrap mode for the first texture coordinate "s" Valid values are:
record modification and/or execution time
Definition: vtkTimeStamp.h:52
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
vtkIdType * GetLoadedExtent()
void ReleaseGraphicsResources(vtkWindow *window)
int GetTextureUnit(void)
double * GetLoadedBounds()
int GetLoadedCellFlag()
window superclass for vtkRenderWindow
Definition: vtkWindow.h:36
@ type
Definition: vtkX3D.h:522
@ spacing
Definition: vtkX3D.h:487
#define GL_UNSIGNED_BYTE
int vtkIdType
Definition: vtkType.h:332
#define VTK_INT_MIN
Definition: vtkType.h:154
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:47
#define VTK_INT_MAX
Definition: vtkType.h:155