-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbox.cpp
125 lines (105 loc) · 1.9 KB
/
bbox.cpp
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
#include "bbox.h"
#include "vector.h"
#include "consts.h"
///
/// BBox implementations
///
BBox::BBox()
: _xmin(0), _xmax(0)
, _ymin(0), _ymax(0)
, _zmin(0), _zmax(0)
{ }
void BBox::setDim(float xmin, float xmax,
float ymin, float ymax,
float zmin, float zmax)
{
_xmin = xmin; _xmax = xmax;
_ymin = ymin; _ymax = ymax;
_zmin = zmin; _zmax = zmax;
}
unsigned nBBoxHitCount = 0;
bool BBox::isHit(Ray &ray)
{
nBBoxHitCount ++;
return ( isHitOnPlane(ray, X_AXIS) &&
isHitOnPlane(ray, Y_AXIS) &&
isHitOnPlane(ray, Z_AXIS) );
}
void BBox::getBoundValues(AxisType eType, float *pmin, float *pmax)
{
switch(eType)
{
case X_AXIS:
*pmin = _xmin;
*pmax = _xmax;
break;
case Y_AXIS:
*pmin = _ymin;
*pmax = _ymax;
break;
case Z_AXIS:
*pmin = _zmin;
*pmax = _zmax;
break;
};
}
bool BBox::isHitOnPlane(Ray &ray, AxisType eType)
{
float min = 0, max = 0;
float start = 0, dir = 0;
switch(eType)
{
case X_AXIS:
min = _xmin;
max = _xmax;
start = ray.start_point[0];
dir = ray.direction_vec[0];
break;
case Y_AXIS:
min = _ymin;
max = _ymax;
start = ray.start_point[1];
dir = ray.direction_vec[1];
break;
case Z_AXIS:
min = _zmin;
max = _zmax;
start = ray.start_point[2];
dir = ray.direction_vec[2];
break;
}
// just between the slabs? yes
if(start <= max && start > min)
{
return true;
}
// no marching in this direction?
if(dir == 0)
{
return false;
}
float toMinT = (min - start)/dir;
float toMaxT = (max - start)/dir;
if( start <= min)
{
return toMinT <= toMaxT;
}
if( start >= max)
{
return toMaxT <= toMinT;
}
return false;
}
void BBox::genBoundingSphereParam(float &fRad, vect3d &ctr)
{
ctr[0] = (_xmin + _xmax) / 2.f;
ctr[1] = (_ymin + _ymax) / 2.f;
ctr[2] = (_zmin + _zmax) / 2.f;
vect3d point;
point[0] = _xmax;
point[1] = _ymax;
point[2] = _zmax;
vect3d len;
points2vec(ctr, point, len);
fRad = vecLen(len);
}