-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection.h
More file actions
697 lines (552 loc) · 16.5 KB
/
Intersection.h
File metadata and controls
697 lines (552 loc) · 16.5 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#pragma once
#ifndef INTERSECTION_H
#define INTERSECTION_H
#include <cmath>
#include <algorithm>
#include <float.h>
#ifndef SUBSPACES_H
#include "Subspaces.h"
#endif
#ifndef CGM_H
#include "cgm.h"
#endif
namespace cliqCity
{
#pragma region Closest Point Tests
template<class Vector>
void ClosestAABBPointToPoint(const AABB<Vector>& aabb, const Vector& point, Vector& cp)
{
int numElements = sizeof(Vector) / sizeof(float);
for (int i = 0; i < numElements; i++)
{
float v = point.pCols[i];
Vector min = aabb.origin - aabb.halfSize;
Vector max = aabb.origin + aabb.halfSize;
if (v < min[i]) v = min[i];
if (v > max[i]) v = max[i];
cp[i] = v;
}
}
template<class Vector, int Dimension>
void ClosestOBBPointToPoint(const OBB<Vector, Dimension>& obb, const Vector& point, Vector& cp)
{
Vector d = point - obb.origin;
// Start result @ obb origin
cp = obb.origin;
int numAxis = Dimension;
for (int i = 0; i < numAxis; i++)
{
// Project d onto axis to get distance along axis to obb origin
float distance = cliqCity::graphicsMath::dot(d, obb.axis[i]);
// Clamp to obb extents
if (distance > obb.halfSize.pCols[i]) distance = obb.halfSize.pCols[i];
if (distance < -obb.halfSize.pCols[i]) distance = -obb.halfSize.pCols[i];
cp += distance * obb.axis[i];
}
}
#pragma endregion
#pragma region Ray - Primitive Tests
template<class Vector>
int IntersectRayPlane(const Ray<Vector>& ray, const Plane<Vector>& plane, Vector& poi, float& t)
{
float denominator = cliqCity::graphicsMath::dot(ray.normal, plane.normal);
// Ray is parallel to plane and there is no intersection
if (denominator == 0.0f)
{
return 0;
}
float numerator = plane.distance - cliqCity::graphicsMath::dot(ray.origin, plane.normal);
t = numerator / denominator;
poi = ray.origin + ray.normal * t;
return 1;
}
template<class Vector>
int IntersectRaySphere(const Ray<Vector>& ray, const Sphere<Vector>& sphere, Vector& poi, float& t)
{
// R(t) = P + tD, where t >= 0
// S(t) = (X - C) * (X - C) = r^2, where X is a point on the surface of the sphere
// Substitute R(t) for X to find the value of t for which the ray intersects the sphere
// Let m = Ray.origin - sphere.origin
Vector m = ray.origin - sphere.origin;
// Let b = projection of ray normal onto vector from rayOrigin to sphereOrigin
float b = cliqCity::graphicsMath::dot(m, ray.normal);
// Let c = difference between distance from rayOrigin to sphereOrigin and sphereRadius
float c = cliqCity::graphicsMath::dot(m, m) - (sphere.radius * sphere.radius);
// If c > 0 rayOrigin is outside of sphere and if b > 0.0f ray is pointing away from sphere.
if (c > 0.0f && b > 0.0f)
{
return 0;
}
float discriminant = b * b - c;
// If discriminant is negative the ray misses the sphere
if (discriminant < 0.0f)
{
return 0;
}
t = -b - sqrt(discriminant);
if (t < 0.0f)
{
t = 0.0f;
}
poi = ray.origin + t * ray.normal;
return 1;
}
template<class Vector>
int IntersectRayAABB(Ray<Vector>& ray, AABB<Vector>& aabb, Vector& poi, float& t)
{
int numElements = sizeof(Vector) / sizeof(float);
float tMin = 0.0f;
float tMax = FLT_MAX;
for (int i = 0; i < numElements; i++)
{
float aabbMin = aabb.origin[i] - aabb.halfSize[i];
float aabbMax = aabb.origin[i] + aabb.halfSize[i];
if (abs(ray.normal[i]) < FLT_EPSILON)
{
// Ray is parallel to slab. Check if origin is contained by plane
if (ray.origin[i] < aabbMin || ray.origin[i] > aabbMax)
{
return 0;
}
}
else
{
float ood = 1.0f / ray.normal[i];
float t1 = (aabbMin - ray.origin[i]) * ood;
float t2 = (aabbMax - ray.origin[i]) * ood;
if (t1 > t2)
{
float temp = t2;
t2 = t1;
t1 = temp;
}
tMin = max(tMin, t1);
tMax = min(tMax, t2);
if (tMin > tMax)
{
return 0;
}
}
}
poi = ray.origin + ray.normal * tMin;
t = tMin;
return 1;
}
template<class Vector>
int IntersectLineAABB(Line<Vector>& line, AABB<Vector>& aabb, Vector& poi, float& t)
{
int numElements = sizeof(Vector) / sizeof(float);
float tMin = 0.0f;
float tMax = 1.0f;
for (int i = 0; i < numElements; i++)
{
Vector normal = line.end - line.origin;
float aabbMin = aabb.origin[i] - aabb.halfSize[i];
float aabbMax = aabb.origin[i] + aabb.halfSize[i];
if (abs(normal[i]) < FLT_EPSILON)
{
// Ray is parallel to slab. Check if origin is contained by plane
if (line.origin[i] < aabbMin || line.origin[i] > aabbMax)
{
return 0;
}
}
else
{
float ood = 1.0f / normal[i];
float t1 = (aabbMin - line.origin[i]) * ood;
float t2 = (aabbMax - line.origin[i]) * ood;
if (t1 > t2)
{
float temp = t2;
t2 = t1;
t1 = temp;
}
tMin = max(tMin, t1);
tMax = min(tMax, t2);
if (tMin > tMax)
{
return 0;
}
}
}
poi = (1 - tMin) * line.origin + tMin * line.end;
t = tMin;
return 1;
}
template<class Vector, int Dimension>
int IntersectRayOBB(Ray<Vector>& ray, OBB<Vector, Dimension>& obb, Vector& poi, float& t)
{
float tMin = 0.0f;
float tMax = FLT_MAX;
Vector rayToObb = obb.origin - ray.origin;
for (int i = 0; i < Dimension; i++)
{
// Projected distance and ray direction onto obb axis
float e = cliqCity::graphicsMath::dot(obb.axis[i], rayToObb);
float f = cliqCity::graphicsMath::dot(obb.axis[i], ray.normal);
if (abs(f) > 0.001f)
{
float inv_f = 1.0f / f;
float t1 = (e + obb.halfSize.pCols[i]) * inv_f;
float t2 = (e - obb.halfSize.pCols[i]) * inv_f;
if (t1 > t2)
{
float temp = t1;
t1 = t2;
t2 = temp;
}
tMin = max(tMin, t1);
tMax = min(tMax, t2);
if (tMin > tMax || tMax < 0.0f)
{
return 0;
}
}
else
{
// Ray is almost parallel to the planes
if (-e - obb.halfSize.pCols[i] > 0.0f || -e + obb.halfSize.pCols[i] < 0.0f)
{
return 0;
}
}
}
t = (tMin > 0.0f) ? tMin : tMax;
poi = ray.origin + ray.normal * t;
return 1;
}
#pragma endregion
#pragma region Sphere - Primitive Tests
template<class Vector>
int IntersectSpherePlane(const Sphere<Vector>& sphere, const Plane<Vector>& plane)
{
// Evaluating the plane equation (N * X) = d for a point gives signed distance from plane
float distance = cliqCity::graphicsMath::dot(plane.normal, sphere.origin) - plane.distance;
// If distance is less than radius we have intersection
return (abs(distance) <= sphere.radius);
}
template<class Vector>
int IntersectDynamicSpherePlane(const Sphere<Vector>& sphere, const Vector& velocity, const Plane<Vector>& plane, Vector& poi, float& t)
{
float distance = cliqCity::graphicsMath::dot(plane.normal, sphere.origin) - plane.distance;
// Check if sphere is all ready overlapping
if (abs(distance) <= sphere.radius)
{
t = 0.0f;
poi = sphere.origin;
return 1;
}
else
{
float denominator = cliqCity::graphicsMath::dot(plane.normal, velocity);
// Check if sphere is moving parallel to (0.0f), or away from (< 0.0f) the plane
if (denominator * distance >= 0.0f)
{
return 0;
}
else
{
// Spere is moving towards the plane
float r = (distance > 0.0f) ? sphere.radius : -sphere.radius;
t = (r - distance) / denominator;
poi = sphere.origin + t * velocity - r * plane.normal;
return 1;
}
}
}
template<class Vector>
int IntersectSphereSphere(const Sphere<Vector>& s0, const Sphere<Vector>& s1)
{
// Spheres are intersecting if distance is less than the sum of radii
Vector distance = s0.origin - s1.origin;
float distanceSquared = cliqCity::graphicsMath::dot(distance, distance);
float sumRadii = (s0.radius + s1.radius);
return (distanceSquared <= (sumRadii * sumRadii));
}
template<class Vector>
int IntersectDynamicSphereSphere(const Sphere<Vector>& s0, const Vector v0, const Sphere<Vector>& s1, const Vector& v1, Vector& poi, float& t)
{
Vector relativeVelocity = v1 - v0;
Vector distance = s1.origin - s0.origin;
float sumRadii = s0.radius + s1.radius;
float c = cliqCity::graphicsMath::dot(distance, distance) - sumRadii * sumRadii;
if (c < 0.0f)
{
t = 0.0f;
poi = s0.origin;
return 1;
}
float a = cliqCity::graphicsMath::dot(relativeVelocity, relativeVelocity);
if (a < FLT_EPSILON)
{
return 0;
}
float b = cliqCity::graphicsMath::dot(relativeVelocity, distance);
if (b >= 0.0f)
{
return 0;
}
float d = b * b - a * c;
if (d < 0.0f)
{
return 0;
}
t = (-b - sqrt(d)) / a;
poi = s0.origin + t * v0; // Probably wrong
return 1;
}
template<class Vector>
int IntersectSphereAABB(const Sphere<Vector>& sphere, const AABB<Vector>& aabb, Vector& cp)
{
// Get closest point on AABB to sphere center
ClosestAABBPointToPoint(aabb, sphere.origin, cp);
Vector d = cp - sphere.origin;
return cliqCity::graphicsMath::dot(d, d) <= sphere.radius * sphere.radius;
}
template<class Vector, int Dimension>
int IntersectSphereOBB(const Sphere<Vector>& sphere, const OBB<Vector, Dimension>& obb, Vector& cp)
{
// Get closest point on OBB to sphere center
ClosestOBBPointToPoint(obb, sphere.origin, cp);
Vector d = cp - sphere.origin;
return cliqCity::graphicsMath::dot(d, d) <= sphere.radius * sphere.radius;
}
#pragma endregion
#pragma region AABB - Primitive Tests
template<class Vector>
int IntersectAABBAABB(AABB<Vector> aabb0, AABB<Vector> aabb1)
{
int numElements = sizeof(Vector) / sizeof(float);
Vector min0 = aabb0.origin - aabb0.halfSize;
Vector max0 = aabb0.origin + aabb0.halfSize;
Vector min1 = aabb1.origin - aabb1.halfSize;
Vector max1 = aabb1.origin + aabb1.halfSize;
for (int i = 0; i < numElements; i++)
{
if (max0[i] < min1[i] || min0[i] > max1[i])
{
return 0;
}
}
return 1;
}
template <class Vector>
int IntersectAABBPlane(const AABB<Vector>& aabb, const Plane<Vector>& plane)
{
int numElements = cliqCity::graphicsMath::Components<Vector>::FloatCount();
float pir = 0.0f;
for (int i = 0; i < numElements; i++)
{
pir += aabb.halfSize.pCols[i] * abs(plane.normal.pCols[i]);
}
float distance = cliqCity::graphicsMath::dot(plane.normal, aabb.origin) - plane.distance;
return abs(distance) <= pir;
}
#pragma endregion
#pragma region OBB - Primitive Tests
template<class Vector, int Dimension>
int IntesectOBBPlane(const OBB<Vector, Dimension>& obb, const Plane<Vector>& plane)
{
// Compute projection interval radius
float pir = 0.0f;
for (int i = 0; i < Dimension; i++)
{
pir += obb.halfSize.pCols[i] * abs(cliqCity::graphicsMath::dot(plane.normal, obb.axis[i]));
}
// Compute distance from obb center to plane
float distance = cliqCity::graphicsMath::dot(plane.normal, obb.origin) - plane.distance;
// Intersection occurs when -pir <= distance <= +pir
return (abs(distance) <= pir);
}
template<class Vector, int Dimension>
int IntersectOBBOBB(const OBB<Vector, Dimension>& a, const OBB<Vector, Dimension>& b)
{
// Projected extents of a and b respectively
float ra, rb;
// Rotation matrices
Vector R[Dimension], AbsR[Dimension];
// Compute rotation matrix expressing b in a's coordinate frame
for (int i = 0; i < Dimension; i++)
{
for (int j = 0; j < Dimension; j++)
{
R[i].pCols[j] = cliqCity::graphicsMath::dot(a.axis[i], b.axis[j]);
}
}
// Compute translation vector
Vector BtoA = b.origin - a.origin;
// Transform Vector into a's coordinate frame
Vector t;
for (int i = 0; i < Dimension; i++)
{
t.pCols[i] = cliqCity::graphicsMath::dot(BtoA, a.axis[i]);
}
// Compute axis for SAT
for (int i = 0; i < Dimension; i++)
{
for (int j = 0; j < Dimension; j++)
{
AbsR[i].pCols[j] = abs(R[i].pCols[j]) + 0.001f;
}
}
// Test basis Axis A
for (int i = 0; i < Dimension; i++)
{
ra = a.halfSize.pCols[i];
rb = 0.0f;
for (int j = 0; j < Dimension; j++)
{
rb += b.halfSize.pCols[j] * AbsR[i].pCols[j];
}
if (abs(t[i]) > ra + rb)
{
return 0;
}
}
// Test basis Axis B
for (int i = 0; i < Dimension; i++)
{
rb = b.halfSize.pCols[i];
ra = 0.0f;
float tAccu = 0.0f;
for (int j = 0; j < Dimension; j++)
{
ra += b.halfSize.pCols[j] * AbsR[j].pCols[i];
tAccu += abs(t[j] * R[j].pCols[i]);
}
if (abs(tAccu) > ra + rb)
{
return 0;
}
}
// If 2D we can bail here
if (Dimension == 2)
{
return 1;
}
// A0 X B0
ra = (a.halfSize.pCols[1] * AbsR[2].pCols[0]) + (a.halfSize.pCols[2] * AbsR[1].pCols[0]);
rb = (b.halfSize.pCols[1] * AbsR[0].pCols[2]) + (b.halfSize.pCols[2] * AbsR[0].pCols[1]);
if (abs((t[2] * R[1].pCols[0]) - (t[1] * R[2].pCols[0])) > ra + rb)
{
return 0;
}
// A0 X B1
ra = (a.halfSize.pCols[1] * AbsR[2].pCols[1]) + (a.halfSize.pCols[2] * AbsR[1].pCols[1]);
rb = (b.halfSize.pCols[0] * AbsR[0].pCols[2]) + (b.halfSize.pCols[2] * AbsR[0].pCols[0]);
if (abs((t[2] * R[1].pCols[1]) - (t[1] * R[2].pCols[1])) > ra + rb)
{
return 0;
}
// A0 X B2
ra = (a.halfSize.pCols[1] * AbsR[2].pCols[2]) + (a.halfSize.pCols[2] * AbsR[1].pCols[2]);
rb = (b.halfSize.pCols[0] * AbsR[0].pCols[1]) + (b.halfSize.pCols[1] * AbsR[0].pCols[0]);
if (abs((t[2] * R[1].pCols[2]) - (t[1] * R[2].pCols[2])) > ra + rb)
{
return 0;
}
// A1 X B0
ra = (a.halfSize.pCols[0] * AbsR[2].pCols[0]) + (a.halfSize.pCols[2] * AbsR[0].pCols[0]);
rb = (b.halfSize.pCols[1] * AbsR[1].pCols[2]) + (b.halfSize.pCols[2] * AbsR[1].pCols[1]);
if (abs((t[0] * R[2].pCols[0]) - (t[2] * R[0].pCols[0])) > ra + rb)
{
return 0;
}
// A1 X B1
ra = (a.halfSize.pCols[0] * AbsR[2].pCols[1]) + (a.halfSize.pCols[2] * AbsR[0].pCols[1]);
rb = (b.halfSize.pCols[0] * AbsR[1].pCols[2]) + (b.halfSize.pCols[2] * AbsR[1].pCols[0]);
if (abs((t[0] * R[2].pCols[1]) - (t[2] * R[0].pCols[1])) > ra + rb)
{
return 0;
}
// A1 X B2
ra = (a.halfSize.pCols[0] * AbsR[2].pCols[2]) + (a.halfSize.pCols[2] * AbsR[0].pCols[2]);
rb = (b.halfSize.pCols[0] * AbsR[1].pCols[1]) + (b.halfSize.pCols[1] * AbsR[1].pCols[0]);
if (abs((t[0] * R[2].pCols[2]) - (t[2] * R[0].pCols[2])) > ra + rb)
{
return 0;
}
// A2 X B0
ra = (a.halfSize.pCols[0] * AbsR[1].pCols[0]) + (a.halfSize.pCols[1] * AbsR[0].pCols[0]);
rb = (b.halfSize.pCols[1] * AbsR[2].pCols[2]) + (b.halfSize.pCols[2] * AbsR[2].pCols[1]);
if (abs((t[1] * R[0].pCols[0]) - (t[0] * R[1].pCols[0])) > ra + rb)
{
return 0;
}
// A2 X B1
ra = (a.halfSize.pCols[0] * AbsR[1].pCols[1]) + (a.halfSize.pCols[1] * AbsR[0].pCols[1]);
rb = (b.halfSize.pCols[0] * AbsR[2].pCols[2]) + (b.halfSize.pCols[2] * AbsR[2].pCols[0]);
if (abs((t[1] * R[0].pCols[1]) - (t[0] * R[1].pCols[1])) > ra + rb)
{
return 0;
}
// A2 X B2
ra = (a.halfSize.pCols[0] * AbsR[1].pCols[2]) + (a.halfSize.pCols[1] * AbsR[0].pCols[2]);
rb = (b.halfSize.pCols[0] * AbsR[2].pCols[1]) + (b.halfSize.pCols[1] * AbsR[2].pCols[0]);
if (abs((t[1] * R[0].pCols[2]) - (t[0] * R[1].pCols[2])) > ra + rb)
{
return 0;
}
return 1;
}
template <class Vector, int Dimension>
int IntersectOBBAABB(const OBB<Vector, Dimension>& obb, const AABB<Vector>& aabb)
{
OBB<Vector, Dimension> o;
o.origin = aabb.origin;
o.halfSize = aabb.halfSize;
for (int i = 0; i < Dimension; i++)
{
o.axis[i].pCols[i] = 1.0f;
}
return IntersectOBBOBB(o, obb);
}
#pragma endregion
#pragma region Contains Tests
template<class Vector>
int ContainsPoint(Vector point, AABB<Vector> bounds)
{
int numElements = sizeof(Vector) / sizeof(float);
Vector min = bounds.origin - bounds.halfSize;
Vector max = bounds.origin + bounds.halfSize;
for (int i = 0; i < numElements; i++)
{
if (point[i] < min[i] || point[i] > max[i])
{
return 0;
}
}
return 1;
}
template<class Vector>
int ContainsAABB(AABB<Vector> inner, AABB<Vector> bounds)
{
int numElements = sizeof(Vector) / sizeof(float);
Vector minOuter = bounds.origin - bounds.halfSize;
Vector maxOuter = bounds.origin + bounds.halfSize;
Vector minInner = inner.origin - inner.halfSize;
Vector maxInner = inner.origin + inner.halfSize;
for (int i = 0; i < numElements; i++)
{
if (minInner[i] < minOuter[i] || maxInner[i] > maxOuter[i])
{
return 0;
}
}
return 1;
}
#pragma endregion
inline int IntersectPlanes(const Plane<vec3f>& p0, const Plane<vec3f>& p1, const Plane<vec3f>& p2, vec3f& poi)
{
vec3f u = cliqCity::graphicsMath::cross(p1.normal, p2.normal);
float denom = cliqCity::graphicsMath::dot(p0.normal, u);
if (abs(denom) < 0.001f)
{
return 0;
}
poi = ((p0.distance * u) + cliqCity::graphicsMath::cross(p0.normal, (p2.distance * p1.normal) - (p1.distance * p2.normal))) / denom;
return 1;
}
}
#endif