@antv/path-util 2.0.13 → 3.0.1-alpha.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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/esm/catmull-rom-2-bezier.d.ts +10 -1
  3. package/esm/catmull-rom-2-bezier.js +108 -57
  4. package/esm/catmull-rom-2-bezier.js.map +1 -1
  5. package/esm/fill-path-by-diff.js +2 -2
  6. package/esm/fill-path-by-diff.js.map +1 -1
  7. package/esm/get-arc-params.js +4 -2
  8. package/esm/get-arc-params.js.map +1 -1
  9. package/esm/is-polygons-intersect.js +8 -8
  10. package/esm/is-polygons-intersect.js.map +1 -1
  11. package/esm/parse-path-string.js +1 -1
  12. package/esm/parse-path-string.js.map +1 -1
  13. package/esm/parse-path.js +4 -3
  14. package/esm/parse-path.js.map +1 -1
  15. package/esm/path-intersection.js +19 -14
  16. package/esm/path-intersection.js.map +1 -1
  17. package/esm/process/arc-2-cubic.js +2 -2
  18. package/esm/process/arc-2-cubic.js.map +1 -1
  19. package/lib/catmull-rom-2-bezier.d.ts +10 -1
  20. package/lib/catmull-rom-2-bezier.js +107 -57
  21. package/lib/catmull-rom-2-bezier.js.map +1 -1
  22. package/lib/fill-path-by-diff.js +3 -3
  23. package/lib/fill-path-by-diff.js.map +1 -1
  24. package/lib/get-arc-params.js +4 -2
  25. package/lib/get-arc-params.js.map +1 -1
  26. package/lib/is-polygons-intersect.js +12 -12
  27. package/lib/is-polygons-intersect.js.map +1 -1
  28. package/lib/parse-path-string.js +2 -2
  29. package/lib/parse-path-string.js.map +1 -1
  30. package/lib/parse-path.js +8 -7
  31. package/lib/parse-path.js.map +1 -1
  32. package/lib/path-intersection.js +20 -15
  33. package/lib/path-intersection.js.map +1 -1
  34. package/lib/process/arc-2-cubic.js +2 -2
  35. package/lib/process/arc-2-cubic.js.map +1 -1
  36. package/package.json +6 -3
  37. package/src/catmull-rom-2-bezier.ts +137 -55
  38. package/src/fill-path-by-diff.ts +3 -5
  39. package/src/get-arc-params.ts +9 -7
  40. package/src/is-polygons-intersect.ts +18 -19
  41. package/src/parse-path-string.ts +11 -7
  42. package/src/parse-path.ts +5 -4
  43. package/src/path-intersection.ts +60 -33
  44. package/src/process/arc-2-cubic.ts +2 -2
@@ -1,4 +1,4 @@
1
- import { isArray } from '@antv/util';
1
+ import { isArray } from 'lodash-es';
2
2
 
3
3
  import rectPath from './rect-path';
4
4
  import path2Curve from './path-2-curve';
@@ -9,16 +9,27 @@ const base3 = function (t: number, p1: number, p2: number, p3: number, p4: numbe
9
9
  return t * t2 - 3 * p1 + 3 * p2;
10
10
  };
11
11
 
