From d6e99a696aa582b81018078b68f6600c8030d643 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 7 Nov 2023 02:51:42 +0000 Subject: WIP typedefing all the things - headers --- lib/geometricPlane.h | 10 +++++---- lib/location.h | 12 ++++++----- lib/maths.h | 61 ++++++++++++++++++++++++++-------------------------- lib/ray.h | 15 +++++++------ 4 files changed, 53 insertions(+), 45 deletions(-) (limited to 'lib') diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index dc8df50..c74beff 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include #include @@ -9,14 +10,15 @@ class GeometricPlane { public: struct DistAndPosition { float dist; - glm::vec3 position; + Position3D position; }; enum class PlaneRelation { Above, Below, On }; - glm::vec3 origin, normal; + Position3D origin; + Normal3D normal; - PlaneRelation getRelation(glm::vec3 point) const; - std::optional getRayIntersectPosition(const Ray &) const; + [[nodiscard]] PlaneRelation getRelation(Position3D point) const; + [[nodiscard]] std::optional getRayIntersectPosition(const Ray &) const; static bool isIntersect(PlaneRelation a, PlaneRelation b); }; diff --git a/lib/location.h b/lib/location.h index 078f5d3..7f2b44d 100644 --- a/lib/location.h +++ b/lib/location.h @@ -1,14 +1,16 @@ #pragma once +#include "config/types.h" #include -#include class Location { public: - explicit Location(glm::vec3 pos = {}, glm::vec3 rot = {}) : pos {pos}, rot {rot} { } +#ifndef __cpp_aggregate_paren_init + explicit Location(Position3D pos = {}, Rotation3D rot = {}) : pos {pos}, rot {rot} { } +#endif - glm::mat4 getTransform() const; + [[nodiscard]] glm::mat4 getTransform() const; - glm::vec3 pos; - glm::vec3 rot; + Position3D pos; + Rotation3D rot; }; diff --git a/lib/maths.h b/lib/maths.h index b95b706..67b2a15 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include #include #include @@ -9,7 +10,7 @@ struct Arc : public std::pair { using std::pair::pair; - Arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const glm::vec3 & e1p); + Arc(const Position3D & centre3, const Position3D & e0p, const Position3D & e1p); float operator[](unsigned int i) const @@ -18,19 +19,19 @@ struct Arc : public std::pair { } }; -constexpr const glm::vec3 origin {0, 0, 0}; -constexpr const glm::vec3 up {0, 0, 1}; -constexpr const glm::vec3 down {0, 0, -1}; -constexpr const glm::vec3 north {0, 1, 0}; -constexpr const glm::vec3 south {0, -1, 0}; -constexpr const glm::vec3 east {1, 0, 0}; -constexpr const glm::vec3 west {-1, 0, 0}; +constexpr const Position3D origin {0, 0, 0}; +constexpr const Position3D up {0, 0, 1}; +constexpr const Position3D down {0, 0, -1}; +constexpr const Position3D north {0, 1, 0}; +constexpr const Position3D south {0, -1, 0}; +constexpr const Position3D east {1, 0, 0}; +constexpr const Position3D west {-1, 0, 0}; constexpr auto half_pi {glm::half_pi()}; constexpr auto quarter_pi {half_pi / 2}; constexpr auto pi {glm::pi()}; constexpr auto two_pi {glm::two_pi()}; -glm::mat4 flat_orientation(const glm::vec3 & diff); +glm::mat4 flat_orientation(const Rotation3D & diff); // C++ wrapper for C's sincosf, but with references, not pointers inline auto @@ -39,10 +40,10 @@ sincosf(float a, float & s, float & c) return sincosf(a, &s, &c); } -inline glm::vec2 +inline Rotation2D sincosf(float a) { - glm::vec2 sc; + Rotation2D sc; sincosf(a, sc.x, sc.y); return sc; } @@ -51,11 +52,11 @@ glm::mat2 rotate_flat(float); glm::mat4 rotate_roll(float); glm::mat4 rotate_yaw(float); glm::mat4 rotate_pitch(float); -glm::mat4 rotate_yp(glm::vec2); -glm::mat4 rotate_ypr(glm::vec3); +glm::mat4 rotate_yp(Rotation2D); +glm::mat4 rotate_ypr(Rotation3D); -float vector_yaw(const glm::vec3 & diff); -float vector_pitch(const glm::vec3 & diff); +float vector_yaw(const Direction3D & diff); +float vector_pitch(const Direction3D & diff); float round_frac(const float & v, const float & frac); @@ -87,26 +88,26 @@ perspective_divide(glm::vec<4, T, Q> v) return v / v.w; } -constexpr inline glm::vec2 -operator!(const glm::vec3 & v) +constexpr inline Position2D +operator!(const Position3D & v) { return {v.x, v.y}; } -constexpr inline glm::vec3 -operator^(const glm::vec2 & v, float z) +constexpr inline Position3D +operator^(const Position2D & v, float z) { return {v.x, v.y, z}; } constexpr inline glm::vec4 -operator^(const glm::vec3 & v, float w) +operator^(const Position3D & v, float w) { return {v.x, v.y, v.z, w}; } -constexpr inline glm::vec3 -operator!(const glm::vec2 & v) +constexpr inline Position3D +operator!(const Position2D & v) { return v ^ 0.F; } @@ -125,15 +126,15 @@ operator||(const glm::vec v1, const T v2) return {v1, v2}; } -inline glm::vec3 -operator%(const glm::vec3 & p, const glm::mat4 & mutation) +inline Position3D +operator%(const Position3D & p, const glm::mat4 & mutation) { const auto p2 = mutation * (p ^ 1); return p2 / p2.w; } -inline glm::vec3 -operator%=(glm::vec3 & p, const glm::mat4 & mutation) +inline Position3D +operator%=(Position3D & p, const glm::mat4 & mutation) { return p = p % mutation; } @@ -146,10 +147,10 @@ arc_length(const Arc & arc) float normalize(float ang); -std::pair find_arc_centre(glm::vec2 start, float entrys, glm::vec2 end, float entrye); -std::pair find_arc_centre(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd); -std::pair find_arcs_radius(glm::vec2 start, float entrys, glm::vec2 end, float entrye); -float find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd); +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 auto diff --git a/lib/ray.h b/lib/ray.h index 9bf47af..bc70c74 100644 --- a/lib/ray.h +++ b/lib/ray.h @@ -1,17 +1,20 @@ #pragma once +#include "config/types.h" #include #include class Ray { public: - Ray(glm::vec3 start, glm::vec3 direction) : start {start}, direction {direction} { } +#ifndef __cpp_aggregate_paren_init + Ray(Position3D start, Direction3D direction) : start {start}, direction {direction} { } +#endif - static Ray fromPoints(glm::vec3, glm::vec3); + static Ray fromPoints(Position3D, Position3D); - glm::vec3 start; - glm::vec3 direction; + Position3D start; + Direction3D direction; - float distanceToLine(const glm::vec3 & a, const glm::vec3 & b) const; - bool passesCloseToEdges(const std::span positions, float distance) const; + [[nodiscard]] float distanceToLine(const Position3D & a, const Position3D & b) const; + [[nodiscard]] bool passesCloseToEdges(const std::span positions, float distance) const; }; -- cgit v1.2.3 From 9c2c3f71065c94a18c02440111b6ff8ca977b90e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 9 Nov 2023 00:40:40 +0000 Subject: WIP typedefing all the things - sources --- lib/geometricPlane.cpp | 4 ++-- lib/maths.cpp | 40 ++++++++++++++++++++-------------------- lib/ray.cpp | 8 ++++---- 3 files changed, 26 insertions(+), 26 deletions(-) (limited to 'lib') diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp index ea4f02d..567f98a 100644 --- a/lib/geometricPlane.cpp +++ b/lib/geometricPlane.cpp @@ -4,10 +4,10 @@ #include GeometricPlane::PlaneRelation -GeometricPlane::getRelation(glm::vec3 p) const +GeometricPlane::getRelation(Position3D p) const { const auto d = glm::dot(normal, p - origin); - return d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On; + return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On; } bool diff --git a/lib/maths.cpp b/lib/maths.cpp index 7594b59..5430ef6 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -6,7 +6,7 @@ #include glm::mat4 -flat_orientation(const glm::vec3 & diff) +flat_orientation(const Direction3D & diff) { static const auto oneeighty {glm::rotate(pi, up)}; const auto flatdiff {glm::normalize(!!diff)}; @@ -16,17 +16,17 @@ flat_orientation(const glm::vec3 & diff) } // Helper to lookup into a matrix given an xy vector coordinate -template +template inline auto & -operator^(M & m, glm::ivec2 xy) +operator^(M & m, glm::vec<2, I> xy) { return m[xy.x][xy.y]; } // Create a matrix for the angle, given the targets into the matrix -template +template inline auto -rotation(typename M::value_type a, glm::ivec2 c1, glm::ivec2 s1, glm::ivec2 c2, glm::ivec2 ms2) +rotation(typename M::value_type a, glm::vec<2, I> c1, glm::vec<2, I> s1, glm::vec<2, I> c2, glm::vec<2, I> ms2) { M m(1); sincosf(a, m ^ s1, m ^ c1); @@ -39,51 +39,51 @@ rotation(typename M::value_type a, glm::ivec2 c1, glm::ivec2 s1, glm::ivec2 c2, glm::mat2 rotate_flat(float a) { - return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); + return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); } // Create a yaw transformation matrix glm::mat4 rotate_yaw(float a) { - return rotation(a, {0, 0}, {1, 0}, {1, 1}, {0, 1}); + return rotation(a, {0, 0}, {1, 0}, {1, 1}, {0, 1}); } // Create a roll transformation matrix glm::mat4 rotate_roll(float a) { - return rotation(a, {0, 0}, {2, 0}, {2, 2}, {0, 2}); + return rotation(a, {0, 0}, {2, 0}, {2, 2}, {0, 2}); } // Create a pitch transformation matrix glm::mat4 rotate_pitch(float a) { - return rotation(a, {1, 1}, {1, 2}, {2, 2}, {2, 1}); + return rotation(a, {1, 1}, {1, 2}, {2, 2}, {2, 1}); } // Create a combined yaw, pitch, roll transformation matrix glm::mat4 -rotate_ypr(glm::vec3 a) +rotate_ypr(Rotation3D a) { return rotate_yaw(a.y) * rotate_pitch(a.x) * rotate_roll(a.z); } glm::mat4 -rotate_yp(glm::vec2 a) +rotate_yp(Rotation2D a) { return rotate_yaw(a.y) * rotate_pitch(a.x); } float -vector_yaw(const glm::vec3 & diff) +vector_yaw(const Direction3D & diff) { return std::atan2(diff.x, diff.y); } float -vector_pitch(const glm::vec3 & diff) +vector_pitch(const Direction3D & diff) { return std::atan(diff.z); } @@ -106,7 +106,7 @@ normalize(float ang) return ang; } -Arc::Arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const glm::vec3 & e1p) : +Arc::Arc(const Position3D & centre3, const Position3D & e0p, const Position3D & e1p) : Arc([&]() -> Arc { const auto diffa = e0p - centre3; const auto diffb = e1p - centre3; @@ -120,8 +120,8 @@ Arc::Arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const glm::vec3 & e1p { } -std::pair -find_arc_centre(glm::vec2 as, float entrys, glm::vec2 bs, float entrye) +std::pair +find_arc_centre(Position2D as, float entrys, Position2D bs, float entrye) { if (as == bs) { return {as, false}; @@ -129,8 +129,8 @@ find_arc_centre(glm::vec2 as, float entrys, glm::vec2 bs, float entrye) return find_arc_centre(as, sincosf(entrys + half_pi), bs, sincosf(entrye - half_pi)); } -std::pair -find_arc_centre(glm::vec2 as, glm::vec2 ad, glm::vec2 bs, glm::vec2 bd) +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 @@ -142,7 +142,7 @@ find_arc_centre(glm::vec2 as, glm::vec2 ad, glm::vec2 bs, glm::vec2 bd) } std::pair -find_arcs_radius(glm::vec2 start, float entrys, glm::vec2 end, float entrye) +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)); @@ -151,7 +151,7 @@ find_arcs_radius(glm::vec2 start, float entrys, glm::vec2 end, float entrye) } float -find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd) +find_arcs_radius(Position2D start, Position2D ad, Position2D end, Position2D bd) { // Short name functions for big forula auto sqrt = [](float v) { diff --git a/lib/ray.cpp b/lib/ray.cpp index c4e0d8c..9fb3648 100644 --- a/lib/ray.cpp +++ b/lib/ray.cpp @@ -2,13 +2,13 @@ #include Ray -Ray::fromPoints(glm::vec3 start, glm::vec3 p) +Ray::fromPoints(Position3D start, Position3D p) { return {start, glm::normalize(p - start)}; } float -Ray::distanceToLine(const glm::vec3 & p1, const glm::vec3 & e1) const +Ray::distanceToLine(const Position3D & p1, const Position3D & e1) const { // https://en.wikipedia.org/wiki/Skew_lines const auto diff = p1 - e1; @@ -25,10 +25,10 @@ Ray::distanceToLine(const glm::vec3 & p1, const glm::vec3 & e1) const } bool -Ray::passesCloseToEdges(const std::span positions, float distance) const +Ray::passesCloseToEdges(const std::span positions, float distance) const { return std::adjacent_find(positions.begin(), positions.end(), - [this, distance](const glm::vec3 & a, const glm::vec3 & b) { + [this, distance](const Position3D & a, const Position3D & b) { return distanceToLine(a, b) <= distance; }) != positions.end(); -- cgit v1.2.3 From 5bc0462311ab4c691102f0bc39d6ae03c61a287b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 11 Nov 2023 17:32:05 +0000 Subject: Add Location method for getting the rotation only transform --- lib/location.cpp | 6 ++++++ lib/location.h | 1 + 2 files changed, 7 insertions(+) (limited to 'lib') diff --git a/lib/location.cpp b/lib/location.cpp index 732dd6d..9a31402 100644 --- a/lib/location.cpp +++ b/lib/location.cpp @@ -7,3 +7,9 @@ Location::getTransform() const { return glm::translate(pos) * rotate_ypr(rot); } + +glm::mat4 +Location::getRotationTransform() const +{ + return rotate_ypr(rot); +} diff --git a/lib/location.h b/lib/location.h index 7f2b44d..55737ae 100644 --- a/lib/location.h +++ b/lib/location.h @@ -10,6 +10,7 @@ public: #endif [[nodiscard]] glm::mat4 getTransform() const; + [[nodiscard]] glm::mat4 getRotationTransform() const; Position3D pos; Rotation3D rot; -- cgit v1.2.3 From 3200eb41d60595813dca751fe15193ba0b44dddf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Nov 2023 14:32:56 +0000 Subject: Remove getTransform --- lib/location.cpp | 6 ------ lib/location.h | 1 - 2 files changed, 7 deletions(-) (limited to 'lib') diff --git a/lib/location.cpp b/lib/location.cpp index 9a31402..ff7cfa6 100644 --- a/lib/location.cpp +++ b/lib/location.cpp @@ -2,12 +2,6 @@ #include "maths.h" #include -glm::mat4 -Location::getTransform() const -{ - return glm::translate(pos) * rotate_ypr(rot); -} - glm::mat4 Location::getRotationTransform() const { diff --git a/lib/location.h b/lib/location.h index 55737ae..85834a0 100644 --- a/lib/location.h +++ b/lib/location.h @@ -9,7 +9,6 @@ public: explicit Location(Position3D pos = {}, Rotation3D rot = {}) : pos {pos}, rot {rot} { } #endif - [[nodiscard]] glm::mat4 getTransform() const; [[nodiscard]] glm::mat4 getRotationTransform() const; Position3D pos; -- cgit v1.2.3 From 0aa665c3648d788755b00c9e431c872d57fddbb8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Nov 2023 16:28:39 +0000 Subject: Model positions as integers Introduces test failure in arcs due to rounding, but I don't want to create a complicated fix as link positions are still floats and hopefully that'll go away... somehow --- lib/location.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/location.h b/lib/location.h index 85834a0..8570fc2 100644 --- a/lib/location.h +++ b/lib/location.h @@ -6,11 +6,11 @@ class Location { public: #ifndef __cpp_aggregate_paren_init - explicit Location(Position3D pos = {}, Rotation3D rot = {}) : pos {pos}, rot {rot} { } + explicit Location(GlobalPosition3D pos = {}, Rotation3D rot = {}) : pos {pos}, rot {rot} { } #endif [[nodiscard]] glm::mat4 getRotationTransform() const; - Position3D pos; + GlobalPosition3D pos; Rotation3D rot; }; -- cgit v1.2.3 From 0a5d23ce263551bab3759e4ac65ecb6cfebdc892 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 Dec 2023 12:23:52 +0000 Subject: Add crossInt - cross product for integer vectors --- lib/maths.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index 67b2a15..cf369a7 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -67,6 +67,17 @@ sq(T v) return v * v; } +template +inline constexpr glm::vec<3, T, Q> +crossInt(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +{ + return { + (a.y * b.z) - (a.z * b.y), + (a.z * b.x) - (a.x * b.z), + (a.x * b.y) - (a.y * b.x), + }; +} + template inline constexpr auto ratio(Ta a, Tb b) -- cgit v1.2.3 From 0841ead91c49a212134f19f7c0b411984b0fda29 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Dec 2023 13:20:55 +0000 Subject: Remove weird operator! on vec2/3 --- lib/maths.cpp | 2 +- lib/maths.h | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/maths.cpp b/lib/maths.cpp index 5430ef6..0c25820 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -9,7 +9,7 @@ glm::mat4 flat_orientation(const Direction3D & diff) { static const auto oneeighty {glm::rotate(pi, up)}; - const auto flatdiff {glm::normalize(!!diff)}; + const auto flatdiff {glm::normalize(diff.xy() ^ 0.F)}; auto e {glm::orientation(flatdiff, north)}; // Handle if diff is exactly opposite to north return (std::isnan(e[0][0])) ? oneeighty : e; diff --git a/lib/maths.h b/lib/maths.h index cf369a7..dd83c4b 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -99,12 +99,6 @@ perspective_divide(glm::vec<4, T, Q> v) return v / v.w; } -constexpr inline Position2D -operator!(const Position3D & v) -{ - return {v.x, v.y}; -} - constexpr inline Position3D operator^(const Position2D & v, float z) { @@ -117,12 +111,6 @@ operator^(const Position3D & v, float w) return {v.x, v.y, v.z, w}; } -constexpr inline Position3D -operator!(const Position2D & v) -{ - return v ^ 0.F; -} - template inline constexpr glm::vec operator||(const glm::vec v1, const glm::vec v2) -- cgit v1.2.3 From 048f18e2a0b32044525cef41fa053984433c74b9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Dec 2023 14:12:40 +0000 Subject: Remove misleading power operator^ on vec2/3 --- lib/maths.cpp | 2 +- lib/maths.h | 24 +++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/maths.cpp b/lib/maths.cpp index 0c25820..17082d4 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -9,7 +9,7 @@ glm::mat4 flat_orientation(const Direction3D & diff) { static const auto oneeighty {glm::rotate(pi, up)}; - const auto flatdiff {glm::normalize(diff.xy() ^ 0.F)}; + const auto flatdiff {glm::normalize(diff.xy() || 0.F)}; auto e {glm::orientation(flatdiff, north)}; // Handle if diff is exactly opposite to north return (std::isnan(e[0][0])) ? oneeighty : e; diff --git a/lib/maths.h b/lib/maths.h index dd83c4b..f7ff148 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -99,18 +99,6 @@ perspective_divide(glm::vec<4, T, Q> v) return v / v.w; } -constexpr inline Position3D -operator^(const Position2D & v, float z) -{ - return {v.x, v.y, z}; -} - -constexpr inline glm::vec4 -operator^(const Position3D & v, float w) -{ - return {v.x, v.y, v.z, w}; -} - template inline constexpr glm::vec operator||(const glm::vec v1, const glm::vec v2) @@ -125,15 +113,17 @@ operator||(const glm::vec v1, const T v2) return {v1, v2}; } -inline Position3D -operator%(const Position3D & p, const glm::mat4 & mutation) +template +inline constexpr glm::vec +operator%(const glm::vec & p, const glm::mat & mutation) { - const auto p2 = mutation * (p ^ 1); + const auto p2 = mutation * (p || T(1)); return p2 / p2.w; } -inline Position3D -operator%=(Position3D & p, const glm::mat4 & mutation) +template +inline constexpr glm::vec +operator%=(glm::vec & p, const glm::mat & mutation) { return p = p % mutation; } -- cgit v1.2.3 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 79 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3 From 69e6b7d2d349dcc42d2d415a72181ba729d5a19d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 1 Jan 2024 15:53:54 +0000 Subject: Remove more use of legacy types --- lib/maths.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index ba8c0e6..c1bf61a 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -21,13 +21,12 @@ struct Arc : public std::pair { } }; -constexpr const Position3D origin {0, 0, 0}; -constexpr const Position3D up {0, 0, 1}; -constexpr const Position3D down {0, 0, -1}; -constexpr const Position3D north {0, 1, 0}; -constexpr const Position3D south {0, -1, 0}; -constexpr const Position3D east {1, 0, 0}; -constexpr const Position3D west {-1, 0, 0}; +constexpr const RelativePosition3D up {0, 0, 1}; +constexpr const RelativePosition3D down {0, 0, -1}; +constexpr const RelativePosition3D north {0, 1, 0}; +constexpr const RelativePosition3D south {0, -1, 0}; +constexpr const RelativePosition3D east {1, 0, 0}; +constexpr const RelativePosition3D west {-1, 0, 0}; constexpr auto half_pi {glm::half_pi()}; constexpr auto quarter_pi {half_pi / 2}; constexpr auto pi {glm::pi()}; -- cgit v1.2.3