diff options
-rw-r--r-- | Reader.cpp | 2 | ||||
-rw-r--r-- | Utils-inl.h | 24 | ||||
-rw-r--r-- | Utils.h | 28 | ||||
-rw-r--r-- | View.cpp | 6 |
4 files changed, 29 insertions, 31 deletions
@@ -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)); -} @@ -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)); +} @@ -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; }); |