Files
ViewDesignEngine/docs/reference/api-reference-core.md
T

16 KiB
Raw Blame History

Core 模块 API 参考

核心几何模块 — 矩阵变换、基础图元、空间计算、几何谓词

模块概述

core 模块是 ViewDesignEngine 的基础层,提供所有上层模块依赖的几何类型与数学运算,包含 19 个头文件。表达式求解见 vde_expression,CAM 加工见 vde_cam,平台抽象见 vde_platform(均为独立模块,详见下文"已拆分模块")。


头文件列表

头文件 说明
matrix4d.h 4×4 变换矩阵(平移/旋转/缩放/镜像)
transform.h Eigen::Transform3D 封装(translate/rotate/scale/mirror)
point.h 点/向量类型重新导出
aabb.h 轴对齐包围盒
triangle.h 三维三角形(面积/法线/包含测试)
polygon.h 二维多边形(面积/三角剖分/凸包)
plane.h 三维平面(Hessian 形式)
line.h 三维直线/线段/射线
distance.h 几何距离计算 + 最近点查询
convex_hull.h 2D/3D 凸包(Graham Scan / QuickHull)
voronoi.h 二维 Voronoi 图(含 3D/加权/质心变体)
icp.h 迭代最近点(ICP)点云配准
version.h 版本信息(编译时/运行时)
exact_predicates.h 精确几何谓词
object_pool.h 对象池(内存管理)
topology_compression.h 拓扑压缩
transaction.h 事务管理
undo_stack.h 撤销/重做栈
simd_vector.h SIMD 向量加速

注:原 core 文档中的以下头文件已随模块拆分迁移至独立模块(详见各模块文档):

  • platform.h → vde_platform(命名空间 vde::platform)
  • expression_eval.h → vde_expression(命名空间 vde::expression)
  • cam_toolpath.h / cam_mesh.h / cam_strategies.h / cam_5axis.h / cam_advanced.h / cam_optimization.h / gcode_import.h → vde_cam(命名空间 vde::cam)
  • performance_tuning.h / concurrent_data.h → vde_infra

详细 API

matrix4d.h — 4×4 变换矩阵

namespace vde::core {

struct Matrix4D {
    double m00=1, m01=0, m02=0, m03=0;
    double m10=0, m11=1, m12=0, m13=0;
    double m20=0, m21=0, m22=1, m23=0;
    double m30=0, m31=0, m32=0, m33=1;

    static Matrix4D Identity();                                          // 单位矩阵
    static Matrix4D Translation(double x, double y, double z);          // 平移变换
    static Matrix4D Translation(const Vector3D& v);                     // 平移变换(向量)
    static Matrix4D RotationX(double angle_rad);                        // 绕 X 轴旋转
    static Matrix4D RotationY(double angle_rad);                        // 绕 Y 轴旋转
    static Matrix4D RotationZ(double angle_rad);                        // 绕 Z 轴旋转
    static Matrix4D Scale(double sx, double sy, double sz);             // 非均匀缩放
    static Matrix4D Mirror(const Point3D& plane_point,                   // 镜面反射
                           const Vector3D& plane_normal);

    double& at(int row, int col);                                       // 元素访问(读写)
    double  at(int row, int col) const;                                 // 元素访问(只读)
    double* data();                                                     // 原始数据指针
    const double* data() const;                                         // 原始数据指针(只读)

    Matrix4D operator*(const Matrix4D& rhs) const;                      // 矩阵乘法
    Point3D  transform_point(const Point3D& p) const;                   // 变换点(含平移)
    Vector3D transform_vector(const Vector3D& v) const;                 // 变换向量(仅旋转缩放)
    Matrix4D inverse() const;                                           // 逆矩阵
};

} // namespace vde::core

已拆分模块 — 平台 / 表达式

以下功能原属 core,现已拆分为独立模块:

原章节 现归属
platform.h include/vde_platform/platform.h,命名空间 vde::platform
expression_eval.h include/vde_expression/expression_eval.h,命名空间 vde::expression

