# SurfaceMesh.hpp 头文件解析
## 概述
本头文件定义了三维网格拓扑操作的核心数据结构,主要用于高效处理三角网格的邻接关系与遍历逻辑。作为3D打印领域网格处理的基础设施,支持模型修复、支撑生成等关键功能。
## 核心数据结构
### 索引类型
```cpp
enum Face_index : int; // 面片索引
class Halfedge_index; // 半边索引(包含面ID+边序)
class Vertex_index; // 顶点索引(包含面ID+顶点序)
SurfaceMesh 类
class SurfaceMesh {
private:
const indexed_triangle_set& m_its; // 三角网格数据引用
std::vector<Vec3i> m_face_neighbors; // 面邻接表
public:
// 遍历方法
Halfedge_index opposite(Halfedge_index) const; // 获取对向边
Vertex_index source(Halfedge_index) const; // 边起点
Vertex_index target(Halfedge_index) const; // 边终点
// 拓扑查询
size_t degree(Vertex_index) const; // 顶点度数
bool is_border(Halfedge_index) const; // 边界检测
};
关键算法解析
邻接关系构建
// 通过并行算法生成面邻接表
m_face_neighbors(its_face_neighbors_par(its))
- 数据结构:每个面记录3个邻接面索引(-1表示无边)
- 复杂度:O(n) 通过哈希加速查询
半边对向查找
Halfedge_index opposite(Halfedge_index h) const {
int neighbor_face = m_face_neighbors[h.face()][h.side()];
// 遍历邻接面的边寻找匹配
for (3次旋转) {
if (顶点匹配) return 对应边;
}
return 无效边;
}
- 应用场景:孔洞边界检测、流形验证
- 性能优化:依赖预计算的邻接表
拓扑遍历方法
环绕遍历
// 环绕目标顶点的下一条边
Halfedge_index next_around_target(Halfedge_index h) {
return opposite(next(h));
}
// 环绕源顶点的前一条边
Halfedge_index prev_around_source(Halfedge_index h) {
return opposite(prev(h));
}
- 典型应用:
- 计算顶点邻域
- 生成轮廓路径
度数计算
size_t degree(Vertex_index v) const {
Halfedge_index h = halfedge(v);
size_t count = 0;
do {
h = next_around_target(h);
++count;
} while (h有效且未循环);
return count;
}
- 特殊处理:防无限循环机制
- 边界情况:开放边返回0
性能特征
操作 | 时间复杂度 | 空间复杂度 |
---|---|---|
邻接表构建 | O(n) | O(n) |
对向边查找 | O(1) | - |
顶点度数计算 | O(d) | - |
环绕遍历 | O(1) | - |
设计亮点
紧凑索引结构
- Halfedge_index:8字节存储(4B面ID + 1B边序)
- Vertex_index:5字节存储(4B面ID + 1B顶点序)
零拷贝设计
- 持有所引用的indexed_triangle_set
- 避免网格数据重复存储
应用场景
3D打印预处理
- 模型修复:检测非流形边
- 支撑生成:识别悬垂区域
- 切片优化:计算截面轮廓
路径规划
扩展建议
功能增强
• 缓存机制:存储常用遍历结果
• 并行计算:度数统计等操作并行化
异常处理
• 添加网格有效性验证方法
• 完善非流形网格处理逻辑
本模块通过高效的拓扑关系管理,为3D打印预处理流程提供基础几何支持,是实现高质量网格操作的核心组件。