#include "surface.hpp" #include #include #include #include #include #include #include #include #include #include namespace { using geom::dot; using geom::Vec3; int failures = 0; void check(const std::string& name, bool passed, const std::string& detail) { std::printf("%-34s %s %s\n", name.c_str(), passed ? "PASS" : "FAIL", detail.c_str()); if (!passed) ++failures; } mesh::Mesh gridMesh(int nx, int ny, double x0, double y0, double spacing, const std::function& height) { mesh::Mesh mesh; mesh.verts.reserve((std::size_t)nx * ny); for (int j = 0; j < ny; ++j) for (int i = 0; i < nx; ++i) { double x = x0 + i * spacing, y = y0 + j * spacing; mesh.verts.push_back({x, y, height(x, y)}); } for (int j = 0; j + 1 < ny; ++j) for (int i = 0; i + 1 < nx; ++i) { int a = j * nx + i, b = a + 1, c = a + nx, d = c + 1; mesh.tris.push_back({a, b, d}); mesh.tris.push_back({a, d, c}); } mesh.computeBounds(); mesh.computeNormals(); return mesh; } std::vector allVertices(const mesh::Mesh& mesh) { std::vector ids(mesh.verts.size()); for (int i = 0; i < (int)ids.size(); ++i) ids[i] = i; return ids; } std::string stats(const surface::Fit& fit) { char text[160]; std::snprintf(text, sizeof text, "%dx%d rms=%.5f max=%.5f within=%.3f%%", fit.nu, fit.nv, fit.rms, fit.max, fit.pctWithin); return text; } } // namespace int main() { std::mt19937 rng(90210); std::normal_distribution noise(0.0, 0.02); mesh::Mesh wave = gridMesh( 161, 121, -40.0, -30.0, 0.5, [&](double x, double y) { return 2.0 * std::sin(x / 8.0) * std::cos(y / 10.0) + noise(rng); }); std::vector waveIds = allVertices(wave); surface::Params automatic; automatic.automatic = true; automatic.tol = 0.05; automatic.extend = 0.08; surface::Fit autoFit = surface::fit(wave, waveIds, automatic); check("wavy auto fit", autoFit.ok && autoFit.rms < 0.05 && autoFit.pctWithin > 99.0, stats(autoFit)); double previewMax = 0; int previewCount = 0; int previewExpected = 0; double data0 = automatic.extend / (1.0 + 2.0 * automatic.extend); double data1 = (1.0 + automatic.extend) / (1.0 + 2.0 * automatic.extend); for (int j = 0; j < autoFit.preview.nv; ++j) { double v = (double)j / (autoFit.preview.nv - 1); for (int i = 0; i < autoFit.preview.nu; ++i) { double u = (double)i / (autoFit.preview.nu - 1); if (u < data0 || u > data1 || v < data0 || v > data1) continue; ++previewExpected; double d = autoFit.preview.deviations[ (std::size_t)j * autoFit.preview.nu + i]; if (!std::isfinite(d)) continue; previewMax = std::max(previewMax, std::fabs(d)); ++previewCount; } } check("preview data-region deviation", previewCount == previewExpected && previewMax < 5.0 * automatic.tol, "max=" + std::to_string(previewMax) + " supported=" + std::to_string(previewCount)); std::vector spherePoints, sphereNormals; constexpr double radius = 100.0; constexpr double capRadius = 50.0; // sin(30 degrees) * radius for (double y = -capRadius; y <= capRadius; y += 0.5) for (double x = -capRadius; x <= capRadius; x += 0.5) { double r2 = x * x + y * y; if (r2 > capRadius * capRadius) continue; double z = std::sqrt(radius * radius - r2); spherePoints.push_back({x, y, z}); sphereNormals.push_back(geom::normalize(Vec3{x, y, z})); } surface::Params sphereParams; sphereParams.nu = sphereParams.nv = 10; sphereParams.smooth = 0.3; sphereParams.extend = 0.08; sphereParams.tol = 0.05; surface::Fit sphereFit = surface::fitPoints(spherePoints, sphereNormals, sphereParams); check("spherical cap 10x10", sphereFit.ok && sphereFit.rms < 0.02, stats(sphereFit)); surface::Params lowSmooth; lowSmooth.nu = lowSmooth.nv = 14; lowSmooth.smooth = 0.0; lowSmooth.extend = 0.08; lowSmooth.tol = 0.05; surface::Params highSmooth = lowSmooth; highSmooth.smooth = 0.9; surface::Fit rough = surface::fit(wave, waveIds, lowSmooth); surface::Fit smooth = surface::fit(wave, waveIds, highSmooth); check("smoothness trade-off", rough.ok && smooth.ok && smooth.rms > rough.rms, "rms(0)=" + std::to_string(rough.rms) + " rms(0.9)=" + std::to_string(smooth.rms)); double minU = 1e300, maxU = -1e300, minV = 1e300, maxV = -1e300; for (const Vec3& p : autoFit.preview.positions) { Vec3 q = p - autoFit.rectOrigin; minU = std::min(minU, dot(q, autoFit.axisU)); maxU = std::max(maxU, dot(q, autoFit.axisU)); minV = std::min(minV, dot(q, autoFit.axisV)); maxV = std::max(maxV, dot(q, autoFit.axisV)); } double extU0 = -minU / autoFit.dataWidth; double extU1 = (maxU - autoFit.dataWidth) / autoFit.dataWidth; double extV0 = -minV / autoFit.dataHeight; double extV1 = (maxV - autoFit.dataHeight) / autoFit.dataHeight; bool extensionOK = std::fabs(extU0 - automatic.extend) < 0.02 && std::fabs(extU1 - automatic.extend) < 0.02 && std::fabs(extV0 - automatic.extend) < 0.02 && std::fabs(extV1 - automatic.extend) < 0.02; check("extended natural domain", extensionOK, "fractions=" + std::to_string(extU0) + "," + std::to_string(extU1) + "," + std::to_string(extV0) + "," + std::to_string(extV1)); std::string stepPath = "/private/tmp/scan2cad_surface_test.step"; std::string stepError; bool wrote = surface::writeSTEP(autoFit, stepPath, &stepError); STEPControl_Reader reader; bool read = wrote && reader.ReadFile(stepPath.c_str()) == IFSelect_RetDone && reader.TransferRoots() > 0; int faces = 0; if (read) for (TopExp_Explorer it(reader.OneShape(), TopAbs_FACE); it.More(); it.Next()) ++faces; check("STEP round trip", read && reader.NbShapes() == 1 && faces >= 1, wrote ? ("shapes=" + std::to_string(reader.NbShapes()) + " faces=" + std::to_string(faces)) : stepError); std::printf("\n%d failure(s)\n", failures); return failures == 0 ? 0 : 1; }