-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.h
230 lines (202 loc) · 6.58 KB
/
core.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once
#include "math.h"
#include "randomness.h"
#include <iostream>
#include <boost/format.hpp>
using std::min;
using std::max;
class Geom;
/**
* A directed line segment.
*/
struct Ray {
Vec origin; /**< The point at which the ray starts. */
Vec direction; /**< The direction and magnitude of the ray. */
/**
* Constructs a ray with the given origin and direction.
*
* @param o the ray's origin
* @param d the ray's direction (and magnitude, if desired)
*/
Ray(const Vec& o, const Vec& d) : origin(o), direction(d) {}
/**
* Constructs a ray with origin at (0, 0, 0) and neither direction nor
* magnitude.
*/
Ray() : origin(0, 0, 0), direction(0, 0, 0) {}
/**
* Returns an interpolated point on the ray, parameterized by a number d.
* Where d = 0, the origin is returned and where d = 1, (origin + direction)
* is returned. This linear interpolation also works for d < 0 and d > 1.
*
* @param d the parameter at which to return a point
* @returns the point on the ray given by the parameter
*/
inline Vec at(float d) const { return origin + direction * d; }
friend std::ostream& operator<<(std::ostream& os, const Ray& r) {
os << boost::format("<origin: %1%, direction: %2%>")
% r.origin % r.direction;
return os;
}
};
/**
* An axis-aligned bounding box.
*/
struct BBox {
Vec lower; /**< The lower X, Y, and Z-axis bounds. */
Vec upper; /**< The upper X, Y, and Z-axis bounds. */
/** Constructs an empty bbox. */
BBox() : lower(0, 0, 0), upper(0, 0, 0) {}
/** Constructs a bbox containing the two given points. */
BBox(const Vec& a, const Vec& b) {
lower = Vec(min(a.x(), b.x()), min(a.y(), b.y()), min(a.z(), b.z()));
upper = Vec(max(a.x(), b.x()), max(a.y(), b.y()), max(a.z(), b.z()));
}
/** Expands the bbox to also contain the given point. */
inline void expand(const Vec& v) {
lower = Vec(
min(lower.x(), v.x()),
min(lower.y(), v.y()),
min(lower.z(), v.z())
);
upper = Vec(
max(upper.x(), v.x()),
max(upper.y(), v.y()),
max(upper.z(), v.z())
);
}
/**
* Expands the dimensions of the bbox by a given amount along all six faces
* of the box. The result is that the bbox is expanded by twice the given
* amount along each axis.
*/
inline void expand(float f = math::VERY_SMALL) {
lower.x() -= f;
lower.y() -= f;
lower.z() -= f;
upper.x() += f;
upper.y() += f;
upper.z() += f;
}
/**
* Expands the bbox to also contain another given bbox.
*/
inline void expand(const BBox& b) {
lower = Vec(
min(lower.x(), b.lower.x()),
min(lower.y(), b.lower.y()),
min(lower.z(), b.lower.z())
);
upper = Vec(
max(upper.x(), b.upper.x()),
max(upper.y(), b.upper.y()),
max(upper.z(), b.upper.z())
);
}
/**
* Returns the surface area of the bbox.
*/
inline float surfaceArea() const {
Vec d = upper - lower;
return 2.0f * (d.x() * d.y() + d.x() * d.z() + d.y() * d.z());
}
/**
* Return the longest axis of the bbox.
*/
inline Axis maximumExtent() const {
Vec d = upper - lower;
if (d.x() > d.y() && d.x() > d.z()) {
return X_AXIS;
} else if (d.y() > d.z()) {
return Y_AXIS;
} else {
return Z_AXIS;
}
}
/**
* Calculates the intersection of a ray with the bbox. (If there is an
* intersection, then there will be two -- the entrance and exit points.)
* This is from Pharr and Humphreys.
*
* @param r the ray to shoot through the box
* @param t0_out [out] the parameter of the ray at the first intersection
* point (the entrance point)
* @param t1_out [out] the parameter of the ray at the second intersection
* point (the exit point)
* @returns true if intersections were found, false if no hit
*/
inline bool intersect(const Ray &r, float* t0_out, float* t1_out) const {
float t0 = 0.0f;
float t1 = std::numeric_limits<float>::max();
for (int i = 0; i < 3; ++i) {
// Update interval for `i`th bounding box slab.
float invRayDir = 1.0f / r.direction[i];
float tNear = (lower[i] - r.origin[i]) * invRayDir;
float tFar = (upper[i] - r.origin[i]) * invRayDir;
// Update parametric interval from slab intersection `t`s.
if (tNear > tFar) std::swap(tNear, tFar);
t0 = tNear > t0 ? tNear : t0;
t1 = tFar < t1? tFar : t1;
if (t0 > t1) return false;
}
if (t0_out) *t0_out = t0;
if (t1_out) *t1_out = t1;
return true;
}
friend std::ostream& operator<<(std::ostream& os, const BBox& b) {
os << boost::format("<lower: %1%, upper: %2%>") % b.lower % b.upper;
return os;
}
};
/**
* A bounding sphere.
*/
struct BSphere {
Vec origin;
float radius;
BSphere(const Vec& o, float r = 0.0f) : origin(o), radius(r) {}
BSphere(const BBox& b) {
origin = (b.lower + b.upper) * 0.5f;
radius = fabsf((b.upper - origin).norm());
}
inline bool contains(const Vec& v) const {
return (v - origin).squaredNorm() <= (radius * radius);
}
};
/**
* Contains the information for a ray-object intersection.
*/
struct Intersection {
Vec position; /**< The point of the intersection in 3D space. */
Vec normal; /**< The normal of the surface at the intersection. */
Ray incomingRay; /**< The incoming ray that caused the intersection. */
const Geom* geom; /**< The geometry hit at the intersection. */
float distance; /**< The distance from the ray origin to the intersection. */
/**
* Constructs an intersection with no information.
*/
Intersection()
: position(0, 0, 0), normal(0, 0, 0), incomingRay(), geom(nullptr),
distance(std::numeric_limits<float>::max()) {}
/**
* Constructs an intersection with the given position, normal, and distance.
* @param p the point of the intersection in 3D space
* @param n the normal of the surface at the intersection
* @param r the incoming ray that caused the Intersection
* @param g the geometry hit, or nullptr
* @param d the distance from the ray origin to the intersection
*/
Intersection(const Vec& p, const Vec& n, const Ray& r, const Geom* g, float d)
: position(p), normal(n), incomingRay(r), geom(g), distance(d) {}
};
/**
* Contains the information for a point along a rendering path.
*/
struct RenderVertex : public Intersection {
Vec beta; /**< The transmission throughput (e.g. of radiance). */
/**
* Creates an empty render vertex with the specified beta value.
* @param b the beta (transmission throughput)
*/
RenderVertex(const Vec& b) : Intersection(), beta(b) {}
};