]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/distance.hpp
reorder world update
[blank.git] / src / geometry / distance.hpp
index b53fcd7c679c7630051ba7911ecdd35818161b48..ef2f8a2fee5c570bd4dac293a8c190c0399b16cf 100644 (file)
@@ -4,37 +4,34 @@
 #include <algorithm>
 #include <limits>
 #include <glm/glm.hpp>
+#include <glm/gtx/component_wise.hpp>
+#include <glm/gtx/norm.hpp>
 
 
 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);
-}
-
-inline float distance(const glm::vec3 &a, const glm::vec3 &b) noexcept {
-       return length(a - b);
-}
-
 template <class T>
 inline bool iszero(const T &v) noexcept {
-       return length_squared(v) < std::numeric_limits<typename T::value_type>::epsilon();
+       return length2(v) < std::numeric_limits<typename T::value_type>::epsilon();
+}
+
+template<class Vec>
+inline void limit(Vec &v, float max) noexcept {
+       float len2 = length2(v);
+       float max2 = max * max;
+       if (len2 > max2) {
+               v = normalize(v) * max;
+       }
 }
 
 template<class T>
 T manhattan_distance(const glm::tvec3<T> &a, const glm::tvec3<T> &b) noexcept {
-       glm::tvec3<T> diff(abs(a - b));
-       return diff.x + diff.y + diff.z;
+       return compAdd(abs(a - b));
 }
 
 template<class T>
 T manhattan_radius(const glm::tvec3<T> &v) noexcept {
-       glm::tvec3<T> a(abs(v));
-       return std::max(a.x, std::max(a.y, a.z));
+       return compMax(abs(v));
 }
 
 }