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