From b30f760596f204cf35d9c233000eff51bd53eb8d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 13 May 2025 01:35:59 +0100 Subject: constexpr, templated, lround vector rounding/addition/subtraction --- lib/maths.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'lib/maths.h') diff --git a/lib/maths.h b/lib/maths.h index 2999d01..86f2e05 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -77,11 +77,23 @@ constexpr auto degreesToRads = pi / 180.F; constexpr auto earthMeanRadius = 6371.01F; // In km constexpr auto astronomicalUnit = 149597890.F; // In km -template +// GLM round is not constexpr :( And we can use lround to convert at the same time +template +[[nodiscard]] constexpr glm::vec +lround(const glm::vec & value) +{ + glm::vec out; + for (glm::length_t axis = 0; axis < D; ++axis) { + out[axis] = std::lround(value[axis]); + } + return out; +} + +template constexpr GlobalPosition -operator+(const GlobalPosition & global, const RelativePosition & relative) +operator+(const GlobalPosition & global, const glm::vec & relative) { - return global + GlobalPosition(glm::round(relative)); + return global + GlobalPosition {lround(relative)}; } template @@ -91,11 +103,11 @@ operator+(const GlobalPosition & global, const CalcPosition & relative) return global + GlobalPosition(relative); } -template +template constexpr GlobalPosition -operator-(const GlobalPosition & global, const RelativePosition & relative) +operator-(const GlobalPosition & global, const glm::vec & relative) { - return global - GlobalPosition(glm::round(relative)); + return global - GlobalPosition {lround(relative)}; } template -- cgit v1.2.3