0025361: Sample "Voxel" crashes during "Demo - Collisions" demonstration
[occt.git] / src / Voxel / Voxel_BoolDS.cxx
1 // Created on: 2008-06-21
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Voxel_BoolDS.ixx>
17
18 #include <stdlib.h>
19
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};
22
23 // Empty constructor
24 Voxel_BoolDS::Voxel_BoolDS():Voxel_DS()
25 {
26
27 }
28
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)
33 :Voxel_DS()
34 {
35   Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
36 }
37
38 // Initialization.
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)
42 {
43   Destroy();
44
45   Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
46
47   if (!myNbX || !myNbY || !myNbZ)
48     return;
49
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*));
53 }
54
55 // Destructor
56 void Voxel_BoolDS::Destroy()
57 {
58   if (myData)
59   {
60     SetZero();
61     free((Standard_Byte**)myData);
62     myData = 0;
63   }
64 }
65
66 void Voxel_BoolDS::SetZero()
67 {
68   if (myData)
69   {
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++)
73     {
74       if (((Standard_Byte**)myData)[ix])
75       {
76         free(((Standard_Byte**)myData)[ix]);
77         ((Standard_Byte**)myData)[ix] = 0;
78       }
79     }
80   }  
81 }
82
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)
87 {
88   Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
89   Standard_Integer islice = ibit >> 6;
90
91   if (!data && !((Standard_Byte**)myData)[islice])
92     return; // don't allocate a slice of data for setting a 0 value
93
94   // Allocate the slice if it is not done yet.
95   if (!((Standard_Byte**)myData)[islice])
96   {
97     ((Standard_Byte**)myData)[islice] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
98   }
99
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;
103
104   // Value (byte)
105   Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
106
107   // Position of data in the 8 bit-"value".
108   Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
109
110   // Set data
111   if (data != ((value & gbits[shift]) ? Standard_True : Standard_False))
112   {
113     if (data)
114       value |= gbits[shift];
115     else
116       value &= gnbits[shift];
117     ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte] = value;
118   }
119 }
120
121 Standard_Boolean Voxel_BoolDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
122 {
123   Standard_Integer ibit = ix + myNbX * iy + myNbXY * iz;
124   Standard_Integer islice = ibit >> 6;
125
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;
129
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;
133
134   // Value (byte)
135   Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)myData)[islice])[ibyte];
136
137   // Position of data in the 8 bit-"value".
138   Standard_Integer shift = ibit_in_current_slice - (ibyte << 3);
139
140   return ((value & gbits[shift]) ? Standard_True : Standard_False);
141 }