@antv/g6 5.0.15 → 5.0.17

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.
Files changed (196) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/drag-canvas.d.ts +13 -0
  3. package/dist/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  4. package/dist/lib/behaviors/scroll-canvas.d.ts +7 -0
  5. package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
  6. package/dist/lib/constants/registry.d.ts +7 -1
  7. package/dist/lib/elements/edges/base-edge.d.ts +2 -2
  8. package/dist/lib/elements/edges/polyline.d.ts +4 -20
  9. package/dist/lib/elements/shapes/base-shape.d.ts +51 -2
  10. package/dist/lib/exports.d.ts +3 -2
  11. package/dist/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  12. package/dist/lib/plugins/index.d.ts +4 -0
  13. package/dist/lib/plugins/snapline/index.d.ts +95 -0
  14. package/dist/lib/registry/types.d.ts +4 -0
  15. package/dist/lib/runtime/element.d.ts +1 -0
  16. package/dist/lib/runtime/plugin.d.ts +1 -1
  17. package/dist/lib/runtime/viewport.d.ts +8 -0
  18. package/dist/lib/types/index.d.ts +1 -0
  19. package/dist/lib/types/router.d.ts +103 -0
  20. package/dist/lib/utils/bbox.d.ts +8 -0
  21. package/dist/lib/utils/router/orth.d.ts +8 -8
  22. package/dist/lib/utils/router/shortest-path.d.ts +57 -0
  23. package/dist/lib/utils/text.d.ts +1 -1
  24. package/dist/lib/utils/visibility.d.ts +2 -1
  25. package/dist/lib/version.d.ts +1 -1
  26. package/esm/behaviors/brush-select.js +11 -6
  27. package/esm/behaviors/brush-select.js.map +1 -1
  28. package/esm/behaviors/drag-canvas.d.ts +13 -0
  29. package/esm/behaviors/drag-canvas.js +10 -1
  30. package/esm/behaviors/drag-canvas.js.map +1 -1
  31. package/esm/behaviors/optimize-viewport-transform.d.ts +0 -1
  32. package/esm/behaviors/optimize-viewport-transform.js +5 -4
  33. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  34. package/esm/behaviors/scroll-canvas.d.ts +7 -0
  35. package/esm/behaviors/scroll-canvas.js +3 -1
  36. package/esm/behaviors/scroll-canvas.js.map +1 -1
  37. package/esm/behaviors/zoom-canvas.d.ts +7 -0
  38. package/esm/behaviors/zoom-canvas.js +3 -1
  39. package/esm/behaviors/zoom-canvas.js.map +1 -1
  40. package/esm/constants/registry.d.ts +7 -1
  41. package/esm/constants/registry.js +6 -0
  42. package/esm/constants/registry.js.map +1 -1
  43. package/esm/elements/edges/base-edge.d.ts +2 -2
  44. package/esm/elements/edges/base-edge.js +10 -4
  45. package/esm/elements/edges/base-edge.js.map +1 -1
  46. package/esm/elements/edges/polyline.d.ts +4 -20
  47. package/esm/elements/edges/polyline.js +32 -33
  48. package/esm/elements/edges/polyline.js.map +1 -1
  49. package/esm/elements/shapes/base-shape.d.ts +51 -2
  50. package/esm/elements/shapes/base-shape.js +23 -5
  51. package/esm/elements/shapes/base-shape.js.map +1 -1
  52. package/esm/exports.d.ts +3 -2
  53. package/esm/exports.js +1 -1
  54. package/esm/exports.js.map +1 -1
  55. package/esm/plugins/edge-filter-lens/index.d.ts +125 -0
  56. package/esm/plugins/edge-filter-lens/index.js +236 -0
  57. package/esm/plugins/edge-filter-lens/index.js.map +1 -0
  58. package/esm/plugins/index.d.ts +4 -0
  59. package/esm/plugins/index.js +2 -0
  60. package/esm/plugins/index.js.map +1 -1
  61. package/esm/plugins/snapline/index.d.ts +95 -0
  62. package/esm/plugins/snapline/index.js +268 -0
  63. package/esm/plugins/snapline/index.js.map +1 -0
  64. package/esm/registry/build-in.js +21 -2
  65. package/esm/registry/build-in.js.map +1 -1
  66. package/esm/registry/store.js +1 -0
  67. package/esm/registry/store.js.map +1 -1
  68. package/esm/registry/types.d.ts +4 -0
  69. package/esm/runtime/element.d.ts +1 -0
  70. package/esm/runtime/element.js +10 -4
  71. package/esm/runtime/element.js.map +1 -1
  72. package/esm/runtime/plugin.d.ts +1 -1
  73. package/esm/runtime/plugin.js +8 -1
  74. package/esm/runtime/plugin.js.map +1 -1
  75. package/esm/runtime/viewport.d.ts +8 -0
  76. package/esm/runtime/viewport.js +35 -2
  77. package/esm/runtime/viewport.js.map +1 -1
  78. package/esm/types/index.d.ts +1 -0
  79. package/esm/types/router.d.ts +103 -0
  80. package/esm/types/router.js +2 -0
  81. package/esm/types/router.js.map +1 -0
  82. package/esm/utils/bbox.d.ts +8 -0
  83. package/esm/utils/bbox.js +19 -0
  84. package/esm/utils/bbox.js.map +1 -1
  85. package/esm/utils/router/orth.d.ts +8 -8
  86. package/esm/utils/router/orth.js +13 -7
  87. package/esm/utils/router/orth.js.map +1 -1
  88. package/esm/utils/router/shortest-path.d.ts +57 -0
  89. package/esm/utils/router/shortest-path.js +387 -0
  90. package/esm/utils/router/shortest-path.js.map +1 -0
  91. package/esm/utils/text.d.ts +1 -1
  92. package/esm/utils/visibility.d.ts +2 -1
  93. package/esm/utils/visibility.js +3 -2
  94. package/esm/utils/visibility.js.map +1 -1
  95. package/esm/version.d.ts +1 -1
  96. package/esm/version.js +1 -1
  97. package/lib/behaviors/brush-select.js +11 -6
  98. package/lib/behaviors/brush-select.js.map +1 -1
  99. package/lib/behaviors/drag-canvas.d.ts +13 -0
  100. package/lib/behaviors/drag-canvas.js +10 -1
  101. package/lib/behaviors/drag-canvas.js.map +1 -1
  102. package/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  103. package/lib/behaviors/optimize-viewport-transform.js +5 -4
  104. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  105. package/lib/behaviors/scroll-canvas.d.ts +7 -0
  106. package/lib/behaviors/scroll-canvas.js +3 -1
  107. package/lib/behaviors/scroll-canvas.js.map +1 -1
  108. package/lib/behaviors/zoom-canvas.d.ts +7 -0
  109. package/lib/behaviors/zoom-canvas.js +3 -1
  110. package/lib/behaviors/zoom-canvas.js.map +1 -1
  111. package/lib/constants/registry.d.ts +7 -1
  112. package/lib/constants/registry.js +6 -0
  113. package/lib/constants/registry.js.map +1 -1
  114. package/lib/elements/edges/base-edge.d.ts +2 -2
  115. package/lib/elements/edges/base-edge.js +9 -3
  116. package/lib/elements/edges/base-edge.js.map +1 -1
  117. package/lib/elements/edges/polyline.d.ts +4 -20
  118. package/lib/elements/edges/polyline.js +32 -33
  119. package/lib/elements/edges/polyline.js.map +1 -1
  120. package/lib/elements/shapes/base-shape.d.ts +51 -2
  121. package/lib/elements/shapes/base-shape.js +23 -5
  122. package/lib/elements/shapes/base-shape.js.map +1 -1
  123. package/lib/exports.d.ts +3 -2
  124. package/lib/exports.js +3 -1
  125. package/lib/exports.js.map +1 -1
  126. package/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  127. package/lib/plugins/edge-filter-lens/index.js +240 -0
  128. package/lib/plugins/edge-filter-lens/index.js.map +1 -0
  129. package/lib/plugins/index.d.ts +4 -0
  130. package/lib/plugins/index.js +5 -1
  131. package/lib/plugins/index.js.map +1 -1
  132. package/lib/plugins/snapline/index.d.ts +95 -0
  133. package/lib/plugins/snapline/index.js +272 -0
  134. package/lib/plugins/snapline/index.js.map +1 -0
  135. package/lib/registry/build-in.js +20 -1
  136. package/lib/registry/build-in.js.map +1 -1
  137. package/lib/registry/store.js +1 -0
  138. package/lib/registry/store.js.map +1 -1
  139. package/lib/registry/types.d.ts +4 -0
  140. package/lib/runtime/element.d.ts +1 -0
  141. package/lib/runtime/element.js +9 -3
  142. package/lib/runtime/element.js.map +1 -1
  143. package/lib/runtime/plugin.d.ts +1 -1
  144. package/lib/runtime/plugin.js +8 -1
  145. package/lib/runtime/plugin.js.map +1 -1
  146. package/lib/runtime/viewport.d.ts +8 -0
  147. package/lib/runtime/viewport.js +34 -1
  148. package/lib/runtime/viewport.js.map +1 -1
  149. package/lib/types/index.d.ts +1 -0
  150. package/lib/types/router.d.ts +103 -0
  151. package/lib/types/router.js +3 -0
  152. package/lib/types/router.js.map +1 -0
  153. package/lib/utils/bbox.d.ts +8 -0
  154. package/lib/utils/bbox.js +20 -0
  155. package/lib/utils/bbox.js.map +1 -1
  156. package/lib/utils/router/orth.d.ts +8 -8
  157. package/lib/utils/router/orth.js +13 -7
  158. package/lib/utils/router/orth.js.map +1 -1
  159. package/lib/utils/router/shortest-path.d.ts +57 -0
  160. package/lib/utils/router/shortest-path.js +394 -0
  161. package/lib/utils/router/shortest-path.js.map +1 -0
  162. package/lib/utils/text.d.ts +1 -1
  163. package/lib/utils/visibility.d.ts +2 -1
  164. package/lib/utils/visibility.js +3 -2
  165. package/lib/utils/visibility.js.map +1 -1
  166. package/lib/version.d.ts +1 -1
  167. package/lib/version.js +1 -1
  168. package/package.json +1 -1
  169. package/src/behaviors/brush-select.ts +12 -5
  170. package/src/behaviors/drag-canvas.ts +24 -1
  171. package/src/behaviors/optimize-viewport-transform.ts +4 -9
  172. package/src/behaviors/scroll-canvas.ts +9 -1
  173. package/src/behaviors/zoom-canvas.ts +9 -1
  174. package/src/constants/registry.ts +6 -0
  175. package/src/elements/edges/base-edge.ts +19 -5
  176. package/src/elements/edges/polyline.ts +36 -65
  177. package/src/elements/shapes/base-shape.ts +76 -6
  178. package/src/exports.ts +5 -0
  179. package/src/plugins/edge-filter-lens/index.ts +391 -0
  180. package/src/plugins/grid-line.ts +1 -1
  181. package/src/plugins/index.ts +4 -0
  182. package/src/plugins/snapline/index.ts +383 -0
  183. package/src/registry/build-in.ts +33 -1
  184. package/src/registry/store.ts +1 -0
  185. package/src/registry/types.ts +2 -0
  186. package/src/runtime/element.ts +11 -4
  187. package/src/runtime/plugin.ts +8 -1
  188. package/src/runtime/viewport.ts +42 -3
  189. package/src/types/index.ts +1 -0
  190. package/src/types/router.ts +107 -0
  191. package/src/utils/bbox.ts +24 -0
  192. package/src/utils/router/orth.ts +19 -10
  193. package/src/utils/router/shortest-path.ts +470 -0
  194. package/src/utils/text.ts +1 -1
  195. package/src/utils/visibility.ts +3 -1
  196. package/src/version.ts +1 -1
