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.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 7 deletions(-) (limited to 'lib/maths.h') 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 -- cgit v1.2.3