12
- const bezlen = function (x1: number, y1: number, x2: number, y2: number,
13
- x3: number, y3: number, x4: number, y4: number, z: number): number {
12
+ const bezlen = function (
13
+ x1: number,
14
+ y1: number,
15
+ x2: number,
16
+ y2: number,
17
+ x3: number,
18
+ y3: number,
19
+ x4: number,
20
+ y4: number,
21
+ z: number
22
+ ): number {
14
23
  if (z === null) {
15
24
  z = 1;
16
25
  }
17
26
  z = z > 1 ? 1 : z < 0 ? 0 : z;
18
27
  const z2 = z / 2;
19
28
  const n = 12;
20
- const Tvalues = [ -0.1252, 0.1252, -0.3678, 0.3678, -0.5873, 0.5873, -0.7699, 0.7699, -0.9041, 0.9041, -0.9816, 0.9816 ];
21
- const Cvalues = [ 0.2491, 0.2491, 0.2335, 0.2335, 0.2032, 0.2032, 0.1601, 0.1601, 0.1069, 0.1069, 0.0472, 0.0472 ];
29
+ const Tvalues = [
30
+ -0.1252, 0.1252, -0.3678, 0.3678, -0.5873, 0.5873, -0.7699, 0.7699, -0.9041, 0.9041, -0.9816, 0.9816,
31
+ ];
32
+ const Cvalues = [0.2491, 0.2491, 0.2335, 0.2335, 0.2032, 0.2032, 0.1601, 0.1601, 0.1069, 0.1069, 0.0472, 0.0472];
22
33
  let sum = 0;
23
34
  for (let i = 0; i < n; i++) {
24
35
  const ct = z2 * Tvalues[i] + z2;
@@ -40,13 +51,18 @@ export interface BoundPoint {
40
51
  max: Point;
41
52
  }
42
53
 
43
- const curveDim = function (x0: number, y0: number, x1: number, y1: number,
44
- x2: number, y2: number, x3: number, y3: number): BoundPoint {
54
+ const curveDim = function (
55
+ x0: number,
56
+ y0: number,
57
+ x1: number,
58
+ y1: number,
59
+ x2: number,
60
+ y2: number,
61
+ x3: number,
62
+ y3: number
63
+ ): BoundPoint {
45
64
  const tvalues = [];
46
- const bounds = [
47
- [],
48
- [],
49
- ];
65
+ const bounds = [[], []];
50
66
  let a;
51
67
  let b;
52
68
  let c;
@@ -93,8 +109,8 @@ const curveDim = function (x0: number, y0: number, x1: number, y1: number,
93
109
  while (j--) {
94
110
  t = tvalues[j];
95
111
  mt = 1 - t;
96
- bounds[0][j] = (mt * mt * mt * x0) + (3 * mt * mt * t * x1) + (3 * mt * t * t * x2) + (t * t * t * x3);
97
- bounds[1][j] = (mt * mt * mt * y0) + (3 * mt * mt * t * y1) + (3 * mt * t * t * y2) + (t * t * t * y3);
112
+ bounds[0][j] = mt * mt * mt * x0 + 3 * mt * mt * t * x1 + 3 * mt * t * t * x2 + t * t * t * x3;
113
+ bounds[1][j] = mt * mt * mt * y0 + 3 * mt * mt * t * y1 + 3 * mt * t * t * y2 + t * t * t * y3;
98
114
  }
99
115
 
100
116
  bounds[0][jlen] = x0;
@@ -115,8 +131,16 @@ const curveDim = function (x0: number, y0: number, x1: number, y1: number,
115
131
  };
116
132
  };
117
133
 
118
- const intersect = function (x1: number, y1: number, x2: number, y2: number,
119
- x3: number, y3: number, x4: number, y4: number): Point {
134
+ const intersect = function (
135
+ x1: number,
136
+ y1: number,
137
+ x2: number,
138
+ y2: number,
139
+ x3: number,
140
+ y3: number,
141
+ x4: number,
142
+ y4: number
143
+ ): Point {
120
144
  if (
121
145
  Math.max(x1, x2) < Math.min(x3, x4) ||
122
146
  Math.min(x1, x2) > Math.max(x3, x4) ||
@@ -155,10 +179,7 @@ const intersect = function (x1: number, y1: number, x2: number, y2: number,
155
179
  };
156
180
 
157
181
  const isPointInsideBBox = function (bbox, x, y) {
158
- return x >= bbox.x &&
159
- x <= bbox.x + bbox.width &&
160
- y >= bbox.y &&
161
- y <= bbox.y + bbox.height;
182
+ return x >= bbox.x && x <= bbox.x + bbox.width && y >= bbox.y && y <= bbox.y + bbox.height;
162
183
  };
163
184
 
164
185
  const box = function (x, y, width, height) {
@@ -186,7 +207,7 @@ const box = function (x, y, width, height) {
186
207
  r2: Math.max(width, height) / 2,
187
208
  r0: Math.sqrt(width * width + height * height) / 2,
188
209
  path: rectPath(x, y, width, height),
189
- vb: [ x, y, width, height ].join(' '),
210
+ vb: [x, y, width, height].join(' '),
190
211
  };
191
212
  };
192
213
 
@@ -195,20 +216,26 @@ const isBBoxIntersect = function (bbox1, bbox2) {
195
216
  bbox1 = box(bbox1);
196
217
  // @ts-ignore
197
218
  bbox2 = box(bbox2);
198
- return isPointInsideBBox(bbox2, bbox1.x, bbox1.y) || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y) || isPointInsideBBox(bbox2, bbox1.x, bbox1.y2) || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y2) || isPointInsideBBox(bbox1, bbox2.x, bbox2.y) || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y) || isPointInsideBBox(bbox1, bbox2.x, bbox2.y2) || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y2) || (bbox1.x < bbox2.x2 && bbox1.x > bbox2.x || bbox2.x < bbox1.x2 && bbox2.x > bbox1.x) && (bbox1.y < bbox2.y2 && bbox1.y > bbox2.y || bbox2.y < bbox1.y2 && bbox2.y > bbox1.y);
219
+ return (
220
+ isPointInsideBBox(bbox2, bbox1.x, bbox1.y) ||
221
+ isPointInsideBBox(bbox2, bbox1.x2, bbox1.y) ||
222
+ isPointInsideBBox(bbox2, bbox1.x, bbox1.y2) ||
223
+ isPointInsideBBox(bbox2, bbox1.x2, bbox1.y2) ||
224
+ isPointInsideBBox(bbox1, bbox2.x, bbox2.y) ||
225
+ isPointInsideBBox(bbox1, bbox2.x2, bbox2.y) ||
226
+ isPointInsideBBox(bbox1, bbox2.x, bbox2.y2) ||
227
+ isPointInsideBBox(bbox1, bbox2.x2, bbox2.y2) ||
228
+ (((bbox1.x < bbox2.x2 && bbox1.x > bbox2.x) || (bbox2.x < bbox1.x2 && bbox2.x > bbox1.x)) &&
229
+ ((bbox1.y < bbox2.y2 && bbox1.y > bbox2.y) || (bbox2.y < bbox1.y2 && bbox2.y > bbox1.y)))
230
+ );
199
231
  };
200
232
 
201
233
  const bezierBBox = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
202
234
  if (!isArray(p1x)) {
203
- p1x = [ p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y ];
235
+ p1x = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y];
204
236
  }
205
237
  const bbox = curveDim.apply(null, p1x);
206
- return box(
207
- bbox.min.x,
208
- bbox.min.y,
209
- bbox.max.x - bbox.min.x,
210
- bbox.max.y - bbox.min.y,
211
- );
238
+ return box(bbox.min.x, bbox.min.y, bbox.max.x - bbox.min.x, bbox.max.y - bbox.min.y);
212
239
  };
213
240
 
214
241
  const findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
@@ -227,7 +254,7 @@ const findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
227
254
  const ay = t1 * p1y + t * c1y;
228
255
  const cx = t1 * c2x + t * p2x;
229
256
  const cy = t1 * c2y + t * p2y;
230
- const alpha = (90 - Math.atan2(mx - nx, my - ny) * 180 / Math.PI);
257
+ const alpha = 90 - (Math.atan2(mx - nx, my - ny) * 180) / Math.PI;
231
258
  // (mx > nx || my < ny) && (alpha += 180);
232
259
  return {
233
260
  x,
@@ -343,11 +370,11 @@ const interPathHelper = function (path1, path2, justCount) {
343
370
  y1 = y1m = pi[2];
344
371
  } else {
345
372
  if (pi[0] === 'C') {
346
- bez1 = [ x1, y1 ].concat(pi.slice(1));
373
+ bez1 = [x1, y1].concat(pi.slice(1));
347
374
  x1 = bez1[6];
348
375
  y1 = bez1[7];
349
376
  } else {
350
- bez1 = [ x1, y1, x1, y1, x1m, y1m, x1m, y1m ];
377
+ bez1 = [x1, y1, x1, y1, x1m, y1m, x1m, y1m];
351
378
  x1 = x1m;
352
379
  y1 = y1m;
353
380
  }
@@ -358,11 +385,11 @@ const interPathHelper = function (path1, path2, justCount) {
358
385
  y2 = y2m = pj[2];
359
386
  } else {
360
387
  if (pj[0] === 'C') {
361
- bez2 = [ x2, y2 ].concat(pj.slice(1));
388
+ bez2 = [x2, y2].concat(pj.slice(1));
362
389
  x2 = bez2[6];
363
390
  y2 = bez2[7];
364
391
  } else {
365
- bez2 = [ x2, y2, x2, y2, x2m, y2m, x2m, y2m ];
392
+ bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
366
393
  x2 = x2m;
367
394
  y2 = y2m;
368
395
  }
@@ -126,7 +126,7 @@ const arcToBezier = ({
126
126
  const curves = []
127
127
 
128
128
  if (rx === 0 || ry === 0) {
129
- return []
129
+ return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }];
130
130
  }
131
131
 
132
132
  const sinphi = Math.sin(xAxisRotation * TAU / 360)
@@ -136,7 +136,7 @@ const arcToBezier = ({
136
136
  const pyp = -sinphi * (px - cx) / 2 + cosphi * (py - cy) / 2
137
137
 
138
138
  if (pxp === 0 && pyp === 0) {
139
- return []
139
+ return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }];
140
140
  }
141
141
 
142
142
  rx = Math.abs(rx)