diff options
author | Aki <please@ignore.pl> | 2022-05-20 19:51:40 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-05-20 19:53:46 +0200 |
commit | d3720f01837e949ce7e7ea9b119358a5bb7b9666 (patch) | |
tree | c063f0f55d4cc58df23dc50d9b141a9237753c91 | |
parent | 21341f788654cfc806778fa34d09885431083d76 (diff) | |
download | derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.zip derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.gz derelict-d3720f01837e949ce7e7ea9b119358a5bb7b9666.tar.bz2 |
Merged distance calculation functions
-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; }); |