数学类型(Math Types)¶
SDK 内置了一套轻量的数学层,用于运行时与 gRPC API 的数据表达:
Vector3/Quaternion:3D 向量与旋转(由pyglm提供)Transform/Pose/AABB:常用空间数据的便捷封装- 一组常用的几何与转换工具函数
单位
TongSIM Lite 采用 Unreal Engine 约定:位置单位为 厘米(UU)。
关键类型¶
| 类型 | 含义 | 常见用途 |
|---|---|---|
Vector3 |
三维向量 | 位置、范围、方向 |
Quaternion |
旋转 | 相机/角色朝向 |
Transform |
位置 + 旋转 + 缩放 | gRPC Transform、坐标变换 |
Pose |
位置 + 旋转 | 简化 pose 传递 |
AABB |
轴对齐包围盒 | Python 侧的包围/包含判断 |
常用工具函数¶
dot,cross,normalize,length,lerpdegrees_to_radians,radians_to_degreeseuler_to_quaternion,quaternion_to_eulercalc_camera_look_at_rotation
API References¶
tongsim.math.geometry.type.Pose ¶
A lightweight container that groups location and rotation as a pose.
Source code in src/tongsim/math/geometry/type.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
tongsim.math.geometry.type.Transform ¶
A spatial transform with location, rotation, and scale.
This structure is aligned with Unreal Engine's Transform concept.
Source code in src/tongsim/math/geometry/type.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
copy ¶
copy() -> Transform
Return a deep copy of this transform.
Source code in src/tongsim/math/geometry/type.py
81 82 83 84 85 86 87 88 89 | |
to_matrix ¶
to_matrix() -> mat4
Return the 4x4 affine transformation matrix for this transform.
The effective order is: scale → rotate → translate.
Source code in src/tongsim/math/geometry/type.py
136 137 138 139 140 141 142 143 144 145 146 | |
transform_vector3 ¶
transform_vector3(point: vec3) -> vec3
Apply this transform to a 3D point and return the transformed result.
Source code in src/tongsim/math/geometry/type.py
148 149 150 151 152 153 154 | |
inverse ¶
inverse() -> Transform
Return the inverse of this transform.
Note: invert scale first, then rotation, then translation.
Source code in src/tongsim/math/geometry/type.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
tongsim.math.geometry.type.AABB ¶
Axis-Aligned Bounding Box (AABB) in 3D space.
Attributes:
| Name | Type | Description |
|---|---|---|
min |
vec3
|
Minimum corner (smallest x, y, z). |
max |
vec3
|
Maximum corner (largest x, y, z). |
Source code in src/tongsim/math/geometry/type.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
deepcopy ¶
deepcopy() -> AABB
Return a deep copy of this AABB.
Source code in src/tongsim/math/geometry/type.py
192 193 194 195 196 | |
center ¶
center() -> vec3
Return the center point of the AABB.
Source code in src/tongsim/math/geometry/type.py
198 199 200 201 202 | |
extent ¶
extent() -> vec3
Return the size of the AABB (width, height, depth).
Source code in src/tongsim/math/geometry/type.py
204 205 206 207 208 | |
contains_point ¶
contains_point(point: vec3) -> bool
Check whether a point lies inside the AABB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
vec3
|
The point to test. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the point is inside; otherwise False. |
Source code in src/tongsim/math/geometry/type.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
tongsim.math.geometry.geometry.degrees_to_radians ¶
degrees_to_radians(
value: float | Vector3,
) -> float | Vector3
Convert degrees to radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | Vector3
|
A scalar degree value or a degree-based |
required |
Returns:
| Type | Description |
|---|---|
float | Vector3
|
float | Vector3: The value converted to radians. |
Source code in src/tongsim/math/geometry/geometry.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
tongsim.math.geometry.geometry.radians_to_degrees ¶
radians_to_degrees(
value: float | Vector3,
) -> float | Vector3
Convert radians to degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | Vector3
|
A scalar radian value or a radian-based |
required |
Returns:
| Type | Description |
|---|---|
float | Vector3
|
float | Vector3: The value converted to degrees. |
Source code in src/tongsim/math/geometry/geometry.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
tongsim.math.geometry.geometry.euler_to_quaternion ¶
euler_to_quaternion(
euler: Vector3, is_degree: bool = False
) -> Quaternion
Convert Euler angles (roll, pitch, yaw) to a quaternion.
Uses Unreal Engine's ZYX rotation order: - roll: rotate around X axis - pitch: rotate around Y axis - yaw: rotate around Z axis
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
euler
|
Vector3
|
Euler angles in (roll, pitch, yaw). |
required |
is_degree
|
bool
|
Whether the input is in degrees. Defaults to False (radians). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Quaternion |
Quaternion
|
The corresponding rotation quaternion. |
Source code in src/tongsim/math/geometry/geometry.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
tongsim.math.geometry.geometry.quaternion_to_euler ¶
quaternion_to_euler(
q: Quaternion, is_degree: bool = False
) -> Vector3
Convert a quaternion to Euler angles (roll, pitch, yaw).
Uses Unreal Engine's coordinate convention and ZYX rotation order: - Roll (X): rotate around X axis - Pitch (Y): rotate around Y axis - Yaw (Z): rotate around Z axis
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Quaternion
|
Input quaternion. |
required |
is_degree
|
bool
|
Whether to return degrees. Defaults to False (radians). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Vector3 |
Vector3
|
Euler angles in (roll, pitch, yaw). |
Source code in src/tongsim/math/geometry/geometry.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
tongsim.math.geometry.geometry.calc_camera_look_at_rotation ¶
calc_camera_look_at_rotation(
pos: Vector3, target: Vector3
) -> Quaternion
Compute the camera rotation quaternion required to look from pos to target.
Assumes the camera's local forward is +X and the world up is +Z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
Vector3
|
Camera position. |
required |
target
|
Vector3
|
Target position. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Quaternion |
Quaternion
|
The rotation that makes the camera look at the target. |
Source code in src/tongsim/math/geometry/geometry.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |