]> git.localhorst.tv Git - gong.git/blob - src/geometry/distance.hpp
code, assets, and other stuff stolen from blank
[gong.git] / src / geometry / distance.hpp
1 #ifndef GONG_GEOMETRY_DISTANCE_HPP_
2 #define GONG_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 gong {
13 namespace geometry {
14
15 template <class T>
16 inline bool iszero(const T &v) noexcept {
17         return glm::length2(v) < std::numeric_limits<typename T::value_type>::epsilon();
18 }
19
20 template<class Vec>
21 inline void limit(Vec &v, float max) noexcept {
22         float len2 = glm::length2(v);
23         float max2 = max * max;
24         if (len2 > max2) {
25                 v = glm::normalize(v) * max;
26         }
27 }
28
29 template<class T, glm::precision P = glm::precision(0)>
30 T manhattan_distance(const TVEC3<T, P> &a, const TVEC3<T, P> &b) noexcept {
31         return glm::compAdd(glm::abs(a - b));
32 }
33
34 template<class T, glm::precision P = glm::precision(0)>
35 T manhattan_radius(const TVEC3<T, P> &v) noexcept {
36         return glm::compMax(glm::abs(v));
37 }
38
39 }
40 }
41
42 #endif