X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fprimitive.hpp;h=76064c1b13f8ce2e26ef26075e3e44b8dce9c750;hb=1b3b7203d0db35236108869961c77eaf31881d4b;hp=72da84749140ca6f2d6b2c71a069c3fd9c031199;hpb=ab0ba4313c473378b4516e3702d524dc1d1fc5d4;p=blank.git diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index 72da847..76064c1 100644 --- a/src/geometry/primitive.hpp +++ b/src/geometry/primitive.hpp @@ -1,9 +1,11 @@ #ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_ #define BLANK_GEOMETRY_PRIMITIVE_HPP_ +#include "../graphics/glm.hpp" + #include #include -#include +#include namespace blank { @@ -24,8 +26,8 @@ struct AABB { /// return distance between origin and farthest vertex float OriginRadius() const noexcept { - glm::vec3 high(glm::max(abs(min), abs(max))); - return length(high); + glm::vec3 high(glm::max(glm::abs(min), glm::abs(max))); + return glm::length(high); } }; @@ -42,6 +44,19 @@ struct Ray { void Update() noexcept { inv_dir = 1.0f / dir; } + + /// get shortest distance of this ray's line to given point + float Distance(const glm::vec3 &point) const noexcept { + // d = |(x2-x1)×(x1-x0)|/|x2-x1| + // where x0 is point, and x1 and x2 are points on the line + // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html + // x1 = orig + // x2-x1 = dir, which means |x2-x1| is 1.0 + return glm::length(glm::cross(dir, orig - point)); + } + float DistanceSquared(const glm::vec3 &point) const noexcept { + return glm::length2(glm::cross(dir, orig - point)); + } }; std::ostream &operator <<(std::ostream &, const Ray &); @@ -91,7 +106,7 @@ struct Plane { : normal(abcd), dist(abcd.w) { } void Normalize() noexcept { - const float l = length(normal); + const float l = glm::length(normal); normal /= l; dist /= l; }