]> git.localhorst.tv Git - blank.git/blob - src/geometry/distance.hpp
tentative optimization of chunk intersection test
[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
8
9 namespace blank {
10
11 inline float length_squared(const glm::vec3 &v) noexcept {
12         return dot(v, v);
13 }
14
15 inline float distance_squared(const glm::vec3 &a, const glm::vec3 &b) noexcept {
16         return length_squared(a - b);
17 }
18
19 inline float distance(const glm::vec3 &a, const glm::vec3 &b) noexcept {
20         return length(a - b);
21 }
22
23 template <class T>
24 inline bool iszero(const T &v) noexcept {
25         return length_squared(v) < std::numeric_limits<typename T::value_type>::epsilon();
26 }
27
28 template<class T>
29 T manhattan_distance(const glm::tvec3<T> &a, const glm::tvec3<T> &b) noexcept {
30         glm::tvec3<T> diff(abs(a - b));
31         return diff.x + diff.y + diff.z;
32 }
33
34 template<class T>
35 T manhattan_radius(const glm::tvec3<T> &v) noexcept {
36         glm::tvec3<T> a(abs(v));
37         return std::max(a.x, std::max(a.y, a.z));
38 }
39
40 }
41
42 #endif