// meshedit.hpp — topology-aware repair, reduction, and surface deviation. #pragma once #include #include #include #include #include "mesh.hpp" namespace meshedit { struct Hole { std::vector vertices; // ordered so patch faces oppose the old boundary faces int nedges = 0; geom::Vec3 center; double radius = 0; }; struct Component { int ntris = 0; double area = 0; }; struct Analysis { int boundaryEdges = 0; int nonManifoldEdges = 0; bool watertight = false; std::vector components; // sorted by ntris descending std::vector holes; // sorted by nedges descending std::vector triangleComponent; // component id for each input triangle }; Analysis analyze(const mesh::Mesh& m); // Returns the surviving triangle count. Targets below four triangles are clamped. std::size_t decimate(mesh::Mesh& m, std::size_t targetTriangles); struct DeviationStats { double max = 0; double rms = 0; double pctWithin = 0; std::vector distances; }; // Compact uniform-grid index. Triangles are assigned to every cell touched by // their AABB; the CSR layout avoids millions of small allocations on scan meshes. struct TriangleGrid { geom::Vec3 origin; double cellSize = 1; int nx = 1, ny = 1, nz = 1; const mesh::Mesh* source = nullptr; std::vector offsets; std::vector triangleIds; TriangleGrid() = default; explicit TriangleGrid(const mesh::Mesh& m) { build(m); } void build(const mesh::Mesh& m); bool closestPoint(const geom::Vec3& p, geom::Vec3& point, geom::Vec3& normal, double& distance) const; double closestDistance(const geom::Vec3& p) const; }; DeviationStats deviationStats(const mesh::Mesh& edited, const mesh::Mesh& original, double tolerance); DeviationStats deviationStats(const mesh::Mesh& edited, const TriangleGrid& original, double tolerance); // Returns one when a loop was filled and zero when it could not be triangulated. int fillHole(mesh::Mesh& m, const Hole& hole, const std::string& method = "auto"); int fillHoles(mesh::Mesh& m, const std::vector& holes, const std::string& method = "auto"); int fillAllHoles(mesh::Mesh& m, const std::string& method = "auto", int maxEdges = 0); struct RemoveOptions { std::vector ids; int minTris = -1; double minArea = -1; bool keepLargest = false; }; // Returns the number of connected components removed. int removeComponents(mesh::Mesh& m, const Analysis& analysis, const RemoveOptions& options); int removeComponents(mesh::Mesh& m, const RemoveOptions& options); } // namespace meshedit