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