summaryrefslogtreecommitdiff
path: root/game/geoData.h
diff options
context:
space:
mode:
Diffstat (limited to 'game/geoData.h')
-rw-r--r--game/geoData.h134
1 files changed, 92 insertions, 42 deletions
diff --git a/game/geoData.h b/game/geoData.h
index f9a7d7b..d4d0fb3 100644
--- a/game/geoData.h
+++ b/game/geoData.h
@@ -1,62 +1,112 @@
#pragma once
-#include <array>
+#include "collections.h" // IWYU pragma: keep IterableCollection
+#include "config/types.h"
+#include "ray.h"
+#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <filesystem>
-#include <glm/glm.hpp>
+#include <glm/vec2.hpp>
#include <optional>
-#include <span>
-#include <utility>
-#include <vector>
+#include <thirdparty/openmesh/glmcompat.h>
-class Ray;
+struct GeoDataTraits : public OpenMesh::DefaultTraits {
+ FaceAttributes(OpenMesh::Attributes::Status);
+ EdgeAttributes(OpenMesh::Attributes::Status);
+ VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status);
+ HalfedgeAttributes(OpenMesh::Attributes::Status);
+ using Point = GlobalPosition3D;
+ using Normal = Normal3D;
+};
-class GeoData {
-public:
- struct Node {
- float height {-1.5F};
- };
+class GeoData : public OpenMesh::TriMesh_ArrayKernelT<GeoDataTraits> {
+private:
+ GeoData() = default;
- using Quad = std::array<glm::vec3, 4>;
+public:
+ static GeoData loadFromAsciiGrid(const std::filesystem::path &);
+ static GeoData createFlat(GlobalPosition2D lower, GlobalPosition2D upper, GlobalDistance h);
- using Limits = std::pair<glm::ivec2, glm::ivec2>;
+ struct PointFace {
+ // NOLINTNEXTLINE(hicpp-explicit-conversions)
+ PointFace(const GlobalPosition2D p) : point {p} { }
- explicit GeoData(Limits limit, float scale = 10.F);
+ PointFace(const GlobalPosition2D p, FaceHandle face) : point {p}, _face {face} { }
- void generateRandom();
- void loadFromImages(const std::filesystem::path &, float scale);
+ PointFace(const GlobalPosition2D p, const GeoData *);
+ PointFace(const GlobalPosition2D p, const GeoData *, FaceHandle start);
- [[nodiscard]] glm::vec3 positionAt(glm::vec2) const;
- [[nodiscard]] std::optional<glm::vec3> intersectRay(const Ray &) const;
+ const GlobalPosition2D point;
+ [[nodiscard]] FaceHandle face(const GeoData *) const;
+ [[nodiscard]] FaceHandle face(const GeoData *, FaceHandle start) const;
- [[nodiscard]] unsigned int at(glm::ivec2) const;
- [[nodiscard]] unsigned int at(int x, int y) const;
- [[nodiscard]] Quad quad(glm::vec2) const;
+ [[nodiscard]] bool
+ isLocated() const
+ {
+ return _face.is_valid();
+ }
- [[nodiscard]] Limits getLimit() const;
- [[nodiscard]] glm::uvec2 getSize() const;
- [[nodiscard]] float getScale() const;
- [[nodiscard]] std::span<const Node> getNodes() const;
+ private:
+ mutable FaceHandle _face {};
+ };
- class RayTracer {
- public:
- RayTracer(glm::vec2 p0, glm::vec2 p1);
+ template<glm::length_t Dim> struct Triangle : public glm::vec<3, glm::vec<Dim, GlobalDistance>> {
+ using base = glm::vec<3, glm::vec<Dim, GlobalDistance>>;
+ using base::base;
- glm::vec2 next();
+ template<IterableCollection Range> Triangle(const GeoData * m, Range range)
+ {
+ assert(std::distance(range.begin(), range.end()) == 3);
+ std::transform(range.begin(), range.end(), &base::operator[](0), [m](auto vh) {
+ return m->point(vh);
+ });
+ }
- private:
- RayTracer(glm::vec2 p0, glm::vec2 p1, glm::vec2 d);
- RayTracer(glm::vec2 p0, glm::vec2 d, std::pair<float, float>, std::pair<float, float>);
- static std::pair<float, float> byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_t);
-
- glm::vec2 p;
- const glm::vec2 d;
- float error;
- glm::vec2 inc;
+ glm::vec<Dim, GlobalDistance>
+ operator*(BaryPosition bari) const
+ {
+ const auto & t {*this};
+ return t[0] + GlobalPosition<Dim>(RelativePosition<Dim>(t[1] - t[0]) * bari.x)
+ + GlobalPosition<Dim>(RelativePosition<Dim>(t[2] - t[1]) * bari.y);
+ }
};
+ [[nodiscard]] FaceHandle findPoint(GlobalPosition2D) const;
+ [[nodiscard]] FaceHandle findPoint(GlobalPosition2D, FaceHandle start) const;
+
+ [[nodiscard]] GlobalPosition3D positionAt(const PointFace &) const;
+ [[nodiscard]] std::optional<GlobalPosition3D> intersectRay(const Ray &) const;
+ [[nodiscard]] std::optional<GlobalPosition3D> intersectRay(const Ray &, FaceHandle start) const;
+
+ void walk(const PointFace & from, const GlobalPosition2D to, const std::function<void(FaceHandle)> & op) const;
+ void walkUntil(const PointFace & from, const GlobalPosition2D to, const std::function<bool(FaceHandle)> & op) const;
+
+ void boundaryWalk(const std::function<void(HalfedgeHandle)> &) const;
+ void boundaryWalk(const std::function<void(HalfedgeHandle)> &, HalfedgeHandle start) const;
+ void boundaryWalkUntil(const std::function<bool(HalfedgeHandle)> &) const;
+ void boundaryWalkUntil(const std::function<bool(HalfedgeHandle)> &, HalfedgeHandle start) const;
+
+ [[nodiscard]] HalfedgeHandle findEntry(const GlobalPosition2D from, const GlobalPosition2D to) const;
+
+ [[nodiscard]] auto
+ getExtents() const
+ {
+ return std::tie(lowerExtent, upperExtent);
+ }
+
protected:
- Limits limit {}; // Base grid limits first(x,y) -> second(x,y)
- glm::uvec2 size {};
- float scale {1};
- std::vector<Node> nodes;
+ template<glm::length_t Dim>
+ [[nodiscard]] Triangle<Dim>
+ triangle(FaceHandle f) const
+ {
+ return {this, fv_range(f)};
+ }
+
+ [[nodiscard]] static bool triangleContainsPoint(const GlobalPosition2D, const Triangle<2> &);
+ [[nodiscard]] bool triangleContainsPoint(const GlobalPosition2D, FaceHandle) const;
+ [[nodiscard]] HalfedgeHandle findBoundaryStart() const;
+
+ void update_vertex_normals_only();
+
+private:
+ GlobalPosition3D lowerExtent {}, upperExtent {};
};