1 // Created on: 2008-08-27
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 2008-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <Voxel_OctBoolDS.ixx>
22 #include <Voxel_TypeDef.hxx>
26 static Standard_Byte gbits[8] = {1, 2, 4, 8, 16, 32, 64, 128};
27 static Standard_Byte gnbits[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
31 Voxel_OctBoolDS::Voxel_OctBoolDS():Voxel_DS(),mySubVoxels(0)
36 // Constructor with intialization.
37 Voxel_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)
42 Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
46 void 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)
52 Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
54 if (!myNbX || !myNbY || !myNbZ)
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*));
63 void Voxel_OctBoolDS::Destroy()
68 free((Standard_Byte**)myData);
72 delete (iXYZBool*)mySubVoxels;
78 void Voxel_OctBoolDS::SetZero()
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++)
86 if (((Standard_Byte**)myData)[ix])
88 free(((Standard_Byte**)myData)[ix]);
89 ((Standard_Byte**)myData)[ix] = 0;
96 iXYZBool* map = (iXYZBool*) mySubVoxels;
101 // Access to the boolean information attached to a particular voxel:
102 // Info: (ix >= 0 && ix < theNb_x), etc.
103 void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
104 const Standard_Boolean data)
106 // All 8 sub-voxels have the same value.
107 // No need anymore to keep them in memory.
108 if (IsSplit(ix, iy, iz))
113 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
114 Standard_Integer islice = ibit >> 6;
116 if (!data && !((Standard_Byte**)myData)[islice])
117 return; // don't allocate a slice of data for setting a 0 value
119 // Allocate the slice if it is not done yet.
120 if (!((Standard_Byte**)myData)[islice])
122 ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
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;
130 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
132 // Position of data in the 8 bit-"value".
133 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
136 if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
139 value |= gbits[shift];
141 value &= gnbits[shift];
142 ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
146 void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
147 const Standard_Integer ioct, const Standard_Boolean data)
149 // If the voxel is not split yet, do it now.
150 if (!IsSplit(ix, iy, iz))
161 Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
164 if (data != ((value & gbits[ioct]) ? Standard_True : Standard_False))
167 value |= gbits[ioct];
169 value &= gnbits[ioct];
170 ((iXYZBool*)mySubVoxels)->ChangeFind(xyz) = value;
174 Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
176 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
177 Standard_Integer islice = ibit >> 6;
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;
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;
188 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
190 // Position of data in the 8 bit-"value".
191 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
193 return ((value & gbits[shift]) ? Standard_True : Standard_False);
196 Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
197 const Standard_Integer ioct) const
199 // If the voxel is not split, return the value of the voxel.
200 if (!IsSplit(ix, iy, iz))
202 return Get(ix, iy, iz);
211 const Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
214 return (value & gbits[ioct]) ? Standard_True : Standard_False;
217 Standard_Boolean Voxel_OctBoolDS::IsSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
220 return Standard_False;
227 return ((iXYZBool*)mySubVoxels)->IsBound(xyz);
230 void Voxel_OctBoolDS::Split(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
239 if (!((iXYZBool*)mySubVoxels)->IsBound(xyz))
241 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
246 mySubVoxels = (Standard_Address) new iXYZBool;
247 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
251 void Voxel_OctBoolDS::UnSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
261 if (((iXYZBool*)mySubVoxels)->IsBound(xyz))
263 ((iXYZBool*)mySubVoxels)->UnBind(xyz);
267 void Voxel_OctBoolDS::OptimizeMemory()
273 iXYZBool::Iterator itr(*((iXYZBool*)mySubVoxels));
274 for (; itr.More(); itr.Next())
277 if (value == 0 || value == 255)
280 Set(xyz.ix, xyz.iy, xyz.iz, value ? Standard_True : Standard_False);
281 UnSplit(xyz.ix, xyz.iy, xyz.iz);
285 // If the map is empty, release it.
286 if (((iXYZBool*)mySubVoxels)->IsEmpty())
288 delete (iXYZBool*)mySubVoxels;