summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-10-31 01:26:58 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-10-31 02:38:22 +0000
commita0f1dc90aabd4cb5cfe2aef7116af170e6d0b953 (patch)
tree6d31ec9e79f94d04d7270c754d5bd8c0814d57d7
parentInitial commit of walk/walkUtil terrain (diff)
downloadilt-a0f1dc90aabd4cb5cfe2aef7116af170e6d0b953.tar.bz2
ilt-a0f1dc90aabd4cb5cfe2aef7116af170e6d0b953.tar.xz
ilt-a0f1dc90aabd4cb5cfe2aef7116af170e6d0b953.zip
Helper type for storing/passing/returning a point and its containing face
point is const as face is mutable as a cache of the face containing point.
-rw-r--r--game/terrain2.cpp18
-rw-r--r--game/terrain2.h10
-rw-r--r--test/test-terrain.cpp9
3 files changed, 37 insertions, 0 deletions
diff --git a/game/terrain2.cpp b/game/terrain2.cpp
index 3e24778..e98cebb 100644
--- a/game/terrain2.cpp
+++ b/game/terrain2.cpp
@@ -54,6 +54,24 @@ TerrainMesh::findPoint(glm::vec2 p) const
return findPoint(p, *faces_begin());
}
+bool
+TerrainMesh::locate(const TerrainMesh::PointFace & pointFace, FaceHandle start) const
+{
+ if (pointFace.face.is_valid()) {
+ assert(triangleContainsPoint(pointFace.point, pointFace.face));
+ return true;
+ }
+ else {
+ return (pointFace.face = findPoint(pointFace.point, start)).is_valid();
+ }
+}
+
+bool
+TerrainMesh::locate(const TerrainMesh::PointFace & pointFace) const
+{
+ return locate(pointFace, *faces_begin());
+}
+
namespace {
[[nodiscard]] constexpr inline bool
pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2)
diff --git a/game/terrain2.h b/game/terrain2.h
index 848c36e..e516719 100644
--- a/game/terrain2.h
+++ b/game/terrain2.h
@@ -18,6 +18,13 @@ class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT<TerrainTraits> {
public:
explicit TerrainMesh(const std::filesystem::path &);
+ struct PointFace {
+ PointFace(const glm::vec2 p) : point {p} { }
+
+ const glm::vec2 point;
+ mutable FaceHandle face {};
+ };
+
[[nodiscard]] FaceHandle findPoint(glm::vec2) const;
[[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const;
@@ -28,4 +35,7 @@ protected:
[[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2);
[[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const;
[[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const;
+
+ bool locate(const PointFace &) const;
+ bool locate(const PointFace &, FaceHandle start) const;
};
diff --git a/test/test-terrain.cpp b/test/test-terrain.cpp
index 429207c..f737f4f 100644
--- a/test/test-terrain.cpp
+++ b/test/test-terrain.cpp
@@ -46,6 +46,15 @@ BOOST_AUTO_TEST_CASE(trianglesContainsPoints)
}
}
+BOOST_AUTO_TEST_CASE(locatePointFace)
+{
+ const PointFace pf {{310002, 490003}};
+ BOOST_CHECK(!pf.face.is_valid());
+ BOOST_CHECK(locate(pf));
+ BOOST_CHECK(pf.face.is_valid());
+ BOOST_CHECK_EQUAL(pf.face.idx(), 0);
+}
+
BOOST_AUTO_TEST_SUITE_END();
using FindPointData = std::tuple<glm::vec2, int>;