summaryrefslogtreecommitdiffhomepage
path: root/Utils.h
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-20 19:51:40 +0200
committerAki <please@ignore.pl>2022-05-20 19:53:46 +0200
commitd3720f01837e949ce7e7ea9b119358a5bb7b9666 (patch)
treec063f0f55d4cc58df23dc50d9b141a9237753c91 /Utils.h
parent21341f788654cfc806778fa34d09885431083d76 (diff)
downloadderelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.zip
derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.gz
derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.bz2
Merged distance calculation functions
Diffstat (limited to 'Utils.h')
-rw-r--r--Utils.h28
1 files changed, 25 insertions, 3 deletions
diff --git a/Utils.h b/Utils.h
index b582540..72eab4d 100644
--- a/Utils.h
+++ b/Utils.h
@@ -1,8 +1,30 @@
#pragma once
+#include <cmath>
+#include <type_traits>
-template <typename V, typename T=decltype(V::x)> T dist2(const V& lhs, const V& rhs);
-template <typename V, typename T=decltype(V::x)> T dist3(const V& lhs, const V& rhs);
+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)) {};
-#include "Utils-inl.h"
+
+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));
+}