]> git.localhorst.tv Git - gong.git/blob - src/geometry/primitive.hpp
simple AABB intersection test
[gong.git] / src / geometry / primitive.hpp
1 #ifndef GONG_GEOMETRY_PRIMITIVE_HPP_
2 #define GONG_GEOMETRY_PRIMITIVE_HPP_
3
4 #include "../graphics/glm.hpp"
5
6 #include <algorithm>
7 #include <iosfwd>
8 #include <glm/gtx/norm.hpp>
9
10
11 namespace gong {
12 namespace geometry {
13
14 struct AABB {
15         glm::vec3 min;
16         glm::vec3 max;
17
18         void Adjust() noexcept {
19                 if (max.x < min.x) std::swap(max.x, min.x);
20                 if (max.y < min.y) std::swap(max.y, min.y);
21                 if (max.z < min.z) std::swap(max.z, min.z);
22         }
23
24         glm::vec3 Center() const noexcept {
25                 return min + (max - min) * 0.5f;
26         }
27
28         /// return distance between origin and farthest vertex
29         float OriginRadius() const noexcept {
30                 glm::vec3 high(glm::max(glm::abs(min), glm::abs(max)));
31                 return glm::length(high);
32         }
33
34         void Move(const glm::vec3 &delta) noexcept {
35                 min += delta;
36                 max += delta;
37         }
38 };
39
40 std::ostream &operator <<(std::ostream &, const AABB &);
41
42 bool Intersection(const AABB &, const AABB &) noexcept;
43
44 // TODO: this should really use setters/getters for dir and inv_dir so
45 //       manipulating code doesn't "forget" to call Update()
46 struct Ray {
47         glm::vec3 orig;
48         glm::vec3 dir;
49
50         glm::vec3 inv_dir;
51
52         void Update() noexcept {
53                 inv_dir = 1.0f / dir;
54         }
55
56         /// get shortest distance of this ray's line to given point
57         float Distance(const glm::vec3 &point) const noexcept {
58                 // d = |(x2-x1)×(x1-x0)|/|x2-x1|
59                 // where x0 is point, and x1 and x2 are points on the line
60                 // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
61                 // x1 = orig
62                 // x2-x1 = dir, which means |x2-x1| is 1.0
63                 return glm::length(glm::cross(dir, orig - point));
64         }
65         float DistanceSquared(const glm::vec3 &point) const noexcept {
66                 return glm::length2(glm::cross(dir, orig - point));
67         }
68 };
69
70 std::ostream &operator <<(std::ostream &, const Ray &);
71
72 /// axis aligned boolean ray/box intersection test
73 /// if true, dist constains distance from ray's origin to intersection point
74 bool Intersection(
75         const Ray &,
76         const AABB &,
77         float &dist) noexcept;
78
79 /// detailed oriented ray/box intersection test
80 bool Intersection(
81         const Ray &,
82         const AABB &,
83         const glm::mat4 &M,
84         float *dist = nullptr,
85         glm::vec3 *normal = nullptr) noexcept;
86
87 /// matrices may translate and rotate, but must not scale/shear/etc
88 /// (basically the first three columns must have unit length)
89 bool Intersection(
90         const AABB &a_box,
91         const glm::mat4 &a_m,
92         const AABB &b_box,
93         const glm::mat4 &b_m,
94         float &depth,
95         glm::vec3 &normal) noexcept;
96
97 /// Plane defined by a surface norml and distance to the origin such that
98 /// the point (normal * dist) lies on the plane.
99 struct Plane {
100         glm::vec3 normal;
101         float dist;
102
103         float &A() noexcept { return normal.x; }
104         float &B() noexcept { return normal.y; }
105         float &C() noexcept { return normal.z; }
106         float &D() noexcept { return dist; }
107         float A() const noexcept { return normal.x; }
108         float B() const noexcept { return normal.y; }
109         float C() const noexcept { return normal.z; }
110         float D() const noexcept { return dist; }
111
112         Plane(const glm::vec3 &n, float d)
113         : normal(n), dist(d) { }
114         explicit Plane(const glm::vec4 &abcd)
115         : normal(abcd), dist(abcd.w) { }
116
117         void Normalize() noexcept {
118                 const float l = glm::length(normal);
119                 normal /= l;
120                 dist /= l;
121         }
122 };
123
124 std::ostream &operator <<(std::ostream &, const Plane &);
125
126 /// Shortest distance from point to plane.
127 float Distance(const glm::vec3 &point, const Plane &plane);
128 /// Shortest distance from point to plane with sign indicating whether
129 /// it's in front of (positive, in direction of normal) or behind
130 /// (negative, counter direction of normal) the surface.
131 float SignedDistance(const glm::vec3 &point, const Plane &plane);
132
133 struct Frustum {
134         Plane plane[6];
135         Plane &Left() noexcept { return plane[0]; }
136         Plane &Right() noexcept { return plane[1]; }
137         Plane &Bottom() noexcept { return plane[2]; }
138         Plane &Top() noexcept { return plane[3]; }
139         Plane &Near() noexcept { return plane[4]; }
140         Plane &Far() noexcept { return plane[5]; }
141         const Plane &Left() const noexcept { return plane[0]; }
142         const Plane &Right() const noexcept { return plane[1]; }
143         const Plane &Bottom() const noexcept { return plane[2]; }
144         const Plane &Top() const noexcept { return plane[3]; }
145         const Plane &Near() const noexcept { return plane[4]; }
146         const Plane &Far() const noexcept { return plane[5]; }
147
148         /// create frustum from transposed MVP
149         explicit Frustum(const glm::mat4 &mat)
150         : plane{
151                 Plane{ mat[3] + mat[0] },
152                 Plane{ mat[3] - mat[0] },
153                 Plane{ mat[3] + mat[1] },
154                 Plane{ mat[3] - mat[1] },
155                 Plane{ mat[3] + mat[2] },
156                 Plane{ mat[3] - mat[2] },
157         } { }
158
159         void Normalize() noexcept {
160                 for (Plane &p : plane) {
161                         p.Normalize();
162                 }
163         }
164 };
165
166 std::ostream &operator <<(std::ostream &, const Plane &);
167 std::ostream &operator <<(std::ostream &, const Frustum &);
168
169 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
170 bool CullTest(const AABB &box, const Frustum &) noexcept;
171
172 struct Sphere {
173         glm::vec3 origin;
174         float radius;
175 };
176
177 std::ostream &operator <<(std::ostream &, const Sphere &);
178
179 /// Test for intersection of sphere with double sided infinite plane.
180 /// If true, dist will hold the smaller interpenetration depth and norm
181 /// the respective contact normal.
182 bool Intersection(
183         const Sphere &sphere,
184         const Plane &plane,
185         float &dist,
186         glm::vec3 &norm) noexcept;
187
188 /// Test for intersection of sphere with half space defined by the
189 /// backface of given plane.
190 /// In all cases, dist will hold the distance between the near points
191 /// of plane and sphere. Contact normal will always be the plane's normal.
192 bool Intersection(
193         const Sphere &sphere,
194         const Plane &plane,
195         float &dist) noexcept;
196
197 }
198 }
199
200 #endif