summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/collections.hpp21
-rw-r--r--lib/geometricPlane.cpp28
-rw-r--r--lib/geometricPlane.h22
-rw-r--r--lib/persistence.cpp27
-rw-r--r--lib/persistence.h28
-rw-r--r--lib/ray.cpp6
-rw-r--r--lib/ray.hpp2
-rw-r--r--lib/stream_support.hpp2
8 files changed, 117 insertions, 19 deletions
diff --git a/lib/collections.hpp b/lib/collections.hpp
index 16870be..59cec6f 100644
--- a/lib/collections.hpp
+++ b/lib/collections.hpp
@@ -92,11 +92,11 @@ operator+(const std::vector<T...> & in, Vn && vn)
return out;
}
-template<template<typename> typename Direction = std::plus>
+template<template<typename> 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<unsigned int> v;
+ std::vector<T> v;
v.resize(N);
std::generate_n(v.begin(), N, [&start, step, adj = Direction {}]() {
return std::exchange(start, adj(start, step));
@@ -117,3 +117,18 @@ materializeRange(const std::pair<In, In> & in)
{
return Rtn(in.first, in.second);
}
+
+template<typename T> struct pair_range {
+ constexpr auto &
+ begin() const noexcept
+ {
+ return pair.first;
+ }
+ constexpr auto &
+ end() const noexcept
+ {
+ return pair.second;
+ }
+ const std::pair<T, T> & pair;
+};
+template<typename T> pair_range(std::pair<T, T>) -> pair_range<T>;
diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp
new file mode 100644
index 0000000..71216c1
--- /dev/null
+++ b/lib/geometricPlane.cpp
@@ -0,0 +1,28 @@
+#include "geometricPlane.h"
+#include "ray.hpp"
+#include <glm/geometric.hpp>
+#include <glm/gtx/intersect.hpp>
+
+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;
+}
+
+bool
+GeometricPlane::isIntersect(PlaneRelation a, PlaneRelation b)
+{
+ return ((a == PlaneRelation::Above && b == PlaneRelation::Below)
+ || (a == PlaneRelation::Below && b == PlaneRelation::Above));
+}
+
+std::optional<GeometricPlane::DistAndPosition>
+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
new file mode 100644
index 0000000..dc8df50
--- /dev/null
+++ b/lib/geometricPlane.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <glm/vec3.hpp>
+#include <optional>
+
+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<DistAndPosition> getRayIntersectPosition(const Ray &) const;
+
+ static bool isIntersect(PlaneRelation a, PlaneRelation b);
+};
diff --git a/lib/persistence.cpp b/lib/persistence.cpp
index 8c7c6a4..e22d74d 100644
--- a/lib/persistence.cpp
+++ b/lib/persistence.cpp
@@ -41,12 +41,22 @@ namespace Persistence {
return ss.str();
}
+ void
+ Persistable::postLoad()
+ {
+ }
+
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
@@ -56,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();
}
@@ -67,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 35d60ca..cc2e4e5 100644
--- a/lib/persistence.h
+++ b/lib/persistence.h
@@ -6,6 +6,7 @@
#include <iosfwd>
#include <map>
#include <memory>
+#include <optional>
#include <span>
#include <special_members.hpp>
#include <sstream>
@@ -147,8 +148,13 @@ namespace Persistence {
}
};
+ template<typename T> struct SelectionT<std::optional<T>> : public SelectionT<T> {
+ explicit SelectionT(std::optional<T> & value) : SelectionT<T> {value.emplace()} { }
+ };
+
struct Persistable;
struct PersistenceStore {
+ using SelectionFactory = std::function<SelectionPtr()>;
PersistenceStore() = default;
virtual ~PersistenceStore() = default;
DEFAULT_MOVE_NO_COPY(PersistenceStore);
@@ -156,12 +162,14 @@ namespace Persistence {
template<typename T> [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti);
enum class NameAction { Push, HandleAndContinue, Ignore };
+ using NameActionSelection = std::pair<NameAction, SelectionPtr>;
template<typename Helper, typename T>
[[nodiscard]] inline bool
persistValue(const std::string_view key, T & value)
{
- auto s = std::make_unique<Helper>(value);
- const auto act {setName(key, *s)};
+ auto [act, s] = setName(key, [&value]() {
+ return std::make_unique<Helper>(value);
+ });
if (act != NameAction::Ignore) {
sel = std::move(s);
if (act == NameAction::HandleAndContinue) {
@@ -171,7 +179,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 +189,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 +199,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;
@@ -311,8 +319,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<Type>::endObject(stk);
}
private:
@@ -327,8 +336,9 @@ namespace Persistence {
void
endObject(Persistence::Stack & stk) override
{
+ // TODO test with unique_ptr
container.emplace_back(std::move(s));
- stk.pop();
+ Persistence::SelectionT<Type>::endObject(stk);
}
private:
@@ -342,6 +352,7 @@ namespace Persistence {
DEFAULT_MOVE_COPY(Persistable);
virtual bool persist(PersistenceStore & store) = 0;
+ virtual void postLoad();
[[nodiscard]] virtual std::string getId() const;
@@ -484,6 +495,9 @@ namespace Persistence {
endObject(Stack & stk) override
{
make_default_as_needed(this->v);
+ if (this->v) {
+ this->v->postLoad();
+ }
stk.pop();
}
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 <algorithm>
+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;
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";