237 lines
9.0 KiB
C++
237 lines
9.0 KiB
C++
/**
|
||
* @file boolean_mesh.cpp
|
||
* @brief 三维网格布尔运算实现
|
||
*/
|
||
#include "vde/boolean/boolean_mesh.h"
|
||
#include "vde/spatial/bvh.h"
|
||
#include "vde/core/triangle.h"
|
||
#include "vde/core/line.h"
|
||
#include <cmath>
|
||
#include <array>
|
||
#include <algorithm>
|
||
|
||
namespace vde::boolean {
|
||
using core::Triangle3D;
|
||
using core::Ray3Dd;
|
||
using vde::mesh::HalfedgeMesh;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Point-in-mesh classification via ray-casting
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
static bool ray_tri_intersect(
|
||
const core::Point3D& origin, const core::Vector3D& dir,
|
||
const core::Point3D& v0, const core::Point3D& v1, const core::Point3D& v2)
|
||
{
|
||
core::Vector3D e1 = v1 - v0;
|
||
core::Vector3D e2 = v2 - v0;
|
||
core::Vector3D h = dir.cross(e2);
|
||
double a = e1.dot(h);
|
||
if (std::abs(a) < 1e-12) return false;
|
||
|
||
double f = 1.0 / a;
|
||
core::Vector3D s = origin - v0;
|
||
double u = f * s.dot(h);
|
||
if (u < 0.0 || u > 1.0) return false;
|
||
|
||
core::Vector3D q = s.cross(e1);
|
||
double v = f * dir.dot(q);
|
||
if (v < 0.0 || u + v > 1.0) return false;
|
||
|
||
double t = f * e2.dot(q);
|
||
return t > 0.0;
|
||
}
|
||
|
||
bool is_point_inside_mesh(const core::Point3D& p, const HalfedgeMesh& mesh) {
|
||
// Shoot a ray in a random-ish direction to avoid degenerate alignments
|
||
// Use a direction biased toward (1, 0.37, 0.53) to minimise edge/vertex grazing
|
||
core::Vector3D dir(1.0, 0.371, 0.539);
|
||
dir.normalize();
|
||
|
||
int hits = 0;
|
||
const size_t nf = mesh.num_faces();
|
||
for (size_t fi = 0; fi < nf; ++fi) {
|
||
auto vis = mesh.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() != 3) continue;
|
||
if (ray_tri_intersect(p, dir,
|
||
mesh.vertex(vis[0]), mesh.vertex(vis[1]), mesh.vertex(vis[2])))
|
||
++hits;
|
||
}
|
||
return (hits & 1) == 1; // odd → inside
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// BVH-accelerated point-in-mesh
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
/// Build a BVH over `mesh` triangles. Returns an empty BVH if the mesh
|
||
/// has no triangles, in which case everything is "outside".
|
||
spatial::BVH build_bvh_for(const HalfedgeMesh& mesh) {
|
||
spatial::BVH bvh;
|
||
std::vector<Triangle3D> tris;
|
||
tris.reserve(mesh.num_faces());
|
||
for (size_t fi = 0; fi < mesh.num_faces(); ++fi) {
|
||
auto vis = mesh.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() == 3)
|
||
tris.emplace_back(mesh.vertex(vis[0]),
|
||
mesh.vertex(vis[1]),
|
||
mesh.vertex(vis[2]));
|
||
}
|
||
if (!tris.empty()) bvh.build(tris);
|
||
return bvh;
|
||
}
|
||
|
||
} // anonymous namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Clip mesh — keep faces whose centroids are OUTSIDE b
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
HalfedgeMesh clip_mesh(const HalfedgeMesh& a, const HalfedgeMesh& b,
|
||
bool invert_b) {
|
||
if (a.num_faces() == 0) return {};
|
||
if (b.num_faces() == 0) {
|
||
// Empty boundary → everything is "outside" (inside inverted-empty)
|
||
return invert_b ? HalfedgeMesh{} : a;
|
||
}
|
||
|
||
spatial::BVH bvh_b = build_bvh_for(b);
|
||
|
||
std::vector<core::Point3D> out_verts;
|
||
std::vector<std::array<int, 3>> out_tris;
|
||
|
||
for (size_t fi = 0; fi < a.num_faces(); ++fi) {
|
||
auto vis = a.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() != 3) continue;
|
||
|
||
core::Point3D v0 = a.vertex(vis[0]);
|
||
core::Point3D v1 = a.vertex(vis[1]);
|
||
core::Point3D v2 = a.vertex(vis[2]);
|
||
core::Point3D centroid = (v0 + v1 + v2) * (1.0 / 3.0);
|
||
|
||
// Use brute-force check for correct results (BVH ray query has epsilon issues)
|
||
bool inside = is_point_inside_mesh(centroid, b);
|
||
if (invert_b) inside = !inside;
|
||
|
||
// Keep the face if its centroid is OUTSIDE (b)
|
||
if (!inside) {
|
||
int idx = static_cast<int>(out_verts.size());
|
||
out_verts.push_back(v0);
|
||
out_verts.push_back(v1);
|
||
out_verts.push_back(v2);
|
||
// Preserve original winding (no flip)
|
||
out_tris.push_back({idx, idx + 1, idx + 2});
|
||
}
|
||
}
|
||
|
||
HalfedgeMesh result;
|
||
if (!out_verts.empty())
|
||
result.build_from_triangles(out_verts, out_tris);
|
||
return result;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Mesh invert / merge helpers
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
HalfedgeMesh invert_mesh(const HalfedgeMesh& m) {
|
||
// Flip face winding — reverse every triangle's vertex order
|
||
std::vector<core::Point3D> verts;
|
||
std::vector<std::array<int, 3>> tris;
|
||
verts.reserve(m.num_vertices());
|
||
tris.reserve(m.num_faces());
|
||
|
||
for (size_t i = 0; i < m.num_vertices(); ++i)
|
||
verts.push_back(m.vertex(i));
|
||
|
||
for (size_t fi = 0; fi < m.num_faces(); ++fi) {
|
||
auto vis = m.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() != 3) continue;
|
||
// Reverse winding: (v0, v1, v2) → (v0, v2, v1)
|
||
tris.push_back({vis[0], vis[2], vis[1]});
|
||
}
|
||
|
||
HalfedgeMesh result;
|
||
if (!verts.empty() && !tris.empty())
|
||
result.build_from_triangles(verts, tris);
|
||
return result;
|
||
}
|
||
|
||
HalfedgeMesh merge_meshes(const HalfedgeMesh& a, const HalfedgeMesh& b) {
|
||
if (a.num_vertices() == 0) return b;
|
||
if (b.num_vertices() == 0) return a;
|
||
|
||
std::vector<core::Point3D> verts;
|
||
std::vector<std::array<int, 3>> tris;
|
||
|
||
// Copy A
|
||
for (size_t i = 0; i < a.num_vertices(); ++i)
|
||
verts.push_back(a.vertex(i));
|
||
for (size_t fi = 0; fi < a.num_faces(); ++fi) {
|
||
auto vis = a.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() != 3) continue;
|
||
tris.push_back({vis[0], vis[1], vis[2]});
|
||
}
|
||
|
||
// Copy B with vertex-index offset
|
||
int offset_b = static_cast<int>(verts.size());
|
||
for (size_t i = 0; i < b.num_vertices(); ++i)
|
||
verts.push_back(b.vertex(i));
|
||
for (size_t fi = 0; fi < b.num_faces(); ++fi) {
|
||
auto vis = b.face_vertices(static_cast<int>(fi));
|
||
if (vis.size() != 3) continue;
|
||
tris.push_back({vis[0] + offset_b,
|
||
vis[1] + offset_b,
|
||
vis[2] + offset_b});
|
||
}
|
||
|
||
HalfedgeMesh result;
|
||
if (!verts.empty())
|
||
result.build_from_triangles(verts, tris);
|
||
return result;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Public boolean operations
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
HalfedgeMesh mesh_union(const HalfedgeMesh& a, const HalfedgeMesh& b) {
|
||
// Keep faces of each mesh that lie OUTSIDE the other
|
||
HalfedgeMesh a_out = clip_mesh(a, b, false);
|
||
HalfedgeMesh b_out = clip_mesh(b, a, false);
|
||
return merge_meshes(a_out, b_out);
|
||
}
|
||
|
||
HalfedgeMesh mesh_intersection(const HalfedgeMesh& a, const HalfedgeMesh& b) {
|
||
// Keep faces whose centroids are INSIDE the other mesh.
|
||
// clip(…, invert=true) keeps faces OUTSIDE the inverted volume,
|
||
// i.e. INSIDE the original volume.
|
||
HalfedgeMesh a_in = clip_mesh(a, b, /*invert_b=*/true);
|
||
HalfedgeMesh b_in = clip_mesh(b, a, /*invert_b=*/true);
|
||
return merge_meshes(a_in, b_in);
|
||
}
|
||
|
||
HalfedgeMesh mesh_difference(const HalfedgeMesh& a, const HalfedgeMesh& b) {
|
||
// A − B: keep A-faces that are OUTSIDE B
|
||
return clip_mesh(a, b, false);
|
||
}
|
||
|
||
HalfedgeMesh mesh_sym_diff(const HalfedgeMesh& a, const HalfedgeMesh& b) {
|
||
return merge_meshes(mesh_difference(a, b), mesh_difference(b, a));
|
||
}
|
||
|
||
HalfedgeMesh mesh_boolean(const HalfedgeMesh& a, const HalfedgeMesh& b,
|
||
BooleanOp op) {
|
||
switch (op) {
|
||
case BooleanOp::Union: return mesh_union(a, b);
|
||
case BooleanOp::Intersection: return mesh_intersection(a, b);
|
||
case BooleanOp::Difference: return mesh_difference(a, b);
|
||
case BooleanOp::SymDiff: return mesh_sym_diff(a, b);
|
||
}
|
||
return {};
|
||
}
|
||
|
||
} // namespace vde::boolean
|