poly-extrude 0.21.0 → 0.22.1
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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/poly-extrude.js +2208 -157
- package/dist/poly-extrude.js.map +1 -1
- package/dist/poly-extrude.min.js +2 -2
- package/dist/poly-extrude.mjs +2208 -158
- package/dist/poly-extrude.mjs.map +1 -1
- package/dist/polyline.js +14 -21
- package/dist/polyline.js.map +1 -1
- package/dist/{polylineoffset.d.ts → polylinehelper.d.ts} +5 -0
- package/dist/{polylineoffset.js → polylinehelper.js} +59 -2
- package/dist/polylinehelper.js.map +1 -0
- package/dist/util.d.ts +4 -0
- package/dist/util.js +7 -0
- package/dist/util.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +3 -2
- package/src/polyline.ts +14 -22
- package/src/{polylineoffset.ts → polylinehelper.ts} +67 -1
- package/src/util.ts +12 -0
- package/dist/polylineoffset.js.map +0 -1
package/dist/poly-extrude.mjs
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/*!
|
2
|
-
* poly-extrude v0.
|
2
|
+
* poly-extrude v0.22.1
|
3
3
|
*/
|
4
4
|
function _defineProperties(target, props) {
|
5
5
|
for (var i = 0; i < props.length; i++) {
|
@@ -35,6 +35,44 @@ function _setPrototypeOf(o, p) {
|
|
35
35
|
return _setPrototypeOf(o, p);
|
36
36
|
}
|
37
37
|
|
38
|
+
function _unsupportedIterableToArray(o, minLen) {
|
39
|
+
if (!o) return;
|
40
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
41
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
42
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
43
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
44
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
45
|
+
}
|
46
|
+
|
47
|
+
function _arrayLikeToArray(arr, len) {
|
48
|
+
if (len == null || len > arr.length) len = arr.length;
|
49
|
+
|
50
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
51
|
+
|
52
|
+
return arr2;
|
53
|
+
}
|
54
|
+
|
55
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
56
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
57
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
58
|
+
|
59
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
60
|
+
if (it) o = it;
|
61
|
+
var i = 0;
|
62
|
+
return function () {
|
63
|
+
if (i >= o.length) return {
|
64
|
+
done: true
|
65
|
+
};
|
66
|
+
return {
|
67
|
+
done: false,
|
68
|
+
value: o[i++]
|
69
|
+
};
|
70
|
+
};
|
71
|
+
}
|
72
|
+
|
73
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
74
|
+
}
|
75
|
+
|
38
76
|
function earcut(data, holeIndices, dim) {
|
39
77
|
if (dim === void 0) {
|
40
78
|
dim = 2;
|
@@ -1973,6 +2011,13 @@ function calLineDistance(line) {
|
|
1973
2011
|
}
|
1974
2012
|
return distance;
|
1975
2013
|
}
|
2014
|
+
function pointEqual(p1, p2) {
|
2015
|
+
return p1[0] === p2[0] && p1[1] === p2[1];
|
2016
|
+
}
|
2017
|
+
function pointDistance(p1, p2) {
|
2018
|
+
const dx = p2[0] - p1[0], dy = p2[1] - p1[1];
|
2019
|
+
return Math.sqrt(dx * dx + dy * dy);
|
2020
|
+
}
|
1976
2021
|
|
1977
2022
|
function extrudePolygons(polygons, opts) {
|
1978
2023
|
const options = Object.assign({}, { depth: 2, top: true }, opts);
|
@@ -2443,7 +2488,7 @@ function expandLine(line, options) {
|
|
2443
2488
|
let p0;
|
2444
2489
|
let p1 = line[i], p2 = line[i + 1];
|
2445
2490
|
const current = p1;
|
2446
|
-
if (pre &&
|
2491
|
+
if (pre && pointEqual(pre, current)) {
|
2447
2492
|
repeatVertex();
|
2448
2493
|
i++;
|
2449
2494
|
continue;
|
@@ -2453,10 +2498,10 @@ function expandLine(line, options) {
|
|
2453
2498
|
if (i === len - 1) {
|
2454
2499
|
p1 = line[len - 2];
|
2455
2500
|
p2 = line[len - 1];
|
2456
|
-
if (
|
2501
|
+
if (pointEqual(p1, p2)) {
|
2457
2502
|
for (let j = line.indexOf(p1); j >= 0; j--) {
|
2458
2503
|
const p = line[j];
|
2459
|
-
if (!
|
2504
|
+
if (!pointEqual(p, current)) {
|
2460
2505
|
p1 = p;
|
2461
2506
|
break;
|
2462
2507
|
}
|
@@ -2464,27 +2509,27 @@ function expandLine(line, options) {
|
|
2464
2509
|
}
|
2465
2510
|
}
|
2466
2511
|
else {
|
2467
|
-
if (
|
2512
|
+
if (pointEqual(p1, p2)) {
|
2468
2513
|
for (let j = line.indexOf(p2); j < len; j++) {
|
2469
2514
|
const p = line[j];
|
2470
|
-
if (!
|
2515
|
+
if (!pointEqual(p, current)) {
|
2471
2516
|
p2 = p;
|
2472
2517
|
break;
|
2473
2518
|
}
|
2474
2519
|
}
|
2475
2520
|
}
|
2476
|
-
if (
|
2521
|
+
if (pointEqual(p1, p2)) {
|
2477
2522
|
lastRepeat = true;
|
2478
2523
|
for (let j = line.indexOf(p1); j >= 0; j--) {
|
2479
2524
|
const p = line[j];
|
2480
|
-
if (!
|
2525
|
+
if (!pointEqual(p, current)) {
|
2481
2526
|
p1 = p;
|
2482
2527
|
break;
|
2483
2528
|
}
|
2484
2529
|
}
|
2485
2530
|
}
|
2486
2531
|
}
|
2487
|
-
if (
|
2532
|
+
if (pointEqual(p1, p2)) {
|
2488
2533
|
console.error('not find next vertex:index:', i, line);
|
2489
2534
|
repeatVertex();
|
2490
2535
|
i++;
|
@@ -2500,16 +2545,16 @@ function expandLine(line, options) {
|
|
2500
2545
|
else {
|
2501
2546
|
// 至少3个顶点才会触发
|
2502
2547
|
p0 = line[i - 1];
|
2503
|
-
if (
|
2548
|
+
if (pointEqual(p0, p2) || pointEqual(p0, p1)) {
|
2504
2549
|
for (let j = line.indexOf(p2); j >= 0; j--) {
|
2505
2550
|
const p = line[j];
|
2506
|
-
if (!
|
2551
|
+
if (!pointEqual(p, p2) && (!pointEqual(p, p1))) {
|
2507
2552
|
p0 = p;
|
2508
2553
|
break;
|
2509
2554
|
}
|
2510
2555
|
}
|
2511
2556
|
}
|
2512
|
-
if (
|
2557
|
+
if (pointEqual(p0, p2) || pointEqual(p0, p1) || pointEqual(p1, p2)) {
|
2513
2558
|
console.error('not find pre vertex:index:', i, line);
|
2514
2559
|
repeatVertex();
|
2515
2560
|
i++;
|
@@ -2571,13 +2616,13 @@ function expandLine(line, options) {
|
|
2571
2616
|
let needCut = false;
|
2572
2617
|
if (cutCorner) {
|
2573
2618
|
const bufferRadius = radius * 2;
|
2574
|
-
if (
|
2619
|
+
if (pointDistance(current, op1) > bufferRadius || pointDistance(current, op2) > bufferRadius) {
|
2575
2620
|
needCut = true;
|
2576
2621
|
}
|
2577
2622
|
}
|
2578
2623
|
if (needCut && p0 && preleftline && prerightline) {
|
2579
2624
|
let cutPoint = op1;
|
2580
|
-
if (
|
2625
|
+
if (pointDistance(op1, p0) < pointDistance(op2, p0)) {
|
2581
2626
|
cutPoint = op2;
|
2582
2627
|
}
|
2583
2628
|
const dy = cutPoint[1] - current[1], dx = cutPoint[0] - current[0];
|
@@ -2627,9 +2672,6 @@ function expandLine(line, options) {
|
|
2627
2672
|
}
|
2628
2673
|
return { offsetPoints: points, leftPoints, rightPoints, line };
|
2629
2674
|
}
|
2630
|
-
function equal(p1, p2) {
|
2631
|
-
return p1[0] === p2[0] && p1[1] === p2[1];
|
2632
|
-
}
|
2633
2675
|
const getAngle$1 = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
|
2634
2676
|
const dot = x1 * x2 + y1 * y2;
|
2635
2677
|
const det = x1 * y2 - y1 * x2;
|
@@ -2637,10 +2679,6 @@ const getAngle$1 = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
|
|
2637
2679
|
return (angle);
|
2638
2680
|
// return (angle + 360) % 360;
|
2639
2681
|
};
|
2640
|
-
function distance(p1, p2) {
|
2641
|
-
const dx = p2[0] - p1[0], dy = p2[1] - p1[1];
|
2642
|
-
return Math.sqrt(dx * dx + dy * dy);
|
2643
|
-
}
|
2644
2682
|
function leftOnLine(p, p1, p2) {
|
2645
2683
|
const [x1, y1] = p1;
|
2646
2684
|
const [x2, y2] = p2;
|
@@ -4870,159 +4908,2115 @@ function generateStartAndEnd(result, polygon, options) {
|
|
4870
4908
|
mergeArray(indices, indexs);
|
4871
4909
|
}
|
4872
4910
|
|
4873
|
-
|
4874
|
-
|
4875
|
-
|
4876
|
-
|
4877
|
-
|
4878
|
-
|
4879
|
-
|
4880
|
-
|
4911
|
+
var __defProp = Object.defineProperty;
|
4912
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4913
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4914
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4915
|
+
|
4916
|
+
var __markAsModule = function __markAsModule(target) {
|
4917
|
+
return __defProp(target, "__esModule", {
|
4918
|
+
value: true
|
4919
|
+
});
|
4920
|
+
};
|
4921
|
+
|
4922
|
+
var __export = function __export(target, all) {
|
4923
|
+
for (var name in all) {
|
4924
|
+
__defProp(target, name, {
|
4925
|
+
get: all[name],
|
4926
|
+
enumerable: true
|
4927
|
+
});
|
4928
|
+
}
|
4929
|
+
};
|
4930
|
+
|
4931
|
+
var __reExport = function __reExport(target, module2, copyDefault, desc) {
|
4932
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
4933
|
+
var _loop = function _loop() {
|
4934
|
+
var key = _step.value;
|
4935
|
+
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) __defProp(target, key, {
|
4936
|
+
get: function get() {
|
4937
|
+
return module2[key];
|
4938
|
+
},
|
4939
|
+
enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable
|
4940
|
+
});
|
4881
4941
|
};
|
4942
|
+
|
4943
|
+
for (var _iterator = _createForOfIteratorHelperLoose(__getOwnPropNames(module2)), _step; !(_step = _iterator()).done;) {
|
4944
|
+
_loop();
|
4945
|
+
}
|
4946
|
+
}
|
4947
|
+
|
4948
|
+
return target;
|
4949
|
+
};
|
4950
|
+
|
4951
|
+
var __toCommonJS = /* @__PURE__ */function (cache) {
|
4952
|
+
return function (module2, temp) {
|
4953
|
+
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
|
4954
|
+
};
|
4955
|
+
}(typeof WeakMap !== "undefined" ? /* @__PURE__ */new WeakMap() : 0); // src/bezier.js
|
4956
|
+
|
4957
|
+
|
4958
|
+
var bezier_exports = {};
|
4959
|
+
|
4960
|
+
__export(bezier_exports, {
|
4961
|
+
Bezier: function Bezier() {
|
4962
|
+
return _Bezier;
|
4963
|
+
}
|
4964
|
+
}); // src/utils.js
|
4965
|
+
|
4966
|
+
|
4967
|
+
var abs = Math.abs,
|
4968
|
+
cos = Math.cos,
|
4969
|
+
sin = Math.sin,
|
4970
|
+
acos = Math.acos,
|
4971
|
+
atan2 = Math.atan2,
|
4972
|
+
sqrt = Math.sqrt,
|
4973
|
+
pow = Math.pow;
|
4974
|
+
|
4975
|
+
function crt(v) {
|
4976
|
+
return v < 0 ? -pow(-v, 1 / 3) : pow(v, 1 / 3);
|
4882
4977
|
}
|
4883
|
-
|
4884
|
-
|
4885
|
-
|
4886
|
-
|
4887
|
-
|
4888
|
-
|
4889
|
-
|
4890
|
-
|
4891
|
-
|
4892
|
-
|
4893
|
-
|
4894
|
-
|
4895
|
-
|
4896
|
-
|
4897
|
-
|
4898
|
-
|
4899
|
-
|
4900
|
-
|
4978
|
+
|
4979
|
+
var pi = Math.PI;
|
4980
|
+
var tau = 2 * pi;
|
4981
|
+
var quart = pi / 2;
|
4982
|
+
var epsilon = 1e-6;
|
4983
|
+
var nMax = Number.MAX_SAFE_INTEGER || 9007199254740991;
|
4984
|
+
var nMin = Number.MIN_SAFE_INTEGER || -9007199254740991;
|
4985
|
+
var ZERO = {
|
4986
|
+
x: 0,
|
4987
|
+
y: 0,
|
4988
|
+
z: 0
|
4989
|
+
};
|
4990
|
+
var utils = {
|
4991
|
+
Tvalues: [-0.06405689286260563, 0.06405689286260563, -0.1911188674736163, 0.1911188674736163, -0.3150426796961634, 0.3150426796961634, -0.4337935076260451, 0.4337935076260451, -0.5454214713888396, 0.5454214713888396, -0.6480936519369755, 0.6480936519369755, -0.7401241915785544, 0.7401241915785544, -0.820001985973903, 0.820001985973903, -0.8864155270044011, 0.8864155270044011, -0.9382745520027328, 0.9382745520027328, -0.9747285559713095, 0.9747285559713095, -0.9951872199970213, 0.9951872199970213],
|
4992
|
+
Cvalues: [0.12793819534675216, 0.12793819534675216, 0.1258374563468283, 0.1258374563468283, 0.12167047292780339, 0.12167047292780339, 0.1155056680537256, 0.1155056680537256, 0.10744427011596563, 0.10744427011596563, 0.09761865210411388, 0.09761865210411388, 0.08619016153195327, 0.08619016153195327, 0.0733464814110803, 0.0733464814110803, 0.05929858491543678, 0.05929858491543678, 0.04427743881741981, 0.04427743881741981, 0.028531388628933663, 0.028531388628933663, 0.0123412297999872, 0.0123412297999872],
|
4993
|
+
arcfn: function arcfn(t2, derivativeFn) {
|
4994
|
+
var d = derivativeFn(t2);
|
4995
|
+
var l = d.x * d.x + d.y * d.y;
|
4996
|
+
|
4997
|
+
if (typeof d.z !== "undefined") {
|
4998
|
+
l += d.z * d.z;
|
4901
4999
|
}
|
4902
|
-
|
4903
|
-
|
4904
|
-
|
5000
|
+
|
5001
|
+
return sqrt(l);
|
5002
|
+
},
|
5003
|
+
compute: function compute(t2, points, _3d) {
|
5004
|
+
if (t2 === 0) {
|
5005
|
+
points[0].t = 0;
|
5006
|
+
return points[0];
|
5007
|
+
}
|
5008
|
+
|
5009
|
+
var order = points.length - 1;
|
5010
|
+
|
5011
|
+
if (t2 === 1) {
|
5012
|
+
points[order].t = 1;
|
5013
|
+
return points[order];
|
5014
|
+
}
|
5015
|
+
|
5016
|
+
var mt = 1 - t2;
|
5017
|
+
var p = points;
|
5018
|
+
|
5019
|
+
if (order === 0) {
|
5020
|
+
points[0].t = t2;
|
5021
|
+
return points[0];
|
5022
|
+
}
|
5023
|
+
|
5024
|
+
if (order === 1) {
|
5025
|
+
var ret = {
|
5026
|
+
x: mt * p[0].x + t2 * p[1].x,
|
5027
|
+
y: mt * p[0].y + t2 * p[1].y,
|
5028
|
+
t: t2
|
5029
|
+
};
|
5030
|
+
|
5031
|
+
if (_3d) {
|
5032
|
+
ret.z = mt * p[0].z + t2 * p[1].z;
|
5033
|
+
}
|
5034
|
+
|
5035
|
+
return ret;
|
5036
|
+
}
|
5037
|
+
|
5038
|
+
if (order < 4) {
|
5039
|
+
var mt2 = mt * mt,
|
5040
|
+
t22 = t2 * t2,
|
5041
|
+
a,
|
5042
|
+
b,
|
5043
|
+
c,
|
5044
|
+
d = 0;
|
5045
|
+
|
5046
|
+
if (order === 2) {
|
5047
|
+
p = [p[0], p[1], p[2], ZERO];
|
5048
|
+
a = mt2;
|
5049
|
+
b = mt * t2 * 2;
|
5050
|
+
c = t22;
|
5051
|
+
} else if (order === 3) {
|
5052
|
+
a = mt2 * mt;
|
5053
|
+
b = mt2 * t2 * 3;
|
5054
|
+
c = mt * t22 * 3;
|
5055
|
+
d = t2 * t22;
|
5056
|
+
}
|
5057
|
+
|
5058
|
+
var _ret = {
|
5059
|
+
x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,
|
5060
|
+
y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,
|
5061
|
+
t: t2
|
5062
|
+
};
|
5063
|
+
|
5064
|
+
if (_3d) {
|
5065
|
+
_ret.z = a * p[0].z + b * p[1].z + c * p[2].z + d * p[3].z;
|
5066
|
+
}
|
5067
|
+
|
5068
|
+
return _ret;
|
5069
|
+
}
|
5070
|
+
|
5071
|
+
var dCpts = JSON.parse(JSON.stringify(points));
|
5072
|
+
|
5073
|
+
while (dCpts.length > 1) {
|
5074
|
+
for (var i = 0; i < dCpts.length - 1; i++) {
|
5075
|
+
dCpts[i] = {
|
5076
|
+
x: dCpts[i].x + (dCpts[i + 1].x - dCpts[i].x) * t2,
|
5077
|
+
y: dCpts[i].y + (dCpts[i + 1].y - dCpts[i].y) * t2
|
5078
|
+
};
|
5079
|
+
|
5080
|
+
if (typeof dCpts[i].z !== "undefined") {
|
5081
|
+
dCpts[i].z = dCpts[i].z + (dCpts[i + 1].z - dCpts[i].z) * t2;
|
4905
5082
|
}
|
4906
|
-
|
4907
|
-
|
4908
|
-
|
4909
|
-
|
5083
|
+
}
|
5084
|
+
|
5085
|
+
dCpts.splice(dCpts.length - 1, 1);
|
5086
|
+
}
|
5087
|
+
|
5088
|
+
dCpts[0].t = t2;
|
5089
|
+
return dCpts[0];
|
5090
|
+
},
|
5091
|
+
computeWithRatios: function computeWithRatios(t2, points, ratios, _3d) {
|
5092
|
+
var mt = 1 - t2,
|
5093
|
+
r = ratios,
|
5094
|
+
p = points;
|
5095
|
+
var f1 = r[0],
|
5096
|
+
f2 = r[1],
|
5097
|
+
f3 = r[2],
|
5098
|
+
f4 = r[3],
|
5099
|
+
d;
|
5100
|
+
f1 *= mt;
|
5101
|
+
f2 *= t2;
|
5102
|
+
|
5103
|
+
if (p.length === 2) {
|
5104
|
+
d = f1 + f2;
|
5105
|
+
return {
|
5106
|
+
x: (f1 * p[0].x + f2 * p[1].x) / d,
|
5107
|
+
y: (f1 * p[0].y + f2 * p[1].y) / d,
|
5108
|
+
z: !_3d ? false : (f1 * p[0].z + f2 * p[1].z) / d,
|
5109
|
+
t: t2
|
5110
|
+
};
|
5111
|
+
}
|
5112
|
+
|
5113
|
+
f1 *= mt;
|
5114
|
+
f2 *= 2 * mt;
|
5115
|
+
f3 *= t2 * t2;
|
5116
|
+
|
5117
|
+
if (p.length === 3) {
|
5118
|
+
d = f1 + f2 + f3;
|
5119
|
+
return {
|
5120
|
+
x: (f1 * p[0].x + f2 * p[1].x + f3 * p[2].x) / d,
|
5121
|
+
y: (f1 * p[0].y + f2 * p[1].y + f3 * p[2].y) / d,
|
5122
|
+
z: !_3d ? false : (f1 * p[0].z + f2 * p[1].z + f3 * p[2].z) / d,
|
5123
|
+
t: t2
|
5124
|
+
};
|
5125
|
+
}
|
5126
|
+
|
5127
|
+
f1 *= mt;
|
5128
|
+
f2 *= 1.5 * mt;
|
5129
|
+
f3 *= 3 * mt;
|
5130
|
+
f4 *= t2 * t2 * t2;
|
5131
|
+
|
5132
|
+
if (p.length === 4) {
|
5133
|
+
d = f1 + f2 + f3 + f4;
|
5134
|
+
return {
|
5135
|
+
x: (f1 * p[0].x + f2 * p[1].x + f3 * p[2].x + f4 * p[3].x) / d,
|
5136
|
+
y: (f1 * p[0].y + f2 * p[1].y + f3 * p[2].y + f4 * p[3].y) / d,
|
5137
|
+
z: !_3d ? false : (f1 * p[0].z + f2 * p[1].z + f3 * p[2].z + f4 * p[3].z) / d,
|
5138
|
+
t: t2
|
5139
|
+
};
|
5140
|
+
}
|
5141
|
+
},
|
5142
|
+
derive: function derive(points, _3d) {
|
5143
|
+
var dpoints = [];
|
5144
|
+
|
5145
|
+
for (var p = points, d = p.length, c = d - 1; d > 1; d--, c--) {
|
5146
|
+
var list = [];
|
5147
|
+
|
5148
|
+
for (var j = 0, dpt; j < c; j++) {
|
5149
|
+
dpt = {
|
5150
|
+
x: c * (p[j + 1].x - p[j].x),
|
5151
|
+
y: c * (p[j + 1].y - p[j].y)
|
4910
5152
|
};
|
4911
|
-
|
4912
|
-
|
5153
|
+
|
5154
|
+
if (_3d) {
|
5155
|
+
dpt.z = c * (p[j + 1].z - p[j].z);
|
5156
|
+
}
|
5157
|
+
|
5158
|
+
list.push(dpt);
|
5159
|
+
}
|
5160
|
+
|
5161
|
+
dpoints.push(list);
|
5162
|
+
p = list;
|
5163
|
+
}
|
5164
|
+
|
5165
|
+
return dpoints;
|
5166
|
+
},
|
5167
|
+
between: function between(v, m, M) {
|
5168
|
+
return m <= v && v <= M || utils.approximately(v, m) || utils.approximately(v, M);
|
5169
|
+
},
|
5170
|
+
approximately: function approximately(a, b, precision) {
|
5171
|
+
return abs(a - b) <= (precision || epsilon);
|
5172
|
+
},
|
5173
|
+
length: function length(derivativeFn) {
|
5174
|
+
var z = 0.5,
|
5175
|
+
len = utils.Tvalues.length;
|
5176
|
+
var sum = 0;
|
5177
|
+
|
5178
|
+
for (var i = 0, t2; i < len; i++) {
|
5179
|
+
t2 = z * utils.Tvalues[i] + z;
|
5180
|
+
sum += utils.Cvalues[i] * utils.arcfn(t2, derivativeFn);
|
5181
|
+
}
|
5182
|
+
|
5183
|
+
return z * sum;
|
5184
|
+
},
|
5185
|
+
map: function map(v, ds, de, ts, te) {
|
5186
|
+
var d1 = de - ds,
|
5187
|
+
d2 = te - ts,
|
5188
|
+
v2 = v - ds,
|
5189
|
+
r = v2 / d1;
|
5190
|
+
return ts + d2 * r;
|
5191
|
+
},
|
5192
|
+
lerp: function lerp(r, v1, v2) {
|
5193
|
+
var ret = {
|
5194
|
+
x: v1.x + r * (v2.x - v1.x),
|
5195
|
+
y: v1.y + r * (v2.y - v1.y)
|
5196
|
+
};
|
5197
|
+
|
5198
|
+
if (v1.z !== void 0 && v2.z !== void 0) {
|
5199
|
+
ret.z = v1.z + r * (v2.z - v1.z);
|
5200
|
+
}
|
5201
|
+
|
5202
|
+
return ret;
|
5203
|
+
},
|
5204
|
+
pointToString: function pointToString(p) {
|
5205
|
+
var s = p.x + "/" + p.y;
|
5206
|
+
|
5207
|
+
if (typeof p.z !== "undefined") {
|
5208
|
+
s += "/" + p.z;
|
5209
|
+
}
|
5210
|
+
|
5211
|
+
return s;
|
5212
|
+
},
|
5213
|
+
pointsToString: function pointsToString(points) {
|
5214
|
+
return "[" + points.map(utils.pointToString).join(", ") + "]";
|
5215
|
+
},
|
5216
|
+
copy: function copy(obj) {
|
5217
|
+
return JSON.parse(JSON.stringify(obj));
|
5218
|
+
},
|
5219
|
+
angle: function angle(o, v1, v2) {
|
5220
|
+
var dx1 = v1.x - o.x,
|
5221
|
+
dy1 = v1.y - o.y,
|
5222
|
+
dx2 = v2.x - o.x,
|
5223
|
+
dy2 = v2.y - o.y,
|
5224
|
+
cross = dx1 * dy2 - dy1 * dx2,
|
5225
|
+
dot = dx1 * dx2 + dy1 * dy2;
|
5226
|
+
return atan2(cross, dot);
|
5227
|
+
},
|
5228
|
+
round: function round(v, d) {
|
5229
|
+
var s = "" + v;
|
5230
|
+
var pos = s.indexOf(".");
|
5231
|
+
return parseFloat(s.substring(0, pos + 1 + d));
|
5232
|
+
},
|
5233
|
+
dist: function dist(p1, p2) {
|
5234
|
+
var dx = p1.x - p2.x,
|
5235
|
+
dy = p1.y - p2.y;
|
5236
|
+
return sqrt(dx * dx + dy * dy);
|
5237
|
+
},
|
5238
|
+
closest: function closest(LUT, point) {
|
5239
|
+
var mdist = pow(2, 63),
|
5240
|
+
mpos,
|
5241
|
+
d;
|
5242
|
+
LUT.forEach(function (p, idx) {
|
5243
|
+
d = utils.dist(point, p);
|
5244
|
+
|
5245
|
+
if (d < mdist) {
|
5246
|
+
mdist = d;
|
5247
|
+
mpos = idx;
|
5248
|
+
}
|
5249
|
+
});
|
5250
|
+
return {
|
5251
|
+
mdist: mdist,
|
5252
|
+
mpos: mpos
|
5253
|
+
};
|
5254
|
+
},
|
5255
|
+
abcratio: function abcratio(t2, n) {
|
5256
|
+
if (n !== 2 && n !== 3) {
|
5257
|
+
return false;
|
4913
5258
|
}
|
4914
|
-
|
4915
|
-
|
4916
|
-
|
4917
|
-
|
4918
|
-
|
4919
|
-
};
|
4920
|
-
p.z = calZ(l1a, l1b, p);
|
4921
|
-
return p;
|
5259
|
+
|
5260
|
+
if (typeof t2 === "undefined") {
|
5261
|
+
t2 = 0.5;
|
5262
|
+
} else if (t2 === 0 || t2 === 1) {
|
5263
|
+
return t2;
|
4922
5264
|
}
|
4923
|
-
|
4924
|
-
|
5265
|
+
|
5266
|
+
var bottom = pow(t2, n) + pow(1 - t2, n),
|
5267
|
+
top = bottom - 1;
|
5268
|
+
return abs(top / bottom);
|
5269
|
+
},
|
5270
|
+
projectionratio: function projectionratio(t2, n) {
|
5271
|
+
if (n !== 2 && n !== 3) {
|
5272
|
+
return false;
|
4925
5273
|
}
|
4926
|
-
|
4927
|
-
|
4928
|
-
|
4929
|
-
|
4930
|
-
|
5274
|
+
|
5275
|
+
if (typeof t2 === "undefined") {
|
5276
|
+
t2 = 0.5;
|
5277
|
+
} else if (t2 === 0 || t2 === 1) {
|
5278
|
+
return t2;
|
5279
|
+
}
|
5280
|
+
|
5281
|
+
var top = pow(1 - t2, n),
|
5282
|
+
bottom = pow(t2, n) + top;
|
5283
|
+
return top / bottom;
|
5284
|
+
},
|
5285
|
+
lli8: function lli8(x1, y1, x2, y2, x3, y3, x4, y4) {
|
5286
|
+
var nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4),
|
5287
|
+
ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4),
|
5288
|
+
d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
5289
|
+
|
5290
|
+
if (d == 0) {
|
5291
|
+
return false;
|
5292
|
+
}
|
5293
|
+
|
5294
|
+
return {
|
5295
|
+
x: nx / d,
|
5296
|
+
y: ny / d
|
4931
5297
|
};
|
4932
|
-
|
4933
|
-
|
4934
|
-
|
4935
|
-
|
5298
|
+
},
|
5299
|
+
lli4: function lli4(p1, p2, p3, p4) {
|
5300
|
+
var x1 = p1.x,
|
5301
|
+
y1 = p1.y,
|
5302
|
+
x2 = p2.x,
|
5303
|
+
y2 = p2.y,
|
5304
|
+
x3 = p3.x,
|
5305
|
+
y3 = p3.y,
|
5306
|
+
x4 = p4.x,
|
5307
|
+
y4 = p4.y;
|
5308
|
+
return utils.lli8(x1, y1, x2, y2, x3, y3, x4, y4);
|
5309
|
+
},
|
5310
|
+
lli: function lli(v1, v2) {
|
5311
|
+
return utils.lli4(v1, v1.c, v2, v2.c);
|
5312
|
+
},
|
5313
|
+
makeline: function makeline(p1, p2) {
|
5314
|
+
return new _Bezier(p1.x, p1.y, (p1.x + p2.x) / 2, (p1.y + p2.y) / 2, p2.x, p2.y);
|
5315
|
+
},
|
5316
|
+
findbbox: function findbbox(sections) {
|
5317
|
+
var mx = nMax,
|
5318
|
+
my = nMax,
|
5319
|
+
MX = nMin,
|
5320
|
+
MY = nMin;
|
5321
|
+
sections.forEach(function (s) {
|
5322
|
+
var bbox = s.bbox();
|
5323
|
+
if (mx > bbox.x.min) mx = bbox.x.min;
|
5324
|
+
if (my > bbox.y.min) my = bbox.y.min;
|
5325
|
+
if (MX < bbox.x.max) MX = bbox.x.max;
|
5326
|
+
if (MY < bbox.y.max) MY = bbox.y.max;
|
5327
|
+
});
|
4936
5328
|
return {
|
4937
|
-
|
4938
|
-
|
4939
|
-
|
5329
|
+
x: {
|
5330
|
+
min: mx,
|
5331
|
+
mid: (mx + MX) / 2,
|
5332
|
+
max: MX,
|
5333
|
+
size: MX - mx
|
5334
|
+
},
|
5335
|
+
y: {
|
5336
|
+
min: my,
|
5337
|
+
mid: (my + MY) / 2,
|
5338
|
+
max: MY,
|
5339
|
+
size: MY - my
|
5340
|
+
}
|
4940
5341
|
};
|
4941
|
-
}
|
4942
|
-
function
|
4943
|
-
|
4944
|
-
|
4945
|
-
|
4946
|
-
|
4947
|
-
|
4948
|
-
|
4949
|
-
|
5342
|
+
},
|
5343
|
+
shapeintersections: function shapeintersections(s1, bbox1, s2, bbox2, curveIntersectionThreshold) {
|
5344
|
+
if (!utils.bboxoverlap(bbox1, bbox2)) return [];
|
5345
|
+
var intersections = [];
|
5346
|
+
var a1 = [s1.startcap, s1.forward, s1.back, s1.endcap];
|
5347
|
+
var a2 = [s2.startcap, s2.forward, s2.back, s2.endcap];
|
5348
|
+
a1.forEach(function (l1) {
|
5349
|
+
if (l1.virtual) return;
|
5350
|
+
a2.forEach(function (l2) {
|
5351
|
+
if (l2.virtual) return;
|
5352
|
+
var iss = l1.intersects(l2, curveIntersectionThreshold);
|
5353
|
+
|
5354
|
+
if (iss.length > 0) {
|
5355
|
+
iss.c1 = l1;
|
5356
|
+
iss.c2 = l2;
|
5357
|
+
iss.s1 = s1;
|
5358
|
+
iss.s2 = s2;
|
5359
|
+
intersections.push(iss);
|
4950
5360
|
}
|
4951
|
-
|
4952
|
-
|
4953
|
-
|
4954
|
-
|
4955
|
-
|
4956
|
-
|
4957
|
-
|
4958
|
-
|
4959
|
-
|
4960
|
-
|
4961
|
-
|
4962
|
-
|
4963
|
-
|
4964
|
-
|
4965
|
-
|
4966
|
-
|
4967
|
-
|
4968
|
-
|
4969
|
-
|
4970
|
-
|
5361
|
+
});
|
5362
|
+
});
|
5363
|
+
return intersections;
|
5364
|
+
},
|
5365
|
+
makeshape: function makeshape(forward, back, curveIntersectionThreshold) {
|
5366
|
+
var bpl = back.points.length;
|
5367
|
+
var fpl = forward.points.length;
|
5368
|
+
var start = utils.makeline(back.points[bpl - 1], forward.points[0]);
|
5369
|
+
var end = utils.makeline(forward.points[fpl - 1], back.points[0]);
|
5370
|
+
var shape = {
|
5371
|
+
startcap: start,
|
5372
|
+
forward: forward,
|
5373
|
+
back: back,
|
5374
|
+
endcap: end,
|
5375
|
+
bbox: utils.findbbox([start, forward, back, end])
|
5376
|
+
};
|
5377
|
+
|
5378
|
+
shape.intersections = function (s2) {
|
5379
|
+
return utils.shapeintersections(shape, shape.bbox, s2, s2.bbox, curveIntersectionThreshold);
|
5380
|
+
};
|
5381
|
+
|
5382
|
+
return shape;
|
5383
|
+
},
|
5384
|
+
getminmax: function getminmax(curve, d, list) {
|
5385
|
+
if (!list) return {
|
5386
|
+
min: 0,
|
5387
|
+
max: 0
|
5388
|
+
};
|
5389
|
+
var min2 = nMax,
|
5390
|
+
max2 = nMin,
|
5391
|
+
t2,
|
5392
|
+
c;
|
5393
|
+
|
5394
|
+
if (list.indexOf(0) === -1) {
|
5395
|
+
list = [0].concat(list);
|
4971
5396
|
}
|
4972
|
-
|
4973
|
-
|
4974
|
-
|
4975
|
-
Join 2 line segments defined by 2 points each with a circular arc
|
4976
|
-
*/
|
4977
|
-
function joinSegments(s1, s2, offset) {
|
4978
|
-
// TODO: different join styles
|
4979
|
-
return circularArc(s1, s2, offset)
|
4980
|
-
.filter(function (x) { return x; });
|
4981
|
-
}
|
4982
|
-
function joinLineSegments(segments, offset) {
|
4983
|
-
var joinedPoints = [];
|
4984
|
-
var first = segments[0];
|
4985
|
-
var last = segments[segments.length - 1];
|
4986
|
-
if (first && last) {
|
4987
|
-
joinedPoints.push(first.offset[0]);
|
4988
|
-
for (let i = 1, len = segments.length; i < len; i++) {
|
4989
|
-
let s1 = segments[i - 1], s2 = segments[i];
|
4990
|
-
const pts = joinSegments(s1, s2, offset);
|
4991
|
-
mergeArray(joinedPoints, pts);
|
4992
|
-
}
|
4993
|
-
joinedPoints.push(last.offset[1]);
|
5397
|
+
|
5398
|
+
if (list.indexOf(1) === -1) {
|
5399
|
+
list.push(1);
|
4994
5400
|
}
|
4995
|
-
|
4996
|
-
|
4997
|
-
|
5401
|
+
|
5402
|
+
for (var i = 0, len = list.length; i < len; i++) {
|
5403
|
+
t2 = list[i];
|
5404
|
+
c = curve.get(t2);
|
5405
|
+
|
5406
|
+
if (c[d] < min2) {
|
5407
|
+
min2 = c[d];
|
5408
|
+
}
|
5409
|
+
|
5410
|
+
if (c[d] > max2) {
|
5411
|
+
max2 = c[d];
|
5412
|
+
}
|
5413
|
+
}
|
5414
|
+
|
4998
5415
|
return {
|
4999
|
-
|
5000
|
-
|
5416
|
+
min: min2,
|
5417
|
+
mid: (min2 + max2) / 2,
|
5418
|
+
max: max2,
|
5419
|
+
size: max2 - min2
|
5001
5420
|
};
|
5002
|
-
}
|
5003
|
-
function
|
5004
|
-
|
5005
|
-
|
5006
|
-
|
5007
|
-
|
5008
|
-
|
5009
|
-
|
5010
|
-
|
5011
|
-
|
5012
|
-
|
5013
|
-
|
5014
|
-
|
5015
|
-
|
5016
|
-
|
5017
|
-
|
5018
|
-
|
5019
|
-
|
5020
|
-
|
5021
|
-
|
5421
|
+
},
|
5422
|
+
align: function align(points, line) {
|
5423
|
+
var tx = line.p1.x,
|
5424
|
+
ty = line.p1.y,
|
5425
|
+
a = -atan2(line.p2.y - ty, line.p2.x - tx),
|
5426
|
+
d = function d(v) {
|
5427
|
+
return {
|
5428
|
+
x: (v.x - tx) * cos(a) - (v.y - ty) * sin(a),
|
5429
|
+
y: (v.x - tx) * sin(a) + (v.y - ty) * cos(a)
|
5430
|
+
};
|
5431
|
+
};
|
5432
|
+
|
5433
|
+
return points.map(d);
|
5434
|
+
},
|
5435
|
+
roots: function roots(points, line) {
|
5436
|
+
line = line || {
|
5437
|
+
p1: {
|
5438
|
+
x: 0,
|
5439
|
+
y: 0
|
5440
|
+
},
|
5441
|
+
p2: {
|
5442
|
+
x: 1,
|
5443
|
+
y: 0
|
5444
|
+
}
|
5445
|
+
};
|
5446
|
+
var order = points.length - 1;
|
5447
|
+
var aligned = utils.align(points, line);
|
5448
|
+
|
5449
|
+
var reduce = function reduce(t2) {
|
5450
|
+
return 0 <= t2 && t2 <= 1;
|
5451
|
+
};
|
5452
|
+
|
5453
|
+
if (order === 2) {
|
5454
|
+
var a2 = aligned[0].y,
|
5455
|
+
b2 = aligned[1].y,
|
5456
|
+
c2 = aligned[2].y,
|
5457
|
+
d2 = a2 - 2 * b2 + c2;
|
5458
|
+
|
5459
|
+
if (d2 !== 0) {
|
5460
|
+
var m1 = -sqrt(b2 * b2 - a2 * c2),
|
5461
|
+
m2 = -a2 + b2,
|
5462
|
+
v12 = -(m1 + m2) / d2,
|
5463
|
+
v2 = -(-m1 + m2) / d2;
|
5464
|
+
return [v12, v2].filter(reduce);
|
5465
|
+
} else if (b2 !== c2 && d2 === 0) {
|
5466
|
+
return [(2 * b2 - c2) / (2 * b2 - 2 * c2)].filter(reduce);
|
5467
|
+
}
|
5468
|
+
|
5469
|
+
return [];
|
5022
5470
|
}
|
5023
|
-
|
5024
|
-
var
|
5025
|
-
|
5471
|
+
|
5472
|
+
var pa = aligned[0].y,
|
5473
|
+
pb = aligned[1].y,
|
5474
|
+
pc = aligned[2].y,
|
5475
|
+
pd = aligned[3].y;
|
5476
|
+
var d = -pa + 3 * pb - 3 * pc + pd,
|
5477
|
+
a = 3 * pa - 6 * pb + 3 * pc,
|
5478
|
+
b = -3 * pa + 3 * pb,
|
5479
|
+
c = pa;
|
5480
|
+
|
5481
|
+
if (utils.approximately(d, 0)) {
|
5482
|
+
if (utils.approximately(a, 0)) {
|
5483
|
+
if (utils.approximately(b, 0)) {
|
5484
|
+
return [];
|
5485
|
+
}
|
5486
|
+
|
5487
|
+
return [-c / b].filter(reduce);
|
5488
|
+
}
|
5489
|
+
|
5490
|
+
var q3 = sqrt(b * b - 4 * a * c),
|
5491
|
+
_a = 2 * a;
|
5492
|
+
|
5493
|
+
return [(q3 - b) / _a, (-b - q3) / _a].filter(reduce);
|
5494
|
+
}
|
5495
|
+
|
5496
|
+
a /= d;
|
5497
|
+
b /= d;
|
5498
|
+
c /= d;
|
5499
|
+
var p = (3 * b - a * a) / 3,
|
5500
|
+
p3 = p / 3,
|
5501
|
+
q = (2 * a * a * a - 9 * a * b + 27 * c) / 27,
|
5502
|
+
q2 = q / 2,
|
5503
|
+
discriminant = q2 * q2 + p3 * p3 * p3;
|
5504
|
+
var u1, v1, x1, x2, x3;
|
5505
|
+
|
5506
|
+
if (discriminant < 0) {
|
5507
|
+
var mp3 = -p / 3,
|
5508
|
+
mp33 = mp3 * mp3 * mp3,
|
5509
|
+
r = sqrt(mp33),
|
5510
|
+
t2 = -q / (2 * r),
|
5511
|
+
cosphi = t2 < -1 ? -1 : t2 > 1 ? 1 : t2,
|
5512
|
+
phi = acos(cosphi),
|
5513
|
+
crtr = crt(r),
|
5514
|
+
t1 = 2 * crtr;
|
5515
|
+
x1 = t1 * cos(phi / 3) - a / 3;
|
5516
|
+
x2 = t1 * cos((phi + tau) / 3) - a / 3;
|
5517
|
+
x3 = t1 * cos((phi + 2 * tau) / 3) - a / 3;
|
5518
|
+
return [x1, x2, x3].filter(reduce);
|
5519
|
+
} else if (discriminant === 0) {
|
5520
|
+
u1 = q2 < 0 ? crt(-q2) : -crt(q2);
|
5521
|
+
x1 = 2 * u1 - a / 3;
|
5522
|
+
x2 = -u1 - a / 3;
|
5523
|
+
return [x1, x2].filter(reduce);
|
5524
|
+
} else {
|
5525
|
+
var sd = sqrt(discriminant);
|
5526
|
+
u1 = crt(-q2 + sd);
|
5527
|
+
v1 = crt(q2 + sd);
|
5528
|
+
return [u1 - v1 - a / 3].filter(reduce);
|
5529
|
+
}
|
5530
|
+
},
|
5531
|
+
droots: function droots(p) {
|
5532
|
+
if (p.length === 3) {
|
5533
|
+
var a = p[0],
|
5534
|
+
b = p[1],
|
5535
|
+
c = p[2],
|
5536
|
+
d = a - 2 * b + c;
|
5537
|
+
|
5538
|
+
if (d !== 0) {
|
5539
|
+
var m1 = -sqrt(b * b - a * c),
|
5540
|
+
m2 = -a + b,
|
5541
|
+
v1 = -(m1 + m2) / d,
|
5542
|
+
v2 = -(-m1 + m2) / d;
|
5543
|
+
return [v1, v2];
|
5544
|
+
} else if (b !== c && d === 0) {
|
5545
|
+
return [(2 * b - c) / (2 * (b - c))];
|
5546
|
+
}
|
5547
|
+
|
5548
|
+
return [];
|
5549
|
+
}
|
5550
|
+
|
5551
|
+
if (p.length === 2) {
|
5552
|
+
var _a2 = p[0],
|
5553
|
+
_b = p[1];
|
5554
|
+
|
5555
|
+
if (_a2 !== _b) {
|
5556
|
+
return [_a2 / (_a2 - _b)];
|
5557
|
+
}
|
5558
|
+
|
5559
|
+
return [];
|
5560
|
+
}
|
5561
|
+
|
5562
|
+
return [];
|
5563
|
+
},
|
5564
|
+
curvature: function curvature(t2, d1, d2, _3d, kOnly) {
|
5565
|
+
var num,
|
5566
|
+
dnm,
|
5567
|
+
adk,
|
5568
|
+
dk,
|
5569
|
+
k = 0,
|
5570
|
+
r = 0;
|
5571
|
+
var d = utils.compute(t2, d1);
|
5572
|
+
var dd = utils.compute(t2, d2);
|
5573
|
+
var qdsum = d.x * d.x + d.y * d.y;
|
5574
|
+
|
5575
|
+
if (_3d) {
|
5576
|
+
num = sqrt(pow(d.y * dd.z - dd.y * d.z, 2) + pow(d.z * dd.x - dd.z * d.x, 2) + pow(d.x * dd.y - dd.x * d.y, 2));
|
5577
|
+
dnm = pow(qdsum + d.z * d.z, 3 / 2);
|
5578
|
+
} else {
|
5579
|
+
num = d.x * dd.y - d.y * dd.x;
|
5580
|
+
dnm = pow(qdsum, 3 / 2);
|
5581
|
+
}
|
5582
|
+
|
5583
|
+
if (num === 0 || dnm === 0) {
|
5584
|
+
return {
|
5585
|
+
k: 0,
|
5586
|
+
r: 0
|
5587
|
+
};
|
5588
|
+
}
|
5589
|
+
|
5590
|
+
k = num / dnm;
|
5591
|
+
r = dnm / num;
|
5592
|
+
|
5593
|
+
if (!kOnly) {
|
5594
|
+
var pk = utils.curvature(t2 - 1e-3, d1, d2, _3d, true).k;
|
5595
|
+
var nk = utils.curvature(t2 + 1e-3, d1, d2, _3d, true).k;
|
5596
|
+
dk = (nk - k + (k - pk)) / 2;
|
5597
|
+
adk = (abs(nk - k) + abs(k - pk)) / 2;
|
5598
|
+
}
|
5599
|
+
|
5600
|
+
return {
|
5601
|
+
k: k,
|
5602
|
+
r: r,
|
5603
|
+
dk: dk,
|
5604
|
+
adk: adk
|
5605
|
+
};
|
5606
|
+
},
|
5607
|
+
inflections: function inflections(points) {
|
5608
|
+
if (points.length < 4) return [];
|
5609
|
+
var p = utils.align(points, {
|
5610
|
+
p1: points[0],
|
5611
|
+
p2: points.slice(-1)[0]
|
5612
|
+
}),
|
5613
|
+
a = p[2].x * p[1].y,
|
5614
|
+
b = p[3].x * p[1].y,
|
5615
|
+
c = p[1].x * p[2].y,
|
5616
|
+
d = p[3].x * p[2].y,
|
5617
|
+
v1 = 18 * (-3 * a + 2 * b + 3 * c - d),
|
5618
|
+
v2 = 18 * (3 * a - b - 3 * c),
|
5619
|
+
v3 = 18 * (c - a);
|
5620
|
+
|
5621
|
+
if (utils.approximately(v1, 0)) {
|
5622
|
+
if (!utils.approximately(v2, 0)) {
|
5623
|
+
var t2 = -v3 / v2;
|
5624
|
+
if (0 <= t2 && t2 <= 1) return [t2];
|
5625
|
+
}
|
5626
|
+
|
5627
|
+
return [];
|
5628
|
+
}
|
5629
|
+
|
5630
|
+
var d2 = 2 * v1;
|
5631
|
+
if (utils.approximately(d2, 0)) return [];
|
5632
|
+
var trm = v2 * v2 - 4 * v1 * v3;
|
5633
|
+
if (trm < 0) return [];
|
5634
|
+
var sq = Math.sqrt(trm);
|
5635
|
+
return [(sq - v2) / d2, -(v2 + sq) / d2].filter(function (r) {
|
5636
|
+
return 0 <= r && r <= 1;
|
5637
|
+
});
|
5638
|
+
},
|
5639
|
+
bboxoverlap: function bboxoverlap(b1, b2) {
|
5640
|
+
var dims = ["x", "y"],
|
5641
|
+
len = dims.length;
|
5642
|
+
|
5643
|
+
for (var i = 0, dim, l, t2, d; i < len; i++) {
|
5644
|
+
dim = dims[i];
|
5645
|
+
l = b1[dim].mid;
|
5646
|
+
t2 = b2[dim].mid;
|
5647
|
+
d = (b1[dim].size + b2[dim].size) / 2;
|
5648
|
+
if (abs(l - t2) >= d) return false;
|
5649
|
+
}
|
5650
|
+
|
5651
|
+
return true;
|
5652
|
+
},
|
5653
|
+
expandbox: function expandbox(bbox, _bbox) {
|
5654
|
+
if (_bbox.x.min < bbox.x.min) {
|
5655
|
+
bbox.x.min = _bbox.x.min;
|
5656
|
+
}
|
5657
|
+
|
5658
|
+
if (_bbox.y.min < bbox.y.min) {
|
5659
|
+
bbox.y.min = _bbox.y.min;
|
5660
|
+
}
|
5661
|
+
|
5662
|
+
if (_bbox.z && _bbox.z.min < bbox.z.min) {
|
5663
|
+
bbox.z.min = _bbox.z.min;
|
5664
|
+
}
|
5665
|
+
|
5666
|
+
if (_bbox.x.max > bbox.x.max) {
|
5667
|
+
bbox.x.max = _bbox.x.max;
|
5668
|
+
}
|
5669
|
+
|
5670
|
+
if (_bbox.y.max > bbox.y.max) {
|
5671
|
+
bbox.y.max = _bbox.y.max;
|
5672
|
+
}
|
5673
|
+
|
5674
|
+
if (_bbox.z && _bbox.z.max > bbox.z.max) {
|
5675
|
+
bbox.z.max = _bbox.z.max;
|
5676
|
+
}
|
5677
|
+
|
5678
|
+
bbox.x.mid = (bbox.x.min + bbox.x.max) / 2;
|
5679
|
+
bbox.y.mid = (bbox.y.min + bbox.y.max) / 2;
|
5680
|
+
|
5681
|
+
if (bbox.z) {
|
5682
|
+
bbox.z.mid = (bbox.z.min + bbox.z.max) / 2;
|
5683
|
+
}
|
5684
|
+
|
5685
|
+
bbox.x.size = bbox.x.max - bbox.x.min;
|
5686
|
+
bbox.y.size = bbox.y.max - bbox.y.min;
|
5687
|
+
|
5688
|
+
if (bbox.z) {
|
5689
|
+
bbox.z.size = bbox.z.max - bbox.z.min;
|
5690
|
+
}
|
5691
|
+
},
|
5692
|
+
pairiteration: function pairiteration(c1, c2, curveIntersectionThreshold) {
|
5693
|
+
var c1b = c1.bbox(),
|
5694
|
+
c2b = c2.bbox(),
|
5695
|
+
r = 1e5,
|
5696
|
+
threshold = curveIntersectionThreshold || 0.5;
|
5697
|
+
|
5698
|
+
if (c1b.x.size + c1b.y.size < threshold && c2b.x.size + c2b.y.size < threshold) {
|
5699
|
+
return [(r * (c1._t1 + c1._t2) / 2 | 0) / r + "/" + (r * (c2._t1 + c2._t2) / 2 | 0) / r];
|
5700
|
+
}
|
5701
|
+
|
5702
|
+
var cc1 = c1.split(0.5),
|
5703
|
+
cc2 = c2.split(0.5),
|
5704
|
+
pairs = [{
|
5705
|
+
left: cc1.left,
|
5706
|
+
right: cc2.left
|
5707
|
+
}, {
|
5708
|
+
left: cc1.left,
|
5709
|
+
right: cc2.right
|
5710
|
+
}, {
|
5711
|
+
left: cc1.right,
|
5712
|
+
right: cc2.right
|
5713
|
+
}, {
|
5714
|
+
left: cc1.right,
|
5715
|
+
right: cc2.left
|
5716
|
+
}];
|
5717
|
+
pairs = pairs.filter(function (pair) {
|
5718
|
+
return utils.bboxoverlap(pair.left.bbox(), pair.right.bbox());
|
5719
|
+
});
|
5720
|
+
var results = [];
|
5721
|
+
if (pairs.length === 0) return results;
|
5722
|
+
pairs.forEach(function (pair) {
|
5723
|
+
results = results.concat(utils.pairiteration(pair.left, pair.right, threshold));
|
5724
|
+
});
|
5725
|
+
results = results.filter(function (v, i) {
|
5726
|
+
return results.indexOf(v) === i;
|
5727
|
+
});
|
5728
|
+
return results;
|
5729
|
+
},
|
5730
|
+
getccenter: function getccenter(p1, p2, p3) {
|
5731
|
+
var dx1 = p2.x - p1.x,
|
5732
|
+
dy1 = p2.y - p1.y,
|
5733
|
+
dx2 = p3.x - p2.x,
|
5734
|
+
dy2 = p3.y - p2.y,
|
5735
|
+
dx1p = dx1 * cos(quart) - dy1 * sin(quart),
|
5736
|
+
dy1p = dx1 * sin(quart) + dy1 * cos(quart),
|
5737
|
+
dx2p = dx2 * cos(quart) - dy2 * sin(quart),
|
5738
|
+
dy2p = dx2 * sin(quart) + dy2 * cos(quart),
|
5739
|
+
mx1 = (p1.x + p2.x) / 2,
|
5740
|
+
my1 = (p1.y + p2.y) / 2,
|
5741
|
+
mx2 = (p2.x + p3.x) / 2,
|
5742
|
+
my2 = (p2.y + p3.y) / 2,
|
5743
|
+
mx1n = mx1 + dx1p,
|
5744
|
+
my1n = my1 + dy1p,
|
5745
|
+
mx2n = mx2 + dx2p,
|
5746
|
+
my2n = my2 + dy2p,
|
5747
|
+
arc = utils.lli8(mx1, my1, mx1n, my1n, mx2, my2, mx2n, my2n),
|
5748
|
+
r = utils.dist(arc, p1);
|
5749
|
+
|
5750
|
+
var s = atan2(p1.y - arc.y, p1.x - arc.x),
|
5751
|
+
m = atan2(p2.y - arc.y, p2.x - arc.x),
|
5752
|
+
e = atan2(p3.y - arc.y, p3.x - arc.x),
|
5753
|
+
_;
|
5754
|
+
|
5755
|
+
if (s < e) {
|
5756
|
+
if (s > m || m > e) {
|
5757
|
+
s += tau;
|
5758
|
+
}
|
5759
|
+
|
5760
|
+
if (s > e) {
|
5761
|
+
_ = e;
|
5762
|
+
e = s;
|
5763
|
+
s = _;
|
5764
|
+
}
|
5765
|
+
} else {
|
5766
|
+
if (e < m && m < s) {
|
5767
|
+
_ = e;
|
5768
|
+
e = s;
|
5769
|
+
s = _;
|
5770
|
+
} else {
|
5771
|
+
e += tau;
|
5772
|
+
}
|
5773
|
+
}
|
5774
|
+
|
5775
|
+
arc.s = s;
|
5776
|
+
arc.e = e;
|
5777
|
+
arc.r = r;
|
5778
|
+
return arc;
|
5779
|
+
},
|
5780
|
+
numberSort: function numberSort(a, b) {
|
5781
|
+
return a - b;
|
5782
|
+
}
|
5783
|
+
}; // src/poly-bezier.js
|
5784
|
+
|
5785
|
+
var PolyBezier = /*#__PURE__*/function () {
|
5786
|
+
function PolyBezier(curves) {
|
5787
|
+
this.curves = [];
|
5788
|
+
this._3d = false;
|
5789
|
+
|
5790
|
+
if (!!curves) {
|
5791
|
+
this.curves = curves;
|
5792
|
+
this._3d = this.curves[0]._3d;
|
5793
|
+
}
|
5794
|
+
}
|
5795
|
+
|
5796
|
+
var _proto = PolyBezier.prototype;
|
5797
|
+
|
5798
|
+
_proto.valueOf = function valueOf() {
|
5799
|
+
return this.toString();
|
5800
|
+
};
|
5801
|
+
|
5802
|
+
_proto.toString = function toString() {
|
5803
|
+
return "[" + this.curves.map(function (curve) {
|
5804
|
+
return utils.pointsToString(curve.points);
|
5805
|
+
}).join(", ") + "]";
|
5806
|
+
};
|
5807
|
+
|
5808
|
+
_proto.addCurve = function addCurve(curve) {
|
5809
|
+
this.curves.push(curve);
|
5810
|
+
this._3d = this._3d || curve._3d;
|
5811
|
+
};
|
5812
|
+
|
5813
|
+
_proto.length = function length() {
|
5814
|
+
return this.curves.map(function (v) {
|
5815
|
+
return v.length();
|
5816
|
+
}).reduce(function (a, b) {
|
5817
|
+
return a + b;
|
5818
|
+
});
|
5819
|
+
};
|
5820
|
+
|
5821
|
+
_proto.curve = function curve(idx) {
|
5822
|
+
return this.curves[idx];
|
5823
|
+
};
|
5824
|
+
|
5825
|
+
_proto.bbox = function bbox() {
|
5826
|
+
var c = this.curves;
|
5827
|
+
var bbox = c[0].bbox();
|
5828
|
+
|
5829
|
+
for (var i = 1; i < c.length; i++) {
|
5830
|
+
utils.expandbox(bbox, c[i].bbox());
|
5831
|
+
}
|
5832
|
+
|
5833
|
+
return bbox;
|
5834
|
+
};
|
5835
|
+
|
5836
|
+
_proto.offset = function offset(d) {
|
5837
|
+
var offset = [];
|
5838
|
+
this.curves.forEach(function (v) {
|
5839
|
+
offset.push.apply(offset, v.offset(d));
|
5840
|
+
});
|
5841
|
+
return new PolyBezier(offset);
|
5842
|
+
};
|
5843
|
+
|
5844
|
+
return PolyBezier;
|
5845
|
+
}(); // src/bezier.js
|
5846
|
+
|
5847
|
+
|
5848
|
+
var abs2 = Math.abs,
|
5849
|
+
min = Math.min,
|
5850
|
+
max = Math.max,
|
5851
|
+
cos2 = Math.cos,
|
5852
|
+
sin2 = Math.sin,
|
5853
|
+
acos2 = Math.acos,
|
5854
|
+
sqrt2 = Math.sqrt;
|
5855
|
+
var pi2 = Math.PI;
|
5856
|
+
|
5857
|
+
var _Bezier = /*#__PURE__*/function () {
|
5858
|
+
function _Bezier(coords) {
|
5859
|
+
var args = coords && coords.forEach ? coords : Array.from(arguments).slice();
|
5860
|
+
var coordlen = false;
|
5861
|
+
|
5862
|
+
if (typeof args[0] === "object") {
|
5863
|
+
coordlen = args.length;
|
5864
|
+
var newargs = [];
|
5865
|
+
args.forEach(function (point2) {
|
5866
|
+
["x", "y", "z"].forEach(function (d) {
|
5867
|
+
if (typeof point2[d] !== "undefined") {
|
5868
|
+
newargs.push(point2[d]);
|
5869
|
+
}
|
5870
|
+
});
|
5871
|
+
});
|
5872
|
+
args = newargs;
|
5873
|
+
}
|
5874
|
+
|
5875
|
+
var higher = false;
|
5876
|
+
var len = args.length;
|
5877
|
+
|
5878
|
+
if (coordlen) {
|
5879
|
+
if (coordlen > 4) {
|
5880
|
+
if (arguments.length !== 1) {
|
5881
|
+
throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");
|
5882
|
+
}
|
5883
|
+
|
5884
|
+
higher = true;
|
5885
|
+
}
|
5886
|
+
} else {
|
5887
|
+
if (len !== 6 && len !== 8 && len !== 9 && len !== 12) {
|
5888
|
+
if (arguments.length !== 1) {
|
5889
|
+
throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");
|
5890
|
+
}
|
5891
|
+
}
|
5892
|
+
}
|
5893
|
+
|
5894
|
+
var _3d = this._3d = !higher && (len === 9 || len === 12) || coords && coords[0] && typeof coords[0].z !== "undefined";
|
5895
|
+
|
5896
|
+
var points = this.points = [];
|
5897
|
+
|
5898
|
+
for (var idx = 0, step = _3d ? 3 : 2; idx < len; idx += step) {
|
5899
|
+
var point = {
|
5900
|
+
x: args[idx],
|
5901
|
+
y: args[idx + 1]
|
5902
|
+
};
|
5903
|
+
|
5904
|
+
if (_3d) {
|
5905
|
+
point.z = args[idx + 2];
|
5906
|
+
}
|
5907
|
+
|
5908
|
+
points.push(point);
|
5909
|
+
}
|
5910
|
+
|
5911
|
+
var order = this.order = points.length - 1;
|
5912
|
+
var dims = this.dims = ["x", "y"];
|
5913
|
+
if (_3d) dims.push("z");
|
5914
|
+
this.dimlen = dims.length;
|
5915
|
+
var aligned = utils.align(points, {
|
5916
|
+
p1: points[0],
|
5917
|
+
p2: points[order]
|
5918
|
+
});
|
5919
|
+
var baselength = utils.dist(points[0], points[order]);
|
5920
|
+
this._linear = aligned.reduce(function (t2, p) {
|
5921
|
+
return t2 + abs2(p.y);
|
5922
|
+
}, 0) < baselength / 50;
|
5923
|
+
this._lut = [];
|
5924
|
+
this._t1 = 0;
|
5925
|
+
this._t2 = 1;
|
5926
|
+
this.update();
|
5927
|
+
}
|
5928
|
+
|
5929
|
+
_Bezier.quadraticFromPoints = function quadraticFromPoints(p1, p2, p3, t2) {
|
5930
|
+
if (typeof t2 === "undefined") {
|
5931
|
+
t2 = 0.5;
|
5932
|
+
}
|
5933
|
+
|
5934
|
+
if (t2 === 0) {
|
5935
|
+
return new _Bezier(p2, p2, p3);
|
5936
|
+
}
|
5937
|
+
|
5938
|
+
if (t2 === 1) {
|
5939
|
+
return new _Bezier(p1, p2, p2);
|
5940
|
+
}
|
5941
|
+
|
5942
|
+
var abc = _Bezier.getABC(2, p1, p2, p3, t2);
|
5943
|
+
|
5944
|
+
return new _Bezier(p1, abc.A, p3);
|
5945
|
+
};
|
5946
|
+
|
5947
|
+
_Bezier.cubicFromPoints = function cubicFromPoints(S, B, E, t2, d1) {
|
5948
|
+
if (typeof t2 === "undefined") {
|
5949
|
+
t2 = 0.5;
|
5950
|
+
}
|
5951
|
+
|
5952
|
+
var abc = _Bezier.getABC(3, S, B, E, t2);
|
5953
|
+
|
5954
|
+
if (typeof d1 === "undefined") {
|
5955
|
+
d1 = utils.dist(B, abc.C);
|
5956
|
+
}
|
5957
|
+
|
5958
|
+
var d2 = d1 * (1 - t2) / t2;
|
5959
|
+
var selen = utils.dist(S, E),
|
5960
|
+
lx = (E.x - S.x) / selen,
|
5961
|
+
ly = (E.y - S.y) / selen,
|
5962
|
+
bx1 = d1 * lx,
|
5963
|
+
by1 = d1 * ly,
|
5964
|
+
bx2 = d2 * lx,
|
5965
|
+
by2 = d2 * ly;
|
5966
|
+
var e1 = {
|
5967
|
+
x: B.x - bx1,
|
5968
|
+
y: B.y - by1
|
5969
|
+
},
|
5970
|
+
e2 = {
|
5971
|
+
x: B.x + bx2,
|
5972
|
+
y: B.y + by2
|
5973
|
+
},
|
5974
|
+
A = abc.A,
|
5975
|
+
v1 = {
|
5976
|
+
x: A.x + (e1.x - A.x) / (1 - t2),
|
5977
|
+
y: A.y + (e1.y - A.y) / (1 - t2)
|
5978
|
+
},
|
5979
|
+
v2 = {
|
5980
|
+
x: A.x + (e2.x - A.x) / t2,
|
5981
|
+
y: A.y + (e2.y - A.y) / t2
|
5982
|
+
},
|
5983
|
+
nc1 = {
|
5984
|
+
x: S.x + (v1.x - S.x) / t2,
|
5985
|
+
y: S.y + (v1.y - S.y) / t2
|
5986
|
+
},
|
5987
|
+
nc2 = {
|
5988
|
+
x: E.x + (v2.x - E.x) / (1 - t2),
|
5989
|
+
y: E.y + (v2.y - E.y) / (1 - t2)
|
5990
|
+
};
|
5991
|
+
return new _Bezier(S, nc1, nc2, E);
|
5992
|
+
};
|
5993
|
+
|
5994
|
+
_Bezier.getUtils = function getUtils() {
|
5995
|
+
return utils;
|
5996
|
+
};
|
5997
|
+
|
5998
|
+
var _proto2 = _Bezier.prototype;
|
5999
|
+
|
6000
|
+
_proto2.getUtils = function getUtils() {
|
6001
|
+
return _Bezier.getUtils();
|
6002
|
+
};
|
6003
|
+
|
6004
|
+
_proto2.valueOf = function valueOf() {
|
6005
|
+
return this.toString();
|
6006
|
+
};
|
6007
|
+
|
6008
|
+
_proto2.toString = function toString() {
|
6009
|
+
return utils.pointsToString(this.points);
|
6010
|
+
};
|
6011
|
+
|
6012
|
+
_proto2.toSVG = function toSVG() {
|
6013
|
+
if (this._3d) return false;
|
6014
|
+
var p = this.points,
|
6015
|
+
x = p[0].x,
|
6016
|
+
y = p[0].y,
|
6017
|
+
s = ["M", x, y, this.order === 2 ? "Q" : "C"];
|
6018
|
+
|
6019
|
+
for (var i = 1, last = p.length; i < last; i++) {
|
6020
|
+
s.push(p[i].x);
|
6021
|
+
s.push(p[i].y);
|
6022
|
+
}
|
6023
|
+
|
6024
|
+
return s.join(" ");
|
6025
|
+
};
|
6026
|
+
|
6027
|
+
_proto2.setRatios = function setRatios(ratios) {
|
6028
|
+
if (ratios.length !== this.points.length) {
|
6029
|
+
throw new Error("incorrect number of ratio values");
|
6030
|
+
}
|
6031
|
+
|
6032
|
+
this.ratios = ratios;
|
6033
|
+
this._lut = [];
|
6034
|
+
};
|
6035
|
+
|
6036
|
+
_proto2.verify = function verify() {
|
6037
|
+
var print = this.coordDigest();
|
6038
|
+
|
6039
|
+
if (print !== this._print) {
|
6040
|
+
this._print = print;
|
6041
|
+
this.update();
|
6042
|
+
}
|
6043
|
+
};
|
6044
|
+
|
6045
|
+
_proto2.coordDigest = function coordDigest() {
|
6046
|
+
return this.points.map(function (c, pos) {
|
6047
|
+
return "" + pos + c.x + c.y + (c.z ? c.z : 0);
|
6048
|
+
}).join("");
|
6049
|
+
};
|
6050
|
+
|
6051
|
+
_proto2.update = function update() {
|
6052
|
+
this._lut = [];
|
6053
|
+
this.dpoints = utils.derive(this.points, this._3d);
|
6054
|
+
this.computedirection();
|
6055
|
+
};
|
6056
|
+
|
6057
|
+
_proto2.computedirection = function computedirection() {
|
6058
|
+
var points = this.points;
|
6059
|
+
var angle = utils.angle(points[0], points[this.order], points[1]);
|
6060
|
+
this.clockwise = angle > 0;
|
6061
|
+
};
|
6062
|
+
|
6063
|
+
_proto2.length = function length() {
|
6064
|
+
return utils.length(this.derivative.bind(this));
|
6065
|
+
};
|
6066
|
+
|
6067
|
+
_Bezier.getABC = function getABC(order, S, B, E, t2) {
|
6068
|
+
if (order === void 0) {
|
6069
|
+
order = 2;
|
6070
|
+
}
|
6071
|
+
|
6072
|
+
if (t2 === void 0) {
|
6073
|
+
t2 = 0.5;
|
6074
|
+
}
|
6075
|
+
|
6076
|
+
var u = utils.projectionratio(t2, order),
|
6077
|
+
um = 1 - u,
|
6078
|
+
C = {
|
6079
|
+
x: u * S.x + um * E.x,
|
6080
|
+
y: u * S.y + um * E.y
|
6081
|
+
},
|
6082
|
+
s = utils.abcratio(t2, order),
|
6083
|
+
A = {
|
6084
|
+
x: B.x + (B.x - C.x) / s,
|
6085
|
+
y: B.y + (B.y - C.y) / s
|
6086
|
+
};
|
6087
|
+
return {
|
6088
|
+
A: A,
|
6089
|
+
B: B,
|
6090
|
+
C: C,
|
6091
|
+
S: S,
|
6092
|
+
E: E
|
6093
|
+
};
|
6094
|
+
};
|
6095
|
+
|
6096
|
+
_proto2.getABC = function getABC(t2, B) {
|
6097
|
+
B = B || this.get(t2);
|
6098
|
+
var S = this.points[0];
|
6099
|
+
var E = this.points[this.order];
|
6100
|
+
return _Bezier.getABC(this.order, S, B, E, t2);
|
6101
|
+
};
|
6102
|
+
|
6103
|
+
_proto2.getLUT = function getLUT(steps) {
|
6104
|
+
this.verify();
|
6105
|
+
steps = steps || 100;
|
6106
|
+
|
6107
|
+
if (this._lut.length === steps + 1) {
|
6108
|
+
return this._lut;
|
6109
|
+
}
|
6110
|
+
|
6111
|
+
this._lut = [];
|
6112
|
+
steps++;
|
6113
|
+
this._lut = [];
|
6114
|
+
|
6115
|
+
for (var i = 0, p, t2; i < steps; i++) {
|
6116
|
+
t2 = i / (steps - 1);
|
6117
|
+
p = this.compute(t2);
|
6118
|
+
p.t = t2;
|
6119
|
+
|
6120
|
+
this._lut.push(p);
|
6121
|
+
}
|
6122
|
+
|
6123
|
+
return this._lut;
|
6124
|
+
};
|
6125
|
+
|
6126
|
+
_proto2.on = function on(point, error) {
|
6127
|
+
error = error || 5;
|
6128
|
+
var lut = this.getLUT(),
|
6129
|
+
hits = [];
|
6130
|
+
|
6131
|
+
for (var i = 0, c, t2 = 0; i < lut.length; i++) {
|
6132
|
+
c = lut[i];
|
6133
|
+
|
6134
|
+
if (utils.dist(c, point) < error) {
|
6135
|
+
hits.push(c);
|
6136
|
+
t2 += i / lut.length;
|
6137
|
+
}
|
6138
|
+
}
|
6139
|
+
|
6140
|
+
if (!hits.length) return false;
|
6141
|
+
return t /= hits.length;
|
6142
|
+
};
|
6143
|
+
|
6144
|
+
_proto2.project = function project(point) {
|
6145
|
+
var LUT = this.getLUT(),
|
6146
|
+
l = LUT.length - 1,
|
6147
|
+
closest = utils.closest(LUT, point),
|
6148
|
+
mpos = closest.mpos,
|
6149
|
+
t1 = (mpos - 1) / l,
|
6150
|
+
t2 = (mpos + 1) / l,
|
6151
|
+
step = 0.1 / l;
|
6152
|
+
var mdist = closest.mdist,
|
6153
|
+
t3 = t1,
|
6154
|
+
ft = t3,
|
6155
|
+
p;
|
6156
|
+
mdist += 1;
|
6157
|
+
|
6158
|
+
for (var d; t3 < t2 + step; t3 += step) {
|
6159
|
+
p = this.compute(t3);
|
6160
|
+
d = utils.dist(point, p);
|
6161
|
+
|
6162
|
+
if (d < mdist) {
|
6163
|
+
mdist = d;
|
6164
|
+
ft = t3;
|
6165
|
+
}
|
6166
|
+
}
|
6167
|
+
|
6168
|
+
ft = ft < 0 ? 0 : ft > 1 ? 1 : ft;
|
6169
|
+
p = this.compute(ft);
|
6170
|
+
p.t = ft;
|
6171
|
+
p.d = mdist;
|
6172
|
+
return p;
|
6173
|
+
};
|
6174
|
+
|
6175
|
+
_proto2.get = function get(t2) {
|
6176
|
+
return this.compute(t2);
|
6177
|
+
};
|
6178
|
+
|
6179
|
+
_proto2.point = function point(idx) {
|
6180
|
+
return this.points[idx];
|
6181
|
+
};
|
6182
|
+
|
6183
|
+
_proto2.compute = function compute(t2) {
|
6184
|
+
if (this.ratios) {
|
6185
|
+
return utils.computeWithRatios(t2, this.points, this.ratios, this._3d);
|
6186
|
+
}
|
6187
|
+
|
6188
|
+
return utils.compute(t2, this.points, this._3d, this.ratios);
|
6189
|
+
};
|
6190
|
+
|
6191
|
+
_proto2.raise = function raise() {
|
6192
|
+
var p = this.points,
|
6193
|
+
np = [p[0]],
|
6194
|
+
k = p.length;
|
6195
|
+
|
6196
|
+
for (var i = 1, pi3, pim; i < k; i++) {
|
6197
|
+
pi3 = p[i];
|
6198
|
+
pim = p[i - 1];
|
6199
|
+
np[i] = {
|
6200
|
+
x: (k - i) / k * pi3.x + i / k * pim.x,
|
6201
|
+
y: (k - i) / k * pi3.y + i / k * pim.y
|
6202
|
+
};
|
6203
|
+
}
|
6204
|
+
|
6205
|
+
np[k] = p[k - 1];
|
6206
|
+
return new _Bezier(np);
|
6207
|
+
};
|
6208
|
+
|
6209
|
+
_proto2.derivative = function derivative(t2) {
|
6210
|
+
return utils.compute(t2, this.dpoints[0], this._3d);
|
6211
|
+
};
|
6212
|
+
|
6213
|
+
_proto2.dderivative = function dderivative(t2) {
|
6214
|
+
return utils.compute(t2, this.dpoints[1], this._3d);
|
6215
|
+
};
|
6216
|
+
|
6217
|
+
_proto2.align = function align() {
|
6218
|
+
var p = this.points;
|
6219
|
+
return new _Bezier(utils.align(p, {
|
6220
|
+
p1: p[0],
|
6221
|
+
p2: p[p.length - 1]
|
6222
|
+
}));
|
6223
|
+
};
|
6224
|
+
|
6225
|
+
_proto2.curvature = function curvature(t2) {
|
6226
|
+
return utils.curvature(t2, this.dpoints[0], this.dpoints[1], this._3d);
|
6227
|
+
};
|
6228
|
+
|
6229
|
+
_proto2.inflections = function inflections() {
|
6230
|
+
return utils.inflections(this.points);
|
6231
|
+
};
|
6232
|
+
|
6233
|
+
_proto2.normal = function normal(t2) {
|
6234
|
+
return this._3d ? this.__normal3(t2) : this.__normal2(t2);
|
6235
|
+
};
|
6236
|
+
|
6237
|
+
_proto2.__normal2 = function __normal2(t2) {
|
6238
|
+
var d = this.derivative(t2);
|
6239
|
+
var q = sqrt2(d.x * d.x + d.y * d.y);
|
6240
|
+
return {
|
6241
|
+
t: t2,
|
6242
|
+
x: -d.y / q,
|
6243
|
+
y: d.x / q
|
6244
|
+
};
|
6245
|
+
};
|
6246
|
+
|
6247
|
+
_proto2.__normal3 = function __normal3(t2) {
|
6248
|
+
var r1 = this.derivative(t2),
|
6249
|
+
r2 = this.derivative(t2 + 0.01),
|
6250
|
+
q1 = sqrt2(r1.x * r1.x + r1.y * r1.y + r1.z * r1.z),
|
6251
|
+
q2 = sqrt2(r2.x * r2.x + r2.y * r2.y + r2.z * r2.z);
|
6252
|
+
r1.x /= q1;
|
6253
|
+
r1.y /= q1;
|
6254
|
+
r1.z /= q1;
|
6255
|
+
r2.x /= q2;
|
6256
|
+
r2.y /= q2;
|
6257
|
+
r2.z /= q2;
|
6258
|
+
var c = {
|
6259
|
+
x: r2.y * r1.z - r2.z * r1.y,
|
6260
|
+
y: r2.z * r1.x - r2.x * r1.z,
|
6261
|
+
z: r2.x * r1.y - r2.y * r1.x
|
6262
|
+
};
|
6263
|
+
var m = sqrt2(c.x * c.x + c.y * c.y + c.z * c.z);
|
6264
|
+
c.x /= m;
|
6265
|
+
c.y /= m;
|
6266
|
+
c.z /= m;
|
6267
|
+
var R = [c.x * c.x, c.x * c.y - c.z, c.x * c.z + c.y, c.x * c.y + c.z, c.y * c.y, c.y * c.z - c.x, c.x * c.z - c.y, c.y * c.z + c.x, c.z * c.z];
|
6268
|
+
var n = {
|
6269
|
+
t: t2,
|
6270
|
+
x: R[0] * r1.x + R[1] * r1.y + R[2] * r1.z,
|
6271
|
+
y: R[3] * r1.x + R[4] * r1.y + R[5] * r1.z,
|
6272
|
+
z: R[6] * r1.x + R[7] * r1.y + R[8] * r1.z
|
6273
|
+
};
|
6274
|
+
return n;
|
6275
|
+
};
|
6276
|
+
|
6277
|
+
_proto2.hull = function hull(t2) {
|
6278
|
+
var p = this.points,
|
6279
|
+
_p = [],
|
6280
|
+
q = [],
|
6281
|
+
idx = 0;
|
6282
|
+
q[idx++] = p[0];
|
6283
|
+
q[idx++] = p[1];
|
6284
|
+
q[idx++] = p[2];
|
6285
|
+
|
6286
|
+
if (this.order === 3) {
|
6287
|
+
q[idx++] = p[3];
|
6288
|
+
}
|
6289
|
+
|
6290
|
+
while (p.length > 1) {
|
6291
|
+
_p = [];
|
6292
|
+
|
6293
|
+
for (var i = 0, pt, l = p.length - 1; i < l; i++) {
|
6294
|
+
pt = utils.lerp(t2, p[i], p[i + 1]);
|
6295
|
+
q[idx++] = pt;
|
6296
|
+
|
6297
|
+
_p.push(pt);
|
6298
|
+
}
|
6299
|
+
|
6300
|
+
p = _p;
|
6301
|
+
}
|
6302
|
+
|
6303
|
+
return q;
|
6304
|
+
};
|
6305
|
+
|
6306
|
+
_proto2.split = function split(t1, t2) {
|
6307
|
+
if (t1 === 0 && !!t2) {
|
6308
|
+
return this.split(t2).left;
|
6309
|
+
}
|
6310
|
+
|
6311
|
+
if (t2 === 1) {
|
6312
|
+
return this.split(t1).right;
|
6313
|
+
}
|
6314
|
+
|
6315
|
+
var q = this.hull(t1);
|
6316
|
+
var result = {
|
6317
|
+
left: this.order === 2 ? new _Bezier([q[0], q[3], q[5]]) : new _Bezier([q[0], q[4], q[7], q[9]]),
|
6318
|
+
right: this.order === 2 ? new _Bezier([q[5], q[4], q[2]]) : new _Bezier([q[9], q[8], q[6], q[3]]),
|
6319
|
+
span: q
|
6320
|
+
};
|
6321
|
+
result.left._t1 = utils.map(0, 0, 1, this._t1, this._t2);
|
6322
|
+
result.left._t2 = utils.map(t1, 0, 1, this._t1, this._t2);
|
6323
|
+
result.right._t1 = utils.map(t1, 0, 1, this._t1, this._t2);
|
6324
|
+
result.right._t2 = utils.map(1, 0, 1, this._t1, this._t2);
|
6325
|
+
|
6326
|
+
if (!t2) {
|
6327
|
+
return result;
|
6328
|
+
}
|
6329
|
+
|
6330
|
+
t2 = utils.map(t2, t1, 1, 0, 1);
|
6331
|
+
return result.right.split(t2).left;
|
6332
|
+
};
|
6333
|
+
|
6334
|
+
_proto2.extrema = function extrema() {
|
6335
|
+
var result = {};
|
6336
|
+
var roots = [];
|
6337
|
+
this.dims.forEach(function (dim) {
|
6338
|
+
var mfn = function mfn(v) {
|
6339
|
+
return v[dim];
|
6340
|
+
};
|
6341
|
+
|
6342
|
+
var p = this.dpoints[0].map(mfn);
|
6343
|
+
result[dim] = utils.droots(p);
|
6344
|
+
|
6345
|
+
if (this.order === 3) {
|
6346
|
+
p = this.dpoints[1].map(mfn);
|
6347
|
+
result[dim] = result[dim].concat(utils.droots(p));
|
6348
|
+
}
|
6349
|
+
|
6350
|
+
result[dim] = result[dim].filter(function (t2) {
|
6351
|
+
return t2 >= 0 && t2 <= 1;
|
6352
|
+
});
|
6353
|
+
roots = roots.concat(result[dim].sort(utils.numberSort));
|
6354
|
+
}.bind(this));
|
6355
|
+
result.values = roots.sort(utils.numberSort).filter(function (v, idx) {
|
6356
|
+
return roots.indexOf(v) === idx;
|
6357
|
+
});
|
6358
|
+
return result;
|
6359
|
+
};
|
6360
|
+
|
6361
|
+
_proto2.bbox = function bbox() {
|
6362
|
+
var extrema = this.extrema(),
|
6363
|
+
result = {};
|
6364
|
+
this.dims.forEach(function (d) {
|
6365
|
+
result[d] = utils.getminmax(this, d, extrema[d]);
|
6366
|
+
}.bind(this));
|
6367
|
+
return result;
|
6368
|
+
};
|
6369
|
+
|
6370
|
+
_proto2.overlaps = function overlaps(curve) {
|
6371
|
+
var lbbox = this.bbox(),
|
6372
|
+
tbbox = curve.bbox();
|
6373
|
+
return utils.bboxoverlap(lbbox, tbbox);
|
6374
|
+
};
|
6375
|
+
|
6376
|
+
_proto2.offset = function offset(t2, d) {
|
6377
|
+
if (typeof d !== "undefined") {
|
6378
|
+
var c = this.get(t2),
|
6379
|
+
n = this.normal(t2);
|
6380
|
+
var ret = {
|
6381
|
+
c: c,
|
6382
|
+
n: n,
|
6383
|
+
x: c.x + n.x * d,
|
6384
|
+
y: c.y + n.y * d
|
6385
|
+
};
|
6386
|
+
|
6387
|
+
if (this._3d) {
|
6388
|
+
ret.z = c.z + n.z * d;
|
6389
|
+
}
|
6390
|
+
|
6391
|
+
return ret;
|
6392
|
+
}
|
6393
|
+
|
6394
|
+
if (this._linear) {
|
6395
|
+
var nv = this.normal(0),
|
6396
|
+
coords = this.points.map(function (p) {
|
6397
|
+
var ret = {
|
6398
|
+
x: p.x + t2 * nv.x,
|
6399
|
+
y: p.y + t2 * nv.y
|
6400
|
+
};
|
6401
|
+
|
6402
|
+
if (p.z && nv.z) {
|
6403
|
+
ret.z = p.z + t2 * nv.z;
|
6404
|
+
}
|
6405
|
+
|
6406
|
+
return ret;
|
6407
|
+
});
|
6408
|
+
return [new _Bezier(coords)];
|
6409
|
+
}
|
6410
|
+
|
6411
|
+
return this.reduce().map(function (s) {
|
6412
|
+
if (s._linear) {
|
6413
|
+
return s.offset(t2)[0];
|
6414
|
+
}
|
6415
|
+
|
6416
|
+
return s.scale(t2);
|
6417
|
+
});
|
6418
|
+
};
|
6419
|
+
|
6420
|
+
_proto2.simple = function simple() {
|
6421
|
+
if (this.order === 3) {
|
6422
|
+
var a1 = utils.angle(this.points[0], this.points[3], this.points[1]);
|
6423
|
+
var a2 = utils.angle(this.points[0], this.points[3], this.points[2]);
|
6424
|
+
if (a1 > 0 && a2 < 0 || a1 < 0 && a2 > 0) return false;
|
6425
|
+
}
|
6426
|
+
|
6427
|
+
var n1 = this.normal(0);
|
6428
|
+
var n2 = this.normal(1);
|
6429
|
+
var s = n1.x * n2.x + n1.y * n2.y;
|
6430
|
+
|
6431
|
+
if (this._3d) {
|
6432
|
+
s += n1.z * n2.z;
|
6433
|
+
}
|
6434
|
+
|
6435
|
+
return abs2(acos2(s)) < pi2 / 3;
|
6436
|
+
};
|
6437
|
+
|
6438
|
+
_proto2.reduce = function reduce() {
|
6439
|
+
var i,
|
6440
|
+
t1 = 0,
|
6441
|
+
t2 = 0,
|
6442
|
+
step = 0.01,
|
6443
|
+
segment,
|
6444
|
+
pass1 = [],
|
6445
|
+
pass2 = [];
|
6446
|
+
var extrema = this.extrema().values;
|
6447
|
+
|
6448
|
+
if (extrema.indexOf(0) === -1) {
|
6449
|
+
extrema = [0].concat(extrema);
|
6450
|
+
}
|
6451
|
+
|
6452
|
+
if (extrema.indexOf(1) === -1) {
|
6453
|
+
extrema.push(1);
|
6454
|
+
}
|
6455
|
+
|
6456
|
+
for (t1 = extrema[0], i = 1; i < extrema.length; i++) {
|
6457
|
+
t2 = extrema[i];
|
6458
|
+
segment = this.split(t1, t2);
|
6459
|
+
segment._t1 = t1;
|
6460
|
+
segment._t2 = t2;
|
6461
|
+
pass1.push(segment);
|
6462
|
+
t1 = t2;
|
6463
|
+
}
|
6464
|
+
|
6465
|
+
pass1.forEach(function (p1) {
|
6466
|
+
t1 = 0;
|
6467
|
+
t2 = 0;
|
6468
|
+
|
6469
|
+
while (t2 <= 1) {
|
6470
|
+
for (t2 = t1 + step; t2 <= 1 + step; t2 += step) {
|
6471
|
+
segment = p1.split(t1, t2);
|
6472
|
+
|
6473
|
+
if (!segment.simple()) {
|
6474
|
+
t2 -= step;
|
6475
|
+
|
6476
|
+
if (abs2(t1 - t2) < step) {
|
6477
|
+
return [];
|
6478
|
+
}
|
6479
|
+
|
6480
|
+
segment = p1.split(t1, t2);
|
6481
|
+
segment._t1 = utils.map(t1, 0, 1, p1._t1, p1._t2);
|
6482
|
+
segment._t2 = utils.map(t2, 0, 1, p1._t1, p1._t2);
|
6483
|
+
pass2.push(segment);
|
6484
|
+
t1 = t2;
|
6485
|
+
break;
|
6486
|
+
}
|
6487
|
+
}
|
6488
|
+
}
|
6489
|
+
|
6490
|
+
if (t1 < 1) {
|
6491
|
+
segment = p1.split(t1, 1);
|
6492
|
+
segment._t1 = utils.map(t1, 0, 1, p1._t1, p1._t2);
|
6493
|
+
segment._t2 = p1._t2;
|
6494
|
+
pass2.push(segment);
|
6495
|
+
}
|
6496
|
+
});
|
6497
|
+
return pass2;
|
6498
|
+
};
|
6499
|
+
|
6500
|
+
_proto2.translate = function translate(v, d1, d2) {
|
6501
|
+
d2 = typeof d2 === "number" ? d2 : d1;
|
6502
|
+
var o = this.order;
|
6503
|
+
var d = this.points.map(function (_, i) {
|
6504
|
+
return (1 - i / o) * d1 + i / o * d2;
|
6505
|
+
});
|
6506
|
+
return new _Bezier(this.points.map(function (p, i) {
|
6507
|
+
return {
|
6508
|
+
x: p.x + v.x * d[i],
|
6509
|
+
y: p.y + v.y * d[i]
|
6510
|
+
};
|
6511
|
+
}));
|
6512
|
+
};
|
6513
|
+
|
6514
|
+
_proto2.scale = function scale(d) {
|
6515
|
+
var _this = this;
|
6516
|
+
|
6517
|
+
var order = this.order;
|
6518
|
+
var distanceFn = false;
|
6519
|
+
|
6520
|
+
if (typeof d === "function") {
|
6521
|
+
distanceFn = d;
|
6522
|
+
}
|
6523
|
+
|
6524
|
+
if (distanceFn && order === 2) {
|
6525
|
+
return this.raise().scale(distanceFn);
|
6526
|
+
}
|
6527
|
+
|
6528
|
+
var clockwise = this.clockwise;
|
6529
|
+
var points = this.points;
|
6530
|
+
|
6531
|
+
if (this._linear) {
|
6532
|
+
return this.translate(this.normal(0), distanceFn ? distanceFn(0) : d, distanceFn ? distanceFn(1) : d);
|
6533
|
+
}
|
6534
|
+
|
6535
|
+
var r1 = distanceFn ? distanceFn(0) : d;
|
6536
|
+
var r2 = distanceFn ? distanceFn(1) : d;
|
6537
|
+
var v = [this.offset(0, 10), this.offset(1, 10)];
|
6538
|
+
var np = [];
|
6539
|
+
var o = utils.lli4(v[0], v[0].c, v[1], v[1].c);
|
6540
|
+
|
6541
|
+
if (!o) {
|
6542
|
+
throw new Error("cannot scale this curve. Try reducing it first.");
|
6543
|
+
}
|
6544
|
+
|
6545
|
+
[0, 1].forEach(function (t2) {
|
6546
|
+
var p = np[t2 * order] = utils.copy(points[t2 * order]);
|
6547
|
+
p.x += (t2 ? r2 : r1) * v[t2].n.x;
|
6548
|
+
p.y += (t2 ? r2 : r1) * v[t2].n.y;
|
6549
|
+
});
|
6550
|
+
|
6551
|
+
if (!distanceFn) {
|
6552
|
+
[0, 1].forEach(function (t2) {
|
6553
|
+
if (order === 2 && !!t2) return;
|
6554
|
+
var p = np[t2 * order];
|
6555
|
+
|
6556
|
+
var d2 = _this.derivative(t2);
|
6557
|
+
|
6558
|
+
var p2 = {
|
6559
|
+
x: p.x + d2.x,
|
6560
|
+
y: p.y + d2.y
|
6561
|
+
};
|
6562
|
+
np[t2 + 1] = utils.lli4(p, p2, o, points[t2 + 1]);
|
6563
|
+
});
|
6564
|
+
return new _Bezier(np);
|
6565
|
+
}
|
6566
|
+
|
6567
|
+
[0, 1].forEach(function (t2) {
|
6568
|
+
if (order === 2 && !!t2) return;
|
6569
|
+
var p = points[t2 + 1];
|
6570
|
+
var ov = {
|
6571
|
+
x: p.x - o.x,
|
6572
|
+
y: p.y - o.y
|
6573
|
+
};
|
6574
|
+
var rc = distanceFn ? distanceFn((t2 + 1) / order) : d;
|
6575
|
+
if (distanceFn && !clockwise) rc = -rc;
|
6576
|
+
var m = sqrt2(ov.x * ov.x + ov.y * ov.y);
|
6577
|
+
ov.x /= m;
|
6578
|
+
ov.y /= m;
|
6579
|
+
np[t2 + 1] = {
|
6580
|
+
x: p.x + rc * ov.x,
|
6581
|
+
y: p.y + rc * ov.y
|
6582
|
+
};
|
6583
|
+
});
|
6584
|
+
return new _Bezier(np);
|
6585
|
+
};
|
6586
|
+
|
6587
|
+
_proto2.outline = function outline(d1, d2, d3, d4) {
|
6588
|
+
d2 = d2 === void 0 ? d1 : d2;
|
6589
|
+
|
6590
|
+
if (this._linear) {
|
6591
|
+
var n = this.normal(0);
|
6592
|
+
var start = this.points[0];
|
6593
|
+
var end = this.points[this.points.length - 1];
|
6594
|
+
var s, mid, e;
|
6595
|
+
|
6596
|
+
if (d3 === void 0) {
|
6597
|
+
d3 = d1;
|
6598
|
+
d4 = d2;
|
6599
|
+
}
|
6600
|
+
|
6601
|
+
s = {
|
6602
|
+
x: start.x + n.x * d1,
|
6603
|
+
y: start.y + n.y * d1
|
6604
|
+
};
|
6605
|
+
e = {
|
6606
|
+
x: end.x + n.x * d3,
|
6607
|
+
y: end.y + n.y * d3
|
6608
|
+
};
|
6609
|
+
mid = {
|
6610
|
+
x: (s.x + e.x) / 2,
|
6611
|
+
y: (s.y + e.y) / 2
|
6612
|
+
};
|
6613
|
+
var fline = [s, mid, e];
|
6614
|
+
s = {
|
6615
|
+
x: start.x - n.x * d2,
|
6616
|
+
y: start.y - n.y * d2
|
6617
|
+
};
|
6618
|
+
e = {
|
6619
|
+
x: end.x - n.x * d4,
|
6620
|
+
y: end.y - n.y * d4
|
6621
|
+
};
|
6622
|
+
mid = {
|
6623
|
+
x: (s.x + e.x) / 2,
|
6624
|
+
y: (s.y + e.y) / 2
|
6625
|
+
};
|
6626
|
+
var bline = [e, mid, s];
|
6627
|
+
var ls2 = utils.makeline(bline[2], fline[0]);
|
6628
|
+
var le2 = utils.makeline(fline[2], bline[0]);
|
6629
|
+
var segments2 = [ls2, new _Bezier(fline), le2, new _Bezier(bline)];
|
6630
|
+
return new PolyBezier(segments2);
|
6631
|
+
}
|
6632
|
+
|
6633
|
+
var reduced = this.reduce(),
|
6634
|
+
len = reduced.length,
|
6635
|
+
fcurves = [];
|
6636
|
+
var bcurves = [],
|
6637
|
+
p,
|
6638
|
+
alen = 0,
|
6639
|
+
tlen = this.length();
|
6640
|
+
var graduated = typeof d3 !== "undefined" && typeof d4 !== "undefined";
|
6641
|
+
|
6642
|
+
function linearDistanceFunction(s, e, tlen2, alen2, slen) {
|
6643
|
+
return function (v) {
|
6644
|
+
var f1 = alen2 / tlen2,
|
6645
|
+
f2 = (alen2 + slen) / tlen2,
|
6646
|
+
d = e - s;
|
6647
|
+
return utils.map(v, 0, 1, s + f1 * d, s + f2 * d);
|
6648
|
+
};
|
6649
|
+
}
|
6650
|
+
|
6651
|
+
reduced.forEach(function (segment) {
|
6652
|
+
var slen = segment.length();
|
6653
|
+
|
6654
|
+
if (graduated) {
|
6655
|
+
fcurves.push(segment.scale(linearDistanceFunction(d1, d3, tlen, alen, slen)));
|
6656
|
+
bcurves.push(segment.scale(linearDistanceFunction(-d2, -d4, tlen, alen, slen)));
|
6657
|
+
} else {
|
6658
|
+
fcurves.push(segment.scale(d1));
|
6659
|
+
bcurves.push(segment.scale(-d2));
|
6660
|
+
}
|
6661
|
+
|
6662
|
+
alen += slen;
|
6663
|
+
});
|
6664
|
+
bcurves = bcurves.map(function (s) {
|
6665
|
+
p = s.points;
|
6666
|
+
|
6667
|
+
if (p[3]) {
|
6668
|
+
s.points = [p[3], p[2], p[1], p[0]];
|
6669
|
+
} else {
|
6670
|
+
s.points = [p[2], p[1], p[0]];
|
6671
|
+
}
|
6672
|
+
|
6673
|
+
return s;
|
6674
|
+
}).reverse();
|
6675
|
+
var fs = fcurves[0].points[0],
|
6676
|
+
fe = fcurves[len - 1].points[fcurves[len - 1].points.length - 1],
|
6677
|
+
bs = bcurves[len - 1].points[bcurves[len - 1].points.length - 1],
|
6678
|
+
be = bcurves[0].points[0],
|
6679
|
+
ls = utils.makeline(bs, fs),
|
6680
|
+
le = utils.makeline(fe, be),
|
6681
|
+
segments = [ls].concat(fcurves).concat([le]).concat(bcurves);
|
6682
|
+
return new PolyBezier(segments);
|
6683
|
+
};
|
6684
|
+
|
6685
|
+
_proto2.outlineshapes = function outlineshapes(d1, d2, curveIntersectionThreshold) {
|
6686
|
+
d2 = d2 || d1;
|
6687
|
+
var outline = this.outline(d1, d2).curves;
|
6688
|
+
var shapes = [];
|
6689
|
+
|
6690
|
+
for (var i = 1, len = outline.length; i < len / 2; i++) {
|
6691
|
+
var shape = utils.makeshape(outline[i], outline[len - i], curveIntersectionThreshold);
|
6692
|
+
shape.startcap.virtual = i > 1;
|
6693
|
+
shape.endcap.virtual = i < len / 2 - 1;
|
6694
|
+
shapes.push(shape);
|
6695
|
+
}
|
6696
|
+
|
6697
|
+
return shapes;
|
6698
|
+
};
|
6699
|
+
|
6700
|
+
_proto2.intersects = function intersects(curve, curveIntersectionThreshold) {
|
6701
|
+
if (!curve) return this.selfintersects(curveIntersectionThreshold);
|
6702
|
+
|
6703
|
+
if (curve.p1 && curve.p2) {
|
6704
|
+
return this.lineIntersects(curve);
|
6705
|
+
}
|
6706
|
+
|
6707
|
+
if (curve instanceof _Bezier) {
|
6708
|
+
curve = curve.reduce();
|
6709
|
+
}
|
6710
|
+
|
6711
|
+
return this.curveintersects(this.reduce(), curve, curveIntersectionThreshold);
|
6712
|
+
};
|
6713
|
+
|
6714
|
+
_proto2.lineIntersects = function lineIntersects(line) {
|
6715
|
+
var _this2 = this;
|
6716
|
+
|
6717
|
+
var mx = min(line.p1.x, line.p2.x),
|
6718
|
+
my = min(line.p1.y, line.p2.y),
|
6719
|
+
MX = max(line.p1.x, line.p2.x),
|
6720
|
+
MY = max(line.p1.y, line.p2.y);
|
6721
|
+
return utils.roots(this.points, line).filter(function (t2) {
|
6722
|
+
var p = _this2.get(t2);
|
6723
|
+
|
6724
|
+
return utils.between(p.x, mx, MX) && utils.between(p.y, my, MY);
|
6725
|
+
});
|
6726
|
+
};
|
6727
|
+
|
6728
|
+
_proto2.selfintersects = function selfintersects(curveIntersectionThreshold) {
|
6729
|
+
var reduced = this.reduce(),
|
6730
|
+
len = reduced.length - 2,
|
6731
|
+
results = [];
|
6732
|
+
|
6733
|
+
for (var i = 0, result, left, right; i < len; i++) {
|
6734
|
+
left = reduced.slice(i, i + 1);
|
6735
|
+
right = reduced.slice(i + 2);
|
6736
|
+
result = this.curveintersects(left, right, curveIntersectionThreshold);
|
6737
|
+
results.push.apply(results, result);
|
6738
|
+
}
|
6739
|
+
|
6740
|
+
return results;
|
6741
|
+
};
|
6742
|
+
|
6743
|
+
_proto2.curveintersects = function curveintersects(c1, c2, curveIntersectionThreshold) {
|
6744
|
+
var pairs = [];
|
6745
|
+
c1.forEach(function (l) {
|
6746
|
+
c2.forEach(function (r) {
|
6747
|
+
if (l.overlaps(r)) {
|
6748
|
+
pairs.push({
|
6749
|
+
left: l,
|
6750
|
+
right: r
|
6751
|
+
});
|
6752
|
+
}
|
6753
|
+
});
|
6754
|
+
});
|
6755
|
+
var intersections = [];
|
6756
|
+
pairs.forEach(function (pair) {
|
6757
|
+
var result = utils.pairiteration(pair.left, pair.right, curveIntersectionThreshold);
|
6758
|
+
|
6759
|
+
if (result.length > 0) {
|
6760
|
+
intersections = intersections.concat(result);
|
6761
|
+
}
|
6762
|
+
});
|
6763
|
+
return intersections;
|
6764
|
+
};
|
6765
|
+
|
6766
|
+
_proto2.arcs = function arcs(errorThreshold) {
|
6767
|
+
errorThreshold = errorThreshold || 0.5;
|
6768
|
+
return this._iterate(errorThreshold, []);
|
6769
|
+
};
|
6770
|
+
|
6771
|
+
_proto2._error = function _error(pc, np1, s, e) {
|
6772
|
+
var q = (e - s) / 4,
|
6773
|
+
c1 = this.get(s + q),
|
6774
|
+
c2 = this.get(e - q),
|
6775
|
+
ref = utils.dist(pc, np1),
|
6776
|
+
d1 = utils.dist(pc, c1),
|
6777
|
+
d2 = utils.dist(pc, c2);
|
6778
|
+
return abs2(d1 - ref) + abs2(d2 - ref);
|
6779
|
+
};
|
6780
|
+
|
6781
|
+
_proto2._iterate = function _iterate(errorThreshold, circles) {
|
6782
|
+
var t_s = 0,
|
6783
|
+
t_e = 1,
|
6784
|
+
safety;
|
6785
|
+
|
6786
|
+
do {
|
6787
|
+
safety = 0;
|
6788
|
+
t_e = 1;
|
6789
|
+
var np1 = this.get(t_s),
|
6790
|
+
np2 = void 0,
|
6791
|
+
np3 = void 0,
|
6792
|
+
arc = void 0,
|
6793
|
+
prev_arc = void 0;
|
6794
|
+
var curr_good = false,
|
6795
|
+
prev_good = false,
|
6796
|
+
done = void 0;
|
6797
|
+
var t_m = t_e,
|
6798
|
+
prev_e = 1;
|
6799
|
+
|
6800
|
+
do {
|
6801
|
+
prev_good = curr_good;
|
6802
|
+
prev_arc = arc;
|
6803
|
+
t_m = (t_s + t_e) / 2;
|
6804
|
+
np2 = this.get(t_m);
|
6805
|
+
np3 = this.get(t_e);
|
6806
|
+
arc = utils.getccenter(np1, np2, np3);
|
6807
|
+
arc.interval = {
|
6808
|
+
start: t_s,
|
6809
|
+
end: t_e
|
6810
|
+
};
|
6811
|
+
|
6812
|
+
var error = this._error(arc, np1, t_s, t_e);
|
6813
|
+
|
6814
|
+
curr_good = error <= errorThreshold;
|
6815
|
+
done = prev_good && !curr_good;
|
6816
|
+
if (!done) prev_e = t_e;
|
6817
|
+
|
6818
|
+
if (curr_good) {
|
6819
|
+
if (t_e >= 1) {
|
6820
|
+
arc.interval.end = prev_e = 1;
|
6821
|
+
prev_arc = arc;
|
6822
|
+
|
6823
|
+
if (t_e > 1) {
|
6824
|
+
var d = {
|
6825
|
+
x: arc.x + arc.r * cos2(arc.e),
|
6826
|
+
y: arc.y + arc.r * sin2(arc.e)
|
6827
|
+
};
|
6828
|
+
arc.e += utils.angle({
|
6829
|
+
x: arc.x,
|
6830
|
+
y: arc.y
|
6831
|
+
}, d, this.get(1));
|
6832
|
+
}
|
6833
|
+
|
6834
|
+
break;
|
6835
|
+
}
|
6836
|
+
|
6837
|
+
t_e = t_e + (t_e - t_s) / 2;
|
6838
|
+
} else {
|
6839
|
+
t_e = t_m;
|
6840
|
+
}
|
6841
|
+
} while (!done && safety++ < 100);
|
6842
|
+
|
6843
|
+
if (safety >= 100) {
|
6844
|
+
break;
|
6845
|
+
}
|
6846
|
+
|
6847
|
+
prev_arc = prev_arc ? prev_arc : arc;
|
6848
|
+
circles.push(prev_arc);
|
6849
|
+
t_s = prev_e;
|
6850
|
+
} while (t_e < 1);
|
6851
|
+
|
6852
|
+
return circles;
|
6853
|
+
};
|
6854
|
+
|
6855
|
+
_createClass(_Bezier, null, [{
|
6856
|
+
key: "PolyBezier",
|
6857
|
+
get: function get() {
|
6858
|
+
return PolyBezier;
|
6859
|
+
}
|
6860
|
+
}]);
|
6861
|
+
|
6862
|
+
return _Bezier;
|
6863
|
+
}();
|
6864
|
+
|
6865
|
+
var bezier = __toCommonJS(bezier_exports); // Annotate the CommonJS export names for ESM import in node:
|
6866
|
+
|
6867
|
+
function lineEquation(pt1, pt2) {
|
6868
|
+
if (pt1.x === pt2.x) {
|
6869
|
+
return pt1.y === pt2.y ? null : { x: pt1.x };
|
6870
|
+
}
|
6871
|
+
var a = (pt2.y - pt1.y) / (pt2.x - pt1.x);
|
6872
|
+
return {
|
6873
|
+
a: a,
|
6874
|
+
b: pt1.y - a * pt1.x,
|
6875
|
+
};
|
6876
|
+
}
|
6877
|
+
function calZ(p1, p2, p) {
|
6878
|
+
const dx = p2.x - p1.x, dy = p2.y - p1.y;
|
6879
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
6880
|
+
const dx1 = p.x - p1.x, dy1 = p.y - p1.y;
|
6881
|
+
const dis = Math.sqrt(dx1 * dx1 + dy1 * dy1);
|
6882
|
+
const percent = dis / distance;
|
6883
|
+
const dz = p2.z - p1.z;
|
6884
|
+
return p1.z + dz * percent;
|
6885
|
+
}
|
6886
|
+
/**
|
6887
|
+
Return the intersection point of two lines defined by two points each
|
6888
|
+
Return null when there's no unique intersection
|
6889
|
+
*/
|
6890
|
+
function intersection(l1a, l1b, l2a, l2b) {
|
6891
|
+
var line1 = lineEquation(l1a, l1b);
|
6892
|
+
var line2 = lineEquation(l2a, l2b);
|
6893
|
+
if (line1 === null || line2 === null) {
|
6894
|
+
return null;
|
6895
|
+
}
|
6896
|
+
if (line1.hasOwnProperty('x')) {
|
6897
|
+
if (line2.hasOwnProperty('x')) {
|
6898
|
+
return null;
|
6899
|
+
}
|
6900
|
+
const p = {
|
6901
|
+
x: line1.x,
|
6902
|
+
y: line2.a * line1.x + line2.b,
|
6903
|
+
z: 0
|
6904
|
+
};
|
6905
|
+
p.z = calZ(l1a, l1b, p);
|
6906
|
+
return p;
|
6907
|
+
}
|
6908
|
+
if (line2.hasOwnProperty('x')) {
|
6909
|
+
const p = {
|
6910
|
+
x: line2.x,
|
6911
|
+
y: line1.a * line2.x + line1.b,
|
6912
|
+
z: 0
|
6913
|
+
};
|
6914
|
+
p.z = calZ(l1a, l1b, p);
|
6915
|
+
return p;
|
6916
|
+
}
|
6917
|
+
if (line1.a === line2.a) {
|
6918
|
+
return null;
|
6919
|
+
}
|
6920
|
+
var x = (line2.b - line1.b) / (line1.a - line2.a);
|
6921
|
+
const p = {
|
6922
|
+
x: x,
|
6923
|
+
y: line1.a * x + line1.b,
|
6924
|
+
z: 0
|
6925
|
+
};
|
6926
|
+
p.z = calZ(l1a, l1b, p);
|
6927
|
+
return p;
|
6928
|
+
}
|
6929
|
+
function translatePoint(pt, dist, heading) {
|
6930
|
+
return {
|
6931
|
+
x: pt.x + dist * Math.cos(heading),
|
6932
|
+
y: pt.y + dist * Math.sin(heading),
|
6933
|
+
z: pt.z || 0
|
6934
|
+
};
|
6935
|
+
}
|
6936
|
+
function offsetPointLine(points, distance) {
|
6937
|
+
var offsetSegments = [];
|
6938
|
+
for (let i = 1, len = points.length; i < len; i++) {
|
6939
|
+
let a = points[i - 1], b = points[i];
|
6940
|
+
const [x1, y1, z1] = a;
|
6941
|
+
const [x2, y2, z2] = b;
|
6942
|
+
if (x1 === x2 && y1 === y2) {
|
6943
|
+
continue;
|
6944
|
+
}
|
6945
|
+
a = {
|
6946
|
+
x: x1,
|
6947
|
+
y: y1,
|
6948
|
+
z: z1 || 0
|
6949
|
+
};
|
6950
|
+
b = {
|
6951
|
+
x: x2,
|
6952
|
+
y: y2,
|
6953
|
+
z: z2 || 0
|
6954
|
+
};
|
6955
|
+
var segmentAngle = Math.atan2(a.y - b.y, a.x - b.x);
|
6956
|
+
var offsetAngle = segmentAngle - Math.PI / 2;
|
6957
|
+
offsetSegments.push({
|
6958
|
+
offsetAngle: offsetAngle,
|
6959
|
+
original: [a, b],
|
6960
|
+
offset: [
|
6961
|
+
translatePoint(a, distance, offsetAngle),
|
6962
|
+
translatePoint(b, distance, offsetAngle)
|
6963
|
+
]
|
6964
|
+
});
|
6965
|
+
}
|
6966
|
+
return offsetSegments;
|
6967
|
+
}
|
6968
|
+
/**
|
6969
|
+
Join 2 line segments defined by 2 points each with a circular arc
|
6970
|
+
*/
|
6971
|
+
function joinSegments(s1, s2, offset) {
|
6972
|
+
// TODO: different join styles
|
6973
|
+
return circularArc(s1, s2, offset)
|
6974
|
+
.filter(function (x) { return x; });
|
6975
|
+
}
|
6976
|
+
function joinLineSegments(segments, offset) {
|
6977
|
+
var joinedPoints = [];
|
6978
|
+
var first = segments[0];
|
6979
|
+
var last = segments[segments.length - 1];
|
6980
|
+
if (first && last) {
|
6981
|
+
joinedPoints.push(first.offset[0]);
|
6982
|
+
for (let i = 1, len = segments.length; i < len; i++) {
|
6983
|
+
let s1 = segments[i - 1], s2 = segments[i];
|
6984
|
+
const pts = joinSegments(s1, s2, offset);
|
6985
|
+
mergeArray(joinedPoints, pts);
|
6986
|
+
}
|
6987
|
+
joinedPoints.push(last.offset[1]);
|
6988
|
+
}
|
6989
|
+
return joinedPoints;
|
6990
|
+
}
|
6991
|
+
function segmentAsVector(s) {
|
6992
|
+
return {
|
6993
|
+
x: s[1].x - s[0].x,
|
6994
|
+
y: s[1].y - s[0].y,
|
6995
|
+
};
|
6996
|
+
}
|
6997
|
+
function getSignedAngle(s1, s2) {
|
6998
|
+
const a = segmentAsVector(s1);
|
6999
|
+
const b = segmentAsVector(s2);
|
7000
|
+
return Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y);
|
7001
|
+
}
|
7002
|
+
/**
|
7003
|
+
Interpolates points between two offset segments in a circular form
|
7004
|
+
*/
|
7005
|
+
function circularArc(s1, s2, distance) {
|
7006
|
+
// if the segments are the same angle,
|
7007
|
+
// there should be a single join point
|
7008
|
+
if (s1.offsetAngle === s2.offsetAngle) {
|
7009
|
+
return [s1.offset[1]];
|
7010
|
+
}
|
7011
|
+
const signedAngle = getSignedAngle(s1.offset, s2.offset);
|
7012
|
+
// for inner angles, just find the offset segments intersection
|
7013
|
+
if ((signedAngle * distance > 0) &&
|
7014
|
+
(signedAngle * getSignedAngle(s1.offset, [s1.offset[0], s2.offset[1]]) > 0)) {
|
7015
|
+
return [intersection(s1.offset[0], s1.offset[1], s2.offset[0], s2.offset[1])];
|
7016
|
+
}
|
7017
|
+
// draws a circular arc with R = offset distance, C = original meeting point
|
7018
|
+
var points = [];
|
7019
|
+
var center = s1.original[1];
|
5026
7020
|
// ensure angles go in the anti-clockwise direction
|
5027
7021
|
var rightOffset = distance > 0;
|
5028
7022
|
var startAngle = rightOffset ? s2.offsetAngle : s1.offsetAngle;
|
@@ -5059,6 +7053,62 @@ function polylineOffset(line, options) {
|
|
5059
7053
|
}
|
5060
7054
|
return result;
|
5061
7055
|
}
|
7056
|
+
function polylineRound(line, options) {
|
7057
|
+
options = Object.assign({ roundSize: 0, steps: 10 }, options);
|
7058
|
+
if (options.roundSize === 0) {
|
7059
|
+
return line;
|
7060
|
+
}
|
7061
|
+
if (!line || line.length < 3) {
|
7062
|
+
return line;
|
7063
|
+
}
|
7064
|
+
const len = line.length;
|
7065
|
+
const { roundSize, steps } = options;
|
7066
|
+
const points = [line[0]];
|
7067
|
+
let pre = line[0];
|
7068
|
+
let idx = 0;
|
7069
|
+
for (let i = 1; i < len; i++) {
|
7070
|
+
const p1 = line[i - 1], p2 = line[i], p3 = line[i + 1];
|
7071
|
+
if (!p3 || !p1 || !p2) {
|
7072
|
+
continue;
|
7073
|
+
}
|
7074
|
+
if (pointEqual(pre, p2)) {
|
7075
|
+
continue;
|
7076
|
+
}
|
7077
|
+
const d1 = pointDistance(p2, p1), d2 = pointDistance(p2, p3);
|
7078
|
+
if (d1 < roundSize || d2 < roundSize) {
|
7079
|
+
pre = p2;
|
7080
|
+
points[++idx] = p2;
|
7081
|
+
continue;
|
7082
|
+
}
|
7083
|
+
const dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1];
|
7084
|
+
const dx2 = p3[0] - p2[0], dy2 = p3[1] - p2[1];
|
7085
|
+
const percent1 = (d1 - roundSize) / d1;
|
7086
|
+
const percent2 = roundSize / d2;
|
7087
|
+
const c1 = {
|
7088
|
+
x: p1[0] + percent1 * dx1,
|
7089
|
+
y: p1[1] + percent1 * dy1
|
7090
|
+
};
|
7091
|
+
const c2 = {
|
7092
|
+
x: p2[0] + percent2 * dx2,
|
7093
|
+
y: p2[1] + percent2 * dy2
|
7094
|
+
};
|
7095
|
+
const d3 = pointDistance([c1.x, c1.y], [c2.x, c2.y]);
|
7096
|
+
if (d3 < roundSize / 10) {
|
7097
|
+
pre = p2;
|
7098
|
+
points[++idx] = p2;
|
7099
|
+
continue;
|
7100
|
+
}
|
7101
|
+
const be = new bezier.Bezier([c1, { x: p2[0], y: p2[1] }, c2]);
|
7102
|
+
const path = be.getLUT(steps);
|
7103
|
+
for (let j = 0, len1 = path.length; j < len1; j++) {
|
7104
|
+
const p = path[j];
|
7105
|
+
points[++idx] = [p.x, p.y];
|
7106
|
+
}
|
7107
|
+
pre = p2;
|
7108
|
+
}
|
7109
|
+
points.push(line[len - 1]);
|
7110
|
+
return points;
|
7111
|
+
}
|
5062
7112
|
|
5063
|
-
export { cylinder, expandLine, expandPaths, expandTubes, extrudePolygons, extrudePolygonsOnPath, extrudePolylines, extrudeSlopes, isClockwise, leftOnLine, merge, plane, polygons, polylineOffset };
|
7113
|
+
export { cylinder, expandLine, expandPaths, expandTubes, extrudePolygons, extrudePolygonsOnPath, extrudePolylines, extrudeSlopes, isClockwise, leftOnLine, merge, plane, polygons, polylineOffset, polylineRound };
|
5064
7114
|
//# sourceMappingURL=poly-extrude.mjs.map
|