]> git.localhorst.tv Git - blank.git/blob - src/geometry/distance.hpp
glm backwards compatibility
[blank.git] / src / geometry / distance.hpp
1 #ifndef BLANK_GEOMETRY_DISTANCE_HPP_
2 #define BLANK_GEOMETRY_DISTANCE_HPP_
3
4 #include "../graphics/glm.hpp"
5
6 #include <algorithm>
7 #include <limits>
8 #include <glm/gtx/component_wise.hpp>
9 #include <glm/gtx/norm.hpp>
10
11
12 namespace blank {
13
14 template <class T>
15 inline bool iszero(const T &v) noexcept {
16         return glm::length2(v) < std::numeric_limits<typename T::value_type>::epsilon();
17 }
18
19 template<class Vec>
20 inline void limit(Vec &v, float max) noexcept {
21         float len2 = glm::length2(v);
22         float max2 = max * max;
23         if (len2 > max2) {
24                 v = glm::normalize(v) * max;
25         }
26 }
27
28 template<class T, glm::precision P = glm::precision(0)>
29 T manhattan_distance(const TVEC3<T, P> &a, const TVEC3<T, P> &b) noexcept {
30         return glm::compAdd(glm::abs(a - b));
31 }
32
33 template<class T, glm::precision P = glm::precision(0)>
34 T manhattan_radius(const TVEC3<T, P> &v) noexcept {
35         return glm::compMax(glm::abs(v));
36 }
37
38 }
39
40 #endif