Files

176 lines
7.0 KiB
C++
Raw Permalink 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.
#pragma once
/**
* @file large_assembly_render.h
* @brief 大装配渲染端 LOD 切换配置与屏幕空间 LOD 选择
*
* 从 `vde/brep/large_assembly.h` 拆分出的渲染/屏幕空间概念:
* - `LODPolicy`:LOD 切换策略(基于距离 / 屏幕占比 / 混合)
* - `LODSwitchConfig`:屏幕尺寸阈值、过渡帧数、自动切换开关
* - `compute_lightweight_lod()`:根据相机距离与屏幕占比计算推荐 LOD 级别
* - `auto_switch_lod()`:批量更新零件的轻量化表示 LOD 级别
*
* 这些概念只依赖内核的公开数据结构(`LightweightRepresentation` /
* `LightweightLevel`),不引入内核对其的反向依赖。
*
* @ingroup render
*/
#include "vde_assembly/large_assembly.h"
#include <algorithm>
#include <cstddef>
#include <vector>
namespace vde::render {
// ═══════════════════════════════════════════════════════════
// LODPolicy / LODSwitchConfig
// ═══════════════════════════════════════════════════════════
/**
* @brief LOD 切换策略
*/
enum class LODPolicy : int {
DistanceBased = 0, ///< 基于相机距离
ScreenSizeBased = 1, ///< 基于屏幕空间占比
Hybrid = 2 ///< 混合策略(距离 + 屏幕占比)
};
/**
* @brief LOD 切换配置(屏幕空间 / 相机距离阈值)
*
* 驱动 `compute_lightweight_lod()` 与 `auto_switch_lod()` 的渲染端参数。
*/
struct LODSwitchConfig {
LODPolicy policy = LODPolicy::Hybrid;
double medium_distance = 50.0; ///< 超过此距离切换到 Medium
double coarse_distance = 200.0; ///< 超过此距离切换到 Coarse
double bbox_distance = 500.0; ///< 超过此距离切换到 BoundingBox
double screen_size_full = 0.05; ///< 屏幕占比 > 此值 → Full
double screen_size_medium = 0.02; ///< 屏幕占比 > 此值 → Medium
double screen_size_coarse = 0.005; ///< 屏幕占比 > 此值 → Coarse
bool enable_auto_switch = true; ///< 是否启用自动切换
int transition_frames = 5; ///< LOD 切换过渡帧数(渐变)
};
// ═══════════════════════════════════════════════════════════
// 屏幕空间 LOD 选择
// ═══════════════════════════════════════════════════════════
/**
* @brief 计算单个零件的推荐 LOD 级别
*
* @param rep 轻量化表示(内核数据,只读)
* @param camera_distance 相机到装配体中心的距离
* @param screen_diagonal 屏幕对角线长度(用于屏幕占比计算)
* @param config LOD 切换配置
* @return 推荐的轻量化级别
*/
[[nodiscard]] brep::LightweightLevel compute_lightweight_lod(
const brep::LightweightRepresentation& rep,
double camera_distance,
double screen_diagonal,
const LODSwitchConfig& config);
/**
* @brief 基于相机距离与屏幕占比自动切换所有零件的 LOD
*
* 跳过手动设置了 LOD 覆盖的零件。当 `config.enable_auto_switch` 为 false 时直接返回。
*
* @param reps 轻量化表示列表(将被原地更新)
* @param camera_distance 相机到装配体中心的距离
* @param screen_diagonal 屏幕对角线长度
* @param config LOD 切换配置
*/
void auto_switch_lod(
std::vector<brep::LightweightRepresentation>& reps,
double camera_distance,
double screen_diagonal,
const LODSwitchConfig& config);
// ── 宿主预算选择(纯数据契约;不依赖内核/GPU)────────────────────────────
struct LODBudgetItem {
std::size_t full_triangles = 0; ///< 该实例 Full LOD 的估算三角形数
bool visible = true;
bool selected = false; ///< 选中/高亮优先
int priority = 0; ///< 越大越优先;同优先级按输入顺序稳定
brep::LightweightLevel lod = brep::LightweightLevel::Full;
};
struct LODBudgetConfig {
std::size_t max_triangles = 0; ///< 0 = 不限制
std::size_t max_instances = 0; ///< 0 = 不限制
double full_weight = 1.0;
double medium_weight = 0.35;
double coarse_weight = 0.10;
double bbox_weight = 0.01;
};
struct LODBudgetResult {
std::size_t submitted_instances = 0;
std::size_t submitted_triangles = 0;
std::size_t downgraded = 0;
std::size_t skipped = 0;
bool limited = false;
};
/**
* @brief 按宿主预算选择 LOD;就地更新 items[i].lod
*
* 硬约束:submitted_triangles <= max_triangles、submitted_instances <= max_instances
* (限制为 0 时视为不限)。顺序:visible、selected、priority、输入顺序。
* 放不下的实例跳过并计入 skipped,不保留超预算实例。
*/
inline LODBudgetResult apply_lod_budget(std::vector<LODBudgetItem>& items,
const LODBudgetConfig& config) {
std::vector<std::size_t> order;
order.reserve(items.size());
for (std::size_t i = 0; i < items.size(); ++i) {
if (items[i].visible && items[i].full_triangles > 0) order.push_back(i);
}
std::stable_sort(order.begin(), order.end(), [&](std::size_t a, std::size_t b) {
if (items[a].selected != items[b].selected) return items[a].selected;
if (items[a].priority != items[b].priority)
return items[a].priority > items[b].priority;
return false;
});
LODBudgetResult result;
const double weights[4] = {config.full_weight, config.medium_weight,
config.coarse_weight, config.bbox_weight};
for (std::size_t index : order) {
if (config.max_instances > 0 && result.submitted_instances >= config.max_instances) {
result.skipped += 1;
result.limited = true;
continue;
}
const std::size_t full = items[index].full_triangles;
bool placed = false;
for (int level = 0; level < 4; ++level) {
std::size_t estimate = static_cast<std::size_t>(
static_cast<double>(full) * weights[level]);
if (estimate == 0) estimate = 1;
if (config.max_triangles > 0 &&
result.submitted_triangles + estimate > config.max_triangles)
continue;
result.submitted_triangles += estimate;
items[index].lod = static_cast<brep::LightweightLevel>(level);
++result.submitted_instances;
if (level > 0) {
++result.downgraded;
result.limited = true;
}
placed = true;
break;
}
if (!placed) {
++result.skipped;
result.limited = true;
}
}
return result;
}
} // namespace vde::render