summaryrefslogtreecommitdiff
path: root/game/geoData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/geoData.cpp')
-rw-r--r--game/geoData.cpp51
1 files changed, 33 insertions, 18 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index 4291a64..b886efd 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -35,16 +35,16 @@ GeoData::loadFromAsciiGrid(const std::filesystem::path & input)
std::vector<VertexHandle> vertices;
vertices.reserve(ncols * nrows);
GeoData mesh;
- mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits<GlobalDistance>::max()};
- mesh.upperExtent = {xllcorner + (cellsize * (ncols - 1)), yllcorner + (cellsize * (nrows - 1)),
- std::numeric_limits<GlobalDistance>::min()};
+ mesh.extents = {{xllcorner, yllcorner, std::numeric_limits<GlobalDistance>::max()},
+ {xllcorner + (cellsize * (ncols - 1)), yllcorner + (cellsize * (nrows - 1)),
+ std::numeric_limits<GlobalDistance>::min()}};
for (size_t row = 0; row < nrows; ++row) {
for (size_t col = 0; col < ncols; ++col) {
float heightf = 0;
f >> heightf;
const auto height = static_cast<GlobalDistance>(std::round(heightf * 1000.F));
- mesh.upperExtent.z = std::max(mesh.upperExtent.z, height);
- mesh.lowerExtent.z = std::min(mesh.lowerExtent.z, height);
+ mesh.extents.max.z = std::max(mesh.extents.max.z, height);
+ mesh.extents.min.z = std::min(mesh.extents.min.z, height);
vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height}));
}
}
@@ -78,8 +78,7 @@ GeoData::createFlat(GlobalPosition2D lower, GlobalPosition2D upper, GlobalDistan
assert((upper - lower) % GRID_SIZE == GlobalPosition2D {});
GeoData mesh;
- mesh.lowerExtent = {lower, h};
- mesh.upperExtent = {upper, h};
+ mesh.extents = {{lower, h}, {upper, h}};
std::vector<VertexHandle> vertices;
for (GlobalDistance row = lower.x; row <= upper.x; row += GRID_SIZE) {
@@ -120,13 +119,11 @@ GeoData::intersectRay(const Ray<GlobalPosition3D> & ray, FaceHandle face) const
{
GeoData::IntersectionResult out;
walkUntil(PointFace {ray.start, face},
- ray.start.xy() + (ray.direction.xy() * RelativePosition2D(upperExtent.xy() - lowerExtent.xy())),
+ ray.start.xy() + (ray.direction.xy() * ::difference(extents.max.xy(), extents.min.xy())),
[&out, &ray, this](const auto & step) {
- BaryPosition bari {};
- RelativeDistance dist {};
const auto t = triangle<3>(step.current);
- if (ray.intersectTriangle(t.x, t.y, t.z, bari, dist)) {
- out.emplace(t * bari, step.current);
+ if (const auto inter = ray.intersectTriangle(t.x, t.y, t.z)) {
+ out.emplace(t * inter->bary, step.current);
return true;
}
return false;
@@ -287,9 +284,27 @@ GeoData::updateAllVertexNormals(const R & range)
void
GeoData::updateVertexNormal(VertexHandle vertex)
{
- Normal3D n;
- calc_vertex_normal_correct(vertex, n);
- set_normal(vertex, glm::normalize(n));
+ Normal3D normal {};
+ { // Lifted from calc_vertex_normal_correct in PolyMeshT_impl.hh but doesn't use Scalar to normalise
+ ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(vertex);
+ if (!cvih_it.is_valid()) { // don't crash on isolated vertices
+ return;
+ }
+ Normal in_he_vec;
+ calc_edge_vector(*cvih_it, in_he_vec);
+ for (; cvih_it.is_valid(); ++cvih_it) { // calculates the sector normal defined by cvih_it and adds it to normal
+ if (this->is_boundary(*cvih_it)) {
+ continue;
+ }
+ HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it));
+ Normal out_he_vec;
+ calc_edge_vector(out_heh, out_he_vec);
+ normal += cross(in_he_vec, out_he_vec); // sector area is taken into account
+ in_he_vec = out_he_vec;
+ in_he_vec *= -1; // change the orientation
+ }
+ } // End lift
+ set_normal(vertex, glm::normalize(normal));
}
OpenMesh::VertexHandle
@@ -325,9 +340,9 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip, const
if (triangleStrip.size() < 3) {
return {};
}
- const auto stripMinMax = std::ranges::minmax(triangleStrip, {}, &GlobalPosition3D::z);
- lowerExtent.z = std::min(upperExtent.z, stripMinMax.min.z);
- upperExtent.z = std::max(upperExtent.z, stripMinMax.max.z);
+ for (const auto & vertex : triangleStrip) {
+ extents += vertex;
+ }
class SetHeights {
public: