-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounding_box.cpp
More file actions
56 lines (41 loc) · 1.39 KB
/
Copy pathbounding_box.cpp
File metadata and controls
56 lines (41 loc) · 1.39 KB
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
#include "bounding_box.h"
bool BoundingBox::intersect(Ray* ray)
{
float tmin [3] = {0,0,0};
float tmax [3] = {0,0,0};
for(int i = 0; i < 3; i++)
{
tmin[i] = (boundMin[i] - ray->origin[i]) / ray->direction[i];
tmax[i] = (boundMax[i] - ray->origin[i]) / ray->direction[i];
if (tmin[i] > tmax[i]) {
std::swap(tmin[i], tmax[i]);
}
}
if ((tmin[X_AXIS] > tmax[Y_AXIS]) || (tmin[Y_AXIS] > tmax[X_AXIS]))
return 0.0;
if (tmin[Y_AXIS] > tmin[X_AXIS]) tmin[X_AXIS] = tmin[Y_AXIS];
if (tmax[Y_AXIS] < tmax[X_AXIS]) tmax[X_AXIS] = tmax[Y_AXIS];
if ((tmin[X_AXIS] > tmax[Z_AXIS]) || (tmin[Z_AXIS] > tmax[X_AXIS]))
return 0,0;
if (tmin[Z_AXIS] > tmin[X_AXIS]) tmin[X_AXIS] = tmin[Z_AXIS];
if (tmax[Z_AXIS] < tmax[X_AXIS]) tmax[X_AXIS] = tmax[Z_AXIS];
return true;
}
// create a box, where point0 < point 1 and they are opposing corners
BoundingBox::BoundingBox(float min [3], float max [3])
{
boundMin[X_AXIS] = min[X_AXIS];
boundMax[X_AXIS] = max[X_AXIS];
boundMin[Y_AXIS] = min[Y_AXIS];
boundMax[Y_AXIS] = max[Y_AXIS];
boundMin[Z_AXIS] = min[Z_AXIS];
boundMax[Z_AXIS] = max[Z_AXIS];
for(int i = 0; i < 3; i++){
center[i] = (boundMin[i] + boundMax[i])/2;
}
}
void BoundingBox::addBox(BoundingBox *box)
{
//printf("\nadded box");
childBoxes.push_back(box);
}