@antv/path-util 2.0.15 → 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.
- package/LICENSE +21 -0
- package/esm/catmull-rom-2-bezier.js +1 -1
- package/esm/catmull-rom-2-bezier.js.map +1 -1
- package/esm/fill-path-by-diff.js +2 -2
- package/esm/fill-path-by-diff.js.map +1 -1
- package/esm/get-arc-params.js +4 -2
- package/esm/get-arc-params.js.map +1 -1
- package/esm/is-polygons-intersect.js +8 -8
- package/esm/is-polygons-intersect.js.map +1 -1
- package/esm/parse-path-string.js +1 -1
- package/esm/parse-path-string.js.map +1 -1
- package/esm/parse-path.js +4 -3
- package/esm/parse-path.js.map +1 -1
- package/esm/path-intersection.js +19 -14
- package/esm/path-intersection.js.map +1 -1
- package/esm/process/quad-2-cubic.js +1 -1
- package/esm/process/quad-2-cubic.js.map +1 -1
- package/esm/types.js +0 -1
- package/lib/catmull-rom-2-bezier.js +25 -25
- package/lib/catmull-rom-2-bezier.js.map +1 -1
- package/lib/fill-path-by-diff.js +3 -3
- package/lib/fill-path-by-diff.js.map +1 -1
- package/lib/get-arc-params.js +4 -2
- package/lib/get-arc-params.js.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/is-polygons-intersect.js +15 -15
- package/lib/is-polygons-intersect.js.map +1 -1
- package/lib/parse-path-string.js +2 -2
- package/lib/parse-path-string.js.map +1 -1
- package/lib/parse-path.js +8 -7
- package/lib/parse-path.js.map +1 -1
- package/lib/path-2-absolute.js +1 -1
- package/lib/path-2-absolute.js.map +1 -1
- package/lib/path-2-curve.js +2 -2
- package/lib/path-2-curve.js.map +1 -1
- package/lib/path-2-segments.js +3 -3
- package/lib/path-2-segments.js.map +1 -1
- package/lib/path-intersection.js +23 -18
- package/lib/path-intersection.js.map +1 -1
- package/lib/process/quad-2-cubic.js +1 -1
- package/lib/process/quad-2-cubic.js.map +1 -1
- package/lib/process/segment-2-cubic.js +4 -4
- package/lib/process/segment-2-cubic.js.map +1 -1
- package/package.json +6 -4
- package/src/catmull-rom-2-bezier.ts +30 -30
- package/src/fill-path-by-diff.ts +3 -5
- package/src/get-arc-params.ts +9 -7
- package/src/is-polygons-intersect.ts +18 -19
- package/src/parse-path-string.ts +11 -7
- package/src/parse-path.ts +5 -4
- package/src/path-intersection.ts +60 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antv/path-util",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1-alpha.0",
|
|
4
4
|
"description": "A common util collection for antv projects",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -35,11 +35,13 @@
|
|
|
35
35
|
"g"
|
|
36
36
|
],
|
|
37
37
|
"author": "https://github.com/orgs/antvis/people",
|
|
38
|
-
"license": "
|
|
38
|
+
"license": "MIT",
|
|
39
39
|
"bugs": {
|
|
40
40
|
"url": "https://github.com/antvis/util/issues"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
+
"@types/gl-matrix": "^2.4.5",
|
|
44
|
+
"@types/lodash-es": "^4.17.6",
|
|
43
45
|
"@antv/torch": "^1.0.0",
|
|
44
46
|
"chai": "^4.2.0",
|
|
45
47
|
"less": "^3.9.0",
|
|
@@ -47,8 +49,8 @@
|
|
|
47
49
|
},
|
|
48
50
|
"homepage": "https://github.com/antvis/util#readme",
|
|
49
51
|
"dependencies": {
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
+
"gl-matrix": "^3.1.0",
|
|
53
|
+
"lodash-es": "^4.17.21",
|
|
52
54
|
"tslib": "^2.0.3"
|
|
53
55
|
}
|
|
54
56
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { vec2 } from '
|
|
1
|
+
import { vec2 } from 'gl-matrix';
|
|
2
2
|
import type { PathCommand } from './types';
|
|
3
3
|
|
|
4
4
|
type Pos = [number, number];
|
|
@@ -16,11 +16,11 @@ function smoothBezier(points: Pos[], smooth: number, isLoop: boolean, constraint
|
|
|
16
16
|
let cp0: vec2;
|
|
17
17
|
|
|
18
18
|
if (hasConstraint) {
|
|
19
|
-
[
|
|
19
|
+
[min, max] = constraint;
|
|
20
20
|
for (let i = 0, l = points.length; i < l; i += 1) {
|
|
21
21
|
const point = points[i];
|
|
22
|
-
min = vec2.min([
|
|
23
|
-
max = vec2.max([
|
|
22
|
+
min = vec2.min([0, 0], min, point);
|
|
23
|
+
max = vec2.max([0, 0], max, point);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -33,11 +33,11 @@ function smoothBezier(points: Pos[], smooth: number, isLoop: boolean, constraint
|
|
|
33
33
|
cps.push(cp0);
|
|
34
34
|
cps.push(cp1);
|
|
35
35
|
} else {
|
|
36
|
-
const prevIdx = [
|
|
36
|
+
const prevIdx = [i ? i - 1 : len - 1, i - 1][isLoop ? 0 : 1];
|
|
37
37
|
prevPoint = points[prevIdx];
|
|
38
38
|
nextPoint = points[isLoop ? (i + 1) % len : i + 1];
|
|
39
39
|
|
|
40
|
-
let v: vec2 = [
|
|
40
|
+
let v: vec2 = [0, 0];
|
|
41
41
|
v = vec2.sub(v, nextPoint, prevPoint);
|
|
42
42
|
v = vec2.scale(v, v, smooth);
|
|
43
43
|
|
|
@@ -50,35 +50,35 @@ function smoothBezier(points: Pos[], smooth: number, isLoop: boolean, constraint
|
|
|
50
50
|
d1 /= sum;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
let v1 = vec2.scale([
|
|
54
|
-
let v2 = vec2.scale([
|
|
53
|
+
let v1 = vec2.scale([0, 0], v, -d0);
|
|
54
|
+
let v2 = vec2.scale([0, 0], v, d1);
|
|
55
55
|
|
|
56
|
-
cp1 = vec2.add([
|
|
57
|
-
nextCp0 = vec2.add([
|
|
56
|
+
cp1 = vec2.add([0, 0], point, v1);
|
|
57
|
+
nextCp0 = vec2.add([0, 0], point, v2);
|
|
58
58
|
|
|
59
59
|
// 下一个控制点必须在这个点和下一个点之间
|
|
60
|
-
nextCp0 = vec2.min([
|
|
61
|
-
nextCp0 = vec2.max([
|
|
60
|
+
nextCp0 = vec2.min([0, 0], nextCp0, vec2.max([0, 0], nextPoint, point));
|
|
61
|
+
nextCp0 = vec2.max([0, 0], nextCp0, vec2.min([0, 0], nextPoint, point));
|
|
62
62
|
|
|
63
63
|
// 重新计算 cp1 的值
|
|
64
|
-
v1 = vec2.sub([
|
|
65
|
-
v1 = vec2.scale([
|
|
66
|
-
cp1 = vec2.add([
|
|
64
|
+
v1 = vec2.sub([0, 0], nextCp0, point);
|
|
65
|
+
v1 = vec2.scale([0, 0], v1, -d0 / d1);
|
|
66
|
+
cp1 = vec2.add([0, 0], point, v1);
|
|
67
67
|
|
|
68
68
|
// 上一个控制点必须要在上一个点和这一个点之间
|
|
69
|
-
cp1 = vec2.min([
|
|
70
|
-
cp1 = vec2.max([
|
|
69
|
+
cp1 = vec2.min([0, 0], cp1, vec2.max([0, 0], prevPoint, point));
|
|
70
|
+
cp1 = vec2.max([0, 0], cp1, vec2.min([0, 0], prevPoint, point));
|
|
71
71
|
|
|
72
72
|
// 重新计算 nextCp0 的值
|
|
73
|
-
v2 = vec2.sub([
|
|
74
|
-
v2 = vec2.scale([
|
|
75
|
-
nextCp0 = vec2.add([
|
|
73
|
+
v2 = vec2.sub([0, 0], point, cp1);
|
|
74
|
+
v2 = vec2.scale([0, 0], v2, d1 / d0);
|
|
75
|
+
nextCp0 = vec2.add([0, 0], point, v2);
|
|
76
76
|
|
|
77
77
|
if (hasConstraint) {
|
|
78
|
-
cp1 = vec2.max([
|
|
79
|
-
cp1 = vec2.min([
|
|
80
|
-
nextCp0 = vec2.max([
|
|
81
|
-
nextCp0 = vec2.min([
|
|
78
|
+
cp1 = vec2.max([0, 0], cp1, min);
|
|
79
|
+
cp1 = vec2.min([0, 0], cp1, max);
|
|
80
|
+
nextCp0 = vec2.max([0, 0], nextCp0, min);
|
|
81
|
+
nextCp0 = vec2.min([0, 0], nextCp0, max);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
cps.push(cp0);
|
|
@@ -104,14 +104,14 @@ function catmullRom2Bezier(
|
|
|
104
104
|
crp: number[],
|
|
105
105
|
z: boolean = false,
|
|
106
106
|
constraint: Pos[] = [
|
|
107
|
-
[
|
|
108
|
-
[
|
|
107
|
+
[0, 0],
|
|
108
|
+
[1, 1],
|
|
109
109
|
]
|
|
110
110
|
): PathCommand[] {
|
|
111
111
|
const isLoop = !!z;
|
|
112
112
|
const pointList: Pos[] = [];
|
|
113
113
|
for (let i = 0, l = crp.length; i < l; i += 2) {
|
|
114
|
-
pointList.push([
|
|
114
|
+
pointList.push([crp[i], crp[i + 1]]);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
const controlPointList = smoothBezier(pointList, 0.4, isLoop, constraint);
|
|
@@ -127,15 +127,15 @@ function catmullRom2Bezier(
|
|
|
127
127
|
cp2 = controlPointList[i * 2 + 1];
|
|
128
128
|
p = pointList[i + 1];
|
|
129
129
|
|
|
130
|
-
d1.push([
|
|
130
|
+
d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]]);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
if (isLoop) {
|
|
134
134
|
cp1 = controlPointList[len];
|
|
135
135
|
cp2 = controlPointList[len + 1];
|
|
136
|
-
[
|
|
136
|
+
[p] = pointList;
|
|
137
137
|
|
|
138
|
-
d1.push([
|
|
138
|
+
d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]]);
|
|
139
139
|
}
|
|
140
140
|
return d1;
|
|
141
141
|
}
|
package/src/fill-path-by-diff.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isEqual } from '
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
2
|
|
|
3
3
|
export interface DiffType {
|
|
4
4
|
type: string;
|
|
@@ -29,8 +29,7 @@ function getMinDiff(del: number, add: number, modify: number): DiffType {
|
|
|
29
29
|
const levenshteinDistance = function (source: string, target: string): DiffType[][] {
|
|
30
30
|
const sourceLen = source.length;
|
|
31
31
|
const targetLen = target.length;
|
|
32
|
-
let sourceSegment,
|
|
33
|
-
targetSegment;
|
|
32
|
+
let sourceSegment, targetSegment;
|
|
34
33
|
let temp = 0;
|
|
35
34
|
if (sourceLen === 0 || targetLen === 0) {
|
|
36
35
|
return null;
|
|
@@ -103,7 +102,7 @@ export default function fillPathByDiff(source: string, target: string) {
|
|
|
103
102
|
// source尾部补齐
|
|
104
103
|
sourceLen = source.length;
|
|
105
104
|
if (sourceLen < targetLen) {
|
|
106
|
-
for (let i = 0; i <
|
|
105
|
+
for (let i = 0; i < targetLen - sourceLen; i++) {
|
|
107
106
|
if (source[sourceLen - 1][0] === 'z' || source[sourceLen - 1][0] === 'Z') {
|
|
108
107
|
// @ts-ignore
|
|
109
108
|
source.splice(sourceLen - 2, 0, source[sourceLen - 2]);
|
|
@@ -111,7 +110,6 @@ export default function fillPathByDiff(source: string, target: string) {
|
|
|
111
110
|
// @ts-ignore
|
|
112
111
|
source.push(source[sourceLen - 1]);
|
|
113
112
|
}
|
|
114
|
-
|
|
115
113
|
}
|
|
116
114
|
}
|
|
117
115
|
return source;
|
package/src/get-arc-params.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
const mod = function (n: number, m: number): number {
|
|
2
|
+
return ((n % m) + m) % m;
|
|
3
|
+
};
|
|
2
4
|
|
|
3
5
|
// 向量长度
|
|
4
6
|
function vMag(v) {
|
|
@@ -29,7 +31,7 @@ export function isSamePoint(point1, point2) {
|
|
|
29
31
|
export default function getArcParams(startPoint, params) {
|
|
30
32
|
let rx = params[1];
|
|
31
33
|
let ry = params[2];
|
|
32
|
-
const xRotation = mod(
|
|
34
|
+
const xRotation = mod((params[3] * Math.PI) / 180, Math.PI * 2);
|
|
33
35
|
const arcFlag = params[4];
|
|
34
36
|
const sweepFlag = params[5];
|
|
35
37
|
// 弧形起点坐标
|
|
@@ -66,11 +68,11 @@ export default function getArcParams(startPoint, params) {
|
|
|
66
68
|
const cy = (y1 + y2) / 2.0 + Math.sin(xRotation) * cxp + Math.cos(xRotation) * cyp;
|
|
67
69
|
|
|
68
70
|
// 起始点的单位向量
|
|
69
|
-
const u = [
|
|
71
|
+
const u = [(xp - cxp) / rx, (yp - cyp) / ry];
|
|
70
72
|
// 终止点的单位向量
|
|
71
|
-
const v = [
|
|
73
|
+
const v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
|
|
72
74
|
// 计算起始点和圆心的连线,与 x 轴正方向的夹角
|
|
73
|
-
const theta = vAngle([
|
|
75
|
+
const theta = vAngle([1, 0], u);
|
|
74
76
|
|
|
75
77
|
// 计算圆弧起始点和终止点与椭圆圆心连线的夹角
|
|
76
78
|
let dTheta = vAngle(u, v);
|
|
@@ -91,8 +93,8 @@ export default function getArcParams(startPoint, params) {
|
|
|
91
93
|
cx,
|
|
92
94
|
cy,
|
|
93
95
|
// 弧形的起点和终点相同时,长轴和短轴的长度按 0 处理
|
|
94
|
-
rx: isSamePoint(startPoint, [
|
|
95
|
-
ry: isSamePoint(startPoint, [
|
|
96
|
+
rx: isSamePoint(startPoint, [x2, y2]) ? 0 : rx,
|
|
97
|
+
ry: isSamePoint(startPoint, [x2, y2]) ? 0 : ry,
|
|
96
98
|
startAngle: theta,
|
|
97
99
|
endAngle: theta + dTheta,
|
|
98
100
|
xRotation,
|
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
|
|
2
1
|
import isPointInPolygon from './point-in-polygon';
|
|
3
2
|
import getLineIntersect from './get-line-intersect';
|
|
4
|
-
import {each} from '
|
|
3
|
+
import { each } from 'lodash-es';
|
|
5
4
|
|
|
6
5
|
function parseToLines(points) {
|
|
7
6
|
const lines = [];
|
|
8
7
|
const count = points.length;
|
|
9
|
-
for(let i = 0; i < count - 1; i++) {
|
|
8
|
+
for (let i = 0; i < count - 1; i++) {
|
|
10
9
|
const point = points[i];
|
|
11
10
|
const next = points[i + 1];
|
|
12
11
|
lines.push({
|
|
13
12
|
from: {
|
|
14
13
|
x: point[0],
|
|
15
|
-
y: point[1]
|
|
14
|
+
y: point[1],
|
|
16
15
|
},
|
|
17
16
|
to: {
|
|
18
17
|
x: next[0],
|
|
19
|
-
y: next[1]
|
|
20
|
-
}
|
|
18
|
+
y: next[1],
|
|
19
|
+
},
|
|
21
20
|
});
|
|
22
21
|
}
|
|
23
22
|
if (lines.length > 1) {
|
|
@@ -26,12 +25,12 @@ function parseToLines(points) {
|
|
|
26
25
|
lines.push({
|
|
27
26
|
from: {
|
|
28
27
|
x: last[0],
|
|
29
|
-
y: last[1]
|
|
28
|
+
y: last[1],
|
|
30
29
|
},
|
|
31
30
|
to: {
|
|
32
31
|
x: first[0],
|
|
33
|
-
y: first[1]
|
|
34
|
-
}
|
|
32
|
+
y: first[1],
|
|
33
|
+
},
|
|
35
34
|
});
|
|
36
35
|
}
|
|
37
36
|
return lines;
|
|
@@ -39,7 +38,7 @@ function parseToLines(points) {
|
|
|
39
38
|
|
|
40
39
|
function lineIntersectPolygon(lines, line) {
|
|
41
40
|
let isIntersect = false;
|
|
42
|
-
each(lines, l => {
|
|
41
|
+
each(lines, (l) => {
|
|
43
42
|
if (getLineIntersect(l.from, l.to, line.from, line.to)) {
|
|
44
43
|
isIntersect = true;
|
|
45
44
|
return false;
|
|
@@ -56,17 +55,17 @@ type BBox = {
|
|
|
56
55
|
};
|
|
57
56
|
|
|
58
57
|
function getBBox(points): BBox {
|
|
59
|
-
const xArr = points.map(p => p[0]);
|
|
60
|
-
const yArr = points.map(p => p[1]);
|
|
58
|
+
const xArr = points.map((p) => p[0]);
|
|
59
|
+
const yArr = points.map((p) => p[1]);
|
|
61
60
|
return {
|
|
62
61
|
minX: Math.min.apply(null, xArr),
|
|
63
62
|
maxX: Math.max.apply(null, xArr),
|
|
64
63
|
minY: Math.min.apply(null, yArr),
|
|
65
|
-
maxY: Math.max.apply(null, yArr)
|
|
64
|
+
maxY: Math.max.apply(null, yArr),
|
|
66
65
|
};
|
|
67
66
|
}
|
|
68
67
|
|
|
69
|
-
function intersectBBox(box1:BBox, box2:BBox) {
|
|
68
|
+
function intersectBBox(box1: BBox, box2: BBox) {
|
|
70
69
|
return !(box2.minX > box1.maxX || box2.maxX < box1.minX || box2.minY > box1.maxY || box2.maxY < box1.minY);
|
|
71
70
|
}
|
|
72
71
|
|
|
@@ -82,10 +81,10 @@ export default function isPolygonsIntersect(points1, points2) {
|
|
|
82
81
|
if (!intersectBBox(bbox1, bbox2)) {
|
|
83
82
|
return false;
|
|
84
83
|
}
|
|
85
|
-
|
|
84
|
+
|
|
86
85
|
let isIn = false;
|
|
87
86
|
// 判定点是否在多边形内部,一旦有一个点在另一个多边形内,则返回
|
|
88
|
-
each(points2, point => {
|
|
87
|
+
each(points2, (point) => {
|
|
89
88
|
if (isPointInPolygon(points1, point[0], point[1])) {
|
|
90
89
|
isIn = true;
|
|
91
90
|
return false;
|
|
@@ -95,7 +94,7 @@ export default function isPolygonsIntersect(points1, points2) {
|
|
|
95
94
|
return true;
|
|
96
95
|
}
|
|
97
96
|
// 两个多边形都需要判定
|
|
98
|
-
each(points1, point => {
|
|
97
|
+
each(points1, (point) => {
|
|
99
98
|
if (isPointInPolygon(points2, point[0], point[1])) {
|
|
100
99
|
isIn = true;
|
|
101
100
|
return false;
|
|
@@ -108,11 +107,11 @@ export default function isPolygonsIntersect(points1, points2) {
|
|
|
108
107
|
const lines1 = parseToLines(points1);
|
|
109
108
|
const lines2 = parseToLines(points2);
|
|
110
109
|
let isIntersect = false;
|
|
111
|
-
each(lines2, line => {
|
|
110
|
+
each(lines2, (line) => {
|
|
112
111
|
if (lineIntersectPolygon(lines1, line)) {
|
|
113
112
|
isIntersect = true;
|
|
114
113
|
return false;
|
|
115
114
|
}
|
|
116
115
|
});
|
|
117
116
|
return isIntersect;
|
|
118
|
-
}
|
|
117
|
+
}
|
package/src/parse-path-string.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { isArray } from '
|
|
1
|
+
import { isArray } from 'lodash-es';
|
|
2
2
|
|
|
3
|
-
const SPACES =
|
|
4
|
-
|
|
3
|
+
const SPACES =
|
|
4
|
+
'\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029';
|
|
5
|
+
const PATH_COMMAND = new RegExp(
|
|
6
|
+
'([a-z])[' + SPACES + ',]*((-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?[' + SPACES + ']*,?[' + SPACES + ']*)+)',
|
|
7
|
+
'ig'
|
|
8
|
+
);
|
|
5
9
|
const PATH_VALUES = new RegExp('(-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?)[' + SPACES + ']*,?[' + SPACES + ']*', 'ig');
|
|
6
10
|
|
|
7
11
|
// Parses given path string into an array of arrays of path segments
|
|
@@ -37,18 +41,18 @@ export default function parsePathString(pathString: string) {
|
|
|
37
41
|
b && params.push(+b);
|
|
38
42
|
});
|
|
39
43
|
if (name === 'm' && params.length > 2) {
|
|
40
|
-
data.push([
|
|
44
|
+
data.push([b].concat(params.splice(0, 2)));
|
|
41
45
|
name = 'l';
|
|
42
46
|
b = b === 'm' ? 'l' : 'L';
|
|
43
47
|
}
|
|
44
48
|
if (name === 'o' && params.length === 1) {
|
|
45
|
-
data.push([
|
|
49
|
+
data.push([b, params[0]]);
|
|
46
50
|
}
|
|
47
51
|
if (name === 'r') {
|
|
48
|
-
data.push([
|
|
52
|
+
data.push([b].concat(params));
|
|
49
53
|
} else {
|
|
50
54
|
while (params.length >= paramCounts[name]) {
|
|
51
|
-
data.push([
|
|
55
|
+
data.push([b].concat(params.splice(0, paramCounts[name])));
|
|
52
56
|
if (!paramCounts[name]) {
|
|
53
57
|
break;
|
|
54
58
|
}
|
package/src/parse-path.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { each, isArray, isString } from '
|
|
1
|
+
import { each, isArray, isString } from 'lodash-es';
|
|
2
2
|
|
|
3
|
-
const regexTags = /[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/
|
|
4
|
-
const regexDot = /[^\s\,]+/
|
|
3
|
+
const regexTags = /[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/gi;
|
|
4
|
+
const regexDot = /[^\s\,]+/gi;
|
|
5
5
|
|
|
6
6
|
function parsePath(p: string): string[] {
|
|
7
|
-
let path = p || [] as string | string[];
|
|
7
|
+
let path = p || ([] as string | string[]);
|
|
8
8
|
if (isArray(path)) {
|
|
9
9
|
return path;
|
|
10
10
|
}
|
|
@@ -23,6 +23,7 @@ function parsePath(p: string): string[] {
|
|
|
23
23
|
}
|
|
24
24
|
// @ts-ignore
|
|
25
25
|
each(item, function (sub, i) {
|
|
26
|
+
// @ts-ignore
|
|
26
27
|
if (!isNaN(sub)) {
|
|
27
28
|
// @ts-ignore
|
|
28
29
|
item[i] = +sub;
|
package/src/path-intersection.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArray } from '
|
|
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 (
|
|
13
|
-
|
|
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 = [
|
|
21
|
-
|
|
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 (
|
|
44
|
-
|
|
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] =
|
|
97
|
-
bounds[1][j] =
|
|
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 (
|
|
119
|
-
|
|
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: [
|
|
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
|
|
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 = [
|
|
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 =
|
|
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 = [
|
|
373
|
+
bez1 = [x1, y1].concat(pi.slice(1));
|
|
347
374
|
x1 = bez1[6];
|
|
348
375
|
y1 = bez1[7];
|
|
349
376
|
} else {
|
|
350
|
-
bez1 = [
|
|
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 = [
|
|
388
|
+
bez2 = [x2, y2].concat(pj.slice(1));
|
|
362
389
|
x2 = bez2[6];
|
|
363
390
|
y2 = bez2[7];
|
|
364
391
|
} else {
|
|
365
|
-
bez2 = [
|
|
392
|
+
bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
|
|
366
393
|
x2 = x2m;
|
|
367
394
|
y2 = y2m;
|
|
368
395
|
}
|