summaryrefslogtreecommitdiffhomepage
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
parent21341f788654cfc806778fa34d09885431083d76 (diff)
downloadderelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.zip
derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.gz
derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.bz2
Merged distance calculation functions
-rw-r--r--Reader.cpp2
-rw-r--r--Utils-inl.h24
-rw-r--r--Utils.h28
-rw-r--r--View.cpp6
4 files changed, 29 insertions, 31 deletions
diff --git a/Reader.cpp b/Reader.cpp
index f7c7ed6..07cb175 100644
--- a/Reader.cpp
+++ b/Reader.cpp
@@ -53,7 +53,7 @@ find_grid_for(std::vector<Grid>& grids, const LongVector3& position)
{
for (auto& grid : grids)
for (auto& wreck : grid.wrecks)
- if (dist3(position, wreck.killmail.position) < EXTENT)
+ if (dist(position, wreck.killmail.position) < EXTENT)
return grid;
grids.push_back(Grid{});
return grids.back();
diff --git a/Utils-inl.h b/Utils-inl.h
deleted file mode 100644
index 01e0ca6..0000000
--- a/Utils-inl.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#pragma once
-
-#include <cmath>
-
-
-template <typename V, typename T>
-T
-dist2(const V& lhs, const V& rhs)
-{
- return std::sqrt(
- std::pow(lhs.x - rhs.x, 2) +
- std::pow(lhs.y - rhs.y, 2));
-}
-
-
-template <typename V, typename T>
-T
-dist3(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));
-}
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));
+}
diff --git a/View.cpp b/View.cpp
index 8e8296d..93ad894 100644
--- a/View.cpp
+++ b/View.cpp
@@ -53,9 +53,9 @@ View::update(const float dt)
if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y)
if (0 > base.x || width < base.x || 0 > base.y || height < base.y)
continue;
- const auto depth = dist3(m_camera.position, wreck.position);
- const auto length = dist2(pos, base);
- const bool hover = 8.0f > dist2(mouse, pos);
+ const auto depth = dist(m_camera.position, wreck.position);
+ const auto length = dist(pos, base);
+ const bool hover = 8.0f > dist(mouse, pos);
m_labels.push_back(Label{pos, base, depth, length, hover});
}
std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });