|
ViewDesignEngine
3.1.0
高性能 CAD 计算几何引擎
|
类 | |
| struct | GJKResult |
| GJK 完整检测结果 更多... | |
| struct | RayTriResult |
| 射线-三角形求交结果 更多... | |
| struct | RaySphereResult |
| 射线-球体求交结果 更多... | |
| struct | TriTriIntersection |
| 三角形-三角形相交线段 更多... | |
类型定义 | |
| using | SupportFunc = std::function< Point3D(const Vector3D &)> |
| 支撑函数类型 更多... | |
函数 | |
| bool | gjk_intersect (const SupportFunc &shape_a, const SupportFunc &shape_b) |
| GJK 相交检测(仅判断是否碰撞) 更多... | |
| double | gjk_distance (const SupportFunc &shape_a, const SupportFunc &shape_b) |
| GJK 最近距离查询 更多... | |
| GJKResult | gjk_full (const SupportFunc &shape_a, const SupportFunc &shape_b) |
| GJK 完整检测:碰撞状态、距离、最近点对 更多... | |
| std::optional< RayTriResult > | ray_triangle_intersect (const Point3D &origin, const Vector3D &dir, const Triangle3D &tri) |
| 射线-三角形求交(Möller-Trumbore 算法) 更多... | |
| std::optional< RayTriResult > | ray_triangle_intersect (const Ray3Dd &ray, const Triangle3D &tri) |
| 射线-三角形求交(Ray3Dd 重载) 更多... | |
| bool | ray_aabb_intersect (const Ray3Dd &ray, const AABB3D &box, double &tmin_out, double &tmax_out) |
| 射线-AABB 求交(slab 方法) 更多... | |
| std::optional< RaySphereResult > | ray_sphere_intersect (const Point3D &origin, const Vector3D &dir, const Point3D ¢er, double radius) |
| 射线-球体求交(解析法) 更多... | |
| std::optional< RaySphereResult > | ray_sphere_intersect (const Ray3Dd &ray, const Point3D ¢er, double radius) |
| 射线-球体求交(Ray3Dd 重载) 更多... | |
| std::optional< double > | ray_plane_intersect (const Point3D &origin, const Vector3D &dir, const Point3D &plane_point, const Vector3D &plane_normal) |
| 射线-平面求交 更多... | |
| std::optional< double > | ray_plane_intersect (const Ray3Dd &ray, const Point3D &plane_point, const Vector3D &plane_normal) |
| 射线-平面求交(Ray3Dd 重载) 更多... | |
| std::optional< RayTriResult > | ray_mesh_intersect (const Point3D &origin, const Vector3D &dir, const std::vector< Triangle3D > &triangles) |
| 射线-网格求交(暴力遍历) 更多... | |
| std::optional< RayTriResult > | ray_mesh_intersect (const Ray3Dd &ray, const std::vector< Triangle3D > &triangles) |
| 射线-网格求交(Ray3Dd 重载) 更多... | |
| bool | sat_intersect (const std::vector< Point3D > &verts_a, const std::vector< std::array< int, 3 >> &faces_a, const std::vector< Point3D > &verts_b, const std::vector< std::array< int, 3 >> &faces_b) |
| SAT 凸多面体相交检测 更多... | |
| bool | tri_tri_intersect (const Triangle3D &t1, const Triangle3D &t2) |
| 三角形-三角形相交检测(布尔测试) 更多... | |
| TriTriIntersection | tri_tri_intersect_detailed (const Triangle3D &t1, const Triangle3D &t2) |
| 三角形-三角形详细相交检测 更多... | |
| bool | tri_aabb_overlap (const Triangle3D &tri, const AABB3D &box) |
| 三角形-AABB 快速相交测试 更多... | |
| using vde::collision::SupportFunc = typedef std::function<Point3D(const Vector3D&)> |
支撑函数类型
给定方向向量 d,返回形状在该方向上最远点的坐标:
支撑函数是 GJK / EPA 算法的核心抽象。 只要实现了支撑函数,即可用于任意凸形状的碰撞检测。
| double vde::collision::gjk_distance | ( | const SupportFunc & | shape_a, |
| const SupportFunc & | shape_b | ||
| ) |
GJK 最近距离查询
在 gjk_intersect 基础上,不相交时额外调用 EPA (Expanding Polytope Algorithm) 计算两形状之间的精确最近距离。
| shape_a | 形状 A 的支撑函数 |
| shape_b | 形状 B 的支撑函数 |
| GJKResult vde::collision::gjk_full | ( | const SupportFunc & | shape_a, |
| const SupportFunc & | shape_b | ||
| ) |
GJK 完整检测:碰撞状态、距离、最近点对
融合 gjk_intersect 和 gjk_distance,一次调用返回 碰撞状态、最近距离及两形状上的最近点。
| shape_a | 形状 A 的支撑函数 |
| shape_b | 形状 B 的支撑函数 |
| bool vde::collision::gjk_intersect | ( | const SupportFunc & | shape_a, |
| const SupportFunc & | shape_b | ||
| ) |
GJK 相交检测(仅判断是否碰撞)
构造 Minkowski 差 A − B,在原点附近迭代构建单纯形。 若原点在 Minkowski 差内 → 碰撞。
算法流程:
时间复杂度 O(n),其中 n 为迭代次数(通常 < 20 对于紧凑形状)。
| shape_a | 形状 A 的支撑函数 |
| shape_b | 形状 B 的支撑函数 |
| bool vde::collision::ray_aabb_intersect | ( | const Ray3Dd & | ray, |
| const AABB3D & | box, | ||
| double & | tmin_out, | ||
| double & | tmax_out | ||
| ) |
射线-AABB 求交(slab 方法)
分别检测射线与三个坐标轴方向 slab(平板块)的交点, 取最大 t_min 和最小 t_max。若 t_min ≤ t_max 且 t_max ≥ 0,则命中。
复杂度 O(1)。
| ray | 射线 |
| box | 轴对齐包围盒 |
| tmin_out | [输出] 进入包围盒的参数 t |
| tmax_out | [输出] 离开包围盒的参数 t |
| std::optional<RayTriResult> vde::collision::ray_mesh_intersect | ( | const Point3D & | origin, |
| const Vector3D & | dir, | ||
| const std::vector< Triangle3D > & | triangles | ||
| ) |
射线-网格求交(暴力遍历)
遍历所有三角形,对每个三角形调用 ray_triangle_intersect, 跟踪最小 t 值返回最近命中。
复杂度 O(n),n 为三角形数量。
| origin | 射线起点 |
| dir | 射线方向 |
| triangles | 三角形列表 |
被这些函数引用 ray_mesh_intersect().
|
inline |
射线-网格求交(Ray3Dd 重载)
| ray | 射线 |
| triangles | 三角形列表 |
在文件 ray_intersect.h 第 210 行定义.
引用了 ray_mesh_intersect().
| std::optional<double> vde::collision::ray_plane_intersect | ( | const Point3D & | origin, |
| const Vector3D & | dir, | ||
| const Point3D & | plane_point, | ||
| const Vector3D & | plane_normal | ||
| ) |
射线-平面求交
解 t = dot(plane_point − origin, plane_normal) / dot(dir, plane_normal)。 若分母 ≈ 0(射线平行于平面)→ 无交点。
| origin | 射线起点 |
| dir | 射线方向 |
| plane_point | 平面上一点 |
| plane_normal | 平面法线(需归一化以获得正确 t) |
被这些函数引用 ray_plane_intersect().
|
inline |
射线-平面求交(Ray3Dd 重载)
| ray | 射线 |
| plane_point | 平面上一点 |
| plane_normal | 平面法线 |
在文件 ray_intersect.h 第 170 行定义.
| std::optional<RaySphereResult> vde::collision::ray_sphere_intersect | ( | const Point3D & | origin, |
| const Vector3D & | dir, | ||
| const Point3D & | center, | ||
| double | radius | ||
| ) |
射线-球体求交(解析法)
解二次方程 |origin + t*dir − center|² = r²。 通过判别式 Δ = b² − 4ac 判断:
复杂度 O(1)。
| origin | 射线起点 |
| dir | 射线方向 |
| center | 球心 |
| radius | 球半径 |
被这些函数引用 ray_sphere_intersect().
|
inline |
射线-球体求交(Ray3Dd 重载)
| ray | 射线 |
| center | 球心 |
| radius | 球半径 |
在文件 ray_intersect.h 第 141 行定义.
| std::optional<RayTriResult> vde::collision::ray_triangle_intersect | ( | const Point3D & | origin, |
| const Vector3D & | dir, | ||
| const Triangle3D & | tri | ||
| ) |
射线-三角形求交(Möller-Trumbore 算法)
标准 MT 算法,直接计算重心坐标 u, v 和参数 t。 不预先计算平面方程,一步到位检测。
算法:
复杂度 O(1)。
| origin | 射线起点 |
| dir | 射线方向(需归一化以获得正确 t 值) |
| tri | 三角形 |
被这些函数引用 ray_triangle_intersect().
|
inline |
射线-三角形求交(Ray3Dd 重载)
| ray | 射线 |
| tri | 三角形 |
在文件 ray_intersect.h 第 76 行定义.
| bool vde::collision::sat_intersect | ( | const std::vector< Point3D > & | verts_a, |
| const std::vector< std::array< int, 3 >> & | faces_a, | ||
| const std::vector< Point3D > & | verts_b, | ||
| const std::vector< std::array< int, 3 >> & | faces_b | ||
| ) |
SAT 凸多面体相交检测
分离轴定理:两个凸多面体不相交,当且仅当存在一条分离轴, 使得二者在该轴上的投影区间不重叠。
待检测的分离轴集合:
对每条候选轴,计算两个多面体投影区间的最小/最大值。 若某轴上投影区间无重叠 → 不相交(早停)。 所有轴都重叠 → 相交。
最坏时间复杂度 O(n_a · n_b),其中 n_a, n_b 为面数。 对于凸多面体通常在遍历部分轴后即早停。
| verts_a | 多面体 A 的顶点数组 |
| faces_a | 多面体 A 的面索引数组(每个面为 {v0, v1, v2}) |
| verts_b | 多面体 B 的顶点数组 |
| faces_b | 多面体 B 的面索引数组 |
| bool vde::collision::tri_aabb_overlap | ( | const Triangle3D & | tri, |
| const AABB3D & | box | ||
| ) |
三角形-AABB 快速相交测试
使用分离轴定理在三角形和轴对齐包围盒之间检测。 候选分离轴包括(共 13 轴):
复杂度 O(1)。
| tri | 三角形 |
| box | 轴对齐包围盒 |
| bool vde::collision::tri_tri_intersect | ( | const Triangle3D & | t1, |
| const Triangle3D & | t2 | ||
| ) |
三角形-三角形相交检测(布尔测试)
使用分离轴定理在两组三角形之间检测相交。 候选分离轴包括:
若存在一条轴使得两三角形投影区间不重叠 → 不相交。
复杂度 O(1)(固定 11+ 轴检测)。
| t1 | 三角形 1 |
| t2 | 三角形 2 |
| TriTriIntersection vde::collision::tri_tri_intersect_detailed | ( | const Triangle3D & | t1, |
| const Triangle3D & | t2 | ||
| ) |
三角形-三角形详细相交检测
返回两三角形相交的交点线段(两个端点)。 适用于需要精确接触几何的场景,如:
算法:
| t1 | 三角形 1 |
| t2 | 三角形 2 |