-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBox.h
65 lines (53 loc) · 1.6 KB
/
BBox.h
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
#pragma once
#include "Ray.h"
class BBox
{
public:
BBox() { }
BBox(const Vector3 &a, const Vector3 &b) : min(a), max(b) { }
BBox(const BBox &box) : min(box.min), max(box.max) { }
bool RayIntersect(const Ray &r, float tmin, float tmax) const;
BBox& operator=(const BBox &b)
{
min = b.min; max = b.max;
return *this;
}
public:
Vector3 min, max;
};
inline BBox Surround(const BBox &b1, const BBox &b2)
{
return BBox(
Vector3(
b1.min.x < b2.min.x ? b1.min.x : b2.min.x,
b1.min.y < b2.min.y ? b1.min.y : b2.min.y,
b1.min.z < b2.min.z ? b1.min.z : b2.min.z),
Vector3(
b1.max.x > b2.max.x ? b1.max.x : b2.max.x,
b1.max.y > b2.max.y ? b1.max.y : b2.max.y,
b1.max.z > b2.max.z ? b1.max.z : b2.max.z));
}
inline bool BBox::RayIntersect(const Ray &r, float tmin, float tmax) const
{
float interval_min = tmin;
float interval_max = tmax;
const Vector3* pp = &min;
int posneg = r.posneg[0];
float t0 = (pp[posneg].x - r.o.x)*r.extra.x;
float t1 = (pp[1 - posneg].x - r.o.x)*r.extra.x;
if (t0 > interval_min) interval_min = t0;
if (t1 < interval_max) interval_max = t1;
if (interval_min > interval_max) return false;
posneg = r.posneg[1];
t0 = (pp[posneg].y - r.o.y)*r.extra.y;
t1 = (pp[1 - posneg].y - r.o.y)*r.extra.y;
if (t0 > interval_min) interval_min = t0;
if (t1 < interval_max) interval_max = t1;
if (interval_min > interval_max) return false;
posneg = r.posneg[2];
t0 = (pp[posneg].z - r.o.z)*r.extra.z;
t1 = (pp[1 - posneg].z - r.o.z)*r.extra.z;
if (t0 > interval_min) interval_min = t0;
if (t1 < interval_max) interval_max = t1;
return (interval_min <= interval_max);
}