0023023: VRML reader fails on attempt to read an attached WRL file
[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
26static Standard_Byte gbits[8] = {1, 2, 4, 8, 16, 32, 64, 128};
27static Standard_Byte gnbits[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
28static iXYZ xyz;
29
30// Empty constructor
31Voxel_OctBoolDS::Voxel_OctBoolDS():Voxel_DS(),mySubVoxels(0)
32{
33
34}
35
36// Constructor with intialization.
37Voxel_OctBoolDS::Voxel_OctBoolDS(const Standard_Real x, const Standard_Real y, const Standard_Real z,
38 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
39 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
40:Voxel_DS(),mySubVoxels(0)
41{
42 Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
43}
44
45// Initialization.
46void Voxel_OctBoolDS::Init(const Standard_Real x, const Standard_Real y, const Standard_Real z,
47 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
48 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
49{
50 Destroy();
51
52 Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
53
54 if (!myNbX || !myNbY || !myNbZ)
55 return;
56
57 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
58 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
59 myData = (Standard_Address) calloc(nb_slices, sizeof(Standard_Byte*));
60}
61
62// Destructor
63void Voxel_OctBoolDS::Destroy()
64{
65 if (myData)
66 {
67 SetZero();
68 free((Standard_Byte**)myData);
69 myData = 0;
70 if (mySubVoxels)
71 {
72 delete (iXYZBool*)mySubVoxels;
73 mySubVoxels = 0;
74 }
75 }
76}
77
78void Voxel_OctBoolDS::SetZero()
79{
80 if (myData)
81 {
82 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
83 Standard_Integer ix = 0, nb_slices = RealToInt(ceil(nb_bytes / 8.0));
84 for (; ix < nb_slices; ix++)
85 {
86 if (((Standard_Byte**)myData)[ix])
87 {
88 free(((Standard_Byte**)myData)[ix]);
89 ((Standard_Byte**)myData)[ix] = 0;
90 }
91 }
92 }
93
94 if (mySubVoxels)
95 {
96 iXYZBool* map = (iXYZBool*) mySubVoxels;
97 map->Clear();
98 }
99}
100
101// Access to the boolean information attached to a particular voxel:
102// Info: (ix >= 0 && ix < theNb_x), etc.
103void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
104 const Standard_Boolean data)
105{
106 // All 8 sub-voxels have the same value.
107 // No need anymore to keep them in memory.
108 if (IsSplit(ix, iy, iz))
109 {
110 UnSplit(ix, iy, iz);
111 }
112
113 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
114 Standard_Integer islice = ibit >> 6;
115
116 if (!data && !((Standard_Byte**)myData)[islice])
117 return; // don't allocate a slice of data for setting a 0 value
118
119 // Allocate the slice if it is not done yet.
120 if (!((Standard_Byte**)myData)[islice])
121 {
122 ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
123 }
124
125 // Index within 8 bytes of the slice.
126 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
127 Standard_Integer ibyte = ibit_in_current_slice >> 3;
128
129 // Value (byte)
130 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
131
132 // Position of data in the 8 bit-"value".
133 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
134
135 // Set data
136 if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
137 {
138 if (data)
139 value |= gbits[shift];
140 else
141 value &= gnbits[shift];
142 ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
143 }
144}
145
146void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
147 const Standard_Integer ioct, const Standard_Boolean data)
148{
149 // If the voxel is not split yet, do it now.
150 if (!IsSplit(ix, iy, iz))
151 {
152 Split(ix, iy, iz);
153 }
154
155 // Voxel
156 xyz.ix = ix;
157 xyz.iy = iy;
158 xyz.iz = iz;
159
160 // Take the value
161 Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
162
163 // Set data
164 if (data != ((value & gbits[ioct]) ? Standard_True : Standard_False))
165 {
166 if (data)
167 value |= gbits[ioct];
168 else
169 value &= gnbits[ioct];
170 ((iXYZBool*)mySubVoxels)->ChangeFind(xyz) = value;
171 }
172}
173
174Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
175{
176 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
177 Standard_Integer islice = ibit >> 6;
178
179 // If the slice of data is not allocated, it means that its values are 0.
180 if (!((Standard_Byte**)myData)[islice])
181 return Standard_False;
182
183 // Index within 8 bytes of the slice.
184 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
185 Standard_Integer ibyte = ibit_in_current_slice >> 3;
186
187 // Value (byte)
188 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
189
190 // Position of data in the 8 bit-"value".
191 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
192
193 return ((value & gbits[shift]) ? Standard_True : Standard_False);
194}
195
196Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
197 const Standard_Integer ioct) const
198{
199 // If the voxel is not split, return the value of the voxel.
200 if (!IsSplit(ix, iy, iz))
201 {
202 return Get(ix, iy, iz);
203 }
204
205 // Voxel
206 xyz.ix = ix;
207 xyz.iy = iy;
208 xyz.iz = iz;
209
210 // Take the value
211 const Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
212
213 // Return data
214 return (value & gbits[ioct]) ? Standard_True : Standard_False;
215}
216
217Standard_Boolean Voxel_OctBoolDS::IsSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
218{
219 if (!mySubVoxels)
220 return Standard_False;
221
222 // Voxel
223 xyz.ix = ix;
224 xyz.iy = iy;
225 xyz.iz = iz;
226
227 return ((iXYZBool*)mySubVoxels)->IsBound(xyz);
228}
229
230void Voxel_OctBoolDS::Split(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
231{
232 // Voxel
233 xyz.ix = ix;
234 xyz.iy = iy;
235 xyz.iz = iz;
236
237 if (mySubVoxels)
238 {
239 if (!((iXYZBool*)mySubVoxels)->IsBound(xyz))
240 {
241 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
242 }
243 }
244 else
245 {
246 mySubVoxels = (Standard_Address) new iXYZBool;
247 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
248 }
249}
250
251void Voxel_OctBoolDS::UnSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
252{
253 if (!mySubVoxels)
254 return;
255
256 // Voxel
257 xyz.ix = ix;
258 xyz.iy = iy;
259 xyz.iz = iz;
260
261 if (((iXYZBool*)mySubVoxels)->IsBound(xyz))
262 {
263 ((iXYZBool*)mySubVoxels)->UnBind(xyz);
264 }
265}
266
267void Voxel_OctBoolDS::OptimizeMemory()
268{
269 if (!mySubVoxels)
270 return;
271
272 Standard_Byte value;
273 iXYZBool::Iterator itr(*((iXYZBool*)mySubVoxels));
274 for (; itr.More(); itr.Next())
275 {
276 value = itr.Value();
277 if (value == 0 || value == 255)
278 {
279 xyz = itr.Key();
280 Set(xyz.ix, xyz.iy, xyz.iz, value ? Standard_True : Standard_False);
281 UnSplit(xyz.ix, xyz.iy, xyz.iz);
282 }
283 }
284
285 // If the map is empty, release it.
286 if (((iXYZBool*)mySubVoxels)->IsEmpty())
287 {
288 delete (iXYZBool*)mySubVoxels;
289 mySubVoxels = 0;
290 }
291}