-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes.cpp
More file actions
286 lines (252 loc) · 10.6 KB
/
shapes.cpp
File metadata and controls
286 lines (252 loc) · 10.6 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "shapes.h"
#include "vector.h"
#include <cmath>
#include <utility>
#include <algorithm>
static constexpr double inv_pi = 1.0 / M_PI;
static constexpr double inv_2pi = 1.0 / (2.0 * M_PI);
Sphere::Sphere(const Vector3& pos, double r) {
position = pos;
radius = r;
aabb = AABB(position - Vector3(radius, radius, radius), position + Vector3(radius, radius, radius));
}
double Sphere::volume() const {
return (4.0 / 3.0) * M_PI * radius * radius * radius;
}
double Sphere::surfaceArea() const {
return 4.0 * M_PI * radius * radius;
}
CollisionInfo Sphere::collide_ray(Vector3& origin, Vector3& direction, Vector3& inv_dir) const {
CollisionInfo info;
// Compute AABB first
if (check_aabbs && !aabb.collide_ray_simple(origin, direction, inv_dir)) {
return info; // No intersection with AABB
}
// Collide with a ray
Vector3 oc = origin - position;
double b = 2.0 * oc.dot(direction);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * c;
if (discriminant < 0) {
// No intersection
return info;
} else {
// Compute intersection point
double sqrt_discriminant = sqrt(discriminant);
double t = (-b - sqrt_discriminant) * 0.5;
if (t < 0) {
t = (-b + sqrt_discriminant) * 0.5;
}
if (t < 0) {
return info;
}
info.hit = true;
info.distance = t;
info.point = origin + direction * t;
info.normal = (info.point - position).normalize();
info.material = material;
if (material.has_texture || material.has_emission_map || material.has_roughness_map || material.has_specular_map || material.has_refraction_map) {
Vector3 n = info.normal;
double u = 0.5 + (atan2(n.z, n.x) * inv_2pi);
double v = 0.5 - (asin(n.y) * inv_pi);
info.uv = Vector3(u, v, 0);
}
return info;
}
}
void Sphere::update_aabb() {
aabb = AABB(position - Vector3(radius), position + Vector3(radius));
}
double AABB::volume() const {
Vector3 diff = max - min;
return diff.x * diff.y * diff.z;
}
double AABB::surfaceArea() const {
Vector3 diff = max - min;
return 2.0 * (diff.x * diff.y + diff.y * diff.z + diff.z * diff.x);
}
bool AABB::collide_ray_simple(Vector3& origin, Vector3& direction, Vector3& inv_dir) const {
// Compute slab intersection for X
double t1x = (min.x - origin.x) * inv_dir.x;
double t2x = (max.x - origin.x) * inv_dir.x;
double tminx = t1x < t2x ? t1x : t2x;
double tmaxx = t1x > t2x ? t1x : t2x;
// Y slab
double t1y = (min.y - origin.y) * inv_dir.y;
double t2y = (max.y - origin.y) * inv_dir.y;
double tminy = t1y < t2y ? t1y : t2y;
double tmaxy = t1y > t2y ? t1y : t2y;
// Intersect X and Y intervals early
double tnear = tminx > tminy ? tminx : tminy;
double tfar = tmaxx < tmaxy ? tmaxx : tmaxy;
if (tnear > tfar) return false;
// Z slab
double t1z = (min.z - origin.z) * inv_dir.z;
double t2z = (max.z - origin.z) * inv_dir.z;
double tminz = t1z < t2z ? t1z : t2z;
double tmaxz = t1z > t2z ? t1z : t2z;
// Final intersection
tnear = tnear > tminz ? tnear : tminz;
tfar = tfar < tmaxz ? tfar : tmaxz;
if (tnear > tfar) return false;
// Valid if box is in front of the ray
return tfar >= 0.0;
}
CollisionInfo AABB::collide_ray(Vector3& origin, Vector3& direction, Vector3& inv_dir) const {
CollisionInfo info;
Vector3 tmin = (min - origin) * inv_dir;
Vector3 tmax = (max - origin) * inv_dir;
Vector3 t1 = Vector3(std::min(tmin.x, tmax.x), std::min(tmin.y, tmax.y), std::min(tmin.z, tmax.z));
Vector3 t2 = Vector3(std::max(tmin.x, tmax.x), std::max(tmin.y, tmax.y), std::max(tmin.z, tmax.z));
double t_near = std::max(std::max(t1.x, t1.y), t1.z);
double t_far = std::min(std::min(t2.x, t2.y), t2.z);
if (t_near > t_far || t_far < 0) {
return info; // No intersection
}
info.hit = true;
info.distance = (t_near >= 0) ? t_near : t_far;
info.point = origin + direction * info.distance;
// Compute normal
Vector3 hitPoint = info.point;
if (fabs(hitPoint.x - min.x) < 1e-6) info.normal = Vector3(-1, 0, 0);
else if (fabs(hitPoint.x - max.x) < 1e-6) info.normal = Vector3(1, 0, 0);
else if (fabs(hitPoint.y - min.y) < 1e-6) info.normal = Vector3(0, -1, 0);
else if (fabs(hitPoint.y - max.y) < 1e-6) info.normal = Vector3(0, 1, 0);
else if (fabs(hitPoint.z - min.z) < 1e-6) info.normal = Vector3(0, 0, -1);
else if (fabs(hitPoint.z - max.z) < 1e-6) info.normal = Vector3(0, 0, 1);
if (material.has_texture || material.has_emission_map || material.has_roughness_map || material.has_specular_map || material.has_refraction_map) {
Vector3 size = max - min;
Vector3 local = hitPoint - min;
double u, v;
if (fabs(local.x) < 1e-6) { // Left face
u = local.z / size.z;
v = local.y / size.y;
} else if (fabs(local.x - size.x) < 1e-6) { // Right face
u = 1.0 - (local.z / size.z);
v = local.y / size.y;
} else if (fabs(local.y) < 1e-6) { // Bottom face
u = local.x / size.x;
v = local.z / size.z;
} else if (fabs(local.y - size.y) < 1e-6) { // Top face
u = local.x / size.x;
v = 1.0 - (local.z / size.z);
} else if (fabs(local.z) < 1e-6) { // Back face
u = local.x / size.x;
v = local.y / size.y;
} else { // Front face
u = 1.0 - (local.x / size.x);
v = local.y / size.y;
}
info.uv = Vector3(u, v, 0);
}
info.material = material;
return info;
}
Box::Box(const Vector3& pos, const Vector3& sz, const Vector3& rot) {
position = pos;
size = sz;
rotation = rot;
// Compute AABB in world space (conservative)
Vector3 half_diag = Vector3(sz.magnitude());
aabb = AABB(position - half_diag, position + half_diag);
}
double Box::volume() const {
return 8.0 * size.x * size.y * size.z;
}
double Box::surfaceArea() const {
return 8.0 * (size.x * size.y + size.y * size.z + size.z * size.x);
}
CollisionInfo Box::collide_ray(Vector3& origin, Vector3& direction, Vector3& inv_dir) const {
CollisionInfo info;
// Quick AABB reject in world space if enabled
if (check_aabbs && !aabb.collide_ray_simple(origin, direction, inv_dir)) {
return info;
}
if (rotation != last_rotation) {
// Build rotation matrix from Euler angles (order Y -> X -> Z)
Vector3 rotation_rad = rotation * (M_PI / 180.0);
double cy = cos(rotation_rad.y);
double sy = sin(rotation_rad.y);
double cx = cos(rotation_rad.x);
double sx = sin(rotation_rad.x);
double cz = cos(rotation_rad.z);
double sz = sin(rotation_rad.z);
// R = R_y * R_x * R_z
double m00 = cy * cz + sx * sy * sz;
double m01 = -cy * sz + sx * sy * cz;
double m02 = cx * sy;
double m10 = cx * sz;
double m11 = cx * cz;
double m12 = -sx;
double m20 = -sy * cz + sx * cy * sz;
double m21 = sy * sz + sx * cy * cz;
double m22 = cx * cy;
rotation_matrix[0] = Vector3(m00, m01, m02);
rotation_matrix[1] = Vector3(m10, m11, m12);
rotation_matrix[2] = Vector3(m20, m21, m22);
last_rotation = rotation;
}
// Helper: rotate a vector by R (world = position + R * local)
auto rotate_world = [&](const Vector3 &v) -> Vector3 {
return Vector3(
rotation_matrix[0].x * v.x + rotation_matrix[0].y * v.y + rotation_matrix[0].z * v.z,
rotation_matrix[1].x * v.x + rotation_matrix[1].y * v.y + rotation_matrix[1].z * v.z,
rotation_matrix[2].x * v.x + rotation_matrix[2].y * v.y + rotation_matrix[2].z * v.z
);
};
// For transforming world -> local we use R^T (inverse of orthonormal R)
auto rotate_local = [&](const Vector3 &v) -> Vector3 {
return Vector3(
rotation_matrix[0].x * v.x + rotation_matrix[1].x * v.y + rotation_matrix[2].x * v.z,
rotation_matrix[0].y * v.x + rotation_matrix[1].y * v.y + rotation_matrix[2].y * v.z,
rotation_matrix[0].z * v.x + rotation_matrix[1].z * v.y + rotation_matrix[2].z * v.z
);
};
// Transform ray into box local space
Vector3 local_origin = rotate_local(origin - position);
Vector3 local_direction = rotate_local(direction);
Vector3 local_invdir = 1 / local_direction;
// Local AABB spans [-size, size]
AABB local_aabb = AABB(size * -1, size);
info = local_aabb.collide_ray(local_origin, local_direction, local_invdir);
if (!info.hit) {
return info;
}
// Transform collision data back to world space.
Vector3 local_point = info.point; // in local space
Vector3 local_normal = info.normal; // in local space
Vector3 world_point = position + rotate_world(local_point);
Vector3 world_normal = rotate_world(local_normal).normalize();
if (material.has_texture || material.has_emission_map || material.has_roughness_map || material.has_specular_map || material.has_refraction_map) {
double u, v;
if (fabs(local_point.x + size.x) < 1e-6) { // Left face
u = (local_point.z + size.z) / (2 * size.z);
v = (local_point.y + size.y) / (2 * size.y);
} else if (fabs(local_point.x - size.x) < 1e-6) { // Right face
u = 1.0 - ((local_point.z + size.z) / (2 * size.z));
v = (local_point.y + size.y) / (2 * size.y);
} else if (fabs(local_point.y + size.y) < 1e-6) { // Bottom face
u = (local_point.x + size.x) / (2 * size.x);
v = (local_point.z + size.z) / (2 * size.z);
} else if (fabs(local_point.y - size.y) < 1e-6) { // Top face
u = (local_point.x + size.x) / (2 * size.x);
v = 1.0 - ((local_point.z + size.z) / (2 * size.z));
} else if (fabs(local_point.z + size.z) < 1e-6) { // Back face
u = (local_point.x + size.x) / (2 * size.x);
v = (local_point.y + size.y) / (2 * size.y);
} else { // Front face
u = 1.0 - ((local_point.x + size.x) / (2 * size.x));
v = (local_point.y + size.y) / (2 * size.y);
}
info.uv = Vector3(u, v, 0);
}
info.point = world_point;
info.normal = world_normal;
info.material = material;
// distance is parameter along direction; rotation preserves direction length so it remains valid.
return info;
}
void Box::update_aabb() {
Vector3 half_diag = Vector3(size.magnitude());
aabb = AABB(position - half_diag, position + half_diag);
}