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