@antv/g6 5.0.15 → 5.0.16
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.
- package/dist/g6.min.js +2 -2
- package/dist/lib/behaviors/drag-canvas.d.ts +13 -0
- package/dist/lib/behaviors/scroll-canvas.d.ts +7 -0
- package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
- package/dist/lib/elements/edges/base-edge.d.ts +2 -2
- package/dist/lib/elements/edges/polyline.d.ts +3 -20
- package/dist/lib/plugins/index.d.ts +2 -0
- package/dist/lib/plugins/snapline/index.d.ts +95 -0
- package/dist/lib/runtime/element.d.ts +1 -0
- package/dist/lib/runtime/viewport.d.ts +8 -0
- package/dist/lib/types/index.d.ts +1 -0
- package/dist/lib/types/router.d.ts +103 -0
- package/dist/lib/utils/bbox.d.ts +8 -0
- package/dist/lib/utils/router/orth.d.ts +8 -8
- package/dist/lib/utils/router/shortest-path.d.ts +57 -0
- package/dist/lib/utils/text.d.ts +1 -1
- package/dist/lib/utils/visibility.d.ts +2 -1
- package/dist/lib/version.d.ts +1 -1
- package/esm/behaviors/brush-select.js +11 -6
- package/esm/behaviors/brush-select.js.map +1 -1
- package/esm/behaviors/drag-canvas.d.ts +13 -0
- package/esm/behaviors/drag-canvas.js +10 -1
- package/esm/behaviors/drag-canvas.js.map +1 -1
- package/esm/behaviors/optimize-viewport-transform.js +1 -1
- package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
- package/esm/behaviors/scroll-canvas.d.ts +7 -0
- package/esm/behaviors/scroll-canvas.js +3 -1
- package/esm/behaviors/scroll-canvas.js.map +1 -1
- package/esm/behaviors/zoom-canvas.d.ts +7 -0
- package/esm/behaviors/zoom-canvas.js +3 -1
- package/esm/behaviors/zoom-canvas.js.map +1 -1
- package/esm/elements/edges/base-edge.d.ts +2 -2
- package/esm/elements/edges/base-edge.js +10 -4
- package/esm/elements/edges/base-edge.js.map +1 -1
- package/esm/elements/edges/polyline.d.ts +3 -20
- package/esm/elements/edges/polyline.js +29 -34
- package/esm/elements/edges/polyline.js.map +1 -1
- package/esm/elements/shapes/base-shape.js +1 -1
- package/esm/elements/shapes/base-shape.js.map +1 -1
- package/esm/plugins/index.d.ts +2 -0
- package/esm/plugins/index.js +1 -0
- package/esm/plugins/index.js.map +1 -1
- package/esm/plugins/snapline/index.d.ts +95 -0
- package/esm/plugins/snapline/index.js +268 -0
- package/esm/plugins/snapline/index.js.map +1 -0
- package/esm/registry/build-in.js +3 -2
- package/esm/registry/build-in.js.map +1 -1
- package/esm/runtime/element.d.ts +1 -0
- package/esm/runtime/element.js +10 -4
- package/esm/runtime/element.js.map +1 -1
- package/esm/runtime/viewport.d.ts +8 -0
- package/esm/runtime/viewport.js +35 -2
- package/esm/runtime/viewport.js.map +1 -1
- package/esm/types/index.d.ts +1 -0
- package/esm/types/router.d.ts +103 -0
- package/esm/types/router.js +2 -0
- package/esm/types/router.js.map +1 -0
- package/esm/utils/bbox.d.ts +8 -0
- package/esm/utils/bbox.js +19 -0
- package/esm/utils/bbox.js.map +1 -1
- package/esm/utils/router/orth.d.ts +8 -8
- package/esm/utils/router/orth.js +13 -7
- package/esm/utils/router/orth.js.map +1 -1
- package/esm/utils/router/shortest-path.d.ts +57 -0
- package/esm/utils/router/shortest-path.js +387 -0
- package/esm/utils/router/shortest-path.js.map +1 -0
- package/esm/utils/text.d.ts +1 -1
- package/esm/utils/visibility.d.ts +2 -1
- package/esm/utils/visibility.js +3 -2
- package/esm/utils/visibility.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/behaviors/brush-select.js +11 -6
- package/lib/behaviors/brush-select.js.map +1 -1
- package/lib/behaviors/drag-canvas.d.ts +13 -0
- package/lib/behaviors/drag-canvas.js +10 -1
- package/lib/behaviors/drag-canvas.js.map +1 -1
- package/lib/behaviors/optimize-viewport-transform.js +1 -1
- package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
- package/lib/behaviors/scroll-canvas.d.ts +7 -0
- package/lib/behaviors/scroll-canvas.js +3 -1
- package/lib/behaviors/scroll-canvas.js.map +1 -1
- package/lib/behaviors/zoom-canvas.d.ts +7 -0
- package/lib/behaviors/zoom-canvas.js +3 -1
- package/lib/behaviors/zoom-canvas.js.map +1 -1
- package/lib/elements/edges/base-edge.d.ts +2 -2
- package/lib/elements/edges/base-edge.js +9 -3
- package/lib/elements/edges/base-edge.js.map +1 -1
- package/lib/elements/edges/polyline.d.ts +3 -20
- package/lib/elements/edges/polyline.js +29 -34
- package/lib/elements/edges/polyline.js.map +1 -1
- package/lib/elements/shapes/base-shape.js +1 -1
- package/lib/elements/shapes/base-shape.js.map +1 -1
- package/lib/plugins/index.d.ts +2 -0
- package/lib/plugins/index.js +3 -1
- package/lib/plugins/index.js.map +1 -1
- package/lib/plugins/snapline/index.d.ts +95 -0
- package/lib/plugins/snapline/index.js +272 -0
- package/lib/plugins/snapline/index.js.map +1 -0
- package/lib/registry/build-in.js +2 -1
- package/lib/registry/build-in.js.map +1 -1
- package/lib/runtime/element.d.ts +1 -0
- package/lib/runtime/element.js +9 -3
- package/lib/runtime/element.js.map +1 -1
- package/lib/runtime/viewport.d.ts +8 -0
- package/lib/runtime/viewport.js +34 -1
- package/lib/runtime/viewport.js.map +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/router.d.ts +103 -0
- package/lib/types/router.js +3 -0
- package/lib/types/router.js.map +1 -0
- package/lib/utils/bbox.d.ts +8 -0
- package/lib/utils/bbox.js +20 -0
- package/lib/utils/bbox.js.map +1 -1
- package/lib/utils/router/orth.d.ts +8 -8
- package/lib/utils/router/orth.js +13 -7
- package/lib/utils/router/orth.js.map +1 -1
- package/lib/utils/router/shortest-path.d.ts +57 -0
- package/lib/utils/router/shortest-path.js +394 -0
- package/lib/utils/router/shortest-path.js.map +1 -0
- package/lib/utils/text.d.ts +1 -1
- package/lib/utils/visibility.d.ts +2 -1
- package/lib/utils/visibility.js +3 -2
- package/lib/utils/visibility.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/src/behaviors/brush-select.ts +12 -5
- package/src/behaviors/drag-canvas.ts +24 -1
- package/src/behaviors/optimize-viewport-transform.ts +1 -0
- package/src/behaviors/scroll-canvas.ts +9 -1
- package/src/behaviors/zoom-canvas.ts +9 -1
- package/src/elements/edges/base-edge.ts +19 -5
- package/src/elements/edges/polyline.ts +33 -68
- package/src/elements/shapes/base-shape.ts +1 -1
- package/src/plugins/grid-line.ts +1 -1
- package/src/plugins/index.ts +2 -0
- package/src/plugins/snapline/index.ts +383 -0
- package/src/registry/build-in.ts +3 -1
- package/src/runtime/element.ts +11 -4
- package/src/runtime/viewport.ts +42 -3
- package/src/types/index.ts +1 -0
- package/src/types/router.ts +107 -0
- package/src/utils/bbox.ts +24 -0
- package/src/utils/router/orth.ts +19 -10
- package/src/utils/router/shortest-path.ts +470 -0
- package/src/utils/text.ts +1 -1
- package/src/utils/visibility.ts +3 -1
- package/src/version.ts +1 -1
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { isNumber } from '@antv/util';
|
|
2
|
+
import type { Direction, ID, Node, Point, ShortestPathRouterOptions } from '../../types';
|
|
3
|
+
import { getBBoxSegments, getBBoxSize, getExpandedBBox, isPointInBBox, isPointOnBBoxBoundary } from '../bbox';
|
|
4
|
+
import { getLinesIntersection } from '../line';
|
|
5
|
+
import { add, manhattanDistance, toVector2 } from '../vector';
|
|
6
|
+
|
|
7
|
+
const defaultCfg: ShortestPathRouterOptions = {
|
|
8
|
+
enableObstacleAvoidance: false,
|
|
9
|
+
offset: 10,
|
|
10
|
+
maxAllowedDirectionChange: Math.PI / 2,
|
|
11
|
+
maximumLoops: 3000,
|
|
12
|
+
gridSize: 5,
|
|
13
|
+
startDirections: ['top', 'right', 'bottom', 'left'],
|
|
14
|
+
endDirections: ['top', 'right', 'bottom', 'left'],
|
|
15
|
+
directionMap: {
|
|
16
|
+
right: { stepX: 1, stepY: 0 },
|
|
17
|
+
left: { stepX: -1, stepY: 0 },
|
|
18
|
+
bottom: { stepX: 0, stepY: 1 },
|
|
19
|
+
top: { stepX: 0, stepY: -1 },
|
|
20
|
+
},
|
|
21
|
+
penalties: { 0: 0, 90: 0 },
|
|
22
|
+
distFunc: manhattanDistance,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const keyOf = (point: Point) => `${Math.round(point[0])}|||${Math.round(point[1])}`;
|
|
26
|
+
|
|
27
|
+
function alignToGrid(p: Point, gridSize: number): Point;
|
|
28
|
+
function alignToGrid(p: number, gridSize: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* <zh/> 将坐标对齐到网格
|
|
31
|
+
*
|
|
32
|
+
* <en/> Align to the grid
|
|
33
|
+
* @param p - <zh/> 坐标 | <en/> point
|
|
34
|
+
* @param gridSize - <zh/> 网格大小 | <en/> grid size
|
|
35
|
+
* @returns <zh/> 对齐后的坐标 | <en/> aligned point
|
|
36
|
+
*/
|
|
37
|
+
function alignToGrid(p: number | Point, gridSize: number): number | Point {
|
|
38
|
+
const align = (value: number) => Math.round(value / gridSize);
|
|
39
|
+
if (isNumber(p)) return align(p);
|
|
40
|
+
return p.map(align) as Point;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* <zh/> 获取两个角度的变化方向,并确保小于 180 度
|
|
45
|
+
*
|
|
46
|
+
* <en/ >Get changed direction angle and make sure less than 180 degrees
|
|
47
|
+
* @param angle1 - <zh/> 第一个角度 | <en/> the first angle
|
|
48
|
+
* @param angle2 - <zh/> 第二个角度 | <en/> the second angle
|
|
49
|
+
* @returns <zh/> 两个角度的变化方向 | <en/> changed direction angle
|
|
50
|
+
*/
|
|
51
|
+
function getAngleDiff(angle1: number, angle2: number) {
|
|
52
|
+
const directionChange = Math.abs(angle1 - angle2);
|
|
53
|
+
return directionChange > Math.PI ? 2 * Math.PI - directionChange : directionChange;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* <zh/> 获取从 p1 指向 p2 的向量与 x 轴正方向之间的夹角,单位为弧度
|
|
58
|
+
*
|
|
59
|
+
* <en/> Get the angle between the vector from p1 to p2 and the positive direction of the x-axis, in radians
|
|
60
|
+
* @param p1 - <zh/> 点 p1 | <en/> point p1
|
|
61
|
+
* @param p2 - <zh/> 点 p2 | <en/> point p2
|
|
62
|
+
* @returns <zh/> 夹角 | <en/> angle
|
|
63
|
+
*/
|
|
64
|
+
function getDirectionAngle(p1: Point, p2: Point) {
|
|
65
|
+
const deltaX = p2[0] - p1[0];
|
|
66
|
+
const deltaY = p2[1] - p1[1];
|
|
67
|
+
|
|
68
|
+
if (!deltaX && !deltaY) return 0;
|
|
69
|
+
|
|
70
|
+
return Math.atan2(deltaY, deltaX);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* <zh/> 获取两个点之间的方向变化
|
|
75
|
+
*
|
|
76
|
+
* <en/> Get direction change between two points
|
|
77
|
+
* @param current - <zh/> 当前点 | <en/> current point
|
|
78
|
+
* @param neighbor - <zh/> 邻居点 | <en/> neighbor point
|
|
79
|
+
* @param cameFrom - <zh/> 来源点 | <en/> source point
|
|
80
|
+
* @param scaleStartPoint - <zh/> 缩放后的起点 | <en/> scaled start point
|
|
81
|
+
* @returns <zh/> 方向变化 | <en/> direction change
|
|
82
|
+
*/
|
|
83
|
+
function getDirectionChange(
|
|
84
|
+
current: Point,
|
|
85
|
+
neighbor: Point,
|
|
86
|
+
cameFrom: Record<string, Point>,
|
|
87
|
+
scaleStartPoint: Point,
|
|
88
|
+
): number {
|
|
89
|
+
const directionAngle = getDirectionAngle(current, neighbor);
|
|
90
|
+
|
|
91
|
+
const currentCameFrom = cameFrom[keyOf(current)];
|
|
92
|
+
const prev = !currentCameFrom ? scaleStartPoint : currentCameFrom;
|
|
93
|
+
const prevDirectionAngle = getDirectionAngle(prev, current);
|
|
94
|
+
|
|
95
|
+
return getAngleDiff(prevDirectionAngle, directionAngle);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* <zh/> 获取障碍物地图
|
|
100
|
+
*
|
|
101
|
+
* <en/> Get obstacle map
|
|
102
|
+
* @param nodes - <zh/> 图上所有节点 | <en/> all nodes on the graph
|
|
103
|
+
* @param options - <zh/> 路由配置 | <en/> router options
|
|
104
|
+
* @returns <zh/> 障碍物地图 | <en/> obstacle map
|
|
105
|
+
*/
|
|
106
|
+
const getObstacleMap = (nodes: Node[], options: Required<ShortestPathRouterOptions>) => {
|
|
107
|
+
const { offset, gridSize } = options;
|
|
108
|
+
const obstacleMap: Record<string, boolean> = {};
|
|
109
|
+
|
|
110
|
+
nodes.forEach((item: Node) => {
|
|
111
|
+
if (!item || item.destroyed || !item.isVisible()) return;
|
|
112
|
+
const bbox = getExpandedBBox(item.getRenderBounds(), offset);
|
|
113
|
+
for (let x = alignToGrid(bbox.min[0], gridSize); x <= alignToGrid(bbox.max[0], gridSize); x += 1) {
|
|
114
|
+
for (let y = alignToGrid(bbox.min[1], gridSize); y <= alignToGrid(bbox.max[1], gridSize); y += 1) {
|
|
115
|
+
obstacleMap[`${x}|||${y}`] = true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return obstacleMap;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* <zh/> 估算从起点到多个锚点的最小代价
|
|
124
|
+
*
|
|
125
|
+
* <en/> Estimate minimum cost from the starting point to multiple anchor points
|
|
126
|
+
* @param from - <zh/> 起点 | <en/> source point
|
|
127
|
+
* @param anchors - <zh/> 锚点 | <en/> anchor points
|
|
128
|
+
* @param distFunc - <zh/> 距离函数 | <en/> distance function
|
|
129
|
+
* @returns <zh/> 最小成本 | <en/> minimum cost
|
|
130
|
+
*/
|
|
131
|
+
export function estimateCost(from: Point, anchors: Point[], distFunc: (p1: Point, p2: Point) => number) {
|
|
132
|
+
return Math.min(...anchors.map((anchor) => distFunc(from, anchor)));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* <zh/> 已知一个点集与一个参考点,从点集中找到距离参考点最近的点
|
|
137
|
+
*
|
|
138
|
+
* <en/> Given a set of points and a reference point, find the point closest to the reference point from the set of points
|
|
139
|
+
* @param points - <zh/> 点集 | <en/> set of points
|
|
140
|
+
* @param refPoint - <zh/> 参考点 | <en/> reference point
|
|
141
|
+
* @param distFunc - <zh/> 距离函数 | <en/> distance function
|
|
142
|
+
* @returns <zh/> 最近的点 | <en/> nearest point
|
|
143
|
+
*/
|
|
144
|
+
export function getNearestPoint(points: Point[], refPoint: Point, distFunc: (p1: Point, p2: Point) => number): Point {
|
|
145
|
+
let nearestPoint = points[0];
|
|
146
|
+
let minDistance = distFunc(points[0], refPoint);
|
|
147
|
+
for (let i = 0; i < points.length; i++) {
|
|
148
|
+
const point = points[i];
|
|
149
|
+
const dis = distFunc(point, refPoint);
|
|
150
|
+
if (dis < minDistance) {
|
|
151
|
+
nearestPoint = point;
|
|
152
|
+
minDistance = dis;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return nearestPoint;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Calculate the connection points on the expanded BBox
|
|
160
|
+
* @param point
|
|
161
|
+
* @param node
|
|
162
|
+
* @param directions
|
|
163
|
+
* @param options
|
|
164
|
+
*/
|
|
165
|
+
const getBoxPoints = (
|
|
166
|
+
point: Point,
|
|
167
|
+
node: Node,
|
|
168
|
+
directions: Direction[],
|
|
169
|
+
options: Required<ShortestPathRouterOptions>,
|
|
170
|
+
): Point[] => {
|
|
171
|
+
// create-edge 生成边的过程中,endNode 为 null
|
|
172
|
+
if (!node) return [point];
|
|
173
|
+
|
|
174
|
+
const { directionMap, offset } = options;
|
|
175
|
+
const expandedBBox = getExpandedBBox(node.getRenderBounds(), offset);
|
|
176
|
+
|
|
177
|
+
const points = (Object.keys(directionMap) as Direction[]).reduce<Point[]>((res, directionKey) => {
|
|
178
|
+
if (directions.includes(directionKey)) {
|
|
179
|
+
const direction = directionMap[directionKey];
|
|
180
|
+
const [width, height] = getBBoxSize(expandedBBox);
|
|
181
|
+
const otherPoint: Point = [point[0] + direction.stepX * width, point[1] + direction.stepY * height];
|
|
182
|
+
const segments = getBBoxSegments(expandedBBox);
|
|
183
|
+
for (let i = 0; i < segments.length; i++) {
|
|
184
|
+
const intersectP = getLinesIntersection([point, otherPoint], segments[i]);
|
|
185
|
+
if (intersectP && isPointOnBBoxBoundary(intersectP, expandedBBox)) {
|
|
186
|
+
res.push(intersectP);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return res;
|
|
192
|
+
}, []);
|
|
193
|
+
|
|
194
|
+
if (!isPointInBBox(point, expandedBBox)) {
|
|
195
|
+
points.push(point);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return points.map((point) => alignToGrid(point, options.gridSize));
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const getControlPoints = (
|
|
202
|
+
current: Point,
|
|
203
|
+
cameFrom: Record<ID, Point>,
|
|
204
|
+
scaleStartPoint: Point,
|
|
205
|
+
endPoint: Point,
|
|
206
|
+
startPoints: Point[],
|
|
207
|
+
scaleEndPoint: Point,
|
|
208
|
+
gridSize: number,
|
|
209
|
+
) => {
|
|
210
|
+
const controlPoints: Point[] = [];
|
|
211
|
+
|
|
212
|
+
// append endPoint
|
|
213
|
+
let pointZero: Point = [
|
|
214
|
+
scaleEndPoint[0] === endPoint[0] ? endPoint[0] : current[0] * gridSize,
|
|
215
|
+
scaleEndPoint[1] === endPoint[1] ? endPoint[1] : current[1] * gridSize,
|
|
216
|
+
];
|
|
217
|
+
controlPoints.unshift(pointZero);
|
|
218
|
+
|
|
219
|
+
let _current = current;
|
|
220
|
+
let _currentCameFrom = cameFrom[keyOf(_current)];
|
|
221
|
+
|
|
222
|
+
while (_currentCameFrom) {
|
|
223
|
+
const prePoint = _currentCameFrom;
|
|
224
|
+
const point = _current;
|
|
225
|
+
const directionChange = getDirectionChange(prePoint, point, cameFrom, scaleStartPoint);
|
|
226
|
+
|
|
227
|
+
if (directionChange) {
|
|
228
|
+
pointZero = [
|
|
229
|
+
prePoint[0] === point[0] ? pointZero[0] : prePoint[0] * gridSize,
|
|
230
|
+
prePoint[1] === point[1] ? pointZero[1] : prePoint[1] * gridSize,
|
|
231
|
+
];
|
|
232
|
+
controlPoints.unshift(pointZero);
|
|
233
|
+
}
|
|
234
|
+
_currentCameFrom = cameFrom[keyOf(prePoint)];
|
|
235
|
+
_current = prePoint;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// append startPoint
|
|
239
|
+
const realStartPoints = startPoints.map((point) => [point[0] * gridSize, point[1] * gridSize] as Point);
|
|
240
|
+
const startPoint = getNearestPoint(realStartPoints, pointZero, manhattanDistance);
|
|
241
|
+
|
|
242
|
+
controlPoints.unshift(startPoint);
|
|
243
|
+
|
|
244
|
+
return controlPoints;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Find the shortest path between a given source node to a destination node according to A* Search Algorithm:https://www.geeksforgeeks.org/a-search-algorithm/
|
|
249
|
+
* @param sourceNode - <zh/> 源节点 | <en/> source node
|
|
250
|
+
* @param targetNode - <zh/> 目标节点 | <en/> target node
|
|
251
|
+
* @param nodes - <zh/> 图上所有节点 | <en/> all nodes on the graph
|
|
252
|
+
* @param config - <zh/> 路由配置 | <en/> router options
|
|
253
|
+
* @returns <zh/> 控制点数组 | <en/> control point array
|
|
254
|
+
*/
|
|
255
|
+
export function aStarSearch(
|
|
256
|
+
sourceNode: Node,
|
|
257
|
+
targetNode: Node,
|
|
258
|
+
nodes: Node[],
|
|
259
|
+
config: ShortestPathRouterOptions,
|
|
260
|
+
): Point[] {
|
|
261
|
+
const startPoint = toVector2(sourceNode.getCenter());
|
|
262
|
+
const endPoint = toVector2(targetNode.getCenter());
|
|
263
|
+
|
|
264
|
+
const options = Object.assign(defaultCfg, config) as Required<ShortestPathRouterOptions>;
|
|
265
|
+
|
|
266
|
+
const { gridSize } = options;
|
|
267
|
+
|
|
268
|
+
const obstacles = options.enableObstacleAvoidance ? nodes : [sourceNode, targetNode];
|
|
269
|
+
const obstacleMap = getObstacleMap(obstacles, options);
|
|
270
|
+
|
|
271
|
+
const scaleStartPoint = alignToGrid(startPoint, gridSize);
|
|
272
|
+
const scaleEndPoint = alignToGrid(endPoint, gridSize);
|
|
273
|
+
|
|
274
|
+
const startPoints = getBoxPoints(startPoint, sourceNode, options.startDirections, options);
|
|
275
|
+
const endPoints = getBoxPoints(endPoint, targetNode, options.endDirections, options);
|
|
276
|
+
|
|
277
|
+
startPoints.forEach((point) => delete obstacleMap[keyOf(point)]);
|
|
278
|
+
endPoints.forEach((point) => delete obstacleMap[keyOf(point)]);
|
|
279
|
+
|
|
280
|
+
const openList: Record<string, Point> = {};
|
|
281
|
+
const closedList: Record<string, boolean> = {};
|
|
282
|
+
const cameFrom: Record<string, Point> = {};
|
|
283
|
+
|
|
284
|
+
// The movement cost to move from the starting point to the current point on the grid.
|
|
285
|
+
const gScore: Record<string, number> = {};
|
|
286
|
+
|
|
287
|
+
// The estimated movement cost to move from the starting point to the end point after passing through the current point.
|
|
288
|
+
// f = g + h
|
|
289
|
+
const fScore: Record<string, number> = {};
|
|
290
|
+
|
|
291
|
+
const sortedOpenSet = new SortedArray();
|
|
292
|
+
|
|
293
|
+
for (let i = 0; i < startPoints.length; i++) {
|
|
294
|
+
const firstStep = startPoints[i];
|
|
295
|
+
const key = keyOf(firstStep);
|
|
296
|
+
openList[key] = firstStep;
|
|
297
|
+
gScore[key] = 0;
|
|
298
|
+
fScore[key] = estimateCost(firstStep, endPoints, options.distFunc);
|
|
299
|
+
|
|
300
|
+
// Push start point to sortedOpenSet
|
|
301
|
+
sortedOpenSet.add({
|
|
302
|
+
id: key,
|
|
303
|
+
value: fScore[key],
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const endPointsKeys = endPoints.map((point) => keyOf(point));
|
|
308
|
+
|
|
309
|
+
let remainLoops = options.maximumLoops;
|
|
310
|
+
let current: Point;
|
|
311
|
+
let curCost = Infinity;
|
|
312
|
+
|
|
313
|
+
for (const [id, value] of Object.entries(openList)) {
|
|
314
|
+
if (fScore[id] <= curCost) {
|
|
315
|
+
curCost = fScore[id];
|
|
316
|
+
current = value;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
while (Object.keys(openList).length > 0 && remainLoops > 0) {
|
|
321
|
+
const minId = sortedOpenSet.minId(false);
|
|
322
|
+
if (minId) {
|
|
323
|
+
current = openList[minId];
|
|
324
|
+
} else {
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
const key = keyOf(current);
|
|
328
|
+
|
|
329
|
+
// If currentNode is final, return the successful path
|
|
330
|
+
if (endPointsKeys.includes(key)) {
|
|
331
|
+
return getControlPoints(current, cameFrom, scaleStartPoint, endPoint, startPoints, scaleEndPoint, gridSize);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Set currentNode as closed
|
|
335
|
+
delete openList[key];
|
|
336
|
+
sortedOpenSet.remove(key);
|
|
337
|
+
closedList[key] = true;
|
|
338
|
+
|
|
339
|
+
// Get the neighbor points of the next step
|
|
340
|
+
for (const dir of Object.values(options.directionMap)) {
|
|
341
|
+
const neighbor = add(current, [dir.stepX, dir.stepY]);
|
|
342
|
+
const neighborId = keyOf(neighbor);
|
|
343
|
+
if (closedList[neighborId]) continue;
|
|
344
|
+
|
|
345
|
+
const directionChange = getDirectionChange(current, neighbor, cameFrom, scaleStartPoint);
|
|
346
|
+
if (directionChange > options.maxAllowedDirectionChange) continue;
|
|
347
|
+
|
|
348
|
+
if (obstacleMap[neighborId]) continue; // skip if intersects
|
|
349
|
+
|
|
350
|
+
// Add neighbor points to openList, and calculate the cost of each neighbor point
|
|
351
|
+
if (!openList[neighborId]) {
|
|
352
|
+
openList[neighborId] = neighbor;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const directionPenalties = options.penalties[directionChange];
|
|
356
|
+
const neighborCost =
|
|
357
|
+
options.distFunc(current, neighbor) + (isNaN(directionPenalties) ? gridSize : directionPenalties);
|
|
358
|
+
const costFromStart = gScore[key] + neighborCost;
|
|
359
|
+
const neighborGScore = gScore[neighborId];
|
|
360
|
+
|
|
361
|
+
if (neighborGScore && costFromStart >= neighborGScore) continue;
|
|
362
|
+
|
|
363
|
+
cameFrom[neighborId] = current;
|
|
364
|
+
gScore[neighborId] = costFromStart;
|
|
365
|
+
fScore[neighborId] = costFromStart + estimateCost(neighbor, endPoints, options.distFunc);
|
|
366
|
+
|
|
367
|
+
sortedOpenSet.add({
|
|
368
|
+
id: neighborId,
|
|
369
|
+
value: fScore[neighborId],
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
remainLoops -= 1;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return [];
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
type Item = {
|
|
379
|
+
id: string;
|
|
380
|
+
value: number;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* <zh/> 有序数组,按升序排列
|
|
385
|
+
*
|
|
386
|
+
* <en/> Sorted array, sorted in ascending order
|
|
387
|
+
*/
|
|
388
|
+
export class SortedArray {
|
|
389
|
+
public arr: Item[] = [];
|
|
390
|
+
|
|
391
|
+
private map: Record<string, boolean> = {};
|
|
392
|
+
|
|
393
|
+
constructor() {
|
|
394
|
+
this.arr = [];
|
|
395
|
+
this.map = {};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
private _innerAdd(item: Item, length: number) {
|
|
399
|
+
let low = 0,
|
|
400
|
+
high = length - 1;
|
|
401
|
+
while (high - low > 1) {
|
|
402
|
+
const mid = Math.floor((low + high) / 2);
|
|
403
|
+
if (this.arr[mid].value > item.value) {
|
|
404
|
+
high = mid;
|
|
405
|
+
} else if (this.arr[mid].value < item.value) {
|
|
406
|
+
low = mid;
|
|
407
|
+
} else {
|
|
408
|
+
this.arr.splice(mid, 0, item);
|
|
409
|
+
this.map[item.id] = true;
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
this.arr.splice(high, 0, item);
|
|
414
|
+
this.map[item.id] = true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* <zh/> 将新项添加到适当的索引位置
|
|
419
|
+
*
|
|
420
|
+
* <en/> Add the new item to the appropriate index
|
|
421
|
+
* @param item - <zh/> 新项 | <en/> new item
|
|
422
|
+
*/
|
|
423
|
+
public add(item: Item) {
|
|
424
|
+
// 已经存在,先移除
|
|
425
|
+
// If exists, remove it
|
|
426
|
+
delete this.map[item.id];
|
|
427
|
+
|
|
428
|
+
const length = this.arr.length;
|
|
429
|
+
// 如果为空或者最后一个元素小于当前元素,直接添加到最后
|
|
430
|
+
// If empty or the last element is less than the current element, add to the end
|
|
431
|
+
if (!length || this.arr[length - 1].value < item.value) {
|
|
432
|
+
this.arr.push(item);
|
|
433
|
+
this.map[item.id] = true;
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// 按照升序排列,找到合适的位置插入
|
|
438
|
+
// Find the appropriate position to insert in ascending order
|
|
439
|
+
this._innerAdd(item, length);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
public remove(id: string) {
|
|
443
|
+
if (!this.map[id]) return;
|
|
444
|
+
delete this.map[id];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private _clearAndGetMinId() {
|
|
448
|
+
let res;
|
|
449
|
+
for (let i = this.arr.length - 1; i >= 0; i--) {
|
|
450
|
+
if (this.map[this.arr[i].id]) res = this.arr[i].id;
|
|
451
|
+
else this.arr.splice(i, 1);
|
|
452
|
+
}
|
|
453
|
+
return res;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private _findFirstId() {
|
|
457
|
+
while (this.arr.length) {
|
|
458
|
+
const first = this.arr.shift()!;
|
|
459
|
+
if (this.map[first.id]) return first.id;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
public minId(clear: boolean) {
|
|
464
|
+
if (clear) {
|
|
465
|
+
return this._clearAndGetMinId();
|
|
466
|
+
} else {
|
|
467
|
+
return this._findFirstId();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
package/src/utils/text.ts
CHANGED
package/src/utils/visibility.ts
CHANGED
|
@@ -8,6 +8,7 @@ const ORIGINAL_MAP = new WeakMap<DisplayObject, BaseStyleProps['visibility']>();
|
|
|
8
8
|
* <en/> Set the visibility of the shape instance
|
|
9
9
|
* @param shape - <zh/> 图形实例 | <en/> shape instance
|
|
10
10
|
* @param value - <zh/> 可见性 | <en/> visibility
|
|
11
|
+
* @param inherited - <zh/> 是否是来自继承样式 | <en/> Whether it is from inherited styles
|
|
11
12
|
* @param filter - <zh/> 筛选出需要设置可见性的图形 | <en/> Filter out the shapes that need to set visibility
|
|
12
13
|
* @remarks
|
|
13
14
|
* <zh/> 在设置 enableCSSParsing 为 false 的情况下,复合图形无法继承父属性,因此需要对所有子图形应用相同的可见性
|
|
@@ -17,6 +18,7 @@ const ORIGINAL_MAP = new WeakMap<DisplayObject, BaseStyleProps['visibility']>();
|
|
|
17
18
|
export function setVisibility(
|
|
18
19
|
shape: DisplayObject,
|
|
19
20
|
value: BaseStyleProps['visibility'],
|
|
21
|
+
inherited = false,
|
|
20
22
|
filter?: (shape: DisplayObject) => boolean,
|
|
21
23
|
) {
|
|
22
24
|
if (value === undefined) return;
|
|
@@ -26,7 +28,7 @@ export function setVisibility(
|
|
|
26
28
|
|
|
27
29
|
if (filter && !filter(current)) return walk();
|
|
28
30
|
|
|
29
|
-
if (current === shape) {
|
|
31
|
+
if (!inherited && current === shape) {
|
|
30
32
|
shape.style.visibility = value;
|
|
31
33
|
ORIGINAL_MAP.delete(shape);
|
|
32
34
|
walk(value);
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.0.
|
|
1
|
+
export const version = '5.0.16';
|