summaryrefslogtreecommitdiff
path: root/game/geoData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/geoData.cpp')
-rw-r--r--game/geoData.cpp340
1 files changed, 182 insertions, 158 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index 34a1030..d32bf8c 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -1,214 +1,238 @@
#include "geoData.h"
-#include "gfx/image.h"
-#include <algorithm>
-#include <array>
-#include <cmath>
-#include <cstddef>
+#include <fstream>
#include <glm/gtx/intersect.hpp>
-#include <initializer_list>
-#include <limits>
#include <maths.h>
-#include <random>
-#include <ray.h>
-#include <stb/stb_image.h>
-#include <stdexcept>
-#include <util.h>
-
-GeoData::GeoData(Limits l, float s) :
- limit {std::move(l)}, size {(limit.second - limit.first) + 1}, scale {s}, nodes {[this]() {
- return (static_cast<std::size_t>(size.x * size.y));
- }()}
-{
-}
-void
-GeoData::generateRandom()
+GeoData
+GeoData::loadFromAsciiGrid(const std::filesystem::path & input)
{
- // We acknowledge this is terrible :)
-
- // Add hills
- std::mt19937 gen(std::random_device {}());
- std::uniform_int_distribution<> rxpos(limit.first.x + 2, limit.second.x - 2),
- rypos(limit.first.y + 2, limit.second.y - 2);
- std::uniform_int_distribution<> rsize(10, 30);
- std::uniform_real_distribution<float> rheight(1, 3);
- for (int h = 0; h < 500;) {
- const glm::ivec2 hpos {rxpos(gen), rypos(gen)};
- const glm::ivec2 hsize {rsize(gen), rsize(gen)};
- if (const auto lim1 = hpos - hsize; lim1.x > limit.first.x && lim1.y > limit.first.y) {
- if (const auto lim2 = hpos + hsize; lim2.x < limit.second.x && lim2.y < limit.second.y) {
- const auto height = rheight(gen);
- const glm::ivec2 hsizesqrd {hsize.x * hsize.x, hsize.y * hsize.y};
- for (auto y = lim1.y; y < lim2.y; y += 1) {
- for (auto x = lim1.x; x < lim2.x; x += 1) {
- const auto dist {hpos - glm::ivec2 {x, y}};
- const glm::ivec2 distsqrd {dist.x * dist.x, dist.y * dist.y};
- const auto out {ratio(sq(x - hpos.x), sq(hsize.x)) + ratio(sq(y - hpos.y), sq(hsize.y))};
- if (out <= 1.0F) {
- auto & node {nodes[at({x, y})]};
- const auto m {1.F / (7.F * out - 8.F) + 1.F};
- node.height += height * m;
- }
- }
- }
- h += 1;
- }
+ size_t ncols = 0, nrows = 0, xllcorner = 0, yllcorner = 0, cellsize = 0;
+ std::map<std::string_view, size_t *> properties {
+ {"ncols", &ncols},
+ {"nrows", &nrows},
+ {"xllcorner", &xllcorner},
+ {"yllcorner", &yllcorner},
+ {"cellsize", &cellsize},
+ };
+ std::ifstream f {input};
+ while (!properties.empty()) {
+ std::string property;
+ f >> property;
+ f >> *properties.at(property);
+ properties.erase(property);
+ }
+ std::vector<VertexHandle> vertices;
+ vertices.reserve(ncols * nrows);
+ GeoData 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}));
}
}
-}
+ if (!f.good()) {
+ throw std::runtime_error("Couldn't read terrain file");
+ }
+ for (size_t row = 1; row < nrows; ++row) {
+ for (size_t col = 1; col < ncols; ++col) {
+ mesh.add_face({
+ vertices[ncols * (row - 1) + (col - 1)],
+ vertices[ncols * (row - 0) + (col - 0)],
+ vertices[ncols * (row - 0) + (col - 1)],
+ });
+ mesh.add_face({
+ vertices[ncols * (row - 1) + (col - 1)],
+ vertices[ncols * (row - 1) + (col - 0)],
+ vertices[ncols * (row - 0) + (col - 0)],
+ });
+ }
+ }
+ mesh.update_face_normals();
+ mesh.update_vertex_normals();
-void
-GeoData::loadFromImages(const std::filesystem::path & fileName, float scale_)
-{
- const Image map {fileName.c_str(), STBI_grey};
- size = {map.width, map.height};
- limit = {{0, 0}, size - glm::uvec2 {1, 1}};
- const auto points {size.x * size.y};
- scale = scale_;
- nodes.resize(points);
-
- std::transform(map.data.data(), map.data.data() + points, nodes.begin(), [](auto d) {
- return Node {(d * 0.1F) - 1.5F};
- });
-}
+ return mesh;
+};
-GeoData::Quad
-GeoData::quad(glm::vec2 wcoord) const
+GeoData
+GeoData::createFlat(glm::vec2 lower, glm::vec2 upper, float h)
{
- constexpr static const std::array<glm::vec2, 4> corners {{{0, 0}, {0, 1}, {1, 0}, {1, 1}}};
- return transform_array(transform_array(corners,
- [coord = (wcoord / scale)](const auto c) {
- return glm::vec2 {std::floor(coord.x), std::floor(coord.y)} + c;
- }),
- [this](const auto c) {
- return (c * scale) || nodes[at(c)].height;
- });
-}
+ GeoData mesh;
-glm::vec3
-GeoData::positionAt(const glm::vec2 wcoord) const
-{
- const auto point {quad(wcoord)};
- const glm::vec2 frac = (wcoord - !point.front()) / scale;
- auto edge = [&point, &frac](auto offset) {
- return point[offset].z + ((point[offset + 2].z - point[offset].z) * frac.x);
- };
- const auto heightFloor = edge(0U), heightCeil = edge(1U),
- heightMid = heightFloor + ((heightCeil - heightFloor) * frac.y);
+ mesh.lowerExtent = lower ^ h;
+ mesh.upperExtent = upper ^ h;
+
+ const auto ll = mesh.add_vertex({lower.x, lower.y, h}), lu = mesh.add_vertex({lower.x, upper.y, h}),
+ ul = mesh.add_vertex({upper.x, lower.y, h}), uu = mesh.add_vertex({upper.x, upper.y, h});
+
+ mesh.add_face(ll, uu, lu);
+ mesh.add_face(ll, ul, uu);
- return wcoord || heightMid;
+ mesh.update_face_normals();
+ mesh.update_vertex_normals();
+
+ return mesh;
}
-GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1) : RayTracer {p0, p1, glm::abs(p1)} { }
-GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1, glm::vec2 d) :
- RayTracer {p0, d, byAxis(p0, p1, d, 0), byAxis(p0, p1, d, 1)}
+OpenMesh::FaceHandle
+GeoData::findPoint(glm::vec2 p) const
{
+ return findPoint(p, *faces_begin());
}
-GeoData::RayTracer::RayTracer(
- glm::vec2 p0, glm::vec2 d_, std::pair<float, float> xdata, std::pair<float, float> ydata) :
- p {glm::floor(p0)},
- d {d_}, error {xdata.second - ydata.second}, inc {xdata.first, ydata.first}
+GeoData::PointFace::PointFace(const glm::vec2 p, const GeoData * mesh) : PointFace {p, mesh, *mesh->faces_begin()} { }
+
+GeoData::PointFace::PointFace(const glm::vec2 p, const GeoData * mesh, FaceHandle start) :
+ PointFace {p, mesh->findPoint(p, start)}
{
}
-std::pair<float, float>
-GeoData::RayTracer::byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_t axis)
+GeoData::FaceHandle
+GeoData::PointFace::face(const GeoData * mesh, FaceHandle start) const
{
- using Limits = std::numeric_limits<typename glm::vec2::value_type>;
- static_assert(Limits::has_infinity);
- if (d[axis] == 0) {
- return {0, Limits::infinity()};
- }
- else if (p1[axis] > 0) {
- return {1, (std::floor(p0[axis]) + 1.F - p0[axis]) * d[1 - axis]};
+ if (_face.is_valid()) {
+ assert(mesh->triangleContainsPoint(point, _face));
+ return _face;
}
else {
- return {-1, (p0[axis] - std::floor(p0[axis])) * d[1 - axis]};
+ return (_face = mesh->findPoint(point, start));
}
}
-glm::vec2
-GeoData::RayTracer::next()
+GeoData::FaceHandle
+GeoData::PointFace::face(const GeoData * mesh) const
{
- const glm::vec2 cur {p};
+ return face(mesh, *mesh->faces_begin());
+}
+
+namespace {
+ [[nodiscard]] constexpr inline bool
+ pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2)
+ {
+ return (e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x);
+ }
- static constexpr const glm::vec2 m {1, -1};
- const int axis = (error > 0) ? 1 : 0;
- p[axis] += inc[axis];
- error += d[1 - axis] * m[axis];
+ static_assert(pointLeftOfLine({1, 2}, {1, 1}, {2, 2}));
+ static_assert(pointLeftOfLine({2, 1}, {2, 2}, {1, 1}));
+ static_assert(pointLeftOfLine({2, 2}, {1, 2}, {2, 1}));
+ static_assert(pointLeftOfLine({1, 1}, {2, 1}, {1, 2}));
- return cur;
+ [[nodiscard]] constexpr inline bool
+ linesCross(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2)
+ {
+ return pointLeftOfLine(a2, b1, b2) && pointLeftOfLine(a1, b2, b1) && pointLeftOfLine(b1, a1, a2)
+ && pointLeftOfLine(b2, a2, a1);
+ }
+
+ static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1}));
}
-std::optional<glm::vec3>
-GeoData::intersectRay(const Ray & ray) const
+OpenMesh::FaceHandle
+GeoData::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const
{
- if (glm::length(!ray.direction) <= 0) {
- return {};
- }
- RayTracer rt {ray.start / scale, ray.direction};
- while (true) {
- const auto n {rt.next() * scale};
- try {
- const auto point = quad(n);
- for (auto offset : {0U, 1U}) {
- glm::vec2 bary;
- float distance;
- if (glm::intersectRayTriangle(ray.start, ray.direction, point[offset], point[offset + 1],
- point[offset + 2], bary, distance)) {
- return point[offset] + ((point[offset + 1] - point[offset]) * bary[0])
- + ((point[offset + 2] - point[offset]) * bary[1]);
+ ConstFaceVertexIter vertices;
+ while (f.is_valid() && !triangleContainsPoint(p, vertices = cfv_iter(f))) {
+ for (auto next = cfh_iter(f); next.is_valid(); ++next) {
+ f = opposite_face_handle(*next);
+ if (f.is_valid()) {
+ const auto e1 = point(to_vertex_handle(*next));
+ const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next)));
+ if (pointLeftOfLine(p, e1, e2)) {
+ break;
}
}
- }
- catch (std::range_error &) {
- const auto rel = n / !ray.direction;
- if (rel.x > 0 && rel.y > 0) {
- return {};
- }
+ f.reset();
}
}
+ return f;
+}
- return {};
+glm::vec3
+GeoData::positionAt(const PointFace & p) const
+{
+ glm::vec3 out {};
+ Triangle<3> t {this, fv_range(p.face(this))};
+ glm::intersectLineTriangle(p.point ^ 0.F, up, t[0], t[1], t[2], out);
+ return p.point ^ out[0];
}
-unsigned int
-GeoData::at(glm::ivec2 coord) const
+[[nodiscard]] std::optional<glm::vec3>
+GeoData::intersectRay(const Ray & ray) const
{
- if (coord.x < limit.first.x || coord.x > limit.second.x || coord.y < limit.first.y || coord.y > limit.second.y) {
- throw std::range_error {"Coordinates outside GeoData limits"};
- }
- const glm::uvec2 offset = coord - limit.first;
- return offset.x + (offset.y * size.x);
+ return intersectRay(ray, findPoint(ray.start));
}
-unsigned int
-GeoData::at(int x, int y) const
+[[nodiscard]] std::optional<glm::vec3>
+GeoData::intersectRay(const Ray & ray, FaceHandle face) const
{
- return at({x, y});
+ std::optional<glm::vec3> out;
+ walkUntil(PointFace {ray.start, face}, ray.start + (ray.direction * 10000.F), [&out, &ray, this](FaceHandle face) {
+ glm::vec2 bari {};
+ float dist {};
+ Triangle<3> t {this, fv_range(face)};
+ if (glm::intersectRayTriangle(ray.start, ray.direction, t[0], t[1], t[2], bari, dist)) {
+ out = t * bari;
+ return true;
+ }
+ return false;
+ });
+ return out;
}
-GeoData::Limits
-GeoData::getLimit() const
+void
+GeoData::walk(const PointFace & from, const glm::vec2 to, const std::function<void(FaceHandle)> & op) const
{
- return limit;
+ walkUntil(from, to, [&op](const auto & fh) {
+ op(fh);
+ return false;
+ });
}
-float
-GeoData::getScale() const
+void
+GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::function<bool(FaceHandle)> & op) const
+{
+ assert(from.face(this).is_valid()); // TODO replace with a boundary search
+ auto f = from.face(this);
+ FaceHandle previousFace;
+ while (f.is_valid() && !op(f)) {
+ for (auto next = cfh_iter(f); next.is_valid(); ++next) {
+ f = opposite_face_handle(*next);
+ if (f.is_valid() && f != previousFace) {
+ const auto e1 = point(to_vertex_handle(*next));
+ const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next)));
+ if (linesCross(from.point, to, e1, e2)) {
+ previousFace = f;
+ break;
+ }
+ }
+ f.reset();
+ }
+ }
+}
+
+bool
+GeoData::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c)
{
- return scale;
+ const auto det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
+
+ return det * ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0
+ && det * ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x)) >= 0
+ && det * ((a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x)) >= 0;
}
-glm::uvec2
-GeoData::getSize() const
+bool
+GeoData::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const
{
- return size;
+ return triangleContainsPoint(p, cfv_iter(face));
}
-std::span<const GeoData::Node>
-GeoData::getNodes() const
+bool
+GeoData::triangleContainsPoint(const glm::vec2 p, ConstFaceVertexIter vertices) const
{
- return nodes;
+ return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++));
}