transform.h — 仿射变换

namespace vde::core {

using Transform3D = Eigen::Transform<double, 3, Eigen::Affine>;  // 3D 仿射变换类型

Transform3D translate(const Vector3D& v);           // 平移变换(向量)
Transform3D translate(double x, double y, double z);// 平移变换(分量)
Transform3D rotate(const Vector3D& axis, double angle_rad);  // 轴-角旋转
Transform3D rotate_x(double rad);                   // 绕 X 轴旋转
Transform3D rotate_y(double rad);                   // 绕 Y 轴旋转
Transform3D rotate_z(double rad);                   // 绕 Z 轴旋转
Transform3D scale(double sx, double sy, double sz); // 非均匀缩放
Transform3D scale(double s);                        // 均匀缩放
Transform3D mirror(const Vector3D& normal);         // 过原点平面反射
Transform3D mirror_about_plane(const Plane3D& plane);// 任意平面反射

} // namespace vde::core

point.h — 点/向量类型

namespace vde::core {

template <typename T, size_t Dim>
using Point = foundation::Point<T, Dim>;            // 通用 N 维点

using Point2D = foundation::Point2D;                // double 二维点
using Point3D = foundation::Point3D;                // double 三维点
using Point2f = foundation::Point2f;                // float 二维点
using Point3f = foundation::Point3f;                // float 三维点

template <typename T, size_t Dim>
using Vector = foundation::Vector<T, Dim>;          // 通用 N 维向量

using Vector2D = foundation::Vector2D;              // double 二维向量
using Vector3D = foundation::Vector3D;              // double 三维向量

} // namespace vde::core

aabb.h — 轴对齐包围盒

namespace vde::core {

template <typename T>
class AABB {
public:
    AABB();                                         // 空包围盒
    AABB(const Point3D& min, const Point3D& max);   // 从角点构造

    void expand(const Point3D& p);                  // 扩展包含点
    void expand(const AABB& other);                 // 扩展包含另一包围盒

    [[nodiscard]] Point3D  center() const;          // 中心点
    [[nodiscard]] Vector3D extent() const;          // 对角线向量
    [[nodiscard]] T        surface_area() const;    // 表面积
    [[nodiscard]] T        volume() const;          // 体积
    [[nodiscard]] bool     contains(const Point3D& p) const;    // 点包含测试
    [[nodiscard]] bool     intersects(const AABB& other) const; // 相交测试

    [[nodiscard]] const Point3D& min() const;       // 最小角点
    [[nodiscard]] const Point3D& max() const;       // 最大角点
};

using AABB3D = AABB<double>;                        // 双精度 AABB
using AABB3f = AABB<float>;                          // 单精度 AABB

} // namespace vde::core

triangle.h — 三角形

namespace vde::core {

template <typename T>
class Triangle {
public:
    Triangle();                                     // 默认构造(退化)
    Triangle(const Point3D& v0, const Point3D& v1, const Point3D& v2);

    [[nodiscard]] const Point3D& v(int i) const;    // 第 i 个顶点
    [[nodiscard]] const std::array<Point3D, 3>& vertices() const;
    [[nodiscard]] Vector3D normal() const;          // 单位法向量
    [[nodiscard]] T        area() const;            // 面积
    [[nodiscard]] Point3D  centroid() const;        // 质心
    [[nodiscard]] bool     contains(const Point3D& p) const; // 包含测试(投影到平面)

    int id = -1;                                    // 用户索引
};

using Triangle3D = Triangle<double>;
using Triangle3f = Triangle<float>;

} // namespace vde::core

polygon.h — 二维多边形

namespace vde::core {

class Polygon2D {
public:
    Polygon2D();
    explicit Polygon2D(std::vector<Point2D> vertices);

    [[nodiscard]] const std::vector<Point2D>& vertices() const;
    [[nodiscard]] size_t size() const;
    [[nodiscard]] bool   empty() const;

