summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-05-13 01:35:59 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2025-05-13 01:35:59 +0100
commitb30f760596f204cf35d9c233000eff51bd53eb8d (patch)
tree0acf62f6bf95b21111e30282a07d3135a9161e38
parentStandard interface for creating link definitions (diff)
downloadilt-b30f760596f204cf35d9c233000eff51bd53eb8d.tar.bz2
ilt-b30f760596f204cf35d9c233000eff51bd53eb8d.tar.xz
ilt-b30f760596f204cf35d9c233000eff51bd53eb8d.zip
constexpr, templated, lround vector rounding/addition/subtraction
-rw-r--r--lib/maths.h24
1 files changed, 18 insertions, 6 deletions
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::length_t D>
+// GLM round is not constexpr :( And we can use lround to convert at the same time
+template<glm::length_t D, std::floating_point T, glm::qualifier Q>
+[[nodiscard]] constexpr glm::vec<D, long, Q>
+lround(const glm::vec<D, T, Q> & value)
+{
+ glm::vec<D, long, Q> out;
+ for (glm::length_t axis = 0; axis < D; ++axis) {
+ out[axis] = std::lround(value[axis]);
+ }
+ return out;
+}
+
+template<glm::length_t D, std::floating_point T, glm::qualifier Q>
constexpr GlobalPosition<D>
-operator+(const GlobalPosition<D> & global, const RelativePosition<D> & relative)
+operator+(const GlobalPosition<D> & global, const glm::vec<D, T, Q> & relative)
{
- return global + GlobalPosition<D>(glm::round(relative));
+ return global + GlobalPosition<D> {lround(relative)};
}
template<glm::length_t D>
@@ -91,11 +103,11 @@ operator+(const GlobalPosition<D> & global, const CalcPosition<D> & relative)
return global + GlobalPosition<D>(relative);
}
-template<glm::length_t D>
+template<glm::length_t D, std::floating_point T, glm::qualifier Q>
constexpr GlobalPosition<D>
-operator-(const GlobalPosition<D> & global, const RelativePosition<D> & relative)
+operator-(const GlobalPosition<D> & global, const glm::vec<D, T, Q> & relative)
{
- return global - GlobalPosition<D>(glm::round(relative));
+ return global - GlobalPosition<D> {lround(relative)};
}
template<glm::length_t D>