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);
35 std::ostream &operator <<(std::ostream &, const AABB &);
37 // TODO: this should really use setters/getters for dir and inv_dir so
38 // manipulating code doesn't "forget" to call Update()
45 void Update() noexcept {
49 /// get shortest distance of this ray's line to given point
50 float Distance(const glm::vec3 &point) const noexcept {
51 // d = |(x2-x1)×(x1-x0)|/|x2-x1|
52 // where x0 is point, and x1 and x2 are points on the line
53 // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
55 // x2-x1 = dir, which means |x2-x1| is 1.0
56 return glm::length(glm::cross(dir, orig - point));
58 float DistanceSquared(const glm::vec3 &point) const noexcept {
59 return glm::length2(glm::cross(dir, orig - point));
63 std::ostream &operator <<(std::ostream &, const Ray &);
65 /// axis aligned boolean ray/box intersection test
66 /// if true, dist constains distance from ray's origin to intersection point
70 float &dist) noexcept;
72 /// detailed oriented ray/box intersection test
77 float *dist = nullptr,
78 glm::vec3 *normal = nullptr) noexcept;
80 /// matrices may translate and rotate, but must not scale/shear/etc
81 /// (basically the first three columns must have unit length)
88 glm::vec3 &normal) noexcept;
90 /// Plane defined by a surface norml and distance to the origin such that
91 /// the point (normal * dist) lies on the plane.
96 float &A() noexcept { return normal.x; }
97 float &B() noexcept { return normal.y; }
98 float &C() noexcept { return normal.z; }
99 float &D() noexcept { return dist; }
100 float A() const noexcept { return normal.x; }
101 float B() const noexcept { return normal.y; }
102 float C() const noexcept { return normal.z; }
103 float D() const noexcept { return dist; }
105 Plane(const glm::vec3 &n, float d)
106 : normal(n), dist(d) { }
107 explicit Plane(const glm::vec4 &abcd)
108 : normal(abcd), dist(abcd.w) { }
110 void Normalize() noexcept {
111 const float l = glm::length(normal);
117 std::ostream &operator <<(std::ostream &, const Plane &);
119 /// Shortest distance from point to plane.
120 float Distance(const glm::vec3 &point, const Plane &plane);
121 /// Shortest distance from point to plane with sign indicating whether
122 /// it's in front of (positive, in direction of normal) or behind
123 /// (negative, counter direction of normal) the surface.
124 float SignedDistance(const glm::vec3 &point, const Plane &plane);
128 Plane &Left() noexcept { return plane[0]; }
129 Plane &Right() noexcept { return plane[1]; }
130 Plane &Bottom() noexcept { return plane[2]; }
131 Plane &Top() noexcept { return plane[3]; }
132 Plane &Near() noexcept { return plane[4]; }
133 Plane &Far() noexcept { return plane[5]; }
134 const Plane &Left() const noexcept { return plane[0]; }
135 const Plane &Right() const noexcept { return plane[1]; }
136 const Plane &Bottom() const noexcept { return plane[2]; }
137 const Plane &Top() const noexcept { return plane[3]; }
138 const Plane &Near() const noexcept { return plane[4]; }
139 const Plane &Far() const noexcept { return plane[5]; }
141 /// create frustum from transposed MVP
142 explicit Frustum(const glm::mat4 &mat)
144 Plane{ mat[3] + mat[0] },
145 Plane{ mat[3] - mat[0] },
146 Plane{ mat[3] + mat[1] },
147 Plane{ mat[3] - mat[1] },
148 Plane{ mat[3] + mat[2] },
149 Plane{ mat[3] - mat[2] },
152 void Normalize() noexcept {
153 for (Plane &p : plane) {
159 std::ostream &operator <<(std::ostream &, const Plane &);
160 std::ostream &operator <<(std::ostream &, const Frustum &);
162 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
163 bool CullTest(const AABB &box, const Frustum &) noexcept;
170 std::ostream &operator <<(std::ostream &, const Sphere &);
172 /// Test for intersection of sphere with double sided infinite plane.
173 /// If true, dist will hold the smaller interpenetration depth and norm
174 /// the respective contact normal.
176 const Sphere &sphere,
179 glm::vec3 &norm) noexcept;
181 /// Test for intersection of sphere with half space defined by the
182 /// backface of given plane.
183 /// In all cases, dist will hold the distance between the near points
184 /// of plane and sphere. Contact normal will always be the plane's normal.
186 const Sphere &sphere,
188 float &dist) noexcept;