- 新增 tools/kernel_audit/:三个独立能力审计探针(B-Rep 106 项 / NURBS 110 项 / 网格·SDF·空间·碰撞 77 项),逐项 [PASS]/[FAIL] 与修复前后基线日志;README 说明 编译运行方式(run_audit.ps1)与判定口径。 - 新增 docs/reference/35-算法支持矩阵与完善计划.md:按 B-Rep / NURBS / 网格 / SDF / 空间 / 碰撞 / 数据交换分族的支持矩阵(✅ 支持 / 🟡 部分 / ⚪ 缺失 / 🔧 本轮修复),逐项证据、本轮修复清单(§9)、修复后探针基线(§9.2)、 剩余差距与 P0–P2 路线图(§10)、复现方式(§11)。 - CHANGELOG [1.0.5]:本轮修复摘要、验收数字、行为变化(fair_surface preserve_shape)与已知未完成项。 - .gitignore:忽略审计工具运行产物 tools/kernel_audit/out/,保留 reports/*.txt。
1315 lines
69 KiB
C++
1315 lines
69 KiB
C++
// audit_mesh.cpp — VDE kernel Phase-1 audit probe (mesh + SDF + spatial + collision).
|
|
// Scratch file under build/probe; NOT part of the repo. Prints [PASS]/[FAIL] per check.
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include <array>
|
|
#include <random>
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
#include <functional>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
|
|
#include "vde/core/point.h"
|
|
#include "vde/core/aabb.h"
|
|
#include "vde/core/triangle.h"
|
|
#include "vde/core/line.h"
|
|
#include "vde/core/polygon.h"
|
|
|
|
#include "vde/mesh/halfedge_mesh.h"
|
|
#include "vde/mesh/mesh_simplify.h"
|
|
#include "vde/mesh/mesh_smooth.h"
|
|
#include "vde/mesh/mesh_repair.h"
|
|
#include "vde/mesh/mesh_boolean.h"
|
|
#include "vde/mesh/delaunay_2d.h"
|
|
#include "vde/mesh/cdt_2d.h"
|
|
#include "vde/mesh/delaunay_3d.h"
|
|
#include "vde/mesh/alpha_shapes.h"
|
|
#include "vde/mesh/mesh_quality.h"
|
|
#include "vde/mesh/mesh_curvature.h"
|
|
#include "vde/mesh/geodesic.h"
|
|
#include "vde/mesh/slicer.h"
|
|
#include "vde/mesh/voxelize.h"
|
|
#include "vde/mesh/marching_cubes.h"
|
|
#include "vde/mesh/mesh_lod.h"
|
|
#include "vde/mesh/subdivision.h"
|
|
#include "vde/mesh/reverse_engineering.h"
|
|
|
|
#include "vde/sdf/sdf_primitives.h"
|
|
#include "vde/sdf/sdf_operations.h"
|
|
#include "vde/sdf/sdf_tree.h"
|
|
#include "vde/sdf/sdf_to_mesh.h"
|
|
#include "vde/sdf/sdf_from_mesh.h"
|
|
#include "vde/sdf/sdf_optimize.h"
|
|
#include "vde/sdf/sdf_gradient.h"
|
|
|
|
#include "vde/spatial/bvh.h"
|
|
#include "vde/spatial/incremental_bvh.h"
|
|
#include "vde/spatial/octree.h"
|
|
#include "vde/spatial/kd_tree.h"
|
|
#include "vde/spatial/r_tree.h"
|
|
#include "vde/spatial/broadphase.h"
|
|
|
|
#include "vde/collision/gjk.h"
|
|
#include "vde/collision/epa.h"
|
|
#include "vde/collision/sat.h"
|
|
#include "vde/collision/ray_intersect.h"
|
|
#include "vde/collision/tri_intersect.h"
|
|
#include "vde/collision/closest_point.h"
|
|
#include "vde/collision/ccd.h"
|
|
|
|
#include "vde/brep/brep.h"
|
|
#include "vde/brep/modeling.h"
|
|
|
|
using vde::core::Point2D;
|
|
using vde::core::Point3D;
|
|
using vde::core::Vector2D;
|
|
using vde::core::Vector3D;
|
|
using vde::core::AABB3D;
|
|
using vde::core::Triangle3D;
|
|
using vde::core::Ray3Dd;
|
|
namespace mesh = vde::mesh;
|
|
namespace sdf = vde::sdf;
|
|
namespace spatial = vde::spatial;
|
|
namespace collision = vde::collision;
|
|
namespace brep = vde::brep;
|
|
|
|
static constexpr double kPi = 3.14159265358979323846;
|
|
|
|
static std::string g_filter;
|
|
static int g_pass = 0, g_fail = 0;
|
|
static void pass(const char* n, const std::string& m) {
|
|
std::printf("[PASS] %s: %s\n", n, m.c_str()); std::fflush(stdout); ++g_pass;
|
|
}
|
|
static void fail(const char* n, const std::string& m) {
|
|
std::printf("[FAIL] %s: %s\n", n, m.c_str()); std::fflush(stdout); ++g_fail;
|
|
}
|
|
static void report(const char* n, bool ok, const std::string& m) { ok ? pass(n, m) : fail(n, m); }
|
|
|
|
static void run(const char* name, const std::function<void()>& fn) {
|
|
if (!g_filter.empty()) {
|
|
bool neg = g_filter[0] == '!';
|
|
std::string f = neg ? g_filter.substr(1) : g_filter;
|
|
bool match = std::string(name).find(f) != std::string::npos;
|
|
if (neg ? match : !match) return;
|
|
}
|
|
try { fn(); }
|
|
catch (const std::exception& e) { fail(name, std::string("EXCEPTION: ") + e.what()); }
|
|
catch (...) { fail(name, "EXCEPTION: unknown"); }
|
|
}
|
|
|
|
// ───────────────────────── mesh helpers ─────────────────────────
|
|
static mesh::HalfedgeMesh make_cube_he(double side = 1.0) {
|
|
double h = side * 0.5;
|
|
std::vector<Point3D> v = {
|
|
{-h,-h,-h}, { h,-h,-h}, { h, h,-h}, {-h, h,-h},
|
|
{-h,-h, h}, { h,-h, h}, { h, h, h}, {-h, h, h}};
|
|
std::vector<std::array<int,3>> t = {
|
|
{0,3,2},{0,2,1}, // -z
|
|
{4,5,6},{4,6,7}, // +z
|
|
{0,1,5},{0,5,4}, // -y
|
|
{3,7,6},{3,6,2}, // +y
|
|
{0,4,7},{0,7,3}, // -x
|
|
{1,2,6},{1,6,5}}; // +x
|
|
mesh::HalfedgeMesh m;
|
|
m.build_from_triangles(v, t);
|
|
return m;
|
|
}
|
|
static mesh::HalfedgeMesh make_cube_soup(double side = 1.0) {
|
|
auto base = make_cube_he(side);
|
|
std::vector<Point3D> v;
|
|
std::vector<std::array<int,3>> t;
|
|
for (size_t f = 0; f < base.num_faces(); ++f) {
|
|
auto fv = base.face_vertices((int)f);
|
|
int i0 = (int)v.size();
|
|
for (int idx : fv) v.push_back(base.vertex((size_t)idx));
|
|
t.push_back({i0, i0+1, i0+2});
|
|
}
|
|
mesh::HalfedgeMesh m;
|
|
m.build_from_triangles(v, t);
|
|
return m;
|
|
}
|
|
static double mesh_volume(const mesh::HalfedgeMesh& m) {
|
|
double vol = 0.0;
|
|
for (size_t f = 0; f < m.num_faces(); ++f) {
|
|
auto fv = m.face_vertices((int)f);
|
|
for (size_t i = 1; i + 1 < fv.size(); ++i) {
|
|
const auto& a = m.vertex((size_t)fv[0]);
|
|
const auto& b = m.vertex((size_t)fv[i]);
|
|
const auto& c = m.vertex((size_t)fv[i+1]);
|
|
vol += a.dot(b.cross(c)) / 6.0;
|
|
}
|
|
}
|
|
return vol;
|
|
}
|
|
static double mesh_area(const mesh::HalfedgeMesh& m) {
|
|
double a = 0.0;
|
|
for (size_t f = 0; f < m.num_faces(); ++f) {
|
|
auto fv = m.face_vertices((int)f);
|
|
for (size_t i = 1; i + 1 < fv.size(); ++i) {
|
|
const auto& p0 = m.vertex((size_t)fv[0]);
|
|
a += 0.5 * ((m.vertex((size_t)fv[i]) - p0).cross(m.vertex((size_t)fv[i+1]) - p0)).norm();
|
|
}
|
|
}
|
|
return a;
|
|
}
|
|
static int boundary_edges(const mesh::HalfedgeMesh& m) {
|
|
int c = 0;
|
|
size_t nh = 2 * m.num_edges();
|
|
for (size_t i = 0; i < nh; ++i) if (m.is_boundary_edge((int)i)) ++c;
|
|
return c / 2;
|
|
}
|
|
static int topology_errors(const mesh::HalfedgeMesh& m) {
|
|
int bad = 0;
|
|
size_t nh = 2 * m.num_edges();
|
|
for (size_t i = 0; i < nh; ++i) {
|
|
const auto& he = m.halfedge(i);
|
|
if (he.face_index < 0) continue;
|
|
if (he.opposite_index < 0 || (size_t)he.opposite_index >= nh) { ++bad; continue; }
|
|
const auto& op = m.halfedge((size_t)he.opposite_index);
|
|
if (op.opposite_index != (int)i) ++bad;
|
|
}
|
|
return bad;
|
|
}
|
|
static mesh::HalfedgeMesh from_mc(const mesh::MCMesh& mc) {
|
|
mesh::HalfedgeMesh m;
|
|
m.build_from_triangles(mc.vertices, mc.triangles);
|
|
return m;
|
|
}
|
|
static mesh::HalfedgeMesh bmesh(const brep::BrepModel& b, double defl = 0.05) {
|
|
return mesh::weld_and_unify_orientation(b.to_mesh(defl));
|
|
}
|
|
// Lightweight watertight unit sphere from SDF marching cubes (shared-vertex, ~2k tris).
|
|
static mesh::HalfedgeMesh mc_sphere_mesh(int res = 32) {
|
|
auto mc = sdf::sdf_to_mesh(sdf::SdfNode::sphere(1.0), res);
|
|
return from_mc(mc);
|
|
}
|
|
static double rnd01(std::mt19937& g) { return std::uniform_real_distribution<double>(0.0, 1.0)(g); }
|
|
|
|
static double tet_volume(const Point3D& a, const Point3D& b, const Point3D& c, const Point3D& d) {
|
|
return std::abs((b - a).dot((c - a).cross(d - a))) / 6.0;
|
|
}
|
|
static double cross2(const Point2D& a, const Point2D& b, const Point2D& c) {
|
|
return (b.x()-a.x())*(c.y()-a.y()) - (b.y()-a.y())*(c.x()-a.x());
|
|
}
|
|
static int convex_hull_count(std::vector<Point2D> p) {
|
|
std::sort(p.begin(), p.end(), [](const Point2D& a, const Point2D& b) {
|
|
return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y()); });
|
|
std::vector<Point2D> h;
|
|
auto push = [&](const Point2D& q) {
|
|
while (h.size() >= 2 && cross2(h[h.size()-2], h.back(), q) <= 1e-14) h.pop_back();
|
|
h.push_back(q);
|
|
};
|
|
for (auto& q : p) push(q);
|
|
size_t lo = h.size();
|
|
for (int i = (int)p.size()-2; i >= 0; --i) push(p[(size_t)i]);
|
|
if (h.size() > 1) h.pop_back();
|
|
return (int)h.size();
|
|
}
|
|
static double polygon_area(const std::vector<Point2D>& p) {
|
|
double a = 0.0;
|
|
for (size_t i = 0; i < p.size(); ++i) {
|
|
const auto& q = p[i]; const auto& r = p[(i+1)%p.size()];
|
|
a += q.x()*r.y() - r.x()*q.y();
|
|
}
|
|
return 0.5 * a;
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
int main(int argc, char** argv) {
|
|
std::setvbuf(stdout, nullptr, _IONBF, 0);
|
|
if (argc > 1) g_filter = argv[1];
|
|
std::printf("=== VDE kernel audit: mesh/sdf/spatial/collision (phase 1) ===\n");
|
|
|
|
// ═══════════ 1) HALFEDGE MESH ═══════════
|
|
run("mesh.build_counts", [] {
|
|
auto m = make_cube_he(1.0);
|
|
std::ostringstream s;
|
|
s << "V=" << m.num_vertices() << " E=" << m.num_edges() << " F=" << m.num_faces();
|
|
bool ok = m.num_vertices() == 8 && m.num_edges() == 18 && m.num_faces() == 12;
|
|
report("mesh.build_counts", ok, s.str() + " (expect 8/18/12)");
|
|
});
|
|
run("mesh.euler", [] {
|
|
auto m = make_cube_he();
|
|
long long euler = (long long)m.num_vertices() - (long long)m.num_edges() + (long long)m.num_faces();
|
|
report("mesh.euler", euler == 2, "V-E+F=" + std::to_string(euler) + " (expect 2)");
|
|
});
|
|
run("mesh.face_normal", [] {
|
|
auto m = make_cube_he();
|
|
double len = m.face_normal(0).norm();
|
|
Vector3D n = m.face_normal(0);
|
|
// face 0 is -z bottom face
|
|
bool ok = std::abs(len - 1.0) < 1e-9 && n.z() < -0.999;
|
|
std::ostringstream s; s << "|n0|=" << len << " n0=(" << n.x() << "," << n.y() << "," << n.z() << ")";
|
|
report("mesh.face_normal", ok, s.str());
|
|
});
|
|
run("mesh.vertex_normal", [] {
|
|
auto m = make_cube_he();
|
|
Vector3D n = m.vertex_normal(0);
|
|
bool unit = std::abs(n.norm() - 1.0) < 1e-9;
|
|
bool corner = n.x() < -0.5 && n.y() < -0.5 && n.z() < -0.5;
|
|
std::ostringstream s; s << "|n|=" << n.norm() << " n=(" << n.x() << "," << n.y() << "," << n.z() << ")";
|
|
report("mesh.vertex_normal", unit && corner, s.str());
|
|
});
|
|
run("mesh.area", [] {
|
|
auto m = make_cube_he(1.0);
|
|
double a = mesh_area(m);
|
|
std::ostringstream s; s.precision(12); s << "area=" << a << " (expect 6)";
|
|
report("mesh.area", std::abs(a - 6.0) < 1e-9, s.str());
|
|
});
|
|
run("mesh.volume_closed_cube", [] {
|
|
auto m = make_cube_he(1.0);
|
|
double v = mesh_volume(m);
|
|
std::ostringstream s; s.precision(12); s << "signed_volume=" << v << " (expect 1.0)";
|
|
report("mesh.volume_closed_cube", std::abs(v - 1.0) < 1e-9, s.str());
|
|
});
|
|
run("mesh.closed_boundary_manifold", [] {
|
|
auto m = make_cube_he();
|
|
int be = boundary_edges(m), te = topology_errors(m);
|
|
size_t expect_e = 3 * m.num_faces() / 2;
|
|
bool ok = be == 0 && te == 0 && m.num_edges() == expect_e;
|
|
std::ostringstream s; s << "boundary_edges=" << be << " topo_errors=" << te
|
|
<< " E=" << m.num_edges() << " 3F/2=" << expect_e;
|
|
report("mesh.closed_boundary_manifold", ok, s.str());
|
|
});
|
|
run("mesh.open_boundary", [] {
|
|
// cube missing its 2 top triangles
|
|
double h = 0.5;
|
|
std::vector<Point3D> v = {{-h,-h,-h},{h,-h,-h},{h,h,-h},{-h,h,-h},{-h,-h,h},{h,-h,h},{h,h,h},{-h,h,h}};
|
|
std::vector<std::array<int,3>> t = {{0,3,2},{0,2,1},{0,1,5},{0,5,4},{3,7,6},{3,6,2},{0,4,7},{0,7,3},{1,2,6},{1,6,5}};
|
|
mesh::HalfedgeMesh m; m.build_from_triangles(v, t);
|
|
int be = boundary_edges(m);
|
|
bool bv = m.is_boundary_vertex(4) && !m.is_boundary_vertex(0) ? true : true; // record only
|
|
std::ostringstream s; s << "boundary_edges=" << be << " (expect 4) vertex4_boundary=" << m.is_boundary_vertex(4);
|
|
report("mesh.open_boundary", be == 4, s.str());
|
|
(void)bv;
|
|
});
|
|
run("mesh.to_mcm", [] {
|
|
auto m = make_cube_he();
|
|
auto mc = m.to_mcm();
|
|
std::ostringstream s; s << "mcm V=" << mc.vertices.size() << " T=" << mc.triangles.size();
|
|
report("mesh.to_mcm", mc.vertices.size() == 8 && mc.triangles.size() == 12, s.str());
|
|
});
|
|
run("mesh.weld_soup", [] {
|
|
auto soup = make_cube_soup(1.0);
|
|
int be_raw = boundary_edges(soup);
|
|
auto w = mesh::weld_and_unify_orientation(soup);
|
|
double v = mesh_volume(w);
|
|
std::ostringstream s; s.precision(12);
|
|
s << "soup V=" << soup.num_vertices() << " T=" << soup.num_faces() << " boundary_raw=" << be_raw
|
|
<< " -> welded V=" << w.num_vertices() << " boundary=" << boundary_edges(w) << " volume=" << v;
|
|
report("mesh.weld_soup", be_raw > 0 && w.num_vertices() == 8 && boundary_edges(w) == 0 && std::abs(v - 1.0) < 1e-6, s.str());
|
|
});
|
|
run("mesh.repair_hole", [] {
|
|
double h = 0.5;
|
|
std::vector<Point3D> v = {{-h,-h,-h},{h,-h,-h},{h,h,-h},{-h,h,-h},{-h,-h,h},{h,-h,h},{h,h,h},{-h,h,h}};
|
|
std::vector<std::array<int,3>> t = {{0,3,2},{0,2,1},{0,1,5},{0,5,4},{3,7,6},{3,6,2},{0,4,7},{0,7,3},{1,2,6},{1,6,5}};
|
|
mesh::HalfedgeMesh m; m.build_from_triangles(v, t);
|
|
auto rep = mesh::mesh_healing_report(m);
|
|
auto fixed = mesh::repair_mesh(m);
|
|
auto rep2 = mesh::mesh_healing_report(fixed);
|
|
double v2 = mesh_volume(fixed);
|
|
std::ostringstream s; s.precision(5);
|
|
s << "report(before): holes=" << rep.hole_count << " watertight=" << rep.is_watertight
|
|
<< " boundary=" << rep.boundary_edges << " | after: holes=" << rep2.hole_count
|
|
<< " watertight=" << rep2.is_watertight << " volume=" << v2;
|
|
bool ok = rep.hole_count >= 1 && !rep.is_watertight && rep2.hole_count == 0 && rep2.is_watertight
|
|
&& std::abs(v2 - 1.0) < 1e-6;
|
|
report("mesh.repair_hole", ok, s.str());
|
|
});
|
|
run("mesh.automatic_hole_filling", [] {
|
|
// open cube (missing 2 top triangles) -> 1 hole, 4 boundary edges
|
|
double h = 0.5;
|
|
std::vector<Point3D> v = {{-h,-h,-h},{h,-h,-h},{h,h,-h},{-h,h,-h},{-h,-h,h},{h,-h,h},{h,h,h},{-h,h,h}};
|
|
std::vector<std::array<int,3>> t = {{0,3,2},{0,2,1},{0,1,5},{0,5,4},{3,7,6},{3,6,2},{0,4,7},{0,7,3},{1,2,6},{1,6,5}};
|
|
mesh::HalfedgeMesh broken; broken.build_from_triangles(v, t);
|
|
int be_before = boundary_edges(broken);
|
|
auto filled = mesh::automatic_hole_filling(broken);
|
|
int be_after = boundary_edges(filled);
|
|
double v1 = mesh_volume(filled);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "boundary before=" << be_before << " after=" << be_after
|
|
<< " F " << broken.num_faces() << " -> " << filled.num_faces() << " volume=" << v1 << " (expect 1)";
|
|
report("mesh.automatic_hole_filling", be_before == 4 && be_after == 0 && std::abs(v1 - 1.0) < 1e-6, s.str());
|
|
});
|
|
run("mesh.bounds_transform", [] {
|
|
auto m = make_cube_he(2.0);
|
|
auto b = m.bounds();
|
|
m.set_vertex(0, Point3D(-2.0, -1.0, -1.0));
|
|
auto b2 = m.bounds();
|
|
std::ostringstream s; s.precision(4);
|
|
s << "bounds min=(" << b.min().x() << "," << b.min().y() << "," << b.min().z() << ") max=("
|
|
<< b.max().x() << "," << b.max().y() << "," << b.max().z() << ") | after set_vertex xmin=" << b2.min().x();
|
|
report("mesh.bounds_transform", std::abs(b.max().x()-1.0) < 1e-12 && std::abs(b2.min().x()+2.0) < 1e-12, s.str());
|
|
});
|
|
run("mesh.brep_box_to_mesh", [] {
|
|
auto b = brep::make_box(1,1,1);
|
|
auto raw = b.to_mesh(0.05);
|
|
auto m = bmesh(b);
|
|
double vol = mesh_volume(m);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "BRep faces=" << b.num_faces() << " raw mesh V=" << raw.num_vertices() << " F=" << raw.num_faces()
|
|
<< " | welded V=" << m.num_vertices() << " F=" << m.num_faces()
|
|
<< " boundary=" << boundary_edges(m) << " volume=" << vol << " valid=" << b.is_valid();
|
|
report("mesh.brep_box_to_mesh", m.num_vertices() > 0 && boundary_edges(m) == 0 && std::abs(vol - 1.0) < 1e-3, s.str());
|
|
});
|
|
run("mesh.brep_sphere_to_mesh", [] {
|
|
auto b = brep::make_sphere(1.0, 16, 8);
|
|
auto raw = b.to_mesh(0.2);
|
|
auto m = mesh::weld_and_unify_orientation(raw);
|
|
double vol = std::abs(mesh_volume(m));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "raw F=" << raw.num_faces() << " -> welded V=" << m.num_vertices() << " F=" << m.num_faces()
|
|
<< " boundary=" << boundary_edges(m) << " volume=" << vol
|
|
<< " analytic=" << (4.0/3.0*kPi);
|
|
report("mesh.brep_sphere_to_mesh", boundary_edges(m) == 0 && std::abs(vol - 4.0/3.0*kPi) < 0.15, s.str());
|
|
});
|
|
|
|
// ═══════════ 2) TRIANGULATION ═══════════
|
|
{
|
|
std::mt19937 gen(12345);
|
|
std::vector<Point2D> pts;
|
|
for (int i = 0; i < 100; ++i) pts.push_back(Point2D(rnd01(gen), rnd01(gen)));
|
|
auto hull_n = convex_hull_count(pts);
|
|
auto hull_area = [&] {
|
|
auto p = pts; std::sort(p.begin(), p.end(), [](const Point2D& a, const Point2D& b) {
|
|
return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y()); });
|
|
std::vector<Point2D> h;
|
|
auto push = [&](const Point2D& q) {
|
|
while (h.size() >= 2 && cross2(h[h.size()-2], h.back(), q) <= 0) h.pop_back();
|
|
h.push_back(q);
|
|
};
|
|
for (auto& q : p) push(q);
|
|
for (int i = (int)p.size()-2; i >= 0; --i) push(p[(size_t)i]);
|
|
if (h.size() > 1) h.pop_back();
|
|
return std::abs(polygon_area(h));
|
|
}();
|
|
auto res = mesh::delaunay_2d(pts);
|
|
run("tri.delaunay2d_counts", [&] {
|
|
int expect = 2*100 - 2 - hull_n;
|
|
std::ostringstream s; s << "n=100 hull=" << hull_n << " tris=" << res.triangles.size()
|
|
<< " expect=" << expect << " (2n-2-h)";
|
|
report("tri.delaunay2d_counts", (int)res.triangles.size() == expect, s.str());
|
|
});
|
|
run("tri.delaunay2d_area", [&] {
|
|
double a = 0.0;
|
|
std::vector<Point2D> rv = pts;
|
|
for (auto& tr : res.triangles) {
|
|
const auto& p0 = rv[(size_t)tr[0]]; const auto& p1 = rv[(size_t)tr[1]]; const auto& p2 = rv[(size_t)tr[2]];
|
|
a += 0.5 * std::abs(cross2(p0, p1, p2));
|
|
}
|
|
std::ostringstream s; s.precision(12);
|
|
s << "sum_tri_area=" << a << " hull_area=" << hull_area << " diff=" << (a - hull_area);
|
|
report("tri.delaunay2d_area", std::abs(a - hull_area) < 1e-9, s.str());
|
|
});
|
|
run("tri.delaunay2d_empty_circle", [&] {
|
|
int violations = 0;
|
|
for (auto& tr : res.triangles) {
|
|
const auto& a = pts[(size_t)tr[0]]; const auto& b = pts[(size_t)tr[1]]; const auto& c = pts[(size_t)tr[2]];
|
|
double d = 2.0 * (a.x()*(b.y()-c.y()) + b.x()*(c.y()-a.y()) + c.x()*(a.y()-b.y()));
|
|
if (std::abs(d) < 1e-14) continue;
|
|
double ux = ((a.x()*a.x()+a.y()*a.y())*(b.y()-c.y()) + (b.x()*b.x()+b.y()*b.y())*(c.y()-a.y())
|
|
+ (c.x()*c.x()+c.y()*c.y())*(a.y()-b.y())) / d;
|
|
double uy = ((a.x()*a.x()+a.y()*a.y())*(c.x()-b.x()) + (b.x()*b.x()+b.y()*b.y())*(a.x()-c.x())
|
|
+ (c.x()*c.x()+c.y()*c.y())*(b.x()-a.x())) / d;
|
|
double r2 = (a.x()-ux)*(a.x()-ux) + (a.y()-uy)*(a.y()-uy);
|
|
for (size_t k = 0; k < pts.size(); ++k) {
|
|
if ((int)k == tr[0] || (int)k == tr[1] || (int)k == tr[2]) continue;
|
|
double dd = (pts[k].x()-ux)*(pts[k].x()-ux) + (pts[k].y()-uy)*(pts[k].y()-uy);
|
|
if (dd < r2 - 1e-9) ++violations;
|
|
}
|
|
}
|
|
report("tri.delaunay2d_empty_circle", violations == 0,
|
|
"circumcircle_violations=" + std::to_string(violations) + " (expect 0)");
|
|
});
|
|
run("tri.cdt_hole", [&] {
|
|
std::vector<Point2D> cp = {{0,0},{1,0},{1,1},{0,1},{0.3,0.3},{0.7,0.3},{0.7,0.7},{0.3,0.7}};
|
|
std::vector<std::pair<int,int>> cons = {{0,1},{1,2},{2,3},{3,0},{4,5},{5,6},{6,7},{7,4}};
|
|
auto r = mesh::constrained_delaunay_2d(cp, cons);
|
|
double area_all = 0.0, area_hole_removed = 0.0;
|
|
for (auto& tr : r.triangles) {
|
|
const auto& a = cp[(size_t)tr[0]]; const auto& b = cp[(size_t)tr[1]]; const auto& c = cp[(size_t)tr[2]];
|
|
double ar = 0.5 * std::abs(cross2(a,b,c));
|
|
area_all += ar;
|
|
double cx = (a.x()+b.x()+c.x())/3.0, cy = (a.y()+b.y()+c.y())/3.0;
|
|
bool in_hole = cx > 0.3+1e-12 && cx < 0.7-1e-12 && cy > 0.3+1e-12 && cy < 0.7-1e-12;
|
|
if (!in_hole) area_hole_removed += ar;
|
|
}
|
|
auto has_edge = [&](int i, int j) {
|
|
for (auto& tr : r.triangles)
|
|
for (int k = 0; k < 3; ++k) {
|
|
int x = tr[(size_t)k], y = tr[(size_t)((k+1)%3)];
|
|
if ((x==i&&y==j)||(x==j&&y==i)) return true;
|
|
}
|
|
return false;
|
|
};
|
|
int missing = 0;
|
|
for (auto& e : cons) if (!has_edge(e.first, e.second)) ++missing;
|
|
std::ostringstream s; s.precision(6);
|
|
s << "tris=" << r.triangles.size() << " area_all=" << area_all
|
|
<< " area_wo_hole=" << area_hole_removed << " (expect 0.84) constraint_edges_missing=" << missing;
|
|
report("tri.cdt_hole", std::abs(area_hole_removed - 0.84) < 1e-9 && missing == 0, s.str());
|
|
});
|
|
run("tri.polygon_earclip", [&] {
|
|
std::vector<Point2D> p = {{0,0},{2,0},{2,1},{1,1},{1,2},{0,2}};
|
|
vde::core::Polygon2D poly(p);
|
|
auto tris = poly.triangulate();
|
|
double a = 0.0;
|
|
for (auto& tr : tris) a += 0.5 * std::abs(cross2(p[(size_t)tr[0]], p[(size_t)tr[1]], p[(size_t)tr[2]]));
|
|
std::ostringstream s; s.precision(9);
|
|
s << "polygon_area=" << poly.area() << " tris=" << tris.size() << " (expect n-2=4) tri_area_sum=" << a;
|
|
report("tri.polygon_earclip", (int)tris.size() == 4 && std::abs(a - 3.0) < 1e-9, s.str());
|
|
});
|
|
}
|
|
run("tri.delaunay3d", [] {
|
|
// cube corners + center: 12 tets of volume 1/12, total exactly 1.0
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i < 8; ++i)
|
|
pts.push_back(Point3D((i&1)?0.5:-0.5, (i&2)?0.5:-0.5, (i&4)?0.5:-0.5));
|
|
pts.push_back(Point3D(0,0,0));
|
|
auto tm = mesh::delaunay_3d(pts);
|
|
double sum = 0.0, ssum = 0.0;
|
|
for (auto& t : tm.tetrahedra) {
|
|
sum += tet_volume(pts[(size_t)t[0]], pts[(size_t)t[1]], pts[(size_t)t[2]], pts[(size_t)t[3]]);
|
|
ssum += (pts[(size_t)t[1]]-pts[(size_t)t[0]]).dot(
|
|
(pts[(size_t)t[2]]-pts[(size_t)t[0]]).cross(pts[(size_t)t[3]]-pts[(size_t)t[0]])) / 6.0;
|
|
}
|
|
std::ostringstream s; s.precision(6);
|
|
s << "tets=" << tm.tetrahedra.size() << " (expect 12) sum_abs_tet_volume=" << sum
|
|
<< " (expect 1.0) signed=" << ssum;
|
|
report("tri.delaunay3d", tm.tetrahedra.size() == 12 && std::abs(sum - 1.0) < 1e-9, s.str());
|
|
});
|
|
run("tri.alpha_shapes", [] {
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i < 8; ++i)
|
|
pts.push_back(Point3D((i&1)?0.5:-0.5, (i&2)?0.5:-0.5, (i&4)?0.5:-0.5));
|
|
auto tets = mesh::alpha_shapes(pts, 10.0);
|
|
auto surf = mesh::alpha_shapes_surface(tets);
|
|
mesh::HalfedgeMesh raw;
|
|
raw.build_from_triangles(surf.first, surf.second);
|
|
int be_raw = boundary_edges(raw);
|
|
auto w = mesh::weld_and_unify_orientation(raw);
|
|
int be_w = boundary_edges(w);
|
|
double v = mesh_volume(w);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "alpha=10 tets=" << tets.tetrahedra.size() << " surf_tris=" << surf.second.size()
|
|
<< " boundary raw=" << be_raw << " welded=" << be_w << " volume=" << v << " (expect 1)";
|
|
report("tri.alpha_shapes", tets.tetrahedra.size() > 0 && std::abs(v - 1.0) < 1e-6, s.str());
|
|
});
|
|
run("mesh.quality_metrics", [] {
|
|
auto m = make_cube_he();
|
|
auto q = mesh::evaluate_mesh_quality(m);
|
|
auto rep = mesh::mesh_quality_report(m);
|
|
std::ostringstream s; s.precision(4);
|
|
s << "min_angle=" << q.min_angle_deg << " max_angle=" << q.max_angle_deg
|
|
<< " avg_aspect=" << q.avg_aspect_ratio << " degenerate=" << q.degenerate_faces
|
|
<< " | report n=" << rep.total << " avg_quality=" << rep.avg_quality;
|
|
report("mesh.quality_metrics", std::abs(q.min_angle_deg - 45.0) < 1e-6 && std::abs(q.max_angle_deg - 90.0) < 1e-6, s.str());
|
|
});
|
|
|
|
// ═══════════ 3) MESH PROCESSING ═══════════
|
|
run("mesh.qem_simplify", [] {
|
|
auto sphere = mc_sphere_mesh(32);
|
|
size_t n0 = sphere.num_faces();
|
|
double v0 = std::abs(mesh_volume(sphere));
|
|
mesh::SimplifyOptions opt; opt.target_ratio = 0.2; opt.max_iterations = 5000; opt.preserve_boundary = true;
|
|
auto sim = mesh::simplify_mesh(sphere, opt);
|
|
size_t n1 = sim.num_faces();
|
|
double v1 = std::abs(mesh_volume(sim));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "faces " << n0 << " -> " << n1 << " (target " << (size_t)(n0*0.2) << ")"
|
|
<< " volume " << v0 << " -> " << v1 << " drift=" << (100.0*std::abs(v1-v0)/v0) << "%";
|
|
report("mesh.qem_simplify", n1 < n0 && n1 <= (size_t)(n0*0.4) && n1 > 4, s.str());
|
|
});
|
|
run("mesh.qem_volume_drift", [] {
|
|
auto sphere = mc_sphere_mesh(32);
|
|
double v0 = std::abs(mesh_volume(sphere));
|
|
mesh::SimplifyOptions opt; opt.target_ratio = 0.3; opt.max_iterations = 5000;
|
|
auto sim = mesh::simplify_mesh(sphere, opt);
|
|
double v1 = std::abs(mesh_volume(sim));
|
|
double drift = 100.0 * std::abs(v1 - v0) / v0;
|
|
std::ostringstream s; s.precision(5);
|
|
s << "volume " << v0 << " -> " << v1 << " drift=" << drift << "% (expect <5%) F=" << sphere.num_faces() << "->" << sim.num_faces();
|
|
report("mesh.qem_volume_drift", drift < 5.0, s.str());
|
|
});
|
|
run("mesh.smooth_laplacian", [] {
|
|
auto noisy = mc_sphere_mesh(32);
|
|
for (size_t i = 0; i < noisy.num_vertices(); ++i) {
|
|
auto p = noisy.vertex(i);
|
|
double r = p.norm();
|
|
double nz = 0.04 * std::sin((double)i * 12.9898);
|
|
noisy.set_vertex(i, p * ((r + nz) / r));
|
|
}
|
|
// 无偏噪声度量:只看到"半径相对均值"的散布(Laplacian 会整体收缩,
|
|
// 用绝对半径 1.0 的偏差会被均匀收缩偏置,误判为噪声增大)。
|
|
auto rms = [](const mesh::HalfedgeMesh& m) {
|
|
const double inv_n = 1.0 / (double)m.num_vertices();
|
|
double mean = 0.0;
|
|
for (size_t i = 0; i < m.num_vertices(); ++i) mean += m.vertex(i).norm();
|
|
mean *= inv_n;
|
|
double s = 0.0;
|
|
for (size_t i = 0; i < m.num_vertices(); ++i) {
|
|
const double d = m.vertex(i).norm() - mean;
|
|
s += d * d;
|
|
}
|
|
return std::sqrt(s * inv_n);
|
|
};
|
|
double n0 = rms(noisy), v0 = std::abs(mesh_volume(noisy));
|
|
auto sm = mesh::smooth_laplacian(noisy, 10, 0.5);
|
|
double n1 = rms(sm), v1 = std::abs(mesh_volume(sm));
|
|
std::ostringstream s; s.precision(5);
|
|
s << "unbiased_noise_rms " << n0 << " -> " << n1 << " volume " << v0 << " -> " << v1;
|
|
report("mesh.smooth_laplacian", n1 < n0, s.str());
|
|
});
|
|
run("mesh.smooth_taubin_volume", [] {
|
|
auto noisy = mc_sphere_mesh(32);
|
|
for (size_t i = 0; i < noisy.num_vertices(); ++i) {
|
|
auto p = noisy.vertex(i); double r = p.norm();
|
|
double nz = 0.04 * std::sin((double)i * 7.13);
|
|
noisy.set_vertex(i, p * ((r + nz) / r));
|
|
}
|
|
auto rms = [](const mesh::HalfedgeMesh& m) {
|
|
double s = 0.0;
|
|
for (size_t i = 0; i < m.num_vertices(); ++i) { double d = m.vertex(i).norm() - 1.0; s += d*d; }
|
|
return std::sqrt(s / (double)m.num_vertices());
|
|
};
|
|
double n0 = rms(noisy), v0 = std::abs(mesh_volume(noisy));
|
|
auto sm = mesh::smooth_taubin(noisy, 20, 0.5, -0.53);
|
|
double n1 = rms(sm), v1 = std::abs(mesh_volume(sm));
|
|
double vdrift = 100.0 * std::abs(v1 - v0) / v0;
|
|
std::ostringstream s; s.precision(5);
|
|
s << "noise_rms " << n0 << " -> " << n1 << " volume " << v0 << " -> " << v1 << " drift=" << vdrift << "%";
|
|
report("mesh.smooth_taubin_volume", n1 < n0 && vdrift < 2.0, s.str());
|
|
});
|
|
run("mesh.subdivision", [] {
|
|
// VDE-1213/Phase-2: triangle Loop/Catmull-Clark subdivision added (mesh/subdivision.h).
|
|
auto cube = bmesh(brep::make_box(1, 1, 1));
|
|
auto fine = mesh::loop_subdivide(cube);
|
|
const double vol = std::abs(mesh_volume(fine));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "F " << cube.num_faces() << " -> " << fine.num_faces()
|
|
<< " V=" << cube.num_vertices() << " -> " << fine.num_vertices()
|
|
<< " boundary=" << boundary_edges(fine) << " volume=" << vol << " (expect 1)";
|
|
report("mesh.subdivision",
|
|
fine.num_faces() == 4 * cube.num_faces() && boundary_edges(fine) == 0 &&
|
|
std::abs(vol - 1.0) < 0.6, s.str());
|
|
// 注:Loop 细分会向邻域平均收缩(立方体 x1 体积约 0.48),故只做拓扑校验
|
|
// 与体积非退化校验;体积保持语义对近似球面网格才成立。
|
|
});
|
|
run("mesh.boolean_union", [] {
|
|
auto a = bmesh(brep::make_box(1,1,1));
|
|
auto b = bmesh(brep::make_box(1,1,1, Point3D(0.5,0,0)));
|
|
auto u = mesh::mesh_boolean(a, b, mesh::BooleanOp::Union);
|
|
auto w = mesh::weld_and_unify_orientation(u);
|
|
double v = std::abs(mesh_volume(w));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "result F=" << u.num_faces() << " welded F=" << w.num_faces() << " volume=" << v
|
|
<< " analytic=1.5 boundary=" << boundary_edges(w);
|
|
report("mesh.boolean_union", std::abs(v - 1.5) < 0.08, s.str());
|
|
});
|
|
run("mesh.boolean_intersection", [] {
|
|
auto a = bmesh(brep::make_box(1,1,1));
|
|
auto b = bmesh(brep::make_box(1,1,1, Point3D(0.5,0,0)));
|
|
auto r = mesh::mesh_boolean(a, b, mesh::BooleanOp::Intersection);
|
|
auto w = mesh::weld_and_unify_orientation(r);
|
|
double v = std::abs(mesh_volume(w));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "result F=" << r.num_faces() << " volume=" << v << " analytic=0.5 boundary=" << boundary_edges(w);
|
|
report("mesh.boolean_intersection", std::abs(v - 0.5) < 0.06, s.str());
|
|
});
|
|
run("mesh.boolean_difference", [] {
|
|
auto a = bmesh(brep::make_box(1,1,1));
|
|
auto b = bmesh(brep::make_box(1,1,1, Point3D(0.5,0,0)));
|
|
auto r = mesh::mesh_boolean(a, b, mesh::BooleanOp::Difference);
|
|
auto w = mesh::weld_and_unify_orientation(r);
|
|
double v = std::abs(mesh_volume(w));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "result F=" << r.num_faces() << " volume=" << v << " analytic=0.5 boundary=" << boundary_edges(w);
|
|
report("mesh.boolean_difference", std::abs(v - 0.5) < 0.06, s.str());
|
|
});
|
|
run("mesh.self_intersection", [] {
|
|
// two interpenetrating cube soups combined into one shell
|
|
auto a = make_cube_soup(1.0);
|
|
auto b = make_cube_he(1.0);
|
|
std::vector<Point3D> verts;
|
|
std::vector<std::array<int,3>> tris;
|
|
for (size_t i = 0; i < a.num_vertices(); ++i) verts.push_back(a.vertex(i));
|
|
for (size_t f = 0; f < a.num_faces(); ++f) {
|
|
auto fv = a.face_vertices((int)f);
|
|
tris.push_back({fv[0], fv[1], fv[2]});
|
|
}
|
|
int base = (int)verts.size();
|
|
for (size_t i = 0; i < b.num_vertices(); ++i) verts.push_back(b.vertex(i) + Vector3D(0.5, 0.3, 0.0));
|
|
for (size_t f = 0; f < b.num_faces(); ++f) {
|
|
auto fv = b.face_vertices((int)f);
|
|
tris.push_back({fv[0]+base, fv[1]+base, fv[2]+base});
|
|
}
|
|
mesh::HalfedgeMesh m; m.build_from_triangles(verts, tris);
|
|
int pairs = 0;
|
|
auto fixed = mesh::repair_self_intersections(m, &pairs);
|
|
std::ostringstream s; s << "intersecting_pairs_resolved=" << pairs << " F " << m.num_faces()
|
|
<< " -> " << fixed.num_faces();
|
|
report("mesh.self_intersection", pairs > 0, s.str());
|
|
});
|
|
run("mesh.slicing", [] {
|
|
auto m = make_cube_he(1.0);
|
|
mesh::SliceConfig cfg;
|
|
cfg.layer_thickness = 0.1; cfg.z_min = -0.499; cfg.z_max = 0.499;
|
|
auto res = mesh::slice_mesh(m, cfg);
|
|
double midperim = -1.0;
|
|
int nonempty = 0;
|
|
for (auto& L : res.layers) {
|
|
if (!L.contours.empty()) ++nonempty;
|
|
if (std::abs(L.z_height - 0.0) < 0.051 && !L.contours.empty()) {
|
|
double p = 0.0;
|
|
for (auto& c : L.contours)
|
|
for (size_t i = 0; i < c.size(); ++i) { auto d = c[(i+1)%c.size()] - c[i]; p += d.norm(); }
|
|
midperim = p;
|
|
}
|
|
}
|
|
std::ostringstream s; s.precision(5);
|
|
s << "success=" << res.success << " layers=" << res.layer_count << " nonempty=" << nonempty
|
|
<< " mid_layer_perimeter=" << midperim << " total_path=" << res.total_path_length();
|
|
report("mesh.slicing", res.success && res.layer_count >= 9 && std::abs(midperim - 4.0) < 0.2, s.str());
|
|
});
|
|
run("mesh.voxelize_mc", [] {
|
|
auto m = make_cube_he(1.0);
|
|
mesh::VoxelizeParams vp; vp.resolution = 32; vp.fill = true;
|
|
auto grid = mesh::voxelize_mesh(m, vp);
|
|
double center_val = grid.at(grid.nx/2, grid.ny/2, grid.nz/2);
|
|
double corner_val = grid.at(0, 0, 0);
|
|
auto surf = mesh::marching_cubes(grid, 0.0);
|
|
auto sm = from_mc(surf);
|
|
double vol = std::abs(mesh_volume(sm));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "grid=" << grid.nx << "^3 center_val=" << center_val << " corner_val=" << corner_val
|
|
<< " | mc F=" << surf.triangles.size() << " boundary=" << boundary_edges(sm) << " volume=" << vol;
|
|
report("mesh.voxelize_mc", center_val < 0.0 && corner_val > 0.0 && surf.triangles.size() > 0
|
|
&& std::abs(vol - 1.0) < 0.08, s.str());
|
|
});
|
|
run("mesh.geodesic", [] {
|
|
auto m = make_cube_he(1.0);
|
|
auto d = mesh::geodesic_distance(m, {0});
|
|
bool finite = true; for (double x : d) if (!std::isfinite(x)) finite = false;
|
|
double opp = d.size() > 6 ? d[6] : -1.0;
|
|
std::ostringstream s; s.precision(5);
|
|
s << "n=" << d.size() << " d[0]=" << (d.empty()?-1:d[0]) << " d[opposite]=" << opp
|
|
<< " (surface shortest ~" << std::sqrt(5.0) << ") finite=" << finite;
|
|
report("mesh.geodesic", finite && std::abs(d[0]) < 1e-9 && opp > 1.8 && opp < 3.0, s.str());
|
|
});
|
|
run("mesh.curvature", [] {
|
|
auto sphere = mc_sphere_mesh(32);
|
|
auto crv = mesh::compute_curvature(sphere);
|
|
bool finite = crv.mean.size() == sphere.num_vertices();
|
|
double sum_abs = 0.0;
|
|
for (double x : crv.mean) { if (!std::isfinite(x)) finite = false; sum_abs += std::abs(x); }
|
|
double avg_abs = crv.mean.empty() ? 0.0 : sum_abs / (double)crv.mean.size();
|
|
auto mg = mesh::mean_gaussian_curvature(sphere, 0);
|
|
std::ostringstream s; s.precision(5);
|
|
s << "verts=" << crv.mean.size() << " avg|H|=" << avg_abs << " (sphere r=1 expect ~1) H[0]="
|
|
<< (crv.mean.empty()?0:crv.mean[0]) << " K[0]=" << (crv.gaussian.empty()?0:crv.gaussian[0]);
|
|
report("mesh.curvature", finite && avg_abs > 0.6 && avg_abs < 1.6, s.str());
|
|
(void)mg;
|
|
});
|
|
run("mesh.lod", [] {
|
|
auto sphere = mc_sphere_mesh(32);
|
|
auto lod = mesh::generate_lod(sphere, 3, 0.5);
|
|
std::ostringstream s; s << "levels=" << lod.levels.size();
|
|
for (size_t i = 0; i < lod.levels.size(); ++i) s << " F" << i << "=" << lod.levels[i].num_faces();
|
|
report("mesh.lod", lod.levels.size() >= 2 && lod.levels.back().num_faces() < lod.levels.front().num_faces(), s.str());
|
|
});
|
|
run("mesh.quad_remesh", [] {
|
|
auto sphere = mc_sphere_mesh(32);
|
|
mesh::QuadRemeshParams p;
|
|
auto q = mesh::quad_remesh(sphere, p);
|
|
std::ostringstream s; s << "in F=" << sphere.num_faces() << " out F=" << q.num_faces();
|
|
report("mesh.quad_remesh", q.num_faces() > 0, s.str());
|
|
});
|
|
|
|
// ═══════════ 4) SDF ═══════════
|
|
run("sdf.sphere", [] {
|
|
double a = sdf::sphere(Point3D(0,0,0), 1.0);
|
|
double b = sdf::sphere(Point3D(2,0,0), 1.0);
|
|
double c = sdf::sphere(Point3D(1,0,0), 1.0);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "d(0)=" << a << " d(sum2,0,0)=" << b << " d(surface)=" << c;
|
|
report("sdf.sphere", std::abs(a+1.0)<1e-12 && std::abs(b-1.0)<1e-12 && std::abs(c)<1e-12, s.str());
|
|
});
|
|
run("sdf.box_roundbox", [] {
|
|
Point3D he(1,1,1);
|
|
double c = sdf::box(Point3D(0,0,0), he);
|
|
double f = sdf::box(Point3D(1.5,0,0), he);
|
|
double cor = sdf::box(Point3D(2,1,1), he);
|
|
double rb = sdf::round_box(Point3D(2,0,0), he, 0.2);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "box(center)=" << c << " box(1.5,0,0)=" << f << " box(2,1,1)=" << cor << " round_box(2,0,0,r=.2)=" << rb;
|
|
report("sdf.box_roundbox", std::abs(c+1.0)<1e-12 && std::abs(f-0.5)<1e-12 && std::abs(cor-1.0)<1e-12
|
|
&& std::abs(rb-0.8)<1e-12, s.str());
|
|
});
|
|
run("sdf.torus_capsule", [] {
|
|
double t_center = sdf::torus(Point3D(2,0,0), 2.0, 0.5);
|
|
double t_surf = sdf::torus(Point3D(2.5,0,0), 2.0, 0.5);
|
|
double t_hole = sdf::torus(Point3D(0,0,0), 2.0, 0.5);
|
|
double cap0 = sdf::capsule(Point3D(0,0,0), Point3D(0,-1,0), Point3D(0,1,0), 0.5);
|
|
double cap1 = sdf::capsule(Point3D(1,0,0), Point3D(0,-1,0), Point3D(0,1,0), 0.5);
|
|
double cap2 = sdf::capsule(Point3D(0,2,0), Point3D(0,-1,0), Point3D(0,1,0), 0.5);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "torus tube_center=" << t_center << " surface=" << t_surf << " hole_center=" << t_hole
|
|
<< " capsule mid=" << cap0 << " side=" << cap1 << " end=" << cap2;
|
|
report("sdf.torus_capsule", std::abs(t_center+0.5)<1e-12 && std::abs(t_surf)<1e-12 && std::abs(t_hole-1.5)<1e-12
|
|
&& std::abs(cap0+0.5)<1e-12 && std::abs(cap1-0.5)<1e-12 && std::abs(cap2-0.5)<1e-12, s.str());
|
|
});
|
|
run("sdf.cylinder_plane_ellipsoid", [] {
|
|
double c0 = sdf::cylinder(Point3D(0,0,0), 1.0, 2.0);
|
|
double c1 = sdf::cylinder(Point3D(1,0,0), 1.0, 2.0);
|
|
double c2 = sdf::cylinder(Point3D(0,1.5,0), 1.0, 2.0);
|
|
double pl = sdf::plane(Point3D(0,2,0), Vector3D(0,1,0), 0.0);
|
|
double el = sdf::ellipsoid(Point3D(2,0,0), Point3D(2,1,1));
|
|
double el0 = sdf::ellipsoid(Point3D(0,0,0), Point3D(2,1,1));
|
|
std::ostringstream s; s.precision(9);
|
|
s << "cyl(center)=" << c0 << " cyl(rim)=" << c1 << " cyl(above cap)=" << c2
|
|
<< " plane=" << pl << " ellipsoid(surface)=" << el << " ellipsoid(origin)=" << el0;
|
|
report("sdf.cylinder_plane_ellipsoid", std::abs(c0+1.0)<1e-12 && std::abs(c1)<1e-12 && std::abs(c2-0.5)<1e-12
|
|
&& std::abs(pl-2.0)<1e-12 && std::abs(el)<1e-12 && std::abs(el0+1.0)<1e-12, s.str());
|
|
});
|
|
run("sdf.cone", [] {
|
|
double apex = sdf::cone(Point3D(0,1,0), kPi/4.0, 2.0);
|
|
double base_center = sdf::cone(Point3D(0,-1,0), kPi/4.0, 2.0);
|
|
double base_rim = sdf::cone(Point3D(2,-1,0), kPi/4.0, 2.0);
|
|
double inside = sdf::cone(Point3D(0,0,0), kPi/4.0, 2.0);
|
|
double above = sdf::cone(Point3D(0,3,0), kPi/4.0, 2.0);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "apex=" << apex << " base_center=" << base_center << " base_rim(2,-1,0)=" << base_rim
|
|
<< " inside=" << inside << " above_apex(0,3,0)=" << above;
|
|
report("sdf.cone", std::abs(apex)<1e-9 && std::abs(base_center)<1e-9 && std::abs(base_rim)<1e-9
|
|
&& inside<0 && std::abs(above-2.0)<1e-9, s.str());
|
|
});
|
|
run("sdf.tri_prism_hex_wedge", [] {
|
|
double tp_in = sdf::triangular_prism(Point3D(0.5,0.5,0), Point3D(0,0,0), Point3D(2,0,0), Point3D(0,2,0), 1.0);
|
|
double tp_surf = sdf::triangular_prism(Point3D(0,0,0.5), Point3D(0,0,0), Point3D(2,0,0), Point3D(0,2,0), 1.0);
|
|
double hp_vertex = sdf::hex_prism(Point3D(1,0,0), 1.0, 2.0);
|
|
double hp_center = sdf::hex_prism(Point3D(0,0,0), 1.0, 2.0);
|
|
double wg = sdf::wedge(Point3D(0,0,0), 2.0, 2.0, 2.0);
|
|
double wg_in = sdf::wedge(Point3D(-0.5,0,0.5), 2.0, 2.0, 2.0);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "tri_prism inside=" << tp_in << " on_edge=" << tp_surf << " hex_prism vertex=" << hp_vertex
|
|
<< " hex center=" << hp_center << " wedge(cut plane)=" << wg << " wedge_inside=" << wg_in;
|
|
report("sdf.tri_prism_hex_wedge", tp_in<0 && std::abs(tp_surf)<1e-9 && std::abs(hp_vertex)<1e-9
|
|
&& hp_center<0 && std::abs(wg)<1e-9 && wg_in<0, s.str());
|
|
});
|
|
run("sdf.infinite_cone_link", [] {
|
|
Vector3D ax(0,1,0);
|
|
double ic_surf = sdf::infinite_cone(Point3D(1,1,0), Point3D(0,0,0), ax, kPi/4.0);
|
|
double ic_axis = sdf::infinite_cone(Point3D(0,1,0), Point3D(0,0,0), ax, kPi/4.0);
|
|
double lk_far = sdf::link(Point3D(10,0,0), 2.0, 1.0, 0.2);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "infinite_cone(45deg surf)=" << ic_surf << " on_axis=" << ic_axis << " link(far)=" << lk_far;
|
|
report("sdf.infinite_cone_link", std::abs(ic_surf)<1e-9 && ic_axis<0 && lk_far>1.0, s.str());
|
|
});
|
|
run("sdf.gradient", [] {
|
|
auto f = [](const Point3D& p) { return sdf::box(p, Point3D(1,1,1)); };
|
|
std::ostringstream s; s.precision(5);
|
|
int good = 0; double worst = 0.0;
|
|
for (Point3D p : {Point3D(2,0,0), Point3D(0,2,0), Point3D(0,0,2), Point3D(1.5,1.5,0), Point3D(1.2,0.3,0.1)}) {
|
|
double h = 1e-6;
|
|
Vector3D g;
|
|
g.x() = (f(p + Vector3D(h,0,0)) - f(p - Vector3D(h,0,0))) / (2*h);
|
|
g.y() = (f(p + Vector3D(0,h,0)) - f(p - Vector3D(0,h,0))) / (2*h);
|
|
g.z() = (f(p + Vector3D(0,0,h)) - f(p - Vector3D(0,0,h))) / (2*h);
|
|
Vector3D ga = sdf::gradient(f, p);
|
|
double err = (g - ga).norm();
|
|
worst = std::max(worst, err);
|
|
if (err < 1e-5 && std::abs(ga.norm() - 1.0) < 1e-4) ++good;
|
|
}
|
|
s << "gradient checked=5 finite-diff-match=" << good << " worst_err=" << worst;
|
|
report("sdf.gradient", good == 5, s.str());
|
|
});
|
|
run("sdf.csg_tree", [] {
|
|
auto root = sdf::SdfNode::op_union(sdf::SdfNode::sphere(1.0), sdf::SdfNode::box(Point3D(0.5,0.5,0.5)));
|
|
double u0 = sdf::evaluate(root, Point3D(1.5,0,0));
|
|
double u1 = sdf::evaluate(root, Point3D(0,0,0));
|
|
auto diff = sdf::SdfNode::op_difference(sdf::SdfNode::sphere(1.0), sdf::SdfNode::box(Point3D(0.5,0.5,0.5)));
|
|
double d0 = sdf::evaluate(diff, Point3D(0,0,0));
|
|
auto inter = sdf::SdfNode::op_intersection(sdf::SdfNode::sphere(1.0), sdf::SdfNode::box(Point3D(0.5,0.5,0.5)));
|
|
double i0 = sdf::evaluate(inter, Point3D(0.4,0,0));
|
|
auto su = sdf::SdfNode::smooth_union(sdf::SdfNode::sphere(1.0), sdf::SdfNode::sphere(1.0), 0.3);
|
|
double u2 = sdf::evaluate(su, Point3D(1.0,0,0));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "union(1.5,0,0)=" << u0 << " (expect .5) union(origin)=" << u1 << " (expect -1)"
|
|
<< " difference(origin)=" << d0 << " (expect .5) intersection(.4)=" << i0 << " (expect -.1)"
|
|
<< " smooth_union(1,0,0)=" << u2;
|
|
report("sdf.csg_tree", std::abs(u0-0.5)<1e-6 && std::abs(u1+1.0)<1e-6 && std::abs(d0-0.5)<1e-6
|
|
&& std::abs(i0+0.1)<1e-6 && u2 < u0, s.str());
|
|
});
|
|
run("sdf.modifiers_transforms", [] {
|
|
double off = sdf::op_round(sdf::sphere(Point3D(1,0,0), 1.0), 0.1);
|
|
Point3D shell_pt(1.1,0,0);
|
|
double shell = sdf::op_onion(sdf::sphere(shell_pt, 1.0), 0.1);
|
|
auto tr = sdf::SdfNode::translate(sdf::SdfNode::sphere(1.0), Point3D(2,0,0));
|
|
double tv = sdf::evaluate(tr, Point3D(3,0,0));
|
|
auto sc = sdf::SdfNode::scale(sdf::SdfNode::sphere(1.0), Point3D(2,2,2));
|
|
double sv = sdf::evaluate(sc, Point3D(1,0,0));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "op_round(surface)=-0.1? ->" << off << " op_onion(surface point)=" << shell
|
|
<< " translate(sphere(2),at 3)=" << tv << " scale(2x sphere at r=1)=" << sv;
|
|
report("sdf.modifiers_transforms", std::abs(off+0.1)<1e-9 && std::abs(shell)<1e-9
|
|
&& std::abs(tv)<1e-9 && std::abs(sv+0.5)<1e-9, s.str());
|
|
});
|
|
run("sdf.bbox_mc_sphere", [] {
|
|
auto root = sdf::SdfNode::sphere(1.0);
|
|
auto bb = sdf::estimate_bbox(root, 1.0);
|
|
auto mc = sdf::sdf_to_mesh(root, 48);
|
|
auto m = from_mc(mc);
|
|
double vol = std::abs(mesh_volume(m));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "bbox min=(" << bb.min.x() << "," << bb.min.y() << "," << bb.min.z() << ") max=("
|
|
<< bb.max.x() << "," << bb.max.y() << "," << bb.max.z() << ") | MC V=" << mc.vertices.size()
|
|
<< " T=" << mc.triangles.size() << " boundary=" << boundary_edges(m)
|
|
<< " volume=" << vol << " analytic=" << (4.0/3.0*kPi);
|
|
report("sdf.bbox_mc_sphere", boundary_edges(m) == 0 && std::abs(vol - 4.0/3.0*kPi) < 0.09, s.str());
|
|
});
|
|
run("sdf.mc_lambda_box", [] {
|
|
auto f = [](double x, double y, double z) {
|
|
double dx = std::abs(x)-0.5, dy = std::abs(y)-0.5, dz = std::abs(z)-0.5;
|
|
return std::sqrt(std::max(dx,0.0)*std::max(dx,0.0)+std::max(dy,0.0)*std::max(dy,0.0)+std::max(dz,0.0)*std::max(dz,0.0))
|
|
+ std::min(std::max({dx,dy,dz}),0.0);
|
|
};
|
|
auto mc = sdf::sdf_to_mesh_lambda(f, Point3D(-1,-1,-1), Point3D(1,1,1), 40);
|
|
auto m = from_mc(mc);
|
|
double vol = std::abs(mesh_volume(m));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "T=" << mc.triangles.size() << " boundary=" << boundary_edges(m) << " volume=" << vol << " (expect 1)";
|
|
report("sdf.mc_lambda_box", boundary_edges(m) == 0 && std::abs(vol - 1.0) < 0.03, s.str());
|
|
});
|
|
run("sdf.estimate_volume", [] {
|
|
auto root = sdf::SdfNode::sphere(1.0);
|
|
double v = sdf::estimate_volume(root, Point3D(-1,-1,-1), Point3D(1,1,1), 20000);
|
|
Point3D com = sdf::center_of_mass(root, Point3D(-1,-1,-1), Point3D(1,1,1), 5000);
|
|
std::ostringstream s; s.precision(5);
|
|
s << "MC volume=" << v << " analytic=" << (4.0/3.0*kPi) << " com=(" << com.x() << "," << com.y() << "," << com.z() << ")";
|
|
report("sdf.estimate_volume", std::abs(v - 4.0/3.0*kPi) < 0.15, s.str());
|
|
});
|
|
run("sdf.fit_point_cloud", [] {
|
|
std::vector<Point3D> pts;
|
|
int nu = 20, nv = 10;
|
|
for (int i = 0; i < nu; ++i)
|
|
for (int j = 0; j < nv; ++j) {
|
|
double th = 2*kPi*i/(double)nu, ph = kPi*(j+0.5)/(double)nv;
|
|
pts.push_back(Point3D(1.05*std::sin(ph)*std::cos(th), 1.05*std::cos(ph), 1.05*std::sin(ph)*std::sin(th)));
|
|
}
|
|
auto init = sdf::SdfNode::sphere(0.7);
|
|
auto r = sdf::fit_to_point_cloud(init, pts, 0.002, 200);
|
|
double rad = r.optimized_shape ? r.optimized_shape->params.radius : -1.0;
|
|
std::ostringstream s; s.precision(5);
|
|
s << "optimized_radius=" << rad << " (target 1.05) loss=" << r.final_loss
|
|
<< " iters=" << r.iterations << " converged=" << r.converged
|
|
<< " loss_hist_n=" << r.loss_history.size();
|
|
report("sdf.fit_point_cloud", rad > 0.95 && rad < 1.2, s.str());
|
|
});
|
|
run("sdf.penetration_depth", [] {
|
|
auto a = sdf::SdfNode::sphere(1.0);
|
|
auto b = sdf::SdfNode::translate(sdf::SdfNode::sphere(1.0), Point3D(1.5,0,0));
|
|
double pd = sdf::penetration_depth(a, b, Point3D(-1.2,-1.2,-1.2), Point3D(2.7,1.2,1.2), 4000);
|
|
std::ostringstream s; s.precision(5);
|
|
s << "penetration_depth=" << pd << " (overlap r=1 spheres d=1.5 => expected ~0.25)";
|
|
report("sdf.penetration_depth", pd > 0.0, s.str());
|
|
});
|
|
|
|
// ═══════════ 5) SPATIAL ═══════════
|
|
{
|
|
std::mt19937 gen(4242);
|
|
std::vector<Triangle3D> tris;
|
|
for (int i = 0; i < 300; ++i) {
|
|
Point3D c(rnd01(gen)*2-1, rnd01(gen)*2-1, rnd01(gen)*2-1);
|
|
double s = 0.08;
|
|
tris.push_back(Triangle3D(c, c + Vector3D(s,0,0), c + Vector3D(0,s,0)));
|
|
}
|
|
spatial::BVHBuildOptions bo;
|
|
spatial::BVH bvh(bo);
|
|
bvh.build(tris);
|
|
auto tri_box = [](const Triangle3D& t) {
|
|
AABB3D b; b.expand(t.v(0)); b.expand(t.v(1)); b.expand(t.v(2)); return b;
|
|
};
|
|
AABB3D range(Point3D(-0.3,-0.3,-0.3), Point3D(0.3,0.3,0.3));
|
|
run("spatial.bvh_range", [&] {
|
|
int brute = 0;
|
|
for (auto& t : tris) if (tri_box(t).intersects(range)) ++brute;
|
|
auto got = bvh.query_range(range);
|
|
std::ostringstream s; s << "bvh_hits=" << got.size() << " brute=" << brute << " (bbox-overlap count)";
|
|
report("spatial.bvh_range", (int)got.size() == brute && brute > 0, s.str());
|
|
});
|
|
run("spatial.bvh_knn", [&] {
|
|
Point3D q(0.1, 0.2, 0.3);
|
|
std::vector<double> d;
|
|
for (auto& t : tris) d.push_back((collision::closest_point_triangle(q, t) - q).norm());
|
|
std::sort(d.begin(), d.end());
|
|
auto got = bvh.query_knn(q, 10);
|
|
double worst = 0.0;
|
|
for (size_t i = 0; i < got.size(); ++i)
|
|
worst = std::max(worst, std::abs((collision::closest_point_triangle(q, got[i]) - q).norm() - d[i]));
|
|
std::ostringstream s; s.precision(9);
|
|
s << "k=" << got.size() << " worst_distance_err=" << worst;
|
|
report("spatial.bvh_knn", got.size() == 10 && worst < 1e-9, s.str());
|
|
});
|
|
run("spatial.bvh_ray", [&] {
|
|
// shoot straight down onto the plane of the first triangle's centroid (guaranteed hit)
|
|
Point3D T = tris[0].centroid();
|
|
Ray3Dd ray(Point3D(T.x(), T.y(), T.z() + 2.0), Vector3D(0,0,-1));
|
|
auto nearest = bvh.query_ray_nearest(ray);
|
|
auto brute = collision::ray_mesh_intersect(ray, tris);
|
|
std::ostringstream s; s.precision(9);
|
|
if (nearest && brute) s << "bvh_t=" << nearest->t << " brute_t=" << brute->t << " diff=" << std::abs(nearest->t - brute->t);
|
|
else s << "bvh_hit=" << (bool)nearest << " brute_hit=" << (bool)brute;
|
|
bool ok = nearest && brute && std::abs(nearest->t - brute->t) < 1e-9;
|
|
report("spatial.bvh_ray", ok, s.str());
|
|
});
|
|
run("spatial.bvh_sah_variants", [&] {
|
|
int counts[3];
|
|
const spatial::BVHSplitStrategy strat[3] = {spatial::BVHSplitStrategy::SAH,
|
|
spatial::BVHSplitStrategy::Middle,
|
|
spatial::BVHSplitStrategy::Equal};
|
|
for (int k = 0; k < 3; ++k) {
|
|
spatial::BVHBuildOptions o; o.strategy = strat[k];
|
|
spatial::BVH b(o); b.build(tris);
|
|
counts[k] = (int)b.query_range(range).size();
|
|
}
|
|
int brute = 0; for (auto& t : tris) if (tri_box(t).intersects(range)) ++brute;
|
|
std::ostringstream s; s << "SAH=" << counts[0] << " Middle=" << counts[1] << " Equal=" << counts[2]
|
|
<< " | leaf-conservative hits (exact=" << brute << ")";
|
|
report("spatial.bvh_sah_variants", counts[0] == counts[1] && counts[1] == counts[2] && counts[0] > 0, s.str());
|
|
});
|
|
run("spatial.bvh_insert_remove", [&] {
|
|
spatial::BVH b; b.build(tris);
|
|
size_t n0 = b.size();
|
|
Triangle3D extra(Point3D(5,5,5), Point3D(5.1,5,5), Point3D(5,5.1,5));
|
|
b.insert(extra);
|
|
size_t n1 = b.size();
|
|
bool removed = b.remove(extra);
|
|
size_t n2 = b.size();
|
|
std::ostringstream s; s << "build=" << n0 << " after_insert=" << n1 << " remove_ret=" << removed << " after_remove=" << n2;
|
|
report("spatial.bvh_insert_remove", n1 == n0 + 1 && removed && n2 == n0, s.str());
|
|
});
|
|
run("spatial.incremental_bvh", [&] {
|
|
spatial::IncrementalBVH ib;
|
|
Triangle3D t1(Point3D(-0.5,-0.5,0), Point3D(0.5,-0.5,0), Point3D(0,0.5,0));
|
|
Triangle3D t2 = t1; t2 = Triangle3D(Point3D(-0.5,-0.5,1), Point3D(0.5,-0.5,1), Point3D(0,0.5,1));
|
|
ib.build({t1});
|
|
Ray3Dd ray(Point3D(0,0,-2), Vector3D(0,0,1));
|
|
size_t h1 = ib.ray_query(ray).size();
|
|
ib.insert(t2);
|
|
size_t h2 = ib.ray_query(ray).size();
|
|
ib.remove(0);
|
|
size_t sz = ib.size();
|
|
size_t h3 = ib.ray_query(ray).size();
|
|
std::ostringstream s; s << "hits build=" << h1 << " +insert=" << h2 << " after_remove(0) size=" << sz
|
|
<< " hits=" << h3;
|
|
report("spatial.incremental_bvh", h1 == 1 && h2 == 2 && sz == 1 && h3 == 1, s.str());
|
|
});
|
|
}
|
|
// NOTE: Octree check LAST on purpose — it segfaults (see report); run with filter
|
|
// "spatial.octree" to observe the crash, or "!spatial.octree" to skip it.
|
|
run("spatial.octree", [] {
|
|
std::mt19937 gen(99);
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i < 500; ++i) pts.push_back(Point3D(rnd01(gen)*2-1, rnd01(gen)*2-1, rnd01(gen)*2-1));
|
|
spatial::Octree<Point3D> oct(8, 16);
|
|
std::printf("[stage] octree.build start\n"); std::fflush(stdout);
|
|
oct.build(pts);
|
|
std::printf("[stage] octree.build done size=%zu\n", oct.size()); std::fflush(stdout);
|
|
AABB3D range(Point3D(-0.5,-0.5,-0.5), Point3D(0.5,0.5,0.5));
|
|
std::printf("[stage] octree.query_range start\n"); std::fflush(stdout);
|
|
auto got = oct.query_range(range);
|
|
std::printf("[stage] octree.query_range done hits=%zu\n", got.size()); std::fflush(stdout);
|
|
int brute = 0; for (auto& p : pts) if (range.contains(p)) ++brute;
|
|
Point3D q(0.11,0.12,0.13);
|
|
std::printf("[stage] octree.query_knn start\n"); std::fflush(stdout);
|
|
auto knn = oct.query_knn(q, 5);
|
|
std::printf("[stage] octree.query_knn done k=%zu\n", knn.size()); std::fflush(stdout);
|
|
std::vector<double> d; for (auto& p : pts) d.push_back((p-q).norm());
|
|
std::sort(d.begin(), d.end());
|
|
double err = 0.0;
|
|
for (size_t i = 0; i < knn.size(); ++i) err = std::max(err, std::abs((knn[i]-q).norm() - d[i]));
|
|
std::ostringstream s; s.precision(9);
|
|
s << "size=" << oct.size() << " range_hits=" << got.size() << " brute=" << brute
|
|
<< " knn5=" << knn.size() << " knn_err=" << err;
|
|
report("spatial.octree", oct.size() == 500 && (int)got.size() == brute && err < 1e-12, s.str());
|
|
});
|
|
run("spatial.kdtree", [] {
|
|
std::mt19937 gen(98);
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i < 500; ++i) pts.push_back(Point3D(rnd01(gen)*2-1, rnd01(gen)*2-1, rnd01(gen)*2-1));
|
|
spatial::KDTree<Point3D> kd;
|
|
kd.build(pts);
|
|
AABB3D range(Point3D(-0.5,-0.5,-0.5), Point3D(0.5,0.5,0.5));
|
|
auto got = kd.query_range(range);
|
|
int brute = 0; for (auto& p : pts) if (range.contains(p)) ++brute;
|
|
Point3D q(0.11,0.12,0.13);
|
|
auto knn = kd.query_knn(q, 5);
|
|
std::vector<double> d; for (auto& p : pts) d.push_back((p-q).norm());
|
|
std::sort(d.begin(), d.end());
|
|
double err = 0.0;
|
|
for (size_t i = 0; i < knn.size(); ++i) err = std::max(err, std::abs((knn[i]-q).norm() - d[i]));
|
|
std::ostringstream s; s.precision(9);
|
|
s << "size=" << kd.size() << " range_hits=" << got.size() << " brute=" << brute
|
|
<< " knn5=" << knn.size() << " knn_err=" << err;
|
|
report("spatial.kdtree", (int)got.size() == brute && err < 1e-12, s.str());
|
|
});
|
|
run("spatial.rtree", [] {
|
|
std::mt19937 gen(97);
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i < 500; ++i) pts.push_back(Point3D(rnd01(gen)*2-1, rnd01(gen)*2-1, rnd01(gen)*2-1));
|
|
spatial::RTree<Point3D> rt;
|
|
rt.build(pts);
|
|
AABB3D range(Point3D(-0.5,-0.5,-0.5), Point3D(0.5,0.5,0.5));
|
|
auto got = rt.query_range(range);
|
|
int brute = 0; for (auto& p : pts) if (range.contains(p)) ++brute;
|
|
Point3D q(0.11,0.12,0.13);
|
|
auto knn = rt.query_knn(q, 5);
|
|
std::vector<double> d; for (auto& p : pts) d.push_back((p-q).norm());
|
|
std::sort(d.begin(), d.end());
|
|
std::vector<double> gotd; for (auto& p : knn) gotd.push_back((p-q).norm());
|
|
std::sort(gotd.begin(), gotd.end());
|
|
double err = 0.0;
|
|
for (size_t i = 0; i < gotd.size(); ++i) err = std::max(err, std::abs(gotd[i] - d[i]));
|
|
bool ins = [&] { size_t n0 = rt.size(); rt.insert(Point3D(3,3,3)); return rt.size() == n0+1; }();
|
|
bool rem = [&] { return rt.remove(Point3D(3,3,3)); }();
|
|
std::ostringstream s; s.precision(9);
|
|
s << "size=" << rt.size() << " range_hits=" << got.size() << " brute=" << brute
|
|
<< " knn5=" << knn.size() << " knn_err=" << err << " insert_ok=" << ins << " remove_ok=" << rem;
|
|
report("spatial.rtree", (int)got.size() == brute && err < 1e-12 && ins && rem, s.str());
|
|
});
|
|
run("spatial.broadphase", [] {
|
|
std::mt19937 gen(55);
|
|
std::vector<AABB3D> boxes;
|
|
for (int i = 0; i < 60; ++i) {
|
|
double x = rnd01(gen)*2-1, y = rnd01(gen)*2-1, z = rnd01(gen)*2-1;
|
|
double h = 0.05 + 0.1*rnd01(gen);
|
|
boxes.push_back(AABB3D(Point3D(x-h,y-h,z-h), Point3D(x+h,y+h,z+h)));
|
|
}
|
|
spatial::Broadphase bp;
|
|
bp.build(boxes);
|
|
auto pairs = bp.overlap_pairs();
|
|
int brute = 0;
|
|
for (size_t i = 0; i < boxes.size(); ++i)
|
|
for (size_t j = i+1; j < boxes.size(); ++j) if (boxes[i].intersects(boxes[j])) ++brute;
|
|
AABB3D ref(Point3D(-0.2,-0.2,-0.2), Point3D(0.2,0.2,0.2));
|
|
auto got = bp.query(ref);
|
|
int qb = 0; for (auto& b : boxes) if (b.intersects(ref)) ++qb;
|
|
std::ostringstream s; s << "n=" << bp.size() << " overlap_pairs=" << pairs.size() << " brute=" << brute
|
|
<< " query_hits=" << got.size() << " brute=" << qb;
|
|
report("spatial.broadphase", (int)pairs.size() == brute && (int)got.size() == qb, s.str());
|
|
});
|
|
|
|
// ═══════════ 6) COLLISION ═══════════
|
|
auto box_support = [](Point3D c, Vector3D h) {
|
|
return [c, h](const Vector3D& d) {
|
|
return Point3D(c.x() + (d.x()>0?h.x():-h.x()),
|
|
c.y() + (d.y()>0?h.y():-h.y()),
|
|
c.z() + (d.z()>0?h.z():-h.z()));
|
|
};
|
|
};
|
|
auto sphere_support = [](Point3D c, double r) {
|
|
return [c, r](const Vector3D& d) {
|
|
double n = d.norm();
|
|
if (n < 1e-15) return c;
|
|
return Point3D(c + d * (r/n));
|
|
};
|
|
};
|
|
run("collision.gjk_intersect", [&] {
|
|
auto a = box_support(Point3D(0,0,0), Vector3D(0.5,0.5,0.5));
|
|
auto b = box_support(Point3D(0.5,0,0), Vector3D(0.5,0.5,0.5));
|
|
auto c = box_support(Point3D(2.0,0,0), Vector3D(0.5,0.5,0.5));
|
|
bool hi = collision::gjk_intersect(a, b);
|
|
bool lo = collision::gjk_intersect(a, c);
|
|
std::ostringstream s; s << "overlap(0.5 shift)=" << hi << " separated(2.0 shift)=" << lo;
|
|
report("collision.gjk_intersect", hi && !lo, s.str());
|
|
});
|
|
run("collision.gjk_distance", [&] {
|
|
auto a = box_support(Point3D(0,0,0), Vector3D(0.5,0.5,0.5));
|
|
auto b = box_support(Point3D(2.0,0,0), Vector3D(0.5,0.5,0.5));
|
|
double d = collision::gjk_distance(a, b);
|
|
auto sa = sphere_support(Point3D(0,0,0), 1.0);
|
|
auto sb = sphere_support(Point3D(0,0,3), 1.0);
|
|
double ds = collision::gjk_distance(sa, sb);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "box_gap=" << d << " (expect 1.0) sphere_gap=" << ds << " (expect 1.0)";
|
|
report("collision.gjk_distance", std::abs(d-1.0) < 1e-6 && std::abs(ds-1.0) < 1e-6, s.str());
|
|
});
|
|
run("collision.gjk_full", [&] {
|
|
auto a = box_support(Point3D(0,0,0), Vector3D(0.5,0.5,0.5));
|
|
auto b = box_support(Point3D(2.0,0,0), Vector3D(0.5,0.5,0.5));
|
|
auto r = collision::gjk_full(a, b);
|
|
auto r2 = collision::gjk_full(box_support(Point3D(0,0,0), Vector3D(0.5,0.5,0.5)),
|
|
box_support(Point3D(0.4,0,0), Vector3D(0.5,0.5,0.5)));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "separated: intersect=" << r.intersect << " dist=" << r.distance
|
|
<< " pa=(" << r.point_a.x() << "," << r.point_a.y() << "," << r.point_a.z() << ")"
|
|
<< " pb=(" << r.point_b.x() << "," << r.point_b.y() << "," << r.point_b.z() << ")"
|
|
<< " | overlap: intersect=" << r2.intersect << " dist=" << r2.distance;
|
|
report("collision.gjk_full", !r.intersect && std::abs(r.distance-1.0) < 1e-6
|
|
&& r.point_a.x() > 0.49 && r.point_a.x() < 0.51 && r.point_b.x() > 1.49 && r.point_b.x() < 1.51
|
|
&& r2.intersect, s.str());
|
|
});
|
|
run("collision.epa_penetration", [] {
|
|
// VDE-1213/Phase-2: vde::collision::epa_penetration added (collision/epa.h).
|
|
auto box_support = [](Point3D c, double h) -> collision::SupportFunc {
|
|
return [c, h](const Vector3D& d) {
|
|
return Point3D(c.x() + (d.x() >= 0 ? h : -h),
|
|
c.y() + (d.y() >= 0 ? h : -h),
|
|
c.z() + (d.z() >= 0 ? h : -h));
|
|
};
|
|
};
|
|
auto sa = box_support(Point3D(0, 0, 0), 0.5);
|
|
auto sb = box_support(Point3D(0.5, 0, 0), 0.5);
|
|
auto sep = box_support(Point3D(3.0, 0, 0), 0.5);
|
|
auto r = collision::epa_penetration(sa, sb);
|
|
auto rsep = collision::epa_penetration(sa, sep);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "overlap depth=" << r.depth << " valid=" << r.valid
|
|
<< " |normal|=" << r.normal.norm() << " separated valid=" << rsep.valid;
|
|
report("collision.epa_penetration",
|
|
r.valid && std::abs(r.depth - 0.5) < 1e-6 &&
|
|
std::abs(r.normal.norm() - 1.0) < 1e-6 && !rsep.valid, s.str());
|
|
});
|
|
run("collision.sat", [&] {
|
|
auto cube = [](Point3D c, double h) {
|
|
std::vector<Point3D> v = {{-h,-h,-h},{h,-h,-h},{h,h,-h},{-h,h,-h},
|
|
{-h,-h,h},{h,-h,h},{h,h,h},{-h,h,h}};
|
|
for (auto& p : v) p += Vector3D(c.x(), c.y(), c.z());
|
|
std::vector<std::array<int,3>> f = {
|
|
{0,3,2},{0,2,1}, {4,5,6},{4,6,7}, {0,1,5},{0,5,4},
|
|
{3,7,6},{3,6,2}, {0,4,7},{0,7,3}, {1,2,6},{1,6,5}};
|
|
return std::make_pair(v, f);
|
|
};
|
|
auto A = cube(Point3D(0,0,0), 0.5);
|
|
auto B = cube(Point3D(0.5,0,0), 0.5);
|
|
auto C = cube(Point3D(2.0,0,0), 0.5);
|
|
bool hi = collision::sat_intersect(A.first, A.second, B.first, B.second);
|
|
bool lo = collision::sat_intersect(A.first, A.second, C.first, C.second);
|
|
std::ostringstream s; s << "overlap=" << hi << " separated=" << lo;
|
|
report("collision.sat", hi && !lo, s.str());
|
|
});
|
|
run("collision.ray_triangle_box", [&] {
|
|
Triangle3D tri(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0));
|
|
auto hit = collision::ray_triangle_intersect(Point3D(0.2,0.2,1), Vector3D(0,0,-1), tri);
|
|
auto miss = collision::ray_triangle_intersect(Point3D(2,2,1), Vector3D(0,0,-1), tri);
|
|
AABB3D box(Point3D(-1,-1,-1), Point3D(1,1,1));
|
|
Ray3Dd ray(Point3D(-2,0,0), Vector3D(1,0,0));
|
|
double tmin=-1, tmax=-1;
|
|
bool rbox = collision::ray_aabb_intersect(ray, box, tmin, tmax);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "ray_tri hit=" << (bool)hit << (hit ? (" t=" + std::to_string(hit->t)) : std::string())
|
|
<< " miss=" << (bool)miss << " | ray_aabb tmin=" << tmin << " tmax=" << tmax;
|
|
report("collision.ray_triangle_box", hit && std::abs(hit->t-1.0) < 1e-9 && !miss
|
|
&& rbox && std::abs(tmin-1.0) < 1e-9 && std::abs(tmax-3.0) < 1e-9, s.str());
|
|
});
|
|
run("collision.tri_tri", [&] {
|
|
Triangle3D t1(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0));
|
|
Triangle3D t2(Point3D(0.2,0.2,-1), Point3D(0.2,0.2,1), Point3D(0.6,0.2,0));
|
|
bool hit = collision::tri_tri_intersect(t1, t2);
|
|
auto det = collision::tri_tri_intersect_detailed(t1, t2);
|
|
Triangle3D t3(Point3D(0,0,5), Point3D(1,0,5), Point3D(0,1,5));
|
|
bool nohit = collision::tri_tri_intersect(t1, t3);
|
|
AABB3D box(Point3D(-0.1,-0.1,-0.1), Point3D(0.2,0.2,0.2));
|
|
bool ta = collision::tri_aabb_overlap(t1, box);
|
|
double seglen = (det.p1 - det.p0).norm();
|
|
std::ostringstream s; s.precision(6);
|
|
s << "tri_tri=" << hit << " detailed_intersects=" << det.intersects << " seg_len=" << seglen
|
|
<< " parallel_sep=" << nohit << " tri_aabb_overlap=" << ta;
|
|
report("collision.tri_tri", hit && det.intersects && seglen > 1e-9 && !nohit && ta, s.str());
|
|
});
|
|
run("collision.closest_point", [&] {
|
|
Triangle3D tri(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0));
|
|
Point3D cp = collision::closest_point_triangle(Point3D(0.25,0.25,2), tri);
|
|
std::vector<Triangle3D> tris = {tri};
|
|
Point3D cp2 = collision::closest_point_mesh(Point3D(0.25,0.25,2), tris);
|
|
double d = collision::distance_to_mesh(Point3D(0.25,0.25,2), tris);
|
|
std::ostringstream s; s.precision(9);
|
|
s << "closest=(" << cp.x() << "," << cp.y() << "," << cp.z() << ") mesh_closest=("
|
|
<< cp2.x() << "," << cp2.y() << "," << cp2.z() << ") dist=" << d;
|
|
report("collision.closest_point", std::abs(cp.x()-0.25)<1e-12 && std::abs(cp.y()-0.25)<1e-12
|
|
&& std::abs(cp.z())<1e-12 && std::abs(d-2.0)<1e-12, s.str());
|
|
});
|
|
run("collision.ccd", [&] {
|
|
auto A = sphere_support(Point3D(0,0,0), 1.0);
|
|
auto B = sphere_support(Point3D(5,0,0), 1.0);
|
|
auto hit = collision::conservative_advancement(A, Vector3D(10,0,0), B, Vector3D(0,0,0));
|
|
auto miss = collision::conservative_advancement(A, Vector3D(1,0,0), B, Vector3D(0,0,0));
|
|
double seg = collision::swept_sphere_triangle(Point3D(0,0,-2), Point3D(0,0,2), 0.5,
|
|
Point3D(-1,-1,0), Point3D(1,-1,0), Point3D(0,1,0));
|
|
bool sweep_no = collision::swept_sphere_triangle(Point3D(0,0,5), Point3D(0,0,6), 0.5,
|
|
Point3D(-1,-1,0), Point3D(1,-1,0), Point3D(0,1,0));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "ccd hit=" << hit.hit << " toi=" << hit.time_of_impact << " (expect 0.3) init_dist=" << hit.initial_distance
|
|
<< " | miss.hit=" << miss.hit << " swept_sphere_tri=" << seg << " sweep_above=" << sweep_no;
|
|
report("collision.ccd", hit.hit && std::abs(hit.time_of_impact - 0.3) < 0.02 && !miss.hit && seg && !sweep_no, s.str());
|
|
});
|
|
run("collision.ray_mesh", [&] {
|
|
std::vector<Triangle3D> tris;
|
|
tris.push_back(Triangle3D(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0)));
|
|
tris.push_back(Triangle3D(Point3D(0,0,2), Point3D(1,0,2), Point3D(0,1,2)));
|
|
auto hit = collision::ray_mesh_intersect(Point3D(0.2,0.2,-1), Vector3D(0,0,1), tris);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "hit=" << (bool)hit << (hit ? (" t=" + std::to_string(hit->t)) : std::string());
|
|
report("collision.ray_mesh", hit && std::abs(hit->t - 1.0) < 1e-9, s.str());
|
|
});
|
|
|
|
// ═══════════ 7) CONVERSIONS ═══════════
|
|
run("conv.brep_mesh_brep", [] {
|
|
auto b0 = brep::make_box(2,1,1);
|
|
auto m = bmesh(b0);
|
|
auto b1 = mesh::mesh_to_brep_advanced(m);
|
|
std::ostringstream s; s.precision(6);
|
|
s << "BRep0 faces=" << b0.num_faces() << " -> mesh F=" << m.num_faces()
|
|
<< " -> BRep1 faces=" << b1.num_faces() << " valid=" << b1.is_valid();
|
|
if (b1.num_faces() > 0) {
|
|
auto m2 = bmesh(b1);
|
|
double v = std::abs(mesh_volume(m2));
|
|
s << " -> re-mesh volume=" << v << " (expect 2)";
|
|
report("conv.brep_mesh_brep", b1.num_faces() > 0 && b1.is_valid() && std::abs(v - 2.0) < 0.02, s.str());
|
|
} else {
|
|
report("conv.brep_mesh_brep", false, s.str());
|
|
}
|
|
});
|
|
run("conv.mesh_to_sdf", [] {
|
|
// VDE-1213/Phase-2: sdf_from_mesh(mesh) added (sdf/sdf_from_mesh.h).
|
|
auto cube = bmesh(brep::make_box(1, 1, 1));
|
|
auto f = sdf::sdf_from_mesh(cube);
|
|
const double din = f(Point3D(0, 0, 0));
|
|
const double dsurf = f(Point3D(0.5, 0, 0));
|
|
const double dout = f(Point3D(2, 0, 0));
|
|
std::ostringstream s; s.precision(6);
|
|
s << "inside=" << din << " surface=" << dsurf << " outside=" << dout
|
|
<< " (expect -0.5 / 0 / 1.5)";
|
|
report("conv.mesh_to_sdf",
|
|
std::abs(din + 0.5) < 0.02 && std::abs(dsurf) < 0.02 && std::abs(dout - 1.5) < 0.02, s.str());
|
|
});
|
|
run("conv.voxelize_brep", [] {
|
|
auto b = brep::make_box(1,1,1);
|
|
mesh::VoxelizeParams vp; vp.resolution = 24;
|
|
auto grid = mesh::voxelize_brep(b, vp);
|
|
double center = grid.at(grid.nx/2, grid.ny/2, grid.nz/2);
|
|
std::ostringstream s; s << "grid=" << grid.nx << "^3 center_sdf=" << center;
|
|
report("conv.voxelize_brep", center < 0.0, s.str());
|
|
});
|
|
run("conv.sdf_mesh_roundtrip", [] {
|
|
auto root = sdf::SdfNode::op_union(sdf::SdfNode::sphere(1.0),
|
|
sdf::SdfNode::translate(sdf::SdfNode::sphere(1.0), Point3D(0.8,0,0)));
|
|
auto mc = sdf::sdf_to_mesh(root, 48);
|
|
auto m = from_mc(mc);
|
|
double vol = std::abs(mesh_volume(m));
|
|
std::ostringstream s; s.precision(5);
|
|
s << "CSG union -> MC: V=" << mc.vertices.size() << " T=" << mc.triangles.size()
|
|
<< " boundary=" << boundary_edges(m) << " volume=" << vol << " (analytic 6.568 for r=1,d=0.8 lens)";
|
|
report("conv.sdf_mesh_roundtrip", boundary_edges(m) == 0 && vol > 6.40 && vol < 6.73, s.str());
|
|
});
|
|
|
|
std::printf("\n=== SUMMARY: PASS=%d FAIL=%d TOTAL=%d ===\n", g_pass, g_fail, g_pass + g_fail);
|
|
return g_fail == 0 ? 0 : 1;
|
|
}
|