1 #ifndef BLOBS_MATH_GEOMETRY_HPP_
2 #define BLOBS_MATH_GEOMETRY_HPP_
8 #include <glm/gtx/io.hpp>
19 void Adjust() noexcept {
20 if (max.x < min.x) std::swap(max.x, min.x);
21 if (max.y < min.y) std::swap(max.y, min.y);
22 if (max.z < min.z) std::swap(max.z, min.z);
25 glm::dvec3 Center() const noexcept {
26 return min + (max - min) * 0.5;
31 inline std::ostream &operator <<(std::ostream &out, const AABB &b) {
32 return out << "AABB(" << b.min << ", " << b.max << ")";
34 /// matrices must not scale
37 const glm::dmat4 &a_m,
39 const glm::dmat4 &b_m,
41 double &depth) noexcept;
46 Ray(const glm::dvec3 &orig, const glm::dvec3 &dir)
47 : orig(orig), dir(dir), inv_dir(1.0 / dir) { }
49 void Origin(const glm::dvec3 &o) noexcept { orig = o; }
50 const glm::dvec3 &Origin() const noexcept { return orig; }
51 void Direction(const glm::dvec3 &d) noexcept { dir = d; inv_dir = 1.0 / d; }
52 const glm::dvec3 &Direction() const noexcept { return dir; }
53 const glm::dvec3 &InverseDirection() const noexcept { return inv_dir; }
62 inline Ray operator *(const glm::dmat4 &m, const Ray &r) noexcept {
63 glm::dvec4 o(m * glm::dvec4(r.Origin(), 1.0));
64 glm::dvec4 d(m * glm::dvec4(r.Origin() + r.Direction(), 1.0));
65 return Ray(glm::dvec3(o) / o.w, glm::normalize((glm::dvec3(d) / d.w) - (glm::dvec3(o) / o.w)));
68 inline std::ostream &operator <<(std::ostream &out, const Ray &r) {
69 return out << "Ray(" << r.Origin() << ", " << r.Direction() << ")";
72 /// oriented ray/box intersection test
78 double &dist) noexcept;