-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.cpp
55 lines (53 loc) · 1.6 KB
/
geometry.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
/*
* pbrt source code Copyright(c) 1998-2004 Matt Pharr and Greg Humphreys
*
* All Rights Reserved.
* For educational use only; commercial use expressly forbidden.
* NO WARRANTY, express or implied, for this software.
* (See file License.txt for complete license)
*/
// geometry.cpp*
#include "geometry.h"
// BBox Method Definitions
BBox Union(const BBox &b, const Point &p) {
BBox ret = b;
ret.pMin.x = min(b.pMin.x, p.x);
ret.pMin.y = min(b.pMin.y, p.y);
ret.pMin.z = min(b.pMin.z, p.z);
ret.pMax.x = max(b.pMax.x, p.x);
ret.pMax.y = max(b.pMax.y, p.y);
ret.pMax.z = max(b.pMax.z, p.z);
return ret;
}
BBox Union(const BBox &b, const BBox &b2) {
BBox ret;
ret.pMin.x = min(b.pMin.x, b2.pMin.x);
ret.pMin.y = min(b.pMin.y, b2.pMin.y);
ret.pMin.z = min(b.pMin.z, b2.pMin.z);
ret.pMax.x = max(b.pMax.x, b2.pMax.x);
ret.pMax.y = max(b.pMax.y, b2.pMax.y);
ret.pMax.z = max(b.pMax.z, b2.pMax.z);
return ret;
}
void BBox::BoundingSphere(Point *c, float *rad) const {
*c = .5f * pMin + .5f * pMax;
*rad = Distance(*c, pMax);
}
bool BBox::IntersectP(const Ray &ray, float *hitt0,
float *hitt1) const {
float t0 = ray.mint, t1 = ray.maxt;
for (int i = 0; i < 3; ++i) {
// Update interval for _i_th bounding box slab
float invRayDir = 1.f / ray.d[i];
float tNear = (pMin[i] - ray.o[i]) * invRayDir;
float tFar = (pMax[i] - ray.o[i]) * invRayDir;
// Update parametric interval from slab intersection $t$s
if (tNear > tFar) swap(tNear, tFar);
t0 = tNear > t0 ? tNear : t0;
t1 = tFar < t1 ? tFar : t1;
if (t0 > t1) return false;
}
if (hitt0) *hitt0 = t0;
if (hitt1) *hitt1 = t1;
return true;
}