diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-04 12:14:59 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-04 12:26:00 +0000 |
commit | c463bce75418a145d319f214571e30df311ff8df (patch) | |
tree | 07dab50c7ad4b728aff6b993bdc9bcf43b912c58 | |
parent | Static helper for loading ASCII grid data (diff) | |
download | ilt-c463bce75418a145d319f214571e30df311ff8df.tar.bz2 ilt-c463bce75418a145d319f214571e30df311ff8df.tar.xz ilt-c463bce75418a145d319f214571e30df311ff8df.zip |
Calculate and expose the extents of the terrain mesh
-rw-r--r-- | game/terrain2.cpp | 5 | ||||
-rw-r--r-- | game/terrain2.h | 9 | ||||
-rw-r--r-- | test/test-terrain.cpp | 3 |
3 files changed, 17 insertions, 0 deletions
diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 105eb33..0e8b78e 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -24,10 +24,15 @@ TerrainMesh::loadFromAsciiGrid(const std::filesystem::path & input) std::vector<VertexHandle> vertices; vertices.reserve(ncols * nrows); TerrainMesh mesh; + mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits<float>::max()}; + mesh.upperExtent + = {xllcorner + (cellsize * ncols), yllcorner + (cellsize * nrows), std::numeric_limits<float>::min()}; for (size_t row = 0; row < nrows; ++row) { for (size_t col = 0; col < ncols; ++col) { float height = 0; f >> height; + mesh.upperExtent.z = std::max(mesh.upperExtent.z, height); + mesh.lowerExtent.z = std::min(mesh.lowerExtent.z, height); vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); } } diff --git a/game/terrain2.h b/game/terrain2.h index 69cd380..eda56d0 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -77,8 +77,17 @@ 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; + [[nodiscard]] auto + getExtents() const + { + return std::tie(lowerExtent, upperExtent); + } + 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; + +private: + glm::vec3 lowerExtent {}, upperExtent {}; }; diff --git a/test/test-terrain.cpp b/test/test-terrain.cpp index 512a90e..71fc1ec 100644 --- a/test/test-terrain.cpp +++ b/test/test-terrain.cpp @@ -19,6 +19,9 @@ BOOST_AUTO_TEST_CASE(loadSuccess) { BOOST_CHECK_EQUAL(ncols * nrows, n_vertices()); BOOST_CHECK_EQUAL(2 * (ncols - 1) * (nrows - 1), n_faces()); + const auto [lower, upper] = getExtents(); + BOOST_CHECK_EQUAL(lower, glm::vec3(310000, 490000, -2.6)); + BOOST_CHECK_EQUAL(upper, glm::vec3(320000, 500000, 571.6)); } BOOST_AUTO_TEST_CASE(normalsAllPointUp) |