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 #include <TColStd_ListOfInteger.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
29 static Standard_Byte gbits[8] = {1, 2, 4, 8, 16, 32, 64, 128};
30 static Standard_Byte gnbits[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
34 Voxel_OctBoolDS::Voxel_OctBoolDS():Voxel_DS(),mySubVoxels(0)
39 // Constructor with intialization.
40 Voxel_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)
45 Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
49 void 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)
55 Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
57 if (!myNbX || !myNbY || !myNbZ)
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*));
66 void Voxel_OctBoolDS::Destroy()
71 free((Standard_Byte**)myData);
75 delete (iXYZBool*)mySubVoxels;
81 void Voxel_OctBoolDS::SetZero()
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++)
89 if (((Standard_Byte**)myData)[ix])
91 free(((Standard_Byte**)myData)[ix]);
92 ((Standard_Byte**)myData)[ix] = 0;
99 iXYZBool* map = (iXYZBool*) mySubVoxels;
104 // Access to the boolean information attached to a particular voxel:
105 // Info: (ix >= 0 && ix < theNb_x), etc.
106 void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
107 const Standard_Boolean data)
109 // All 8 sub-voxels have the same value.
110 // No need anymore to keep them in memory.
111 if (IsSplit(ix, iy, iz))
116 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
117 Standard_Integer islice = ibit >> 6;
119 if (!data && !((Standard_Byte**)myData)[islice])
120 return; // don't allocate a slice of data for setting a 0 value
122 // Allocate the slice if it is not done yet.
123 if (!((Standard_Byte**)myData)[islice])
125 ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
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;
133 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
135 // Position of data in the 8 bit-"value".
136 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
139 if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
142 value |= gbits[shift];
144 value &= gnbits[shift];
145 ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
149 void Voxel_OctBoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
150 const Standard_Integer ioct, const Standard_Boolean data)
152 // If the voxel is not split yet, do it now.
153 if (!IsSplit(ix, iy, iz))
164 Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
167 if (data != ((value & gbits[ioct]) ? Standard_True : Standard_False))
170 value |= gbits[ioct];
172 value &= gnbits[ioct];
173 ((iXYZBool*)mySubVoxels)->ChangeFind(xyz) = value;
177 Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
179 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
180 Standard_Integer islice = ibit >> 6;
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;
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;
191 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
193 // Position of data in the 8 bit-"value".
194 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
196 return ((value & gbits[shift]) ? Standard_True : Standard_False);
199 Standard_Boolean Voxel_OctBoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
200 const Standard_Integer ioct) const
202 // If the voxel is not split, return the value of the voxel.
203 if (!IsSplit(ix, iy, iz))
205 return Get(ix, iy, iz);
214 const Standard_Byte value = ((iXYZBool*)mySubVoxels)->Find(xyz);
217 return (value & gbits[ioct]) ? Standard_True : Standard_False;
220 Standard_Boolean Voxel_OctBoolDS::IsSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
223 return Standard_False;
230 return ((iXYZBool*)mySubVoxels)->IsBound(xyz);
233 void Voxel_OctBoolDS::Split(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
242 if (!((iXYZBool*)mySubVoxels)->IsBound(xyz))
244 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
249 mySubVoxels = (Standard_Address) new iXYZBool;
250 ((iXYZBool*)mySubVoxels)->Bind(xyz, 0);
254 void Voxel_OctBoolDS::UnSplit(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz)
264 if (((iXYZBool*)mySubVoxels)->IsBound(xyz))
266 ((iXYZBool*)mySubVoxels)->UnBind(xyz);
270 void Voxel_OctBoolDS::OptimizeMemory()
276 TColStd_ListOfInteger ixs, iys, izs, values;
277 iXYZBool::Iterator itr(*((iXYZBool*)mySubVoxels));
278 for (; itr.More(); itr.Next())
281 if (value == 0 || value == 255)
287 values.Append((Standard_Integer)value);
291 TColStd_ListIteratorOfListOfInteger itrix(ixs), itriy(iys), itriz(izs), itrvalues(values);
292 for (; itrix.More(); itrix.Next(), itriy.Next(), itriz.Next(), itrvalues.Next())
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();
299 Set(ix, iy, iz, (value ? Standard_True : Standard_False));
303 // If the map is empty, release it.
304 if (((iXYZBool*)mySubVoxels)->IsEmpty())
306 delete (iXYZBool*)mySubVoxels;