From d3720f01837e949ce7e7ea9b119358a5bb7b9666 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 May 2022 19:51:40 +0200 Subject: Merged distance calculation functions --- Reader.cpp | 2 +- Utils-inl.h | 24 ------------------------ Utils.h | 28 +++++++++++++++++++++++++--- View.cpp | 6 +++--- 4 files changed, 29 insertions(+), 31 deletions(-) delete mode 100644 Utils-inl.h 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& 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 - - -template -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 -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 +#include -template T dist2(const V& lhs, const V& rhs); -template T dist3(const V& lhs, const V& rhs); +template static auto test_z(int) -> decltype(decltype(T::z){}, std::true_type{}); +template static auto test_z(...) -> std::false_type; +template struct has_z : decltype(test_z(0)) {}; -#include "Utils-inl.h" + +template ::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 ::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; }); -- cgit v1.1