0023032: A crash of a draw-command voxeloctboolds on MMGT_OPT=0, MMGT_REENTRANT=1
[occt.git] / src / Voxel / Voxel_OctBoolDS.cxx
CommitLineData
b311480e 1// Created on: 2008-08-27
2// Created by: Vladislav ROMASHKO
3// Copyright (c) 2008-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21#include <Voxel_OctBoolDS.ixx>
22#include <Voxel_TypeDef.hxx>
23
24#include <stdlib.h>
25
d5e4971b 26#include <TColStd_ListOfInteger.hxx>
27#include <TColStd_ListIteratorOfListOfInteger.hxx>
28
7fd59977 29static Standard_Byte gbits[8] = {1, 2, 4, 8, 16, 32, 64, 128};
30static Standard_Byte gnbits[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
31static iXYZ xyz;
32
33// Empty constructor
34Voxel_OctBoolDS::Voxel_OctBoolDS():Voxel_DS(),mySubVoxels(0)
35{
36
37}
38
39// Constructor with intialization.
40Voxel_OctBoolDS::Voxel_OctBoolDS(const Standard_Real x, const Standard_Real y, const Standard_Real z,
41 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
42 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
43:Voxel_DS(),mySubVoxels(0)
44{
45 Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
46}
47
48// Initialization.
49void Voxel_OctBoolDS::Init(const Standard_Real x, const Standard_Real y, const Standard_Real z,
50 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
51 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
52{
53 Destroy();
54
55 Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
56
57 if (!myNbX || !myNbY || !myNbZ)
58 return;
59
60 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
61 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
62 myData = (Standard_Address) calloc(nb_slices, sizeof(Standard_Byte*));
63}
64
65// Destructor
66void Voxel_OctBoolDS::Destroy()
67{
68 if (myData)
69 {
70 SetZero();
71 free((Standard_Byte**)myData);
72 myData = 0;
73 if (mySubVoxels)
74 {
75 delete (iXYZBool*)mySubVoxels;
76 mySubVoxels = 0;
77 }
78 }
79}
80
81void Voxel_OctBoolDS::SetZero()
82{
83 if (myData)
84 {
85 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
86 Standard_Integer ix = 0, nb_slices = RealToInt(ceil(nb_bytes / 8.0));
87 for (; ix < nb_slices; ix++)
88 {
89 if (((Standard_Byte**)myData)[ix])
90 {
91 free(((Standard_Byte**)myData)[ix]);
92 ((Standard_Byte**)myData)[ix] = 0;
93 }
94 }
95 }
96
97 if (mySubVoxels)
98 {
99 iXYZBool* map = (iXYZBool*) mySubVoxels;
100 map->Clear();
101 }
102}
103
104// Access to the boolean information attached to a particular voxel:
105// Info: (ix >= 0 && ix < theNb_x), etc.
106void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
107 const Standard_Boolean data)
108{
109 // All 8 sub-voxels have the same value.
110 // No need anymore to keep them in memory.
111 if (IsSplit(ix, iy, iz))
112 {
113 UnSplit(ix, iy, iz);
114 }
115
116 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
117 Standard_Integer islice = ibit >> 6;
118
119 if (!data && !((Standard_Byte**)myData)[islice])
120 return; // don't allocate a slice of data for setting a 0 value
121
122 // Allocate the slice if it is not done yet.
123 if (!((Standard_Byte**)myData)[islice])
124 {
125 ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
126 }
127
128 // Index within 8 bytes of the slice.
129 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
130 Standard_Integer ibyte = ibit_in_current_slice >> 3;
131
132 // Value (byte)
133 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
134
135 // Position of data in the 8 bit-"value".
136 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
137
138 // Set data
139 if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
140 {
141 if (data)
142 value |= gbits[shift];
143 else
144 value &= gnbits[shift];
145 ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
146 }
147}
148
149void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
150 const Standard_Integer ioct, const Standard_Boolean data)
151{
152 // If the voxel is not split yet, do it now.
153 if (!IsSplit(ix, iy, iz))
154 {
155 Split(ix, iy, iz);
156 }
157
158 // Voxel
159 xyz.ix = ix;
160 xyz.iy = iy;
161 xyz.iz = iz;
162
163 // Take the value
164 Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
165
166 // Set data
167 if (data != ((value & gbits[ioct]) ? Standard_True : Standard_False))
168 {
169 if (data)
170 value |= gbits[ioct];
171 else
172 value &= gnbits[ioct];
173 ((iXYZBool*)mySubVoxels)->ChangeFind(xyz) = value;
174 }
175}
176
177Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
178{
179 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
180 Standard_Integer islice = ibit >> 6;
181
182 // If the slice of data is not allocated, it means that its values are 0.
183 if (!((Standard_Byte**)myData)[islice])
184 return Standard_False;
185
186 // Index within 8 bytes of the slice.
187 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
188 Standard_Integer ibyte = ibit_in_current_slice >> 3;
189
190 // Value (byte)
191 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
192
193 // Position of data in the 8 bit-"value".
194 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
195
196 return ((value & gbits[shift]) ? Standard_True : Standard_False);
197}
198
199Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
200 const Standard_Integer ioct) const
201{
202 // If the voxel is not split, return the value of the voxel.
203 if (!IsSplit(ix, iy, iz))
204 {
205 return Get(ix, iy, iz);
206 }
207
208 // Voxel
209 xyz.ix = ix;
210 xyz.iy = iy;
211 xyz.iz = iz;
212
213 // Take the value
214 const Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
215
216 // Return data
217 return (value & gbits[ioct]) ? Standard_True : Standard_False;
218}
219
220Standard_Boolean Voxel_OctBoolDS::IsSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
221{
222 if (!mySubVoxels)
223 return Standard_False;
224
225 // Voxel
226 xyz.ix = ix;
227 xyz.iy = iy;
228 xyz.iz = iz;
229
230 return ((iXYZBool*)mySubVoxels)->IsBound(xyz);
231}
232
233void Voxel_OctBoolDS::Split(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
234{
235 // Voxel
236 xyz.ix = ix;
237 xyz.iy = iy;
238 xyz.iz = iz;
239
240 if (mySubVoxels)
241 {
242 if (!((iXYZBool*)mySubVoxels)->IsBound(xyz))
243 {
244 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
245 }
246 }
247 else
248 {
249 mySubVoxels = (Standard_Address) new iXYZBool;
250 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
251 }
252}
253
254void Voxel_OctBoolDS::UnSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
255{
256 if (!mySubVoxels)
257 return;
258
259 // Voxel
260 xyz.ix = ix;
261 xyz.iy = iy;
262 xyz.iz = iz;
263
264 if (((iXYZBool*)mySubVoxels)->IsBound(xyz))
265 {
266 ((iXYZBool*)mySubVoxels)->UnBind(xyz);
267 }
268}
269
270void Voxel_OctBoolDS::OptimizeMemory()
271{
272 if (!mySubVoxels)
273 return;
274
275 Standard_Byte value;
d5e4971b 276 TColStd_ListOfInteger ixs, iys, izs, values;
7fd59977 277 iXYZBool::Iterator itr(*((iXYZBool*)mySubVoxels));
278 for (; itr.More(); itr.Next())
279 {
280 value = itr.Value();
281 if (value == 0 || value == 255)
282 {
283 xyz = itr.Key();
d5e4971b 284 ixs.Append(xyz.ix);
285 iys.Append(xyz.iy);
286 izs.Append(xyz.iz);
287 values.Append((Standard_Integer)value);
7fd59977 288 }
289 }
290
d5e4971b 291 TColStd_ListIteratorOfListOfInteger itrix(ixs), itriy(iys), itriz(izs), itrvalues(values);
292 for (; itrix.More(); itrix.Next(), itriy.Next(), itriz.Next(), itrvalues.Next())
293 {
294 const Standard_Integer ix = itrix.Value();
295 const Standard_Integer iy = itriy.Value();
296 const Standard_Integer iz = itriz.Value();
297 const Standard_Integer value = itrvalues.Value();
298
299 Set(ix, iy, iz, (value ? Standard_True : Standard_False));
300 UnSplit(ix, iy, iz);
301 }
302
7fd59977 303 // If the map is empty, release it.
304 if (((iXYZBool*)mySubVoxels)->IsEmpty())
305 {
306 delete (iXYZBool*)mySubVoxels;
307 mySubVoxels = 0;
308 }
309}