X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fdistance.hpp;h=ef2f8a2fee5c570bd4dac293a8c190c0399b16cf;hb=7c9675c5678f94bfbdf96e55f2865c53bd42fe7e;hp=d4bec9b94f5dbdbc98fa8a84120a07d631efa964;hpb=4fbf5a3c1b0e530706023f5fc4be2f68d30ea645;p=blank.git diff --git a/src/geometry/distance.hpp b/src/geometry/distance.hpp index d4bec9b..ef2f8a2 100644 --- a/src/geometry/distance.hpp +++ b/src/geometry/distance.hpp @@ -4,33 +4,34 @@ #include #include #include +#include +#include namespace blank { -inline float length_squared(const glm::vec3 &v) noexcept { - return dot(v, v); -} - -inline float distance_squared(const glm::vec3 &a, const glm::vec3 &b) noexcept { - return length_squared(a - b); -} - template inline bool iszero(const T &v) noexcept { - return length_squared(v) < std::numeric_limits::epsilon(); + return length2(v) < std::numeric_limits::epsilon(); +} + +template +inline void limit(Vec &v, float max) noexcept { + float len2 = length2(v); + float max2 = max * max; + if (len2 > max2) { + v = normalize(v) * max; + } } template T manhattan_distance(const glm::tvec3 &a, const glm::tvec3 &b) noexcept { - glm::tvec3 diff(abs(a - b)); - return diff.x + diff.y + diff.z; + return compAdd(abs(a - b)); } template T manhattan_radius(const glm::tvec3 &v) noexcept { - glm::tvec3 a(abs(v)); - return std::max(a.x, std::max(a.y, a.z)); + return compMax(abs(v)); } }