From ca276ca5471a4e7a137f68a81feb150282eae62f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 7 Feb 2024 23:50:50 +0000 Subject: Add stripiter A generic iterator wrapper returning a tuple of 3 references to the original values, as processed in the fashion of an OpenGL triangle strip. --- lib/collections.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'lib') diff --git a/lib/collections.h b/lib/collections.h index 943b986..6cee10c 100644 --- a/lib/collections.h +++ b/lib/collections.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -160,3 +161,62 @@ template struct pair_range { }; template pair_range(std::pair) -> pair_range; + +template struct stripiter { + [[nodiscard]] constexpr bool + operator!=(const stripiter & other) const + { + return current != other.current; + } + + [[nodiscard]] constexpr bool + operator==(const stripiter & other) const + { + return current == other.current; + } + + constexpr stripiter & + operator++() + { + ++current; + off = 1 - off; + return *this; + } + + constexpr stripiter & + operator--() + { + --current; + off = 1 - off; + return *this; + } + + constexpr auto + operator-(const stripiter & other) const + { + return current - other.current; + } + + constexpr auto + operator*() const + { + return std::tie(*(current - (2 - off)), *(current - off - 1), *current); + } + + iter current; + uint8_t off {}; +}; + +template struct std::iterator_traits> : std::iterator_traits { }; + +constexpr auto +strip_begin(IterableCollection auto & cont) +{ + return stripiter {cont.begin() + 2}; +} + +constexpr auto +strip_end(IterableCollection auto & cont) +{ + return stripiter {cont.end()}; +} -- cgit v1.2.3 From fa48542d7387ba376b56dc4f60f90fccbd037f92 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 12 Feb 2024 20:28:50 +0000 Subject: Cast relative position accordingly in geometric plane --- lib/geometricPlane.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index 3f95d3c..3b1a0e4 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -27,7 +27,7 @@ public: [[nodiscard]] inline PlaneRelation getRelation(PositionType point) const { - const auto d = glm::dot(normal, point - origin); + const auto d = glm::dot(normal, RelativePosition3D(point - origin)); return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On; } -- cgit v1.2.3 From 40f5a59dba7d5061821e143cebcfa30f6faa9464 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 12 Feb 2024 20:32:01 +0000 Subject: Add materializeRange override for naked iterator pair --- lib/collections.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/collections.h b/lib/collections.h index 6cee10c..dd603be 100644 --- a/lib/collections.h +++ b/lib/collections.h @@ -130,18 +130,25 @@ vectorOfN(std::integral auto N, T start = {}, T step = 1) return v; } +template typename Rtn = std::vector, typename In> +[[nodiscard]] auto +materializeRange(const In begin, const In end) +{ + return Rtn(begin, end); +} + template typename Rtn = std::vector, IterableCollection In> [[nodiscard]] auto -materializeRange(In && in) +materializeRange(const In & in) { - return Rtn(in.begin(), in.end()); + return materializeRange(in.begin(), in.end()); } template typename Rtn = std::vector, typename In> [[nodiscard]] auto materializeRange(const std::pair & in) { - return Rtn(in.first, in.second); + return materializeRange(in.first, in.second); } template struct pair_range { -- cgit v1.2.3 From 861c046ed527474a18a1358246a818ce70bb0b75 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 14 Feb 2024 02:07:04 +0000 Subject: Check ray intersects triangle 'in front' of start --- lib/ray.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/ray.h b/lib/ray.h index e1f43c3..67fdd12 100644 --- a/lib/ray.h +++ b/lib/ray.h @@ -47,11 +47,11 @@ public: RelativeDistance & distance) const { if constexpr (std::is_floating_point_v) { - return glm::intersectRayTriangle(start, direction, t0, t1, t2, bary, distance); + return glm::intersectRayTriangle(start, direction, t0, t1, t2, bary, distance) && distance >= 0.F; } else { const RelativePosition3D t0r = t0 - start, t1r = t1 - start, t2r = t2 - start; - return glm::intersectRayTriangle({}, direction, t0r, t1r, t2r, bary, distance); + return glm::intersectRayTriangle({}, direction, t0r, t1r, t2r, bary, distance) && distance >= 0.F; } } -- cgit v1.2.3 From 52fe8fddaf36fe3c1472fa04c1bed0da848770ac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 26 Feb 2024 22:18:48 +0000 Subject: Support extra arguments in SelectionV::make_s --- lib/persistence.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/persistence.h b/lib/persistence.h index c53ff99..75f578e 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -81,11 +81,11 @@ namespace Persistence { return make_s>(value); } - template + template [[nodiscard]] static SelectionPtr - make_s(T & value) + make_s(T & value, Extra &&... extra) { - return std::make_unique(value); + return std::make_unique(value, std::forward(extra)...); } T & v; -- cgit v1.2.3 From 1511e3f3faadc61b462577ccc9c740157982b867 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 26 Feb 2024 22:26:10 +0000 Subject: Add read persistence support for tuple and pair --- lib/persistence.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'lib') diff --git a/lib/persistence.h b/lib/persistence.h index 75f578e..3c95a00 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -331,6 +331,65 @@ namespace Persistence { } }; + template struct SelectionT> : public SelectionV> { + using V = std::tuple; + using SelectionV::SelectionV; + + struct Members : public SelectionV { + template + explicit Members(V & v, std::integer_sequence) : + SelectionV {v}, members {SelectionV>::make(std::get(v))...} + { + } + + void + beforeValue(Stack & stk) override + { + stk.push(std::move(members[idx++])); + } + + std::size_t idx {0}; + std::array> members; + }; + + void + beginArray(Stack & stk) override + { + stk.push(this->template make_s( + this->v, std::make_integer_sequence>())); + } + }; + + template struct SelectionT> : public SelectionV> { + using V = std::pair; + using SelectionV::SelectionV; + + struct Members : public SelectionV { + explicit Members(V & v) : + SelectionV {v}, members { + SelectionV::make(v.first), + SelectionV::make(v.second), + } + { + } + + void + beforeValue(Stack & stk) override + { + stk.push(std::move(members[idx++])); + } + + std::size_t idx {0}; + std::array members; + }; + + void + beginArray(Stack & stk) override + { + stk.push(this->template make_s(this->v)); + } + }; + template struct MapByMember : public Persistence::SelectionT { MapByMember(Map & m) : Persistence::SelectionT {s}, map {m} { } -- cgit v1.2.3 From 421ae75fa94a05b71c03255af4ad597a60fc8ed9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 8 Mar 2024 01:17:11 +0000 Subject: Rework stream support to work with any collection --- lib/stream_support.h | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/stream_support.h b/lib/stream_support.h index fa536f1..57d82a1 100644 --- a/lib/stream_support.h +++ b/lib/stream_support.h @@ -11,17 +11,16 @@ template concept stringlike = requires(const S & s) { s.substr(0); }; template -concept spanable = std::is_constructible_v, T> && !stringlike - && !std::is_same_v, T>; +concept NonStringIterableCollection + = std::is_same_v().begin()), decltype(std::declval().end())> && !stringlike; namespace std { - template std::ostream & - operator<<(std::ostream & s, const span v) + operator<<(std::ostream & s, const NonStringIterableCollection auto & v) { s << '('; for (const auto & i : v) { - if (&i != &v.front()) { + if (&i != &*v.begin()) { s << ", "; } s << i; @@ -43,13 +42,6 @@ namespace std { return (s << std::span {&v[0], L}); } - template - std::ostream & - operator<<(std::ostream & s, const T & v) - { - return (s << std::span {v}); - } - template std::ostream & operator<<(std::ostream & s, const std::pair & v) -- cgit v1.2.3 From cfe9b339b401eaf5c049981bbfc50ac13ce1def9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 9 Mar 2024 12:08:24 +0000 Subject: Fix non-64bit upgrade wrapper for crossProduct --- lib/maths.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index 5886326..adf2158 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -111,7 +111,7 @@ template inline constexpr glm::vec<3, T, Q> crossProduct(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) { - return crossProduct(a, b); + return crossProduct(a, b); } template -- cgit v1.2.3 From c0ff72c1aef7f1ad502b838af899cb0a9be35263 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 18 Mar 2024 00:04:18 +0000 Subject: Add Ray::intersectPlane --- lib/ray.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib') diff --git a/lib/ray.h b/lib/ray.h index 67fdd12..a831270 100644 --- a/lib/ray.h +++ b/lib/ray.h @@ -42,6 +42,18 @@ public: }) != positions.end(); } + bool + intersectPlane(const PositionType orig, const Direction3D norm, RelativeDistance & distance) const + { + if constexpr (std::is_floating_point_v) { + return glm::intersectRayPlane(start, direction, orig, norm, distance) && distance >= 0.F; + } + else { + const RelativePosition3D origr = orig - start; + return glm::intersectRayPlane({}, direction, origr, norm, distance) && distance >= 0.F; + } + } + bool intersectTriangle(const PositionType t0, const PositionType t1, const PositionType t2, BaryPosition & bary, RelativeDistance & distance) const -- cgit v1.2.3 From 1dc1e08222f346d4434c52e524d199b4b8eeac1f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 18 Mar 2024 00:18:09 +0000 Subject: Add non-default constructor to GeometricPlaneT --- lib/geometricPlane.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index 3b1a0e4..d4b803d 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -16,13 +16,17 @@ public: template class GeometricPlaneT : public GeometricPlane { public: + GeometricPlaneT() = default; + + GeometricPlaneT(PositionType origin, Normal3D normal) : origin(std::move(origin)), normal(normal) { } + struct DistAndPosition { PositionType::value_type dist; PositionType position; }; - PositionType origin; - Normal3D normal; + PositionType origin {}; + Normal3D normal {}; [[nodiscard]] inline PlaneRelation getRelation(PositionType point) const -- cgit v1.2.3 From 49cfebf74e97ccc88d2d6a459172cb2a17e545b8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 21 Mar 2024 20:05:56 +0000 Subject: vector_yaw only needs 2 dimensions --- lib/maths.cpp | 2 +- lib/maths.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/maths.cpp b/lib/maths.cpp index 68662fc..bf17204 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -76,7 +76,7 @@ rotate_yp(Rotation2D a) } float -vector_yaw(const Direction3D & diff) +vector_yaw(const Direction2D & diff) { return std::atan2(diff.x, diff.y); } diff --git a/lib/maths.h b/lib/maths.h index adf2158..ce18b3f 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -84,7 +84,7 @@ glm::mat4 rotate_pitch(float); glm::mat4 rotate_yp(Rotation2D); glm::mat4 rotate_ypr(Rotation3D); -float vector_yaw(const Direction3D & diff); +float vector_yaw(const Direction2D & diff); float vector_pitch(const Direction3D & diff); float round_frac(const float & v, const float & frac); -- cgit v1.2.3 From 31f73b6f44f3deac749af41e84435f5bc9b037f2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 21 Mar 2024 20:09:34 +0000 Subject: Make arc_length a member function --- lib/maths.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index ce18b3f..20a397b 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -19,6 +19,12 @@ struct Arc : public std::pair { { return i ? second : first; } + + [[nodiscard]] constexpr inline float + length() const + { + return second - first; + } }; constexpr const RelativePosition3D up {0, 0, 1}; @@ -171,12 +177,6 @@ operator%=(glm::vec & p, const glm::mat & mutation) return p = p % mutation; } -constexpr inline float -arc_length(const Arc & arc) -{ - return arc.second - arc.first; -} - float normalize(float ang); template -- cgit v1.2.3 From 4571ce1b4cac2ab9bf2454ed06d0d787604d62fa Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 21 Mar 2024 20:23:44 +0000 Subject: Add helper constructors to Arc * Two angles, wraps logic ensuring b after a * Two vector directions * Centre and two endpoints, in at least 2 dimensions, uses .xy() --- lib/maths.cpp | 5 +++++ lib/maths.h | 28 +++++++++------------------- 2 files changed, 14 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/maths.cpp b/lib/maths.cpp index bf17204..51e27fe 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -4,6 +4,11 @@ #include #include +Arc::Arc(const RelativePosition2D & dir0, const RelativePosition2D & dir1) : + Arc {vector_yaw(dir0), vector_yaw(dir1)} { } + +Arc::Arc(const Angle anga, const Angle angb) : pair {anga, (angb < anga) ? angb + two_pi : angb} { } + glm::mat4 flat_orientation(const Direction3D & diff) { diff --git a/lib/maths.h b/lib/maths.h index 20a397b..656fefd 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -8,11 +8,16 @@ #include #include -struct Arc : public std::pair { - using std::pair::pair; +struct Arc : public std::pair { + template + requires(Lc >= 2, Le >= 2) + Arc(const glm::vec & centre, const glm::vec & e0p, const glm::vec & e1p) : + Arc {RelativePosition2D {e0p.xy() - centre.xy()}, RelativePosition2D {e1p.xy() - centre.xy()}} + { + } - template - Arc(const glm::vec<3, T, Q> & centre3, const glm::vec<3, T, Q> & e0p, const glm::vec<3, T, Q> & e1p); + Arc(const RelativePosition2D & dir0, const RelativePosition2D & dir1); + Arc(const Angle angb, const Angle anga); auto operator[](bool i) const @@ -244,21 +249,6 @@ 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 c6c0d8005880d73b8560bcb10c34a6e8b2da703e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 23 Mar 2024 18:04:57 +0000 Subject: Extract vector_normal helper into lib --- lib/maths.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index 656fefd..63b752a 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -98,6 +98,13 @@ glm::mat4 rotate_ypr(Rotation3D); float vector_yaw(const Direction2D & diff); float vector_pitch(const Direction3D & diff); +template +glm::vec<2, T, Q> +vector_normal(const glm::vec<2, T, Q> & v) +{ + return {-v.y, v.x}; +}; + float round_frac(const float & v, const float & frac); template -- cgit v1.2.3