    [[nodiscard]] double  signed_area() const;      // 有向面积(CCW > 0)
    [[nodiscard]] double  area() const;             // 绝对面积
    [[nodiscard]] double  perimeter() const;        // 周长
    [[nodiscard]] Point2D centroid() const;         // 质心
    [[nodiscard]] AABB<double> bounding_box() const;// 包围盒
    [[nodiscard]] bool    contains(const Point2D& p) const; // 射线法包含测试
    [[nodiscard]] bool    is_ccw() const;           // 是否逆时针
    [[nodiscard]] bool    is_convex() const;        // 是否凸多边形

    [[nodiscard]] Polygon2D offset(double distance) const;        // 边界偏移
    [[nodiscard]] Polygon2D simplify(double tolerance) const;     // Douglas-Peucker 简化
    [[nodiscard]] std::vector<std::array<int, 3>> triangulate() const; // 耳切法三角剖分

    static Polygon2D convex_hull_2d(const std::vector<Point2D>& points); // Andrew 凸包
};

} // namespace vde::core

plane.h — 平面

namespace vde::core {

template <typename T>
class Plane {
public:
    Plane();                                        // 默认 XY 平面
    Plane(const Point3D& point, const Vector3D& normal); // 点和法线
    Plane(const Point3D& a, const Point3D& b, const Point3D& c); // 三点

    [[nodiscard]] const Vector3D& normal() const;   // 单位法向量
    [[nodiscard]] T d() const;                      // 平面方程常数
    [[nodiscard]] T signed_distance(const Point3D& p) const; // 有向距离
    [[nodiscard]] T distance(const Point3D& p) const;        // 绝对距离
    [[nodiscard]] Point3D project(const Point3D& p) const;   // 正交投影
};

using Plane3D = Plane<double>;

} // namespace vde::core

line.h — 直线/线段/射线

namespace vde::core {

template <typename T>
class Line3D {                                      // 无限直线
public:
    Line3D();
    Line3D(const Point3D& origin, const Vector3D& direction);
    [[nodiscard]] const Point3D& origin() const;
    [[nodiscard]] const Vector3D& direction() const;
    [[nodiscard]] Point3D point_at(T t) const;      // 参数求点
};

template <typename T>
class Segment3D {                                   // 线段
public:
    Segment3D();
    Segment3D(const Point3D& p0, const Point3D& p1);
    [[nodiscard]] const Point3D& p0() const;
    [[nodiscard]] const Point3D& p1() const;
    [[nodiscard]] Vector3D direction() const;       // 方向向量
    [[nodiscard]] T length() const;                 // 长度
};

template <typename T>
class Ray3D {                                       // 射线 (t ≥ 0)
public:
    Ray3D();
    Ray3D(const Point3D& origin, const Vector3D& direction);
    [[nodiscard]] const Point3D& origin() const;
    [[nodiscard]] const Vector3D& direction() const;
    [[nodiscard]] Point3D point_at(T t) const;
};

using Line3Dd    = Line3D<double>;
using Segment3Dd = Segment3D<double>;
using Ray3Dd     = Ray3D<double>;

} // namespace vde::core

distance.h — 距离计算与最近点

namespace vde::core {

// 点到几何体的距离
double distance(const Point3D& a, const Point3D& b);                        // 点-点
double distance(const Point3D& p, const Line3D<double>& line);              // 点-直线
double distance(const Point3D& p, const Segment3D<double>& seg);            // 点-线段
double distance(const Point3D& p, const Plane<double>& plane);              // 点-平面
double distance(const Point3D& p, const Triangle<double>& tri);             // 点-三角形
double distance(const Point3D& p, const AABB<double>& box);                 // 点-AABB

// 几何体之间的最短距离
double distance(const Segment3D<double>& a, const Segment3D<double>& b);    // 线段-线段
double distance(const Line3D<double>& a, const Line3D<double>& b);          // 直线-直线
double distance(const AABB<double>& a, const AABB<double>& b);              // AABB-AABB
double distance(const Triangle<double>& a, const Triangle<double>& b);      // 三角形-三角形

// 最近点查询
Point3D closest_point(const Point3D& p, const Triangle<double>& tri);
Point3D closest_point(const Point3D& p, const Segment3D<double>& seg);
Point3D closest_point(const Point3D& p, const AABB<double>& box);
Point3D closest_point(const Point3D& p, const Plane<double>& plane);

} // namespace vde::core

