ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
halfedge_mesh.h
浏览该文件的文档.
1 #pragma once
2 #include "vde/core/point.h"
3 #include "vde/core/aabb.h"
4 #include <vector>
5 #include <cstdint>
6 
7 namespace vde::mesh {
8 using core::AABB3D;
9 using core::Vector3D;
10 using core::Point3D;
11 
20 struct Halfedge {
21  int vertex_index = -1;
22  int face_index = -1;
23  int next_index = -1;
24  int prev_index = -1;
25  int opposite_index = -1;
26 };
27 
35 struct Face {
36  int halfedge_index = -1;
37  int valence = 3;
38 };
39 
54 class HalfedgeMesh {
55 public:
56  HalfedgeMesh() = default;
57 
58  // ── Construction ──────────────────────────────────────
59 
63  void clear();
64 
70  int add_vertex(const Point3D& p);
71 
78  int add_face(const std::vector<int>& vertex_indices);
79 
90  void build_from_triangles(const std::vector<Point3D>& verts,
91  const std::vector<std::array<int, 3>>& tris);
92 
93  // ── Accessors ─────────────────────────────────────────
94 
99  [[nodiscard]] size_t num_vertices() const { return vertices_.size(); }
100 
105  [[nodiscard]] size_t num_faces() const { return faces_.size(); }
106 
111  [[nodiscard]] size_t num_edges() const { return halfedges_.size() / 2; }
112 
118  [[nodiscard]] const Point3D& vertex(size_t idx) const { return vertices_[idx]; }
119 
125  [[nodiscard]] const Face& face(size_t idx) const { return faces_[idx]; }
126 
132  [[nodiscard]] const Halfedge& halfedge(size_t idx) const { return halfedges_[idx]; }
133 
140  void set_vertex(size_t idx, const Point3D& p) { vertices_[idx] = p; normals_dirty_ = true; }
141 
142  // ── Topology queries ──────────────────────────────────
143 
149  [[nodiscard]] std::vector<int> face_vertices(int fi) const;
150 
156  [[nodiscard]] std::vector<int> vertex_faces(int vi) const;
157 
163  [[nodiscard]] bool is_boundary_edge(int hei) const { return halfedges_[hei].face_index < 0; }
164 
170  [[nodiscard]] bool is_boundary_vertex(int vi) const;
171 
172  // ── Normals ───────────────────────────────────────────
173 
179  [[nodiscard]] Vector3D face_normal(int fi) const;
180 
186  [[nodiscard]] Vector3D vertex_normal(int vi) const;
187 
193 
194  // ── Bounds ────────────────────────────────────────────
195 
200  [[nodiscard]] AABB3D bounds() const;
201 
202  // ── Iterators for range-based for ─────────────────────
203 
207  class FaceRange;
208 
212  class VertexOneRing;
213 
221  [[nodiscard]] FaceRange faces_range() const;
222 
233  [[nodiscard]] VertexOneRing vertex_one_ring(int vi) const;
234 
235 private:
236  std::vector<Point3D> vertices_;
237  std::vector<Halfedge> halfedges_;
238  std::vector<Face> faces_;
239  std::vector<Vector3D> face_normals_;
240  std::vector<Vector3D> vertex_normals_;
241  bool normals_dirty_ = true;
242 
249  int find_or_create_edge(int v0, int v1);
250 };
251 
252 } // namespace vde::mesh
半边数据结构三角网格
Definition: halfedge_mesh.h:54
const Face & face(size_t idx) const
访问面
void build_from_triangles(const std::vector< Point3D > &verts, const std::vector< std::array< int, 3 >> &tris)
从三角形列表批量构建半边形网格
Vector3D vertex_normal(int vi) const
顶点法向(邻面法向的面积加权平均)
size_t num_vertices() const
顶点数量
Definition: halfedge_mesh.h:99
int add_vertex(const Point3D &p)
添加顶点
AABB3D bounds() const
网格包围盒
size_t num_faces() const
面数量
void update_normals()
重新计算所有面法向和顶点法向
const Point3D & vertex(size_t idx) const
访问顶点
FaceRange faces_range() const
获取所有面范围(range-based for 支持)
const Halfedge & halfedge(size_t idx) const
访问半边
void set_vertex(size_t idx, const Point3D &p)
更新顶点位置
void clear()
清空所有数据
int add_face(const std::vector< int > &vertex_indices)
添加面
size_t num_edges() const
边数量
bool is_boundary_vertex(int vi) const
是否为边界顶点
bool is_boundary_edge(int hei) const
是否为边界半边
VertexOneRing vertex_one_ring(int vi) const
获取顶点的 1-ring 邻域迭代器
std::vector< int > face_vertices(int fi) const
获取面的所有顶点索引(按环绕顺序)
Vector3D face_normal(int fi) const
面法向(几何法向,非归一化?由实现决定)
std::vector< int > vertex_faces(int vi) const
获取顶点的邻面索引环
AABB< double > AABB3D
双精度三维包围盒
Definition: aabb.h:131
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
int halfedge_index
该面的任意一条半边
Definition: halfedge_mesh.h:36
int valence
边数(三角面 = 3)
Definition: halfedge_mesh.h:37
半边数据结构元素
Definition: halfedge_mesh.h:20
int face_index
所属面的索引(边界半边为 -1)
Definition: halfedge_mesh.h:22
int prev_index
沿面的上一条半边
Definition: halfedge_mesh.h:24
int vertex_index
半边起点(终点 = next 半边的起点)
Definition: halfedge_mesh.h:21
int opposite_index
对侧半边索引(反方向半边)
Definition: halfedge_mesh.h:25
int next_index
沿面的下一条半边
Definition: halfedge_mesh.h:23