1 // Created on: 2008-06-21
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Voxel_BoolDS.ixx>
20 static Standard_Byte gbits[8] = {1, 2, 4, 8, 16, 32, 64, 128};
21 static Standard_Byte gnbits[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
24 Voxel_BoolDS::Voxel_BoolDS():Voxel_DS()
29 // Constructor with intialization.
30 Voxel_BoolDS::Voxel_BoolDS(const Standard_Real x, const Standard_Real y, const Standard_Real z,
31 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
32 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
35 Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
39 void Voxel_BoolDS::Init(const Standard_Real x, const Standard_Real y, const Standard_Real z,
40 const Standard_Real xlen, const Standard_Real ylen, const Standard_Real zlen,
41 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
45 Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
47 if (!myNbX || !myNbY || !myNbZ)
50 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
51 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
52 myData = (Standard_Address) calloc(nb_slices, sizeof(Standard_Byte*));
56 void Voxel_BoolDS::Destroy()
61 free((Standard_Byte**)myData);
66 void Voxel_BoolDS::SetZero()
70 Standard_Integer nb_bytes = RealToInt(ceil(myNbXY * myNbZ / 8.0));
71 Standard_Integer ix = 0, nb_slices = RealToInt(ceil(nb_bytes / 8.0));
72 for (; ix < nb_slices; ix++)
74 if (((Standard_Byte**)myData)[ix])
76 free(((Standard_Byte**)myData)[ix]);
77 ((Standard_Byte**)myData)[ix] = 0;
83 // Access to the boolean information attached to a particular voxel:
84 // Info: (ix >= 0 && ix < theNb_x), etc.
85 void Voxel_BoolDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
86 const Standard_Boolean data)
88 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
89 Standard_Integer islice = ibit >> 6;
91 if (!data && !((Standard_Byte**)myData)[islice])
92 return; // don't allocate a slice of data for setting a 0 value
94 // Allocate the slice if it is not done yet.
95 if (!((Standard_Byte**)myData)[islice])
97 ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
100 // Index within 8 bytes of the slice.
101 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
102 Standard_Integer ibyte = ibit_in_current_slice >> 3;
105 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
107 // Position of data in the 8 bit-"value".
108 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
111 if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
114 value |= gbits[shift];
116 value &= gnbits[shift];
117 ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
121 Standard_Boolean Voxel_BoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
123 Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
124 Standard_Integer islice = ibit >> 6;
126 // If the slice of data is not allocated, it means that its values are 0.
127 if (!((Standard_Byte**)myData)[islice])
128 return Standard_False;
130 // Index within 8 bytes of the slice.
131 Standard_Integer ibit_in_current_slice = ibit - (islice << 6);
132 Standard_Integer ibyte = ibit_in_current_slice >> 3;
135 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
137 // Position of data in the 8 bit-"value".
138 Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
140 return ((value & gbits[shift]) ? Standard_True : Standard_False);