1 #ifndef GONG_GEOMETRY_PRIMITIVE_HPP_
2 #define GONG_GEOMETRY_PRIMITIVE_HPP_
4 #include "../graphics/glm.hpp"
8 #include <glm/gtx/norm.hpp>
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);
24 glm::vec3 Center() const noexcept {
25 return min + (max - min) * 0.5f;
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);
34 void Move(const glm::vec3 &delta) noexcept {
40 std::ostream &operator <<(std::ostream &, const AABB &);
42 bool Intersection(const AABB &, const AABB &) noexcept;
44 // TODO: this should really use setters/getters for dir and inv_dir so
45 // manipulating code doesn't "forget" to call Update()
52 void Update() noexcept {
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
62 // x2-x1 = dir, which means |x2-x1| is 1.0
63 return glm::length(glm::cross(dir, orig - point));
65 float DistanceSquared(const glm::vec3 &point) const noexcept {
66 return glm::length2(glm::cross(dir, orig - point));
70 std::ostream &operator <<(std::ostream &, const Ray &);
72 /// axis aligned boolean ray/box intersection test
73 /// if true, dist constains distance from ray's origin to intersection point
77 float &dist) noexcept;
79 /// detailed oriented ray/box intersection test
84 float *dist = nullptr,
85 glm::vec3 *normal = nullptr) noexcept;
87 /// matrices may translate and rotate, but must not scale/shear/etc
88 /// (basically the first three columns must have unit length)
95 glm::vec3 &normal) noexcept;
97 /// Plane defined by a surface norml and distance to the origin such that
98 /// the point (normal * dist) lies on the plane.
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; }
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) { }
117 void Normalize() noexcept {
118 const float l = glm::length(normal);
124 std::ostream &operator <<(std::ostream &, const Plane &);
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);
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]; }
148 /// create frustum from transposed MVP
149 explicit Frustum(const glm::mat4 &mat)
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] },
159 void Normalize() noexcept {
160 for (Plane &p : plane) {
166 std::ostream &operator <<(std::ostream &, const Plane &);
167 std::ostream &operator <<(std::ostream &, const Frustum &);
169 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
170 bool CullTest(const AABB &box, const Frustum &) noexcept;
177 std::ostream &operator <<(std::ostream &, const Sphere &);
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.
183 const Sphere &sphere,
186 glm::vec3 &norm) noexcept;
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.
193 const Sphere &sphere,
195 float &dist) noexcept;