提案 4 项 API 全部实现(vde_ui/viewport_api.h): - screen_to_ray:用 (proj*view) 逆矩阵反投影屏幕像素到世界空间射线; PickRay 扩展 t_near/t_far - OrbitState + orbit_to_pose + update_orbit:球坐标↔相机姿态, 方位角环绕、仰角钳制 ±89°、距离下限 - zoom_to_fit:包围盒自适应距离,相机方向与上轴成 preferred_angle - ViewLayout/LayoutView/compute_view_layout:五种标准布局 附带修复:既有 orientation_cube_face 将 Y 轴当上下、Z 轴当前后, 与工程 Z-up 约定及既有测试(+Z→"top")矛盾,已按 Z-up 修正。 测试:test_viewport_api 22/22 全绿(新增 VDE-399 共 16 例)。
351 lines
12 KiB
C++
351 lines
12 KiB
C++
#pragma once
|
||
/**
|
||
* @file viewport_api.h
|
||
* @brief Viewport / Projection Matrix API (VDE-185)
|
||
*
|
||
* 提供视口相关的几何查询:
|
||
* - bounding_sphere: 模型最小包围球
|
||
* - view_frustum_cull: 视锥体裁剪测试
|
||
*
|
||
* VDE-197: 相机视图矩阵
|
||
* - camera_matrix: 构建 look-at 视图矩阵
|
||
* - orientation_cube_face: 方向→前/后/左/右/上/下
|
||
*
|
||
* VDE-200: 相机插值动画
|
||
* - camera_interpolate: 双位置球面线性插值
|
||
*
|
||
* VDE-202: 场景网格 + 地平面
|
||
* - scene_grid: 生成视口网格线
|
||
* - ground_plane: 计算场景地平面
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/core/point.h"
|
||
#include "vde/core/matrix4d.h"
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::ui {
|
||
|
||
using brep::BrepModel;
|
||
using brep::Color;
|
||
|
||
// ═══════════════════════════════════════════════
|
||
// VDE-185: bounding_sphere / view_frustum_cull
|
||
// ═══════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 包围球数据
|
||
*/
|
||
struct BoundingSphere {
|
||
core::Point3D center{0, 0, 0}; ///< 球心
|
||
double radius = 0.0; ///< 半径
|
||
};
|
||
|
||
/**
|
||
* @brief 计算模型的近似最小包围球
|
||
*/
|
||
[[nodiscard]] BoundingSphere bounding_sphere(const BrepModel& body);
|
||
|
||
/**
|
||
* @brief 视锥体裁剪测试
|
||
*/
|
||
[[nodiscard]] bool view_frustum_cull(const BrepModel& body,
|
||
const core::Matrix4D& view_matrix,
|
||
const core::Matrix4D& proj_matrix);
|
||
|
||
// ═══════════════════════════════════════════════
|
||
// VDE-197: 相机视图矩阵
|
||
// ═══════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 构建 look-at 视图矩阵
|
||
*
|
||
* 构建标准右手坐标系视图矩阵:
|
||
* - 前向量 f = normalize(target - camera_pos)
|
||
* - 右向量 s = normalize(f × up)
|
||
* - 上向量 u = s × f
|
||
*
|
||
* @param body B-Rep 模型(可选,用于默认 target 参考)
|
||
* @param camera_pos 相机位置
|
||
* @param target 观察目标点
|
||
* @param up 上方向向量(默认 (0,0,1))
|
||
* @return 4×4 视图矩阵(世界→相机空间)
|
||
*/
|
||
[[nodiscard]] core::Matrix4D camera_matrix(const BrepModel& body,
|
||
const core::Point3D& camera_pos,
|
||
const core::Point3D& target,
|
||
const core::Vector3D& up = core::Vector3D(0, 0, 1));
|
||
|
||
/**
|
||
* @brief 方向立方体面标签
|
||
*
|
||
* 将方向向量归类到最近的正交面方向(前/后/左/右/上/下)。
|
||
* 用于渲染方向立方体(orientation cube)的当前高亮面。
|
||
*
|
||
* @param dir 方向向量(不必归一化)
|
||
* @return 面标签:"front"/"back"/"left"/"right"/"top"/"bottom"
|
||
*/
|
||
[[nodiscard]] std::string orientation_cube_face(const core::Vector3D& dir);
|
||
|
||
// ═══════════════════════════════════════════════
|
||
// VDE-200: 相机插值动画
|
||
// ═══════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 相机姿态
|
||
*/
|
||
struct CameraPose {
|
||
core::Point3D position{0, 0, 0}; ///< 相机位置
|
||
core::Point3D target{0, 0, 0}; ///< 观察目标
|
||
core::Vector3D up{0, 0, 1}; ///< 上方向
|
||
};
|
||
|
||
/**
|
||
* @brief 相机插值动画
|
||
*
|
||
* 在起始姿态与结束姿态之间进行球面线性插值(位置)+ 线性 target 插值。
|
||
*
|
||
* @param from_pos 起始相机位置
|
||
* @param from_target 起始观察目标
|
||
* @param to_pos 结束相机位置
|
||
* @param to_target 结束观察目标
|
||
* @param t 插值参数 [0,1]
|
||
* @return 插值后的相机姿态
|
||
*/
|
||
[[nodiscard]] CameraPose camera_interpolate(
|
||
const core::Point3D& from_pos,
|
||
const core::Point3D& from_target,
|
||
const core::Point3D& to_pos,
|
||
const core::Point3D& to_target,
|
||
double t);
|
||
|
||
// ═══════════════════════════════════════════════
|
||
// VDE-202: 场景网格 + 地平面
|
||
// ═══════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 网格线数据
|
||
*/
|
||
struct GridLine {
|
||
core::Point3D start{0, 0, 0}; ///< 线段起点
|
||
core::Point3D end{0, 0, 0}; ///< 线段终点
|
||
};
|
||
|
||
/**
|
||
* @brief 场景网格数据
|
||
*/
|
||
struct GridData {
|
||
std::vector<GridLine> lines; ///< 网格线段列表
|
||
Color grid_color{0.5, 0.5, 0.5}; ///< 网格线颜色
|
||
Color axis_color{0.8, 0.0, 0.0}; ///< 坐标轴颜色 (X=红)
|
||
double extent_x = 10.0; ///< X 方向范围
|
||
double extent_y = 10.0; ///< Y 方向范围
|
||
};
|
||
|
||
/**
|
||
* @brief 场景网格生成
|
||
*
|
||
* 在 XZ 平面生成以原点为中心的网格线。
|
||
*
|
||
* @param extents 网格范围(正负范围,如 5.0 生成 [-5,5] 网格)
|
||
* @param spacing 格线间距
|
||
* @return 网格数据
|
||
*/
|
||
[[nodiscard]] GridData scene_grid(double extents, double spacing);
|
||
|
||
/**
|
||
* @brief 地平面数据
|
||
*/
|
||
struct PlaneData {
|
||
core::Point3D origin{0, 0, 0}; ///< 平面上的一点
|
||
core::Vector3D normal{0, 0, 1}; ///< 平面法向量
|
||
double offset = 0.0; ///< 平面偏移(距原点沿法向距离)
|
||
};
|
||
|
||
/**
|
||
* @brief 场景地平面计算
|
||
*
|
||
* 根据场景中所有体的包围盒计算地平面。
|
||
* 地平面为 Y=min_y 的水平面(XZ 平面)。
|
||
*
|
||
* @param bodies 场景中的所有体
|
||
* @return 地平面数据
|
||
*/
|
||
[[nodiscard]] PlaneData ground_plane(const std::vector<BrepModel>& bodies);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-399: 增强视口 API
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 拾取射线
|
||
*
|
||
* 从相机位置通过屏幕坐标 (screen_x, screen_y) 生成世界空间的射线。
|
||
* 用于鼠标拾取 / 选择操作。
|
||
*
|
||
* @param camera 相机姿态(position + target + up)
|
||
* @param screen_x 屏幕 X 坐标(像素,左上角为原点)
|
||
* @param screen_y 屏幕 Y 坐标(像素,左上角为原点)
|
||
* @param viewport_width 视口宽度(像素)
|
||
* @param viewport_height 视口高度(像素)
|
||
* @return 拾取射线数据
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
struct PickRay {
|
||
core::Point3D origin; ///< 射线起点(相机位置)
|
||
core::Vector3D direction; ///< 射线方向(归一化)
|
||
double t_near = 0.0; ///< 近裁剪面参数
|
||
double t_far = 0.0; ///< 远裁剪面参数
|
||
};
|
||
[[nodiscard]] PickRay pick_ray(const CameraPose& camera,
|
||
double screen_x, double screen_y,
|
||
int viewport_width, int viewport_height);
|
||
|
||
/**
|
||
* @brief 屏幕坐标反投影为世界空间拾取射线(原始矩阵版)
|
||
*
|
||
* 使用 view / proj 矩阵的逆将屏幕像素反投影到世界空间:
|
||
* 取 NDC 近平面 (z=-1) 与远平面 (z=+1) 两点,连线即为射线。
|
||
*
|
||
* @param pixel_x 屏幕 X 像素(左上角为原点)
|
||
* @param pixel_y 屏幕 Y 像素(左上角为原点)
|
||
* @param viewport_width 视口宽度(像素)
|
||
* @param viewport_height 视口高度(像素)
|
||
* @param view 视图矩阵(世界→相机空间)
|
||
* @param proj 投影矩阵(相机→裁剪空间)
|
||
* @return 拾取射线(origin 为近平面点,t_far 为近远平面间距)
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
[[nodiscard]] PickRay screen_to_ray(int pixel_x, int pixel_y,
|
||
int viewport_width, int viewport_height,
|
||
const core::Matrix4D& view, const core::Matrix4D& proj);
|
||
|
||
/**
|
||
* @brief 轨道相机状态(动态 tumbling 的可约束参数化)
|
||
*/
|
||
struct OrbitState {
|
||
double azimuth = 0.0; ///< 方位角(弧度,绕上轴水平旋转)
|
||
double elevation = 0.0; ///< 仰角(弧度,update_orbit 钳制到 ±89°)
|
||
double distance = 10.0; ///< 相机到 pivot 的距离
|
||
core::Point3D pivot{0, 0, 0}; ///< 旋转中心
|
||
};
|
||
|
||
/**
|
||
* @brief 轨道状态 → 相机姿态
|
||
*
|
||
* 以 +Z 为上轴,将球坐标 (azimuth, elevation, distance) 转换为世界空间相机姿态。
|
||
*
|
||
* @param orbit 轨道状态
|
||
* @return 对应的相机姿态
|
||
*/
|
||
[[nodiscard]] CameraPose orbit_to_pose(const OrbitState& orbit);
|
||
|
||
/**
|
||
* @brief 更新轨道状态(鼠标拖拽 / 滚轮)
|
||
*
|
||
* 累加增量:方位角环绕到 (-π, π],仰角钳制到 [-89°, 89°],
|
||
* 距离下限 1e-6(防止翻转/穿模)。
|
||
*
|
||
* @param current 当前轨道状态
|
||
* @param d_azimuth 方位角增量(弧度)
|
||
* @param d_elevation 仰角增量(弧度)
|
||
* @param d_distance 距离增量
|
||
* @return 更新后的轨道状态
|
||
*/
|
||
[[nodiscard]] OrbitState update_orbit(const OrbitState& current,
|
||
double d_azimuth, double d_elevation, double d_distance);
|
||
|
||
/**
|
||
* @brief 缩放到合适视图(zoom-to-fit)
|
||
*
|
||
* 根据模型包围盒计算相机位置与距离,使整个模型连同 padding 边距可见。
|
||
*
|
||
* @param body 目标模型
|
||
* @param padding_factor 额外边距系数(>1 表示更远)
|
||
* @param up 上方向(默认 +Z)
|
||
* @param preferred_angle 相机方向与上轴的夹角(度,默认 30°)
|
||
* @return 相机姿态
|
||
*/
|
||
[[nodiscard]] CameraPose zoom_to_fit(const BrepModel& body,
|
||
double padding_factor = 1.2,
|
||
const core::Vector3D& up = core::Vector3D(0, 0, 1),
|
||
double preferred_angle = 30.0);
|
||
|
||
/**
|
||
* @brief 多视图布局类型
|
||
*/
|
||
enum class ViewLayout {
|
||
Single, ///< 单视图(ISO)
|
||
TwoHorizontal, ///< 左右两视图
|
||
TwoVertical, ///< 上下两视图
|
||
FourQuad, ///< 四宫格
|
||
ThreeStandard ///< 标准三视图(前/上/右)
|
||
};
|
||
|
||
/**
|
||
* @brief 布局中的单个视图
|
||
*/
|
||
struct LayoutView {
|
||
std::string name; ///< 视图名("Top"/"Front"/"Right"/"Iso")
|
||
core::Matrix4D view_matrix; ///< 视图矩阵(世界→相机空间)
|
||
bool is_active = false; ///< 是否为当前活动视图
|
||
};
|
||
|
||
/**
|
||
* @brief 计算标准多视图布局
|
||
*
|
||
* 为指定布局生成各视图的名称与视图矩阵(相机位置按包围盒自适应)。
|
||
* 默认第一个视图为活动视图。
|
||
*
|
||
* @param layout 布局类型
|
||
* @param body 目标模型(用于自适应相机距离)
|
||
* @return 各视图列表
|
||
*/
|
||
[[nodiscard]] std::vector<LayoutView> compute_view_layout(
|
||
ViewLayout layout, const BrepModel& body);
|
||
|
||
/**
|
||
* @brief 动态旋转(Tumbling)
|
||
*
|
||
* 根据鼠标拖拽增量 (delta_x, delta_y) 绕目标点旋转相机。
|
||
* delta_x 绕上轴旋转,delta_y 绕右轴旋转。
|
||
*
|
||
* @param camera 当前相机姿态
|
||
* @param delta_x 水平拖拽增量(像素→角度)
|
||
* @param delta_y 垂直拖拽增量(像素→角度)
|
||
* @return 旋转后的相机姿态
|
||
*
|
||
* @note 旋转角度 = pixel_delta * 0.005 弧度(约 0.3°/像素)
|
||
* @ingroup brep
|
||
*/
|
||
[[nodiscard]] CameraPose dynamic_tumbling(const CameraPose& camera,
|
||
double delta_x, double delta_y);
|
||
|
||
/**
|
||
* @brief 视口→世界坐标变换
|
||
*
|
||
* 将屏幕坐标点反投影到世界空间。
|
||
* 假设深度 = 0(近裁剪面),返回点在相机坐标系中的对应位置。
|
||
*
|
||
* @param camera 相机姿态
|
||
* @param screen_point 屏幕坐标点 (x, y, depth)
|
||
* depth ∈ [0,1], 0=近裁剪面
|
||
* @param viewport_width 视口宽度
|
||
* @param viewport_height 视口高度
|
||
* @param near_plane 近裁剪面距离(默认 0.1)
|
||
* @param far_plane 远裁剪面距离(默认 100.0)
|
||
* @return 对应的世界空间点
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
[[nodiscard]] core::Point3D viewport_to_world(const CameraPose& camera,
|
||
const core::Point3D& screen_point,
|
||
int viewport_width, int viewport_height,
|
||
double near_plane = 0.1, double far_plane = 100.0);
|
||
|
||
} // namespace vde::ui
|