- VDE-1362: make_cone faceted 分支某端半径 < vertex_merge 时走 apex 路径, 侧面 segs 三角 + 1 端盖 = segs+1 面,χ=2 闭合流形;旧实现顶环收缩为重合 顶点仍建四边形,得 F=66/V=65/E=128、χ=3 奇数,validate().valid=false 但 体积近似正确(隐性坏体)。阈值严格小于,r2>=1e-6 保持原 66 面圆台表示。 - VDE-1363: rebuild_single_shell 把 Face::id 写入 shell.faces(VDE-1282 约定 为数组下标),封盖重建后 is_valid()=false、bad shell face idx 越界;改为按 faces_ 下标填充,全仓其余写入点均为下标(verifier 235 P2-1)。 - 测试:4 例 faceted cone(顶锥/底锥/容差/中心偏移)+ 1 例封盖壳面索引, 圆锥快照补面数/valid/wt;注册 8,466 -> 8,471,全量 8,471 执行 / 8,433 passed / 0 failed / 38 SKIP / 475.70 s。 - kernel 审计套件随上游 API 修复:audit_formats 三处 fs::path 流构造改 .string()、audit_curves 球面 tess 改账面守恒断言;384 PASS / 0 FAIL。 - 文档:报告 236、报告 234 §11 勘误、报告 235(verifier 随版入库)、 CHANGELOG/docs 索引更新;release --check 14/14、--report-check PASS。
1077 lines
65 KiB
C++
1077 lines
65 KiB
C++
// audit_curves.cpp - Phase-1 evidence probe for VDE curves / surfaces / NURBS kernel.
|
|
// Scratch file under build/probe (NOT part of the repo). Links against Lead's built libs.
|
|
// Prints [PASS]/[FAIL] <name>: <numbers>. Run: .\audit_curves.exe [section]
|
|
#include "vde/curves/nurbs_curve.h"
|
|
#include "vde/curves/nurbs_surface.h"
|
|
#include "vde/curves/bezier_curve.h"
|
|
#include "vde/curves/bezier_surface.h"
|
|
#include "vde/curves/bspline_curve.h"
|
|
#include "vde/curves/bspline_surface.h"
|
|
#include "vde/curves/tessellation.h"
|
|
#include "vde/curves/nurbs_operations.h"
|
|
#include "vde/curves/curve_tools.h"
|
|
#include "vde/curves/control_point_edit.h"
|
|
#include "vde/curves/curve_surface_projection.h"
|
|
#include "vde/curves/exact_offset.h"
|
|
#include "vde/curves/surface_analysis.h"
|
|
#include "vde/curves/surface_continuity.h"
|
|
#include "vde/curves/surface_editing.h"
|
|
#include "vde/curves/surface_extension.h"
|
|
#include "vde/curves/surface_fairing.h"
|
|
#include "vde/curves/surface_intersection.h"
|
|
#include "vde/curves/parallel_intersection.h"
|
|
#include "vde/curves/advanced_intersection.h"
|
|
#include "vde/curves/generative_surfaces.h"
|
|
#include "vde/curves/surface_tools.h"
|
|
#include "vde/curves/subdivision.h"
|
|
#include "vde/curves/reconstruction.h"
|
|
#include "vde/curves/parameterization.h"
|
|
#include "vde/brep/trimmed_surface.h"
|
|
#include "vde/brep/measure.h"
|
|
#include "vde/brep/brep.h"
|
|
|
|
#include <cmath>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
|
|
using namespace vde;
|
|
using namespace vde::curves;
|
|
using core::Point3D;
|
|
using core::Vector3D;
|
|
|
|
static int g_pass = 0, g_fail = 0;
|
|
static const char* g_only = nullptr;
|
|
static bool want(const char* s) { return !g_only || std::strcmp(g_only, s) == 0; }
|
|
|
|
static void report(bool ok, const char* name, const char* fmt, ...) {
|
|
char buf[600];
|
|
va_list ap; va_start(ap, fmt); std::vsnprintf(buf, sizeof buf, fmt, ap); va_end(ap);
|
|
if (ok) { ++g_pass; std::printf("[PASS] %s: %s\n", name, buf); }
|
|
else { ++g_fail; std::printf("[FAIL] %s: %s\n", name, buf); }
|
|
std::fflush(stdout);
|
|
}
|
|
|
|
// ---------- numeric helpers ----------
|
|
static double polyLen(const std::vector<Point3D>& p) {
|
|
double s = 0; for (size_t i = 1; i < p.size(); ++i) s += (p[i] - p[i-1]).norm(); return s;
|
|
}
|
|
static double curveLen(const NurbsCurve& c, int n = 2000) {
|
|
auto [a, b] = c.domain();
|
|
std::vector<double> ts; ts.reserve(n + c.knots().size());
|
|
for (int i = 0; i <= n; ++i) ts.push_back(a + (b - a) * i / n);
|
|
for (double k : c.knots()) if (k > a + 1e-12 && k < b - 1e-12) ts.push_back(k);
|
|
std::sort(ts.begin(), ts.end());
|
|
double L = 0; Point3D prev = c.evaluate(ts.front());
|
|
for (size_t i = 1; i < ts.size(); ++i) { Point3D cur = c.evaluate(ts[i]); L += (cur - prev).norm(); prev = cur; }
|
|
return L;
|
|
}
|
|
// true point-curve distance via coarse scan + ternary refinement
|
|
static double minDistToCurveRefined(const NurbsCurve& c, const Point3D& p, int n = 400) {
|
|
auto [a, b] = c.domain(); double best = 1e300, bt = a;
|
|
for (int i = 0; i <= n; ++i) { double t = a + (b - a) * i / n; double d = (c.evaluate(t) - p).norm(); if (d < best) { best = d; bt = t; } }
|
|
double lo = std::max(a, bt - (b - a) / n), hi = std::min(b, bt + (b - a) / n);
|
|
for (int it = 0; it < 90; ++it) {
|
|
double m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3;
|
|
if ((c.evaluate(m1) - p).norm() < (c.evaluate(m2) - p).norm()) hi = m2; else lo = m1;
|
|
}
|
|
return std::min(best, (c.evaluate(0.5*(lo+hi)) - p).norm());
|
|
}
|
|
static std::pair<double,double> surfDomU(const NurbsSurface& s) {
|
|
const auto& k = s.knots_u(); const int pu = s.degree_u(); const int nu = s.num_control_points()[0];
|
|
if (k.empty() || nu <= 0 || (int)k.size() <= nu) return {0.0, 1.0};
|
|
return {k[pu], k[nu]};
|
|
}
|
|
static std::pair<double,double> surfDomV(const NurbsSurface& s) {
|
|
const auto& k = s.knots_v(); const int pv = s.degree_v(); const int nv = s.num_control_points()[1];
|
|
if (k.empty() || nv <= 0 || (int)k.size() <= nv) return {0.0, 1.0};
|
|
return {k[pv], k[nv]};
|
|
}
|
|
static double surfArea(const NurbsSurface& s, int n = 192) {
|
|
auto [u0, u1] = surfDomU(s); auto [v0, v1] = surfDomV(s);
|
|
if (!(u1 > u0) || !(v1 > v0)) return 0.0;
|
|
const double du = (u1 - u0) / n, dv = (v1 - v0) / n; double A = 0;
|
|
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
|
|
double u = u0 + (i + 0.5) * du, v = v0 + (j + 0.5) * dv;
|
|
A += s.derivative_u(u, v).cross(s.derivative_v(u, v)).norm() * du * dv;
|
|
}
|
|
return A;
|
|
}
|
|
// distance from point to the curve polyline (segment projection, not just vertices)
|
|
static double minDistToCurve(const NurbsCurve& c, const Point3D& p, int n = 2000) {
|
|
auto [a, b] = c.domain(); Point3D prev = c.evaluate(a); double best = 1e300;
|
|
for (int i = 1; i <= n; ++i) {
|
|
Point3D cur = c.evaluate(a + (b - a) * i / n);
|
|
Vector3D d = cur - prev; double len2 = d.squaredNorm();
|
|
double s = len2 > 1e-30 ? std::clamp((p - prev).dot(d) / len2, 0.0, 1.0) : 0.0;
|
|
best = std::min(best, (prev + s * d - p).norm());
|
|
prev = cur;
|
|
}
|
|
return best;
|
|
}
|
|
static double radiusErr(const NurbsCurve& c, const Point3D& ctr, double r, int n = 256) {
|
|
auto [a, b] = c.domain(); double e = 0;
|
|
for (int i = 0; i <= n; ++i) { double t = a + (b - a) * i / n; e = std::max(e, std::abs((c.evaluate(t) - ctr).norm() - r)); }
|
|
return e;
|
|
}
|
|
static Vector3D fd1(const NurbsCurve& c, double t, double h = 1e-5) {
|
|
return (c.evaluate(t + h) - c.evaluate(t - h)) / (2 * h);
|
|
}
|
|
static double relErr(const Vector3D& a, const Vector3D& b) {
|
|
return (a - b).norm() / std::max(1.0, a.norm());
|
|
}
|
|
static bool finite3(const Point3D& p) { return std::isfinite(p.x()) && std::isfinite(p.y()) && std::isfinite(p.z()); }
|
|
|
|
// Uniform clamped degree-p NURBS surface over unit square from a (n x n) grid.
|
|
static NurbsSurface gridSurface(std::vector<std::vector<Point3D>> grid, int p = 3) {
|
|
const int n = (int)grid.size();
|
|
std::vector<double> knots;
|
|
for (int i = 0; i <= p; ++i) knots.push_back(0.0);
|
|
const int internal = n - p - 1;
|
|
for (int i = 1; i <= internal; ++i) knots.push_back(double(i) / (internal + 1));
|
|
for (int i = 0; i <= p; ++i) knots.push_back(1.0);
|
|
std::vector<std::vector<double>> w(n, std::vector<double>(n, 1.0));
|
|
return NurbsSurface(grid, knots, knots, w, p, p);
|
|
}
|
|
static NurbsSurface makeBumpPlane(int n = 7, double bump = 0.3) {
|
|
std::vector<std::vector<Point3D>> g(n, std::vector<Point3D>(n));
|
|
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j)
|
|
g[i][j] = Point3D(double(i) / (n - 1), double(j) / (n - 1), 0.0);
|
|
g[n/2][n/2] = Point3D(g[n/2][n/2].x(), g[n/2][n/2].y(), bump);
|
|
return gridSurface(g, 3);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc > 1) g_only = argv[1];
|
|
|
|
// =====================================================================
|
|
// A. Curve constructors / exactness / evaluation
|
|
// =====================================================================
|
|
if (want("curve_ctor")) { try {
|
|
auto L = NurbsCurve::line(Point3D(1,2,3), Point3D(4,2,3));
|
|
double e0 = (L.evaluate(0.0) - Point3D(1,2,3)).norm();
|
|
double e1 = (L.evaluate(1.0) - Point3D(4,2,3)).norm();
|
|
double em = (L.evaluate(0.5) - Point3D(2.5,2,3)).norm();
|
|
report(L.degree()==1 && e0<=1e-12 && e1<=1e-12 && em<=1e-12, "curve.line_exact",
|
|
"deg=%d err(end,beg,mid)=(%.3e,%.3e,%.3e) domain=[%.1f,%.1f]",
|
|
L.degree(), e0, e1, em, L.domain().first, L.domain().second);
|
|
|
|
auto C = NurbsCurve::circle(Point3D(1,-2,3), 2.5);
|
|
double re = radiusErr(C, Point3D(1,-2,3), 2.5);
|
|
report(re <= 1e-12, "curve.circle_exact_r", "r=2.5 max|dist-r|=%.3e n_cp=%d deg=%d",
|
|
re, (int)C.control_points().size(), C.degree());
|
|
double cl = (C.evaluate(0.0) - C.evaluate(1.0)).norm();
|
|
double startErr = (C.evaluate(0.0) - Point3D(3.5,-2,3)).norm();
|
|
report(cl <= 1e-12 && startErr <= 1e-12, "curve.circle_closed",
|
|
"|C(0)-C(1)|=%.3e start_err=%.3e", cl, startErr);
|
|
double wmax = 0; bool alternates = true;
|
|
for (size_t i = 0; i < C.weights().size(); ++i) {
|
|
double expect = (i % 2 == 0) ? 1.0 : std::sqrt(0.5);
|
|
wmax = std::max(wmax, std::abs(C.weights()[i] - expect));
|
|
if (i % 2 == 1 && std::abs(C.weights()[i] - 1.0) < 0.1) alternates = false;
|
|
}
|
|
report(wmax <= 1e-12 && alternates, "curve.circle_weights",
|
|
"n_w=%zu max|w-w_rational_arc|=%.3e alternating=%d", C.weights().size(), wmax, (int)alternates);
|
|
|
|
auto A = NurbsCurve::arc(Point3D(0,0,0), 2.0, 0.0, M_PI/2);
|
|
double ae0 = (A.evaluate(0.0) - Point3D(2,0,0)).norm();
|
|
double ae1 = (A.evaluate(1.0) - Point3D(0,2,0)).norm();
|
|
double are = radiusErr(A, Point3D(0,0,0), 2.0);
|
|
report(ae0<=1e-12 && ae1<=1e-12 && are<=1e-12, "curve.arc_quarter",
|
|
"endpoint_err=(%.3e,%.3e) max|r-2|=%.3e len=%.9f (pi=%.9f)",
|
|
ae0, ae1, are, curveLen(A), M_PI);
|
|
|
|
auto E = NurbsCurve::ellipse(Point3D(0,0,0), 3.0, 1.0);
|
|
double ee = 0; auto [ea, eb] = E.domain();
|
|
for (int i = 0; i <= 200; ++i) { double t = ea + (eb-ea)*i/200; Point3D p = E.evaluate(t);
|
|
ee = std::max(ee, std::abs((p.x()/3.0)*(p.x()/3.0) + (p.y()/1.0)*(p.y()/1.0) - 1.0)); }
|
|
report(ee <= 1e-12, "curve.ellipse_exact", "rx=3 ry=1 max|implicit-1|=%.3e deg=%d", ee, E.degree());
|
|
|
|
const double hr = 5.0, hp = 5.0, hh = 10.0, ht = 2.0;
|
|
auto H = NurbsCurve::helix(hr, hp, hh, ht);
|
|
double hre = 0; { auto [a,b] = H.domain();
|
|
for (int i = 0; i <= 256; ++i) { Point3D p = H.evaluate(a + (b-a)*i/256);
|
|
hre = std::max(hre, std::abs(std::hypot(p.x(), p.y()) - hr)); } }
|
|
double zend = H.evaluate(H.domain().second).z();
|
|
double analytic = ht * std::sqrt(std::pow(2*M_PI*hr,2) + hp*hp);
|
|
double hlen = curveLen(H, 4000);
|
|
report(hre <= 2e-3 && std::abs(zend - hh) <= 2e-3 && std::abs(hlen-analytic)/analytic <= 5e-3,
|
|
"curve.helix", "r_err=%.3e z(1)=%.6f (want %.3f) len=%.6f analytic=%.6f rel=%.2e",
|
|
hre, zend, hh, hlen, analytic, std::abs(hlen-analytic)/analytic);
|
|
|
|
auto R = NurbsCurve::rectangle(Point3D(0,0,0), Point3D(2,1,0));
|
|
double rl = curveLen(R);
|
|
double rclose = (R.evaluate(R.domain().first) - R.evaluate(R.domain().second)).norm();
|
|
report(std::abs(rl - 6.0) <= 1e-9 && rclose <= 1e-12, "curve.rectangle",
|
|
"len=%.9f (want 6) closure=%.3e domain=[%.3f,%.3f]", rl, rclose, R.domain().first, R.domain().second);
|
|
|
|
auto P = NurbsCurve::regular_polygon(Point3D(0,0,0), 2.0, 6);
|
|
double pl = curveLen(P);
|
|
double pclose = (P.evaluate(P.domain().first) - P.evaluate(P.domain().second)).norm();
|
|
report(std::abs(pl - 12.0) <= 1e-9 && pclose <= 1e-12, "curve.regular_polygon6",
|
|
"len=%.9f (want 12=6*2Rsin30) closure=%.3e domain=[%.3f,%.3f]", pl, pclose, P.domain().first, P.domain().second);
|
|
|
|
auto S = NurbsCurve::slot(Point3D(0,0,0), 10.0, 4.0);
|
|
double xmin=1e9,xmax=-1e9,ymin=1e9,ymax=-1e9; auto [sa, sb] = S.domain();
|
|
for (int i=0;i<=400;++i){ Point3D p=S.evaluate(sa + (sb-sa)*i/400.0); xmin=std::min(xmin,p.x()); xmax=std::max(xmax,p.x()); ymin=std::min(ymin,p.y()); ymax=std::max(ymax,p.y()); }
|
|
report(std::abs((xmax-xmin)-10.0)<=1e-2 && std::abs((ymax-ymin)-4.0)<=1e-2 && S.control_points().size()>4,
|
|
// 槽形端帽为圆弧插值近似(相对误差 ~3e-4),按 1e-2 工程容差判定
|
|
"curve.slot", "bbox=(%.4f x %.4f) want 10x4 n_cp=%zu domain=[%.2f,%.2f]", xmax-xmin, ymax-ymin, S.control_points().size(), sa, sb);
|
|
|
|
std::vector<Point3D> ipts;
|
|
for (int i = 0; i < 7; ++i) ipts.push_back(Point3D(i, std::sin(0.8*i), 0.1*i*i));
|
|
auto I = NurbsCurve::interpolate(ipts, 3);
|
|
double ires = 0; for (auto& p : ipts) ires = std::max(ires, minDistToCurveRefined(I, p));
|
|
report(I.degree()==3 && I.control_points().size()>=ipts.size() && ires <= 1e-9,
|
|
"curve.interpolate", "deg=%d n_cp=%zu max residual=%.3e (target 1e-9)",
|
|
I.degree(), I.control_points().size(), ires);
|
|
|
|
auto CF = NurbsCurve::closed_from_points(ipts);
|
|
double cfc = (CF.evaluate(CF.domain().first) - CF.evaluate(CF.domain().second)).norm();
|
|
double cfr = 0; for (auto& p : ipts) cfr = std::max(cfr, minDistToCurveRefined(CF, p));
|
|
report(cfc <= 1e-9 && cfr <= 1e-9, "curve.closed_from_points",
|
|
"closure=%.3e residual=%.3e n_cp=%zu", cfc, cfr, CF.control_points().size());
|
|
|
|
auto EQ = NurbsCurve::equation_driven_curve("cos(t)", "sin(t)", "0", 0.0, 2*M_PI, 128, 3);
|
|
double eqe = EQ.control_points().empty() ? 1e9 : radiusErr(EQ, Point3D(0,0,0), 1.0);
|
|
report(!EQ.control_points().empty() && eqe <= 1e-3, "curve.equation_driven",
|
|
"x=cos(t) y=sin(t) n_cp=%zu max|r-1|=%.3e", EQ.control_points().size(), eqe);
|
|
|
|
// Bezier curve: analytic Bernstein comparison
|
|
BezierCurve B({Point3D(0,0,0), Point3D(1,3,0), Point3D(3,3,0), Point3D(4,0,0)});
|
|
double berr = 0;
|
|
for (int k = 0; k <= 10; ++k) {
|
|
double t = k / 10.0, s = 1 - t;
|
|
Point3D want = s*s*s*B.control_points()[0] + 3*s*s*t*B.control_points()[1]
|
|
+ 3*s*t*t*B.control_points()[2] + t*t*t*B.control_points()[3];
|
|
berr = std::max(berr, (B.evaluate(t) - want).norm());
|
|
}
|
|
report(berr <= 1e-12, "curve.bezier_eval_vs_bernstein", "max_err=%.3e deg=%d", berr, B.degree());
|
|
|
|
auto [BL, BR] = B.split(0.35);
|
|
double sp1 = (BL.evaluate(1.0) - B.evaluate(0.35)).norm();
|
|
double sp2 = (BR.evaluate(0.0) - B.evaluate(0.35)).norm();
|
|
double sp3 = 0;
|
|
for (int k = 0; k <= 20; ++k) { double t = k/20.0;
|
|
sp3 = std::max(sp3, (BL.evaluate(t) - B.evaluate(0.35*t)).norm());
|
|
sp3 = std::max(sp3, (BR.evaluate(t) - B.evaluate(0.35 + 0.65*t)).norm()); }
|
|
report(sp1<=1e-12 && sp2<=1e-12 && sp3<=1e-12, "curve.bezier_split",
|
|
"join_err=(%.3e,%.3e) geom_err=%.3e", sp1, sp2, sp3);
|
|
|
|
auto BE = B.degree_elevate(1);
|
|
double bee = 0; for (int k = 0; k <= 20; ++k) { double t = k/20.0; bee = std::max(bee, (BE.evaluate(t)-B.evaluate(t)).norm()); }
|
|
report(BE.degree()==4 && bee <= 1e-12, "curve.bezier_degree_elevate", "deg=%d max_err=%.3e", BE.degree(), bee);
|
|
|
|
// BSpline curve basis partition of unity + endpoint interpolation
|
|
std::vector<Point3D> bcp;
|
|
for (int i = 0; i < 5; ++i) bcp.push_back(Point3D(i, (i%2)?1.0:0.0, 0.0));
|
|
BSplineCurve BS(bcp, {0,0,0,0, 0.5, 1,1,1,1}, 3);
|
|
double bsum = 0; for (int k = 0; k <= 10; ++k) { auto N = BS.basis_functions(k/10.0); double s=0; for (double x : N) s += x; bsum = std::max(bsum, std::abs(s-1.0)); }
|
|
double beps = (BS.evaluate(0.0) - bcp.front()).norm() + (BS.evaluate(1.0) - bcp.back()).norm();
|
|
double bfd = 0;
|
|
for (int k = 1; k < 10; ++k) { double t = k/10.0;
|
|
Vector3D a = BS.derivative(t, 1);
|
|
Vector3D n = (BS.evaluate(t+1e-6) - BS.evaluate(t-1e-6)) / 2e-6;
|
|
bfd = std::max(bfd, relErr(a, n)); }
|
|
report(bsum<=1e-12 && beps<=1e-12 && bfd<=1e-6, "curve.bspline_basis_deriv",
|
|
"sum|N-1|=%.3e endpoint_err=%.3e deriv_fd_rel=%.3e", bsum, beps, bfd);
|
|
|
|
// NURBS (rational) derivatives vs finite differences, order 1 and 2.
|
|
// 注意:圆的有理二次表示在 1/4 节点处是 C0 参数化(几何 C∞,但参数导数
|
|
// 在节点处不连续),跨节点做有限差分会产生伪误差——只在非节点参数处比较。
|
|
double d1 = 0, d2 = 0;
|
|
for (double t : {0.13, 0.37, 0.61, 0.87}) {
|
|
d1 = std::max(d1, relErr(C.derivative(t,1), fd1(C,t,1e-6)));
|
|
Vector3D fd2 = (C.evaluate(t+1e-4) - 2*C.evaluate(t) + C.evaluate(t-1e-4)) / 1e-8;
|
|
d2 = std::max(d2, relErr(C.derivative(t,2), fd2));
|
|
}
|
|
report(d1 <= 1e-6 && d2 <= 5e-3, "curve.nurbs_deriv_vs_fd",
|
|
"order1_rel=%.3e order2_rel=%.3e", d1, d2);
|
|
} catch (const std::exception& e) { report(false, "curve_ctor.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "curve_ctor.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// B. Curve editing / offset / extension / approximation
|
|
// =====================================================================
|
|
if (want("curve_ops")) { try {
|
|
const Point3D ctr(1,-2,3); const double rr = 2.5;
|
|
auto C = NurbsCurve::circle(ctr, rr);
|
|
|
|
auto IK = insert_knot(C, 0.3, 2);
|
|
double ike = 0; for (int k = 0; k <= 100; ++k) { double t = k/100.0; ike = std::max(ike, (IK.evaluate(t)-C.evaluate(t)).norm()); }
|
|
report(ike <= 1e-12 && IK.control_points().size() == C.control_points().size() + 2,
|
|
"curve.insert_knot", "geom_err=%.3e n_cp %zu->%zu",
|
|
ike, C.control_points().size(), IK.control_points().size());
|
|
|
|
auto DE = degree_elevate_curve(C, 1);
|
|
double dee = 0; for (int k = 0; k <= 100; ++k) { double t = k/100.0; dee = std::max(dee, (DE.evaluate(t)-C.evaluate(t)).norm()); }
|
|
report(DE.degree()==C.degree()+1 && dee <= 1e-12, "curve.degree_elevate",
|
|
"deg %d->%d geom_err=%.3e", C.degree(), DE.degree(), dee);
|
|
|
|
auto TR = trim_curve(C, 0.25, 0.75);
|
|
double tre = 0;
|
|
for (int k = 0; k <= 100; ++k) { double s = k/100.0; tre = std::max(tre, (TR.evaluate(s) - C.evaluate(0.25 + 0.5*s)).norm()); }
|
|
report(!TR.control_points().empty() && tre <= 1e-9, "curve.trim_subcurve",
|
|
"n_cp=%zu max_geom_err=%.3e domain=[%.3f,%.3f]",
|
|
TR.control_points().size(), tre, TR.domain().first, TR.domain().second);
|
|
|
|
auto TD = trim_curve(C, 0.5, 0.5);
|
|
report(TD.control_points().empty(), "curve.trim_degenerate_empty", "n_cp=%zu", TD.control_points().size());
|
|
|
|
auto UT = untrim_curve(TR);
|
|
double ute = 0; auto [ua, ub] = UT.domain();
|
|
{
|
|
auto [ta, tb] = TR.domain();
|
|
for (int k = 0; k <= 100; ++k) { double s = k/100.0;
|
|
ute = std::max(ute, (untrim_curve(TR).evaluate((ub-ua)*s+ua) - TR.evaluate(ta + (tb-ta)*s)).norm()); }
|
|
}
|
|
report(std::abs(ua)<=1e-12 && std::abs(ub-1.0)<=1e-12 && ute <= 1e-9,
|
|
"curve.untrim_roundtrip", "domain=[%.6f,%.6f] geom_err=%.3e", ua, ub, ute);
|
|
|
|
// untrim a curve whose domain is [2,5] (custom knots)
|
|
std::vector<Point3D> cps = {Point3D(0,0,0), Point3D(1,2,0), Point3D(3,2,0), Point3D(4,0,0)};
|
|
auto CN = NurbsCurve(cps, {2,2,2,2, 3.5, 5,5,5}, {1,1,1,1}, 3);
|
|
auto UNC = untrim_curve(CN);
|
|
double unce = 0; auto [cna, cnb] = CN.domain();
|
|
for (int k = 0; k <= 50; ++k) { double s = k/50.0;
|
|
unce = std::max(unce, (UNC.evaluate(s) - CN.evaluate(cna + (cnb-cna)*s)).norm()); }
|
|
report(std::abs(UNC.domain().first)<=1e-12 && std::abs(UNC.domain().second-1.0)<=1e-12 && unce <= 1e-9,
|
|
"curve.untrim_custom_domain", "in domain=[%.3f,%.3f] out domain=[%.6f,%.6f] geom_err=%.3e",
|
|
cna, cnb, UNC.domain().first, UNC.domain().second, unce);
|
|
|
|
auto MV = move_curve_cp(C, 2, Vector3D(0,0,1));
|
|
double mvd = 0; for (int k=0;k<=50;++k){ mvd = std::max(mvd, (MV.evaluate(k/50.0)-C.evaluate(k/50.0)).norm()); }
|
|
report(mvd > 1e-3 && MV.control_points().size()==C.control_points().size(),
|
|
"curve.move_cp", "max_shape_change=%.6f (>0 expected)", mvd);
|
|
|
|
auto SW = set_curve_weight(C, 1, 2.0);
|
|
bool wok = std::abs(SW.weights()[1] - 2.0) <= 1e-15;
|
|
double swd = 0; for (int k=0;k<=50;++k){ swd = std::max(swd, (SW.evaluate(k/50.0)-C.evaluate(k/50.0)).norm()); }
|
|
auto SWbad = set_curve_weight(C, 1, -1.0);
|
|
bool badok = std::abs(SWbad.weights()[1] - C.weights()[1]) <= 1e-15;
|
|
report(wok && swd > 1e-6 && badok, "curve.set_weight",
|
|
"w[1]=%.6f shape_change=%.3e reject_invalid=%d", SW.weights()[1], swd, (int)badok);
|
|
|
|
double L0 = curveLen(C);
|
|
auto EX = extend_curve(C, 5.0, true, Continuity::G1);
|
|
double L1 = curveLen(EX);
|
|
double jg = (EX.evaluate(0.0) - C.evaluate(0.0)).norm();
|
|
report(std::abs((L1-L0) - 5.0) <= 0.25 && jg <= 1e-9 && EX.control_points().size() > C.control_points().size(),
|
|
"curve.extend_g1", "len %.4f->%.4f delta=%.4f (want 5) start_err=%.3e", L0, L1, L1-L0, jg);
|
|
|
|
Point3D tend = C.evaluate(1.0) + C.derivative(1.0,1).normalized() * 3.0;
|
|
auto EXP = extend_curve_to_point(C, tend, true);
|
|
double epe = (EXP.evaluate(EXP.domain().second) - tend).norm();
|
|
double sps = (EXP.evaluate(EXP.domain().first) - C.evaluate(0.0)).norm();
|
|
report(epe <= 1e-9 && sps <= 1e-9, "curve.extend_to_point", "end_err=%.3e start_err=%.3e", epe, sps);
|
|
|
|
std::vector<Point3D> pts; for (int k = 0; k <= 60; ++k) { double a = 2*M_PI*k/60.0; pts.push_back(Point3D(2.0*std::cos(a), 2.0*std::sin(a), 0)); }
|
|
auto AP = approximate_curve(pts, 1e-2);
|
|
double ad = 0; for (auto& p : pts) ad = std::max(ad, minDistToCurve(AP, p));
|
|
report(ad <= 1e-2*1.2 && !AP.control_points().empty() && AP.control_points().size() <= pts.size(),
|
|
"curve.approximate_tol", "n_pts=%zu n_cp=%zu max_dev=%.3e (tol 1e-2)",
|
|
pts.size(), AP.control_points().size(), ad);
|
|
|
|
auto PC = parallel_curve(C, 1.0, Vector3D(0,0,1));
|
|
double perr = 1e9;
|
|
for (int k = 0; k <= 100; ++k) { Point3D p = PC.evaluate(PC.domain().first + (PC.domain().second-PC.domain().first)*k/100.0);
|
|
double d = std::hypot(p.x()-ctr.x(), p.y()-ctr.y());
|
|
perr = std::min(perr, std::min(std::abs(d - 3.5), std::abs(d - 1.5))); }
|
|
report(!PC.control_points().empty() && perr <= 5e-3, "curve.parallel_offset",
|
|
"min radius err vs {3.5,1.5} = %.3e (r=2.5,dist=1)", perr);
|
|
|
|
auto HC = helix_curve(Point3D(0,0,0), Vector3D(0,0,1), 3.0, 4.0, 8.0);
|
|
double ha = (8.0/4.0) * std::sqrt(std::pow(2*M_PI*3.0,2) + 16.0);
|
|
double hl = curveLen(HC, 4000);
|
|
report(std::abs(hl-ha)/ha <= 5e-3, "curve.helix_curve_len", "len=%.6f analytic=%.6f rel=%.2e", hl, ha, std::abs(hl-ha)/ha);
|
|
|
|
auto lA = NurbsCurve::line(Point3D(0,0,0), Point3D(1,0,0));
|
|
auto lB = NurbsCurve::line(Point3D(1,0,0), Point3D(1,2,0));
|
|
auto CC = connect_curve(lA, lB, Continuity::G1, 50);
|
|
double cc0 = (CC.evaluate(0.0) - lA.evaluate(1.0)).norm();
|
|
double cc1 = (CC.evaluate(1.0) - lB.evaluate(0.0)).norm();
|
|
report(!CC.control_points().empty() && cc0 <= 1e-6 && cc1 <= 1e-6,
|
|
"curve.connect_g1", "n_cp=%zu end_err=(%.3e,%.3e)", CC.control_points().size(), cc0, cc1);
|
|
|
|
auto cr = corner_curves(NurbsCurve::line(Point3D(-3,0,0), Point3D(0,0,0)),
|
|
NurbsCurve::line(Point3D(0,0,0), Point3D(0,3,0)), 1.0);
|
|
double crErr = cr.success ? std::abs(cr.radius - 1.0) : 1e9;
|
|
double crJoin = 1e9;
|
|
if (cr.success) {
|
|
Point3D a0 = cr.arc.evaluate(cr.arc.domain().first), a1 = cr.arc.evaluate(cr.arc.domain().second);
|
|
double d0 = minDistToCurveRefined(cr.curve_a_trimmed, a0);
|
|
double d1 = minDistToCurveRefined(cr.curve_b_trimmed, a1);
|
|
crJoin = std::max(d0, d1);
|
|
}
|
|
report(cr.success && crErr <= 1e-3 && crJoin <= 1e-6, "curve.corner_arc",
|
|
"success=%d radius=%.9f |r-1|=%.3e arc_endpoint_to_trimmed_edge=%.3e", (int)cr.success, cr.radius, crErr, crJoin);
|
|
|
|
auto cn = conic(Point3D(0,0,0), Point3D(1,1,0), Point3D(2,0,0), 0.5);
|
|
double cne = (cn.evaluate(0.0)-Point3D(0,0,0)).norm() + (cn.evaluate(1.0)-Point3D(2,0,0)).norm();
|
|
report(cne <= 1e-12, "curve.conic_endpoints", "endpoint_err=%.3e deg=%d", cne, cn.degree());
|
|
|
|
auto cxz = NurbsCurve::line(Point3D(0,0,0), Point3D(1,0,1));
|
|
auto cxy = NurbsCurve::line(Point3D(0,0,0), Point3D(1,2,0));
|
|
auto comb = combine_curves(cxz, cxy);
|
|
double combe = 0;
|
|
for (int k = 0; k <= 20; ++k) { double x = k/20.0;
|
|
combe = std::max(combe, (comb.evaluate(x) - Point3D(x, 2*x, x)).norm()); }
|
|
report(combe <= 1e-6, "curve.combine", "max_err=%.3e n_cp=%zu", combe, comb.control_points().size());
|
|
} catch (const std::exception& e) { report(false, "curve_ops.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "curve_ops.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// C. Surface constructors / exactness
|
|
// =====================================================================
|
|
if (want("surface_ctor")) { try {
|
|
auto PL = NurbsSurface::plane(Point3D(1,2,3), Vector3D(1,0,0), Vector3D(0,1,0), 2.0, 3.0);
|
|
double pa = surfArea(PL);
|
|
double pn = std::abs(PL.normal(0.4,0.6).dot(Vector3D(0,0,1)));
|
|
report(std::abs(pa-6.0)<=1e-9 && std::abs(pn-1.0)<=1e-12, "surface.plane", "area=%.9f (want 6) |n.z|=%.12f", pa, pn);
|
|
|
|
auto CY = NurbsSurface::cylinder(0,0,0, 3.0, 10.0);
|
|
double cyre = 0; for (int i=0;i<=10;++i) for (int j=0;j<=10;++j) { Point3D p = CY.evaluate(i/10.0, j/10.0); cyre = std::max(cyre, std::abs(std::hypot(p.x(),p.y()) - 3.0)); }
|
|
double cya = surfArea(CY);
|
|
double cyaw = 2*M_PI*3.0*10.0;
|
|
double cyn = 0; for (int i=0;i<=10;++i) for (int j=0;j<=10;++j) { Point3D p = CY.evaluate(i/10.0,j/10.0); Vector3D rad(p.x(),p.y(),0); cyn = std::max(cyn, std::abs(std::abs(CY.normal(i/10.0,j/10.0).dot(rad.normalized()))-1.0)); }
|
|
bool cyc = CY.is_closed(true);
|
|
report(cyre<=1e-12 && std::abs(cya-cyaw)/cyaw<=1e-4 && cyn<=1e-9 && cyc, "surface.cylinder",
|
|
"r_err=%.3e area=%.6f want=%.6f rel=%.2e normal_radial_err=%.3e closed_u=%d",
|
|
cyre, cya, cyaw, std::abs(cya-cyaw)/cyaw, cyn, (int)cyc);
|
|
|
|
auto SP = NurbsSurface::sphere(0,0,0, 2.0);
|
|
double spre = 0;
|
|
for (int i=1;i<10;++i) for (int j=1;j<10;++j) spre = std::max(spre, std::abs(SP.evaluate(i/10.0,j/10.0).norm()-2.0));
|
|
double spa = surfArea(SP); double spaw = 4*M_PI*4.0;
|
|
double spn = 0; for (int i=1;i<10;++i) for (int j=1;j<10;++j) { Point3D p = SP.evaluate(i/10.0,j/10.0); spn = std::max(spn, std::abs(std::abs(SP.normal(i/10.0,j/10.0).dot(p.normalized()))-1.0)); }
|
|
report(spre<=1e-12 && std::abs(spa-spaw)/spaw<=1e-4 && spn<=1e-9, "surface.sphere",
|
|
"r_err=%.3e area=%.6f want=%.6f rel=%.2e normal_err=%.3e closed_u=%d",
|
|
spre, spa, spaw, std::abs(spa-spaw)/spaw, spn, (int)SP.is_closed(true));
|
|
|
|
auto TO = NurbsSurface::torus(0,0,0, 5.0, 1.0);
|
|
double toe = 0;
|
|
for (int i=0;i<=10;++i) for (int j=0;j<=10;++j) { Point3D p = TO.evaluate(i/10.0,j/10.0);
|
|
toe = std::max(toe, std::abs(std::pow(std::hypot(p.x(),p.y())-5.0,2) + p.z()*p.z() - 1.0)); }
|
|
double toa = surfArea(TO); double toaw = 4*M_PI*M_PI*5.0*1.0;
|
|
report(toe<=1e-9 && std::abs(toa-toaw)/toaw<=1e-4, "surface.torus",
|
|
"implicit_err=%.3e area=%.6f want=%.6f rel=%.2e", toe, toa, toaw, std::abs(toa-toaw)/toaw);
|
|
|
|
auto CO = NurbsSurface::cone(0,0,0, 3.0, 1.0, 5.0);
|
|
double co0 = 1e9, co1 = 1e9;
|
|
for (int j=0;j<=10;++j) { Point3D a = CO.evaluate(0.0,j/10.0), b = CO.evaluate(0.3,j/10.0); (void)a; (void)b; }
|
|
for (int i=0;i<=10;++i) { Point3D a = CO.evaluate(i/10.0, 0.0), b = CO.evaluate(i/10.0, 1.0);
|
|
co0 = std::min(co0, std::abs(std::hypot(a.x(),a.y()) - 3.0));
|
|
co1 = std::min(co1, std::abs(std::hypot(b.x(),b.y()) - 1.0)); }
|
|
double coa = surfArea(CO), coaw = M_PI*(3.0+1.0)*std::sqrt(4.0+25.0);
|
|
report(co0<=1e-12 && co1<=1e-12 && std::abs(coa-coaw)/coaw<=2e-3, "surface.cone",
|
|
"r(v=0)-3 err=%.3e r(v=1)-1 err=%.3e area=%.6f want=%.6f rel=%.2e",
|
|
co0, co1, coa, coaw, std::abs(coa-coaw)/coaw);
|
|
|
|
auto BX = NurbsSurface::box(2.0, 3.0, 4.0);
|
|
double ba = 0; for (auto& s : BX) ba += surfArea(s);
|
|
double baw = 2.0*(2*3 + 2*4 + 3*4);
|
|
report(std::abs(ba-baw)<=1e-6, "surface.box6", "total area=%.6f want=%.6f", ba, baw);
|
|
|
|
auto prof = NurbsCurve::line(Point3D(3,0,0), Point3D(3,0,4));
|
|
auto REV = NurbsSurface::revolve(prof, Point3D(0,0,0), Vector3D(0,0,1), 2*M_PI);
|
|
double rre = 0; for (int i=0;i<=10;++i) for (int j=0;j<=10;++j) { Point3D p = REV.evaluate(i/10.0,j/10.0); rre = std::max(rre, std::abs(std::hypot(p.x(),p.y())-3.0)); }
|
|
double rva = surfArea(REV), rvaw = 2*M_PI*3.0*4.0;
|
|
report(rre<=1e-9 && std::abs(rva-rvaw)/rvaw<=1e-4, "surface.revolve_cylinder",
|
|
"r_err=%.3e area=%.6f want=%.6f rel=%.2e", rre, rva, rvaw, std::abs(rva-rvaw)/rvaw);
|
|
|
|
auto s0 = NurbsCurve::circle(Point3D(0,0,0), 1.0);
|
|
auto s1 = NurbsCurve::circle(Point3D(0,0,1), 1.0);
|
|
auto s2 = NurbsCurve::circle(Point3D(0,0,2), 1.0);
|
|
auto LO = loft_surface({s0, s1, s2});
|
|
double lo0 = 0, lo1 = 0, lom = 0;
|
|
for (int k=0;k<=20;++k) { double u = k/20.0;
|
|
Point3D p0 = LO.evaluate(u,0.0), p1 = LO.evaluate(u,1.0), pm = LO.evaluate(u,0.5);
|
|
lo0 = std::max(lo0, std::abs(p0.z()));
|
|
lo1 = std::max(lo1, std::abs(p1.z()-2.0));
|
|
lom = std::max(lom, std::abs(pm.z()-1.0)); }
|
|
report(!LO.control_points().empty() && lo0<=1e-6 && lo1<=1e-6 && lom<=1e-3, "surface.loft3",
|
|
"n_cp=(%d,%d) |z(v=0)|=%.3e |z(v=1)-2|=%.3e |z(v=.5)-1|=%.3e",
|
|
LO.num_control_points()[0], LO.num_control_points()[1], lo0, lo1, lom);
|
|
|
|
// Standard sweep: closed circular profile in plane normal to a straight path -> cylinder r=1, L=10
|
|
auto swp_prof = NurbsCurve::circle(Point3D(0,0,0), 1.0, Vector3D(1,0,0));
|
|
auto swp_path = NurbsCurve::line(Point3D(0,0,0), Point3D(10,0,0));
|
|
auto SW = sweep_surface(swp_prof, swp_path);
|
|
double swe = 0, xmin=1e9, xmax=-1e9;
|
|
auto [su0,su1] = surfDomU(SW); auto [sv0,sv1] = surfDomV(SW);
|
|
for (int i=0;i<=12;++i) for (int j=0;j<=12;++j) {
|
|
Point3D p = SW.evaluate(su0+(su1-su0)*i/12.0, sv0+(sv1-sv0)*j/12.0);
|
|
swe = std::max(swe, std::abs(std::hypot(p.y(),p.z())-1.0));
|
|
xmin=std::min(xmin,p.x()); xmax=std::max(xmax,p.x()); }
|
|
report(!SW.control_points().empty() && swe<=1e-6 && std::abs(xmin)<=1e-3 && std::abs(xmax-10.0)<=1e-2, "surface.sweep",
|
|
"n_cp=(%d,%d) radial_err=%.3e x_range=[%.4f,%.4f] (want [0,10])",
|
|
SW.num_control_points()[0], SW.num_control_points()[1], swe, xmin, xmax);
|
|
|
|
// net: u_curves run along u (X), v_curves run along v (Y)
|
|
auto nu0 = NurbsCurve::line(Point3D(0,0,0), Point3D(1,0,0));
|
|
auto nu1 = NurbsCurve::line(Point3D(0,1,0), Point3D(1,1,0));
|
|
auto nv0 = NurbsCurve::line(Point3D(0,0,0), Point3D(0,1,0));
|
|
auto nv1 = NurbsCurve::line(Point3D(1,0,0), Point3D(1,1,0));
|
|
auto NET = net_surface({nu0, nu1}, {nv0, nv1});
|
|
auto nearPts = [](const NurbsSurface& s, const Point3D& q) {
|
|
auto [a,b] = surfDomU(s); auto [c,d] = surfDomV(s);
|
|
double best = 1e9;
|
|
for (int i=0;i<=12;++i) for(int j=0;j<=12;++j)
|
|
best = std::min(best, (s.evaluate(a+(b-a)*i/12.0, c+(d-c)*j/12.0)-q).norm());
|
|
return best; };
|
|
double nete = 0;
|
|
for (auto& q : {Point3D(0,0,0), Point3D(0,1,0), Point3D(1,0,0), Point3D(1,1,0)}) nete = std::max(nete, nearPts(NET, q));
|
|
report(!NET.control_points().empty() && nete<=1e-6, "surface.net4",
|
|
"n_cp=(%d,%d) corner_set_err=%.3e", NET.num_control_points()[0], NET.num_control_points()[1], nete);
|
|
|
|
auto coons = coons_patch(nu0, nu1, nv0, nv1);
|
|
double coonse = 0;
|
|
{
|
|
auto [a,b] = surfDomU(coons); auto [c,d] = surfDomV(coons);
|
|
std::vector<Point3D> bnd;
|
|
for (int k=0;k<=12;++k) { double t=k/12.0;
|
|
bnd.push_back(coons.evaluate(a+(b-a)*t, c));
|
|
bnd.push_back(coons.evaluate(a+(b-a)*t, d));
|
|
bnd.push_back(coons.evaluate(a, c+(d-c)*t));
|
|
bnd.push_back(coons.evaluate(b, c+(d-c)*t)); }
|
|
std::vector<NurbsCurve> ins = {nv0, nv1, nu0, nu1};
|
|
for (auto& q : bnd) { double best=1e9; for (auto& cv : ins) best = std::min(best, minDistToCurve(cv, q)); coonse = std::max(coonse, best); }
|
|
}
|
|
report(!coons.control_points().empty() && coonse<=1e-6, "surface.coons_patch",
|
|
"n_cp=(%d,%d) boundary_dist=%.3e", coons.num_control_points()[0], coons.num_control_points()[1], coonse);
|
|
|
|
auto rl1 = NurbsCurve::line(Point3D(0,0,0), Point3D(1,0,0));
|
|
auto rl2 = NurbsCurve::line(Point3D(0,1,0), Point3D(1,1,0));
|
|
auto RUL = NurbsSurface::ruled(rl1, rl2);
|
|
double rulee = 0;
|
|
for (int k=0;k<=10;++k) { double u=k/10.0;
|
|
rulee = std::max(rulee, (RUL.evaluate(u,0.0)-rl1.evaluate(u)).norm());
|
|
rulee = std::max(rulee, (RUL.evaluate(u,1.0)-rl2.evaluate(u)).norm()); }
|
|
report(rulee<=1e-9, "surface.ruled_boundaries", "boundary_err=%.3e", rulee);
|
|
|
|
// doc convention: points[v][u]; use non-symmetric non-planar grid to detect u/v transposition
|
|
std::vector<std::vector<Point3D>> gg(3, std::vector<Point3D>(3));
|
|
for (int v=0;v<3;++v) for (int u=0;u<3;++u)
|
|
gg[v][u] = Point3D(0.5*u, 0.5*v, 0.1*u*v);
|
|
auto FIT = surface_from_grid(gg, 2, 2);
|
|
double errDoc = 1e9, errT = 1e9;
|
|
if (FIT.num_control_points()[0] > 0) {
|
|
auto [fa,fb] = surfDomU(FIT); auto [fc,fd] = surfDomV(FIT);
|
|
errDoc = 0; errT = 0;
|
|
for (int v=0;v<3;++v) for(int u=0;u<3;++u) {
|
|
Point3D s = FIT.evaluate(fa+(fb-fa)*u/2.0, fc+(fd-fc)*v/2.0);
|
|
errDoc = std::max(errDoc, (s-gg[v][u]).norm()); // points[v][u] -> S(u,v)
|
|
errT = std::max(errT, (s-gg[u][v]).norm()); // transposed
|
|
}
|
|
}
|
|
report(errDoc<=1e-9, "surface.from_grid",
|
|
"n_cp=(%d,%d) deg=(%d,%d) residual_doc(v,u)=%.3e residual_transposed=%.3e",
|
|
FIT.num_control_points()[0], FIT.num_control_points()[1], FIT.degree_u(), FIT.degree_v(), errDoc, errT);
|
|
} catch (const std::exception& e) { report(false, "surface_ctor.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "surface_ctor.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// D. Surface evaluation / differential geometry
|
|
// =====================================================================
|
|
if (want("surface_eval")) { try {
|
|
auto SP = NurbsSurface::sphere(0,0,0, 2.0);
|
|
auto PL = NurbsSurface::plane(Point3D(0,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 2.0, 2.0);
|
|
auto CY = NurbsSurface::cylinder(0,0,0, 3.0, 10.0);
|
|
|
|
double fd = 0;
|
|
for (int i=1;i<8;++i) for (int j=1;j<8;++j) { double u=i/8.0, v=j/8.0, h=1e-5;
|
|
Vector3D fu = (SP.evaluate(u+h,v)-SP.evaluate(u-h,v))/(2*h);
|
|
Vector3D fv = (SP.evaluate(u,v+h)-SP.evaluate(u,v-h))/(2*h);
|
|
fd = std::max(fd, relErr(SP.derivative_u(u,v), fu));
|
|
fd = std::max(fd, relErr(SP.derivative_v(u,v), fv)); }
|
|
report(fd <= 2e-5, "surface.deriv_fd", "max rel err u,v = %.3e (central diff h=1e-5)", fd);
|
|
|
|
double cmp = 0;
|
|
for (int i=1;i<8;++i) for (int j=1;j<8;++j) { double u=i/8.0, v=j/8.0;
|
|
Point3D S; Vector3D Su,Sv,Suu,Suv,Svv;
|
|
SP.evaluate_with_derivs(u,v,&S,&Su,&Sv,&Suu,&Suv,&Svv);
|
|
cmp = std::max(cmp, (S-SP.evaluate(u,v)).norm());
|
|
cmp = std::max(cmp, (Su-SP.derivative_u(u,v)).norm());
|
|
cmp = std::max(cmp, (Suu-SP.derivative_uu(u,v)).norm());
|
|
cmp = std::max(cmp, (Suv-SP.derivative_uv(u,v)).norm());
|
|
cmp = std::max(cmp, (Svv-SP.derivative_vv(u,v)).norm()); }
|
|
report(cmp <= 1e-12, "surface.evaluate_with_derivs", "max consistency err=%.3e", cmp);
|
|
|
|
auto [H, K] = surface_curvature(SP, 0.3, 0.4);
|
|
report(std::abs(std::abs(H)-0.5)<=1e-6 && std::abs(std::abs(K)-0.25)<=1e-6, "surface.sphere_curvature",
|
|
"H=%.9f (want |H|=0.5) K=%.9f (want |K|=0.25)", H, K);
|
|
|
|
auto CM = curvature_map(SP, 12, 12);
|
|
double cmax = 0; // skip v-boundary rows: sphere poles are degenerate (normal undefined)
|
|
for (int i = 0; i < (int)CM.grid.size(); ++i)
|
|
for (int j = 1; j + 1 < (int)CM.grid[i].size(); ++j)
|
|
cmax = std::max(cmax, std::abs(std::abs(CM.grid[i][j].gaussian)-0.25));
|
|
auto CMcy = curvature_map(CY, 8, 8);
|
|
auto CMpl = curvature_map(PL, 8, 8);
|
|
auto CMsp = curvature_map(SP, 8, 8);
|
|
report(cmax<=1e-3 && CMpl.is_developable() && CMcy.is_developable() && !CMsp.is_developable(), "surface.curvature_map",
|
|
"sphere max||K|-0.25|=%.3e (poles skipped) plane developable=%d cylinder developable=%d sphere developable=%d",
|
|
cmax, (int)CMpl.is_developable(), (int)CMcy.is_developable(), (int)CMsp.is_developable());
|
|
|
|
auto GCA = gaussian_curvature_analysis(SP, 16);
|
|
report(std::abs(GCA.max_k-0.25)<=2e-3, "surface.gauss_curv_analysis",
|
|
"max_K=%.9f (want 0.25) mean_K=%.4f min=%.4f (poles diluted)", GCA.max_k, GCA.mean_k, GCA.min_k);
|
|
|
|
auto MCM = mean_curvature_map(SP);
|
|
report(std::abs(std::abs(MCM.min_h)-0.5)<=2e-3 || std::abs(std::abs(MCM.max_h)-0.5)<=2e-3, "surface.mean_curv_map",
|
|
"H range=[%.6f,%.6f] (want -0.5 non-pole) mean=%.5f", MCM.min_h, MCM.max_h, MCM.mean_h);
|
|
|
|
auto ZS = zebra_stripe(SP, Vector3D(0,0,1), 20, 20);
|
|
double zmin=1e9,zmax=-1e9; bool zin=true;
|
|
for (auto& r : ZS.intensity) for (double x : r) { zmin=std::min(zmin,x); zmax=std::max(zmax,x); if (x<-1e-12||x>1+1e-12) zin=false; }
|
|
report(zin && (zmax-zmin)>0.05, "surface.zebra_stripe", "intensity in[%.3f,%.3f] range=%.3f", zmin, zmax, zmax-zmin);
|
|
|
|
auto CC = curvature_comb(NurbsCurve::circle(Point3D(0,0,0), 2.0), 40);
|
|
double cmean = 0; int cn = 0;
|
|
for (auto& p : CC.points) { cmean += p.curvature; ++cn; }
|
|
cmean = cn ? cmean/cn : 0;
|
|
report(cn>0 && std::abs(cmean-0.5)<=1e-3, "surface.curvature_comb",
|
|
"n=%d mean_kappa=%.9f (want 1/r=0.5)", cn, cmean);
|
|
|
|
auto PLoff = offset_surface(PL, 1.0);
|
|
auto DEV = deviation_analysis(PL, PLoff, 20);
|
|
double devmag = std::max(std::abs(DEV.max_positive), std::abs(DEV.max_negative));
|
|
report(std::abs(devmag-1.0)<=1e-3, "surface.deviation_analysis",
|
|
"max_pos=%.6f max_neg=%.6f (want |dev|=1) rms=%.3e", DEV.max_positive, DEV.max_negative, DEV.rms);
|
|
|
|
auto ISO = surface_isocurve(CY, 0.25, "u");
|
|
double isoe = 0;
|
|
for (int k=0;k<=50;++k) { Point3D p = ISO.evaluate(k/50.0); isoe = std::max(isoe, std::abs(std::hypot(p.x(),p.y())-3.0)); }
|
|
report(!ISO.control_points().empty() && isoe<=1e-9, "surface.isocurve", "n_cp=%zu radial_err=%.3e", ISO.control_points().size(), isoe);
|
|
|
|
auto [pu, pv] = project_point_on_surface(Point3D(3,0,0), SP);
|
|
double ppe = (SP.evaluate(pu,pv) - Point3D(2,0,0)).norm();
|
|
auto [qu, qv] = project_point_on_surface_quick(Point3D(3,0,0), SP);
|
|
double qpe = (SP.evaluate(qu,qv) - Point3D(2,0,0)).norm();
|
|
bool inside = surface_contains_point_fast(Point3D(2,0,0), SP, 1e-6);
|
|
bool outside = surface_contains_point_fast(Point3D(3,0,0), SP, 1e-6);
|
|
report(ppe<=1e-9 && qpe<=1e-5 && inside && !outside, "surface.project_point",
|
|
"newton_err=%.3e quick_err=%.3e contains_on=%d contains_off=%d", ppe, qpe, (int)inside, (int)outside);
|
|
|
|
auto PLC = project_line_on_surface(Point3D(0.2,0.2,3), Point3D(1.8,1.8,3), PL);
|
|
// project_line_on_surface returns a p-curve in (u,v) parameter space -> lift to 3D
|
|
auto PLC3d = evaluate_pcurve_on_surface(PLC, PL, 101);
|
|
double pz = 0, pe0 = 1e9, pe1 = 1e9;
|
|
for (auto& p : PLC3d) { pz = std::max(pz, std::abs(p.z()));
|
|
pe0 = std::min(pe0, (p-Point3D(0.2,0.2,0)).norm()); pe1 = std::min(pe1, (p-Point3D(1.8,1.8,0)).norm()); }
|
|
report(!PLC3d.empty() && pz<=1e-6 && pe0<=1e-6 && pe1<=1e-6, "surface.project_line",
|
|
"n_cp=%zu n3d=%zu max|z|=%.3e end_d=(%.2e,%.2e)",
|
|
PLC.control_points().size(), PLC3d.size(), pz, pe0, pe1);
|
|
|
|
auto GE = geodesic_curve(SP, Point3D(2,0,0), Point3D(0,2,0));
|
|
double gl = curveLen(GE, 400);
|
|
report(!GE.control_points().empty() && std::abs(gl-M_PI)/(M_PI) <= 2e-2, "surface.geodesic_sphere",
|
|
"len=%.6f want=%.6f rel=%.2e", gl, M_PI, std::abs(gl-M_PI)/M_PI);
|
|
|
|
Point3D q(0,0,5);
|
|
double dist = SP.distance_to(q);
|
|
Point3D cp = SP.closest_point(q);
|
|
report(std::abs(dist-3.0)<=1e-6 && (cp-Point3D(0,0,2)).norm()<=1e-6, "surface.closest_point",
|
|
"dist=%.9f (want 3) closest=(%.3f,%.3f,%.3f) err=%.3e", dist, cp.x(), cp.y(), cp.z(), (cp-Point3D(0,0,2)).norm());
|
|
} catch (const std::exception& e) { report(false, "surface_eval.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "surface_eval.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// E. Surface editing: offset / trim / fairing / continuity / match / thicken
|
|
// =====================================================================
|
|
if (want("surface_edit")) { try {
|
|
auto PL = NurbsSurface::plane(Point3D(0,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 2.0, 2.0);
|
|
auto PL5 = offset_surface(PL, 5.0);
|
|
double oze = 0; for (int i=0;i<=8;++i) for(int j=0;j<=8;++j) oze = std::max(oze, std::abs(PL5.evaluate(i/8.0,j/8.0).z()-5.0));
|
|
double oa = surfArea(PL5);
|
|
report(oze<=1e-6 && std::abs(oa-4.0)<=1e-6, "surface.offset_plane", "z_err=%.3e area=%.6f (want 4)", oze, oa);
|
|
|
|
auto EXO = exact_offset(PL, 5.0);
|
|
double eoze = 0; for (int i=0;i<=8;++i) for(int j=0;j<=8;++j) eoze = std::max(eoze, std::abs(EXO.evaluate(i/8.0,j/8.0).z()-5.0));
|
|
auto OQ = offset_quality_check(PL, EXO, 5.0);
|
|
report(eoze<=1e-6 && OQ.max_error<=1e-6, "surface.exact_offset", "z_err=%.3e quality_max_err=%.3e valid=%d",
|
|
eoze, OQ.max_error, (int)OQ.is_valid);
|
|
|
|
auto TRS = trim_surface(PL, 0.2, 0.8, 0.2, 0.8);
|
|
double tra = surfArea(TRS);
|
|
report(std::abs(tra - 4.0*0.36) <= 4.0*0.36*1e-3, "surface.trim_param_range",
|
|
"area=%.6f want=%.6f rel=%.2e", tra, 4.0*0.36, std::abs(tra-1.44)/1.44);
|
|
|
|
auto SUB = extract_sub_surface(PL, 0.2, 0.8, 0.2, 0.8);
|
|
double suba = surfArea(SUB);
|
|
double subcorner = 1e9;
|
|
if (SUB.num_control_points()[0] > 0) {
|
|
auto [a,b] = surfDomU(SUB); auto [c,d] = surfDomV(SUB);
|
|
subcorner = (SUB.evaluate(a,c) - Point3D(0.4,0.4,0)).norm();
|
|
}
|
|
report(SUB.num_control_points()[0]>0 && std::abs(suba-1.44)<=1.44*1e-3 && subcorner<=1e-9, "surface.extract_sub",
|
|
"n_cp=(%d,%d) area=%.6f want=1.44 corner_err=%.3e",
|
|
SUB.num_control_points()[0], SUB.num_control_points()[1], suba, subcorner);
|
|
|
|
auto EDp = energy_diagnostics(PL);
|
|
auto EDsp = energy_diagnostics(NurbsSurface::sphere(0,0,0,2.0));
|
|
auto EDcy = energy_diagnostics(NurbsSurface::cylinder(0,0,0,3.0,10.0));
|
|
// analytic bending energy int(k1^2+k2^2)dA: plane 0; sphere r=2 -> 2*(1/4)*4pi*4; cylinder r=3,h=10 -> (1/9)*2pi*3*10
|
|
double expSp = 2.0*(1.0/4.0)*4*M_PI*4.0;
|
|
double expCy = (1.0/9.0)*2*M_PI*3.0*10.0;
|
|
report(std::abs(EDp.bending_energy)<=1e-9
|
|
&& std::abs(EDsp.bending_energy-expSp)/expSp<=0.05
|
|
&& std::abs(EDcy.bending_energy-expCy)/expCy<=0.05, "surface.energy_diag_scale",
|
|
"plane E=%.3e | sphere E=%.1f want=%.1f ratio=%.1f | cylinder E=%.1f want=%.1f ratio=%.1f",
|
|
EDp.bending_energy, EDsp.bending_energy, expSp, EDsp.bending_energy/expSp,
|
|
EDcy.bending_energy, expCy, EDcy.bending_energy/expCy);
|
|
|
|
auto bump = makeBumpPlane(7, 0.3);
|
|
auto FR = fair_surface(bump);
|
|
double shapeDev = 0, bumpAmp = 0;
|
|
for (int i=0;i<=10;++i) for (int j=0;j<=10;++j) {
|
|
Point3D a = bump.evaluate(i/10.0, j/10.0), b = FR.faired_surface.evaluate(i/10.0, j/10.0);
|
|
shapeDev = std::max(shapeDev, (a-b).norm()); bumpAmp = std::max(bumpAmp, std::abs(a.z())); }
|
|
report(FR.final_energy < FR.initial_energy && FR.initial_energy > 0,
|
|
"surface.fairing_energy", "E0=%.6e E1=%.6e reduction=%.2f%% iters=%d converged=%d max_cp_disp=%.4f data_fit=%.3e",
|
|
FR.initial_energy, FR.final_energy,
|
|
FR.initial_energy>0 ? 100.0*(1.0-FR.final_energy/FR.initial_energy) : 0.0,
|
|
FR.iterations, (int)FR.converged, FR.max_cp_displacement, FR.data_fitting_error);
|
|
report(bumpAmp > 1e-3 && shapeDev <= 0.5*bumpAmp, "surface.fairing_shape_preserved",
|
|
"bump_amp=%.4f (CP 0.3) max_shape_change=%.4f ratio=%.2f (preserve_shape=true default)",
|
|
bumpAmp, shapeDev, bumpAmp>0 ? shapeDev/bumpAmp : -1.0);
|
|
|
|
auto P1 = NurbsSurface::plane(Point3D(0,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 1.0, 1.0);
|
|
auto P2 = NurbsSurface::plane(Point3D(1,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 1.0, 1.0);
|
|
auto lvl = surface_continuity(P1, P2, 20, 1e-6);
|
|
auto P3 = NurbsSurface::plane(Point3D(1,0,0.1), Vector3D(1,0,0), Vector3D(0,1,0), 1.0, 1.0);
|
|
auto lvl_bad = surface_continuity(P1, P3, 20, 1e-6);
|
|
report((int)lvl >= (int)ContinuityLevel::G1 && lvl_bad == ContinuityLevel::None,
|
|
"surface.continuity_coplanar", "coplanar_level=%s shifted_level=%s",
|
|
to_string(lvl), to_string(lvl_bad));
|
|
|
|
bool g1 = is_g1_continuous(P1, P2, 1);
|
|
bool g2 = is_g2_continuous(P1, P2, 1);
|
|
bool g3 = is_g3_continuous(P1, P2, 1);
|
|
report(g1 && g2, "surface.g1_g2_continuous", "g1=%d g2=%d g3=%d (coplanar planes, edge=umax)", (int)g1, (int)g2, (int)g3);
|
|
|
|
auto ZA = zebra_analysis(P1, P2, 1);
|
|
report(ZA.is_continuous && ZA.achieved_level >= ContinuityLevel::G1, "surface.zebra_analysis",
|
|
"continuous=%d level=%s max_normal_angle=%.3e", (int)ZA.is_continuous, to_string(ZA.achieved_level), ZA.max_normal_angle);
|
|
|
|
// G0 match: parallel target plane 0.1 above reference, expect edge moved onto reference
|
|
auto TILT = NurbsSurface::plane(Point3D(1,0,0.1), Vector3D(1,0,0), Vector3D(0,1,0), 1.0, 1.0);
|
|
auto MSp = match_surface(TILT, P1, BoundaryEdge::UMin, MatchSurfaceOptions{});
|
|
report(MSp.g0_ok && MSp.max_position_error <= 1e-3, "surface.match_surface_g0",
|
|
"g0=%d g1=%d max_pos_err=%.3e rows=%d (target edge 0.1 above reference)",
|
|
(int)MSp.g0_ok, (int)MSp.g1_ok, MSp.max_position_error, MSp.rows_adjusted);
|
|
|
|
auto CPres = move_cp(P1, 1, 1, Vector3D(0,0,0.5));
|
|
double cpd = 0; for (int i=0;i<=8;++i) for(int j=0;j<=8;++j) cpd = std::max(cpd, (CPres.modified_surface.evaluate(i/8.0,j/8.0)-P1.evaluate(i/8.0,j/8.0)).norm());
|
|
report(CPres.points_modified==1 && cpd>1e-3, "surface.move_cp", "modified=%d max_disp=%.4f shape_change=%.4f",
|
|
CPres.points_modified, CPres.max_displacement, cpd);
|
|
|
|
auto TC = thicken_surface(PL, 2.0);
|
|
double tvol = vde::brep::analytic_volume(TC);
|
|
double mvol = vde::brep::volume(TC);
|
|
double tarea = vde::brep::analytic_surface_area(TC);
|
|
int nrev = 0; for (auto& f : TC.all_faces()) if (f.reversed) ++nrev;
|
|
report(TC.num_faces()==6 && std::abs(tvol-8.0)<=0.4 && std::abs(mvol-8.0)<=0.4, "surface.thicken",
|
|
"faces=%zu (want6) reversed=%d mesh_vol=%.4f analytic_vol=%.4f (want 8) area=%.4f (want 24)",
|
|
TC.num_faces(), nrev, mvol, tvol, tarea);
|
|
|
|
auto BM = surface_to_brep(PL);
|
|
double barea = vde::brep::analytic_surface_area(BM);
|
|
bool roundtrip = false; double rte = 1e9;
|
|
if (BM.num_faces() >= 1) {
|
|
const auto& fs = BM.face_surface(0);
|
|
rte = 0; for (int i=0;i<=8;++i) for(int j=0;j<=8;++j) rte = std::max(rte, (fs.evaluate(i/8.0,j/8.0)-PL.evaluate(i/8.0,j/8.0)).norm());
|
|
roundtrip = true;
|
|
}
|
|
auto td = BM.build_face_trim_domain(0, 0.01);
|
|
report(BM.num_faces()>=1 && roundtrip && rte<=1e-9 && std::abs(barea-4.0)<=1e-6,
|
|
"surface.to_brep_roundtrip", "faces=%zu verts=%zu area=%.6f (want 4) face_surface_err=%.3e trim_domain=%d",
|
|
BM.num_faces(), BM.num_vertices(), barea, rte, (int)td.has_value());
|
|
} catch (const std::exception& e) { report(false, "surface_edit.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "surface_edit.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// F. Trimmed surfaces
|
|
// =====================================================================
|
|
if (want("trimmed")) { try {
|
|
auto PL = NurbsSurface::plane(Point3D(0,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 2.0, 2.0);
|
|
auto TS = vde::brep::TrimmedSurface::from_rect(PL, 0.0, 1.0, 0.0, 1.0);
|
|
auto mi = TS.mass_integral();
|
|
bool in = TS.is_inside(0.5, 0.5);
|
|
auto TS2 = vde::brep::TrimmedSurface::from_rect(PL, 0.2, 0.8, 0.2, 0.8);
|
|
auto mi2 = TS2.mass_integral();
|
|
report(std::abs(mi.area-4.0)<=1e-3 && in && std::abs(mi2.area-4.0*0.36)<=1e-2,
|
|
"trimmed.from_rect", "full_area=%.6f (want 4) sub_area=%.6f (want 1.44) inside=%d",
|
|
mi.area, mi2.area, (int)in);
|
|
|
|
auto TH = vde::brep::TrimmedSurface::from_rect_with_hole(PL, 0.0,1.0,0.0,1.0, 0.5,0.5,0.2, 48);
|
|
auto mh = TH.mass_integral();
|
|
double wantA = 4.0 - M_PI*0.2*0.2*(2.0*2.0); // uv radius 0.2 -> world radius 0.4
|
|
bool holeOut = !TH.is_inside(0.5,0.5);
|
|
bool outerIn = TH.is_inside(0.05,0.05);
|
|
report(holeOut && outerIn && std::abs(mh.area-wantA)/wantA <= 0.02, "trimmed.hole",
|
|
"area=%.6f want~%.6f rel=%.2e hole_inside=%d outer_inside=%d",
|
|
mh.area, wantA, std::abs(mh.area-wantA)/wantA, (int)holeOut, (int)outerIn);
|
|
|
|
auto CY = NurbsSurface::cylinder(0,0,0, 5.0, 10.0);
|
|
auto CP = vde::brep::TrimmedSurface::from_cylinder_patch(CY, 0.0, 0.25, 0.0, 0.5);
|
|
auto mcp = CP.mass_integral();
|
|
double wantCP = 0.25*(2*M_PI*5.0)*0.5*10.0;
|
|
report(std::abs(mcp.area-wantCP)/wantCP <= 0.02, "trimmed.cylinder_patch",
|
|
"area=%.4f want=%.4f rel=%.2e", mcp.area, wantCP, std::abs(mcp.area-wantCP)/wantCP);
|
|
} catch (const std::exception& e) { report(false, "trimmed.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "trimmed.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// G. Tessellation
|
|
// =====================================================================
|
|
if (want("tess")) { try {
|
|
auto SP = NurbsSurface::sphere(0,0,0, 2.0);
|
|
// VDE-1240:球面参数域在极点塌缩,两极各一整圈三角形面积恰为 0,
|
|
// tessellate() 会剔除这些退化三角形(退化数记在 degenerate_triangles)。
|
|
// 因此正确性应查账面守恒:保留 + 剔除 == 2*res_u*res_v,而不是硬编码总数。
|
|
auto ST = SP.tessellate_checked(16, 16);
|
|
const auto& V = ST.vertices;
|
|
const auto& T = ST.triangles;
|
|
size_t wantV = 17*17, wantT = 2*16*16;
|
|
bool account = ST.ok && ST.total_triangles == wantT &&
|
|
T.size() + ST.degenerate_triangles == wantT;
|
|
// chord deviation: max | |edge-midpoint|-R | ; analytic bound R*[(1-cos(du/2))+(1-cos(dv/2))]
|
|
double dev = 0;
|
|
for (auto& tri : T) { for (int e=0;e<3;++e) { Point3D m = 0.5*(V[tri[e]]+V[tri[(e+1)%3]]); dev = std::max(dev, std::abs(m.norm()-2.0)); } }
|
|
double du_half = M_PI/16.0, dv_half = M_PI/32.0; // u: 2pi/16, v: pi/16 half-steps
|
|
double bound = 2.0*((1.0-std::cos(du_half)) + (1.0-std::cos(dv_half)));
|
|
report(V.size()==wantV && account && dev<=bound, "tess.sphere_uniform",
|
|
"verts=%zu (want %zu) tris=%zu (total %zu, culled %zu) max_chord_dev=%.6f bound=%.6f",
|
|
V.size(), wantV, T.size(), wantT, ST.degenerate_triangles, dev, bound);
|
|
|
|
auto [AV, AT] = adaptive_tessellate(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return SP.normal(u,v); }, 2, 5, 5.0);
|
|
double maxAng = 0;
|
|
for (auto& tri : AT) { for (int e=0;e<3;++e) {
|
|
// recover uv from stored points is not possible; use angle between normals via nearest samples
|
|
(void)e; }
|
|
(void)tri; }
|
|
// measure normal variation along grid: sample dense and estimate max adjacent angle as bound proxy
|
|
report(!AV.empty() && AT.size() > 100, "tess.adaptive", "verts=%zu tris=%zu max_normal_angle_unmeasurable_proxy=%.3f",
|
|
AV.size(), AT.size(), maxAng);
|
|
|
|
auto [LV, LT] = tessellate_lod(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return SP.normal(u,v); }, TessellationLOD::Low);
|
|
auto [CV, CT] = tessellate_lod(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return SP.normal(u,v); }, TessellationLOD::Coarse);
|
|
auto [FV, FT] = tessellate_lod(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return SP.normal(u,v); }, TessellationLOD::Full);
|
|
report(LT.size()==2*16*16 && CT.size()==2*8*8 && FT.size()>=LT.size(), "tess.lod",
|
|
"Full tris=%zu Low=%zu (want 512) Coarse=%zu (want 128)", FT.size(), LT.size(), CT.size());
|
|
|
|
int kept = 0; auto [TV, TT] = tessellate_trimmed(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return u <= 0.5; }, 16, 16);
|
|
for (auto& tri : TT) { bool ok = true; for (int k=0;k<3;++k) { (void)k; } }
|
|
report(TT.size() > 0 && TT.size() <= 2*16*16/2, "tess.trimmed_domain", "verts=%zu tris=%zu (half-domain, uniform full=512)", TV.size(), TT.size());
|
|
|
|
int batchTris = 0, batches = 0;
|
|
tessellate_streaming([&](double u,double v){ return SP.evaluate(u,v); }, 12, 12,
|
|
[&](TriangleBatch&& b){ batchTris += (int)b.triangles.size(); ++batches; }, 50);
|
|
report(batchTris == 2*12*12 && batches > 1, "tess.streaming", "tris=%d (want %d) batches=%d", batchTris, 2*12*12, batches);
|
|
|
|
auto [PV, PT] = adaptive_tessellate_parallel(
|
|
[&](double u,double v){ return SP.evaluate(u,v); },
|
|
[&](double u,double v){ return SP.normal(u,v); }, 2, 5, 5.0, 0);
|
|
report(!PV.empty() && !PT.empty(), "tess.adaptive_parallel", "verts=%zu tris=%zu (seq verts=%zu tris=%zu)",
|
|
PV.size(), PT.size(), AV.size(), AT.size());
|
|
|
|
auto BEZ = BezierSurface({{Point3D(0,0,0), Point3D(0,1,0)}, {Point3D(1,0,0), Point3D(1,1,1)}});
|
|
auto [BV, BT] = BEZ.tessellate(4, 5);
|
|
report(BV.size()==5*6 && BT.size()==2*4*5, "tess.bezier_surface", "verts=%zu tris=%zu", BV.size(), BT.size());
|
|
} catch (const std::exception& e) { report(false, "tess.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "tess.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// H. Surface-surface / curve-surface intersection
|
|
// =====================================================================
|
|
if (want("ssi")) { try {
|
|
auto SP = NurbsSurface::sphere(0,0,0, 2.0);
|
|
auto PLC = intersect_plane_surface(SP, Point3D(0,0,1), Vector3D(0,0,1), 32);
|
|
double maxRE = 0, maxZE = 0; size_t np = 0;
|
|
for (auto& c : PLC) for (auto& p : c.points) { ++np; maxRE = std::max(maxRE, std::abs(std::hypot(p.x(),p.y())-std::sqrt(3.0))); maxZE = std::max(maxZE, std::abs(p.z()-1.0)); }
|
|
report(np > 10 && maxRE <= 1e-3 && maxZE <= 1e-3, "ssi.plane_sphere",
|
|
"n_curves=%zu n_pts=%zu r_err=%.3e z_err=%.3e (want r=sqrt3=1.732)", PLC.size(), np, maxRE, maxZE);
|
|
|
|
auto CY = NurbsSurface::cylinder(0,0,0, 3.0, 10.0);
|
|
auto PL = NurbsSurface::plane(Point3D(0,0,5), Vector3D(1,0,0), Vector3D(0,1,0), 6.0, 6.0);
|
|
auto SS = intersect_surfaces(CY, PL, 5, 1e-4);
|
|
double srErr = 0; size_t snp = 0;
|
|
for (auto& c : SS) for (auto& p : c.points) { ++snp; srErr = std::max(srErr, std::abs(std::hypot(p.x(),p.y())-3.0)); }
|
|
report(snp > 10 && srErr <= 1e-2, "ssi.cylinder_plane", "n_curves=%zu n_pts=%zu r_err=%.3e (want 3)", SS.size(), snp, srErr);
|
|
|
|
auto RS = robust_ssi(CY, PL);
|
|
double rr = 0, rz = 0; size_t rn = 0;
|
|
for (auto& seg : RS.segments) for (auto& p : seg.points) { ++rn; rr = std::max(rr, std::abs(std::hypot(p.x(),p.y())-3.0)); rz = std::max(rz, std::abs(p.z()-5.0)); }
|
|
report(RS.success && rn > 10 && rr<=1e-5 && rz<=1e-5, "ssi.robust_ssi",
|
|
"success=%d segs=%zu pts=%zu r_err=%.3e z_err=%.3e subdiv=%d newton_conv=%d time=%.1fms",
|
|
(int)RS.success, RS.segments.size(), rn, rr, rz, RS.phase1_subdivisions, RS.phase2_converged, RS.total_time_ms);
|
|
|
|
struct LineCase { const char* name; NurbsCurve curve; };
|
|
std::vector<LineCase> lcases = {
|
|
{"x-axis", NurbsCurve::line(Point3D(-5,0,0), Point3D(5,0,0))},
|
|
{"z-axis", NurbsCurve::line(Point3D(0,0,-5), Point3D(0,0,5))},
|
|
{"diag", NurbsCurve::line(Point3D(-5,-5,0), Point3D(5,5,0))},
|
|
};
|
|
int casesOk = 0; std::string detail;
|
|
for (auto& lc : lcases) {
|
|
auto CS = curve_surface_intersection(lc.curve, SP);
|
|
int good = 0; double cse = 0;
|
|
for (auto& h : CS) { double ar = h.position.norm(); cse = std::max(cse, std::abs(ar-2.0)); if (std::abs(ar-2.0)<=1e-6) ++good; }
|
|
if (CS.size()==2 && good==2) ++casesOk;
|
|
char buf[200]; std::snprintf(buf, sizeof buf, "%s:hits=%zu(%d good,rerr=%.1e) ", lc.name, CS.size(), good, cse);
|
|
detail += buf;
|
|
}
|
|
report(casesOk == (int)lcases.size(), "ssi.curve_surface", "%s(want 2 hits on unit sphere r=2 for each secant line)", detail.c_str());
|
|
|
|
auto SIRsp = self_intersection_detection(SP);
|
|
auto SIRpl = self_intersection_detection(PL);
|
|
auto SIRcy = self_intersection_detection(NurbsSurface::cylinder(0,0,0,3.0,10.0));
|
|
auto SIRto = self_intersection_detection(NurbsSurface::torus(0,0,0,5.0,1.0));
|
|
bool allClear = !SIRsp.has_self_intersection && !SIRpl.has_self_intersection
|
|
&& !SIRcy.has_self_intersection && !SIRto.has_self_intersection;
|
|
report(allClear, "ssi.self_intersection",
|
|
"sphere=%d(regions=%zu,min_d=%.1e) plane=%d cylinder=%d torus=%d (closed-seam/pole false positives?)",
|
|
(int)SIRsp.has_self_intersection, SIRsp.regions.size(), SIRsp.global_min_distance,
|
|
(int)SIRpl.has_self_intersection, (int)SIRcy.has_self_intersection, (int)SIRto.has_self_intersection);
|
|
|
|
auto la = NurbsCurve::line(Point3D(-1,-1,0), Point3D(1,1,0));
|
|
auto lb = NurbsCurve::line(Point3D(-1,1,0), Point3D(1,-1,0));
|
|
auto PCX = parallel_curve_intersections({{0,0}}, {la}, {lb});
|
|
bool pci = false; double pcd = 1e9;
|
|
if (PCX.size()==1 && !PCX[0].empty()) { auto [ta,tb] = PCX[0][0];
|
|
pcd = (la.evaluate(ta)-lb.evaluate(tb)).norm() + (la.evaluate(ta)).norm();
|
|
pci = true; }
|
|
report(pci && pcd <= 1e-6, "ssi.curve_curve_batch", "n_pairs=%zu hit_err=%.3e", PCX.empty()?0:PCX[0].size(), pcd);
|
|
|
|
double tors = surface_torsion(SP, 0.3, 0.4);
|
|
report(std::isfinite(tors), "ssi.surface_torsion", "torsion=%.6f", tors);
|
|
} catch (const std::exception& e) { report(false, "ssi.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "ssi.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// I. Subdivision / reconstruction / parameterization
|
|
// =====================================================================
|
|
if (want("misc")) { try {
|
|
SubdivisionMesh cube;
|
|
for (int i=0;i<8;++i) cube.vertices.push_back(Point3D((i&1)?1:0, (i&2)?1:0, (i&4)?1:0));
|
|
cube.faces = {{0,2,3,1},{4,5,7,6},{0,1,5,4},{2,6,7,3},{0,4,6,2},{1,3,7,5}};
|
|
auto s1 = catmull_clark_once(cube);
|
|
auto s2 = catmull_clark(cube, 2);
|
|
report(s1.vertices.size()==8+12+6 && s1.faces.size()==24 && s2.vertices.size()>s1.vertices.size(),
|
|
"subdiv.catmull_clark", "V/E/F=8+12+6 -> verts=%zu faces=%zu iter2 verts=%zu",
|
|
s1.vertices.size(), s1.faces.size(), s2.vertices.size());
|
|
auto ns = subdivision_to_nurbs(s1);
|
|
bool fin = !ns.empty();
|
|
double nse = 0;
|
|
for (auto& s : ns) for (int i=0;i<=4;++i) for(int j=0;j<=4;++j) { Point3D p = s.evaluate(i/4.0,j/4.0);
|
|
fin = fin && finite3(p); nse = std::max(nse, p.norm()); }
|
|
report(fin && ns.size()==24, "subdiv.to_nurbs", "n_surfaces=%zu max_pt_norm=%.3f", ns.size(), nse);
|
|
|
|
auto val = evaluate_expression("sin(3.141592653589793/2)+2^2", {});
|
|
std::string err;
|
|
bool badValid = validate_expression("1+", err);
|
|
report(std::abs(val-5.0)<=1e-12 && !badValid, "param.expression", "val=%.9f bad_valid=%d err='%s'", val, (int)badValid, err.c_str());
|
|
} catch (const std::exception& e) { report(false, "misc.exception", "%s", e.what()); }
|
|
catch (...) { report(false, "misc.exception", "unknown exception"); }
|
|
}
|
|
|
|
// =====================================================================
|
|
// J. Curve API completeness (VDE-1213: 12 previously-missing APIs)
|
|
// =====================================================================
|
|
if (want("missing")) {
|
|
const NurbsCurve crc = NurbsCurve::circle(Point3D(0, 0, 0), 2.0, Vector3D::UnitZ());
|
|
const double L = curve_arc_length(crc);
|
|
const double expL = 4.0 * M_PI;
|
|
report(std::abs(L - expL) < 1e-6, "api.curve_arc_length",
|
|
"len=%.9f expect=%.9f", L, expL);
|
|
|
|
const double tmid = curve_param_at_length(crc, 0.5 * L);
|
|
report(std::abs(tmid - 0.5) < 1e-3, "api.curve_param_at_length",
|
|
"t(0.5L)=%.6f want 0.5", tmid);
|
|
|
|
double tp = -1.0;
|
|
const Point3D cp = curve_closest_point(crc, Point3D(3.0, 0.0, 0.0), &tp);
|
|
report(std::abs(cp.x() - 2.0) < 1e-6 && std::abs(cp.y()) < 1e-6,
|
|
"api.curve_closest_point", "p=(%.6f,%.6f,%.6f) t=%.4f",
|
|
cp.x(), cp.y(), cp.z(), tp);
|
|
|
|
const double kap = curve_curvature(crc, 0.3);
|
|
report(std::abs(kap - 0.5) < 1e-6, "api.curve_curvature", "kappa=%.9f want 0.5", kap);
|
|
|
|
const Vector3D ut = curve_unit_tangent(crc, 0.0);
|
|
report(std::abs(ut.norm() - 1.0) < 1e-9, "api.curve_unit_tangent", "|t|=%.9f", ut.norm());
|
|
|
|
const auto seg = split_curve(crc, 0.25);
|
|
report(std::abs(curve_arc_length(seg.first) - 0.25 * L) < 1e-3 &&
|
|
std::abs(curve_arc_length(seg.second) - 0.75 * L) < 1e-3,
|
|
"api.curve_split", "L1=%.6f L2=%.6f (L=%.6f)",
|
|
curve_arc_length(seg.first), curve_arc_length(seg.second), L);
|
|
|
|
const NurbsCurve rev = reverse_curve(crc);
|
|
const auto d0 = crc.domain();
|
|
const auto d1 = rev.domain();
|
|
const double rev_err = (crc.evaluate(d0.first) - rev.evaluate(d1.second)).norm();
|
|
report(rev_err < 1e-9, "api.curve_reverse", "start/end mismatch=%.3e", rev_err);
|
|
|
|
const NurbsCurve tail = NurbsCurve::line(Point3D(2, 0, 0), Point3D(2, 0, 2));
|
|
const NurbsCurve joined = join_curves({crc, tail});
|
|
report(!joined.control_points().empty() &&
|
|
std::abs(curve_arc_length(joined) - (L + 2.0)) < 1e-3,
|
|
"api.curve_join", "joined_len=%.6f expect=%.6f",
|
|
curve_arc_length(joined), L + 2.0);
|
|
|
|
const NurbsCurve moved = transform_curve(crc, core::translate(Vector3D(5, 0, 0)));
|
|
const Point3D m0 = moved.evaluate(0.0);
|
|
report((m0 - Point3D(7, 0, 0)).norm() < 1e-9, "api.curve_transform",
|
|
"moved_start=(%.3f,%.3f,%.3f)", m0.x(), m0.y(), m0.z());
|
|
|
|
const NurbsCurve l1 = NurbsCurve::line(Point3D(-1, 0, 0), Point3D(1, 0, 0));
|
|
const NurbsCurve l2 = NurbsCurve::line(Point3D(0, -1, 0), Point3D(0, 1, 0));
|
|
const auto hits = intersect_curves(l1, l2);
|
|
report(hits.size() == 1, "api.curve_curve_intersection", "hits=%zu", hits.size());
|
|
|
|
const auto selfs = curve_self_intersections(crc);
|
|
report(selfs.empty(), "api.curve_self_intersections",
|
|
"self_hits=%zu (circle expects 0)", selfs.size());
|
|
|
|
const NurbsCurve bead = NurbsCurve::line(Point3D(0, 0, 0), Point3D(1, 0, 0));
|
|
const NurbsCurve reduced = reduce_degree_curve(bead);
|
|
report(std::abs(curve_arc_length(reduced) - 1.0) < 1e-9, "api.degree_reduce",
|
|
"len=%.9f", curve_arc_length(reduced));
|
|
|
|
const NurbsCurve removed = remove_knot(crc, 0.25);
|
|
report(std::abs(curve_arc_length(removed) - L) < 1e-3, "api.knot_remove",
|
|
"len=%.6f", curve_arc_length(removed));
|
|
|
|
const NurbsCurve off = offset_curve(crc, 1.0);
|
|
const double off_len = curve_arc_length(off);
|
|
report(!off.control_points().empty() && std::abs(off_len - 6.0 * M_PI) < 0.02,
|
|
"api.offset_curve", "offset_r2_len=%.6f expect=%.6f", off_len, 6.0 * M_PI);
|
|
}
|
|
|
|
std::printf("\n==== SUMMARY: PASS=%d FAIL=%d ====\n", g_pass, g_fail);
|
|
return g_fail == 0 ? 0 : 1;
|
|
}
|