2026-03-18 17:49:22 +08:00
|
|
|
# Math 模块概览
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Math`
|
|
|
|
|
|
|
|
|
|
**类型**: `module`
|
|
|
|
|
|
|
|
|
|
**描述**: XCEngine 的数学库模块,提供图形引擎常用的数学类型和函数。
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
Math 模块提供了一套完整的图形数学库,包括向量、矩阵、四元数、变换和几何类型等。
|
|
|
|
|
|
|
|
|
|
## 模块内容
|
|
|
|
|
|
|
|
|
|
### 向量类型
|
|
|
|
|
|
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|
|
|
|------|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
| [Vector2](vector2/vector2.md) | `Vector2.h` | 二维向量 |
|
|
|
|
|
| [Vector3](vector3/vector3.md) | `Vector3.h` | 三维向量 |
|
|
|
|
|
| [Vector4](vector4/vector4.md) | `Vector4.h` | 四维向量 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
### 矩阵类型
|
|
|
|
|
|
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|
|
|
|------|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
| [Matrix3](matrix3/matrix3.md) | `Matrix3.h` | 3x3 矩阵 |
|
|
|
|
|
| [Matrix4](matrix4/matrix4.md) | `Matrix4.h` | 4x4 矩阵 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
### 旋转/变换
|
|
|
|
|
|
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|
|
|
|------|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
| [Quaternion](quaternion/quaternion.md) | `Quaternion.h` | 四元数 |
|
|
|
|
|
| [Transform](transform/transform.md) | `Transform.h` | 变换组件 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
### 几何类型
|
|
|
|
|
|
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|
|
|
|------|------|------|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
| [Color](color/color.md) | `Color.h` | 颜色 |
|
|
|
|
|
| [Ray](ray/ray.md) | `Ray.h` | 射线 |
|
|
|
|
|
| [Plane](plane/plane.md) | `Plane.h` | 平面 |
|
|
|
|
|
| [Sphere](sphere/sphere.md) | `Sphere.h` | 球体 |
|
|
|
|
|
| [Box](box/box.md) | `Box.h` | 盒子 |
|
|
|
|
|
| [Bounds](bounds/bounds.md) | `Bounds.h` | 包围盒 |
|
|
|
|
|
| [AABB](aabb/aabb.md) | `AABB.h` | 轴对齐包围盒 |
|
|
|
|
|
| [Frustum](frustum/frustum.md) | `Frustum.h` | 视锥体 |
|
|
|
|
|
| [Rect](rect/rect.md) | `Rect.h` | 二维矩形 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
## 常量定义
|
|
|
|
|
|
|
|
|
|
| 常量 | 值 | 描述 |
|
|
|
|
|
|------|-----|------|
|
|
|
|
|
| `PI` | 3.14159265358979323846f | 圆周率 |
|
|
|
|
|
| `TWO_PI` | 6.28318530717958647692f | 2π |
|
|
|
|
|
| `HALF_PI` | 1.57079632679489661923f | π/2 |
|
|
|
|
|
| `DEG_TO_RAD` | PI / 180.0f | 度到弧度 |
|
|
|
|
|
| `RAD_TO_DEG` | 180.0f / PI | 弧度到度 |
|
|
|
|
|
| `EPSILON` | 1e-6f | 浮点精度 |
|
|
|
|
|
| `FLOAT_MAX` | 3.402823466e+38f | 浮点最大值 |
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
详细文档: [Math.h - 常量和辅助函数](h/h.md)
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
## 辅助函数
|
|
|
|
|
|
|
|
|
|
| 函数 | 描述 |
|
|
|
|
|
|------|------|
|
|
|
|
|
| `Radians(float degrees)` | 度转弧度 |
|
|
|
|
|
| `Degrees(float radians)` | 弧度转度 |
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#include <XCEngine/Math/Math.h>
|
|
|
|
|
#include <XCEngine/Math/Vector3.h>
|
|
|
|
|
#include <XCEngine/Math/Matrix4.h>
|
|
|
|
|
#include <XCEngine/Math/Quaternion.h>
|
|
|
|
|
|
|
|
|
|
using namespace XCEngine::Math;
|
|
|
|
|
|
|
|
|
|
// 向量运算
|
|
|
|
|
Vector3 a(1.0f, 0.0f, 0.0f);
|
|
|
|
|
Vector3 b(0.0f, 1.0f, 0.0f);
|
|
|
|
|
float dot = Vector3::Dot(a, b);
|
|
|
|
|
Vector3 cross = Vector3::Cross(a, b);
|
|
|
|
|
Vector3 normalized = Vector3::Normalize(a);
|
|
|
|
|
|
|
|
|
|
// 矩阵运算
|
|
|
|
|
Matrix4 model = Matrix4::TRS(position, rotation, scale);
|
|
|
|
|
Matrix4 view = Matrix4::LookAt(eye, target, up);
|
|
|
|
|
Matrix4 projection = Matrix4::Perspective(fov, aspect, near, far);
|
|
|
|
|
Matrix4 mvp = projection * view * model;
|
|
|
|
|
|
|
|
|
|
// 四元数运算
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
Quaternion q1 = Quaternion::FromEulerAngles(0, 90 * DEG_TO_RAD, 0);
|
2026-03-18 17:49:22 +08:00
|
|
|
Quaternion q2 = Quaternion::FromAxisAngle(Vector3::Up(), Math::Radians(45.0f));
|
|
|
|
|
Quaternion combined = q1 * q2;
|
|
|
|
|
Vector3 rotated = q2 * Vector3::Forward();
|
|
|
|
|
```
|