convex_hull.h — 凸包

namespace vde::core {

std::vector<Point2D> convex_hull_2d(const std::vector<Point2D>& points);  // Graham Scan
std::vector<std::array<Point3D, 3>> convex_hull_3d(const std::vector<Point3D>& points); // QuickHull

} // namespace vde::core

voronoi.h — Voronoi 图

namespace vde::core {

struct VoronoiCell {
    Point2D site;
    std::vector<Point2D> vertices;
    std::vector<std::array<int, 2>> edges;
};

std::vector<VoronoiCell> voronoi_2d(const std::vector<Point2D>& points); // Delaunay 对偶

} // namespace vde::core

icp.h — 迭代最近点配准

namespace vde::core {

struct ICPResult {
    Transform3D transform;                          // 对齐变换
    double rms_error;                               // 均方根误差
    int iterations;                                 // 实际迭代次数
    bool converged;                                 // 是否收敛
};

[[nodiscard]] ICPResult icp_register(
    const std::vector<Point3D>& source,
    const std::vector<Point3D>& target,
    int max_iter = 50,
    double tolerance = 1e-6);

} // namespace vde::core

已拆分模块 — CAM

CAM 相关功能已拆分为独立模块 vde_cam(命名空间 vde::cam),头文件位于 include/vde_cam/:cam_toolpath.h / cam_mesh.h / cam_strategies.h / cam_5axis.h / cam_advanced.h / cam_optimization.h / gcode_import.h。

version.h — 版本信息

namespace vde::core {

#define VDE_VERSION_MAJOR  1
#define VDE_VERSION_MINOR  0
#define VDE_VERSION_PATCH  1
#define VDE_VERSION_STRING "1.0.1"

struct VersionInfo {
    int major, minor, patch;
    std::string version_string, build_type, build_timestamp, compiler_info, cpp_standard;
};

VersionInfo vde_version();
std::string vde_version_string();
int vde_version_compare(int lhs_major, int lhs_minor, int lhs_patch,
                         int rhs_major, int rhs_minor, int rhs_patch);

} // namespace vde::core

代码示例

示例 1: 矩阵链式变换

#include <vde/core/matrix4d.h>
#include <vde/core/transform.h>

using namespace vde::core;

// 先平移再旋转
auto T = Matrix4D::Translation(10, 0, 0);       // 沿 X 平移 10
auto R = Matrix4D::RotationZ(M_PI / 4);          // 绕 Z 旋转 45°
auto M = T * R;                                  // 复合变换

Point3D p(1, 2, 3);
Point3D result = M.transform_point(p);           // 变换后的点

// 逆变换
Matrix4D inv = M.inverse();
Point3D back = inv.transform_point(result);       // 回到原始位置

示例 2: CAM 刀路生成(CAM 属 vde_cam 模块)

#include <vde_cam/cam_toolpath.h>
#include <vde_cam/cam_strategies.h>
#include <vde/brep/modeling.h>

using namespace vde;

// 创建工件
auto body = brep::make_box(100, 50, 20);

// 定义刀具
cam::Tool tool;
tool.type = cam::ToolType::ENDMILL;
tool.diameter = 10.0;
tool.name = "D10";

// 粗加工
cam::RoughingParams rough;
rough.step_down = 2.0;
rough.step_over = 6.0;
rough.stock_to_leave = 0.5;

auto rough_tp = cam::roughing_toolpath(body, tool, rough);

// 精加工
cam::FinishingParams finish;
finish.step_over = 0.3;

auto finish_tp = cam::finishing_toolpath(body, tool, finish, cam::FinishingStrategy::SPIRAL);

// 导出 G-code
std::string gcode = cam::export_gcode(finish_tp);