@esengine/pathfinding 12.1.2 → 13.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,407 @@
1
+ import { IVector2 } from '@esengine/ecs-framework-math';
2
+
3
+ /**
4
+ * @zh ORCA 局部避让接口定义
5
+ * @en ORCA Local Avoidance Interface Definitions
6
+ */
7
+
8
+ /**
9
+ * @zh ORCA 约束线(半平面)
10
+ * @en ORCA constraint line (half-plane)
11
+ *
12
+ * @zh 约束线定义了一个半平面,代理的速度必须在允许的一侧
13
+ * @en A constraint line defines a half-plane, agent's velocity must be on the allowed side
14
+ */
15
+ interface IORCALine {
16
+ /**
17
+ * @zh 线上的一点
18
+ * @en A point on the line
19
+ */
20
+ point: IVector2;
21
+ /**
22
+ * @zh 线的方向向量(单位向量,允许区域在左侧)
23
+ * @en Direction vector of the line (unit vector, allowed region is on the left)
24
+ */
25
+ direction: IVector2;
26
+ }
27
+ /**
28
+ * @zh 避让代理数据
29
+ * @en Avoidance agent data
30
+ *
31
+ * @zh 包含计算 ORCA 所需的所有代理信息
32
+ * @en Contains all agent information needed for ORCA computation
33
+ */
34
+ interface IAvoidanceAgent {
35
+ /**
36
+ * @zh 代理唯一标识
37
+ * @en Unique identifier for the agent
38
+ */
39
+ id: number;
40
+ /**
41
+ * @zh 当前位置
42
+ * @en Current position
43
+ */
44
+ position: IVector2;
45
+ /**
46
+ * @zh 当前速度
47
+ * @en Current velocity
48
+ */
49
+ velocity: IVector2;
50
+ /**
51
+ * @zh 首选速度(通常指向目标方向)
52
+ * @en Preferred velocity (usually towards target)
53
+ */
54
+ preferredVelocity: IVector2;
55
+ /**
56
+ * @zh 代理半径
57
+ * @en Agent radius
58
+ */
59
+ radius: number;
60
+ /**
61
+ * @zh 最大速度
62
+ * @en Maximum speed
63
+ */
64
+ maxSpeed: number;
65
+ /**
66
+ * @zh 邻居检测距离
67
+ * @en Neighbor detection distance
68
+ */
69
+ neighborDist: number;
70
+ /**
71
+ * @zh 最大邻居数量
72
+ * @en Maximum number of neighbors to consider
73
+ */
74
+ maxNeighbors: number;
75
+ /**
76
+ * @zh 代理避让时间视野(秒)
77
+ * @en Time horizon for agent avoidance (seconds)
78
+ *
79
+ * @zh 更大的值会让代理更早开始避让
80
+ * @en Larger values make agents start avoiding earlier
81
+ */
82
+ timeHorizon: number;
83
+ /**
84
+ * @zh 障碍物避让时间视野(秒)
85
+ * @en Time horizon for obstacle avoidance (seconds)
86
+ */
87
+ timeHorizonObst: number;
88
+ }
89
+ /**
90
+ * @zh 静态障碍物顶点(链表节点)
91
+ * @en Static obstacle vertex (linked list node)
92
+ */
93
+ interface IObstacleVertex {
94
+ /**
95
+ * @zh 顶点位置
96
+ * @en Vertex position
97
+ */
98
+ point: IVector2;
99
+ /**
100
+ * @zh 下一个顶点(构成障碍物边,循环链表)
101
+ * @en Next vertex (forms obstacle edge, circular linked list)
102
+ */
103
+ next: IObstacleVertex;
104
+ /**
105
+ * @zh 前一个顶点(循环链表)
106
+ * @en Previous vertex (circular linked list)
107
+ */
108
+ previous: IObstacleVertex;
109
+ /**
110
+ * @zh 边的单位方向向量(从此顶点指向 next 顶点)
111
+ * @en Unit direction vector of edge (from this vertex towards next vertex)
112
+ */
113
+ direction: IVector2;
114
+ /**
115
+ * @zh 是否为凸顶点
116
+ * @en Whether this is a convex vertex
117
+ */
118
+ isConvex: boolean;
119
+ /**
120
+ * @zh 顶点 ID(用于调试)
121
+ * @en Vertex ID (for debugging)
122
+ */
123
+ id: number;
124
+ }
125
+ /**
126
+ * @zh 静态障碍物(多边形)
127
+ * @en Static obstacle (polygon)
128
+ *
129
+ * @zh 重要:顶点必须按逆时针(CCW)顺序排列(在 Y 轴向上的坐标系中)
130
+ * @en Important: Vertices must be in counter-clockwise (CCW) order (in Y-axis up coordinate system)
131
+ *
132
+ * @zh 可以使用 math 库中的 Polygon.ensureCCW() 确保正确顺序
133
+ * @en Use Polygon.ensureCCW() from math library to ensure correct order
134
+ *
135
+ * @zh 在 Y 轴向下的坐标系(如 Canvas)中,视觉上的 CCW 需要传入 yAxisDown=true
136
+ * @en In Y-axis down coordinate system (like Canvas), use yAxisDown=true for visual CCW
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * import { Polygon } from '@esengine/ecs-framework-math';
141
+ *
142
+ * // 标准 Y 轴向上坐标系
143
+ * const obstacle: IObstacle = {
144
+ * vertices: Polygon.ensureCCW(myVertices)
145
+ * };
146
+ *
147
+ * // Canvas/屏幕坐标系(Y 轴向下)
148
+ * const obstacle: IObstacle = {
149
+ * vertices: Polygon.ensureCCW(myVertices, true)
150
+ * };
151
+ * ```
152
+ */
153
+ interface IObstacle {
154
+ /**
155
+ * @zh 顶点列表(逆时针顺序,Y 轴向上坐标系)
156
+ * @en Vertex list (counter-clockwise order in Y-axis up coordinate system)
157
+ */
158
+ vertices: IVector2[];
159
+ }
160
+ /**
161
+ * @zh ORCA 求解器配置
162
+ * @en ORCA solver configuration
163
+ */
164
+ interface IORCASolverConfig {
165
+ /**
166
+ * @zh 默认时间视野(代理)
167
+ * @en Default time horizon for agents
168
+ */
169
+ defaultTimeHorizon?: number;
170
+ /**
171
+ * @zh 默认时间视野(障碍物)
172
+ * @en Default time horizon for obstacles
173
+ */
174
+ defaultTimeHorizonObst?: number;
175
+ /**
176
+ * @zh 时间步长(用于碰撞响应)
177
+ * @en Time step (for collision response)
178
+ */
179
+ timeStep?: number;
180
+ /**
181
+ * @zh 数值精度阈值
182
+ * @en Numerical precision threshold
183
+ */
184
+ epsilon?: number;
185
+ }
186
+ /**
187
+ * @zh ORCA 求解结果
188
+ * @en ORCA solve result
189
+ */
190
+ interface IORCAResult {
191
+ /**
192
+ * @zh 计算得到的新速度
193
+ * @en Computed new velocity
194
+ */
195
+ velocity: IVector2;
196
+ /**
197
+ * @zh 是否找到可行解
198
+ * @en Whether a feasible solution was found
199
+ */
200
+ feasible: boolean;
201
+ /**
202
+ * @zh 生成的 ORCA 约束线数量
203
+ * @en Number of ORCA lines generated
204
+ */
205
+ numLines: number;
206
+ }
207
+ /**
208
+ * @zh ORCA 求解器接口
209
+ * @en ORCA solver interface
210
+ */
211
+ interface IORCASolver {
212
+ /**
213
+ * @zh 计算代理的新速度
214
+ * @en Compute new velocity for agent
215
+ *
216
+ * @param agent - @zh 当前代理 @en Current agent
217
+ * @param neighbors - @zh 邻近代理列表 @en List of neighbor agents
218
+ * @param obstacles - @zh 静态障碍物列表 @en List of static obstacles
219
+ * @param deltaTime - @zh 时间步长 @en Time step
220
+ * @returns @zh 新速度 @en New velocity
221
+ */
222
+ computeNewVelocity(agent: IAvoidanceAgent, neighbors: readonly IAvoidanceAgent[], obstacles: readonly IObstacle[], deltaTime: number): IVector2;
223
+ }
224
+ /**
225
+ * @zh 邻居查询结果
226
+ * @en Neighbor query result
227
+ */
228
+ interface INeighborResult {
229
+ /**
230
+ * @zh 代理数据
231
+ * @en Agent data
232
+ */
233
+ agent: IAvoidanceAgent;
234
+ /**
235
+ * @zh 距离的平方
236
+ * @en Squared distance
237
+ */
238
+ distanceSq: number;
239
+ }
240
+ /**
241
+ * @zh 空间索引接口(用于快速邻居查询)
242
+ * @en Spatial index interface (for fast neighbor queries)
243
+ */
244
+ interface ISpatialIndex {
245
+ /**
246
+ * @zh 构建空间索引
247
+ * @en Build spatial index
248
+ *
249
+ * @param agents - @zh 代理列表 @en List of agents
250
+ */
251
+ build(agents: readonly IAvoidanceAgent[]): void;
252
+ /**
253
+ * @zh 查询指定范围内的邻居
254
+ * @en Query neighbors within specified range
255
+ *
256
+ * @param position - @zh 查询位置 @en Query position
257
+ * @param radius - @zh 查询半径 @en Query radius
258
+ * @param maxResults - @zh 最大返回数量 @en Maximum number of results
259
+ * @param excludeId - @zh 排除的代理 ID @en Agent ID to exclude
260
+ * @returns @zh 邻居列表(按距离排序)@en List of neighbors (sorted by distance)
261
+ */
262
+ queryNeighbors(position: IVector2, radius: number, maxResults: number, excludeId?: number): INeighborResult[];
263
+ /**
264
+ * @zh 清空索引
265
+ * @en Clear the index
266
+ */
267
+ clear(): void;
268
+ }
269
+ /**
270
+ * @zh 默认 ORCA 求解器配置
271
+ * @en Default ORCA solver configuration
272
+ */
273
+ declare const DEFAULT_ORCA_CONFIG: Required<IORCASolverConfig>;
274
+ /**
275
+ * @zh 默认代理参数
276
+ * @en Default agent parameters
277
+ */
278
+ declare const DEFAULT_AGENT_PARAMS: {
279
+ radius: number;
280
+ maxSpeed: number;
281
+ neighborDist: number;
282
+ maxNeighbors: number;
283
+ timeHorizon: number;
284
+ timeHorizonObst: number;
285
+ };
286
+
287
+ /**
288
+ * @zh ORCA 避让算法求解器
289
+ * @en ORCA Avoidance Algorithm Solver
290
+ *
291
+ * @zh 实现最优互惠碰撞避免(ORCA)算法,用于多代理局部避让
292
+ * @en Implements Optimal Reciprocal Collision Avoidance (ORCA) algorithm for multi-agent local avoidance
293
+ */
294
+
295
+ /**
296
+ * @zh ORCA 求解器实现
297
+ * @en ORCA Solver implementation
298
+ *
299
+ * @zh 实现最优互惠碰撞避免算法,计算代理的安全速度
300
+ * @en Implements Optimal Reciprocal Collision Avoidance algorithm to compute safe velocities for agents
301
+ */
302
+ declare class ORCASolver implements IORCASolver {
303
+ private readonly config;
304
+ constructor(config?: IORCASolverConfig);
305
+ /**
306
+ * @zh 计算代理的新速度
307
+ * @en Compute new velocity for agent
308
+ *
309
+ * @param agent - @zh 当前代理 @en Current agent
310
+ * @param neighbors - @zh 邻近代理列表 @en List of neighboring agents
311
+ * @param obstacles - @zh 障碍物列表 @en List of obstacles
312
+ * @param deltaTime - @zh 时间步长 @en Time step
313
+ * @returns @zh 计算得到的新速度 @en Computed new velocity
314
+ */
315
+ computeNewVelocity(agent: IAvoidanceAgent, neighbors: readonly IAvoidanceAgent[], obstacles: readonly IObstacle[], deltaTime: number): IVector2;
316
+ /**
317
+ * @zh 创建代理间的 ORCA 约束线
318
+ * @en Create ORCA constraint lines for agent-agent avoidance
319
+ */
320
+ private createAgentORCALines;
321
+ /**
322
+ * @zh 创建障碍物的 ORCA 约束线
323
+ * @en Create ORCA constraint lines for obstacle avoidance
324
+ */
325
+ private createObstacleORCALines;
326
+ }
327
+ /**
328
+ * @zh 创建 ORCA 求解器
329
+ * @en Create ORCA solver
330
+ *
331
+ * @param config - @zh 可选配置参数 @en Optional configuration parameters
332
+ * @returns @zh ORCA 求解器实例 @en ORCA solver instance
333
+ */
334
+ declare function createORCASolver(config?: IORCASolverConfig): ORCASolver;
335
+
336
+ /**
337
+ * @zh KD-Tree 空间索引
338
+ * @en KD-Tree Spatial Index
339
+ *
340
+ * @zh 用于快速查询指定范围内的邻近代理
341
+ * @en Used for fast neighbor queries within specified range
342
+ */
343
+
344
+ /**
345
+ * @zh KD-Tree 空间索引
346
+ * @en KD-Tree spatial index
347
+ *
348
+ * @zh 每帧重建,支持高效的范围查询
349
+ * @en Rebuilt every frame, supports efficient range queries
350
+ */
351
+ declare class KDTree implements ISpatialIndex {
352
+ private agents;
353
+ private agentIndices;
354
+ private nodes;
355
+ /**
356
+ * @zh 最大叶节点大小
357
+ * @en Maximum leaf size
358
+ */
359
+ private readonly maxLeafSize;
360
+ /**
361
+ * @zh 构建 KD-Tree
362
+ * @en Build KD-Tree
363
+ */
364
+ build(agents: readonly IAvoidanceAgent[]): void;
365
+ /**
366
+ * @zh 递归构建 KD-Tree
367
+ * @en Recursively build KD-Tree
368
+ */
369
+ private buildRecursive;
370
+ /**
371
+ * @zh 按 X 坐标排序
372
+ * @en Sort by X coordinate
373
+ */
374
+ private sortByX;
375
+ /**
376
+ * @zh 按 Y 坐标排序
377
+ * @en Sort by Y coordinate
378
+ */
379
+ private sortByY;
380
+ /**
381
+ * @zh 查询邻居
382
+ * @en Query neighbors
383
+ */
384
+ queryNeighbors(position: IVector2, radius: number, maxResults: number, excludeId?: number): INeighborResult[];
385
+ /**
386
+ * @zh 递归查询
387
+ * @en Recursive query
388
+ */
389
+ private queryRecursive;
390
+ /**
391
+ * @zh 清空索引
392
+ * @en Clear the index
393
+ */
394
+ clear(): void;
395
+ /**
396
+ * @zh 获取代理数量
397
+ * @en Get agent count
398
+ */
399
+ get agentCount(): number;
400
+ }
401
+ /**
402
+ * @zh 创建 KD-Tree
403
+ * @en Create KD-Tree
404
+ */
405
+ declare function createKDTree(): KDTree;
406
+
407
+ export { DEFAULT_ORCA_CONFIG as D, type IORCALine as I, KDTree as K, ORCASolver as O, type IAvoidanceAgent as a, type IObstacle as b, type IORCASolverConfig as c, type IORCAResult as d, type IORCASolver as e, type INeighborResult as f, type ISpatialIndex as g, DEFAULT_AGENT_PARAMS as h, createORCASolver as i, createKDTree as j, type IObstacleVertex as k };
@@ -0,0 +1,56 @@
1
+ import { IVector2, Vector2 } from '@esengine/ecs-framework-math';
2
+ import { I as IORCALine } from './KDTree-2rs2EXvm.js';
3
+
4
+ /**
5
+ * @zh 2D 线性规划求解器
6
+ * @en 2D Linear Programming Solver
7
+ *
8
+ * @zh 用于 ORCA 算法中的速度优化求解
9
+ * @en Used for velocity optimization in ORCA algorithm
10
+ */
11
+
12
+ /**
13
+ * @zh 2D 线性规划
14
+ * @en 2D Linear Programming
15
+ *
16
+ * @zh 在多个半平面约束下找到最优速度
17
+ * @en Find optimal velocity under multiple half-plane constraints
18
+ *
19
+ * @param lines - @zh 约束线列表 @en List of constraint lines
20
+ * @param radius - @zh 最大速度(圆盘半径)@en Maximum speed (disk radius)
21
+ * @param optVelocity - @zh 首选速度 @en Preferred velocity
22
+ * @param directionOpt - @zh 是否优化方向 @en Whether to optimize direction
23
+ * @param result - @zh 结果向量(输出)@en Result vector (output)
24
+ * @returns @zh 第一个失败的约束索引,如果成功则返回 lines.length @en Index of first failed constraint, or lines.length if successful
25
+ */
26
+ declare function linearProgram2(lines: readonly IORCALine[], radius: number, optVelocity: IVector2, directionOpt: boolean, result: Vector2): number;
27
+ /**
28
+ * @zh 3D 线性规划(回退方案)
29
+ * @en 3D Linear Programming (fallback)
30
+ *
31
+ * @zh 当 2D 线性规划失败时,使用此方法找到最小穿透的速度
32
+ * @en When 2D LP fails, use this to find velocity with minimum penetration
33
+ *
34
+ * @param lines - @zh 约束线列表 @en List of constraint lines
35
+ * @param numObstLines - @zh 障碍物约束线数量 @en Number of obstacle constraint lines
36
+ * @param beginLine - @zh 开始处理的线索引 @en Index of line to start processing
37
+ * @param radius - @zh 最大速度 @en Maximum speed
38
+ * @param result - @zh 结果向量(输入/输出)@en Result vector (input/output)
39
+ */
40
+ declare function linearProgram3(lines: IORCALine[], numObstLines: number, beginLine: number, radius: number, result: Vector2): void;
41
+ /**
42
+ * @zh 求解 ORCA 线性规划
43
+ * @en Solve ORCA Linear Programming
44
+ *
45
+ * @zh 综合使用 2D 和 3D 线性规划求解最优速度
46
+ * @en Use both 2D and 3D LP to solve for optimal velocity
47
+ *
48
+ * @param lines - @zh ORCA 约束线列表 @en List of ORCA constraint lines
49
+ * @param numObstLines - @zh 障碍物约束线数量(优先级更高)@en Number of obstacle lines (higher priority)
50
+ * @param maxSpeed - @zh 最大速度 @en Maximum speed
51
+ * @param preferredVelocity - @zh 首选速度 @en Preferred velocity
52
+ * @returns @zh 计算得到的新速度 @en Computed new velocity
53
+ */
54
+ declare function solveORCALinearProgram(lines: IORCALine[], numObstLines: number, maxSpeed: number, preferredVelocity: IVector2): Vector2;
55
+
56
+ export { linearProgram3 as a, linearProgram2 as l, solveORCALinearProgram as s };
@@ -0,0 +1,31 @@
1
+ import { k as IObstacleVertex, b as IObstacle } from './KDTree-2rs2EXvm.js';
2
+ export { h as DEFAULT_AGENT_PARAMS, D as DEFAULT_ORCA_CONFIG, a as IAvoidanceAgent, f as INeighborResult, I as IORCALine, d as IORCAResult, e as IORCASolver, c as IORCASolverConfig, g as ISpatialIndex, K as KDTree, O as ORCASolver, j as createKDTree, i as createORCASolver } from './KDTree-2rs2EXvm.js';
3
+ export { l as linearProgram2, a as linearProgram3, s as solveORCALinearProgram } from './LinearProgram-DyD3pI6v.js';
4
+ import { IVector2 } from '@esengine/ecs-framework-math';
5
+ export { IVector2 } from '@esengine/ecs-framework-math';
6
+
7
+ /**
8
+ * @zh 障碍物构建器
9
+ * @en Obstacle Builder
10
+ */
11
+
12
+ /**
13
+ * @zh 从顶点数组创建障碍物链表
14
+ * @en Create obstacle linked list from vertex array
15
+ *
16
+ * @param vertices - @zh 顶点数组(CCW 顺序)@en Vertex array (CCW order)
17
+ * @param startId - @zh 起始顶点 ID @en Starting vertex ID
18
+ */
19
+ declare function createObstacleVertices(vertices: readonly IVector2[], startId?: number): IObstacleVertex[];
20
+ /**
21
+ * @zh 从障碍物数组创建所有障碍物顶点
22
+ * @en Create all obstacle vertices from obstacle array
23
+ */
24
+ declare function buildObstacleVertices(obstacles: readonly IObstacle[]): IObstacleVertex[];
25
+ /**
26
+ * @zh 确保顶点按逆时针顺序排列
27
+ * @en Ensure vertices are in counter-clockwise order
28
+ */
29
+ declare function ensureCCW(vertices: IVector2[], yAxisDown?: boolean): IVector2[];
30
+
31
+ export { IObstacle, IObstacleVertex, buildObstacleVertices, createObstacleVertices, ensureCCW };
@@ -0,0 +1,31 @@
1
+ import "./chunk-KEYTX37K.js";
2
+ import {
3
+ DEFAULT_AGENT_PARAMS,
4
+ DEFAULT_ORCA_CONFIG,
5
+ KDTree,
6
+ ORCASolver,
7
+ buildObstacleVertices,
8
+ createKDTree,
9
+ createORCASolver,
10
+ createObstacleVertices,
11
+ ensureCCW,
12
+ linearProgram2,
13
+ linearProgram3,
14
+ solveORCALinearProgram
15
+ } from "./chunk-JTZP55BJ.js";
16
+ import "./chunk-T626JPC7.js";
17
+ export {
18
+ DEFAULT_AGENT_PARAMS,
19
+ DEFAULT_ORCA_CONFIG,
20
+ KDTree,
21
+ ORCASolver,
22
+ buildObstacleVertices,
23
+ createKDTree,
24
+ createORCASolver,
25
+ createObstacleVertices,
26
+ ensureCCW,
27
+ linearProgram2,
28
+ linearProgram3,
29
+ solveORCALinearProgram
30
+ };
31
+ //# sourceMappingURL=avoidance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}