From 5dc841c1a4428f1a1ea353583e3b51ea24b4c608 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 11 Mar 2023 12:05:57 +0000 Subject: CLOG includes line number --- lib/stream_support.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/stream_support.hpp b/lib/stream_support.hpp index 6a3c2cf..52cc9d9 100644 --- a/lib/stream_support.hpp +++ b/lib/stream_support.hpp @@ -83,4 +83,4 @@ streamed_string(const T & v) return std::move(ss).str(); } -#define CLOG(x) std::cerr << #x " : " << x << "\n"; +#define CLOG(x) std::cerr << __LINE__ << " : " #x " : " << x << "\n"; -- cgit v1.2.3 From 2d6772cb1c592e4bd75be40f1fdfa924bbbc3c07 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 14 Mar 2023 18:14:54 +0000 Subject: Add postLoad support to persistence --- lib/persistence.cpp | 5 +++++ lib/persistence.h | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/persistence.cpp b/lib/persistence.cpp index 8c7c6a4..06e40c7 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -41,6 +41,11 @@ namespace Persistence { return ss.str(); } + void + Persistable::postLoad() + { + } + PersistenceSelect::PersistenceSelect(const std::string & n) : name {n} { } PersistenceStore::NameAction diff --git a/lib/persistence.h b/lib/persistence.h index 35d60ca..1cb3af0 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -311,8 +311,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr map.emplace(std::invoke(Key, s), std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -327,8 +328,9 @@ namespace Persistence { void endObject(Persistence::Stack & stk) override { + // TODO test with unique_ptr container.emplace_back(std::move(s)); - stk.pop(); + Persistence::SelectionT::endObject(stk); } private: @@ -342,6 +344,7 @@ namespace Persistence { DEFAULT_MOVE_COPY(Persistable); virtual bool persist(PersistenceStore & store) = 0; + virtual void postLoad(); [[nodiscard]] virtual std::string getId() const; @@ -484,6 +487,9 @@ namespace Persistence { endObject(Stack & stk) override { make_default_as_needed(this->v); + if (this->v) { + this->v->postLoad(); + } stk.pop(); } -- cgit v1.2.3 From d75e737de182d11d0293212d6fd0fc2c6732af54 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 17 Mar 2023 02:03:30 +0000 Subject: Allow overriding vectorOfN's returned type --- lib/collections.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/collections.hpp b/lib/collections.hpp index 16870be..2b83500 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -92,11 +92,11 @@ operator+(const std::vector & in, Vn && vn) return out; } -template typename Direction = std::plus> +template typename Direction = std::plus, typename T = unsigned int> [[nodiscard]] static auto -vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) +vectorOfN(std::integral auto N, T start = {}, T step = 1) { - std::vector v; + std::vector v; v.resize(N); std::generate_n(v.begin(), N, [&start, step, adj = Direction {}]() { return std::exchange(start, adj(start, step)); -- cgit v1.2.3 From 5bfeeb70c6a7ed6ccda0576dc39050eb4240434f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 20 Mar 2023 19:32:26 +0000 Subject: Lazy initialisation of SelectionPtr in persistValue Always needed for write phase, but only need on read if their's a name match. --- lib/persistence.cpp | 22 ++++++++++++++-------- lib/persistence.h | 13 ++++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/persistence.cpp b/lib/persistence.cpp index 06e40c7..e22d74d 100644 --- a/lib/persistence.cpp +++ b/lib/persistence.cpp @@ -48,10 +48,15 @@ namespace Persistence { PersistenceSelect::PersistenceSelect(const std::string & n) : name {n} { } - PersistenceStore::NameAction - PersistenceSelect::setName(const std::string_view key, const Selection &) + PersistenceStore::NameActionSelection + PersistenceSelect::setName(const std::string_view key, SelectionFactory && factory) { - return (key == name) ? NameAction::Push : NameAction::Ignore; + if (key == name) { + return {NameAction::Push, factory()}; + } + else { + return {NameAction::Ignore, nullptr}; + } } void @@ -61,10 +66,11 @@ namespace Persistence { PersistenceWrite::PersistenceWrite(const Writer & o, bool sh) : out {o}, shared {sh} { } - PersistenceStore::NameAction - PersistenceWrite::setName(const std::string_view key, const Selection & s) + PersistenceStore::NameActionSelection + PersistenceWrite::setName(const std::string_view key, SelectionFactory && factory) { - if (s.needsWrite()) { + auto s = factory(); + if (s->needsWrite()) { if (!first) { out.nextValue(); } @@ -72,9 +78,9 @@ namespace Persistence { first = false; } out.pushKey(key); - return NameAction::HandleAndContinue; + return {NameAction::HandleAndContinue, std::move(s)}; } - return NameAction::Ignore; + return {NameAction::Ignore, nullptr}; } void diff --git a/lib/persistence.h b/lib/persistence.h index 1cb3af0..d38d4b1 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -149,6 +149,7 @@ namespace Persistence { struct Persistable; struct PersistenceStore { + using SelectionFactory = std::function; PersistenceStore() = default; virtual ~PersistenceStore() = default; DEFAULT_MOVE_NO_COPY(PersistenceStore); @@ -156,12 +157,14 @@ namespace Persistence { template [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti); enum class NameAction { Push, HandleAndContinue, Ignore }; + using NameActionSelection = std::pair; template [[nodiscard]] inline bool persistValue(const std::string_view key, T & value) { - auto s = std::make_unique(value); - const auto act {setName(key, *s)}; + auto [act, s] = setName(key, [&value]() { + return std::make_unique(value); + }); if (act != NameAction::Ignore) { sel = std::move(s); if (act == NameAction::HandleAndContinue) { @@ -171,7 +174,7 @@ namespace Persistence { return (act != NameAction::Push); } - virtual NameAction setName(const std::string_view key, const Selection &) = 0; + [[nodiscard]] virtual NameActionSelection setName(const std::string_view key, SelectionFactory &&) = 0; virtual void selHandler() {}; virtual void setType(const std::string_view, const Persistable *) = 0; @@ -181,7 +184,7 @@ namespace Persistence { struct PersistenceSelect : public PersistenceStore { explicit PersistenceSelect(const std::string & n); - NameAction setName(const std::string_view key, const Selection &) override; + NameActionSelection setName(const std::string_view key, SelectionFactory &&) override; void setType(const std::string_view, const Persistable *) override; @@ -191,7 +194,7 @@ namespace Persistence { struct PersistenceWrite : public PersistenceStore { explicit PersistenceWrite(const Writer & o, bool sh); - NameAction setName(const std::string_view key, const Selection &) override; + NameActionSelection setName(const std::string_view key, SelectionFactory &&) override; void selHandler() override; -- cgit v1.2.3 From 3f2822035b3f1c67e0d6ce410bf43e84b0846037 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 20 Mar 2023 23:22:04 +0000 Subject: Add persistence support for std::optional<>s --- lib/persistence.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib') diff --git a/lib/persistence.h b/lib/persistence.h index d38d4b1..cc2e4e5 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,10 @@ namespace Persistence { } }; + template struct SelectionT> : public SelectionT { + explicit SelectionT(std::optional & value) : SelectionT {value.emplace()} { } + }; + struct Persistable; struct PersistenceStore { using SelectionFactory = std::function; -- cgit v1.2.3 From b244b896503065198bc736bbff52d6ff79c24986 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 7 Apr 2023 23:47:26 +0100 Subject: Add helper for iterating over a range defined by a pair of iterators --- lib/collections.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/collections.hpp b/lib/collections.hpp index 2b83500..59cec6f 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -117,3 +117,18 @@ materializeRange(const std::pair & in) { return Rtn(in.first, in.second); } + +template struct pair_range { + constexpr auto & + begin() const noexcept + { + return pair.first; + } + constexpr auto & + end() const noexcept + { + return pair.second; + } + const std::pair & pair; +}; +template pair_range(std::pair) -> pair_range; -- cgit v1.2.3 From b222c21715384efc2eaa53d3ba295fb34da5b599 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 9 Apr 2023 12:24:55 +0100 Subject: Start to factor out geometric place from face controller split --- lib/geometricPlane.cpp | 9 +++++++++ lib/geometricPlane.h | 12 ++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 lib/geometricPlane.cpp create mode 100644 lib/geometricPlane.h (limited to 'lib') diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp new file mode 100644 index 0000000..cb52997 --- /dev/null +++ b/lib/geometricPlane.cpp @@ -0,0 +1,9 @@ +#include "geometricPlane.h" +#include + +GeometricPlane::PlaneRelation +GeometricPlane::getRelation(glm::vec3 p) const +{ + const auto d = glm::dot(normal, p - origin); + return d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On; +} diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h new file mode 100644 index 0000000..96816f2 --- /dev/null +++ b/lib/geometricPlane.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class GeometricPlane { +public: + enum class PlaneRelation { Above, Below, On }; + + glm::vec3 origin, normal; + + PlaneRelation getRelation(glm::vec3 point) const; +}; -- cgit v1.2.3 From d8f61abae79a3154d5e6dda98713295d1b850e95 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 9 Apr 2023 12:42:09 +0100 Subject: Add helper to create a Ray from two points --- lib/ray.cpp | 6 ++++++ lib/ray.hpp | 2 ++ 2 files changed, 8 insertions(+) (limited to 'lib') diff --git a/lib/ray.cpp b/lib/ray.cpp index acbb807..f9e3311 100644 --- a/lib/ray.cpp +++ b/lib/ray.cpp @@ -1,6 +1,12 @@ #include "ray.hpp" #include +Ray +Ray::fromPoints(glm::vec3 start, glm::vec3 p) +{ + return {start, glm::normalize(p - start)}; +} + float Ray::distanceToLine(const glm::vec3 & p1, const glm::vec3 & e1) const { diff --git a/lib/ray.hpp b/lib/ray.hpp index 8bef1c8..9bf47af 100644 --- a/lib/ray.hpp +++ b/lib/ray.hpp @@ -7,6 +7,8 @@ class Ray { public: Ray(glm::vec3 start, glm::vec3 direction) : start {start}, direction {direction} { } + static Ray fromPoints(glm::vec3, glm::vec3); + glm::vec3 start; glm::vec3 direction; -- cgit v1.2.3 From 560bfce98146895cfd8429150792d49a18e24d98 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 9 Apr 2023 13:15:46 +0100 Subject: Add lots of split required stuff to geometric plane --- lib/geometricPlane.cpp | 19 +++++++++++++++++++ lib/geometricPlane.h | 10 ++++++++++ 2 files changed, 29 insertions(+) (limited to 'lib') diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp index cb52997..71216c1 100644 --- a/lib/geometricPlane.cpp +++ b/lib/geometricPlane.cpp @@ -1,5 +1,7 @@ #include "geometricPlane.h" +#include "ray.hpp" #include +#include GeometricPlane::PlaneRelation GeometricPlane::getRelation(glm::vec3 p) const @@ -7,3 +9,20 @@ GeometricPlane::getRelation(glm::vec3 p) const const auto d = glm::dot(normal, p - origin); return d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On; } + +bool +GeometricPlane::isIntersect(PlaneRelation a, PlaneRelation b) +{ + return ((a == PlaneRelation::Above && b == PlaneRelation::Below) + || (a == PlaneRelation::Below && b == PlaneRelation::Above)); +} + +std::optional +GeometricPlane::getRayIntersectPosition(const Ray & ray) const +{ + float dist {}; + if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) { + return {}; + } + return DistAndPosition {dist, ray.start + (ray.direction * dist)}; +} diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index 96816f2..dc8df50 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -1,12 +1,22 @@ #pragma once #include +#include + +class Ray; class GeometricPlane { public: + struct DistAndPosition { + float dist; + glm::vec3 position; + }; enum class PlaneRelation { Above, Below, On }; glm::vec3 origin, normal; PlaneRelation getRelation(glm::vec3 point) const; + std::optional getRayIntersectPosition(const Ray &) const; + + static bool isIntersect(PlaneRelation a, PlaneRelation b); }; -- cgit v1.2.3