From dc672a3488ec1d665fa898ced401e40ebc609bf8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Dec 2023 16:46:48 +0000 Subject: Templatise functions in maths.h using PositionND --- lib/maths.cpp | 72 --------------------------------------------- lib/maths.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++----- test/test-maths.cpp | 3 +- 3 files changed, 79 insertions(+), 80 deletions(-) diff --git a/lib/maths.cpp b/lib/maths.cpp index 17082d4..68662fc 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -3,7 +3,6 @@ #include #include #include -#include glm::mat4 flat_orientation(const Direction3D & diff) @@ -106,77 +105,6 @@ normalize(float ang) return ang; } -Arc::Arc(const Position3D & centre3, const Position3D & e0p, const Position3D & e1p) : - Arc([&]() -> Arc { - const auto diffa = e0p - centre3; - const auto diffb = e1p - centre3; - const auto anga = vector_yaw(diffa); - const auto angb = [&diffb, &anga]() { - const auto angb = vector_yaw(diffb); - return (angb < anga) ? angb + two_pi : angb; - }(); - return {anga, angb}; - }()) -{ -} - -std::pair -find_arc_centre(Position2D as, float entrys, Position2D bs, float entrye) -{ - if (as == bs) { - return {as, false}; - } - return find_arc_centre(as, sincosf(entrys + half_pi), bs, sincosf(entrye - half_pi)); -} - -std::pair -find_arc_centre(Position2D as, Position2D ad, Position2D bs, Position2D bd) -{ - const auto det = bd.x * ad.y - bd.y * ad.x; - if (det != 0) { // near parallel line will yield noisy results - const auto d = bs - as; - const auto u = (d.y * bd.x - d.x * bd.y) / det; - return {as + ad * u, u < 0}; - } - throw std::runtime_error("no intersection"); -} - -std::pair -find_arcs_radius(Position2D start, float entrys, Position2D end, float entrye) -{ - const auto getrad = [&](float leftOrRight) { - return find_arcs_radius(start, sincosf(entrys + leftOrRight), end, sincosf(entrye + leftOrRight)); - }; - return {getrad(-half_pi), getrad(half_pi)}; -} - -float -find_arcs_radius(Position2D start, Position2D ad, Position2D end, Position2D bd) -{ - // Short name functions for big forula - auto sqrt = [](float v) { - return std::sqrt(v); - }; - - // Calculates path across both arcs along the normals... pythagorean theorem... for some known radius r - // (2r)^2 = ((m + (X*r)) - (o + (Z*r)))^2 + ((n + (Y*r)) - (p + (W*r)))^2 - // According to symbolabs.com equation tool, that solves for r to give: - // r=(-2 m X+2 X o+2 m Z-2 o Z-2 n Y+2 Y p+2 n W-2 p W-sqrt((2 m X-2 X o-2 m Z+2 o Z+2 n Y-2 Y p-2 n W+2 p W)^(2)-4 - // (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y W+W^(2)-4) (m^(2)-2 m o+o^(2)+n^(2)-2 n p+p^(2))))/(2 (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y - // W+W^(2)-4)) - - // These exist cos limitations of online formula rearrangement, and I'm OK with that. - const auto &m {start.x}, &n {start.y}, &o {end.x}, &p {end.y}; - const auto &X {ad.x}, &Y {ad.y}, &Z {bd.x}, &W {bd.y}; - - return (2 * m * X - 2 * X * o - 2 * m * Z + 2 * o * Z + 2 * n * Y - 2 * Y * p - 2 * n * W + 2 * p * W - - sqrt(sq(-2 * m * X + 2 * X * o + 2 * m * Z - 2 * o * Z - 2 * n * Y + 2 * Y * p + 2 * n * W - - 2 * p * W) - - (4 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4) - * (sq(m) - 2 * m * o + sq(o) + sq(n) - 2 * n * p + sq(p))))) - / (2 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4)); -} - float operator"" _mph(const long double v) { diff --git a/lib/maths.h b/lib/maths.h index f7ff148..ba8c0e6 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -5,15 +5,17 @@ #include #include #include +#include #include struct Arc : public std::pair { using std::pair::pair; - Arc(const Position3D & centre3, const Position3D & e0p, const Position3D & e1p); + template + Arc(const glm::vec<3, T, Q> & centre3, const glm::vec<3, T, Q> & e0p, const glm::vec<3, T, Q> & e1p); - float - operator[](unsigned int i) const + auto + operator[](bool i) const { return i ? second : first; } @@ -136,10 +138,63 @@ arc_length(const Arc & arc) float normalize(float ang); -std::pair find_arc_centre(Position2D start, float entrys, Position2D end, float entrye); -std::pair find_arc_centre(Position2D start, Position2D ad, Position2D end, Position2D bd); -std::pair find_arcs_radius(Position2D start, float entrys, Position2D end, float entrye); -float find_arcs_radius(Position2D start, Position2D ad, Position2D end, Position2D bd); +template +std::pair, bool> +find_arc_centre(glm::vec<2, T, Q> start, Rotation2D startDir, glm::vec<2, T, Q> end, Rotation2D endDir) +{ + const auto det = endDir.x * startDir.y - endDir.y * startDir.x; + if (det != 0) { // near parallel line will yield noisy results + const auto d = end - start; + const auto u = (d.y * endDir.x - d.x * endDir.y) / det; + return {start + startDir * u, u < 0}; + } + throw std::runtime_error("no intersection"); +} + +template +std::pair, bool> +find_arc_centre(glm::vec<2, T, Q> start, Angle entrys, glm::vec<2, T, Q> end, Angle entrye) +{ + if (start == end) { + return {start, false}; + } + return find_arc_centre(start, sincosf(entrys + half_pi), end, sincosf(entrye - half_pi)); +} + +template +Angle +find_arcs_radius(glm::vec<2, T, Q> start, Rotation2D ad, glm::vec<2, T, Q> end, Rotation2D bd) +{ + using std::sqrt; + + // Calculates path across both arcs along the normals... pythagorean theorem... for some known radius r + // (2r)^2 = ((m + (X*r)) - (o + (Z*r)))^2 + ((n + (Y*r)) - (p + (W*r)))^2 + // According to symbolabs.com equation tool, that solves for r to give: + // r=(-2 m X+2 X o+2 m Z-2 o Z-2 n Y+2 Y p+2 n W-2 p W-sqrt((2 m X-2 X o-2 m Z+2 o Z+2 n Y-2 Y p-2 n W+2 p W)^(2)-4 + // (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y W+W^(2)-4) (m^(2)-2 m o+o^(2)+n^(2)-2 n p+p^(2))))/(2 (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y + // W+W^(2)-4)) + + // These exist cos limitations of online formula rearrangement, and I'm OK with that. + const auto &m {start.x}, &n {start.y}, &o {end.x}, &p {end.y}; + const auto &X {ad.x}, &Y {ad.y}, &Z {bd.x}, &W {bd.y}; + + return (2 * m * X - 2 * X * o - 2 * m * Z + 2 * o * Z + 2 * n * Y - 2 * Y * p - 2 * n * W + 2 * p * W + - sqrt(sq(-2 * m * X + 2 * X * o + 2 * m * Z - 2 * o * Z - 2 * n * Y + 2 * Y * p + 2 * n * W + - 2 * p * W) + - (4 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4) + * (sq(m) - 2 * m * o + sq(o) + sq(n) - 2 * n * p + sq(p))))) + / (2 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4)); +} + +template +std::pair +find_arcs_radius(glm::vec<2, T, Q> start, Angle entrys, glm::vec<2, T, Q> end, Angle entrye) +{ + const auto getrad = [&](auto leftOrRight) { + return find_arcs_radius(start, sincosf(entrys + leftOrRight), end, sincosf(entrye + leftOrRight)); + }; + return {getrad(-half_pi), getrad(half_pi)}; +} template auto @@ -148,6 +203,21 @@ midpoint(const std::pair & v) return std::midpoint(v.first, v.second); } +template +Arc::Arc(const glm::vec<3, T, Q> & centre3, const glm::vec<3, T, Q> & e0p, const glm::vec<3, T, Q> & e1p) : + Arc([&]() -> Arc { + const auto diffa = e0p - centre3; + const auto diffb = e1p - centre3; + const auto anga = vector_yaw(diffa); + const auto angb = [&diffb, &anga]() { + const auto angb = vector_yaw(diffb); + return (angb < anga) ? angb + two_pi : angb; + }(); + return {anga, angb}; + }()) +{ +} + // Conversions template inline constexpr auto diff --git a/test/test-maths.cpp b/test/test-maths.cpp index 3f8c2a0..ede884d 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -171,7 +171,8 @@ BOOST_DATA_TEST_CASE(test_find_arc_centre, BOOST_AUTO_TEST_CASE(test_find_arcs_radius) { - BOOST_CHECK_CLOSE(find_arcs_radius({10.32, 26.71}, {0.4, .92}, {2.92, 22.41}, {-0.89, -0.45}), 2.29, 1); + BOOST_CHECK_CLOSE( + find_arcs_radius(RelativePosition2D {10.32, 26.71}, {0.4, .92}, {2.92, 22.41}, {-0.89, -0.45}), 2.29, 1); } struct TestLinkStraight : public LinkStraight { -- cgit v1.2.3