@@ -0,0 +1,107 @@
1
+ import type { Padding } from './padding';
2
+ import type { CardinalPlacement } from './placement';
3
+ import { Point } from './point';
4
+
5
+ export type Direction = CardinalPlacement;
6
+
7
+ export type PolylineRouter = false | OrthRouter | ShortestPathRouter;
8
+
9
+ export interface OrthRouter extends OrthRouterOptions {
10
+ /**
11
+ * <zh/> 正交路由,通过在路径上添加额外的控制点,使得边的每一段都保持水平或垂直
12
+ *
13
+ * <en/> Orthogonal routing that adds additional control points on the path to ensure each segment of the edge horizontal or vertical
14
+ * @remarks
15
+ * <zh/> 采用基于节点的相对位置和专家经验得出的寻径算法来模糊计算控制点,非最优解但计算速度快。该路由支持 `controlPoints` 来作为额外的控制点,但不支持自动避障。
16
+ *
17
+ * <en/> It uses a pathfinding algorithm based on the relative position of the nodes and expert experience to calculate the control points, which is not the optimal solution but is fast to calculate. This routing supports `controlPoints` as additional control points, but does not support automatic obstacle avoidance.
18
+ */
19
+ type: 'orth';
20
+ }
21
+
22
+ export interface ShortestPathRouter extends ShortestPathRouterOptions {
23
+ /**
24
+ * <zh/> 最短路径路由,是正交路由 `'orth'` 的智能版本。该路由由水平或垂直的正交线段组成。采用 A* 算法计算最短路径,并支持自动避开路径上的其他节点(障碍)
25
+ *
26
+ * <en/> The shortest path routing is an intelligent version of the orthogonal routing `'orth'`. The routing consists of horizontal or vertical orthogonal line segments. It uses the A* algorithm to calculate the shortest path and supports automatic avoidance of other nodes (obstacles) on the path.
27
+ */
28
+ type: 'shortest-path';
29
+ }
30
+
31
+ export type RouterOptions = OrthRouterOptions | ShortestPathRouterOptions;
32
+
33
+ export interface OrthRouterOptions {
34
+ /**
35
+ * <zh/> 指定节点连接点与转角的最小距离
36
+ *
37
+ * <en/> The minimum distance between the node connection point and the corner
38
+ */
39
+ padding?: Padding;
40
+ }
41
+
42
+ export interface ShortestPathRouterOptions {
43
+ /**
44
+ * <zh/> 节点锚点与转角的最小距离
45
+ *
46
+ * <en/> The minimum distance between the node anchor point and the corner
47
+ */
48
+ offset?: Padding;
49
+ /**
50
+ * <zh/> grid 格子大小
51
+ *
52
+ * <en/> grid size
53
+ */
54
+ gridSize?: number;
55
+ /**
56
+ * <zh/> 支持的最大旋转角度(弧度)
57
+ *
58
+ * <en/> Maximum allowable rotation angle (radian)
59
+ */
60
+ maxAllowedDirectionChange?: number;
61
+ /**
62
+ * <zh/> 节点的可能起始方向
63
+ *
64
+ * <en/> Possible starting directions from a node
65
+ */
66
+ startDirections?: Direction[];
67
+ /**
68
+ * <zh/> 节点的可能结束方向
69
+ *
70
+ * <en/> Possible ending directions from a node
71
+ */
72
+ endDirections?: Direction[];
73
+ /**
74
+ * <zh/> 指定可移动的方向
75
+ *
76
+ * <en/> Allowed edge directions
77
+ */
78
+ directionMap?: {
79
+ [key in Direction]: { stepX: number; stepY: number };
80
+ };
81
+ /**
82
+ * <zh/> 表示在路径搜索过程中某些路径的额外代价。key 为弧度值,value 为代价
83
+ *
84
+ * <en/> Penalties for direction changes. Key is the radian value, value is the penalty
85
+ */
86
+ penalties?: {
87
+ [key: string]: number;
88
+ };
89
+ /**
90
+ * <zh/> 指定计算两点之间距离的函数
91
+ *
92
+ * <en/> Function to calculate the distance between two points
93
+ */
94
+ distFunc?: (p1: Point, p2: Point) => number;
95
+ /**
96
+ * <zh/> 最大迭代次数
97
+ *
98
+ * <en/> Maximum loops
99
+ */
100
+ maximumLoops?: number;
101
+ /**
102
+ * <zh/> 是否开启避障
103
+ *
104
+ * <en/> Whether to enable obstacle avoidance while computing the path
105
+ */
106
+ enableObstacleAvoidance?: boolean;
107
+ }
package/src/utils/bbox.ts CHANGED
@@ -247,3 +247,27 @@ export function getIncircleRadius(bbox: AABB, direction: TriangleDirection): num
247
247
  // 三角形的内切圆半径
