// mesh_test.cpp — destructive end-to-end mesh editing smoke test. #include #include #include #include #include #include #include #include #include #include #include "mesh.hpp" #include "meshedit.hpp" namespace { using Clock = std::chrono::steady_clock; using geom::Vec3; std::uint64_t edgeKey(int a, int b) { std::uint32_t lo = (std::uint32_t)std::min(a, b); std::uint32_t hi = (std::uint32_t)std::max(a, b); return ((std::uint64_t)lo << 32) | hi; } double elapsed(Clock::time_point t) { return std::chrono::duration(Clock::now() - t).count(); } int punchPatches(mesh::Mesh& m) { if (m.tris.size() < 100) return 0; std::mt19937 rng(501); std::uniform_int_distribution pick(0, (int)m.tris.size() - 1); std::vector seeds; while (seeds.size() < 3) { int s = pick(rng); bool far = true; for (int old : seeds) if (std::abs(s - old) < (int)m.tris.size() / 20) far = false; if (far) seeds.push_back(s); } std::unordered_map seedEdges; for (int seed : seeds) { const auto& t = m.tris[seed]; seedEdges[edgeKey(t[0], t[1])] = seed; seedEdges[edgeKey(t[1], t[2])] = seed; seedEdges[edgeKey(t[2], t[0])] = seed; } std::vector erase(m.tris.size(), 0); for (int seed : seeds) erase[seed] = 1; for (int ti = 0; ti < (int)m.tris.size(); ++ti) { const auto& t = m.tris[ti]; if (seedEdges.count(edgeKey(t[0], t[1])) || seedEdges.count(edgeKey(t[1], t[2])) || seedEdges.count(edgeKey(t[2], t[0]))) erase[ti] = 1; } int removed = 0; std::vector> keep; keep.reserve(m.tris.size()); for (int ti = 0; ti < (int)m.tris.size(); ++ti) { if (erase[ti]) { ++removed; } else { keep.push_back(m.tris[ti]); } } m.tris.swap(keep); return removed; } void appendFloatingCube(mesh::Mesh& m) { double diag = std::max(m.bboxDiag(), 1.0); double s = diag * 0.004; Vec3 c{m.bbMax.x + diag * 0.1, m.bbMax.y + diag * 0.1, m.bbMax.z + diag * 0.1}; int v = (int)m.verts.size(); m.verts.insert(m.verts.end(), {{c.x - s, c.y - s, c.z - s}, {c.x + s, c.y - s, c.z - s}, {c.x + s, c.y + s, c.z - s}, {c.x - s, c.y + s, c.z - s}, {c.x - s, c.y - s, c.z + s}, {c.x + s, c.y - s, c.z + s}, {c.x + s, c.y + s, c.z + s}, {c.x - s, c.y + s, c.z + s}}); const int f[][3] = {{0, 2, 1}, {0, 3, 2}, {4, 5, 6}, {4, 6, 7}, {0, 1, 5}, {0, 5, 4}, {1, 2, 6}, {1, 6, 5}, {2, 3, 7}, {2, 7, 6}, {3, 0, 4}, {3, 4, 7}}; for (const auto& t : f) m.tris.push_back({v + t[0], v + t[1], v + t[2]}); } } // namespace int main(int argc, char** argv) { if (argc != 2) { std::fprintf(stderr, "usage: mesh_test input.stl\n"); return 2; } mesh::Mesh original; std::string err; auto totalStart = Clock::now(); if (!mesh::load(argv[1], original, err)) { std::fprintf(stderr, "FAIL load: %s\n", err.c_str()); return 1; } std::fprintf(stderr, "loaded %zu triangles in %.2fs\n", original.tris.size(), elapsed(totalStart)); auto t = Clock::now(); meshedit::Analysis initial = meshedit::analyze(original); double analyzeOriginalSec = elapsed(t); std::fprintf(stderr, "analyzed original in %.2fs: watertight=%d boundary=%d nonmanifold=%d " "components=%zu holes=%zu\n", analyzeOriginalSec, initial.watertight, initial.boundaryEdges, initial.nonManifoldEdges, initial.components.size(), initial.holes.size()); mesh::Mesh edited; edited.verts = original.verts; edited.tris = original.tris; edited.bbMin = original.bbMin; edited.bbMax = original.bbMax; edited.computeNormals(); int punched = punchPatches(edited); appendFloatingCube(edited); edited.computeNormals(); edited.computeBounds(); t = Clock::now(); meshedit::Analysis damaged = meshedit::analyze(edited); double analyzeDamagedSec = elapsed(t); std::fprintf(stderr, "analyzed damaged mesh in %.2fs: boundary=%d components=%zu holes=%zu\n", analyzeDamagedSec, damaged.boundaryEdges, damaged.components.size(), damaged.holes.size()); meshedit::RemoveOptions ro; ro.keepLargest = true; t = Clock::now(); int removed = meshedit::removeComponents(edited, damaged, ro); double removeSec = elapsed(t); std::fprintf(stderr, "removed components in %.2fs\n", removeSec); meshedit::Analysis afterRemove = meshedit::analyze(edited); std::fprintf(stderr, "after removal: %zu holes", afterRemove.holes.size()); for (std::size_t i = 0; i < std::min(10, afterRemove.holes.size()); ++i) std::fprintf(stderr, " %d", afterRemove.holes[i].nedges); std::fprintf(stderr, "\n"); t = Clock::now(); int filled = meshedit::fillAllHoles(edited, "auto"); double fillSec = elapsed(t); std::fprintf(stderr, "filled holes in %.2fs\n", fillSec); std::size_t preDecimate = edited.tris.size(); t = Clock::now(); meshedit::decimate(edited, preDecimate / 2); double decimateSec = elapsed(t); std::fprintf(stderr, "decimated in %.2fs\n", decimateSec); t = Clock::now(); meshedit::Analysis final = meshedit::analyze(edited); double analyzeFinalSec = elapsed(t); std::fprintf(stderr, "analyzed final mesh in %.2fs\n", analyzeFinalSec); t = Clock::now(); meshedit::DeviationStats deviation = meshedit::deviationStats(edited, original, original.bboxDiag() * 0.001); double deviationSec = elapsed(t); std::fprintf(stderr, "measured deviation in %.2fs\n", deviationSec); double rmsLimit = original.bboxDiag() * 0.001; bool topologyOK = !initial.watertight || final.watertight; bool rmsOK = std::isfinite(deviation.rms) && deviation.rms < rmsLimit; bool operationsOK = punched > 0 && removed >= 1 && filled >= 1 && edited.tris.size() <= preDecimate / 2 + 2; bool pass = topologyOK && rmsOK && operationsOK; std::printf( "%s %s\n" " original: %zu verts %zu tris watertight=%s analyze=%.2fs\n" " damaged: punched=%d components=%zu holes=%zu boundary=%d analyze=%.2fs\n" " repair: removed=%d (%.2fs) filled=%d (%.2fs)\n" " decimate: %zu -> %zu tris (%.2fs)\n" " final: watertight=%s boundary=%d nonmanifold=%d components=%zu analyze=%.2fs\n" " deviation: rms=%.9g max=%.9g limit=%.9g within=%.2f%% (%.2fs)\n" " total: %.2fs\n", pass ? "PASS" : "FAIL", argv[1], original.verts.size(), original.tris.size(), initial.watertight ? "true" : "false", analyzeOriginalSec, punched, damaged.components.size(), damaged.holes.size(), damaged.boundaryEdges, analyzeDamagedSec, removed, removeSec, filled, fillSec, preDecimate, edited.tris.size(), decimateSec, final.watertight ? "true" : "false", final.boundaryEdges, final.nonManifoldEdges, final.components.size(), analyzeFinalSec, deviation.rms, deviation.max, rmsLimit, deviation.pctWithin, deviationSec, elapsed(totalStart)); return pass ? 0 : 1; }