]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/distance.hpp
glm backwards compatibility
[blank.git] / src / geometry / distance.hpp
index d4bec9b94f5dbdbc98fa8a84120a07d631efa964..91568082c3e06f8a001b0035840a8540f0857834 100644 (file)
@@ -1,36 +1,38 @@
 #ifndef BLANK_GEOMETRY_DISTANCE_HPP_
 #define BLANK_GEOMETRY_DISTANCE_HPP_
 
+#include "../graphics/glm.hpp"
+
 #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);
-}
-
 template <class T>
 inline bool iszero(const T &v) noexcept {
-       return length_squared(v) < std::numeric_limits<typename T::value_type>::epsilon();
+       return glm::length2(v) < std::numeric_limits<typename T::value_type>::epsilon();
+}
+
+template<class Vec>
+inline void limit(Vec &v, float max) noexcept {
+       float len2 = glm::length2(v);
+       float max2 = max * max;
+       if (len2 > max2) {
+               v = glm::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;
+template<class T, glm::precision P = glm::precision(0)>
+T manhattan_distance(const TVEC3<T, P> &a, const TVEC3<T, P> &b) noexcept {
+       return glm::compAdd(glm::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));
+template<class T, glm::precision P = glm::precision(0)>
+T manhattan_radius(const TVEC3<T, P> &v) noexcept {
+       return glm::compMax(glm::abs(v));
 }
 
 }