forked from juj/MathGeoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRay.cpp
438 lines (375 loc) · 9.27 KB
/
Ray.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Copyright Jukka Jylänki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/** @file Ray.cpp
@author Jukka Jylänki
@brief Implementation for the Ray geometry object. */
#include "Ray.h"
#include "AABB.h"
#include "Line.h"
#include "LineSegment.h"
#include "../Math/float3x3.h"
#include "../Math/float3x4.h"
#include "../Math/float4x4.h"
#include "OBB.h"
#include "Plane.h"
#include "Polygon.h"
#include "Polyhedron.h"
#include "Frustum.h"
#include "../Math/Quat.h"
#include "Sphere.h"
#include "Capsule.h"
#include "Triangle.h"
#include "Circle.h"
#include "../Math/MathFunc.h"
#ifdef MATH_ENABLE_STL_SUPPORT
#include <iostream>
#endif
MATH_BEGIN_NAMESPACE
Ray::Ray(const vec &pos_, const vec &dir_)
:pos(pos_), dir(dir_)
{
assume2(dir.IsNormalized(), dir, dir.LengthSq());
}
Ray::Ray(const Line &line)
:pos(line.pos), dir(line.dir)
{
assume2(dir.IsNormalized(), dir, dir.LengthSq());
}
Ray::Ray(const LineSegment &lineSegment)
:pos(lineSegment.a), dir(lineSegment.Dir())
{
}
bool Ray::IsFinite() const
{
return pos.IsFinite() && dir.IsFinite();
}
vec Ray::GetPoint(float d) const
{
assume2(dir.IsNormalized(), dir, dir.LengthSq());
return pos + d * dir;
}
void Ray::Translate(const vec &offset)
{
pos += offset;
}
void Ray::Transform(const float3x3 &transform)
{
pos = transform.Transform(pos);
dir = transform.Transform(dir);
}
void Ray::Transform(const float3x4 &transform)
{
pos = transform.MulPos(pos);
dir = transform.MulDir(dir);
}
void Ray::Transform(const float4x4 &transform)
{
pos = transform.MulPos(pos);
dir = transform.MulDir(dir);
}
void Ray::Transform(const Quat &transform)
{
pos = transform.Transform(pos);
dir = transform.Transform(dir);
}
bool Ray::Contains(const vec &point, float distanceThreshold) const
{
return ClosestPoint(point).DistanceSq(point) <= distanceThreshold;
}
bool Ray::Contains(const LineSegment &lineSegment, float distanceThreshold) const
{
return Contains(lineSegment.a, distanceThreshold) && Contains(lineSegment.b, distanceThreshold);
}
bool Ray::Equals(const Ray &rhs, float epsilon) const
{
return pos.Equals(rhs.pos, epsilon) && dir.Equals(rhs.dir, epsilon);
}
float Ray::Distance(const vec &point, float &d) const
{
return ClosestPoint(point, d).Distance(point);
}
float Ray::Distance(const Ray &other, float &d, float &d2) const
{
vec c = ClosestPoint(other, d, d2);
return c.Distance(other.GetPoint(d2));
}
float Ray::Distance(const Line &other, float &d, float &d2) const
{
vec c = ClosestPoint(other, d, d2);
return c.Distance(other.GetPoint(d2));
}
float Ray::Distance(const LineSegment &other, float &d, float &d2) const
{
vec c = ClosestPoint(other, d, d2);
return c.Distance(other.GetPoint(d2));
}
float Ray::Distance(const Sphere &sphere) const
{
return Max(0.f, Distance(sphere.pos) - sphere.r);
}
float Ray::Distance(const Capsule &capsule) const
{
return Max(0.f, Distance(capsule.l) - capsule.r);
}
vec Ray::ClosestPoint(const vec &targetPoint, float &d) const
{
d = Max(0.f, Dot(targetPoint - pos, dir));
return GetPoint(d);
}
vec Ray::ClosestPoint(const Ray &other, float &d, float &d2) const
{
Line::ClosestPointLineLine(pos, dir, other.pos, other.dir, d, d2);
if (d < 0.f && d2 < 0.f)
{
vec closestPoint = ClosestPoint(other.pos, d);
vec closestPoint2 = other.ClosestPoint(pos, d2);
if (closestPoint.DistanceSq(other.pos) <= closestPoint2.DistanceSq(pos))
{
d2 = 0.f;
return closestPoint;
}
else
{
d = 0.f;
return pos;
}
}
else if (d < 0.f)
{
d = 0.f;
other.ClosestPoint(pos, d2);
d2 = Max(0.f, d2);
return pos;
}
else if (d2 < 0.f)
{
vec pt = ClosestPoint(other.pos, d);
d = Max(0.f, d);
d2 = 0.f;
return pt;
}
else
{
return GetPoint(d);
}
}
vec Ray::ClosestPoint(const Line &other, float &d, float &d2) const
{
Line::ClosestPointLineLine(pos, dir, other.pos, other.dir, d, d2);
if (d < 0.f)
{
d = 0.f;
other.ClosestPoint(pos, d2);
return pos;
}
else
return GetPoint(d);
}
vec Ray::ClosestPoint(const LineSegment &other, float &d, float &d2) const
{
Line::ClosestPointLineLine(pos, dir, other.a, other.b - other.a, d, d2);
if (d < 0.f)
{
d = 0.f;
if (d2 >= 0.f && d2 <= 1.f)
{
other.ClosestPoint(pos, d2);
return pos;
}
vec p;
float t2;
if (d2 < 0.f)
{
p = other.a;
t2 = 0.f;
}
else // u2 > 1.f
{
p = other.b;
t2 = 1.f;
}
vec closestPoint = ClosestPoint(p, d);
vec closestPoint2 = other.ClosestPoint(pos, d2);
if (closestPoint.DistanceSq(p) <= closestPoint2.DistanceSq(pos))
{
d2 = t2;
return closestPoint;
}
else
{
d = 0.f;
return pos;
}
}
else if (d2 < 0.f)
{
d2 = 0.f;
return ClosestPoint(other.a, d);
}
else if (d2 > 1.f)
{
d2 = 1.f;
return ClosestPoint(other.b, d);
}
else
return GetPoint(d);
}
bool Ray::Intersects(const Triangle &triangle, float *d, vec *intersectionPoint) const
{
return triangle.Intersects(*this, d, intersectionPoint);
}
bool Ray::Intersects(const Triangle &triangle) const
{
float u, v;
float t = Triangle::IntersectLineTri(pos, dir, triangle.a, triangle.b, triangle.c, u, v);
if (t < 0.f || t == FLOAT_INF)
return false;
return true;
}
bool Ray::Intersects(const Plane &plane, float *d) const
{
return plane.Intersects(*this, d);
}
bool Ray::Intersects(const Plane &plane) const
{
return plane.Intersects(*this, 0);
}
bool Ray::Intersects(const Sphere &sphere, vec *intersectionPoint, vec *intersectionNormal, float *d) const
{
return sphere.Intersects(*this, intersectionPoint, intersectionNormal, d) > 0;
}
bool Ray::Intersects(const Sphere &sphere) const
{
return sphere.Intersects(*this, 0, 0, 0) > 0;
}
bool Ray::Intersects(const AABB &aabb) const
{
return aabb.Intersects(*this);
}
bool Ray::Intersects(const AABB &aabb, float &dNear, float &dFar) const
{
return aabb.Intersects(*this, dNear, dFar);
}
bool Ray::Intersects(const OBB &obb, float &dNear, float &dFar) const
{
return obb.Intersects(*this, dNear, dFar);
}
bool Ray::Intersects(const OBB &obb) const
{
return obb.Intersects(*this);
}
bool Ray::Intersects(const Capsule &capsule) const
{
return capsule.Intersects(*this);
}
bool Ray::Intersects(const Polygon &polygon) const
{
return polygon.Intersects(*this);
}
bool Ray::Intersects(const Frustum &frustum) const
{
return frustum.Intersects(*this);
}
bool Ray::Intersects(const Polyhedron &polyhedron) const
{
return polyhedron.Intersects(*this);
}
bool Ray::IntersectsDisc(const Circle &disc) const
{
return disc.IntersectsDisc(*this);
}
Line Ray::ToLine() const
{
return Line(pos, dir);
}
LineSegment Ray::ToLineSegment(float d) const
{
return LineSegment(pos, GetPoint(d));
}
LineSegment Ray::ToLineSegment(float dStart, float dEnd) const
{
return LineSegment(GetPoint(dStart), GetPoint(dEnd));
}
void Ray::ProjectToAxis(const vec &direction, float &outMin, float &outMax) const
{
outMin = outMax = Dot(direction, pos);
float d = Dot(direction, dir);
// Most of the time, the projection interval will be a half-infinite range, extending to either -inf or +inf.
if (d > 1e-4f)
outMax = FLOAT_INF;
else if (d < -1e4f)
outMin = -FLOAT_INF;
}
#ifdef MATH_ENABLE_STL_SUPPORT
std::string Ray::ToString() const
{
char str[256];
sprintf(str, "Ray(Pos:(%.2f, %.2f, %.2f) Dir:(%.3f, %.3f, %.3f))", pos.x, pos.y, pos.z, dir.x, dir.y, dir.z);
return str;
}
std::string Ray::SerializeToString() const
{
char str[256];
char *s = SerializeFloat(pos.x, str); *s = ','; ++s;
s = SerializeFloat(pos.y, s); *s = ','; ++s;
s = SerializeFloat(pos.z, s); *s = ','; ++s;
s = SerializeFloat(dir.x, s); *s = ','; ++s;
s = SerializeFloat(dir.y, s); *s = ','; ++s;
s = SerializeFloat(dir.z, s);
assert(s+1 - str < 256);
MARK_UNUSED(s);
return str;
}
std::string Ray::SerializeToCodeString() const
{
return "Ray(" + pos.SerializeToCodeString() + "," + dir.SerializeToCodeString() + ")";
}
std::ostream &operator <<(std::ostream &o, const Ray &ray)
{
o << ray.ToString();
return o;
}
#endif
Ray Ray::FromString(const char *str, const char **outEndStr)
{
assume(str);
if (!str)
return Ray(vec::nan, vec::nan);
Ray r;
MATH_SKIP_WORD(str, "Ray(");
MATH_SKIP_WORD(str, "Pos:(");
r.pos = PointVecFromString(str, &str);
MATH_SKIP_WORD(str, " Dir:(");
r.dir = DirVecFromString(str, &str);
if (outEndStr)
*outEndStr = str;
return r;
}
Ray operator *(const float3x3 &transform, const Ray &ray)
{
assume(transform.IsInvertible());
return Ray(transform * ray.pos, (transform * ray.dir).Normalized());
}
Ray operator *(const float3x4 &transform, const Ray &ray)
{
assume(transform.IsInvertible());
return Ray(transform.MulPos(ray.pos), transform.MulDir(ray.dir).Normalized());
}
Ray operator *(const float4x4 &transform, const Ray &ray)
{
assume(transform.IsInvertible());
return Ray(transform.MulPos(ray.pos), transform.MulDir(ray.dir).Normalized());
}
Ray operator *(const Quat &transform, const Ray &ray)
{
return Ray(transform * ray.pos, transform * ray.dir);
}
MATH_END_NAMESPACE