GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
geometry.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_GEOMETRY_HPP
5#define GREM_CORE_GEOMETRY_HPP
6
8
18#include <GREM/core/math.hpp>
19
20namespace grem {
21
28template <size_t N, typename T>
30
37template <size_t N, typename T>
39
46template <size_t N, typename T>
47struct Ray {
48 static constexpr size_t RANK = N;
49 using Component = T;
50
61 [[nodiscard]] static constexpr Optional<Ray> between(Point<N, T> origin, Point<N, T> destination) {
62 const vec<N, T> difference = destination - origin;
63 const T distance = length(difference);
64 const Optional<T> inverseDistance = tryDivide(T{1}, distance);
65 if (!inverseDistance) {
66 return {};
67 }
68 return Ray{.origin = origin, .direction = difference * *inverseDistance, .maxDistance = distance};
69 }
70
75
82
89
96};
97
101struct RayMiss {};
102
109template <size_t N, typename T>
115
122template <size_t N, typename T>
126
133template <size_t N, typename T>
134struct RaycastResult : Variant<RayMiss, RayHit<N, T>, RayHitInterior<N, T>> {
135 static constexpr size_t RANK = N;
136 using Component = T;
137
139};
140
148template <size_t N, typename T>
149struct Box {
150 static constexpr size_t RANK = N;
151 using Component = T;
152
155
161 template <typename U>
162 GREM_ALWAYS_INLINE constexpr explicit operator Box<N, U>() const noexcept {
163 return Box<N, U>{.min{min}, .max{max}};
164 }
165
173 [[nodiscard]] bool operator==(const Box& other) const = default;
174
182 [[nodiscard]] constexpr bool contains(const Point<N, T>& point) const noexcept {
183 return all(greaterThanEqual(point, min) & lessThan(point, max));
184 }
185
193 [[nodiscard]] constexpr bool intersects(const Ray<N, T>& ray) const noexcept {
194 const vec<N, T> minIntersection = (min - ray.origin) * ray.directionInverse;
195 const vec<N, T> maxIntersection = (max - ray.origin) * ray.directionInverse;
196 const vec<N, T> tEnter = grem::min(minIntersection, maxIntersection);
197 const vec<N, T> tLeave = grem::max(maxIntersection, minIntersection);
198 T t{};
199 T minLeave{};
200 if constexpr (N == 2) {
201 t = grem::max(tEnter.x, grem::max(tEnter.y, T{}));
202 minLeave = grem::min(tLeave.x, grem::min(tLeave.y, ray.maxDistance));
203 } else {
204 t = grem::max(tEnter.x, grem::max(tEnter.y, grem::max(tEnter.z, T{})));
205 minLeave = grem::min(tLeave.x, grem::min(tLeave.y, grem::min(tLeave.z, ray.maxDistance)));
206 }
207 return t <= minLeave;
208 }
209
217 [[nodiscard]] constexpr RaycastResult<N, T> raycast(const Ray<N, T>& ray) const noexcept {
218 const vec<N, T> minIntersection = (min - ray.origin) * ray.directionInverse;
219 const vec<N, T> maxIntersection = (max - ray.origin) * ray.directionInverse;
220 const vec<N, T> tEnter = grem::min(minIntersection, maxIntersection);
221 const vec<N, T> tLeave = grem::max(maxIntersection, minIntersection);
222 T t{};
223 T minLeave{};
224 if constexpr (N == 2) {
225 t = grem::max(tEnter.x, grem::max(tEnter.y, T{}));
226 minLeave = grem::min(tLeave.x, grem::min(tLeave.y, ray.maxDistance));
227 } else {
228 t = grem::max(tEnter.x, grem::max(tEnter.y, grem::max(tEnter.z, T{})));
229 minLeave = grem::min(tLeave.x, grem::min(tLeave.y, grem::min(tLeave.z, ray.maxDistance)));
230 }
231 if (t > minLeave) {
232 return RayMiss{};
233 }
234 if (contains(ray.origin)) {
235 return RayHitInterior<N, T>{.localOffset = ray.origin};
236 }
237 const vec<N, T> point = ray.origin + t * ray.direction;
238 return RayHit<N, T>{
239 .localOffset = point,
240 .distance = t,
241 .normal = normalize(trunc((point - midpoint(min, max)) / ((max - min) * static_cast<T>(0.49999)))),
242 };
243 }
244
250 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
251 return *this;
252 }
253
259 [[nodiscard]] constexpr Box<N, T> toBox() const noexcept {
260 return *this;
261 }
262
272 [[nodiscard]] constexpr Box<N, T> getExpanded(Length<N, T> expansion) const noexcept {
274 return {.min = min - expansion, .max = max + expansion};
275 }
276
286 [[nodiscard]] constexpr Box<N, T> getExpanded(T expansion) const noexcept {
287 return getExpanded(Length<N, T>{expansion});
288 }
289};
290
296template <typename T>
297struct Rectangle {
298 static constexpr size_t RANK = 2;
299 using Component = T;
300
303
309 template <typename U>
310 GREM_ALWAYS_INLINE constexpr explicit operator Rectangle<U>() const noexcept {
311 return Rectangle<U>{.position{position}, .size{size}};
312 }
313
319 constexpr operator Box<2, T>() const noexcept {
320 return toBox();
321 }
322
330 [[nodiscard]] bool operator==(const Rectangle& other) const = default;
331
339 [[nodiscard]] constexpr bool contains(const Point<2, T>& point) const noexcept {
340 return toBox().contains(point);
341 }
342
350 [[nodiscard]] constexpr bool intersects(const Ray<2, T>& ray) const noexcept {
351 return toBox().intersects(ray);
352 }
353
361 [[nodiscard]] constexpr RaycastResult<2, T> raycast(const Ray<2, T>& ray) const noexcept {
362 return toBox().raycast(ray);
363 }
364
370 [[nodiscard]] constexpr Box<2, T> getBoundingBox() const noexcept {
371 return toBox();
372 }
373
379 [[nodiscard]] constexpr Box<2, T> toBox() const noexcept {
380 return Box<2, T>{.min = position, .max = position + size};
381 }
382};
383
389template <typename T>
390struct Square {
391 static constexpr size_t RANK = 2;
392 using Component = T;
393
396
402 template <typename U>
403 GREM_ALWAYS_INLINE constexpr explicit operator Square<U>() const noexcept {
404 return Square<U>{.position{position}, .width = static_cast<U>(width)};
405 }
406
412 constexpr operator Rectangle<T>() const noexcept {
413 return Rectangle<T>{.position = position, .size{width}};
414 }
415
421 constexpr operator Box<2, T>() const noexcept {
422 return toBox();
423 }
424
432 [[nodiscard]] bool operator==(const Square& other) const = default;
433
441 [[nodiscard]] constexpr bool contains(const Point<2, T>& point) const noexcept {
442 return toBox().contains(point);
443 }
444
452 [[nodiscard]] constexpr bool intersects(const Ray<2, T>& ray) const noexcept {
453 return toBox().intersects(ray);
454 }
455
463 [[nodiscard]] constexpr RaycastResult<2, T> raycast(const Ray<2, T>& ray) const noexcept {
464 return toBox().raycast(ray);
465 }
466
472 [[nodiscard]] constexpr Box<2, T> getBoundingBox() const noexcept {
473 return toBox();
474 }
475
481 [[nodiscard]] constexpr Box<2, T> toBox() const noexcept {
482 return Box<2, T>{.min = position, .max = position + Length<2, T>{width}};
483 }
484};
485
491template <typename T>
492struct Cube {
493 static constexpr size_t RANK = 3;
494 using Component = T;
495
498
504 template <typename U>
505 GREM_ALWAYS_INLINE constexpr explicit operator Cube<U>() const noexcept {
506 return Cube<U>{.position{position}, .width = static_cast<U>(width)};
507 }
508
514 constexpr operator Box<3, T>() const noexcept {
515 return toBox();
516 }
517
525 [[nodiscard]] bool operator==(const Cube& other) const = default;
526
534 [[nodiscard]] constexpr bool contains(const Point<3, T>& point) const noexcept {
535 return toBox().contains(point);
536 }
537
545 [[nodiscard]] constexpr bool intersects(const Ray<3, T>& ray) const noexcept {
546 return toBox().intersects(ray);
547 }
548
556 [[nodiscard]] constexpr RaycastResult<3, T> raycast(const Ray<3, T>& ray) const noexcept {
557 return toBox().raycast(ray);
558 }
559
565 [[nodiscard]] constexpr Box<3, T> getBoundingBox() const noexcept {
566 return toBox();
567 }
568
574 [[nodiscard]] constexpr Box<3, T> toBox() const noexcept {
575 return Box<3, T>{.min = position, .max = position + Length<3, T>{width}};
576 }
577};
578
585template <size_t N, typename T>
586struct Triangle {
587 static constexpr size_t RANK = N;
588 using Component = T;
589
593
599 template <typename U>
600 GREM_ALWAYS_INLINE constexpr explicit operator Triangle<N, U>() const noexcept {
601 return Triangle<N, U>{.pointA{pointA}, .pointB{pointB}, .pointC{pointC}};
602 }
603
611 [[nodiscard]] bool operator==(const Triangle& other) const = default;
612
620 [[nodiscard]] constexpr bool contains(const Point<N, T>& point) const noexcept requires(N == 2) {
621 return (pointA.x - pointB.x) * (point.y - pointA.y) - (pointA.y - pointB.y) * (point.x - pointA.x) >= T{} && //
622 (pointB.x - pointC.x) * (point.y - pointB.y) - (pointB.y - pointC.y) * (point.x - pointB.x) >= T{} && //
623 (pointC.x - pointA.x) * (point.y - pointC.y) - (pointC.y - pointA.y) * (point.x - pointC.x) >= T{};
624 }
625
633 [[nodiscard]] constexpr bool intersects(const Ray<N, T>& ray) const noexcept {
634 return !raycast(ray).template is<RayMiss>();
635 }
636
644 [[nodiscard]] constexpr RaycastResult<N, T> raycast(const Ray<N, T>& ray) const noexcept {
645 if constexpr (N == 2) {
646 // Treat as a convex polygon with 3 sides.
647
648 const Length<N, T> ab = pointB - pointA;
649 const Length<N, T> bc = pointC - pointB;
650 const Length<N, T> ca = pointA - pointC;
651 const Array<Point<N, T>, 3> facePoints{pointA, pointB, pointC};
652 const Array<vec<N, T>, 3> faceNormals{
653 vec<N, T>{ab.y, -ab.x},
654 vec<N, T>{bc.y, -bc.x},
655 vec<N, T>{ca.y, -ca.x},
656 };
657
658 T farthestFrontFacingDistance = Limits<T>::MIN;
659 T closestBackFacingDistance = Limits<T>::MAX;
660 size_t farthestFrontFacingFaceIndex = 0;
661 for (size_t faceIndex = 0; faceIndex < 3; ++faceIndex) {
662 const Point<N, T> facePoint = facePoints[faceIndex];
663 const vec<N, T> faceNormal = faceNormals[faceIndex];
664
665 const T faceNormalAlongRay = dot(faceNormal, ray.direction);
666 const T rayOffsetFromFaceAlongFaceNormal = dot(ray.origin - facePoint, faceNormal);
667
668 const bool rayIsParallelWithFacePlane = abs(faceNormalAlongRay) < Limits<T>::MACHINE_EPSILON;
669 if (rayIsParallelWithFacePlane) {
670 const bool rayIsInFrontOfFacePlane = rayOffsetFromFaceAlongFaceNormal > T{};
671 if (rayIsInFrontOfFacePlane) {
672 // The ray is in front of a face and parallel with it.
673 // This means we have missed the entire shape, since it's convex.
674 return RayMiss{};
675 }
676
677 // We can't hit a parallel face. Ignore it.
678 continue;
679 }
680
681 // Compute signed hit distance.
682 const T t = -rayOffsetFromFaceAlongFaceNormal / faceNormalAlongRay;
683
684 const bool faceIsFrontFacing = faceNormalAlongRay < T{};
685 if (faceIsFrontFacing) {
686 // Face is front-facing. Make sure it's not further than any previous back-facing face.
687 if (t > closestBackFacingDistance) {
688 return RayMiss{};
689 }
690
691 // Update farthest front-facing distance.
692 if (t > farthestFrontFacingDistance) {
693 farthestFrontFacingDistance = t;
694 farthestFrontFacingFaceIndex = faceIndex;
695 }
696 } else {
697 // Face is back-facing. Make sure it's not closer than any previous front-facing face.
698 if (t < farthestFrontFacingDistance) {
699 return RayMiss{};
700 }
701
702 // Update closest back-facing distance.
703 if (t < closestBackFacingDistance) {
704 closestBackFacingDistance = t;
705 }
706 }
707 }
708
709 if (farthestFrontFacingDistance >= T{}) {
710 // We hit a front face.
711 if (farthestFrontFacingDistance > ray.maxDistance) {
712 return RayMiss{};
713 }
714 return RayHit<N, T>{
715 .localOffset = ray.origin + farthestFrontFacingDistance * ray.direction,
716 .distance = farthestFrontFacingDistance,
717 .normal = tryNormalize(faceNormals[farthestFrontFacingFaceIndex]).value_or(-ray.direction),
718 };
719 }
720
721 if (closestBackFacingDistance >= T{}) {
722 // We hit a back face from inside the shape.
723 return RayHitInterior<N, T>{.localOffset = ray.origin};
724 }
725
726 return RayMiss{};
727 } else {
728 const Length<N, T> ab = pointB - pointA;
729 const Length<N, T> ac = pointC - pointA;
730 const Length<N, T> ao = ray.origin - pointA;
731 const vec<N, T> n = cross(ab, ac);
732 const vec<N, T> q = cross(ao, ray.direction);
733 const T nAlongRay = dot(n, ray.direction);
734 if (abs(nAlongRay) <= Limits<T>::MACHINE_EPSILON) {
735 return RayMiss{};
736 }
737 const T d = T{1} / nAlongRay;
738 const T u = d * dot(-q, ac);
739 const T v = d * dot(q, ab);
740 if (u < T{} || v < T{} || u + v > T{1}) {
741 return RayMiss{};
742 }
743 const T t = d * dot(-n, ao);
744 if (t < T{} || t > ray.maxDistance) {
745 return RayMiss{};
746 }
747 return RayHit<N, T>{
748 .localOffset = ray.origin + t * ray.direction,
749 .distance = t,
750 .normal = tryNormalize(flipSignIf(n, !signbit(nAlongRay))).value_or(-ray.direction),
751 };
752 }
753 }
754
760 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
761 return {.min = min(min(pointA, pointB), pointC), .max = max(max(pointA, pointB), pointC)};
762 }
763};
764
771template <size_t N, typename T>
773 static constexpr size_t RANK = N;
774 using Component = T;
775
778
785 template <typename U>
786 GREM_ALWAYS_INLINE constexpr explicit operator LineSegment<N, U>() const noexcept {
787 return LineSegment<N, U>{.pointA{pointA}, .pointB{pointB}};
788 }
789
797 [[nodiscard]] bool operator==(const LineSegment& other) const = default;
798
804 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
805 return {
806 .min = min(pointA, pointB),
807 .max = max(pointA, pointB),
808 };
809 }
810};
811
819template <size_t N, typename T>
820struct Ellipsoid {
821 static constexpr size_t RANK = N;
822 using Component = T;
823
826
832 template <typename U>
833 GREM_ALWAYS_INLINE constexpr explicit operator Ellipsoid<N, U>() const noexcept {
834 return Ellipsoid<N, U>{.center{center}, .radii{radii}};
835 }
836
844 [[nodiscard]] bool operator==(const Ellipsoid& other) const = default;
845
853 [[nodiscard]] constexpr bool contains(const Point<N, T>& point) const noexcept {
854 return length2((point - center) / radii) < T{1};
855 }
856
864 [[nodiscard]] constexpr bool intersects(const Ray<N, T>& ray) const noexcept {
865 const Length<N, T> localRayOrigin = ray.origin - center;
866 const vec<N, T> offsetPerRadii = localRayOrigin / radii;
867 const T offsetPerRadiiSquared = length2(offsetPerRadii);
868 if (offsetPerRadiiSquared < T{1}) {
869 return true;
870 }
871 const vec<N, T> directionPerRadii = ray.direction / radii;
872 const T directionPerRadiiSquared = length2(directionPerRadii);
873 const T offsetAlongDirectionPerRadii = dot(offsetPerRadii, directionPerRadii);
874 const T discriminant = length2(offsetAlongDirectionPerRadii) - directionPerRadiiSquared * (offsetPerRadiiSquared - T{1});
875 if (discriminant < T{}) {
876 return false;
877 }
878 const T t = (-offsetAlongDirectionPerRadii - sqrt(discriminant)) / directionPerRadiiSquared;
879 return t >= T{} && t <= ray.maxDistance;
880 }
881
889 [[nodiscard]] constexpr RaycastResult<N, T> raycast(const Ray<N, T>& ray) const noexcept {
890 const Length<N, T> localRayOrigin = ray.origin - center;
891 const vec<N, T> offsetPerRadii = localRayOrigin / radii;
892 const T offsetPerRadiiSquared = length2(offsetPerRadii);
893 if (offsetPerRadiiSquared < T{1}) {
894 return RayHitInterior<N, T>{.localOffset = ray.origin};
895 }
896 const vec<N, T> directionPerRadii = ray.direction / radii;
897 const T directionPerRadiiSquared = length2(directionPerRadii);
898 const T offsetAlongDirectionPerRadii = dot(offsetPerRadii, directionPerRadii);
899 const T discriminant = length2(offsetAlongDirectionPerRadii) - directionPerRadiiSquared * (offsetPerRadiiSquared - T{1});
900 if (discriminant < T{}) {
901 return RayMiss{};
902 }
903 const T t = (-offsetAlongDirectionPerRadii - sqrt(discriminant)) / directionPerRadiiSquared;
904 if (t < T{} || t > ray.maxDistance) {
905 return RayMiss{};
906 }
907 const Point<N, T> point = ray.origin + t * ray.direction;
908 return RayHit<N, T>{
909 .localOffset = point,
910 .distance = t,
911 .normal = tryNormalize(point - center).value_or(-ray.direction),
912 };
913 }
914
920 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
921 return {
922 .min = center - radii,
923 .max = center + radii,
924 };
925 }
926
932 [[nodiscard]] constexpr Ellipsoid<N, T> toEllipsoid() const noexcept {
933 return *this;
934 }
935};
936
942template <typename T>
943struct Ellipse {
944 static constexpr size_t RANK = 2;
945 using Component = T;
946
949
955 template <typename U>
956 GREM_ALWAYS_INLINE constexpr explicit operator Ellipse<U>() const noexcept {
957 return Ellipse<U>{.center{center}, .radii{radii}};
958 }
959
965 constexpr operator Ellipsoid<2, T>() const noexcept {
966 return toEllipsoid();
967 }
968
976 [[nodiscard]] bool operator==(const Ellipse& other) const = default;
977
985 [[nodiscard]] constexpr bool contains(const Point<2, T>& point) const noexcept {
986 return toEllipsoid().contains(point);
987 }
988
994 [[nodiscard]] constexpr Box<2, T> getBoundingBox() const noexcept {
995 return {
996 .min = center - radii,
997 .max = center + radii,
998 };
999 }
1000
1006 [[nodiscard]] constexpr Ellipsoid<2, T> toEllipsoid() const noexcept {
1007 return Ellipsoid<2, T>{.center = center, .radii = radii};
1008 }
1009};
1010
1017template <size_t N, typename T>
1018struct Sphere {
1019 static constexpr size_t RANK = N;
1020 using Component = T;
1021
1024
1030 template <typename U>
1031 GREM_ALWAYS_INLINE constexpr explicit operator Sphere<N, U>() const noexcept {
1032 return Sphere<N, U>{.center{center}, .radius = static_cast<U>(radius)};
1033 }
1034
1040 constexpr operator Ellipsoid<N, T>() const noexcept {
1041 return toEllipsoid();
1042 }
1043
1049 constexpr operator Ellipse<T>() const noexcept requires(N == 2) {
1050 return Ellipse<T>{.center = center, .radii{radius}};
1051 }
1052
1060 [[nodiscard]] bool operator==(const Sphere& other) const = default;
1061
1069 [[nodiscard]] constexpr bool contains(const Point<N, T>& point) const noexcept {
1070 return distance2(center, point) < length2(radius);
1071 }
1072
1080 [[nodiscard]] constexpr bool intersects(const Ray<N, T>& ray) const noexcept {
1081 const Length<N, T> difference = ray.origin - center;
1082 const T differenceAlongRay = dot(difference, ray.direction);
1083 const T differenceOfSquares = length2(difference) - length2(radius);
1084 if (differenceOfSquares < T{}) {
1085 return true;
1086 }
1087 if (differenceAlongRay > T{}) {
1088 return false;
1089 }
1090 const T discriminant = length2(differenceAlongRay) - differenceOfSquares;
1091 if (discriminant < T{}) {
1092 return false;
1093 }
1094 const T t = -differenceAlongRay - sqrt(discriminant);
1095 return t >= T{} && t <= ray.maxDistance;
1096 }
1097
1105 [[nodiscard]] constexpr RaycastResult<N, T> raycast(const Ray<N, T>& ray) const noexcept {
1106 const Length<N, T> difference = ray.origin - center;
1107 const T differenceAlongRay = dot(difference, ray.direction);
1108 const T differenceOfSquares = length2(difference) - length2(radius);
1109 if (differenceOfSquares < T{}) {
1110 return RayHitInterior<N, T>{.localOffset = ray.origin};
1111 }
1112 if (differenceAlongRay > T{}) {
1113 return RayMiss{};
1114 }
1115 const T discriminant = length2(differenceAlongRay) - differenceOfSquares;
1116 if (discriminant < T{}) {
1117 return RayMiss{};
1118 }
1119 const T t = -differenceAlongRay - sqrt(discriminant);
1120 if (t < T{} || t > ray.maxDistance) {
1121 return RayMiss{};
1122 }
1123 const Point<N, T> point = ray.origin + t * ray.direction;
1124 return RayHit<N, T>{
1125 .localOffset = point,
1126 .distance = t,
1127 .normal = tryNormalize(point - center).value_or(-ray.direction),
1128 };
1129 }
1130
1136 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
1137 return {
1138 .min = center - Length<N, T>{radius},
1139 .max = center + Length<N, T>{radius},
1140 };
1141 }
1142
1148 [[nodiscard]] constexpr Ellipsoid<N, T> toEllipsoid() const noexcept {
1149 return Ellipsoid<N, T>{.center = center, .radii{radius}};
1150 }
1151
1157 [[nodiscard]] constexpr Sphere<N, T> toSphere() const noexcept {
1158 return *this;
1159 }
1160};
1161
1167template <typename T>
1168struct Circle {
1169 static constexpr size_t RANK = 2;
1170 using Component = T;
1171
1174
1180 template <typename U>
1181 GREM_ALWAYS_INLINE constexpr explicit operator Circle<U>() const noexcept {
1182 return Circle<U>{.center{center}, .radius = static_cast<U>(radius)};
1183 }
1184
1190 constexpr operator Ellipsoid<2, T>() const noexcept {
1191 return toEllipsoid();
1192 }
1193
1199 constexpr operator Ellipse<T>() const noexcept {
1200 return Ellipse<T>{.center = center, .radii{radius}};
1201 }
1202
1208 constexpr operator Sphere<2, T>() const noexcept {
1209 return toSphere();
1210 }
1211
1219 [[nodiscard]] bool operator==(const Circle& other) const = default;
1220
1228 [[nodiscard]] constexpr bool contains(const Point<2, T>& point) const noexcept {
1229 return static_cast<Sphere<2, T>>(*this).contains(point);
1230 }
1231
1237 [[nodiscard]] constexpr Box<2, T> getBoundingBox() const noexcept {
1238 return {
1239 .min = center - Length<2, T>{radius},
1240 .max = center + Length<2, T>{radius},
1241 };
1242 }
1243
1249 [[nodiscard]] constexpr Ellipsoid<2, T> toEllipsoid() const noexcept {
1250 return Ellipsoid<2, T>{.center = center, .radii{radius}};
1251 }
1252
1258 [[nodiscard]] constexpr Sphere<2, T> toSphere() const noexcept {
1259 return Sphere<2, T>{.center = center, .radius = radius};
1260 }
1261};
1262
1270template <size_t N, typename T>
1271struct Capsule {
1272 static constexpr size_t RANK = N;
1273 using Component = T;
1274
1277
1283 template <typename U>
1284 GREM_ALWAYS_INLINE constexpr explicit operator Capsule<N, U>() const noexcept {
1285 return Capsule<N, U>{.centerLine = static_cast<LineSegment<N, U>>(centerLine), .radius = static_cast<U>(radius)};
1286 }
1287
1295 [[nodiscard]] bool operator==(const Capsule& other) const = default;
1296
1304 [[nodiscard]] constexpr bool contains(const Point<N, T>& point) const noexcept;
1305
1311 [[nodiscard]] constexpr Box<N, T> getBoundingBox() const noexcept {
1312 return {
1313 .min = min(centerLine.pointA, centerLine.pointB) - Length<N, T>{radius},
1314 .max = max(centerLine.pointA, centerLine.pointB) + Length<N, T>{radius},
1315 };
1316 }
1317};
1318
1324template <typename T>
1325struct Frustum {
1326 static constexpr size_t RANK = 3;
1327 using Component = T;
1328
1337 static constexpr Frustum fromViewProjectionMatrix(const mat<4, 4, T>& viewProjectionMatrix) noexcept {
1338 constexpr mat<4, 4, T> CONVERT_DEPTH_REVERSE_RANGE{
1339 // clang-format off
1340 T{1}, T{0}, T{0}, T{0},
1341 T{0}, T{1}, T{0}, T{0},
1342 T{0}, T{0}, T{-1}, T{0},
1343 T{0}, T{0}, T{1}, T{1},
1344 // clang-format on
1345 };
1346
1347 constexpr mat<4, 4, T> CONVERT_DEPTH_FROM_ZO_TO_NO{
1348 // clang-format off
1349 T{1}, T{0}, T{0}, T{0},
1350 T{0}, T{1}, T{0}, T{0},
1351 T{0}, T{0}, T{2}, T{0},
1352 T{0}, T{0}, T{-1}, T{1},
1353 // clang-format on
1354 };
1355
1356 constexpr mat<4, 4, T> DEPTH_CONVERSION_MATRIX = CONVERT_DEPTH_FROM_ZO_TO_NO * CONVERT_DEPTH_REVERSE_RANGE;
1357
1358 const mat<4, 4, T> transposedViewProjectionMatrix = transpose(DEPTH_CONVERSION_MATRIX * viewProjectionMatrix);
1359
1360 const auto normalizeXYZ = [](vec<4, T> plane) -> vec<4, T> {
1361 return plane / length(vec<3, T>{plane});
1362 };
1363
1364 const vec<4, T> left = normalizeXYZ(transposedViewProjectionMatrix[3] + transposedViewProjectionMatrix[0]);
1365 const vec<4, T> right = normalizeXYZ(transposedViewProjectionMatrix[3] - transposedViewProjectionMatrix[0]);
1366 const vec<4, T> bottom = normalizeXYZ(transposedViewProjectionMatrix[3] + transposedViewProjectionMatrix[1]);
1367 const vec<4, T> top = normalizeXYZ(transposedViewProjectionMatrix[3] - transposedViewProjectionMatrix[1]);
1368 const vec<4, T> farPlane = normalizeXYZ(transposedViewProjectionMatrix[3] + transposedViewProjectionMatrix[2]);
1369 const vec<4, T> nearPlane = normalizeXYZ(transposedViewProjectionMatrix[3] - transposedViewProjectionMatrix[2]);
1370
1371 const Array planeNormalCrossProducts{
1372 cross(vec<3, T>{left}, vec<3, T>{right}),
1373 cross(vec<3, T>{left}, vec<3, T>{bottom}),
1374 cross(vec<3, T>{left}, vec<3, T>{top}),
1375 cross(vec<3, T>{left}, vec<3, T>{farPlane}),
1376 cross(vec<3, T>{left}, vec<3, T>{nearPlane}),
1377 cross(vec<3, T>{right}, vec<3, T>{bottom}),
1378 cross(vec<3, T>{right}, vec<3, T>{top}),
1379 cross(vec<3, T>{right}, vec<3, T>{farPlane}),
1380 cross(vec<3, T>{right}, vec<3, T>{nearPlane}),
1381 cross(vec<3, T>{bottom}, vec<3, T>{top}),
1382 cross(vec<3, T>{bottom}, vec<3, T>{farPlane}),
1383 cross(vec<3, T>{bottom}, vec<3, T>{nearPlane}),
1384 cross(vec<3, T>{top}, vec<3, T>{farPlane}),
1385 cross(vec<3, T>{top}, vec<3, T>{nearPlane}),
1386 cross(vec<3, T>{farPlane}, vec<3, T>{nearPlane}),
1387 };
1388
1389 const Array planes{left, right, bottom, top, farPlane, nearPlane};
1390
1391 const Array corners{
1392 getCorner<0, 2, 4>(planes, planeNormalCrossProducts),
1393 getCorner<0, 3, 4>(planes, planeNormalCrossProducts),
1394 getCorner<1, 2, 4>(planes, planeNormalCrossProducts),
1395 getCorner<1, 3, 4>(planes, planeNormalCrossProducts),
1396 getCorner<0, 2, 5>(planes, planeNormalCrossProducts),
1397 getCorner<0, 3, 5>(planes, planeNormalCrossProducts),
1398 getCorner<1, 2, 5>(planes, planeNormalCrossProducts),
1399 getCorner<1, 3, 5>(planes, planeNormalCrossProducts),
1400 };
1401
1402 return Frustum{.planes = planes, .corners = corners};
1403 }
1404
1424 [[nodiscard]] static constexpr Frustum fromCorners(StridedSpan<const Point<3, T>, 8> corners) {
1425 const auto getPlaneFromTriangle = [](Point<3, T> a, Point<3, T> b, Point<3, T> c) -> vec<4, T> {
1426 const vec<3, T> v = normalize(cross(b - a, c - a));
1427 return vec<4, T>{v, -dot(v, a)};
1428 };
1429 return Frustum{
1430 .planes{
1431 getPlaneFromTriangle(corners[4], corners[0], corners[1]),
1432 getPlaneFromTriangle(corners[6], corners[3], corners[2]),
1433 getPlaneFromTriangle(corners[4], corners[2], corners[0]),
1434 getPlaneFromTriangle(corners[5], corners[1], corners[3]),
1435 getPlaneFromTriangle(corners[0], corners[2], corners[1]),
1436 getPlaneFromTriangle(corners[4], corners[7], corners[6]),
1437 },
1438 .corners{corners[0], corners[1], corners[2], corners[3], corners[4], corners[5], corners[6], corners[7]},
1439 };
1440 }
1441
1453
1466
1472 template <typename U>
1473 GREM_ALWAYS_INLINE constexpr explicit operator Frustum<U>() const noexcept {
1474 Frustum<U> result{};
1475 for (size_t i = 0; i < planes.size(); ++i) {
1476 result.planes[i] = static_cast<vec<4, U>>(planes[i]);
1477 }
1478 for (size_t i = 0; i < corners.size(); ++i) {
1479 result.corners[i] = static_cast<Point<3, U>>(corners[i]);
1480 }
1481 return result;
1482 }
1483
1493 [[nodiscard]] constexpr bool isPotentiallyIntersecting(const Box<3, T>& aabb) const noexcept {
1494 const Array aabbCorners{
1495 Point<3, T>{aabb.min.x, aabb.min.y, aabb.min.z},
1496 Point<3, T>{aabb.max.x, aabb.min.y, aabb.min.z},
1497 Point<3, T>{aabb.min.x, aabb.max.y, aabb.min.z},
1498 Point<3, T>{aabb.max.x, aabb.max.y, aabb.min.z},
1499 Point<3, T>{aabb.min.x, aabb.min.y, aabb.max.z},
1500 Point<3, T>{aabb.max.x, aabb.min.y, aabb.max.z},
1501 Point<3, T>{aabb.min.x, aabb.max.y, aabb.max.z},
1502 Point<3, T>{aabb.max.x, aabb.max.y, aabb.max.z},
1503 };
1504 return noneOf(planes, [&](const vec<4, T>& plane) -> bool {
1505 return allOf(aabbCorners, [&](const Point<3, T>& aabbCorner) -> bool { return dot(aabbCorner, vec<3, T>{plane}) < -plane.w; });
1506 });
1507 }
1508
1517 [[nodiscard]] constexpr bool isPotentiallyIntersecting(const Sphere<3, T>& sphere) const noexcept {
1518 bool culled = false;
1519 for (const vec<4, T>& plane : planes) {
1520 culled |= dot(sphere.center, vec<3, T>{plane}) + sphere.radius < -plane.w;
1521 }
1522 return !culled;
1523 }
1524
1525private:
1526 [[nodiscard]] static constexpr size_t getPlaneNormalCrossProductIndex(size_t planeIndexA, size_t planeIndexB) noexcept {
1527 return planeIndexA * (9 - planeIndexA) / 2 + planeIndexB - 1;
1528 }
1529
1530 template <size_t PlaneIndexA, size_t PlaneIndexB, size_t PlaneIndexC>
1531 [[nodiscard]] static constexpr Point<3, T> getCorner(Span<const vec<4, T>, 6> planes, Span<const vec<3, T>, 15> planeNormalCrossProducts) noexcept {
1532 const vec<4, T>& a = planes[PlaneIndexA];
1533 const vec<4, T>& b = planes[PlaneIndexB];
1534 const vec<4, T>& c = planes[PlaneIndexC];
1535 constexpr size_t ab = getPlaneNormalCrossProductIndex(PlaneIndexA, PlaneIndexB);
1536 constexpr size_t ac = getPlaneNormalCrossProductIndex(PlaneIndexA, PlaneIndexC);
1537 constexpr size_t bc = getPlaneNormalCrossProductIndex(PlaneIndexB, PlaneIndexC);
1538 const vec<3, T> axb = planeNormalCrossProducts[ab];
1539 const vec<3, T> axc = planeNormalCrossProducts[ac];
1540 const vec<3, T> bxc = planeNormalCrossProducts[bc];
1541 return (mat<3, 3, T>{bxc, axc, axb} * vec<3, T>{a.w, -b.w, c.w}) * (-1.0f / dot(vec<3, T>{a}, bxc));
1542 }
1543};
1544
1550template <typename T>
1552 { t.contains(point) } -> convertible_to<bool>;
1553 { t.intersects(ray) } -> convertible_to<bool>;
1555};
1556
1562template <typename T>
1563concept axis_aligned_box_shape = requires(const T t) {
1565};
1566
1572template <typename T>
1573concept axis_aligned_ellipsoid_shape = requires(const T t) {
1575};
1576
1582template <typename T>
1583concept spherical_shape = requires(const T t) {
1585};
1586
1594template <size_t N, typename T>
1595[[nodiscard]] constexpr Box<N, T> calculateBoundingBox(StridedSpan<const Point<N, T>> points) noexcept {
1596 if (points.empty()) {
1597 return Box<N, T>{};
1598 }
1599 Box<N, T> result{
1600 .min = points.front(),
1601 .max = points.front(),
1602 };
1603 for (const Point<N, T> vertex : points.subspan(1)) {
1604 result.min = min(result.min, vertex);
1605 result.max = max(result.max, vertex);
1606 }
1607 return result;
1608}
1609
1617template <size_t N, typename T>
1618[[nodiscard]] constexpr T calculateBoundingRadius(StridedSpan<const Point<N, T>> points) noexcept requires(floating_point<T>) {
1619 T result{};
1620 for (const Point<N, T> point : points) {
1621 result = max(result, length2(point));
1622 }
1623 return sqrt(result);
1624}
1625
1636template <size_t N, typename T>
1637[[nodiscard]] constexpr Box<N, T> getTransformedBoundingBox(const mat<N + 1, N + 1, T>& transformation, const Box<N, T>& aabb) noexcept requires(floating_point<T>) {
1638 const Length<N, T> localCenter = midpoint(aabb.min, aabb.max);
1639 const Point<N, T> center = Point<N, T>{transformation * Length<N + 1, T>{localCenter, T{1}}};
1640 const Length<N, T> halfExtents = localCenter - aabb.min;
1641 Box<N, T> result{.min = center, .max = center};
1642 for (size_t y = 0; y < N; ++y) {
1643 for (size_t x = 0; x < N; ++x) {
1644 const T a = transformation[y][x] * -halfExtents[y];
1645 const T b = transformation[y][x] * halfExtents[y];
1646 result.min[x] += min(a, b);
1647 result.max[x] += max(a, b);
1648 }
1649 }
1650 return result;
1651}
1652
1662[[nodiscard]] constexpr bool intersects(const spherical_shape auto& a, const spherical_shape auto& b) noexcept {
1663 const auto sphereA = a.toSphere();
1664 const auto sphereB = b.toSphere();
1665 return distance2(sphereA.center, sphereB.center) < length2(sphereA.radius + sphereB.radius);
1666}
1667
1677[[nodiscard]] constexpr bool intersects(const axis_aligned_box_shape auto& a, const axis_aligned_box_shape auto& b) noexcept {
1678 const auto boxA = a.toBox();
1679 const auto boxB = b.toBox();
1680 return all(lessThan(boxA.min, boxB.max) & greaterThan(boxA.max, boxB.min));
1681}
1682
1692[[nodiscard]] constexpr bool intersects(const spherical_shape auto& a, const axis_aligned_box_shape auto& b) noexcept {
1693 const auto sphereA = a.toSphere();
1694 const auto boxB = b.toBox();
1695 return distance2(sphereA.center, clamp(sphereA.center, boxB.min, boxB.max)) < length2(sphereA.radius);
1696}
1697
1707[[nodiscard]] constexpr bool intersects(const axis_aligned_box_shape auto& a, const spherical_shape auto& b) noexcept {
1708 return intersects(b, a);
1709}
1710
1720template <size_t N, typename T>
1721[[nodiscard]] constexpr bool intersects(const raycastable_shape auto& a, const LineSegment<N, T>& b) noexcept {
1722 const Optional<Ray<N, T>> ray = Ray<N, T>::between(b.pointA, b.pointB);
1723 return (ray) ? a.intersects(*ray) : a.contains(b.pointA);
1724}
1725
1735template <size_t N, typename T>
1736[[nodiscard]] constexpr bool intersects(const LineSegment<N, T>& a, const raycastable_shape auto& b) noexcept {
1737 return intersects(b, a);
1738}
1739
1749template <typename T>
1750[[nodiscard]] constexpr bool intersects(const axis_aligned_box_shape auto& a, const Capsule<2, T>& b) noexcept {
1751 const Box<2, T> boxA = a.toBox();
1752 const Circle<T> capsuleCircleA{.center = b.centerLine.pointA, .radius = b.radius};
1753 const Circle<T> capsuleCircleB{.center = b.centerLine.pointB, .radius = b.radius};
1754 if (intersects(boxA, capsuleCircleA) || intersects(boxA, capsuleCircleB)) {
1755 return true;
1756 }
1757 const Optional<Ray<2, T>> ray = Ray<2, T>::between(b.centerLine.pointA, b.centerLine.pointB);
1758 if (!ray) {
1759 return false;
1760 }
1761 const vec<2, T> boxExpansion = b.radius * vec<2, T>{abs(ray->direction.y), abs(ray->direction.x)};
1762 return boxA.getExpanded(boxExpansion).intersects(*ray);
1763}
1764
1774template <typename T>
1775[[nodiscard]] constexpr bool intersects(const Capsule<2, T>& a, const axis_aligned_box_shape auto& b) noexcept {
1776 return intersects(b, a);
1777}
1778
1788template <size_t N, typename T>
1789[[nodiscard]] constexpr bool intersects(const spherical_shape auto& a, const Capsule<N, T>& b) noexcept {
1790 const Sphere<N, T> sphereA = a.toSphere();
1791 const T combinedRadiusSquared = length2(sphereA.radius + b.radius);
1792 const vec<N, T> linePointAToPointB = b.centerLine.pointB - b.centerLine.pointA;
1793 const vec<N, T> linePointAToSphereCenter = sphereA.center - b.centerLine.pointA;
1794 const T linePointAToSphereCenterAlongLine = dot(linePointAToSphereCenter, linePointAToPointB);
1795 if (linePointAToSphereCenterAlongLine <= 0.0f) {
1796 return length2(linePointAToSphereCenter) < combinedRadiusSquared;
1797 }
1798 const vec<N, T> linePointBToSphereCenter = sphereA.center - b.centerLine.pointB;
1799 const T linePointBToSphereCenterAlongLine = dot(linePointBToSphereCenter, linePointAToPointB);
1800 if (linePointBToSphereCenterAlongLine >= 0.0f) {
1801 return length2(linePointBToSphereCenter) < combinedRadiusSquared;
1802 }
1803 const vec<N, T> lineToSphereCenterOrthogonal = linePointAToSphereCenter - linePointAToPointB * (linePointAToSphereCenterAlongLine / length2(linePointAToPointB));
1804 return length2(lineToSphereCenterOrthogonal) < combinedRadiusSquared;
1805}
1806
1816template <size_t N, typename T>
1817[[nodiscard]] constexpr bool intersects(const Capsule<N, T>& a, const spherical_shape auto& b) noexcept {
1818 return intersects(b, a);
1819}
1820
1830template <size_t N, typename T>
1831[[nodiscard]] constexpr bool intersects(const Sphere<N, T>& a, const LineSegment<N, T>& b) noexcept {
1832 return intersects(a, Capsule<N, T>{.centerLine = b, .radius = T{}});
1833}
1834
1844template <size_t N, typename T>
1845[[nodiscard]] constexpr bool intersects(const LineSegment<N, T>& a, const Sphere<N, T>& b) noexcept {
1846 return intersects(b, a);
1847}
1848
1858template <typename T>
1859[[nodiscard]] constexpr bool intersects(const Circle<T>& a, const LineSegment<2, T>& b) noexcept {
1860 return intersects(static_cast<Sphere<2, T>>(a), b);
1861}
1862
1872template <typename T>
1873[[nodiscard]] constexpr bool intersects(const LineSegment<2, T>& a, const Circle<T>& b) noexcept {
1874 return intersects(b, a);
1875}
1876
1877template <size_t N, typename T>
1878constexpr bool Capsule<N, T>::contains(const Point<N, T>& point) const noexcept {
1879 return intersects(*this, Sphere<N, T>{.center = point, .radius = T{}});
1880}
1881
1882} // namespace grem
1883
1884#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition StridedSpan.hpp:427
Concept that checks if a given type is an axis-aligned box shape type.
Definition geometry.hpp:1563
Concept that checks if a given type is an axis-aligned ellipsoid shape type.
Definition geometry.hpp:1573
Definition concepts.hpp:47
Definition concepts.hpp:85
Concept that checks if a given type is a raycastable shape type.
Definition geometry.hpp:1551
Concept that checks if a given type is a spherical shape type.
Definition geometry.hpp:1583
Definition aliases.hpp:9
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length2(T a)
Definition math.hpp:1969
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL lessThan(T a, T b)
Definition math.hpp:1240
GREM_ALWAYS_INLINE constexpr T midpoint(T a, T b)
Get the midpoint between two values.
Definition fundamentals.hpp:559
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance(T a, T b)
Definition math.hpp:2038
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL flipSignIf(T value, bool flip)
Get a value with its sign flipped if a boolean is true.
Definition math.hpp:3983
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sqrt(T a)
Get the square root of a value.
Definition math.hpp:1114
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL normalize(vec< N, T > a)
Convert a vector to a unit vector of length 1 in the same direction.
Definition math.hpp:2057
constexpr Box< N, T > getTransformedBoundingBox(const mat< N+1, N+1, T > &transformation, const Box< N, T > &aabb) noexcept
Get the axis-aligned bounding box of an axis-aligned box with an affine transformation applied to it.
Definition geometry.hpp:1637
constexpr T calculateBoundingRadius(StridedSpan< const Point< N, T > > points) noexcept
Calculate the bounding radius from the origin of a set of points.
Definition geometry.hpp:1618
GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T &&value, Low &&minValue, High &&maxValue)
Clamp a value to a specific interval.
Definition fundamentals.hpp:630
GREM_ALWAYS_INLINE constexpr Optional< T > GREM_VECTORCALL tryDivide(T a, T b)
Try to divide a floating-point value by a scalar and produce a defined result.
Definition math.hpp:2101
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThan(T a, T b)
Definition math.hpp:1250
vec< N, T > Length
Generic length in N-dimensional space.
Definition geometry.hpp:38
constexpr bool intersects(const spherical_shape auto &a, const spherical_shape auto &b) noexcept
Check if two spherical shapes intersect.
Definition geometry.hpp:1662
GREM_ALWAYS_INLINE constexpr mat< R, C, T > transpose(const mat< C, R, T > &a)
Definition math.hpp:2247
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL cross(vec< 2, T > a, vec< 2, T > b)
Definition math.hpp:2426
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL abs(vec< N, T > a)
Definition math.hpp:1386
constexpr bool noneOf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:230
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance2(T a, T b)
Definition math.hpp:2026
vec< N, T > Point
Generic point in N-dimensional space.
Definition geometry.hpp:29
GREM_ALWAYS_INLINE constexpr Optional< vec< N, T > > GREM_VECTORCALL tryNormalize(vec< N, T > a)
Try to convert a vector to a unit vector of length 1 in the same direction.
Definition math.hpp:2082
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL trunc(vec< N, T > a)
Definition math.hpp:1420
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
GREM_ALWAYS_INLINE vec< N, bool > GREM_VECTORCALL signbit(vec< N, T > values)
Get the sign bits of the components of a vector.
Definition math.hpp:3584
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThanEqual(T a, T b)
Definition math.hpp:1255
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL all(bool a)
Definition math.hpp:1285
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
constexpr bool allOf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:226
constexpr Box< N, T > calculateBoundingBox(StridedSpan< const Point< N, T > > points) noexcept
Calculate the axis-aligned bounding box of a set of points.
Definition geometry.hpp:1595
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL dot(vec< 1, T > a, vec< 1, T > b)
Definition math.hpp:1942
Definition Array.hpp:20
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
constexpr Box< N, T > getExpanded(T expansion) const noexcept
Get a version of the box that is symmetrically expanded by a certain amount.
Definition geometry.hpp:286
constexpr bool contains(const Point< N, T > &point) const noexcept
Check if a given point is contained within the extents of this box.
Definition geometry.hpp:182
constexpr Box< N, T > getExpanded(Length< N, T > expansion) const noexcept
Get a version of the box that is symmetrically expanded by a certain amount.
Definition geometry.hpp:272
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:150
Point< N, T > max
Position with the maximum coordinates of the box extents on each coordinate axis.
Definition geometry.hpp:154
bool operator==(const Box &other) const =default
Compare this box to another for equality.
constexpr Box< N, T > toBox() const noexcept
Convert this shape to an equivalent axis-aligned bounding box.
Definition geometry.hpp:259
constexpr RaycastResult< N, T > raycast(const Ray< N, T > &ray) const noexcept
Find the closest intersection of this box with a given ray.
Definition geometry.hpp:217
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the box.
Definition geometry.hpp:250
constexpr bool intersects(const Ray< N, T > &ray) const noexcept
Check if this box intersects a given ray.
Definition geometry.hpp:193
T Component
Scalar coordinate component type.
Definition geometry.hpp:151
Point< N, T > min
Position with the minimum coordinates of the box extents on each coordinate axis.
Definition geometry.hpp:153
Generic capsule shape with a center line segment and radius in N-dimensional space.
Definition geometry.hpp:1271
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the capsule.
Definition geometry.hpp:1311
T radius
Radius of the capsule from the center line.
Definition geometry.hpp:1276
bool operator==(const Capsule &other) const =default
Compare this capsule to another for equality.
LineSegment< N, T > centerLine
Center line of the capsule.
Definition geometry.hpp:1275
T Component
Scalar coordinate component type.
Definition geometry.hpp:1273
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:1272
constexpr bool contains(const Point< N, T > &point) const noexcept
Check if a given point is contained within the extents of this capsule.
Definition geometry.hpp:1878
Flat 2D circle shape with a center and radius.
Definition geometry.hpp:1168
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:1169
constexpr Sphere< 2, T > toSphere() const noexcept
Convert this shape to an equivalent sphere.
Definition geometry.hpp:1258
T Component
Scalar coordinate component type.
Definition geometry.hpp:1170
constexpr Ellipsoid< 2, T > toEllipsoid() const noexcept
Convert this shape to an equivalent ellipsoid.
Definition geometry.hpp:1249
T radius
Radius of the circle.
Definition geometry.hpp:1173
constexpr bool contains(const Point< 2, T > &point) const noexcept
Check if a given point is contained within the extents of this circle.
Definition geometry.hpp:1228
constexpr Box< 2, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the circle.
Definition geometry.hpp:1237
Point< 2, T > center
Position of the center of the circle.
Definition geometry.hpp:1172
bool operator==(const Circle &other) const =default
Compare this circle to another for equality.
3D axis-aligned cube shape with a position and width.
Definition geometry.hpp:492
constexpr bool contains(const Point< 3, T > &point) const noexcept
Check if a given point is contained within the extents of this cube.
Definition geometry.hpp:534
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:493
constexpr bool intersects(const Ray< 3, T > &ray) const noexcept
Check if this cube intersects a given ray.
Definition geometry.hpp:545
constexpr Box< 3, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the cube.
Definition geometry.hpp:565
T width
Width, height and depth of the cube.
Definition geometry.hpp:497
Point< 3, T > position
Position of the far bottom left corner of the cube.
Definition geometry.hpp:496
constexpr RaycastResult< 3, T > raycast(const Ray< 3, T > &ray) const noexcept
Find the closest intersection of this cube with a given ray.
Definition geometry.hpp:556
constexpr Box< 3, T > toBox() const noexcept
Convert this shape to an equivalent axis-aligned bounding box.
Definition geometry.hpp:574
T Component
Scalar coordinate component type.
Definition geometry.hpp:494
bool operator==(const Cube &other) const =default
Compare this cube to another for equality.
Flat 2D ellipse shape with a center and axis-aligned radii.
Definition geometry.hpp:943
Point< 2, T > center
Position of the center of the ellipse.
Definition geometry.hpp:947
Length< 2, T > radii
Radii of the ellipse along each axis.
Definition geometry.hpp:948
constexpr Ellipsoid< 2, T > toEllipsoid() const noexcept
Convert this shape to an equivalent ellipsoid.
Definition geometry.hpp:1006
T Component
Scalar coordinate component type.
Definition geometry.hpp:945
constexpr bool contains(const Point< 2, T > &point) const noexcept
Check if a given point is contained within the extents of this ellipse.
Definition geometry.hpp:985
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:944
constexpr Box< 2, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the ellipse.
Definition geometry.hpp:994
bool operator==(const Ellipse &other) const =default
Compare this ellipse to another for equality.
Generic ellipsoid shape with a center and axis-aligned radii in N-dimensional space.
Definition geometry.hpp:820
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:821
constexpr bool intersects(const Ray< N, T > &ray) const noexcept
Check if this ellipsoid intersects a given ray.
Definition geometry.hpp:864
Length< N, T > radii
Radii of the ellipsoid along each axis.
Definition geometry.hpp:825
constexpr Ellipsoid< N, T > toEllipsoid() const noexcept
Convert this shape to an equivalent ellipsoid.
Definition geometry.hpp:932
Point< N, T > center
Position of the center of the ellipsoid.
Definition geometry.hpp:824
constexpr RaycastResult< N, T > raycast(const Ray< N, T > &ray) const noexcept
Find the closest intersection of this ellipsoid with a given ray.
Definition geometry.hpp:889
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the sphere.
Definition geometry.hpp:920
bool operator==(const Ellipsoid &other) const =default
Compare this ellipsoid to another for equality.
T Component
Scalar coordinate component type.
Definition geometry.hpp:822
constexpr bool contains(const Point< N, T > &point) const noexcept
Check if a given point is contained within the extents of this ellipsoid.
Definition geometry.hpp:853
Truncated pyramid shape in 3D space.
Definition geometry.hpp:1325
constexpr bool isPotentiallyIntersecting(const Box< 3, T > &aabb) const noexcept
Check if an axis-aligned bounding box is potentially intersecting with the frustum.
Definition geometry.hpp:1493
Array< Point< 3, T >, 8 > corners
Positions of the 8 corners of the frustum, in the following order:
Definition geometry.hpp:1465
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:1326
Array< vec< 4, T >, 6 > planes
Plane equation coefficients of the 6 planes of the frustum, pointing inwards, in the following order:
Definition geometry.hpp:1452
static constexpr Frustum fromViewProjectionMatrix(const mat< 4, 4, T > &viewProjectionMatrix) noexcept
Create a world-space view frustum from a combined view-projection matrix.
Definition geometry.hpp:1337
static constexpr Frustum fromCorners(StridedSpan< const Point< 3, T >, 8 > corners)
Create a frustum from its corners.
Definition geometry.hpp:1424
T Component
Scalar coordinate component type.
Definition geometry.hpp:1327
constexpr bool isPotentiallyIntersecting(const Sphere< 3, T > &sphere) const noexcept
Check if a sphere is potentially intersecting with the frustum.
Definition geometry.hpp:1517
Definition fundamentals.hpp:27
static constexpr T MAX
Definition fundamentals.hpp:29
static constexpr T MIN
Definition fundamentals.hpp:28
Generic line segment between two points in N-dimensional space.
Definition geometry.hpp:772
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the line segment.
Definition geometry.hpp:804
Point< N, T > pointB
Position of the second point of the line segment.
Definition geometry.hpp:777
Point< N, T > pointA
Position of the first point of the line segment.
Definition geometry.hpp:776
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:773
T Component
Scalar coordinate component type.
Definition geometry.hpp:774
bool operator==(const LineSegment &other) const =default
Compare this line segment to another for equality.
Result of an interor raycast hit.
Definition geometry.hpp:123
vec< N, T > localOffset
Offset from the object's center of mass in shape-local space at which the hit occured.
Definition geometry.hpp:124
Result of a raycast hit.
Definition geometry.hpp:110
vec< N, T > normal
Unit normal vector of the surface that was hit.
Definition geometry.hpp:113
vec< N, T > localOffset
Offset from the object's center of mass in shape-local space at which the hit occured.
Definition geometry.hpp:111
T distance
Distance from the ray origin, along the ray direction, at which the hit occured.
Definition geometry.hpp:112
Result of a raycast miss.
Definition geometry.hpp:101
Generic ray with an origin and direction in N-dimensional space.
Definition geometry.hpp:47
vec< N, T > direction
Unit vector pointing in the direction of the ray.
Definition geometry.hpp:81
Point< N, T > origin
Starting position of the ray.
Definition geometry.hpp:74
static constexpr Optional< Ray > between(Point< N, T > origin, Point< N, T > destination)
Create a ray between two points.
Definition geometry.hpp:61
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:48
T maxDistance
Maximum hit distance of the ray.
Definition geometry.hpp:88
T Component
Scalar coordinate component type.
Definition geometry.hpp:49
vec< N, T > directionInverse
Component-wise reciprocal of the ray direction.
Definition geometry.hpp:95
Result of a raycast.
Definition geometry.hpp:134
T Component
Scalar coordinate component type.
Definition geometry.hpp:136
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:135
Flat 2D axis-aligned rectangle shape with a position and size.
Definition geometry.hpp:297
constexpr Box< 2, T > toBox() const noexcept
Convert this shape to an equivalent axis-aligned bounding box.
Definition geometry.hpp:379
constexpr RaycastResult< 2, T > raycast(const Ray< 2, T > &ray) const noexcept
Find the closest intersection of this rectangle with a given ray.
Definition geometry.hpp:361
T Component
Scalar coordinate component type.
Definition geometry.hpp:299
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:298
constexpr Box< 2, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the rectangle.
Definition geometry.hpp:370
constexpr bool intersects(const Ray< 2, T > &ray) const noexcept
Check if this rectangle intersects a given ray.
Definition geometry.hpp:350
Point< 2, T > position
Position of the bottom left corner of the rectangle.
Definition geometry.hpp:301
constexpr bool contains(const Point< 2, T > &point) const noexcept
Check if a given point is contained within the extents of this rectangle.
Definition geometry.hpp:339
bool operator==(const Rectangle &other) const =default
Compare this rectangle to another for equality.
Length< 2, T > size
Width and height of the rectangle.
Definition geometry.hpp:302
Generic sphere shape with a center and radius in N-dimensional space.
Definition geometry.hpp:1018
constexpr Ellipsoid< N, T > toEllipsoid() const noexcept
Convert this shape to an equivalent ellipsoid.
Definition geometry.hpp:1148
bool operator==(const Sphere &other) const =default
Compare this sphere to another for equality.
constexpr bool intersects(const Ray< N, T > &ray) const noexcept
Check if this sphere intersects a given ray.
Definition geometry.hpp:1080
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:1019
constexpr RaycastResult< N, T > raycast(const Ray< N, T > &ray) const noexcept
Find the closest intersection of this sphere with a given ray.
Definition geometry.hpp:1105
T radius
Radius of the sphere.
Definition geometry.hpp:1023
constexpr bool contains(const Point< N, T > &point) const noexcept
Check if a given point is contained within the extents of this sphere.
Definition geometry.hpp:1069
Point< N, T > center
Position of the center of the sphere.
Definition geometry.hpp:1022
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the sphere.
Definition geometry.hpp:1136
constexpr Sphere< N, T > toSphere() const noexcept
Convert this shape to an equivalent sphere.
Definition geometry.hpp:1157
T Component
Scalar coordinate component type.
Definition geometry.hpp:1020
Flat 2D axis-aligned square shape with a position and width.
Definition geometry.hpp:390
constexpr bool contains(const Point< 2, T > &point) const noexcept
Check if a given point is contained within the extents of this square.
Definition geometry.hpp:441
constexpr RaycastResult< 2, T > raycast(const Ray< 2, T > &ray) const noexcept
Find the closest intersection of this square with a given ray.
Definition geometry.hpp:463
constexpr Box< 2, T > toBox() const noexcept
Convert this shape to an equivalent axis-aligned bounding box.
Definition geometry.hpp:481
bool operator==(const Square &other) const =default
Compare this square to another for equality.
Point< 2, T > position
Position of the bottom left corner of the square.
Definition geometry.hpp:394
T width
Width and height of the square.
Definition geometry.hpp:395
constexpr Box< 2, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the square.
Definition geometry.hpp:472
T Component
Scalar coordinate component type.
Definition geometry.hpp:392
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:391
constexpr bool intersects(const Ray< 2, T > &ray) const noexcept
Check if this square intersects a given ray.
Definition geometry.hpp:452
Generic triangle shape in N-dimensional space.
Definition geometry.hpp:586
constexpr bool contains(const Point< N, T > &point) const noexcept
Check if a given point is contained within the extents of this triangle.
Definition geometry.hpp:620
Point< N, T > pointB
Second point of the triangle.
Definition geometry.hpp:591
bool operator==(const Triangle &other) const =default
Compare this triangle to another for equality.
constexpr Box< N, T > getBoundingBox() const noexcept
Get the axis-aligned bounding box of the triangle.
Definition geometry.hpp:760
Point< N, T > pointC
Third point of the triangle.
Definition geometry.hpp:592
static constexpr size_t RANK
Number of dimensions of the vector space.
Definition geometry.hpp:587
constexpr bool intersects(const Ray< N, T > &ray) const noexcept
Check if this triangle intersects a given ray.
Definition geometry.hpp:633
constexpr RaycastResult< N, T > raycast(const Ray< N, T > &ray) const noexcept
Find the closest intersection of this triangle with a given ray.
Definition geometry.hpp:644
T Component
Scalar coordinate component type.
Definition geometry.hpp:588
Point< N, T > pointA
First point of the triangle.
Definition geometry.hpp:590
Definition math.hpp:663
Definition math.hpp:58