diff options
Diffstat (limited to 'VectorMath.h')
-rw-r--r-- | VectorMath.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/VectorMath.h b/VectorMath.h new file mode 100644 index 0000000..72eab4d --- /dev/null +++ b/VectorMath.h @@ -0,0 +1,30 @@ +#pragma once + +#include <cmath> +#include <type_traits> + + +template <typename T> static auto test_z(int) -> decltype(decltype(T::z){}, std::true_type{}); +template <typename> static auto test_z(...) -> std::false_type; +template <typename T> struct has_z : decltype(test_z<T>(0)) {}; + + +template <typename V, typename T=decltype(V::x), typename std::enable_if<has_z<V>::value, bool>::type=true> +T +dist(const V& lhs, const V& rhs) +{ + return std::sqrt( + std::pow(lhs.x - rhs.x, 2) + + std::pow(lhs.y - rhs.y, 2) + + std::pow(lhs.z - rhs.z, 2)); +} + + +template <typename V, typename T=decltype(V::x), typename std::enable_if<!has_z<V>::value, bool>::type=true> +T +dist(const V& lhs, const V& rhs) +{ + return std::sqrt( + std::pow(lhs.x - rhs.x, 2) + + std::pow(lhs.y - rhs.y, 2)); +} |