// mesh.hpp — STL/OBJ loading, per-vertex normals, and spatial region queries. #pragma once #include #include #include #include "geom.hpp" namespace mesh { struct Mesh { std::vector verts; std::vector> tris; std::vector vnormals; // per-vertex, area-weighted // Lazily built by the discovery engine: std::vector> adj; // vertex adjacency (1-ring, deduped) std::vector curv; // per-vertex surface variation [0..1/3] geom::Vec3 bbMin, bbMax; bool empty() const { return verts.empty() || tris.empty(); } void computeNormals(); void computeBounds(); void buildAdjacency(); // fills adj void computeCurvature(); // fills curv (builds adj if needed) double bboxDiag() const; }; // Load from a file path. Format inferred from extension (.stl / .obj), with a // content sniff fallback. Returns false on failure and fills `err`. bool load(const std::string& path, Mesh& out, std::string& err); // Load from an in-memory buffer (format hinted by `ext`, e.g. "stl"/"obj"). bool loadFromMemory(const std::string& data, const std::string& ext, Mesh& out, std::string& err); // Load only points from an in-memory buffer — accepts face-less files (raw // scanner point clouds, typically PLY). Faces, if present, are ignored. bool loadPointsFromMemory(const std::string& data, const std::string& ext, std::vector& pts, std::string& err); // Indices of vertices within `radius` of `center`. std::vector verticesInRadius(const Mesh& m, const geom::Vec3& center, double radius); // Global winding polarity from the signed volume: +1 if triangle winding (and thus // the vertex normals) faces outward, -1 if the mesh is inside-out, 0 if it is too // open/flat for the volume test to mean anything. int windingOrientation(const Mesh& m); // Write a binary STL of only the triangles whose centroid lies within [lo,hi] (a crop // box). Returns the number of triangles written (0 => nothing in the box / write fail). int writeSTLRegion(const std::string& path, const Mesh& m, const geom::Vec3& lo, const geom::Vec3& hi); // Binary STL in memory, for HTTP responses. The indexed overload writes only the // selected triangle ids and preserves their original winding. std::string binarySTL(const Mesh& m); std::string binarySTL(const Mesh& m, const std::vector& triangleIds); } // namespace mesh