summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/bounds.cpp
blob: ccc291d4c58eb3824f0c011a2dbb3db20f1b67ea (plain) (blame)
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
/*
 * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include <bx/rng.h>
#include <bx/math.h>
#include "bounds.h"

using namespace bx;

Vec3 getCenter(const Aabb& _aabb)
{
	return mul(add(_aabb.min, _aabb.max), 0.5f);
}

Vec3 getExtents(const Aabb& _aabb)
{
	return mul(sub(_aabb.max, _aabb.min), 0.5f);
}

Vec3 getCenter(const Triangle& _triangle)
{
	return mul(add(add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f);
}

void toAabb(Aabb& _outAabb, const Vec3& _extents)
{
	_outAabb.min = neg(_extents);
	_outAabb.max =     _extents;
}

void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extents)
{
	_outAabb.min = sub(_center, _extents);
	_outAabb.max = add(_center, _extents);
}

void toAabb(Aabb& _outAabb, const Cylinder& _cylinder)
{
	// Reference(s):
	// - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
	//
	const Vec3 axis = sub(_cylinder.end, _cylinder.pos);
	const Vec3 asq  = mul(axis, axis);
	const Vec3 nsq  = mul(asq, 1.0f/dot(axis, axis) );
	const Vec3 tmp  = sub(Vec3(1.0f), nsq);

	const float inv = 1.0f/(tmp.x*tmp.y*tmp.z);

	const Vec3 extent =
	{
		_cylinder.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
		_cylinder.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
		_cylinder.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
	};

	const Vec3 minP = sub(_cylinder.pos, extent);
	const Vec3 minE = sub(_cylinder.end, extent);
	const Vec3 maxP = add(_cylinder.pos, extent);
	const Vec3 maxE = add(_cylinder.end, extent);

	_outAabb.min = min(minP, minE);
	_outAabb.max = max(maxP, maxE);
}

void toAabb(Aabb& _outAabb, const Disk& _disk)
{
	// Reference(s):
	// - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
	//
	const Vec3 nsq = mul(_disk.normal, _disk.normal);
	const Vec3 one = { 1.0f, 1.0f, 1.0f };
	const Vec3 tmp = sub(one, nsq);
	const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);

	const Vec3 extent =
	{
		_disk.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
		_disk.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
		_disk.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
	};

	_outAabb.min = sub(_disk.center, extent);
	_outAabb.max = add(_disk.center, extent);
}

void toAabb(Aabb& _outAabb, const Obb& _obb)
{
	Vec3 xyz = { 1.0f, 1.0f, 1.0f };
	Vec3 tmp = mul(xyz, _obb.mtx);

	_outAabb.min = tmp;
	_outAabb.max = tmp;

	for (uint32_t ii = 1; ii < 8; ++ii)
	{
		xyz.x = ii & 1 ? -1.0f : 1.0f;
		xyz.y = ii & 2 ? -1.0f : 1.0f;
		xyz.z = ii & 4 ? -1.0f : 1.0f;
		tmp = mul(xyz, _obb.mtx);

		_outAabb.min = min(_outAabb.min, tmp);
		_outAabb.max = max(_outAabb.max, tmp);
	}
}

void toAabb(Aabb& _outAabb, const Sphere& _sphere)
{
	const float radius = _sphere.radius;
	_outAabb.min = sub(_sphere.center, radius);
	_outAabb.max = add(_sphere.center, radius);
}

void toAabb(Aabb& _outAabb, const Triangle& _triangle)
{
	_outAabb.min = min(_triangle.v0, _triangle.v1, _triangle.v2);
	_outAabb.max = max(_triangle.v0, _triangle.v1, _triangle.v2);
}

void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx)
{
	toObb(_obb, _aabb);
	float result[16];
	mtxMul(result, _obb.mtx, _mtx);
	memCopy(_obb.mtx, result, sizeof(result) );
}

void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
{
	Vec3 mn, mx;
	uint8_t* vertex = (uint8_t*)_vertices;

	mn = mx = load<Vec3>(vertex);
	vertex += _stride;

	for (uint32_t ii = 1; ii < _numVertices; ++ii)
	{
		const Vec3 pos = load<Vec3>(vertex);
		vertex += _stride;

		mn = min(pos, mn);
		mx = max(pos, mx);
	}

	_outAabb.min = mn;
	_outAabb.max = mx;
}

void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
{
	Vec3 mn, mx;
	uint8_t* vertex = (uint8_t*)_vertices;
	mn = mx = mul(load<Vec3>(vertex), _mtx);

	vertex += _stride;

	for (uint32_t ii = 1; ii < _numVertices; ++ii)
	{
		Vec3 pos = mul(load<Vec3>(vertex), _mtx);
		vertex += _stride;

		mn = min(pos, mn);
		mx = max(pos, mx);
	}

	_outAabb.min = mn;
	_outAabb.max = mx;
}

float calcAreaAabb(const Aabb& _aabb)
{
	const float ww = _aabb.max.x - _aabb.min.x;
	const float hh = _aabb.max.y - _aabb.min.y;
	const float dd = _aabb.max.z - _aabb.min.z;
	return 2.0f * (ww*hh + ww*dd + hh*dd);
}

void aabbExpand(Aabb& _outAabb, float _factor)
{
	_outAabb.min.x -= _factor;
	_outAabb.min.y -= _factor;
	_outAabb.min.z -= _factor;
	_outAabb.max.x += _factor;
	_outAabb.max.y += _factor;
	_outAabb.max.z += _factor;
}

void aabbExpand(Aabb& _outAabb, const Vec3& _pos)
{
	_outAabb.min = min(_outAabb.min, _pos);
	_outAabb.max = max(_outAabb.max, _pos);
}

void toObb(Obb& _outObb, const Aabb& _aabb)
{
	memSet(_outObb.mtx, 0, sizeof(_outObb.mtx) );
	_outObb.mtx[ 0] = (_aabb.max.x - _aabb.min.x) * 0.5f;
	_outObb.mtx[ 5] = (_aabb.max.y - _aabb.min.y) * 0.5f;
	_outObb.mtx[10] = (_aabb.max.z - _aabb.min.z) * 0.5f;
	_outObb.mtx[12] = (_aabb.min.x + _aabb.max.x) * 0.5f;
	_outObb.mtx[13] = (_aabb.min.y + _aabb.max.y) * 0.5f;
	_outObb.mtx[14] = (_aabb.min.z + _aabb.max.z) * 0.5f;
	_outObb.mtx[15] = 1.0f;
}

void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
{
	Aabb aabb;
	toAabb(aabb, _vertices, _numVertices, _stride);
	float minArea = calcAreaAabb(aabb);

	Obb best;
	toObb(best, aabb);

	float angleStep = float(kPiHalf/_steps);
	float ax = 0.0f;
	float mtx[16];

	for (uint32_t ii = 0; ii < _steps; ++ii)
	{
		float ay = 0.0f;

		for (uint32_t jj = 0; jj < _steps; ++jj)
		{
			float az = 0.0f;

			for (uint32_t kk = 0; kk < _steps; ++kk)
			{
				mtxRotateXYZ(mtx, ax, ay, az);

				float mtxT[16];
				mtxTranspose(mtxT, mtx);
				toAabb(aabb, mtxT, _vertices, _numVertices, _stride);

				float area = calcAreaAabb(aabb);
				if (area < minArea)
				{
					minArea = area;
					aabbTransformToObb(best, aabb, mtx);
				}

				az += angleStep;
			}

			ay += angleStep;
		}

		ax += angleStep;
	}

	memCopy(&_outObb, &best, sizeof(Obb) );
}

void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
{
	Aabb aabb;
	toAabb(aabb, _vertices, _numVertices, _stride);

	Vec3 center = getCenter(aabb);

	float maxDistSq = 0.0f;
	uint8_t* vertex = (uint8_t*)_vertices;

	for (uint32_t ii = 0; ii < _numVertices; ++ii)
	{
		const Vec3& pos = load<Vec3>(vertex);
		vertex += _stride;

		const Vec3 tmp = sub(pos, center);
		const float distSq = dot(tmp, tmp);
		maxDistSq = max(distSq, maxDistSq);
	}

	_sphere.center = center;
	_sphere.radius = sqrt(maxDistSq);
}

void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
{
	RngMwc rng;

	uint8_t* vertex = (uint8_t*)_vertices;

	Vec3 center;
	float* position = (float*)&vertex[0];
	center.x = position[0];
	center.y = position[1];
	center.z = position[2];

	position = (float*)&vertex[1*_stride];
	center.x += position[0];
	center.y += position[1];
	center.z += position[2];

	center.x *= 0.5f;
	center.y *= 0.5f;
	center.z *= 0.5f;

	float xx = position[0] - center.x;
	float yy = position[1] - center.y;
	float zz = position[2] - center.z;
	float maxDistSq = xx*xx + yy*yy + zz*zz;

	float radiusStep = _step * 0.37f;

	bool done;
	do
	{
		done = true;
		for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
		{
			position = (float*)&vertex[index*_stride];

			xx = position[0] - center.x;
			yy = position[1] - center.y;
			zz = position[2] - center.z;
			float distSq = xx*xx + yy*yy + zz*zz;

			if (distSq > maxDistSq)
			{
				done = false;

				center.x += xx * radiusStep;
				center.y += yy * radiusStep;
				center.z += zz * radiusStep;
				maxDistSq = lerp(maxDistSq, distSq, _step);

				break;
			}
		}

	} while (!done);

	_sphere.center = center;
	_sphere.radius = sqrt(maxDistSq);
}

void buildFrustumPlanes(Plane* _result, const float* _viewProj)
{
	const float xw = _viewProj[ 3];
	const float yw = _viewProj[ 7];
	const float zw = _viewProj[11];
	const float ww = _viewProj[15];

	const float xz = _viewProj[ 2];
	const float yz = _viewProj[ 6];
	const float zz = _viewProj[10];
	const float wz = _viewProj[14];

	Plane& near   = _result[0];
	Plane& far    = _result[1];
	Plane& left   = _result[2];
	Plane& right  = _result[3];
	Plane& top    = _result[4];
	Plane& bottom = _result[5];

	near.normal.x = xw - xz;
	near.normal.y = yw - yz;
	near.normal.z = zw - zz;
	near.dist     = ww - wz;

	far.normal.x = xw + xz;
	far.normal.y = yw + yz;
	far.normal.z = zw + zz;
	far.dist     = ww + wz;

	const float xx = _viewProj[ 0];
	const float yx = _viewProj[ 4];
	const float zx = _viewProj[ 8];
	const float wx = _viewProj[12];

	left.normal.x = xw - xx;
	left.normal.y = yw - yx;
	left.normal.z = zw - zx;
	left.dist     = ww - wx;

	right.normal.x = xw + xx;
	right.normal.y = yw + yx;
	right.normal.z = zw + zx;
	right.dist     = ww + wx;

	const float xy = _viewProj[ 1];
	const float yy = _viewProj[ 5];
	const float zy = _viewProj[ 9];
	const float wy = _viewProj[13];

	top.normal.x = xw + xy;
	top.normal.y = yw + yy;
	top.normal.z = zw + zy;
	top.dist     = ww + wy;

	bottom.normal.x = xw - xy;
	bottom.normal.y = yw - yy;
	bottom.normal.z = zw - zy;
	bottom.dist     = ww - wy;

	Plane* plane = _result;
	for (uint32_t ii = 0; ii < 6; ++ii)
	{
		const float invLen = 1.0f/length(plane->normal);
		plane->normal = normalize(plane->normal);
		plane->dist  *= invLen;
		++plane;
	}
}

Ray makeRay(float _x, float _y, const float* _invVp)
{
	Ray ray;

	const Vec3 near = { _x, _y, 0.0f };
	ray.pos = mulH(near, _invVp);

	const Vec3 far = { _x, _y, 1.0f };
	Vec3 tmp = mulH(far, _invVp);

	const Vec3 dir = sub(tmp, ray.pos);
	ray.dir = normalize(dir);

	return ray;
}

inline Vec3 getPointAt(const Ray& _ray, float _t)
{
	return mad(_ray.dir, _t, _ray.pos);
}

bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
{
	const Vec3 invDir = rcp(_ray.dir);
	const Vec3 tmp0   = sub(_aabb.min, _ray.pos);
	const Vec3 t0     = mul(tmp0, invDir);
	const Vec3 tmp1   = sub(_aabb.max, _ray.pos);
	const Vec3 t1     = mul(tmp1, invDir);

	const Vec3 mn = min(t0, t1);
	const Vec3 mx = max(t0, t1);

	const float tmin = max(mn.x, mn.y, mn.z);
	const float tmax = min(mx.x, mx.y, mx.z);

	if (0.0f > tmax
	||  tmin > tmax)
	{
		return false;
	}

	if (NULL != _hit)
	{
		_hit->plane.normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
		_hit->plane.normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
		_hit->plane.normal.z = float( (t1.z == tmin) - (t0.z == tmin) );

		_hit->plane.dist = tmin;
		_hit->pos        = getPointAt(_ray, tmin);
	}

	return true;
}

static constexpr Aabb kUnitAabb =
{
	{ -1.0f, -1.0f, -1.0f },
	{  1.0f,  1.0f,  1.0f },
};

bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
{
	Aabb aabb;
	toAabb(aabb, _obb);

	if (!intersect(_ray, aabb) )
	{
		return false;
	}

	float mtxInv[16];
	mtxInverse(mtxInv, _obb.mtx);

	Ray obbRay;
	obbRay.pos = mul(_ray.pos, mtxInv);
	obbRay.dir = mulXyz0(_ray.dir, mtxInv);

	if (intersect(obbRay, kUnitAabb, _hit) )
	{
		if (NULL != _hit)
		{
			_hit->pos = mul(_hit->pos, _obb.mtx);

			const Vec3 tmp = mulXyz0(_hit->plane.normal, _obb.mtx);
			_hit->plane.normal = normalize(tmp);
		}

		return true;
	}

	return false;
}

bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
{
	Plane plane;
	plane.normal = _disk.normal;
	plane.dist   = -dot(_disk.center, _disk.normal);

	Hit tmpHit;
	_hit = NULL != _hit ? _hit : &tmpHit;

	if (intersect(_ray, plane, _hit) )
	{
		const Vec3 tmp = sub(_disk.center, _hit->pos);
		return dot(tmp, tmp) <= square(_disk.radius);
	}

	return false;
}

static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
{
	Vec3 axis = sub(_cylinder.end, _cylinder.pos);
	const Vec3 rc   = sub(_ray.pos, _cylinder.pos);
	const Vec3 dxa  = cross(_ray.dir, axis);

	const float len    = length(dxa);
	const Vec3  normal = normalize(dxa);
	const float dist   = bx::abs(dot(rc, normal) );

	if (dist > _cylinder.radius)
	{
		return false;
	}

	Vec3 vo = cross(rc, axis);
	const float t0 = -dot(vo, normal) / len;

	vo = normalize(cross(normal, axis) );

	const float rsq   = square(_cylinder.radius);
	const float ddoto = dot(_ray.dir, vo);
	const float ss    = t0 - bx::abs(sqrt(rsq - square(dist) ) / ddoto);

	if (0.0f > ss)
	{
		return false;
	}

	const Vec3 point = getPointAt(_ray, ss);

	const float axisLen = length(axis);
	axis = normalize(axis);
	const float pdota  = dot(_cylinder.pos, axis);
	const float height = dot(point, axis) - pdota;

	if (0.0f    < height
	&&  axisLen > height)
	{
		if (NULL != _hit)
		{
			const float t1 = height / axisLen;
			const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);

			_hit->pos = point;

			const Vec3 tmp = sub(point, pointOnAxis);
			_hit->plane.normal = normalize(tmp);

			_hit->plane.dist = ss;
		}

		return true;
	}

	if (_capsule)
	{
		const float rdota = dot(_ray.pos, axis);
		const float pp    = rdota - pdota;
		const float t1    = pp / axisLen;

		const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
		const Vec3 axisToRay   = sub(_ray.pos, pointOnAxis);

		if (_cylinder.radius < length(axisToRay)
		&&  0.0f > ss)
		{
			return false;
		}

		Sphere sphere;
		sphere.radius = _cylinder.radius;

		sphere.center = 0.0f >= height
			? _cylinder.pos
			: _cylinder.end
			;

		return intersect(_ray, sphere, _hit);
	}

	Plane plane;
	Vec3 pos;

	if (0.0f >= height)
	{
		plane.normal = neg(axis);
		pos = _cylinder.pos;
	}
	else
	{
		plane.normal = axis;
		pos = _cylinder.end;
	}

	plane.dist = -dot(pos, plane.normal);

	Hit tmpHit;
	_hit = NULL != _hit ? _hit : &tmpHit;

	if (intersect(_ray, plane, _hit) )
	{
		const Vec3 tmp = sub(pos, _hit->pos);
		return dot(tmp, tmp) <= rsq;
	}

	return false;
}

bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
{
	return intersect(_ray, _cylinder, false, _hit);
}

bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
{
	BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
	return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
}

bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
{
	const Vec3 axis = sub(_cone.pos, _cone.end);

	const float len = length(axis);
	const Vec3 normal = normalize(axis);

	Disk disk;
	disk.center = _cone.pos;
	disk.normal = normal;
	disk.radius = _cone.radius;

	Hit tmpInt;
	Hit* out = NULL != _hit ? _hit : &tmpInt;
	bool hit = intersect(_ray, disk, out);

	const Vec3 ro = sub(_ray.pos, _cone.end);

	const float hyp    = sqrt(square(_cone.radius) + square(len) );
	const float cosaSq = square(len/hyp);
	const float ndoto  = dot(normal, ro);
	const float ndotd  = dot(normal, _ray.dir);

	const float aa = square(ndotd) - cosaSq;
	const float bb = 2.0f * (ndotd*ndoto - dot(_ray.dir, ro)*cosaSq);
	const float cc = square(ndoto) - dot(ro, ro)*cosaSq;

	float det = bb*bb - 4.0f*aa*cc;

	if (0.0f > det)
	{
		return hit;
	}

	det = sqrt(det);
	const float invA2 = 1.0f / (2.0f*aa);
	const float t1 = (-bb - det) * invA2;
	const float t2 = (-bb + det) * invA2;

	float tt = t1;
	if (0.0f > t1
	|| (0.0f < t2 && t2 < t1) )
	{
		tt = t2;
	}

	if (0.0f > tt)
	{
		return hit;
	}

	const Vec3 hitPos = getPointAt(_ray, tt);
	const Vec3 point  = sub(hitPos, _cone.end);

	const float hh = dot(normal, point);

	if (0.0f > hh
	||  len  < hh)
	{
		return hit;
	}

	if (NULL != _hit)
	{
		if (!hit
		||  tt < _hit->plane.dist)
		{
			_hit->plane.dist = tt;
			_hit->pos  = hitPos;

			const float scale = hh / dot(point, point);
			const Vec3 pointScaled = mul(point, scale);

			const Vec3 tmp = sub(pointScaled, normal);
			_hit->plane.normal = normalize(tmp);
		}
	}

	return true;
}

bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
{
	const float dist = distance(_plane, _ray.pos);
	if (0.0f > dist)
	{
		return false;
	}

	const float ndotd = dot(_ray.dir, _plane.normal);
	if (0.0f < ndotd)
	{
		return false;
	}

	if (NULL != _hit)
	{
		_hit->plane.normal = _plane.normal;

		float tt = -dist/ndotd;
		_hit->plane.dist = tt;
		_hit->pos = getPointAt(_ray, tt);
	}

	return true;
}

bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
{
	const Vec3 rs = sub(_ray.pos, _sphere.center);

	const float bb = dot(rs, _ray.dir);
	if (0.0f < bb)
	{
		return false;
	}

	const float aa = dot(_ray.dir, _ray.dir);
	const float cc = dot(rs, rs) - square(_sphere.radius);

	const float discriminant = bb*bb - aa*cc;

	if (0.0f >= discriminant)
	{
		return false;
	}

	const float sqrtDiscriminant = sqrt(discriminant);
	const float invA = 1.0f / aa;
	const float tt = -(bb + sqrtDiscriminant)*invA;

	if (0.0f >= tt)
	{
		return false;
	}

	if (NULL != _hit)
	{
		_hit->plane.dist = tt;

		const Vec3 point = getPointAt(_ray, tt);
		_hit->pos = point;

		const Vec3 tmp = sub(point, _sphere.center);
		_hit->plane.normal = normalize(tmp);
	}

	return true;
}

bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
{
	const Vec3 edge10 = sub(_triangle.v1, _triangle.v0);
	const Vec3 edge02 = sub(_triangle.v0, _triangle.v2);
	const Vec3 normal = cross(edge02, edge10);
	const Vec3 vo     = sub(_triangle.v0, _ray.pos);
	const Vec3 dxo    = cross(_ray.dir, vo);
	const float det = dot(normal, _ray.dir);

	if (0.0f < det)
	{
		return false;
	}

	const float invDet = 1.0f/det;
	const float bz = dot(dxo, edge02) * invDet;
	const float by = dot(dxo, edge10) * invDet;
	const float bx = 1.0f - by - bz;

	if (0.0f > bx
	||  0.0f > by
	||  0.0f > bz)
	{
		return false;
	}

	if (NULL != _hit)
	{
		_hit->plane.normal = normalize(normal);

		const float tt = dot(normal, vo) * invDet;
		_hit->plane.dist = tt;
		_hit->pos  = getPointAt(_ray, tt);
	}

	return true;
}

Vec3 barycentric(const Triangle& _triangle, const Vec3& _pos)
{
	const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
	const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
	const Vec3 v2 = sub(_pos, _triangle.v0);

	const float dot00 = dot(v0, v0);
	const float dot01 = dot(v0, v1);
	const float dot02 = dot(v0, v2);
	const float dot11 = dot(v1, v1);
	const float dot12 = dot(v1, v2);

	const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );

	const float vv = (dot11*dot02 - dot01*dot12)*invDenom;
	const float ww = (dot00*dot12 - dot01*dot02)*invDenom;
	const float uu = 1.0f - vv - ww;

	return { uu, vv, ww };
}

Vec3 cartesian(const Triangle& _triangle, const Vec3& _uvw)
{
	const Vec3 b0 = mul(_triangle.v0, _uvw.x);
	const Vec3 b1 = mul(_triangle.v1, _uvw.y);
	const Vec3 b2 = mul(_triangle.v2, _uvw.z);

	return add(add(b0, b1), b2);
}

void calcPlane(Plane& _outPlane, const Disk& _disk)
{
	calcPlane(_outPlane, _disk.normal, _disk.center);
}

void calcPlane(Plane& _outPlane, const Triangle& _triangle)
{
	calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
}

struct Interval
{
	float start;
	float end;
};

bool overlap(const Interval& _a, const Interval& _b)
{
	return _a.end > _b.start
		&& _b.end > _a.start
		;
}

float projectToAxis(const Vec3& _axis, const Vec3& _point)
{
	return dot(_axis, _point);
}

Interval projectToAxis(const Vec3& _axis, const Aabb& _aabb)
{
	const float extent = bx::abs(dot(abs(_axis), getExtents(_aabb) ) );
	const float center =         dot(    _axis , getCenter (_aabb) );
	return
	{
		center - extent,
		center + extent,
	};
}

Interval projectToAxis(const Vec3& _axis, const Triangle& _triangle)
{
	const float a0 = dot(_axis, _triangle.v0);
	const float a1 = dot(_axis, _triangle.v1);
	const float a2 = dot(_axis, _triangle.v2);
	return
	{
		min(a0, a1, a2),
		max(a0, a1, a2),
	};
}

struct Srt
{
	Quaternion rotation;
	Vec3       translation;
	Vec3       scale;
};

Srt toSrt(const void* _mtx)
{
	Srt result;

	const float* mtx = (const float*)_mtx;

	result.translation = { mtx[12], mtx[13], mtx[14] };

	float xx = mtx[ 0];
	float xy = mtx[ 1];
	float xz = mtx[ 2];
	float yx = mtx[ 4];
	float yy = mtx[ 5];
	float yz = mtx[ 6];
	float zx = mtx[ 8];
	float zy = mtx[ 9];
	float zz = mtx[10];

	result.scale =
	{
		sqrt(xx*xx + xy*xy + xz*xz),
		sqrt(yx*yx + yy*yy + yz*yz),
		sqrt(zx*zx + zy*zy + zz*zz),
	};

	const Vec3 invScale = rcp(result.scale);

	xx *= invScale.x;
	xy *= invScale.x;
	xz *= invScale.x;
	yx *= invScale.y;
	yy *= invScale.y;
	yz *= invScale.y;
	zx *= invScale.z;
	zy *= invScale.z;
	zz *= invScale.z;

	const float trace = xx + yy + zz;

	if (0.0f < trace)
	{
		const float invS = 0.5f * rsqrt(trace + 1.0f);
		result.rotation =
		{
			(yz - zy) * invS,
			(zx - xz) * invS,
			(xy - yx) * invS,
			0.25f     / invS,
		};
	}
	else
	{
		if (xx > yy
		&&  xx > zz)
		{
			const float invS = 0.5f * sqrt(max(1.0f + xx - yy - zz, 1e-8f) );
			result.rotation =
			{
				0.25f     / invS,
				(xy + yx) * invS,
				(xz + zx) * invS,
				(yz - zy) * invS,
			};
		}
		else if (yy > zz)
		{
			const float invS = 0.5f * sqrt(max(1.0f + yy - xx - zz, 1e-8f) );
			result.rotation =
			{
				(xy + yx) * invS,
				0.25f     / invS,
				(yz + zy) * invS,
				(zx - xz) * invS,
			};
		}
		else
		{
			const float invS = 0.5f * sqrt(max(1.0f + zz - xx - yy, 1e-8f) );
			result.rotation =
			{
				(xz + zx) * invS,
				(yz + zy) * invS,
				0.25f     / invS,
				(xy - yx) * invS,
			};
		}
	}

	return result;
}

void mtxFromSrt(float* _outMtx, const Srt& _srt)
{
	mtxQuat(_outMtx, _srt.rotation);

	store<Vec3>(&_outMtx[0], mul(load<Vec3>(&_outMtx[0]), _srt.scale.x) );
	store<Vec3>(&_outMtx[4], mul(load<Vec3>(&_outMtx[4]), _srt.scale.y) );
	store<Vec3>(&_outMtx[8], mul(load<Vec3>(&_outMtx[8]), _srt.scale.z) );

	store<Vec3>(&_outMtx[12], _srt.translation);
}

bool isNearZero(float _v)
{
	return equal(_v, 0.0f, 0.00001f);
}

bool isNearZero(const Vec3& _v)
{
	return isNearZero(dot(_v, _v) );
}

struct Line
{
	Vec3 pos;
	Vec3 dir;
};

inline Vec3 getPointAt(const Line& _line, float _t)
{
	return mad(_line.dir, _t, _line.pos);
}

bool intersect(Line& _outLine, const Plane& _planeA, const Plane& _planeB)
{
	const Vec3  axb   = cross(_planeA.normal, _planeB.normal);
	const float denom = dot(axb, axb);

	if (isNearZero(denom) )
	{
		return false;
	}

	const Vec3 bxaxb = cross(_planeB.normal, axb);
	const Vec3 axbxa = cross(axb, _planeA.normal);
	const Vec3 tmp0  = mul(bxaxb, _planeA.dist);
	const Vec3 tmp1  = mul(axbxa, _planeB.dist);
	const Vec3 tmp2  = add(tmp0, tmp1);

	_outLine.pos = mul(tmp2, -1.0f/denom);
	_outLine.dir = normalize(axb);

	return true;
}

Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc)
{
	const Vec3 axb  = cross(_pa.normal, _pb.normal);
	const Vec3 bxc  = cross(_pb.normal, _pc.normal);
	const Vec3 cxa  = cross(_pc.normal, _pa.normal);
	const Vec3 tmp0 = mul(bxc, _pa.dist);
	const Vec3 tmp1 = mul(cxa, _pb.dist);
	const Vec3 tmp2 = mul(axb, _pc.dist);
	const Vec3 tmp3 = add(tmp0, tmp1);
	const Vec3 tmp4 = add(tmp3, tmp2);

	const float denom = dot(_pa.normal, bxc);
	const Vec3 result = mul(tmp4, -1.0f/denom);

	return result;
}

struct LineSegment
{
	Vec3 pos;
	Vec3 end;
};

inline Vec3 getPointAt(const LineSegment& _line, float _t)
{
	return lerp(_line.pos, _line.end, _t);
}

bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment& _b)
{
	// Reference(s):
	//
	// - The shortest line between two lines in 3D
	//   https://web.archive.org/web/20120309093234/http://paulbourke.net/geometry/lineline3d/

	const Vec3 bd = sub(_b.end, _b.pos);
	if (isNearZero(bd) )
	{
		return false;
	}

	const Vec3 ad = sub(_a.end, _a.pos);
	if (isNearZero(ad) )
	{
		return false;
	}

	const Vec3  ab = sub(_a.pos, _b.pos);

	const float d0 = projectToAxis(ab, bd);
	const float d1 = projectToAxis(ad, bd);
	const float d2 = projectToAxis(ab, ad);
	const float d3 = projectToAxis(bd, bd);
	const float d4 = projectToAxis(ad, ad);

	const float denom = d4*d3 - square(d1);

	float ta = 0.0f;

	if (!isNearZero(denom) )
	{
		ta = (d0*d1 - d2*d3)/denom;
	}

	_outTa = ta;
	_outTb = (d0+d1*ta)/d3;

	return true;
}

bool intersect(const LineSegment& _a, const LineSegment& _b)
{
	float ta, tb;
	if (!intersect(ta, tb, _a, _b) )
	{
		return false;
	}

	return 0.0f >= ta
		&& 1.0f <= ta
		&& 0.0f >= tb
		&& 1.0f <= tb
		;
}

bool intersect(const LineSegment& _line, const Plane& _plane, Hit* _hit)
{
	const float dist  = distance(_plane, _line.pos);
	const float flip  = sign(dist);
	const Vec3  dir   = normalize(sub(_line.end, _line.pos) );
	const float ndotd = dot(dir, _plane.normal);
	const float tt    = -dist/ndotd;
	const float len   = length(sub(_line.end, _line.pos) );

	if (tt < 0.0f || tt > len)
	{
		return false;
	}

	if (NULL != _hit)
	{
		_hit->pos = mad(dir, tt, _line.pos);

		_hit->plane.normal =  mul(_plane.normal, flip);
		_hit->plane.dist   = -dot(_hit->plane.normal, _hit->pos);
	}

	return true;
}

float distance(const Plane& _plane, const LineSegment& _line)
{
	const float pd = distance(_plane, _line.pos);
	const float ed = distance(_plane, _line.end);
	return min(max(pd*ed, 0.0f), bx::abs(pd), bx::abs(ed) );
}

Vec3 closestPoint(const Line& _line, const Vec3& _point)
{
	const float tt = projectToAxis(_line.dir, sub(_point, _line.pos) );
	return getPointAt(_line, tt);
}

Vec3 closestPoint(const LineSegment& _line, const Vec3& _point, float& _outT)
{
	const Vec3  axis     = sub(_line.end, _line.pos);
	const float lengthSq = dot(axis, axis);
	const float tt       = clamp(projectToAxis(axis, sub(_point, _line.pos) ) / lengthSq, 0.0f, 1.0f);
	_outT = tt;
	return mad(axis, tt, _line.pos);
}

Vec3 closestPoint(const LineSegment& _line, const Vec3& _point)
{
	float ignored;
	return closestPoint(_line, _point, ignored);
}

Vec3 closestPoint(const Plane& _plane, const Vec3& _point)
{
	const float dist = distance(_plane, _point);
	return sub(_point, mul(_plane.normal, dist) );
}

Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point)
{
	return clamp(_point, _aabb.min, _aabb.max);
}

Vec3 closestPoint(const Obb& _obb, const Vec3& _point)
{
	Srt srt = toSrt(_obb.mtx);

	Aabb aabb;
	toAabb(aabb, srt.scale);

	const Quaternion invRotation = invert(srt.rotation);
	const Vec3 obbSpacePos = mul(sub(_point, srt.translation), srt.rotation);
	const Vec3 pos = closestPoint(aabb, obbSpacePos);

	return add(mul(pos, invRotation), srt.translation);
}

Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point)
{
	Plane plane;
	calcPlane(plane, _triangle);

	const Vec3 pos = closestPoint(plane, _point);
	const Vec3 uvw = barycentric(_triangle, pos);

	return cartesian(_triangle, clamp<Vec3>(uvw, Vec3(0.0f), Vec3(1.0f) ) );
}

bool overlap(const Aabb& _aabb, const Vec3& _pos)
{
	const Vec3 ac  = getCenter(_aabb);
	const Vec3 ae  = getExtents(_aabb);
	const Vec3 abc = bx::abs(sub(ac, _pos) );

	return abc.x <= ae.x
		&& abc.y <= ae.y
		&& abc.z <= ae.z
		;
}

bool overlap(const Aabb& _aabb, const Sphere& _sphere)
{
	return overlap(_sphere, _aabb);
}

bool overlap(const Aabb& _aabbA, const Aabb& _aabbB)
{
	return true
		&& _aabbA.max.x > _aabbB.min.x
		&& _aabbB.max.x > _aabbA.min.x
		&& _aabbA.max.y > _aabbB.min.y
		&& _aabbB.max.y > _aabbA.min.y
		&& _aabbA.max.z > _aabbB.min.z
		&& _aabbB.max.z > _aabbA.min.z
		;
}

bool overlap(const Aabb& _aabb, const Plane& _plane)
{
	const Vec3 center = getCenter(_aabb);
	const float dist  = distance(_plane, center);

	const Vec3 extents = getExtents(_aabb);
	const Vec3 normal  = bx::abs(_plane.normal);
	const float radius = dot(extents, normal);

	return bx::abs(dist) <= radius;
}

static constexpr Vec3 kAxis[] =
{
	{ 1.0f, 0.0f, 0.0f },
	{ 0.0f, 1.0f, 0.0f },
	{ 0.0f, 0.0f, 1.0f },
};

bool overlap(const Aabb& _aabb, const Triangle& _triangle)
{
	Aabb triAabb;
	toAabb(triAabb, _triangle);

	if (!overlap(_aabb, triAabb) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _triangle);

	if (!overlap(_aabb, plane) )
	{
		return false;
	}

	const Vec3 center = getCenter(_aabb);
	const Vec3 v0 = sub(_triangle.v0, center);
	const Vec3 v1 = sub(_triangle.v1, center);
	const Vec3 v2 = sub(_triangle.v2, center);

	const Vec3 edge[] =
	{
		sub(v1, v0),
		sub(v2, v1),
		sub(v0, v2),
	};

	for (uint32_t ii = 0; ii < 3; ++ii)
	{
		for (uint32_t jj = 0; jj < 3; ++jj)
		{
			const Vec3 axis = cross(kAxis[ii], edge[jj]);

			const Interval aabbR = projectToAxis(axis, _aabb);
			const Interval triR  = projectToAxis(axis, _triangle);

			if (!overlap(aabbR, triR) )
			{
				return false;
			}
		}
	}

	return true;
}

bool overlap(const Aabb& _aabb, const Cylinder& _cylinder)
{
	return overlap(_cylinder, _aabb);
}

bool overlap(const Aabb& _aabb, const Capsule& _capsule)
{
	const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, getCenter(_aabb) );
	return overlap(_aabb, Sphere{pos, _capsule.radius});
}

bool overlap(const Aabb& _aabb, const Cone& _cone)
{
	float tt;
	const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, getCenter(_aabb), tt);
	return overlap(_aabb, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
}

bool overlap(const Aabb& _aabb, const Disk& _disk)
{
	if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	return overlap(_aabb, plane);
}

bool overlap(const Aabb& _aabb, const Obb& _obb)
{
	BX_UNUSED(_aabb, _obb);
	return false;
}

bool overlap(const Capsule& _capsule, const Vec3& _pos)
{
	const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _pos);
	return overlap(Sphere{pos, _capsule.radius}, _pos);
}

bool overlap(const Capsule& _capsule, const Sphere& _sphere)
{
	return overlap(_sphere, _capsule);
}

bool overlap(const Capsule& _capsule, const Aabb& _aabb)
{
	return overlap(_aabb, _capsule);
}

bool overlap(const Capsule& _capsule, const Plane& _plane)
{
	return distance(_plane, LineSegment{_capsule.pos, _capsule.end}) <= _capsule.radius;
}

bool overlap(const Capsule& _capsule, const Triangle& _triangle)
{
	return overlap(_triangle, _capsule);
}

bool overlap(const Capsule& _capsule, const Cylinder& _cylinder)
{
	return overlap(_cylinder, _capsule);
}

bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB)
{
	float ta, tb;
	if (!intersect(ta, tb, {_capsuleA.pos, _capsuleA.end}, {_capsuleB.pos, _capsuleB.end}) )
	{
		return false;
	}

	if (0.0f <= ta
	&&  1.0f >= ta
	&&  0.0f <= tb
	&&  1.0f >= tb)
	{
		const Vec3 ad = sub(_capsuleA.end, _capsuleA.pos);
		const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);

		return overlap(
			  Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
			, Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
			);
	}

	if (0.0f <= ta
	&&  1.0f >= ta)
	{
		return overlap(_capsuleA, Sphere{0.0f >= tb ? _capsuleB.pos : _capsuleB.end, _capsuleB.radius});
	}

	if (0.0f <= tb
	&&  1.0f >= tb)
	{
		return overlap(_capsuleB, Sphere{0.0f >= ta ? _capsuleA.pos : _capsuleA.end, _capsuleA.radius});
	}

	const Vec3 pa = 0.0f > ta ? _capsuleA.pos : _capsuleA.end;
	const Vec3 pb = 0.0f > tb ? _capsuleB.pos : _capsuleB.end;
	const Vec3 closestA = closestPoint(LineSegment{_capsuleA.pos, _capsuleA.end}, pb);
	const Vec3 closestB = closestPoint(LineSegment{_capsuleB.pos, _capsuleB.end}, pa);

	if (dot(closestA, pb) <= dot(closestB, pa) )
	{
		return overlap(_capsuleA, Sphere{closestB, _capsuleB.radius});
	}

	return overlap(_capsuleB, Sphere{closestA, _capsuleA.radius});
}

bool overlap(const Capsule& _capsule, const Cone& _cone)
{
	BX_UNUSED(_capsule, _cone);
	return false;
}

bool overlap(const Capsule& _capsule, const Disk& _disk)
{
	return overlap(_disk, _capsule);
}

bool overlap(const Capsule& _capsule, const Obb& _obb)
{
	return overlap(_obb, _capsule);
}

bool overlap(const Cone& _cone, const Vec3& _pos)
{
	float tt;
	const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _pos, tt);
	return overlap(Disk{pos, normalize(sub(_cone.end, _cone.pos) ), lerp(_cone.radius, 0.0f, tt)}, _pos);
}

bool overlap(const Cone& _cone, const Sphere& _sphere)
{
	return overlap(_sphere, _cone);
}

bool overlap(const Cone& _cone, const Aabb& _aabb)
{
	return overlap(_aabb, _cone);
}

bool overlap(const Cone& _cone, const Plane& _plane)
{
	BX_UNUSED(_cone, _plane);
	return false;
}

bool overlap(const Cone& _cone, const Triangle& _triangle)
{
	return overlap(_triangle, _cone);
}

bool overlap(const Cone& _cone, const Cylinder& _cylinder)
{
	BX_UNUSED(_cone, _cylinder);
	return false;
}

bool overlap(const Cone& _cone, const Capsule& _capsule)
{
	BX_UNUSED(_cone, _capsule);
	return false;
}

bool overlap(const Cone& _coneA, const Cone& _coneB)
{
	BX_UNUSED(_coneA, _coneB);
	return false;
}

bool overlap(const Cone& _cone, const Disk& _disk)
{
	BX_UNUSED(_cone, _disk);
	return false;
}

bool overlap(const Cone& _cone, const Obb& _obb)
{
	BX_UNUSED(_cone, _obb);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Vec3& _pos)
{
	const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _pos);
	return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _pos);
}

bool overlap(const Cylinder& _cylinder, const Sphere& _sphere)
{
	const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _sphere.center);
	return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _sphere);
}

bool overlap(const Cylinder& _cylinder, const Aabb& _aabb)
{
	const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, getCenter(_aabb) );
	return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _aabb);
}

bool overlap(const Cylinder& _cylinder, const Plane& _plane)
{
	BX_UNUSED(_cylinder, _plane);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Triangle& _triangle)
{
	return overlap(_triangle, _cylinder);
}

bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB)
{
	BX_UNUSED(_cylinderA, _cylinderB);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Capsule& _capsule)
{
	BX_UNUSED(_cylinder, _capsule);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Cone& _cone)
{
	BX_UNUSED(_cylinder, _cone);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Disk& _disk)
{
	BX_UNUSED(_cylinder, _disk);
	return false;
}

bool overlap(const Cylinder& _cylinder, const Obb& _obb)
{
	BX_UNUSED(_cylinder, _obb);
	return false;
}

bool overlap(const Disk& _disk, const Vec3& _pos)
{
	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	if (!isNearZero(distance(plane, _pos) ) )
	{
		return false;
	}

	return distanceSq(_disk.center, _pos) <= square(_disk.radius);
}

bool overlap(const Disk& _disk, const Sphere& _sphere)
{
	return overlap(_sphere, _disk);
}

bool overlap(const Disk& _disk, const Aabb& _aabb)
{
	return overlap(_aabb, _disk);
}

bool overlap(const Disk& _disk, const Plane& _plane)
{
	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	if (!overlap(plane, _plane) )
	{
		return false;
	}

	return overlap(_plane, Sphere{_disk.center, _disk.radius});
}

bool overlap(const Disk& _disk, const Triangle& _triangle)
{
	return overlap(_triangle, _disk);
}

bool overlap(const Disk& _disk, const Cylinder& _cylinder)
{
	return overlap(_cylinder, _disk);
}

bool overlap(const Disk& _disk, const Capsule& _capsule)
{
	if (!overlap(_capsule, Sphere{_disk.center, _disk.radius}) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	return overlap(_capsule, plane);
}

bool overlap(const Disk& _disk, const Cone& _cone)
{
	BX_UNUSED(_disk, _cone);
	return false;
}

bool overlap(const Disk& _diskA, const Disk& _diskB)
{
	Plane planeA;
	calcPlane(planeA, _diskA.normal, _diskA.center);

	Plane planeB;
	calcPlane(planeB, _diskB);

	Line line;

	if (!intersect(line, planeA, planeB) )
	{
		return false;
	}

	const Vec3 pa = closestPoint(line, _diskA.center);
	const Vec3 pb = closestPoint(line, _diskB.center);

	const float lenA = distance(pa, _diskA.center);
	const float lenB = distance(pb, _diskB.center);

	return sqrt(square(_diskA.radius) - square(lenA) )
		+  sqrt(square(_diskB.radius) - square(lenB) )
		>= distance(pa, pb)
		;
}

bool overlap(const Disk& _disk, const Obb& _obb)
{
	if (!overlap(_obb, Sphere{_disk.center, _disk.radius}) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	return overlap(_obb, plane);
}

bool overlap(const Obb& _obb, const Vec3& _pos)
{
	Srt srt = toSrt(_obb.mtx);

	Aabb aabb;
	toAabb(aabb, srt.scale);

	const Quaternion invRotation = invert(srt.rotation);
	const Vec3 pos = mul(sub(_pos, srt.translation), invRotation);

	return overlap(aabb, pos);
}

bool overlap(const Obb& _obb, const Sphere& _sphere)
{
	return overlap(_sphere, _obb);
}

bool overlap(const Obb& _obb, const Aabb& _aabb)
{
	return overlap(_aabb, _obb);
}

bool overlap(const Obb& _obb, const Plane& _plane)
{
	Srt srt = toSrt(_obb.mtx);

	const Quaternion invRotation = invert(srt.rotation);
	const Vec3 axis =
	{
		projectToAxis(_plane.normal, mul(Vec3{1.0f, 0.0f, 0.0f}, invRotation) ),
		projectToAxis(_plane.normal, mul(Vec3{0.0f, 1.0f, 0.0f}, invRotation) ),
		projectToAxis(_plane.normal, mul(Vec3{0.0f, 0.0f, 1.0f}, invRotation) ),
	};

	const float dist   = bx::abs(distance(_plane, srt.translation) );
	const float radius = dot(srt.scale, bx::abs(axis) );

	return dist <= radius;
}

bool overlap(const Obb& _obb, const Triangle& _triangle)
{
	return overlap(_triangle, _obb);
}

bool overlap(const Obb& _obb, const Cylinder& _cylinder)
{
	BX_UNUSED(_obb, _cylinder);
	return false;
}

bool overlap(const Obb& _obb, const Capsule& _capsule)
{
	Srt srt = toSrt(_obb.mtx);

	Aabb aabb;
	toAabb(aabb, srt.scale);

	const Quaternion invRotation = invert(srt.rotation);

	const Capsule capsule =
	{
		mul(sub(_capsule.pos, srt.translation), invRotation),
		mul(sub(_capsule.end, srt.translation), invRotation),
		_capsule.radius,
	};

	return overlap(aabb, capsule);
}

bool overlap(const Obb& _obb, const Cone& _cone)
{
	BX_UNUSED(_obb, _cone);
	return false;
}

bool overlap(const Obb& _obb, const Disk& _disk)
{
	return overlap(_disk, _obb);
}

bool overlap(const Obb& _obbA, const Obb& _obbB)
{
	BX_UNUSED(_obbA, _obbB);
	return false;
}

bool overlap(const Plane& _plane, const Vec3& _pos)
{
	return isNearZero(distance(_plane, _pos) );
}

bool overlap(const Plane& _plane, const Sphere& _sphere)
{
	return overlap(_sphere, _plane);
}

bool overlap(const Plane& _plane, const Aabb& _aabb)
{
	return overlap(_aabb, _plane);
}

bool overlap(const Plane& _planeA, const Plane& _planeB)
{
	const Vec3  dir = cross(_planeA.normal, _planeB.normal);
	const float len = length(dir);

	return !isNearZero(len);
}

bool overlap(const Plane& _plane, const Triangle& _triangle)
{
	return overlap(_triangle, _plane);
}

bool overlap(const Plane& _plane, const Cylinder& _cylinder)
{
	return overlap(_cylinder, _plane);
}

bool overlap(const Plane& _plane, const Capsule& _capsule)
{
	return overlap(_capsule, _plane);
}

bool overlap(const Plane& _plane, const Cone& _cone)
{
	BX_UNUSED(_plane, _cone);
	return false;
}

bool overlap(const Plane& _plane, const Disk& _disk)
{
	return overlap(_disk, _plane);
}

bool overlap(const Plane& _plane, const Obb& _obb)
{
	return overlap(_obb, _plane);
}

bool overlap(const Sphere& _sphere, const Vec3& _pos)
{
	const float distSq   = distanceSq(_sphere.center, _pos);
	const float radiusSq = square(_sphere.radius);
	return distSq <= radiusSq;
}

bool overlap(const Sphere& _sphereA, const Sphere& _sphereB)
{
	const float distSq   = distanceSq(_sphereA.center, _sphereB.center);
	const float radiusSq = square(_sphereA.radius + _sphereB.radius);
	return distSq <= radiusSq;
}

bool overlap(const Sphere& _sphere, const Aabb& _aabb)
{
	const Vec3 pos = closestPoint(_aabb, _sphere.center);
	return overlap(_sphere, pos);
}

bool overlap(const Sphere& _sphere, const Plane& _plane)
{
	return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
}

bool overlap(const Sphere& _sphere, const Triangle& _triangle)
{
	Plane plane;
	calcPlane(plane, _triangle);

	if (!overlap(_sphere, plane) )
	{
		return false;
	}

	const Vec3 pos = closestPoint(plane, _sphere.center);
	const Vec3 uvw = barycentric(_triangle, pos);
	const float nr = -_sphere.radius;

	return uvw.x >= nr
		&& uvw.y >= nr
		&& uvw.z >= nr
		;
}

bool overlap(const Sphere& _sphere, const Cylinder& _cylinder)
{
	return overlap(_cylinder, _sphere);
}

bool overlap(const Sphere& _sphere, const Capsule& _capsule)
{
	const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center);
	return overlap(_sphere, Sphere{pos, _capsule.radius});
}

bool overlap(const Sphere& _sphere, const Cone& _cone)
{
	float tt;
	const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt);
	return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
}

bool overlap(const Sphere& _sphere, const Disk& _disk)
{
	if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	return overlap(_sphere, plane);
}

bool overlap(const Sphere& _sphere, const Obb& _obb)
{
	const Vec3 pos = closestPoint(_obb, _sphere.center);
	return overlap(_sphere, pos);
}

bool overlap(const Triangle& _triangle, const Vec3& _pos)
{
	const Vec3 uvw = barycentric(_triangle, _pos);

	return uvw.x >= 0.0f
		&& uvw.y >= 0.0f
		&& uvw.z >= 0.0f
		;
}

bool overlap(const Triangle& _triangle, const Sphere& _sphere)
{
	return overlap(_sphere, _triangle);
}

bool overlap(const Triangle& _triangle, const Aabb& _aabb)
{
	return overlap(_aabb, _triangle);
}

bool overlap(const Triangle& _triangle, const Plane& _plane)
{
	const float dist0 = distance(_plane, _triangle.v0);
	const float dist1 = distance(_plane, _triangle.v1);
	const float dist2 = distance(_plane, _triangle.v2);

	const float minDist = min(dist0, dist1, dist2);
	const float maxDist = max(dist0, dist1, dist2);

	return 0.0f > minDist
		&& 0.0f < maxDist
		;
}

inline bool overlap(const Triangle& _triangleA, const Triangle& _triangleB, const Vec3& _axis)
{
	const Interval ia = projectToAxis(_axis, _triangleA);
	const Interval ib = projectToAxis(_axis, _triangleB);
	return overlap(ia, ib);
}

bool overlap(const Triangle& _triangleA, const Triangle& _triangleB)
{
	const Vec3 baA = sub(_triangleA.v1, _triangleA.v0);
	const Vec3 cbA = sub(_triangleA.v2, _triangleA.v1);
	const Vec3 acA = sub(_triangleA.v0, _triangleA.v2);

	const Vec3 baB = sub(_triangleB.v1, _triangleB.v0);
	const Vec3 cbB = sub(_triangleB.v2, _triangleB.v1);
	const Vec3 acB = sub(_triangleB.v0, _triangleB.v2);

	return overlap(_triangleA, _triangleB, cross(baA, cbA) )
		&& overlap(_triangleA, _triangleB, cross(baB, cbB) )
		&& overlap(_triangleA, _triangleB, cross(baB, baA) )
		&& overlap(_triangleA, _triangleB, cross(baB, cbA) )
		&& overlap(_triangleA, _triangleB, cross(baB, acA) )
		&& overlap(_triangleA, _triangleB, cross(cbB, baA) )
		&& overlap(_triangleA, _triangleB, cross(cbB, cbA) )
		&& overlap(_triangleA, _triangleB, cross(cbB, acA) )
		&& overlap(_triangleA, _triangleB, cross(acB, baA) )
		&& overlap(_triangleA, _triangleB, cross(acB, cbA) )
		&& overlap(_triangleA, _triangleB, cross(acB, acA) )
		;
}

template<typename Ty>
bool overlap(const Triangle& _triangle, const Ty& _ty)
{
	Plane plane;
	calcPlane(plane, _triangle);

	plane.normal = neg(plane.normal);
	plane.dist   = -plane.dist;

	const LineSegment line =
	{
		_ty.pos,
		_ty.end,
	};

	Hit hit;
	if (intersect(line, plane, &hit) )
	{
		return true;
	}

	const Vec3 pos = closestPoint(plane, hit.pos);
	const Vec3 uvw = barycentric(_triangle, pos);

	const float nr = -_ty.radius;

	if (uvw.x >= nr
	&&  uvw.y >= nr
	&&  uvw.z >= nr)
	{
		return true;
	}

	const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1};
	const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2};
	const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0};

	float ta0, tb0;
	const bool i0 = intersect(ta0, tb0, ab, line);

	float ta1, tb1;
	const bool i1 = intersect(ta1, tb1, bc, line);

	float ta2, tb2;
	const bool i2 = intersect(ta2, tb2, ca, line);

	if (!i0
	||  !i1
	||  !i2)
	{
		return false;
	}

	ta0 = clamp(ta0, 0.0f, 1.0f);
	ta1 = clamp(ta1, 0.0f, 1.0f);
	ta2 = clamp(ta2, 0.0f, 1.0f);
	tb0 = clamp(tb0, 0.0f, 1.0f);
	tb1 = clamp(tb1, 0.0f, 1.0f);
	tb2 = clamp(tb2, 0.0f, 1.0f);

	const Vec3 pa0 = getPointAt(ab, ta0);
	const Vec3 pa1 = getPointAt(bc, ta1);
	const Vec3 pa2 = getPointAt(ca, ta2);

	const Vec3 pb0 = getPointAt(line, tb0);
	const Vec3 pb1 = getPointAt(line, tb1);
	const Vec3 pb2 = getPointAt(line, tb2);

	const float d0 = distanceSq(pa0, pb0);
	const float d1 = distanceSq(pa1, pb1);
	const float d2 = distanceSq(pa2, pb2);

	if (d0 <= d1
	&&  d0 <= d2)
	{
		return overlap(_ty, pa0);
	}
	else if (d1 <= d2)
	{
		return overlap(_ty, pa1);
	}

	return overlap(_ty, pa2);
}

bool overlap(const Triangle& _triangle, const Cylinder& _cylinder)
{
	return overlap<Cylinder>(_triangle, _cylinder);
}

bool overlap(const Triangle& _triangle, const Capsule& _capsule)
{
	return overlap<Capsule>(_triangle, _capsule);
}

bool overlap(const Triangle& _triangle, const Cone& _cone)
{
	const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1};
	const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2};
	const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0};

	const LineSegment line =
	{
		_cone.pos,
		_cone.end,
	};

	float ta0 = 0.0f, tb0 = 0.0f;
	const bool i0 = intersect(ta0, tb0, ab, line);

	float ta1, tb1;
	const bool i1 = intersect(ta1, tb1, bc, line);

	float ta2, tb2;
	const bool i2 = intersect(ta2, tb2, ca, line);

	if (!i0
	||  !i1
	||  !i2)
	{
		return false;
	}

	ta0 = clamp(ta0, 0.0f, 1.0f);
	ta1 = clamp(ta1, 0.0f, 1.0f);
	ta2 = clamp(ta2, 0.0f, 1.0f);
	tb0 = clamp(tb0, 0.0f, 1.0f);
	tb1 = clamp(tb1, 0.0f, 1.0f);
	tb2 = clamp(tb2, 0.0f, 1.0f);

	const Vec3 pa0 = getPointAt(ab, ta0);
	const Vec3 pa1 = getPointAt(bc, ta1);
	const Vec3 pa2 = getPointAt(ca, ta2);

	const Vec3 pb0 = getPointAt(line, tb0);
	const Vec3 pb1 = getPointAt(line, tb1);
	const Vec3 pb2 = getPointAt(line, tb2);

	const float d0 = distanceSq(pa0, pb0);
	const float d1 = distanceSq(pa1, pb1);
	const float d2 = distanceSq(pa2, pb2);

	if (d0 <= d1
	&&  d0 <= d2)
	{
		return overlap(_cone, pa0);
	}
	else if (d1 <= d2)
	{
		return overlap(_cone, pa1);
	}

	return overlap(_cone, pa2);
}

bool overlap(const Triangle& _triangle, const Disk& _disk)
{
	if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
	{
		return false;
	}

	Plane plane;
	calcPlane(plane, _disk.normal, _disk.center);

	return overlap(_triangle, plane);
}

bool overlap(const Triangle& _triangle, const Obb& _obb)
{
	Srt srt = toSrt(_obb.mtx);

	Aabb aabb;
	toAabb(aabb, srt.scale);

	const Quaternion invRotation = invert(srt.rotation);

	const Triangle triangle =
	{
		mul(sub(_triangle.v0, srt.translation), invRotation),
		mul(sub(_triangle.v1, srt.translation), invRotation),
		mul(sub(_triangle.v2, srt.translation), invRotation),
	};

	return overlap(triangle, aabb);
}