Files
hm 26e89aa5f6
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat(proposal): 落地 VDE-370 提案 API(SSAO + 曲率可视化)
- ssao():新增 SSAOParams 针孔相机参数;软件深度缓冲光栅化 + 屏幕空间
  黄金角螺旋采样,实现视相关环境光遮蔽(复用 AOResult)。
- curvature_visualization():新增 CurvatureVisData,复用 compute_curvature
  的 (H,K) 反解主曲率 κ1,2 = H ± √(max(0,H²−K))。
- 回归测试 test_visualization:新增 5 例(平面内部零曲率/主曲率一致性/
  平面零自遮蔽/凹谷遮蔽/空网格),并修正既有 EdgeHighlightTest 两例错误期望。
  全套 16/16 PASSED。
- 核验表计数 39/38/0/230(VDE-370 缺失令牌清零)。
2026-10-05 06:22:26 +08:00

199 lines
8.5 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 visualization_quality.h
* @brief 网格可视化增强与质量评估
*
* 提供环境光遮蔽、硬边检测/高亮、线框叠加、法线可视化等渲染辅助数据。
* 所有函数输出纯数据(颜色数组、线段数组等),由渲染管线消费。
*
* @ingroup mesh
*/
#include "vde/mesh/halfedge_mesh.h"
#include "vde/core/point.h"
#include <vector>
#include <array>
#include <utility>
#include <cstdint>
namespace vde::mesh {
using core::Point3D;
using core::Vector3D;
// ═══════════════════════════════════════════════════════════
// 数据结构
// ═══════════════════════════════════════════════════════════
/// 逐顶点 AO 值
struct AOResult {
std::vector<double> vertex_ao; ///< 逐顶点环境光遮蔽因子 [0,1](1=完全暴露)
double min_ao; ///< 最小 AO 值
double max_ao; ///< 最大 AO 值
double avg_ao; ///< 平均 AO 值
int samples; ///< 实际采样数
};
/// 硬边检测结果
struct EdgeHighlightResult {
std::vector<std::pair<int,int>> hard_edges; ///< 硬边:(vA, vB) 顶点索引对
std::vector<double> edge_angles; ///< 对应二面角(弧度)
double angle_threshold; ///< 使用的角度阈值
int total_edges; ///< 检测总边数
int hard_count; ///< 硬边数
};
/// 线框叠加线段
struct WireframeOverlay {
std::vector<std::pair<int,int>> wire_edges; ///< 线框边:(vA, vB)
std::vector<Point3D> vertices; ///< 顶点坐标
};
/// 法线可视化数据
struct NormalVisData {
std::vector<Point3D> face_centers; ///< 面中心
std::vector<Vector3D> face_normals; ///< 面法线
std::vector<Point3D> vertex_positions; ///< 顶点位置
std::vector<Vector3D> vertex_normals; ///< 顶点法线(平均面法线)
};
// ═══════════════════════════════════════════════════════════
// 环境光遮蔽
// ═══════════════════════════════════════════════════════════
/**
* @brief 计算逐顶点环境光遮蔽(Ambient Occlusion)
*
* 对每个顶点在其上半球采样 `samples` 条射线,统计被遮挡比例。
* AO = 1 - (被遮挡射线数 / 总射线数)。
*
* @param mesh 三角网格
* @param samples 每顶点采样射线数(默认 128,越多越精确)
* @return AOResult 包含逐顶点 AO 值及统计信息
*
* @ingroup mesh
*/
AOResult ambient_occlusion(const HalfedgeMesh& mesh, int samples = 128);
// ═══════════════════════════════════════════════════════════
// 边高亮(硬边检测)
// ═══════════════════════════════════════════════════════════
/**
* @brief 检测硬边(二面角超过阈值的边)
*
* 遍历网格所有边,计算相邻两面的二面角,
* 角度超过 angle_threshold(弧度)的边标记为硬边。
*
* @param mesh 三角网格
* @param angle_threshold 硬边阈值(弧度),默认 π/4 = 45°
* @return EdgeHighlightResult
*
* @ingroup mesh
*/
EdgeHighlightResult edge_highlighting(const HalfedgeMesh& mesh,
double angle_threshold = 0.785398163);
// ═══════════════════════════════════════════════════════════
// 线框叠加
// ═══════════════════════════════════════════════════════════
/**
* @brief 提取线框叠加数据
*
* 从实体网格提取所有唯一边,生成线框表示。
* 可选地排除与 solid_mesh 重合的边(只显示差异边)。
*
* @param mesh 三角网格
* @param solid_mesh 实体网格(空网格 = 忽略)
* @return WireframeOverlay
*
* @ingroup mesh
*/
WireframeOverlay wireframe_overlay(const HalfedgeMesh& mesh,
const HalfedgeMesh& solid_mesh = HalfedgeMesh());
// ═══════════════════════════════════════════════════════════
// 法线可视化
// ═══════════════════════════════════════════════════════════
/**
* @brief 生成法线可视化数据
*
* 计算每个面的中心点与法线、每个顶点法线(相邻面法线的面积加权平均)。
*
* @param mesh 三角网格
* @return NormalVisData
*
* @ingroup mesh
*/
NormalVisData normal_visualization(const HalfedgeMesh& mesh);
// ═══════════════════════════════════════════════════════════
// 曲率可视化(VDE-370 §2)
// ═══════════════════════════════════════════════════════════
/**
* @brief 曲率可视化数据
*
* 逐顶点的平均/高斯/主曲率,供渲染端按曲率着色(曲面质量检查)。
*/
struct CurvatureVisData {
std::vector<double> mean_curvature; ///< 逐顶点平均曲率 H
std::vector<double> gaussian_curvature; ///< 逐顶点高斯曲率 K
std::vector<std::pair<double,double>> principal_values;///< 逐顶点主曲率 (κ₁, κ₂)
};
/**
* @brief 计算逐顶点曲率可视化数据(VDE-370 §2)
*
* 复用 `mesh_curvature.h` 的离散曲率(Cotan 公式,Meyer et al. 2003)
* 得到 (H, K),再由 κ₁,₂ = H ± √(max(0, H²−K)) 反解主曲率。
*
* @param mesh 三角网格
* @return CurvatureVisData
*
* @ingroup mesh
*/
CurvatureVisData curvature_visualization(const HalfedgeMesh& mesh);
// ═══════════════════════════════════════════════════════════
// 屏幕空间环境光遮蔽(SSAO,VDE-370 §1)
// ═══════════════════════════════════════════════════════════
/**
* @brief SSAO 相机与采样参数
*
* 相机采用针孔模型;`radius` 为视空间采样半径,`bias` 为深度偏移
* (抑制表面自遮蔽)。
*/
struct SSAOParams {
Point3D eye{0.0, 0.0, 0.0}; ///< 相机位置
Vector3D forward{0.0, 0.0, -1.0}; ///< 视线方向(自动归一化)
Vector3D up{0.0, 1.0, 0.0}; ///< 上方向
double fov_y_deg = 45.0; ///< 垂直视场角(度)
double near_z = 0.1; ///< 近裁剪面
double far_z = 1000.0; ///< 远裁剪面
int screen_width = 256; ///< 深度缓冲宽度(像素)
int screen_height = 256; ///< 深度缓冲高度(像素)
int samples = 16; ///< 每顶点屏幕空间采样数
double radius = 0.5; ///< 视空间采样半径
double bias = 0.002; ///< 深度偏移
};
/**
* @brief 屏幕空间环境光遮蔽(SSAO)(VDE-370 §1)
*
* 1) 将网格光栅化到软件深度缓冲(逐像素最近视深度);
* 2) 对每个顶点投影到屏幕,在半径 `radius` 的圆盘内采样深度缓冲,
* 若邻域存在更近的几何(深度 < 顶点深度 − bias)则计为被遮蔽。
* AO = 1 − 被遮蔽采样数 / samples。
*
* @param mesh 三角网格
* @param params 相机与采样参数
* @return AOResult 逐顶点 SSAO 值(与物体空间 `ambient_occlusion` 同构)
*
* @ingroup mesh
*/
AOResult ssao(const HalfedgeMesh& mesh, const SSAOParams& params);
} // namespace vde::mesh