248
248
  return (h ** 2 - (Math.sqrt((w / 2) ** 2 + h ** 2) - w / 2) ** 2) / (2 * h);
249
249
  }
250
+
251
+ /**
252
+ * <zh/> 获取包围盒的四条边,顺序依次为上、右、下、左
253
+ *
254
+ * <en/> Get the four segments of the bounding box, in order from top, right, bottom, left
255
+ * @param bbox - <zh/> 包围盒 | <en/> Bounding box
256
+ * @returns <zh/> 包围盒的四条边 | <en/> The four segments of the bounding box
257
+ */
258
+ export function getBBoxSegments(bbox: AABB): [Point, Point][] {
259
+ const {
260
+ min: [minX, minY],
261
+ max: [maxX, maxY],
262
+ } = bbox;
263
+ const topLeftCorner: Point = [minX, maxY];
264
+ const topRightCorner: Point = [maxX, maxY];
265
+ const bottomRightCorner: Point = [maxX, minY];
266
+ const bottomLeftCorner: Point = [minX, minY];
267
+
268
+ const top = [topLeftCorner, topRightCorner];
269
+ const right = [topRightCorner, bottomRightCorner];
270
+ const bottom = [bottomRightCorner, bottomLeftCorner];
271
+ const left = [bottomLeftCorner, topLeftCorner];
272
+ return [top, right, bottom, left] as [Point, Point][];
273
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AABB } from '@antv/g';
2
2
  import { difference, isEqual } from '@antv/util';
