diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-05 13:07:32 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-05 13:07:40 +0000 |
commit | 04078dbb3fe4acd09d150e016c2b2e0d5d036950 (patch) | |
tree | a612b49c5be8104dafe403674716f3b3403e65ae | |
parent | Refactor to further simplify line/point operations (diff) | |
download | ilt-04078dbb3fe4acd09d150e016c2b2e0d5d036950.tar.bz2 ilt-04078dbb3fe4acd09d150e016c2b2e0d5d036950.tar.xz ilt-04078dbb3fe4acd09d150e016c2b2e0d5d036950.zip |
Add methods to walk the boundary of the terrain mesh
-rw-r--r-- | game/geoData.cpp | 45 | ||||
-rw-r--r-- | game/geoData.h | 6 | ||||
-rw-r--r-- | test/perf-geoData.cpp | 11 | ||||
-rw-r--r-- | test/test-geoData.cpp | 10 |
4 files changed, 72 insertions, 0 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp index c58658a..f9c11bf 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -218,6 +218,43 @@ GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::functi } } +void +GeoData::boundaryWalk(const std::function<void(HalfedgeHandle)> & op) const +{ + boundaryWalk(op, findBoundaryStart()); +} + +void +GeoData::boundaryWalk(const std::function<void(HalfedgeHandle)> & op, HalfedgeHandle start) const +{ + assert(is_boundary(start)); + boundaryWalkUntil( + [&op](auto heh) { + op(heh); + return false; + }, + start); +} + +void +GeoData::boundaryWalkUntil(const std::function<bool(HalfedgeHandle)> & op) const +{ + boundaryWalkUntil(op, findBoundaryStart()); +} + +void +GeoData::boundaryWalkUntil(const std::function<bool(HalfedgeHandle)> & op, HalfedgeHandle start) const +{ + assert(is_boundary(start)); + if (!op(start)) { + for (auto heh = next_halfedge_handle(start); heh != start; heh = next_halfedge_handle(heh)) { + if (op(heh)) { + break; + } + } + } +} + bool GeoData::triangleContainsPoint(const glm::vec2 p, const Triangle<2> & t) { @@ -230,3 +267,11 @@ GeoData::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const { return triangleContainsPoint(p, triangle<2>(face)); } + +GeoData::HalfedgeHandle +GeoData::findBoundaryStart() const +{ + return *std::find_if(halfedges_begin(), halfedges_end(), [this](const auto heh) { + return is_boundary(heh); + }); +} diff --git a/game/geoData.h b/game/geoData.h index b8561d0..15d7e24 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -78,6 +78,11 @@ public: void walk(const PointFace & from, const glm::vec2 to, const std::function<void(FaceHandle)> & op) const; void walkUntil(const PointFace & from, const glm::vec2 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]] auto getExtents() const { @@ -94,6 +99,7 @@ protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const Triangle<2> &); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; + [[nodiscard]] HalfedgeHandle findBoundaryStart() const; private: glm::vec3 lowerExtent {}, upperExtent {}; diff --git a/test/perf-geoData.cpp b/test/perf-geoData.cpp index 2d79a90..4fed632 100644 --- a/test/perf-geoData.cpp +++ b/test/perf-geoData.cpp @@ -23,9 +23,20 @@ namespace { }); } } + + void + terrain_walkBoundary(benchmark::State & state) + { + for (auto _ : state) { + tm.boundaryWalk([](auto heh) { + benchmark::DoNotOptimize(heh); + }); + } + } } BENCHMARK(terrain_findPoint); BENCHMARK(terrain_walk); +BENCHMARK(terrain_walkBoundary); BENCHMARK_MAIN(); diff --git a/test/test-geoData.cpp b/test/test-geoData.cpp index 258bd9c..34c9cb6 100644 --- a/test/test-geoData.cpp +++ b/test/test-geoData.cpp @@ -118,6 +118,16 @@ BOOST_DATA_TEST_CASE(findRayIntersect, BOOST_CHECK_CLOSE_VEC(fixedTerrtain.intersectRay({p, d}).value(), i); } +BOOST_AUTO_TEST_CASE(boundaryWalk) +{ + size_t count {}; + fixedTerrtain.boundaryWalk([&count](auto heh) { + BOOST_CHECK(fixedTerrtain.is_boundary(heh)); + count++; + }); + BOOST_CHECK_EQUAL(count, 2 * (ncols + nrows - 2)); +} + using WalkTerrainData = std::tuple<glm::vec2, glm::vec2, std::vector<int>>; BOOST_DATA_TEST_CASE(walkTerrain, |