113 lines
3.5 KiB
C++
113 lines
3.5 KiB
C++
|
|
#pragma once
|
|||
|
|
/**
|
|||
|
|
* @file incremental_mesh.h
|
|||
|
|
* @brief 增量网格生成
|
|||
|
|
*
|
|||
|
|
* B-Rep 参数修改后,仅重建受影响面的 tessellation,而非全量重算。
|
|||
|
|
* 与 IncrementalUpdateEngine 集成,维护面级网格缓存。
|
|||
|
|
*
|
|||
|
|
* @ingroup brep
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#include "vde/brep/brep.h"
|
|||
|
|
#include "vde/mesh/halfedge_mesh.h"
|
|||
|
|
#include <unordered_map>
|
|||
|
|
#include <unordered_set>
|
|||
|
|
#include <vector>
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
namespace vde::brep {
|
|||
|
|
|
|||
|
|
// ═══════════════════════════════════════════════════════════
|
|||
|
|
// IncrementalMesher
|
|||
|
|
// ═══════════════════════════════════════════════════════════
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 增量网格生成器
|
|||
|
|
*
|
|||
|
|
* 维护 face_id → 三角网格片段的映射。
|
|||
|
|
* 参数修改时仅标脏受影响面,重建时只重新 tessellate 脏面。
|
|||
|
|
*
|
|||
|
|
* @ingroup brep
|
|||
|
|
*/
|
|||
|
|
class IncrementalMesher {
|
|||
|
|
public:
|
|||
|
|
/// 网格片段:单个面的三角网格
|
|||
|
|
struct FaceMesh {
|
|||
|
|
std::vector<core::Point3D> vertices;
|
|||
|
|
std::vector<std::array<int, 3>> triangles;
|
|||
|
|
double tessellation_deflection = 0.01;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 标脏指定面(参数已修改,需要重新 tessellate)
|
|||
|
|
*
|
|||
|
|
* @param face_id 面ID
|
|||
|
|
*/
|
|||
|
|
void invalidate_face(int face_id);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 增量重建:仅重新 tessellate 脏面
|
|||
|
|
*
|
|||
|
|
* @param body B-Rep 实体(提供几何数据)
|
|||
|
|
* @param deflection tessellation 精度
|
|||
|
|
* @return 重建的面数量
|
|||
|
|
*/
|
|||
|
|
[[nodiscard]] int rebuild_dirty(const BrepModel& body, double deflection = 0.01);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 全量重建(清除所有缓存后重建)
|
|||
|
|
*
|
|||
|
|
* @param body B-Rep 实体
|
|||
|
|
* @param deflection tessellation 精度
|
|||
|
|
* @return 重建的面数量
|
|||
|
|
*/
|
|||
|
|
[[nodiscard]] int rebuild_all(const BrepModel& body, double deflection = 0.01);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取单个面的缓存网格
|
|||
|
|
*
|
|||
|
|
* @param face_id 面ID
|
|||
|
|
* @return 缓存网格指针,脏面返回 nullptr
|
|||
|
|
*/
|
|||
|
|
[[nodiscard]] const FaceMesh* get_mesh(int face_id) const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取完整网格(合并所有已缓存面网格)
|
|||
|
|
*
|
|||
|
|
* 只包含非脏面的缓存数据。
|
|||
|
|
*
|
|||
|
|
* @return 合并后的完整网格
|
|||
|
|
*/
|
|||
|
|
[[nodiscard]] mesh::HalfedgeMesh merged_mesh() const;
|
|||
|
|
|
|||
|
|
/// 清除所有缓存
|
|||
|
|
void clear_all();
|
|||
|
|
|
|||
|
|
/// 统计
|
|||
|
|
[[nodiscard]] int dirty_count() const { return static_cast<int>(dirty_set_.size()); }
|
|||
|
|
[[nodiscard]] int total_faces() const { return static_cast<int>(meshes_.size()); }
|
|||
|
|
[[nodiscard]] int cache_hits() const { return cache_hits_; }
|
|||
|
|
[[nodiscard]] int cache_misses() const { return cache_misses_; }
|
|||
|
|
[[nodiscard]] double hit_rate() const;
|
|||
|
|
|
|||
|
|
/// 估计缓存占用内存(字节)
|
|||
|
|
[[nodiscard]] size_t memory_estimate() const;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
std::unordered_map<int, FaceMesh> meshes_; ///< face_id → cached mesh
|
|||
|
|
std::unordered_set<int> dirty_set_; ///< 脏面集合
|
|||
|
|
int cache_hits_ = 0;
|
|||
|
|
int cache_misses_ = 0;
|
|||
|
|
|
|||
|
|
/// Tessellate a single face of the BrepModel
|
|||
|
|
[[nodiscard]] FaceMesh tessellate_face(const BrepModel& body, int face_id,
|
|||
|
|
double deflection) const;
|
|||
|
|
|
|||
|
|
/// Merge vertex arrays from multiple face meshes into one HalfedgeMesh
|
|||
|
|
[[nodiscard]] mesh::HalfedgeMesh merge_face_meshes(
|
|||
|
|
const std::vector<const FaceMesh*>& meshes) const;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace vde::brep
|