Files
ViewDesignEngine/docs/reference/api-reference-mesh.md
T

326 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Mesh 模块 API 参考
> 网格模块 — 半边网格、网格→曲线转换、点云(E57/LAS)、网格曲面混合
## 模块概述
mesh 模块提供三角网格处理、网格与 NURBS 之间的桥接、点云数据处理能力。核心数据结构为 HalfedgeMesh(半边网格),支持简化、平滑、布尔、参数化、修复等操作。
---
## 头文件列表
| 头文件 | 说明 |
|--------|------|
| `halfedge_mesh.h` | 半边网格核心数据结构 |
| `mesh_to_curve.h` | 网格→NURBS 曲线桥接(VDE-021) |
| `point_cloud.h` | 点云(E57/LAS/PLY/PTX 加载,降采样,法线估计) |
| `mesh_simplify.h` | 网格简化 |
| `mesh_smooth.h` | 网格平滑 |
| `mesh_repair.h` | 网格修复 |
| `mesh_boolean.h` | 网格布尔运算 |
| `mesh_curvature.h` | 网格曲率分析 |
| `mesh_parameterize.h` | 网格参数化 |
| `mesh_quality.h` | 网格质量评估 |
| `mesh_lod.h` | 网格 LOD |
| `flattening.h` | 曲面展开/展平(VDE-152,复合材料铺层、排料等) |
| `voxelize.h` | 体素化与批量 SDF 采样(VoxelGrid) |
| `slicer.h` | 增材制造切片(VDE-565) |
| `shape_descriptor.h` | 形状描述符与相似零件检索 |
| `marching_cubes.h` | Marching Cubes 等值面提取(含 VoxelGrid/体素化桥接) |
| `delaunay_2d.h` | 二维 Delaunay 三角剖分 |
| `delaunay_3d.h` | 三维 Delaunay 四面体剖分 |
| `cdt_2d.h` | 约束 Delaunay 三角剖分 |
| `alpha_shapes.h` | Alpha Shapes |
| `geodesic.h` | 测地线距离 |
| `reverse_engineering.h` | 逆向工程(点云→B-Rep) |
| `fea_mesh.h` | FEA 有限元网格 |
| `visualization_quality.h` | 可视化质量 |
| `parallel_mc.h` | 并行 Marching Cubes |
---
## 详细 API
### `halfedge_mesh.h` — 半边网格核心
```cpp
namespace vde::mesh {
class HalfedgeMesh { // 数据成员私有,通过访问器查询拓扑
public:
HalfedgeMesh() = default;
// ── 构造 ─────────────────────────────────────────────
void clear();
int add_vertex(const Point3D& p); // 返回新顶点索引(0-based)
int add_face(const std::vector<int>& vertex_indices); // 逆时针顶点环,失败返回 -1
void build_from_triangles(const std::vector<Point3D>& verts,
const std::vector<std::array<int, 3>>& tris);
[[nodiscard]] MCMesh to_mcm() const; // 导出为 MCMesh(顶点+三角形索引)
// ── 访问器 ───────────────────────────────────────────
[[nodiscard]] size_t num_vertices() const;
[[nodiscard]] size_t num_faces() const;
[[nodiscard]] size_t num_edges() const; // = 半边数 / 2
[[nodiscard]] const Point3D& vertex(size_t idx) const;
[[nodiscard]] const Face& face(size_t idx) const; // Face{halfedge_index, valence}
[[nodiscard]] const Halfedge& halfedge(size_t idx) const; // Halfedge{vertex_index, face_index, next/prev/opposite_index}
void set_vertex(size_t idx, const Point3D& p); // 置脏法向缓存
// ── 拓扑查询 ─────────────────────────────────────────
[[nodiscard]] std::vector<int> face_vertices(int fi) const; // 面的顶点环(按环绕顺序)
[[nodiscard]] std::vector<int> vertex_faces(int vi) const; // 顶点的邻面索引环
[[nodiscard]] bool is_boundary_edge(int hei) const; // 对侧半边无所属面
[[nodiscard]] bool is_boundary_vertex(int vi) const;
// ── 法向 / 包围盒 ────────────────────────────────────
[[nodiscard]] Vector3D face_normal(int fi) const;
[[nodiscard]] Vector3D vertex_normal(int vi) const;
void update_normals();
[[nodiscard]] AABB3D bounds() const;
// ── 迭代器(支持 range-based for)────────────────────
[[nodiscard]] FaceRange faces_range() const; // 遍历所有面
[[nodiscard]] VertexOneRing vertex_one_ring(int vi) const; // 顶点 1-ring 邻域
};
} // namespace vde::mesh
```
### `mesh_to_curve.h` — 网格→曲线桥接(VDE-021)
```cpp
namespace vde::mesh {
[[nodiscard]] curves::NurbsCurve mesh_boundary_to_curve(const HalfedgeMesh& mesh); // 边界环→NURBS
[[nodiscard]] curves::NurbsCurve extract_boundary_curves(const HalfedgeMesh& mesh); // 等效别名
[[nodiscard]] std::vector<curves::NurbsCurve> extract_profiles_from_mesh(
const HalfedgeMesh& mesh, const core::Vector3D& projection_axis); // 多轮廓提取
[[nodiscard]] std::vector<curves::NurbsCurve> mesh_to_directed_curve(
const HalfedgeMesh& mesh, const core::Vector3D& axis); // 有向轮廓(别名)
[[nodiscard]] std::vector<curves::NurbsCurve> mesh_contour_to_curves(
const HalfedgeMesh& mesh, double z_height); // Z 层截面轮廓
} // namespace vde::mesh
```
### `point_cloud.h` — 点云
```cpp
namespace vde::mesh {
class PointCloud {
public:
std::vector<Point3D> vertices;
std::vector<Vector3D> normals;
std::vector<std::array<uint8_t, 3>> colors;
std::vector<double> intensities;
PointCloud();
explicit PointCloud(std::vector<Point3D> pts);
[[nodiscard]] size_t size() const;
[[nodiscard]] bool has_normals() const;
[[nodiscard]] bool has_colors() const;
[[nodiscard]] bool has_intensity() const;
[[nodiscard]] AABB3D bounds() const;
// I/O
static PointCloud load_ply(const std::string& path); // PLY 加载
static PointCloud load_e57(const std::string& path); // E57 加载(ASTM E2807)
static PointCloud load_las(const std::string& path); // LAS/LAZ 加载(1.2/1.3/1.4)
static PointCloud load_ptx(const std::string& path); // PTX 加载(Leica)
bool save_ply(const std::string& path) const; // PLY 保存
// 滤波/降采样
[[nodiscard]] PointCloud voxel_downsample(double voxel_size) const; // 体素降采样
[[nodiscard]] PointCloud statistical_outlier_removal(int k=6, double std_ratio=1.0) const; // SOR 离群点剔除
[[nodiscard]] std::vector<size_t> curvature_aware_sampling(size_t target_count) const; // 曲率感知采样
[[nodiscard]] PointCloud smoothing_filter(double sigma_c=0.0, double sigma_n=0.3, int iterations=3) const; // 双边滤波
// 法线估计
[[nodiscard]] PointCloud normal_estimation(int k = 10) const; // kNN 法线估计
// 双边法向感知平滑(每迭代先重估法线,适合高噪声扫描数据)
[[nodiscard]] PointCloud normal_aware_smoothing(
int k_normal = 10, double sigma_c = 0.0, double sigma_n = 0.3,
int iterations = 3) const;
};
} // namespace vde::mesh
```
### 其余网格模块摘要
```cpp
namespace vde::mesh {
// mesh_simplify.h
[[nodiscard]] HalfedgeMesh simplify_mesh(const HalfedgeMesh& mesh,
const SimplifyOptions& opts = {}); // QEM 简化(target_ratio 默认 0.5)
// mesh_smooth.h
[[nodiscard]] HalfedgeMesh smooth_mesh(const HalfedgeMesh& mesh, const SmoothOptions& opts = {}); // 按 method 分派
[[nodiscard]] HalfedgeMesh smooth_laplacian(const HalfedgeMesh& mesh, int iterations, double lambda);
[[nodiscard]] HalfedgeMesh smooth_taubin(const HalfedgeMesh& mesh, int iterations, double lambda, double mu);
[[nodiscard]] HalfedgeMesh smooth_hc_laplacian(const HalfedgeMesh& mesh, const SmoothOptions& opts = {});
[[nodiscard]] HalfedgeMesh smooth_bilateral(const HalfedgeMesh& mesh, const SmoothOptions& opts = {});
// mesh_repair.h
[[nodiscard]] HalfedgeMesh repair_mesh(const HalfedgeMesh& mesh, const RepairOptions& opts = {});
[[nodiscard]] HalfedgeMesh automatic_hole_filling(const HalfedgeMesh& mesh); // 补洞
[[nodiscard]] HalfedgeMesh non_manifold_repair(const HalfedgeMesh& mesh); // 非流形边修复
[[nodiscard]] MeshHealingReport mesh_healing_report(const HalfedgeMesh& mesh); // 修复诊断报告
// mesh_boolean.h(通用入口;便捷包装 mesh_union/mesh_intersection/mesh_difference 见 vde::boolean/boolean_mesh.h)
[[nodiscard]] HalfedgeMesh mesh_boolean(const HalfedgeMesh& a, const HalfedgeMesh& b, BooleanOp op);
// marching_cubes.h(返回 MCMesh:顶点 + 三角形索引,而非 HalfedgeMesh)
[[nodiscard]] MCMesh marching_cubes(
const std::function<double(double,double,double)>& f,
double iso_level, const Point3D& bmin, const Point3D& bmax, int resolution);
[[nodiscard]] MCMesh marching_cubes(const VoxelGrid& grid, double isoValue = 0.5);
[[nodiscard]] MCMesh tessellate(const brep::BrepModel& model,
double deflection = 0.1, double angleDeg = 20.0); // B-Rep → 扁平三角网格
// delaunay_2d.h
DelaunayResult delaunay_2d(const std::vector<Point2D>& points);
// cdt_2d.h
DelaunayResult constrained_delaunay_2d(
const std::vector<Point2D>& points,
const std::vector<std::pair<int, int>>& constraints); // 约束边强制出现在三角化中
// delaunay_3d.h
TetrahedronMesh delaunay_3d(const std::vector<Point3D>& points);
// alpha_shapes.h
TetrahedronMesh alpha_shapes(const std::vector<Point3D>& points, double alpha);
std::pair<std::vector<Point3D>, std::vector<std::array<int,3>>> alpha_shapes_surface(
const TetrahedronMesh& tet_mesh); // 提取重建表面三角形
// geodesic.h
std::vector<double> geodesic_distance(const HalfedgeMesh& mesh,
const std::vector<int>& sources,
double t = -1.0); // 热方法,支持多源点
// reverse_engineering.h
HalfedgeMesh point_cloud_to_mesh(const PointCloud& cloud,
const PointCloudToMeshParams& params = {}); // Poisson 曲面重建
NurbsSurface mesh_to_nurbs_surface(const HalfedgeMesh& mesh,
const MeshToNURBSParams& params = {});
[[nodiscard]] brep::BrepModel mesh_to_brep_advanced(
const HalfedgeMesh& mesh, BRepFeatureFlag features = BRepFeatureFlag::All);
[[nodiscard]] NurbsSurface point_cloud_to_surface(const std::vector<Point3D>& points,
double tolerance = 0.01);
// fea_mesh.h
FEAMesh tetrahedral_mesh(const brep::BrepModel& body,
const TetMeshParams& params = {}); // Delaunay 3D + 质量优化
// mesh_curvature.h
CurvatureResult compute_curvature(const HalfedgeMesh& mesh);
[[nodiscard]] PrincipalCurvatureResult principal_curvature_directions(
const HalfedgeMesh& mesh, int vertex_id);
// mesh_parameterize.h
[[nodiscard]] std::vector<Point2D> tutte_parameterization(const HalfedgeMesh& mesh);
[[nodiscard]] std::vector<Point2D> lscm_parameterization(const HalfedgeMesh& mesh);
[[nodiscard]] std::vector<Point2D> arap_parameterization(const HalfedgeMesh& mesh, int max_iter = 50);
// mesh_quality.h
MeshQuality evaluate_mesh_quality(const HalfedgeMesh& mesh);
QualityReport mesh_quality_report(const HalfedgeMesh& mesh);
// mesh_lod.h
[[nodiscard]] LODMesh generate_lod(const HalfedgeMesh& mesh,
int num_levels = 3, double reduction_ratio = 0.5);
[[nodiscard]] LODMesh generate_brep_lod(const brep::BrepModel& body, int num_levels = 3);
// visualization_quality.h
AOResult ambient_occlusion(const HalfedgeMesh& mesh, int samples = 128);
EdgeHighlightResult edge_highlighting(const HalfedgeMesh& mesh, double angle_threshold = 0.785398163);
// parallel_mc.h
[[nodiscard]] HalfedgeMesh parallel_marching_cubes(
const std::function<double(double,double,double)>& sdf,
const core::AABB3D& bounds, const ParallelMCConfig& config = {});
// flattening.h(VDE-152 曲面展开)
[[nodiscard]] FlattenResult flattenMesh(const HalfedgeMesh& mesh, const FlattenOptions& options = {});
[[nodiscard]] FlattenResult flattenSurface(const curves::NurbsSurface& surface,
int sampleU = 50, int sampleV = 50, const FlattenOptions& options = {});
// voxelize.h(批量 SDF 采样 → VoxelGrid,可喂给 marching_cubes(grid, iso))
[[nodiscard]] VoxelGrid sample_sdf(
const std::function<double(double,double,double)>& sdf,
const core::AABB3D& bounds, const VoxelizeParams& params = {});
[[nodiscard]] VoxelGrid sample_sdf_batch(const BatchSdfFn& batch_sdf,
const core::AABB3D& bounds, const VoxelizeParams& params = {});
// slicer.h(VDE-565 增材制造切片)
[[nodiscard]] SliceResult slice_mesh(const HalfedgeMesh& mesh, const SliceConfig& config = {});
// shape_descriptor.h(VDE-580)
[[nodiscard]] ShapeDescriptor compute_shape_descriptor(const HalfedgeMesh& mesh, int d2_samples = 100000);
[[nodiscard]] std::vector<SimilarityMatch> find_similar_parts(
const ShapeDescriptor& query,
const std::vector<ShapeDescriptor>& library, int k = 5);
} // namespace vde::mesh
```
---
## 代码示例
### 示例 1: 网格→B-Rep 建模流水线
```cpp
#include <vde/mesh/mesh_to_curve.h>
#include <vde/brep/modeling.h>
using namespace vde;
// 加载 STL 网格(假设已有)
mesh::HalfedgeMesh mesh = /* ... */;
// 提取边界轮廓 → NURBS 曲线
auto profile = mesh::mesh_boundary_to_curve(mesh);
// 挤出为 B-Rep 实体
auto brep_body = brep::extrude(profile, core::Vector3D(0, 0, 10.0));
// 倒角
auto result = brep::fillet(brep_body, 0, 2.0);
```
### 示例 2: 点云处理
```cpp
#include <vde/mesh/point_cloud.h>
using namespace vde::mesh;
// 加载 E57 点云(激光扫描仪数据)
auto cloud = PointCloud::load_e57("scan_001.e57");
std::cout << "原始点数: " << cloud.size() << "\n";
// 体素降采样
auto downsampled = cloud.voxel_downsample(0.01); // 1cm 体素
// 离群点剔除
auto filtered = downsampled.statistical_outlier_removal(6, 1.0);
// 法线估计
auto with_normals = filtered.normal_estimation(10);
// 保存为 PLY
with_normals.save_ply("processed.ply");
```