3
- import type { Padding, Point } from '../../types';
3
+ import type { Node, OrthRouterOptions, Point } from '../../types';
4
4
  import {
5
5
  getBBoxHeight,
6
6
  getBBoxWidth,
@@ -21,26 +21,35 @@ type Route = {
21
21
  direction: Direction;
22
22
  };
23
23
 
24
+ const defaultOptions: OrthRouterOptions = {
25
+ padding: 10,
26
+ };
27
+
24
28
  /**
25
- * <zh/> 获取两点之间的直角线段路径
29
+ * <zh/> 获取两点之间的正交线段路径
26
30
  *
27
- * <en/> Get the orthogonal line segment path between two points
31
+ * <en/> Get orthogonal line segments between two points
28
32
  * @param sourcePoint - <zh/> 起始点 | <en/> start point
29
33
  * @param targetPoint - <zh/> 终止点 | <en/> end point
30
- * @param sourceBBox - <zh/> 起始节点的包围盒 | <en/> bounding box of the start node
31
- * @param targetBBox - <zh/> 终止节点的包围盒 | <en/> bounding box of the end node
34
+ * @param sourceNode - <zh/> 起始节点 | <en/> source node
35
+ * @param targetNode - <zh/> 终止节点 | <en/> target node
32
36
  * @param controlPoints - <zh/> 控制点 | <en/> control points
33
- * @param padding - <zh/> 内边距 | <en/> padding
34
- * @returns <zh/> 直角线段路径 | <en/> orthogonal line segment path
37
+ * @param options - <zh/> 配置项 | <en/> options
38
+ * @returns <zh/> 路径点集 | <en/> vertices
35
39
  */
36
40
  export function orth(
37
41
  sourcePoint: Point,
38
42
  targetPoint: Point,
39
- sourceBBox: AABB,
40
- targetBBox: AABB,
43
+ sourceNode: Node,
44
+ targetNode: Node,
41
45
  controlPoints: Point[],
42
- padding: Padding,
46
+ options: OrthRouterOptions,
43
47
  ) {
48
+ const { padding } = Object.assign(defaultOptions, options);
49
+
50
+ const sourceBBox = getNodeBBox(sourceNode, padding);
51
+ const targetBBox = getNodeBBox(targetNode, padding);
52
+
44
53
  const points: Point[] = [sourcePoint, ...controlPoints, targetPoint];
45
54
 
46
55
  // direction of previous route segment
@@ -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
@@ -1,5 +1,5 @@
1
1
  import { AABB } from '@antv/g';
2
- import { Point } from '../types';
2
+ import type { Point } from '../types';
3
3
  import { distance } from './vector';
4
4
 
5
5
  /**
@@ -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.15';
1
+ export const version = '5.0.17';