// gentest.cpp — generate realistic watertight test parts (plate + varied holes), // tessellate with OpenCASCADE, optionally add scan-like Gaussian noise, and write // a binary STL plus a ground-truth JSON for the validation harness. // // gen_test [noiseSigmaMM] [seed] // // presets: bracket | varied | dense #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "builder.hpp" #include "solid.hpp" using geom::Vec3; struct GTHole { double x, y, diameter; }; static builder::BuildParams makePreset(const std::string& name, std::vector& gt) { builder::BuildParams bp; bp.datum.origin = {0, 0, 0}; bp.datum.normal = {0, 0, 1}; bp.datum.u = {1, 0, 0}; bp.datum.v = {0, 1, 0}; bp.thickness = 6; bp.margin = 8; auto add = [&](double x, double y, double dia, bool through = true, double depth = 0) { builder::Hole h; h.center = {x, y, 0}; h.axis = {0, 0, 1}; h.diameter = dia; h.through = through; h.depth = depth; bp.holes.push_back(h); gt.push_back({x, y, dia}); }; if (name == "bracket") { // 4 corner M5-clearance holes + a central Ø12 add(-22, -13, 5.5); add(22, -13, 5.5); add(22, 13, 5.5); add(-22, 13, 5.5); add(0, 0, 12); } else if (name == "varied") { // a spread of common sizes: tap-drills + clearance add(-25, 0, 3.4); add(-10, 0, 5.0); add(8, 0, 8.5); add(28, 0, 10.2); add(0, 18, 6.0); bp.thickness = 8; } else { // dense for (int i = 0; i < 6; ++i) add(-30 + i * 12, -8, 4.2 + (i % 3)); for (int i = 0; i < 6; ++i) add(-30 + i * 12, 8, 6.0); bp.thickness = 5; } return bp; } int main(int argc, char** argv) { if (argc < 4) { fprintf(stderr, "usage: gen_test [noiseSigmaMM] [seed] [maxEdgeMM]\n"); return 2; } std::string preset = argv[1], stlPath = argv[2], gtPath = argv[3]; double sigma = argc > 4 ? atof(argv[4]) : 0.0; unsigned seed = argc > 5 ? (unsigned)atoi(argv[5]) : 1234u; double maxEdgeArg = argc > 6 ? atof(argv[6]) : 0.6; // densify target; larger => smaller files std::vector gt; builder::BuildParams bp = makePreset(preset, gt); int holesCut = 0; std::string err; TopoDS_Shape shape = builder::buildSolid(bp, holesCut, err); if (shape.IsNull()) { fprintf(stderr, "build failed: %s\n", err.c_str()); return 1; } // tessellate finely (conforming Delaunay — no T-junctions) to resemble a dense // scan. Fine angular deflection puts many segments around every hole wall. BRepMesh_IncrementalMesh mesher(shape, 0.10 /*linear mm*/, Standard_False, 0.06 /*angular rad*/, Standard_True); mesher.Perform(); // collect triangles in world coordinates std::vector> tris; for (TopExp_Explorer ex(shape, TopAbs_FACE); ex.More(); ex.Next()) { TopoDS_Face face = TopoDS::Face(ex.Current()); TopLoc_Location loc; Handle(Poly_Triangulation) tri = BRep_Tool::Triangulation(face, loc); if (tri.IsNull()) continue; const gp_Trsf& trsf = loc.Transformation(); bool rev = (face.Orientation() == TopAbs_REVERSED); for (int i = 1; i <= tri->NbTriangles(); ++i) { int a, b, c; tri->Triangle(i).Get(a, b, c); if (rev) std::swap(b, c); gp_Pnt pa = tri->Node(a).Transformed(trsf); gp_Pnt pb = tri->Node(b).Transformed(trsf); gp_Pnt pc = tri->Node(c).Transformed(trsf); tris.push_back({Vec3{pa.X(), pa.Y(), pa.Z()}, Vec3{pb.X(), pb.Y(), pb.Z()}, Vec3{pc.X(), pc.Y(), pc.Z()}}); } } // Densify to a scan-like resolution. Recursively split each triangle 1->4 // until its longest edge is below the target. (Edge-length recursion keeps the // count reasonable; the discovery engine tolerates the resulting T-junctions // because cylinder detection is component-based per wall.) { const double maxEdge = maxEdgeArg; auto longest = [](const std::array& t) { return std::max({geom::norm(t[1] - t[0]), geom::norm(t[2] - t[1]), geom::norm(t[0] - t[2])}); }; std::vector> out, stack(tris.begin(), tris.end()); while (!stack.empty()) { auto t = stack.back(); stack.pop_back(); if (longest(t) <= maxEdge) { out.push_back(t); continue; } Vec3 m01 = (t[0] + t[1]) * 0.5, m12 = (t[1] + t[2]) * 0.5, m20 = (t[2] + t[0]) * 0.5; stack.push_back({t[0], m01, m20}); stack.push_back({m01, t[1], m12}); stack.push_back({m20, m12, t[2]}); stack.push_back({m01, m12, m20}); } tris.swap(out); } // position-consistent Gaussian noise: coincident vertices (shared corners/edges) // get the SAME offset so the welder still merges them and topology survives. if (sigma > 0) { std::mt19937 rng(seed); std::normal_distribution g(0.0, sigma); std::map, Vec3> offsets; const double q = 1e-4; // grouping quantum (mm) auto key = [&](const Vec3& p) { return std::array{(int64_t)llround(p.x / q), (int64_t)llround(p.y / q), (int64_t)llround(p.z / q)}; }; auto offsetFor = [&](const Vec3& p) -> Vec3 { auto k = key(p); auto it = offsets.find(k); if (it != offsets.end()) return it->second; Vec3 o{g(rng), g(rng), g(rng)}; offsets.emplace(k, o); return o; }; for (auto& t : tris) for (auto& vtx : t) vtx = vtx + offsetFor(vtx); } // write binary STL std::ofstream f(stlPath, std::ios::binary); if (!f) { fprintf(stderr, "cannot write %s\n", stlPath.c_str()); return 1; } char header[80] = {0}; std::snprintf(header, 80, "scan2cad gen_test %s sigma=%.3f", preset.c_str(), sigma); f.write(header, 80); uint32_t n = (uint32_t)tris.size(); f.write(reinterpret_cast(&n), 4); for (auto& t : tris) { Vec3 e1 = t[1] - t[0], e2 = t[2] - t[0]; Vec3 nrm = geom::normalize(geom::cross(e1, e2)); float fn[3] = {(float)nrm.x, (float)nrm.y, (float)nrm.z}; f.write(reinterpret_cast(fn), 12); for (auto& v : t) { float fv[3] = {(float)v.x, (float)v.y, (float)v.z}; f.write(reinterpret_cast(fv), 12); } uint16_t attr = 0; f.write(reinterpret_cast(&attr), 2); } f.close(); // write ground-truth JSON std::ofstream gj(gtPath); gj << "{\n \"preset\": \"" << preset << "\",\n \"noiseSigmaMM\": " << sigma << ",\n \"thickness\": " << bp.thickness << ",\n \"datumNormal\": [0,0,1],\n \"triangles\": " << tris.size() << ",\n \"holes\": [\n"; for (size_t i = 0; i < gt.size(); ++i) gj << " {\"x\": " << gt[i].x << ", \"y\": " << gt[i].y << ", \"diameter\": " << gt[i].diameter << "}" << (i + 1 < gt.size() ? "," : "") << "\n"; gj << " ]\n}\n"; printf("wrote %s (%zu triangles, %zu holes, sigma=%.3f) + %s\n", stlPath.c_str(), tris.size(), gt.size(), sigma, gtPath.c_str()); return 0; }