### 1.0.74 验证 + scaffold 缺陷修复 - 收录报告 70(1.0.74 核心 PASS,format scaffold --check RC=3 缺陷); - 修复 scaffold 两阶段 token 替换(新 id 不再被 .example 规则二次改写);四类 scaffold --check/--list PASS; - 新增 CTest VDE_PluginToolScaffoldSmoke 1/1;插件相关 ctest 112/112; ### P3-15f assembly 官方模块拆分(部分) - 新增 vde_assembly:assembly_patterns/constraints/instance/flexible/large_assembly 5 TU 迁出;规范头+兼容转发头; - VDE_BUILD_ASSEMBLY/VDE_DEPS_assembly、聚合/app、render 依赖级联、测试门控、守卫更新; - vde-assembly 包 + consumer VDE_ASSEMBLY_CONSUMER_PASS parts=3 placed=1 unique=1;kernel-only OFF 构建 0 failed/0 undefined; - 阻塞清单:assembly 核心数据模型与 assembly_feature/PMI 留内核(step_import 双向包含、序列化/格式导入依赖),下一轮先拆 PMI/解耦; ### P3-17c 远程安装/依赖解析/卸载/原子升级 - --fetch-packages(URL/本地 artifacts 下载/复制+SHA-256+staging 原子提交+回滚)、requires 简化 semver 依赖解析、 --uninstall、--upgrade(备份→原子替换→失败恢复); - test_plugin_tool_remote_install 9/9;docs plugin-distribution §2/§4.7/§4.8/§4.9/§7/§9;roadmap P3-17; ### 工程 - 版本 1.0.75(CMake/version.h/test_version/README/docs index/module-manual);模块目标 27; - 全量构建 0 error(369 targets);全量 ctest 7550 注册、7455/7455 PASS(0 failed,25 skipped,177.37s)。
467 lines
18 KiB
C++
467 lines
18 KiB
C++
#pragma once
|
||
/**
|
||
* @file large_assembly.h
|
||
* @brief 大装配性能优化(10K+ 零件)
|
||
*
|
||
* 通过空间索引加速、实例共享、LOD 自动切换,
|
||
* 支持大规模装配体的高效遍历和渲染。
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
|
||
#include "vde/brep/assembly.h"
|
||
#include "vde_assembly/assembly_instance.h"
|
||
#include "vde/spatial/bvh.h"
|
||
#include "vde/core/aabb.h"
|
||
#include "vde/core/plane.h"
|
||
#include "vde/mesh/mesh_lod.h"
|
||
#include <unordered_map>
|
||
#include <string>
|
||
#include <vector>
|
||
#include <optional>
|
||
|
||
namespace vde::brep {
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// LightweightRepresentation — 轻量化表示 + LOD 切换
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 轻量化表示级别
|
||
*/
|
||
enum class LightweightLevel : int {
|
||
Full = 0, ///< 完整 B-Rep
|
||
Medium = 1, ///< 中等精度网格
|
||
Coarse = 2, ///< 低精度网格
|
||
BoundingBox = 3 ///< 仅包围盒
|
||
};
|
||
|
||
/// 轻量化级别转字符串
|
||
[[nodiscard]] inline const char* to_string(LightweightLevel lv) {
|
||
switch (lv) {
|
||
case LightweightLevel::Full: return "Full";
|
||
case LightweightLevel::Medium: return "Medium";
|
||
case LightweightLevel::Coarse: return "Coarse";
|
||
case LightweightLevel::BoundingBox: return "BoundingBox";
|
||
}
|
||
return "Unknown";
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// LODHint — 子装配体级 LOD 配置(VDE-361 §3)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 子装配体 LOD 提示级别
|
||
*
|
||
* 按子装配体名称指定期望的细节层级,例如
|
||
* "轴承显示为圆柱、轴保持完整细节"。
|
||
*
|
||
* 与渲染端 `LightweightLevel` 一一对应(Simplest→BoundingBox …
|
||
* Detailed→Full)。
|
||
*/
|
||
enum class LODHint : int {
|
||
Simplest = 0, ///< 最简表示(仅包围盒)
|
||
Simplified = 1, ///< 简化表示(低精度网格)
|
||
Normal = 2, ///< 常规表示(中等精度网格)
|
||
Detailed = 3 ///< 完整细节(原始 B-Rep)
|
||
};
|
||
|
||
[[nodiscard]] inline const char* to_string(LODHint h) {
|
||
switch (h) {
|
||
case LODHint::Simplest: return "Simplest";
|
||
case LODHint::Simplified: return "Simplified";
|
||
case LODHint::Normal: return "Normal";
|
||
case LODHint::Detailed: return "Detailed";
|
||
}
|
||
return "Unknown";
|
||
}
|
||
|
||
/// LODHint → LightweightLevel 映射
|
||
[[nodiscard]] inline LightweightLevel to_lightweight_level(LODHint h) {
|
||
switch (h) {
|
||
case LODHint::Simplest: return LightweightLevel::BoundingBox;
|
||
case LODHint::Simplified: return LightweightLevel::Coarse;
|
||
case LODHint::Normal: return LightweightLevel::Medium;
|
||
case LODHint::Detailed: return LightweightLevel::Full;
|
||
}
|
||
return LightweightLevel::Full;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// OcclusionCullingResult — 遮挡剔除结果(VDE-361 §1)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 遮挡剔除结果
|
||
*
|
||
* `occluded` 中的零件均经过保守判定:其全部投影区域都位于
|
||
* 更近几何之后,可安全剔除;其余进入 `visible`。
|
||
*/
|
||
struct OcclusionCullingResult {
|
||
std::vector<std::string> visible; ///< 通过遮挡测试的零件名称
|
||
std::vector<std::string> occluded; ///< 被遮挡剔除的零件名称
|
||
size_t tested = 0; ///< 参与遮挡测试的零件数
|
||
size_t buffer_tiles_x = 0; ///< 层级 Z 缓冲级别 0 的列数
|
||
size_t buffer_tiles_y = 0; ///< 层级 Z 缓冲级别 0 的行数
|
||
|
||
/// 遮挡剔除比例 [0,1]
|
||
[[nodiscard]] double occlusion_ratio() const {
|
||
return tested ? static_cast<double>(occluded.size()) /
|
||
static_cast<double>(tested)
|
||
: 0.0;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* @brief 轻量化表示 — 零件被简化后的替代几何
|
||
*
|
||
* 每个零件在 LOD 切换时有四种可选表示级别,
|
||
* 同时维护原 B-Rep 引用以便需要时恢复。
|
||
*/
|
||
struct LightweightRepresentation {
|
||
int part_id = -1; ///< 零件 ID
|
||
std::string part_name; ///< 零件名称
|
||
LightweightLevel current_level = LightweightLevel::Full; ///< 当前 LOD 级别
|
||
BrepModel const* full_brep = nullptr; ///< 原始 B-Rep(不拥有)
|
||
std::optional<vde::mesh::LODMesh> medium_mesh; ///< 中等精度网格
|
||
std::optional<vde::mesh::LODMesh> coarse_mesh; ///< 低精度网格
|
||
core::AABB3D bbox; ///< 包围盒(缓存)
|
||
bool shared_instance = false; ///< 该零件是否为共享实例
|
||
int instance_group_id = -1; ///< 实例组 ID(相同几何)
|
||
bool manual_lod_override = false; ///< 是否手动设置 LOD
|
||
|
||
/// 是否已生成简化网格
|
||
[[nodiscard]] bool has_simplified() const {
|
||
return medium_mesh.has_value() || coarse_mesh.has_value();
|
||
}
|
||
|
||
/// 获取当前表示的三角面数
|
||
[[nodiscard]] size_t current_triangle_count() const;
|
||
};
|
||
|
||
/**
|
||
* @brief LOD 切换配置与屏幕空间 LOD 选择已移至
|
||
* `vde_render/large_assembly_render.h`(`vde::render` 命名空间)。
|
||
*
|
||
* 渲染端通过 `render::compute_lightweight_lod()` / `render::auto_switch_lod()`
|
||
* 在公开数据结构上执行 LOD 切换,避免内核对其产生反向依赖。
|
||
*
|
||
* @see vde_render/large_assembly_render.h
|
||
*/
|
||
|
||
/**
|
||
* @brief 轻量化表示管理器
|
||
*
|
||
* 为装配体每个零件维护 LightweightRepresentation,
|
||
* 集成实例共享减少重复零件的几何存储。
|
||
*
|
||
* 屏幕空间 LOD 自动切换已移至渲染端
|
||
* (`vde::render::auto_switch_lod`);本类仅负责数据管理
|
||
* (零件收集、简化网格生成、手动 LOD、统计)。
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
class LightweightRepresentationManager {
|
||
public:
|
||
|
||
/// 默认构造函数 — 使用内置默认装配体
|
||
LightweightRepresentationManager() : assembly_(nullptr) {}
|
||
|
||
/**
|
||
* @brief 从装配体构建轻量化表示管理器
|
||
* @param assembly 装配体引用
|
||
*/
|
||
explicit LightweightRepresentationManager(Assembly& assembly);
|
||
|
||
/// 获取所有轻量化表示
|
||
[[nodiscard]] const std::vector<LightweightRepresentation>& representations() const {
|
||
return reps_;
|
||
}
|
||
|
||
/// 获取所有轻量化表示(可变引用,供渲染端 LOD 切换使用)
|
||
[[nodiscard]] std::vector<LightweightRepresentation>& representations() {
|
||
return reps_;
|
||
}
|
||
|
||
/// 获取某零件的轻量化表示
|
||
[[nodiscard]] const LightweightRepresentation* get_rep(int part_id) const;
|
||
|
||
/**
|
||
* @brief 基于相机距离的自动 LOD 切换已移至渲染端
|
||
* (`vde::render::auto_switch_lod`)。
|
||
*/
|
||
|
||
/// 手动设置某零件的 LOD
|
||
void set_lod_level(int part_id, LightweightLevel level);
|
||
|
||
/// 清除手动 LOD 设置
|
||
void clear_manual_override(int part_id);
|
||
|
||
/// 清除所有手动设置
|
||
void clear_all_manuals();
|
||
|
||
/// 生成所有简化网格
|
||
void generate_simplified_meshes();
|
||
|
||
/// 统计信息
|
||
struct LODStats {
|
||
size_t full_count = 0, medium_count = 0;
|
||
size_t coarse_count = 0, bbox_count = 0;
|
||
size_t shared_count = 0;
|
||
size_t manual_count = 0;
|
||
size_t total_triangles = 0;
|
||
};
|
||
/// 获取 LOD 统计信息
|
||
[[nodiscard]] LODStats statistics() const;
|
||
|
||
/// 零件总数
|
||
[[nodiscard]] size_t part_count() const { return reps_.size(); }
|
||
|
||
private:
|
||
Assembly* assembly_ = nullptr;
|
||
std::vector<LightweightRepresentation> reps_;
|
||
core::AABB3D total_bounds_;
|
||
|
||
/// 收集装配体中所有零件并构建轻量化表示
|
||
void collect_parts(AssemblyNode* node, const Transform3D& parent_tf,
|
||
int depth, std::unordered_map<std::string, int>& instance_map);
|
||
|
||
static Assembly& default_assembly() {
|
||
static Assembly a("__default__");
|
||
return a;
|
||
}
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// InstanceCache — shared mesh data
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 实例缓存:相同零件共享 mesh 数据
|
||
*
|
||
* 对同一 instance_id 的零件只存储一份 mesh,
|
||
* 不同实例通过变换矩阵区分。
|
||
*/
|
||
class InstanceCache {
|
||
public:
|
||
/// 注册实例(返回是否新实例)
|
||
bool register_instance(int instance_id, const BrepModel& model);
|
||
|
||
/// 获取实例的 LOD mesh
|
||
[[nodiscard]] const vde::mesh::LODMesh* get_lod(int instance_id) const;
|
||
|
||
/// 获取实例的 AABB
|
||
[[nodiscard]] const core::AABB3D* get_bounds(int instance_id) const;
|
||
|
||
/// 唯一实例数
|
||
[[nodiscard]] int unique_count() const { return static_cast<int>(meshes_.size()); }
|
||
|
||
/// 总内存估算(字节)
|
||
[[nodiscard]] size_t memory_estimate() const;
|
||
|
||
/// 获取实例的轻量化表示
|
||
[[nodiscard]] LightweightLevel get_lod_level(int instance_id) const;
|
||
|
||
/// 设置实例的 LOD 级别
|
||
void set_lod_level(int instance_id, LightweightLevel level);
|
||
|
||
private:
|
||
std::unordered_map<int, vde::mesh::LODMesh> meshes_;
|
||
std::unordered_map<int, core::AABB3D> bounds_;
|
||
std::unordered_map<int, LightweightLevel> lod_levels_;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Frustum — 视锥体裁剪(E7:百万级零件平截头体裁剪)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 视锥体(6 个平面,法向均指向体内)
|
||
*
|
||
* 平面顺序:near / far / left / right / top / bottom。
|
||
* 点在体内 ⇔ 对全部 6 个平面 signed_distance ≥ 0。
|
||
*/
|
||
struct Frustum {
|
||
core::Plane3D planes[6];
|
||
|
||
/**
|
||
* @brief 由透视相机参数构造视锥体
|
||
* @param eye 相机位置
|
||
* @param forward 视线方向(自动归一化)
|
||
* @param up 上方向
|
||
* @param fov_y_deg 垂直视场角(度)
|
||
* @param aspect 宽高比 (w/h)
|
||
* @param near_z 近裁剪面距离(> 0)
|
||
* @param far_z 远裁剪面距离(> near_z)
|
||
*/
|
||
static Frustum from_perspective(const core::Point3D& eye,
|
||
const core::Vector3D& forward,
|
||
const core::Vector3D& up,
|
||
double fov_y_deg, double aspect,
|
||
double near_z, double far_z);
|
||
|
||
/**
|
||
* @brief AABB 是否与视锥体相交(用于剔除测试)
|
||
*
|
||
* 对每个平面使用 p-vertex(法向方向最远角点)判定:
|
||
* 若存在平面使得 p-vertex 完全在体外,则不相交。
|
||
*/
|
||
[[nodiscard]] bool intersects(const core::AABB3D& box) const;
|
||
|
||
/**
|
||
* @brief AABB 是否完全位于视锥体内
|
||
*
|
||
* 对每个平面使用 n-vertex(法向方向最近角点)判定。
|
||
*/
|
||
[[nodiscard]] bool contains(const core::AABB3D& box) const;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// LargeAssemblyManager
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 大装配管理器
|
||
*
|
||
* 提供空间索引加速的装配遍历、视锥剔除、LOD 选择。
|
||
*/
|
||
class LargeAssemblyManager {
|
||
public:
|
||
/**
|
||
* @brief 从装配体构建空间索引
|
||
*
|
||
* 遍历所有零件,将其 AABB 插入零件级 BVH(O(n log n))。
|
||
*
|
||
* @param assembly 装配体
|
||
*/
|
||
void build_index(const Assembly& assembly);
|
||
|
||
/**
|
||
* @brief 视锥剔除查询
|
||
*
|
||
* 返回在指定 AABB 范围内的零件名称列表。
|
||
*
|
||
* @param view_aabb 视口 AABB(世界坐标)
|
||
* @return 可见零件名称
|
||
*/
|
||
[[nodiscard]] std::vector<std::string> query_visible(
|
||
const core::AABB3D& view_aabb) const;
|
||
|
||
/**
|
||
* @brief 平截头体剔除查询(E7)
|
||
*
|
||
* 基于零件级 BVH 索引,返回与视锥体相交的零件名称列表。
|
||
* 复杂度 O(log n + k)。
|
||
*
|
||
* @param frustum 视锥体(世界坐标)
|
||
* @return 可见零件名称
|
||
*/
|
||
[[nodiscard]] std::vector<std::string> query_visible(
|
||
const Frustum& frustum) const;
|
||
|
||
/**
|
||
* @brief 层级 Z 缓冲遮挡剔除(VDE-361 §1)
|
||
*
|
||
* 在 BVH 平截头体裁剪的候选集之上,按前→后顺序将零件 AABB
|
||
* 投影到屏幕空间,维护多层(mip)保守深度缓冲:
|
||
* 某零件最近深度严格大于其投影覆盖区域的缓冲最大深度时,
|
||
* 判定为完全被遮挡并剔除。
|
||
*
|
||
* 采用保守判据(缓冲取区域最大深度),因此 `occluded` 中的
|
||
* 零件必定被真实遮挡,不会误剔;代价是可能漏剔部分边缘零件。
|
||
*
|
||
* @param eye 相机位置
|
||
* @param forward 视线方向
|
||
* @param up 上方向
|
||
* @param fov_y_deg 垂直视场角(度)
|
||
* @param aspect 宽高比 (w/h)
|
||
* @param near_z 近裁剪面距离(> 0)
|
||
* @param far_z 远裁剪面距离(> near_z)
|
||
* @param screen_width 屏幕宽度(像素)
|
||
* @param screen_height 屏幕高度(像素)
|
||
* @param tile_size 级别 0 深度缓冲瓦片边长(像素,> 0)
|
||
*/
|
||
[[nodiscard]] OcclusionCullingResult occlusion_cull(
|
||
const core::Point3D& eye, const core::Vector3D& forward,
|
||
const core::Vector3D& up, double fov_y_deg, double aspect,
|
||
double near_z, double far_z, int screen_width, int screen_height,
|
||
int tile_size = 16) const;
|
||
|
||
/**
|
||
* @brief 设置子装配体 LOD 提示(VDE-361 §3)
|
||
* @param name 子装配体名称
|
||
* @param hint 期望细节层级
|
||
*/
|
||
void set_subassembly_lod(const std::string& name, LODHint hint);
|
||
|
||
/// 查询子装配体 LOD 提示(未设置返回 nullopt)
|
||
[[nodiscard]] std::optional<LODHint> get_subassembly_lod(
|
||
const std::string& name) const;
|
||
|
||
/// 全部子装配体 LOD 配置
|
||
[[nodiscard]] const std::unordered_map<std::string, LODHint>&
|
||
subassembly_lods() const {
|
||
return subassembly_lods_;
|
||
}
|
||
|
||
/**
|
||
* @brief 获取装配统计信息
|
||
*/
|
||
struct AssemblyStats {
|
||
int total_parts = 0;
|
||
int unique_parts = 0;
|
||
int subassemblies = 0;
|
||
int max_depth = 0;
|
||
core::AABB3D total_bounds;
|
||
size_t estimated_memory = 0;
|
||
};
|
||
|
||
/// 计算装配统计信息
|
||
[[nodiscard]] AssemblyStats compute_stats(const Assembly& assembly) const;
|
||
|
||
/// 零件数
|
||
[[nodiscard]] int part_count() const { return part_count_; }
|
||
|
||
/// 索引构建状态
|
||
[[nodiscard]] bool index_built() const { return index_built_; }
|
||
|
||
/// 获取 LightweightRepresentationManager
|
||
[[nodiscard]] LightweightRepresentationManager& lw_manager() { return lw_manager_; }
|
||
[[nodiscard]] const LightweightRepresentationManager& lw_manager() const { return lw_manager_; }
|
||
|
||
private:
|
||
/// 零件级 BVH 节点(AABB 图元,E7 百万级平截头体裁剪)
|
||
struct PartBVHNode {
|
||
core::AABB3D bounds;
|
||
int left = -1; ///< 左子节点索引,-1 表示叶节点
|
||
int right = -1; ///< 右子节点索引
|
||
int first = 0; ///< 叶节点:零件范围起始索引
|
||
int count = 0; ///< 叶节点:零件数量
|
||
};
|
||
|
||
std::vector<core::AABB3D> part_aabbs_;
|
||
std::vector<std::string> part_names_;
|
||
std::vector<PartBVHNode> bvh_nodes_; ///< 零件级 BVH 节点数组
|
||
int part_count_ = 0;
|
||
bool index_built_ = false;
|
||
LightweightRepresentationManager lw_manager_;
|
||
std::unordered_map<std::string, LODHint> subassembly_lods_;
|
||
|
||
void collect_parts(const AssemblyNode& node, const core::Transform3D& parent_tf,
|
||
int depth, AssemblyStats& stats) const;
|
||
|
||
/// 递归构建零件级 BVH(按最长轴中心分裂)
|
||
void build_part_bvh(int node_idx, int first, int count, int depth);
|
||
|
||
/// BVH 范围查询(AABB 相交)
|
||
void query_bvh_range(int node_idx, const core::AABB3D& range,
|
||
std::vector<std::string>& out) const;
|
||
|
||
/// BVH 平截头体查询(视锥相交)
|
||
void query_bvh_frustum(int node_idx, const Frustum& frustum,
|
||
std::vector<std::string>& out) const;
|
||
};
|
||
|
||
} // namespace vde::brep
|