summaryrefslogtreecommitdiff
path: root/src/Voxel/Voxel_DS.cxx
blob: 82f3350428a3a16ad4964800c7487d4791a52b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// File:	Voxel_DS.cxx
// Created:	Mon May 11 13:46:35 2008
// Author:	Vladislav ROMASHKO
//		<vladislav.romashko@opencascade.com>

#include <Voxel_DS.ixx>

// Empty constructor
Voxel_DS::Voxel_DS()
:myData(0),
 myX(0.0),myY(0.0),myZ(0.0),
 myXLen(0.0),myYLen(0.0),myZLen(0.0),
 myNbX(0),myNbY(0),myNbZ(0),
 myNbXY(0),myDX(0.0),myDY(0.0),myDZ(0.0),
 myHalfDX(0.0),myHalfDY(0.0),myHalfDZ(0.0)
{

}

// Constructor with intialization.
Voxel_DS::Voxel_DS(const Standard_Real      x, const Standard_Real      y, const Standard_Real      z, 
		   const Standard_Real   xlen, const Standard_Real   ylen, const Standard_Real   zlen,
		   const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
{
  Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
}

// Initialization.
void Voxel_DS::Init(const Standard_Real      x, const Standard_Real      y, const Standard_Real      z, 
		    const Standard_Real   xlen, const Standard_Real   ylen, const Standard_Real   zlen,
		    const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
{
  myX    = x;
  myY    = y;
  myZ    = z;
  myXLen = xlen;
  myYLen = ylen;
  myZLen = zlen;
  myNbX  = nbx;
  myNbY  = nby;
  myNbZ  = nbz;
  myNbXY = myNbX * myNbY;
  myDX   = myXLen / (Standard_Real) myNbX;
  myDY   = myYLen / (Standard_Real) myNbY;
  myDZ   = myZLen / (Standard_Real) myNbZ;
  myHalfDX = myDX / 2.0;
  myHalfDY = myDY / 2.0;
  myHalfDZ = myDZ / 2.0;
}

// Get the initial information on voxels
Standard_Real Voxel_DS::GetX() const
{
  return myX;
}

Standard_Real Voxel_DS::GetY() const
{
  return myY;
}

Standard_Real Voxel_DS::GetZ() const
{
  return myZ;
}

Standard_Real Voxel_DS::GetXLen() const
{
  return myXLen;
}

Standard_Real Voxel_DS::GetYLen() const
{
  return myYLen;
}

Standard_Real Voxel_DS::GetZLen() const
{
  return myZLen;
}

Standard_Integer Voxel_DS::GetNbX() const
{
  return myNbX;
}

Standard_Integer Voxel_DS::GetNbY() const
{
  return myNbY;
}

Standard_Integer Voxel_DS::GetNbZ() const
{
  return myNbZ;
}

void Voxel_DS::GetCenter(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz,
			 Standard_Real&         xc, Standard_Real&         yc, Standard_Real&         zc) const
{
  xc = myX + ix * myDX + myHalfDX;
  yc = myY + iy * myDY + myHalfDY;
  zc = myZ + iz * myDZ + myHalfDZ;
}

// The method uses a chordial approach to find the index of voxel by co-ordinate.
static Standard_Integer findIndex(const Standard_Real xstart, const Standard_Real dx,
				  const Standard_Integer ix1, const Standard_Integer ix2,
				  const Standard_Real x)
{
  if (ix2 - ix1 < 2)
  { 
    if (x < xstart + ix2 * dx)
      return ix1;
    return ix2;
  }

  // Middle index
  const Standard_Integer ixm = (ix1 + ix2) / 2;

  // Check if it is in the first half:
  if (x >= xstart + ix1 * dx && x < xstart + ixm * dx)
  {
    return findIndex(xstart, dx, ix1, ixm, x);
  }

  return findIndex(xstart, dx, ixm, ix2, x);
}

Standard_Boolean Voxel_DS::GetVoxel(const Standard_Real x, const Standard_Real y, const Standard_Real z, 
				    Standard_Integer&  ix, Standard_Integer&  iy, Standard_Integer&  iz) const
{
  // X
  if (!GetVoxelX(x, ix))
    return Standard_False;
  
  // Y
  if (!GetVoxelY(y, iy))
    return Standard_False;
  
  // Z
  return GetVoxelZ(z, iz);
}

Standard_Boolean Voxel_DS::GetVoxelX(const Standard_Real x,
				     Standard_Integer&  ix) const
{
  // X
  if (x < myX || x > myX + myXLen)
    return Standard_False;
  ix = findIndex(myX, myXLen / (Standard_Real) myNbX, 0, myNbX - 1, x);
  return Standard_True;
}

Standard_Boolean Voxel_DS::GetVoxelY(const Standard_Real y,
				     Standard_Integer&  iy) const
{
  // Y
  if (y < myY || y > myY + myYLen)
    return Standard_False;
  iy = findIndex(myY, myYLen / (Standard_Real) myNbY, 0, myNbY - 1, y);
  return Standard_True;
}

Standard_Boolean Voxel_DS::GetVoxelZ(const Standard_Real z,
				     Standard_Integer&  iz) const
{
  // Z
  if (z < myZ || z > myZ + myZLen)
    return Standard_False;
  iz = findIndex(myZ, myZLen / (Standard_Real) myNbZ, 0, myNbZ - 1, z);
  return Standard_True;
}