-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.cpp
245 lines (237 loc) · 8.09 KB
/
box.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
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
#include "box.h"
#include "material.h"
/*! Return boolean value if box is hit by specified ray.
*
* Example:
*
* Box::Hit(ray, hitrec, 0.001, DBL_MAX);
*/
bool Box::Hit(const Ray &r, HitRecord &rec, double tMin, double tMax) const {
Vector3D bounds[2] = { m_vBounds[0], m_vBounds[1] };
double tmin = (bounds[r.m_iSign[0]].x() - r.m_vA.x()) * r.m_vInvDir.x();
double tmax = (bounds[1 - r.m_iSign[0]].x() - r.m_vA.x()) * r.m_vInvDir.x();
double tymin = (bounds[r.m_iSign[1]].y() - r.m_vA.y()) * r.m_vInvDir.y();
double tymax = (bounds[1 - r.m_iSign[1]].y() - r.m_vA.y()) * r.m_vInvDir.y();
if ((tmin > tymax) || (tymin > tmax)) { return false; }
if (tymin > tmin) { tmin = tymin; }
if (tymax < tmax) { tmax = tymax; }
double tzmin = (bounds[r.m_iSign[2]].z() - r.m_vA.z()) * r.m_vInvDir.z();
double tzmax = (bounds[1 - r.m_iSign[2]].z() - r.m_vA.z()) * r.m_vInvDir.z();
if ((tmin > tzmax) || (tzmin > tmax)) { return false; }
if (tzmin > tmin) { tmin = tzmin; }
if (tzmax < tmax) { tmax = tzmax; }
double dT = tmin;
if (dT < 0) {
dT = tmax;
if (dT < 0) return false;
}
if (m_pmCurMat->MatType() == 0 || m_pmCurMat->MatType() == 2) { dT *= 1.000001; }
else { dT *= 1.76000001; }
rec = { dT, r.PointAtParameter(dT), NormalCalc(r.PointAtParameter(dT)), m_pmCurMat };
return true;
}
/*! Return Vector3D surface normal for specified Vector3D point on ray.
*
* Example:
*
* Box::NormalCalc(vP);
*/
Vector3D Box::NormalCalc(const Vector3D vP) const {
// Intersects within front face
if (vP.x() > m_vCenter.x() && (vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x())) {
// Falls on top or bottom edge
if ((vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y()) && (vP.z() != m_vBounds[0].z() && vP.z() != m_vBounds[1].z())) {
// Falls on top edge
if (vP.y() > m_vCenter.y()) {
return Vector3D(1, 1, 0) * vP;
}
// Falls on bottom edge
else if (vP.y() < m_vCenter.y()) {
return Vector3D(1, 1, 0) * vP;
}
}
// Falls on left or right edge
else if ((vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z()) && (vP.y() != m_vBounds[0].y() && vP.y() != m_vBounds[1].y())) {
// Falls on right edge
if (vP.z() < m_vCenter.x()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on left edge
else if (vP.z() > m_vCenter.z()) {
return Vector3D(1, 0, 1) * vP;
}
}
// Top left corner
else if (vP.z() == m_vBounds[1].z() && vP.y() == m_vBounds[1].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Bottom left corner
else if (vP.z() == m_vBounds[0].z() && vP.y() == m_vBounds[1].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Top right corner
else if (vP.z() == m_vBounds[1].z() && vP.y() == m_vBounds[0].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Bottom right corner
else if (vP.z() == m_vBounds[0].z() && vP.y() == m_vBounds[0].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Falls within center of face
else { return Vector3D(1, 0, 0) * vP; }
}
// Intersects within back face
else if (vP.x() < m_vCenter.x() && (vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x())) {
// Falls on top or bottom edge
if ((vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y()) && (vP.z() != m_vBounds[0].z() && vP.z() != m_vBounds[1].z())) {
// Falls on top edge
if (vP.y() > m_vCenter.y()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on bottom edge
else if (vP.y() < m_vCenter.y()) {
return Vector3D(1, 0, 1) * vP;
}
}
// Falls on left or right edge
else if ((vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z()) && (vP.y() != m_vBounds[0].y() && vP.y() != m_vBounds[1].y())) {
// Falls on right edge
if (vP.z() < m_vCenter.z()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on left edge
else if (vP.z() > m_vCenter.z()) {
return Vector3D(1, 0, 1) * vP;
}
}
// Top left corner
else if (vP.x() == m_vBounds[1].x() && vP.y() == m_vBounds[1].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Bottom left corner
else if (vP.x() == m_vBounds[0].x() && vP.y() == m_vBounds[1].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Top right corner
else if (vP.x() == m_vBounds[1].x() && vP.y() == m_vBounds[0].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Bottom right corner
else if (vP.x() == m_vBounds[0].x() && vP.y() == m_vBounds[0].y()) {
return Vector3D(1, 1, 1) * vP;
}
// Falls within center of face
else { return Vector3D(-1, 0, 0) * vP; }
}
// Intersects within left face
else if (vP.z() > m_vCenter.z() && (vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z())) {
// Falls on top or bottom edge
if ((vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y()) && (vP.x() != m_vBounds[0].x() && vP.x() != m_vBounds[1].x())) {
// Falls on top edge
if (vP.y() > m_vCenter.y()) {
return Vector3D(0, 1, 1) * vP;
}
// Falls on bottom edge
else if (vP.y() < m_vCenter.y()) {
return Vector3D(0, 1, 1) * vP;
}
}
// Falls on left or right edge
else if ((vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x()) && (vP.y() != m_vBounds[0].y() && vP.y() != m_vBounds[1].y())) {
// Falls on right edge
if (vP.x() > m_vCenter.x()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on left edge
else if (vP.x() < m_vCenter.x()) {
return Vector3D(1, 0, 1) * vP;
}
}
// Falls within center of face
else { return Vector3D(0, 0, 1) * vP; }
}
// Intersects within right face
else if (vP.z() < m_vCenter.z() && (vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z())) {
// Falls on top or bottom edge
if ((vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y()) && (vP.x() != m_vBounds[0].x() && vP.x() != m_vBounds[1].x())) {
// Falls on top edge
if (vP.y() > m_vCenter.y()) {
return Vector3D(1, 1, 0) * vP;
}
// Falls on bottom edge
else if (vP.y() < m_vCenter.y()) {
return Vector3D(1, 1, 0) * vP;
}
}
// Falls on left or right edge
else if ((vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x()) && (vP.y() != m_vBounds[0].y() && vP.y() != m_vBounds[1].y())) {
// Falls on right edge
if (vP.x() < m_vCenter.x()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on left edge
else if (vP.x() > m_vCenter.x()) {
return Vector3D(1, 0, 1) * vP;
}
}
// Falls within center of face
else { return Vector3D(0, 0, -1) * vP; }
}
// Intersects within top face
else if (vP.y() > m_vCenter.y() && (vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y())) {
// Falls on top or bottom edge
if ((vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x()) && (vP.z() != m_vBounds[0].z() && vP.z() != m_vBounds[1].z())) {
// Falls on top edge
if (vP.x() < m_vCenter.x()) {
return Vector3D(1, 1, 0) * vP;
}
// Falls on bottom edge
else if (vP.x() > m_vCenter.x()) {
return Vector3D(1, 1, 0) * vP;
}
}
// Falls on left or right edge
else if ((vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z()) && (vP.x() != m_vBounds[0].x() && vP.x() != m_vBounds[1].x())) {
// Falls on right edge
if (vP.z() < m_vCenter.z()) {
return Vector3D(0, 1, 1) * vP;
}
// Falls on left edge
else if (vP.z() > m_vCenter.z()) {
return Vector3D(0, 1, 1) * vP;
}
}
// Falls within center of face
else { return Vector3D(0, 1, 0) * vP; }
}
// Intersects within bottom face
else if (vP.y() < m_vCenter.y() && (vP.y() == m_vBounds[0].y() || vP.y() == m_vBounds[1].y())) {
// Falls on top or bottom edge
if ((vP.x() == m_vBounds[0].x() || vP.x() == m_vBounds[1].x()) && (vP.z() != m_vBounds[0].z() && vP.z() != m_vBounds[1].z())) {
// Falls on top edge
if (vP.x() > m_vCenter.x()) {
return Vector3D(1, 1, 0) * vP;
}
// Falls on bottom edge
else if (vP.x() < m_vCenter.x()) {
return Vector3D(1, 1, 0) * vP;
}
}
// Falls on left or right edge
else if ((vP.z() == m_vBounds[0].z() || vP.z() == m_vBounds[1].z()) && (vP.x() != m_vBounds[0].x() && vP.x() != m_vBounds[1].x())) {
// Falls on right edge
if (vP.z() < m_vCenter.z()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls on left edge
else if (vP.z() > m_vCenter.z()) {
return Vector3D(1, 0, 1) * vP;
}
// Falls within center of face
else { return Vector3D(0, -1, 0) * vP; }
}
}
else {
// Ray outside of bounds
}
}