#include "prim.hpp" #include #include #include #include #include using geom::dot; using geom::norm; using geom::normalize; using geom::Vec3; namespace { constexpr double PI = 3.14159265358979323846; std::mt19937_64 rng(501); std::normal_distribution gauss(0.0, 0.02); int failures = 0; Vec3 noisy(const Vec3& p) { return {p.x + gauss(rng), p.y + gauss(rng), p.z + gauss(rng)}; } double angleDeg(const Vec3& a, const Vec3& b, bool unsignedAxis = true) { double c = dot(normalize(a), normalize(b)); if (unsignedAxis) c = std::fabs(c); c = std::max(-1.0, std::min(1.0, c)); return std::acos(c) * 180.0 / PI; } void report(const std::string& name, bool ok, const std::string& detail = {}) { std::printf("%s %-28s%s%s\n", ok ? "PASS" : "FAIL", name.c_str(), detail.empty() ? "" : " ", detail.c_str()); if (!ok) ++failures; } void planeCase() { const Vec3 origin{4, -3, 8}; const Vec3 normal = normalize({0.2, -0.35, 0.915}); Vec3 u, v; geom::basisFromNormal(normal, u, v); std::vector pts, nrm; for (int iy = 0; iy < 75; ++iy) for (int ix = 0; ix < 300; ++ix) { double x = (ix - 149.5) * 0.3; double y = (iy - 37.0) * 0.3; pts.push_back(noisy(origin + u * x + v * y)); nrm.push_back(normal); } auto f = prim::fitPlane(pts, nrm); double originErr = std::fabs(dot(f.origin - origin, normal)); bool ok = f.ok && angleDeg(f.normal, normal) < 0.5 && originErr < 0.05 && f.rms > 0.015 && f.rms < 0.027 && f.npts == (int)pts.size(); char d[180]; std::snprintf(d, sizeof d, "angle=%.4f deg offset=%.4f rms=%.4f n=%d", angleDeg(f.normal, normal), originErr, f.rms, f.npts); report("plane patch", ok, d); } void cylinderCase(const std::string& name, double arcDeg) { const Vec3 center{2, -5, 7}; const Vec3 axis = normalize({0.23, -0.31, 0.922}); Vec3 u, v; geom::basisFromNormal(axis, u, v); const double radius = 30.0, height = 30.0; int nz = 101; int na = std::max(210, (int)std::lround(radius * arcDeg * PI / 180.0 / 0.3)); std::vector pts, nrm; pts.reserve((size_t)nz * na); for (int iz = 0; iz < nz; ++iz) { double h = -height * 0.5 + height * iz / (nz - 1); for (int ia = 0; ia < na; ++ia) { double phi = -arcDeg * PI / 360.0 + arcDeg * PI / 180.0 * ia / (na - 1); Vec3 radial = u * std::cos(phi) + v * std::sin(phi); pts.push_back(noisy(center + axis * h + radial * radius)); nrm.push_back(radial); } } auto f = prim::fitCylinder(pts, nrm); Vec3 off = f.center - center; double axisOffset = norm(off - axis * dot(off, axis)); bool ok = f.ok && angleDeg(f.axis, axis) < 0.5 && std::fabs(f.radius - radius) < radius * 0.005 && axisOffset < 0.05 && f.rms > 0.014 && f.rms < 0.03; char d[200]; std::snprintf(d, sizeof d, "axis=%.4f deg dr=%.4f offset=%.4f rms=%.4f n=%d", angleDeg(f.axis, axis), f.radius - radius, axisOffset, f.rms, f.npts); report(name, ok, d); } void coneCase(double halfAngleDeg) { const Vec3 apex{-4, 3, -2}; const Vec3 axis = normalize({-0.27, 0.18, 0.946}); Vec3 u, v; geom::basisFromNormal(axis, u, v); double theta = halfAngleDeg * PI / 180.0; double hLo = 15.0, hHi = 35.0; double meanR = 25.0 * std::tan(theta); int na = std::min(800, std::max(300, (int)std::lround(2 * PI * meanR / 0.3))); int nh = 100; std::vector pts, nrm; pts.reserve((size_t)na * nh); for (int ih = 0; ih < nh; ++ih) { double h = hLo + (hHi - hLo) * ih / (nh - 1); for (int ia = 0; ia < na; ++ia) { double phi = 2 * PI * ia / na; Vec3 radial = u * std::cos(phi) + v * std::sin(phi); pts.push_back(noisy(apex + axis * h + radial * (h * std::tan(theta)))); nrm.push_back(normalize(radial * std::cos(theta) - axis * std::sin(theta))); } } auto f = prim::fitCone(pts, nrm); bool ok = f.ok && angleDeg(f.axis, axis, false) < 0.5 && std::fabs(f.halfAngleDeg - halfAngleDeg) < 0.3 && norm(f.apex - apex) < 0.05 && f.rms > 0.014 && f.rms < 0.03 && std::fabs(f.h1 - hLo) < 0.15 && std::fabs(f.h2 - hHi) < 0.15; char d[220]; std::snprintf(d, sizeof d, "axis=%.4f deg da=%.4f apex=%.4f rms=%.4f h=[%.3f,%.3f] n=%d%s", f.ok ? angleDeg(f.axis, axis, false) : 999.0, f.halfAngleDeg - halfAngleDeg, norm(f.apex - apex), f.rms, f.h1, f.h2, f.npts, f.ok ? "" : f.err.c_str()); report("cone " + std::to_string((int)halfAngleDeg) + " deg", ok, d); } void cylindricalConeGuard() { const Vec3 apex{0, 0, 0}, axis{0, 0, 1}; const double theta = 1.0 * PI / 180.0; std::vector pts, nrm; for (int ih = 0; ih < 80; ++ih) { double h = 200 + ih * 0.25; for (int ia = 0; ia < 300; ++ia) { double phi = 2 * PI * ia / 300; Vec3 r{std::cos(phi), std::sin(phi), 0}; pts.push_back(noisy(apex + axis * h + r * (h * std::tan(theta)))); nrm.push_back(r * std::cos(theta) - axis * std::sin(theta)); } } auto f = prim::fitCone(pts, nrm); report("cone cylindrical guard", !f.ok && f.err == "looks cylindrical - fit a cylinder", f.err); } void sphereCase(const std::string& name, bool cap) { const Vec3 center{6, -8, 3}; const double radius = 30.0; const Vec3 capAxis = normalize({0.3, 0.4, 0.866}); Vec3 u, v; geom::basisFromNormal(capAxis, u, v); int n = cap ? 24000 : 70000; double z0 = cap ? std::cos(45.0 * PI / 180.0) : -1.0; std::vector pts; pts.reserve(n); const double golden = PI * (3.0 - std::sqrt(5.0)); for (int i = 0; i < n; ++i) { double z = z0 + (1.0 - z0) * (i + 0.5) / n; double phi = golden * i; double rr = std::sqrt(std::max(0.0, 1.0 - z * z)); Vec3 d = capAxis * z + u * (rr * std::cos(phi)) + v * (rr * std::sin(phi)); pts.push_back(noisy(center + d * radius)); } auto f = prim::fitSphere(pts); bool ok = f.ok && norm(f.center - center) < 0.05 && std::fabs(f.radius - radius) < radius * 0.005 && f.rms > 0.014 && f.rms < 0.03 && f.npts == n; char d[180]; std::snprintf(d, sizeof d, "center=%.4f dr=%.4f rms=%.4f n=%d", norm(f.center - center), f.radius - radius, f.rms, f.npts); report(name, ok, d); } bool close(const Vec3& a, const Vec3& b, double eps = 1e-8) { return norm(a - b) < eps; } void constructionCases() { auto pp = prim::lineFromPlanes({0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 1, 0}); report("construct plane-plane", pp.ok && close(pp.p, {0, 0, 0}) && angleDeg(pp.d, {0, 0, 1}) < 1e-8); auto ax = prim::axisLine({1, 2, 3}, {0, 0, 2}); report("construct axis", ax.ok && close(ax.p, {1, 2, 3}) && close(ax.d, {0, 0, 1})); auto av = prim::averageLines( {prim::axisLine({-1, 0, 0}, {0, 0, 1}), prim::axisLine({1, 0, 0}, {0, 0.02, 1})}); report("construct average", av.ok && angleDeg(av.d, {0, 0.01, 1}) < 0.05 && std::fabs(av.p.x) < 1e-8); auto projected = prim::projectLine(prim::axisLine({1, 2, 3}, {1, 0, 1}), {0, 0, 0}, {0, 0, 1}); report("construct project", projected.ok && close(projected.p, {1, 2, 0}) && close(projected.d, {1, 0, 0})); auto lp = prim::linePlanePoint(prim::axisLine({1, 2, 3}, {0, 0, 1}), {0, 0, 5}, {0, 0, 1}); report("construct line-plane", lp.ok && close(lp.p, {1, 2, 5})); auto p3 = prim::threePlanesPoint({Vec3{2, 0, 0}, Vec3{0, 3, 0}, Vec3{0, 0, 4}}, {Vec3{1, 0, 0}, Vec3{0, 1, 0}, Vec3{0, 0, 1}}); report("construct three planes", p3.ok && close(p3.p, {2, 3, 4})); auto mp = prim::midpointPoints({0, 2, 4}, {2, 4, 8}); auto ml = prim::midpointLine(prim::axisLine({3, 4, 5}, {1, 0, 0})); auto sc = prim::sphereCenter({7, 8, 9}); auto cp = prim::centroidPoint({Vec3{0, 0, 0}, Vec3{3, 6, 9}}); report("construct midpoints/centers", mp.ok && close(mp.p, {1, 3, 6}) && ml.ok && close(ml.p, {3, 4, 5}) && sc.ok && close(sc.p, {7, 8, 9}) && cp.ok && close(cp.p, {1.5, 3, 4.5})); auto nearPlanes = prim::lineFromPlanes({0, 0, 0}, {0, 0, 1}, {0, 0, 1}, {0.01, 0, 1}); auto spread = prim::averageLines( {prim::axisLine({0, 0, 0}, {0, 0, 1}), prim::axisLine({1, 0, 0}, {0.2, 0, 1})}); auto collapse = prim::projectLine(prim::axisLine({0, 0, 0}, {0, 0, 1}), {0, 0, 2}, {0, 0, 1}); auto parallel = prim::linePlanePoint(prim::axisLine({0, 0, 1}, {1, 0, 0}), {0, 0, 0}, {0, 0, 1}); auto singular = prim::threePlanesPoint( {Vec3{0, 0, 0}, Vec3{0, 0, 1}, Vec3{0, 0, 2}}, {Vec3{0, 0, 1}, Vec3{0, 0, 1}, Vec3{1, 0, 0}}); report("construct degenerates", !nearPlanes.ok && !spread.ok && !collapse.ok && !parallel.ok && !singular.ok); } void selectionCases() { mesh::Mesh m; m.verts = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, -1}, {1, 1, -1}}; m.tris = {{0, 1, 2}, {1, 3, 2}, {1, 4, 3}, {4, 5, 3}}; m.vnormals = {{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {1, 0, 0}, {1, 0, 0}}; std::vector selected; prim::wandSelect(m, {0.1, 0.1, 0}, 15, selected); bool wand = selected.size() == 4 && !m.adj.empty(); prim::paintSelect(m, {{1, 0, -1}}, 0.1, selected); bool paint = selected.size() == 1 && selected[0] == 4; report("wand/paint selection", wand && paint); } void bracketSmoke() { mesh::Mesh m; std::string err; if (!mesh::load("testdata/bracket.stl", m, err)) { report("bracket mesh smoke", false, err); return; } std::printf("INFO bracket bbox [%.3f %.3f %.3f] [%.3f %.3f %.3f], %zu verts\n", m.bbMin.x, m.bbMin.y, m.bbMin.z, m.bbMax.x, m.bbMax.y, m.bbMax.z, m.verts.size()); auto fitAt = [&](const Vec3& seed) { std::vector ids; prim::wandSelect(m, seed, 15, ids); std::vector points, normals; points.reserve(ids.size()); normals.reserve(ids.size()); for (int i : ids) { points.push_back(m.verts[i]); normals.push_back(m.vnormals[i]); } return prim::fitPlane(points, normals); }; auto top = fitAt({10, 5, m.bbMax.z}); auto side = fitAt({m.bbMax.x, 0, (m.bbMin.z + m.bbMax.z) * 0.5}); auto line = prim::lineFromPlanes(top.origin, top.normal, side.origin, side.normal); char detail[200]; std::snprintf(detail, sizeof detail, "top=%d rms=%.6f side=%d rms=%.6f line=%d", top.npts, top.rms, side.npts, side.rms, line.ok); report("bracket wand/fit/construct", top.ok && side.ok && line.ok, detail); } } // namespace int main(int argc, char** argv) { planeCase(); cylinderCase("cylinder 120 deg arc", 120.0); cylinderCase("cylinder full wall", 360.0); coneCase(10); coneCase(30); coneCase(60); cylindricalConeGuard(); sphereCase("sphere full", false); sphereCase("sphere 45 deg cap", true); constructionCases(); selectionCases(); if (argc > 1 && std::string(argv[1]) == "--bracket") bracketSmoke(); std::printf("\n%s: %d failure(s)\n", failures ? "FAIL" : "ALL PASS", failures); return failures ? 1 : 0; }