flo-mat 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/browser/index.js +8230 -8229
  2. package/browser/index.min.js +1 -1
  3. package/node/circle.d.ts +20 -20
  4. package/node/circle.js +31 -31
  5. package/node/compare-curvatures-at-interface.d.ts +19 -19
  6. package/node/compare-curvatures-at-interface.js +124 -124
  7. package/node/contact-point.d.ts +26 -26
  8. package/node/contact-point.js +25 -25
  9. package/node/cp-node/clone.d.ts +8 -8
  10. package/node/cp-node/clone.js +31 -31
  11. package/node/cp-node/remove.d.ts +9 -9
  12. package/node/cp-node/remove.js +19 -19
  13. package/node/cp-node.d.ts +202 -202
  14. package/node/cp-node.js +320 -320
  15. package/node/curve.d.ts +46 -46
  16. package/node/curve.js +88 -88
  17. package/node/debug/cp-node-for-debugging.d.ts +11 -11
  18. package/node/debug/cp-node-for-debugging.js +1 -1
  19. package/node/debug/debug-elem-types.d.ts +32 -32
  20. package/node/debug/debug-elem-types.js +1 -1
  21. package/node/debug/debug.d.ts +50 -50
  22. package/node/debug/debug.js +75 -75
  23. package/node/debug/functions/draw-elem/bounding-hull.d.ts +3 -3
  24. package/node/debug/functions/draw-elem/bounding-hull.js +7 -7
  25. package/node/debug/functions/draw-elem/branch.d.ts +4 -4
  26. package/node/debug/functions/draw-elem/branch.js +22 -22
  27. package/node/debug/functions/draw-elem/culls.d.ts +4 -4
  28. package/node/debug/functions/draw-elem/culls.js +21 -21
  29. package/node/debug/functions/draw-elem/draw-circle-percent.d.ts +3 -3
  30. package/node/debug/functions/draw-elem/draw-circle-percent.js +12 -12
  31. package/node/debug/functions/draw-elem/draw-elem.d.ts +8 -8
  32. package/node/debug/functions/draw-elem/draw-elem.js +45 -45
  33. package/node/debug/functions/draw-elem/dull-corner.d.ts +4 -4
  34. package/node/debug/functions/draw-elem/dull-corner.js +10 -10
  35. package/node/debug/functions/draw-elem/leaves.d.ts +4 -4
  36. package/node/debug/functions/draw-elem/leaves.js +12 -12
  37. package/node/debug/functions/draw-elem/loop.d.ts +4 -4
  38. package/node/debug/functions/draw-elem/loop.js +11 -11
  39. package/node/debug/functions/draw-elem/loops.d.ts +4 -4
  40. package/node/debug/functions/draw-elem/loops.js +6 -6
  41. package/node/debug/functions/draw-elem/loose-bounding-box.d.ts +3 -3
  42. package/node/debug/functions/draw-elem/loose-bounding-box.js +9 -9
  43. package/node/debug/functions/draw-elem/mat.d.ts +4 -4
  44. package/node/debug/functions/draw-elem/mat.js +30 -30
  45. package/node/debug/functions/draw-elem/max-vertex.d.ts +4 -4
  46. package/node/debug/functions/draw-elem/max-vertex.js +8 -8
  47. package/node/debug/functions/draw-elem/min-y.d.ts +4 -4
  48. package/node/debug/functions/draw-elem/min-y.js +13 -13
  49. package/node/debug/functions/draw-elem/one-prong-at-dull-corner.d.ts +4 -4
  50. package/node/debug/functions/draw-elem/one-prong-at-dull-corner.js +12 -12
  51. package/node/debug/functions/draw-elem/one-prong.d.ts +4 -4
  52. package/node/debug/functions/draw-elem/one-prong.js +16 -16
  53. package/node/debug/functions/draw-elem/sharp-corner.d.ts +4 -4
  54. package/node/debug/functions/draw-elem/sharp-corner.js +9 -9
  55. package/node/debug/functions/draw-elem/three-prong.d.ts +4 -4
  56. package/node/debug/functions/draw-elem/three-prong.js +16 -16
  57. package/node/debug/functions/draw-elem/tight-bounding-box.d.ts +3 -3
  58. package/node/debug/functions/draw-elem/tight-bounding-box.js +7 -7
  59. package/node/debug/functions/draw-elem/two-prong.d.ts +4 -4
  60. package/node/debug/functions/draw-elem/two-prong.js +36 -36
  61. package/node/debug/functions/draw-elem/vertex.d.ts +4 -4
  62. package/node/debug/functions/draw-elem/vertex.js +27 -27
  63. package/node/debug/functions/general.d.ts +48 -48
  64. package/node/debug/functions/general.js +52 -52
  65. package/node/debug/functions/three-prong.d.ts +11 -11
  66. package/node/debug/functions/three-prong.js +184 -184
  67. package/node/debug/functions/two-prong.d.ts +13 -13
  68. package/node/debug/functions/two-prong.js +117 -117
  69. package/node/debug/three-prong-for-debugging.d.ts +31 -31
  70. package/node/debug/three-prong-for-debugging.js +20 -20
  71. package/node/debug/two-prong-for-debugging.d.ts +26 -26
  72. package/node/debug/two-prong-for-debugging.js +1 -1
  73. package/node/find-mats.d.ts +26 -26
  74. package/node/find-mats.js +122 -122
  75. package/node/get-boundary-bezier-parts-to-next.d.ts +9 -9
  76. package/node/get-boundary-bezier-parts-to-next.js +42 -42
  77. package/node/get-boundary-beziers-to-next.d.ts +8 -8
  78. package/node/get-boundary-beziers-to-next.js +44 -44
  79. package/node/get-bounding-box-.d.ts +2 -2
  80. package/node/get-bounding-box-.js +4 -4
  81. package/node/get-branches.d.ts +12 -12
  82. package/node/get-branches.js +68 -68
  83. package/node/get-curve/get-curve-between.d.ts +9 -9
  84. package/node/get-curve/get-curve-between.js +56 -56
  85. package/node/get-curve/get-edge-direction.d.ts +9 -9
  86. package/node/get-curve/get-edge-direction.js +53 -53
  87. package/node/get-curve-to-next.d.ts +9 -9
  88. package/node/get-curve-to-next.js +11 -11
  89. package/node/get-interface-ccw.d.ts +24 -24
  90. package/node/get-interface-ccw.js +97 -97
  91. package/node/index.d.ts +36 -36
  92. package/node/index.js +32 -32
  93. package/node/loop/get-max-coordinate.d.ts +10 -10
  94. package/node/loop/get-max-coordinate.js +43 -43
  95. package/node/loop.d.ts +16 -16
  96. package/node/loop.js +34 -34
  97. package/node/mat/add-to-cp-graph.d.ts +16 -16
  98. package/node/mat/add-to-cp-graph.js +30 -30
  99. package/node/mat/bezier-piece.d.ts +12 -12
  100. package/node/mat/bezier-piece.js +1 -1
  101. package/node/mat/closest-boundary-point/closest-points-on-curve.d.ts +14 -14
  102. package/node/mat/closest-boundary-point/closest-points-on-curve.js +72 -72
  103. package/node/mat/closest-boundary-point/cull-bezier-pieces.d.ts +9 -9
  104. package/node/mat/closest-boundary-point/cull-bezier-pieces.js +19 -19
  105. package/node/mat/closest-boundary-point/cull-by-loose-bounding-box.d.ts +11 -11
  106. package/node/mat/closest-boundary-point/cull-by-loose-bounding-box.js +24 -24
  107. package/node/mat/closest-boundary-point/cull-by-tight-boundary-box.d.ts +11 -11
  108. package/node/mat/closest-boundary-point/cull-by-tight-boundary-box.js +26 -26
  109. package/node/mat/closest-boundary-point/get-best-distance-squared.d.ts +8 -8
  110. package/node/mat/closest-boundary-point/get-best-distance-squared.js +24 -24
  111. package/node/mat/closest-boundary-point/get-close-boundary-points.d.ts +17 -17
  112. package/node/mat/closest-boundary-point/get-close-boundary-points.js +80 -80
  113. package/node/mat/closest-boundary-point/get-closest-boundary-point.d.ts +18 -18
  114. package/node/mat/closest-boundary-point/get-closest-boundary-point.js +40 -40
  115. package/node/mat/corner.d.ts +37 -37
  116. package/node/mat/corner.js +1 -1
  117. package/node/mat/create-new-cp-tree.d.ts +9 -9
  118. package/node/mat/create-new-cp-tree.js +23 -23
  119. package/node/mat/elem-type-two-prong.d.ts +3 -3
  120. package/node/mat/elem-type-two-prong.js +1 -1
  121. package/node/mat/find-mat/add-1-prong.d.ts +12 -12
  122. package/node/mat/find-mat/add-1-prong.js +42 -42
  123. package/node/mat/find-mat/add-2-prong.d.ts +21 -21
  124. package/node/mat/find-mat/add-2-prong.js +192 -192
  125. package/node/mat/find-mat/add-3-prong.d.ts +19 -19
  126. package/node/mat/find-mat/add-3-prong.js +54 -54
  127. package/node/mat/find-mat/add-debug-info.d.ts +9 -9
  128. package/node/mat/find-mat/add-debug-info.js +67 -67
  129. package/node/mat/find-mat/create-get-interesting-points-on-loop.d.ts +11 -11
  130. package/node/mat/find-mat/create-get-interesting-points-on-loop.js +51 -51
  131. package/node/mat/find-mat/create-initial-cp-graph.d.ts +12 -12
  132. package/node/mat/find-mat/create-initial-cp-graph.js +40 -40
  133. package/node/mat/find-mat/find-2-prong/add-debug-info.d.ts +8 -8
  134. package/node/mat/find-mat/find-2-prong/add-debug-info.js +21 -21
  135. package/node/mat/find-mat/find-2-prong/cull-bezier-pieces.d.ts +11 -11
  136. package/node/mat/find-mat/find-2-prong/cull-bezier-pieces.js +29 -29
  137. package/node/mat/find-mat/find-2-prong/find-2-prong.d.ts +37 -37
  138. package/node/mat/find-mat/find-2-prong/find-2-prong.js +232 -232
  139. package/node/mat/find-mat/find-2-prong/find-equidistant-point-on-line.d.ts +9 -9
  140. package/node/mat/find-mat/find-2-prong/find-equidistant-point-on-line.js +39 -39
  141. package/node/mat/find-mat/find-2-prong/get-initial-bezier-pieces.d.ts +11 -11
  142. package/node/mat/find-mat/find-2-prong/get-initial-bezier-pieces.js +37 -37
  143. package/node/mat/find-mat/find-2-prong/x-for-debugging.d.ts +9 -9
  144. package/node/mat/find-mat/find-2-prong/x-for-debugging.js +1 -1
  145. package/node/mat/find-mat/find-3-prong/calc-better-x.d.ts +19 -19
  146. package/node/mat/find-mat/find-3-prong/calc-better-x.js +36 -36
  147. package/node/mat/find-mat/find-3-prong/calc-initial-3-prong-center.d.ts +13 -13
  148. package/node/mat/find-mat/find-3-prong/calc-initial-3-prong-center.js +25 -25
  149. package/node/mat/find-mat/find-3-prong/find-3-prong-for-delta3s.d.ts +22 -22
  150. package/node/mat/find-mat/find-3-prong/find-3-prong-for-delta3s.js +159 -159
  151. package/node/mat/find-mat/find-3-prong/find-3-prong.d.ts +16 -16
  152. package/node/mat/find-mat/find-3-prong/find-3-prong.js +76 -76
  153. package/node/mat/find-mat/find-3-prong/get-closest-points.d.ts +9 -9
  154. package/node/mat/find-mat/find-3-prong/get-closest-points.js +16 -16
  155. package/node/mat/find-mat/find-and-add-2-prongs-on-all-paths.d.ts +15 -15
  156. package/node/mat/find-mat/find-and-add-2-prongs-on-all-paths.js +20 -20
  157. package/node/mat/find-mat/find-and-add-2-prongs.d.ts +16 -16
  158. package/node/mat/find-mat/find-and-add-2-prongs.js +73 -73
  159. package/node/mat/find-mat/find-and-add-3-prongs.d.ts +13 -13
  160. package/node/mat/find-mat/find-and-add-3-prongs.js +146 -146
  161. package/node/mat/find-mat/find-and-add-hole-closing-2-prongs.d.ts +14 -14
  162. package/node/mat/find-mat/find-and-add-hole-closing-2-prongs.js +33 -33
  163. package/node/mat/find-mat/get-potential-2-prongs.d.ts +4 -4
  164. package/node/mat/find-mat/get-potential-2-prongs.js +16 -16
  165. package/node/mat/find-mat/get-sharp-corners.d.ts +4 -4
  166. package/node/mat/find-mat/get-sharp-corners.js +17 -17
  167. package/node/mat/geometry/get-closest-square-distance-to-rect.d.ts +5 -5
  168. package/node/mat/geometry/get-closest-square-distance-to-rect.js +49 -49
  169. package/node/mat/geometry/get-closest-squared-distance-to-rotated-rect.d.ts +5 -5
  170. package/node/mat/geometry/get-closest-squared-distance-to-rotated-rect.js +15 -15
  171. package/node/mat/get-boundary-piece-beziers.d.ts +11 -11
  172. package/node/mat/get-boundary-piece-beziers.js +58 -58
  173. package/node/mat/get-contact-circles-at-interface.d.ts +8 -8
  174. package/node/mat/get-contact-circles-at-interface.js +20 -20
  175. package/node/mat/get-largest-vertex.d.ts +4 -4
  176. package/node/mat/get-largest-vertex.js +9 -9
  177. package/node/mat/get-leaves.d.ts +4 -4
  178. package/node/mat/get-leaves.js +12 -12
  179. package/node/mat/get-neighboring-cps.d.ts +14 -14
  180. package/node/mat/get-neighboring-cps.js +30 -30
  181. package/node/mat/get-two-prong-type.d.ts +5 -5
  182. package/node/mat/get-two-prong-type.js +8 -8
  183. package/node/mat/is-another-cp-closeby.d.ts +20 -20
  184. package/node/mat/is-another-cp-closeby.js +55 -55
  185. package/node/mat/simplify-mat-map-only.d.ts +23 -23
  186. package/node/mat/simplify-mat-map-only.js +80 -80
  187. package/node/mat/to-scale-axis/add-debug-info.d.ts +7 -7
  188. package/node/mat/to-scale-axis/add-debug-info.js +13 -13
  189. package/node/mat/to-scale-axis/cull-non-cycles.d.ts +9 -9
  190. package/node/mat/to-scale-axis/cull-non-cycles.js +51 -51
  191. package/node/mat/to-scale-axis/cull.d.ts +12 -12
  192. package/node/mat/to-scale-axis/cull.js +132 -132
  193. package/node/mat/trim-mat.d.ts +8 -8
  194. package/node/mat/trim-mat.js +16 -16
  195. package/node/mat.d.ts +22 -22
  196. package/node/mat.js +1 -1
  197. package/node/point-on-shape.d.ts +73 -73
  198. package/node/point-on-shape.js +167 -167
  199. package/node/simplify-mat.d.ts +16 -16
  200. package/node/simplify-mat.js +71 -71
  201. package/node/svg/get-loop-bounds.d.ts +12 -12
  202. package/node/svg/get-loop-bounds.js +80 -80
  203. package/node/svg/get-min-y-pos.d.ts +8 -8
  204. package/node/svg/get-min-y-pos.js +9 -9
  205. package/node/svg/get-shape-bounds.d.ts +9 -9
  206. package/node/svg/get-shape-bounds.js +35 -35
  207. package/node/sweep-line/sweep-line.d.ts +18 -18
  208. package/node/sweep-line/sweep-line.js +72 -72
  209. package/node/to-scale-axis.d.ts +16 -16
  210. package/node/to-scale-axis.js +74 -74
  211. package/node/traverse-edges.d.ts +10 -10
  212. package/node/traverse-edges.js +21 -21
  213. package/node/traverse-vertices.d.ts +12 -12
  214. package/node/traverse-vertices.js +27 -27
  215. package/package.json +11 -11
  216. package/tsconfig.json +0 -1
@@ -1,160 +1,160 @@
1
- import { fromTo, circumCenter, len, distanceBetween, toUnitVector, rotate90Degrees, cross } from 'flo-vector2d';
2
- import { tangent } from 'flo-bezier3';
3
- import { isPosDullCorner } from '../../../point-on-shape.js';
4
- import { getClosestBoundaryPoint } from '../../closest-boundary-point/get-closest-boundary-point.js';
5
- import { calcInitial3ProngCenter } from './calc-initial-3-prong-center.js';
6
- import { getClosestPoints } from './get-closest-points.js';
7
- import { calcBetterX } from './calc-better-x.js';
8
- import { getCornerAtEnd } from '../../../curve.js';
9
- /** @hidden */
10
- const calcVectorToZeroV_StraightToIt = fromTo;
11
- /**
12
- * @hidden
13
- * Finds a 3-prong using only the 3 given δs.
14
- * @param δs The boundary pieces
15
- * @param idx δ identifier
16
- * @param bezierPiecess
17
- * @param extreme The maximum coordinate value used to calculate floating point
18
- * tolerances.
19
- */
20
- function find3ProngForDelta3s(δs, idx, k, bezierPiecess, extreme) {
21
- const TOLERANCE = extreme * 1e-10;
22
- const MAX_ITERATIONS = 10;
23
- const δs_ = [
24
- δs[0],
25
- δs[idx],
26
- δs[δs.length - 1]
27
- ];
28
- const bezierPieces_ = [
29
- bezierPiecess[0],
30
- bezierPiecess[idx],
31
- bezierPiecess[δs.length - 1]
32
- ];
33
- const δ3ss = [
34
- [δs_[0], δs_[1], δs_[2]],
35
- [δs_[1], δs_[2], δs_[0]],
36
- [δs_[2], δs_[0], δs_[1]],
37
- ];
38
- const bezierPiecess_ = [
39
- [bezierPieces_[0], bezierPieces_[1], bezierPieces_[2]],
40
- [bezierPieces_[1], bezierPieces_[2], bezierPieces_[0]],
41
- [bezierPieces_[2], bezierPieces_[0], bezierPieces_[1]],
42
- ];
43
- const δ3s = δ3ss[k];
44
- const bezierPiece3s = bezierPiecess_[k];
45
- if (δ3s[0][0].isSharp()) {
46
- return undefined;
47
- }
48
- let ps;
49
- let circumCenter_;
50
- let j = 0; // Safeguard for slow convergence
51
- let x = calcInitial3ProngCenter(δ3s, bezierPiece3s);
52
- if (typeof _debug_ !== 'undefined') {
53
- const threeProngs = _debug_.generated.elems.threeProng;
54
- const d = threeProngs[threeProngs.length - 1];
55
- const trace = d.traces[d.traces.length - 1];
56
- trace.push(x);
57
- }
58
- let tolerance = Number.POSITIVE_INFINITY;
59
- while (tolerance > TOLERANCE && j < MAX_ITERATIONS) {
60
- j++;
61
- ps = getClosestPoints(x, bezierPiece3s);
62
- if (!Number.isFinite(x[0]) || !Number.isFinite(x[1])) {
63
- // TODO - the code can be cleaned up and sped up a lot if we don't
64
- // use this function as is but instead use δs[0] and δs[2] as is
65
- // and make δs[1] include all the rest of the beziers around the
66
- // loop. This check, for instance, would be eliminated completely.
67
- return undefined;
68
- }
69
- circumCenter_ = circumCenter(ps.map(x => x.p));
70
- const vectorToZeroV = calcVectorToZeroV_StraightToIt(x, circumCenter_);
71
- if (!Number.isFinite(vectorToZeroV[0]) || !Number.isFinite(vectorToZeroV[1])) {
72
- // TODO - the code can be cleaned up and sped up a lot if we don't
73
- // use this function as is but instead use δs[0] and δs[2] as is
74
- // and make δs[1] include all the rest of the beziers around the
75
- // loop. This check, for instance, would be eliminated completely.
76
- return undefined;
77
- }
78
- const upds = calcBetterX(bezierPiece3s, x, vectorToZeroV);
79
- x = upds.newX;
80
- ps = upds.newPs;
81
- if (typeof _debug_ !== 'undefined') {
82
- const threeProngs = _debug_.generated.elems.threeProng;
83
- const d = threeProngs[threeProngs.length - 1];
84
- const trace = d.traces[d.traces.length - 1];
85
- trace.push(x);
86
- }
87
- const V = len(vectorToZeroV); // The 'potential'
88
- tolerance = Math.abs(V - upds.newV);
89
- }
90
- const radius = (distanceBetween(x, ps[0].p) +
91
- distanceBetween(x, ps[1].p) +
92
- distanceBetween(x, ps[2].p)) / 3;
93
- const circle = { center: x, radius };
94
- //-------------------------------------------------------------------------
95
- // Calculate the unit tangent vector at 3-prong circle points - they should
96
- // be very close to tangent to the boundary piece tangents at those points
97
- // (up to sign). Sharp corners are a common special case.
98
- //-------------------------------------------------------------------------
99
- let totalAngleError = 0;
100
- for (let i = 0; i < 3; i++) {
101
- const p = ps[i];
102
- //----------------------------
103
- // Tangent of circle at point
104
- //----------------------------
105
- const v = toUnitVector(fromTo(p.p, x));
106
- const v1 = rotate90Degrees(v);
107
- //-----------------------------------
108
- // Check if point is on dull crorner
109
- //-----------------------------------
110
- //if (PointOnShape.isDullCorner(p)) {
111
- if (isPosDullCorner(p)) {
112
- //const corner = Curve.getCornerAtEnd(p.curve);
113
- const corner = getCornerAtEnd(p.curve);
114
- const tans = corner.tangents;
115
- const perps = tans.map(rotate90Degrees);
116
- const angleError1 = Math.asin(cross(perps[0], v));
117
- const angleError2 = Math.asin(cross(v, perps[1]));
118
- let angleError = 0;
119
- if (angleError1 > 0) {
120
- angleError += angleError1;
121
- }
122
- if (angleError2 > 0) {
123
- angleError += angleError2;
124
- }
125
- totalAngleError += angleError;
126
- }
127
- else {
128
- //---------------------------
129
- // Tangent of curve at point
130
- //---------------------------
131
- const v2 = toUnitVector(tangent(p.curve.ps, p.t));
132
- // Cross is more numerically stable than Vector.dot at angles a
133
- // multiple of Math.PI **and** is close to the actual angle value
134
- // and can thus just be added to cone method of looking at
135
- // tolerance.
136
- // Should be close to zero and is close to the actual angle.
137
- const cross_ = Math.abs(Math.asin(cross(v1, v2)));
138
- totalAngleError += cross_;
139
- }
140
- }
141
- //-------------------------------------------------------------------------
142
- // Calculate radiusDelta, the difference between the radius and the closest
143
- // point to the 3-prong. It should be around 0. If not, this is not a good
144
- // candidate for the 3-prong.
145
- //-------------------------------------------------------------------------
146
- const closestDs = [];
147
- for (let i = 0; i < bezierPiecess.length; i++) {
148
- const p = getClosestBoundaryPoint(bezierPiecess[i], x, undefined, undefined);
149
- closestDs.push(distanceBetween(p.pos.p, x));
150
- }
151
- const closestD = Math.min(...closestDs);
152
- const radiusDelta = Math.abs(radius - closestD);
153
- // Weights below still need to be optimized.
154
- const W1 = 1;
155
- const W2 = 1;
156
- const error = W1 * radiusDelta + W2 * totalAngleError;
157
- return { ps, circle, error, δ3s };
158
- }
159
- export { find3ProngForDelta3s };
1
+ import { fromTo, circumCenter, len, distanceBetween, toUnitVector, rotate90Degrees, cross } from 'flo-vector2d';
2
+ import { tangent } from 'flo-bezier3';
3
+ import { isPosDullCorner } from '../../../point-on-shape.js';
4
+ import { getClosestBoundaryPoint } from '../../closest-boundary-point/get-closest-boundary-point.js';
5
+ import { calcInitial3ProngCenter } from './calc-initial-3-prong-center.js';
6
+ import { getClosestPoints } from './get-closest-points.js';
7
+ import { calcBetterX } from './calc-better-x.js';
8
+ import { getCornerAtEnd } from '../../../curve.js';
9
+ /** @hidden */
10
+ const calcVectorToZeroV_StraightToIt = fromTo;
11
+ /**
12
+ * @hidden
13
+ * Finds a 3-prong using only the 3 given δs.
14
+ * @param δs The boundary pieces
15
+ * @param idx δ identifier
16
+ * @param bezierPiecess
17
+ * @param extreme The maximum coordinate value used to calculate floating point
18
+ * tolerances.
19
+ */
20
+ function find3ProngForDelta3s(δs, idx, k, bezierPiecess, extreme) {
21
+ const TOLERANCE = extreme * 1e-10;
22
+ const MAX_ITERATIONS = 10;
23
+ const δs_ = [
24
+ δs[0],
25
+ δs[idx],
26
+ δs[δs.length - 1]
27
+ ];
28
+ const bezierPieces_ = [
29
+ bezierPiecess[0],
30
+ bezierPiecess[idx],
31
+ bezierPiecess[δs.length - 1]
32
+ ];
33
+ const δ3ss = [
34
+ [δs_[0], δs_[1], δs_[2]],
35
+ [δs_[1], δs_[2], δs_[0]],
36
+ [δs_[2], δs_[0], δs_[1]],
37
+ ];
38
+ const bezierPiecess_ = [
39
+ [bezierPieces_[0], bezierPieces_[1], bezierPieces_[2]],
40
+ [bezierPieces_[1], bezierPieces_[2], bezierPieces_[0]],
41
+ [bezierPieces_[2], bezierPieces_[0], bezierPieces_[1]],
42
+ ];
43
+ const δ3s = δ3ss[k];
44
+ const bezierPiece3s = bezierPiecess_[k];
45
+ if (δ3s[0][0].isSharp()) {
46
+ return undefined;
47
+ }
48
+ let ps;
49
+ let circumCenter_;
50
+ let j = 0; // Safeguard for slow convergence
51
+ let x = calcInitial3ProngCenter(δ3s, bezierPiece3s);
52
+ if (typeof _debug_ !== 'undefined') {
53
+ const threeProngs = _debug_.generated.elems.threeProng;
54
+ const d = threeProngs[threeProngs.length - 1];
55
+ const trace = d.traces[d.traces.length - 1];
56
+ trace.push(x);
57
+ }
58
+ let tolerance = Number.POSITIVE_INFINITY;
59
+ while (tolerance > TOLERANCE && j < MAX_ITERATIONS) {
60
+ j++;
61
+ ps = getClosestPoints(x, bezierPiece3s);
62
+ if (!Number.isFinite(x[0]) || !Number.isFinite(x[1])) {
63
+ // TODO - the code can be cleaned up and sped up a lot if we don't
64
+ // use this function as is but instead use δs[0] and δs[2] as is
65
+ // and make δs[1] include all the rest of the beziers around the
66
+ // loop. This check, for instance, would be eliminated completely.
67
+ return undefined;
68
+ }
69
+ circumCenter_ = circumCenter(ps.map(x => x.p));
70
+ const vectorToZeroV = calcVectorToZeroV_StraightToIt(x, circumCenter_);
71
+ if (!Number.isFinite(vectorToZeroV[0]) || !Number.isFinite(vectorToZeroV[1])) {
72
+ // TODO - the code can be cleaned up and sped up a lot if we don't
73
+ // use this function as is but instead use δs[0] and δs[2] as is
74
+ // and make δs[1] include all the rest of the beziers around the
75
+ // loop. This check, for instance, would be eliminated completely.
76
+ return undefined;
77
+ }
78
+ const upds = calcBetterX(bezierPiece3s, x, vectorToZeroV);
79
+ x = upds.newX;
80
+ ps = upds.newPs;
81
+ if (typeof _debug_ !== 'undefined') {
82
+ const threeProngs = _debug_.generated.elems.threeProng;
83
+ const d = threeProngs[threeProngs.length - 1];
84
+ const trace = d.traces[d.traces.length - 1];
85
+ trace.push(x);
86
+ }
87
+ const V = len(vectorToZeroV); // The 'potential'
88
+ tolerance = Math.abs(V - upds.newV);
89
+ }
90
+ const radius = (distanceBetween(x, ps[0].p) +
91
+ distanceBetween(x, ps[1].p) +
92
+ distanceBetween(x, ps[2].p)) / 3;
93
+ const circle = { center: x, radius };
94
+ //-------------------------------------------------------------------------
95
+ // Calculate the unit tangent vector at 3-prong circle points - they should
96
+ // be very close to tangent to the boundary piece tangents at those points
97
+ // (up to sign). Sharp corners are a common special case.
98
+ //-------------------------------------------------------------------------
99
+ let totalAngleError = 0;
100
+ for (let i = 0; i < 3; i++) {
101
+ const p = ps[i];
102
+ //----------------------------
103
+ // Tangent of circle at point
104
+ //----------------------------
105
+ const v = toUnitVector(fromTo(p.p, x));
106
+ const v1 = rotate90Degrees(v);
107
+ //-----------------------------------
108
+ // Check if point is on dull crorner
109
+ //-----------------------------------
110
+ //if (PointOnShape.isDullCorner(p)) {
111
+ if (isPosDullCorner(p)) {
112
+ //const corner = Curve.getCornerAtEnd(p.curve);
113
+ const corner = getCornerAtEnd(p.curve);
114
+ const tans = corner.tangents;
115
+ const perps = tans.map(rotate90Degrees);
116
+ const angleError1 = Math.asin(cross(perps[0], v));
117
+ const angleError2 = Math.asin(cross(v, perps[1]));
118
+ let angleError = 0;
119
+ if (angleError1 > 0) {
120
+ angleError += angleError1;
121
+ }
122
+ if (angleError2 > 0) {
123
+ angleError += angleError2;
124
+ }
125
+ totalAngleError += angleError;
126
+ }
127
+ else {
128
+ //---------------------------
129
+ // Tangent of curve at point
130
+ //---------------------------
131
+ const v2 = toUnitVector(tangent(p.curve.ps, p.t));
132
+ // Cross is more numerically stable than Vector.dot at angles a
133
+ // multiple of Math.PI **and** is close to the actual angle value
134
+ // and can thus just be added to cone method of looking at
135
+ // tolerance.
136
+ // Should be close to zero and is close to the actual angle.
137
+ const cross_ = Math.abs(Math.asin(cross(v1, v2)));
138
+ totalAngleError += cross_;
139
+ }
140
+ }
141
+ //-------------------------------------------------------------------------
142
+ // Calculate radiusDelta, the difference between the radius and the closest
143
+ // point to the 3-prong. It should be around 0. If not, this is not a good
144
+ // candidate for the 3-prong.
145
+ //-------------------------------------------------------------------------
146
+ const closestDs = [];
147
+ for (let i = 0; i < bezierPiecess.length; i++) {
148
+ const p = getClosestBoundaryPoint(bezierPiecess[i], x, undefined, undefined);
149
+ closestDs.push(distanceBetween(p.pos.p, x));
150
+ }
151
+ const closestD = Math.min(...closestDs);
152
+ const radiusDelta = Math.abs(radius - closestD);
153
+ // Weights below still need to be optimized.
154
+ const W1 = 1;
155
+ const W2 = 1;
156
+ const error = W1 * radiusDelta + W2 * totalAngleError;
157
+ return { ps, circle, error, δ3s };
158
+ }
159
+ export { find3ProngForDelta3s };
160
160
  //# sourceMappingURL=find-3-prong-for-delta3s.js.map
@@ -1,16 +1,16 @@
1
- import { CpNode } from '../../../cp-node.js';
2
- import { Circle } from '../../../circle.js';
3
- import { IPointOnShape } from '../../../point-on-shape.js';
4
- /**
5
- * @hidden
6
- * Find and return a 3-prong from the given boundary piece.
7
- * @param δs A boundary piece
8
- * @param extreme The maximum coordinate value used to calculate floating point
9
- * tolerances.
10
- */
11
- declare function find3Prong(δs: CpNode[][], extreme: number): {
12
- circle: Circle;
13
- ps: IPointOnShape[];
14
- δ3s: CpNode[][];
15
- };
16
- export { find3Prong };
1
+ import { CpNode } from '../../../cp-node.js';
2
+ import { Circle } from '../../../circle.js';
3
+ import { IPointOnShape } from '../../../point-on-shape.js';
4
+ /**
5
+ * @hidden
6
+ * Find and return a 3-prong from the given boundary piece.
7
+ * @param δs A boundary piece
8
+ * @param extreme The maximum coordinate value used to calculate floating point
9
+ * tolerances.
10
+ */
11
+ declare function find3Prong(δs: CpNode[][], extreme: number): {
12
+ circle: Circle;
13
+ ps: IPointOnShape[];
14
+ δ3s: CpNode[][];
15
+ };
16
+ export { find3Prong };
@@ -1,77 +1,77 @@
1
- import { fromTo } from 'flo-bezier3';
2
- import { createEmptyThreeProngForDebugging } from '../../../debug/three-prong-for-debugging.js';
3
- import { find3ProngForDelta3s } from './find-3-prong-for-delta3s.js';
4
- import { getBoundaryPieceBeziers } from '../../get-boundary-piece-beziers.js';
5
- /**
6
- * @hidden
7
- * Find and return a 3-prong from the given boundary piece.
8
- * @param δs A boundary piece
9
- * @param extreme The maximum coordinate value used to calculate floating point
10
- * tolerances.
11
- */
12
- function find3Prong(δs, extreme) {
13
- const bezierPiecess = δs.map(getBoundaryPieceBeziers);
14
- if (typeof _debug_ !== 'undefined') {
15
- const threeProngs = _debug_.generated.elems.threeProng;
16
- threeProngs.push(createEmptyThreeProngForDebugging());
17
- const d = threeProngs[threeProngs.length - 1];
18
- d.boundaries = [];
19
- for (const bezierPieces of bezierPiecess) {
20
- const boundary = [];
21
- d.boundaries.push(boundary);
22
- for (const bezierPiece of bezierPieces) {
23
- /* qqq
24
- let bezier = fromTo(bezierPiece.curve.ps)(
25
- bezierPiece.ts[0], bezierPiece.ts[1]
26
- );
27
- */
28
- const bezier = fromTo(bezierPiece.curve.ps, bezierPiece.ts[0], bezierPiece.ts[1]);
29
- boundary.push(bezier);
30
- }
31
- }
32
- d.traces = [];
33
- }
34
- const candidateThreeProngs = [];
35
- // The best candidate amongst the different 'permutations' of the given δs.
36
- let threeProng;
37
- let bestIndx = undefined;
38
- let smallestError = Number.POSITIVE_INFINITY;
39
- for (let i = 1; i < δs.length - 1; i++) {
40
- for (let k = 0; k < 3; k++) {
41
- if (typeof _debug_ !== 'undefined') {
42
- const threeProngs = _debug_.generated.elems.threeProng;
43
- const d = threeProngs[threeProngs.length - 1];
44
- const trace = [];
45
- d.traces.push(trace);
46
- }
47
- const threeProngInfo = find3ProngForDelta3s(δs, i, k, bezierPiecess, extreme);
48
- if (!threeProngInfo) {
49
- continue;
50
- }
51
- const { circle, ps, error, δ3s } = threeProngInfo;
52
- if (typeof _debug_ !== 'undefined') {
53
- candidateThreeProngs.push({ circle, ps });
54
- }
55
- if (error < smallestError) {
56
- smallestError = error;
57
- bestIndx = i - 1;
58
- threeProng = { circle, ps, δ3s };
59
- }
60
- }
61
- }
62
- //threeProng.δ3s = [δs[0], δs[bestIndx+1], δs[δs.length-1]];
63
- if (typeof _debug_ !== 'undefined') {
64
- const threeProngs = _debug_.generated.elems.threeProng;
65
- const d = threeProngs[threeProngs.length - 1];
66
- d.generated = _debug_.generated;
67
- d.circle = threeProng.circle;
68
- d.poss = threeProng.ps;
69
- d.cp3ss = threeProng.δ3s;
70
- d.cpss = δs;
71
- d.bestIndx = bestIndx;
72
- d.candidateThreeProngs = candidateThreeProngs;
73
- }
74
- return threeProng;
75
- }
76
- export { find3Prong };
1
+ import { fromTo } from 'flo-bezier3';
2
+ import { createEmptyThreeProngForDebugging } from '../../../debug/three-prong-for-debugging.js';
3
+ import { find3ProngForDelta3s } from './find-3-prong-for-delta3s.js';
4
+ import { getBoundaryPieceBeziers } from '../../get-boundary-piece-beziers.js';
5
+ /**
6
+ * @hidden
7
+ * Find and return a 3-prong from the given boundary piece.
8
+ * @param δs A boundary piece
9
+ * @param extreme The maximum coordinate value used to calculate floating point
10
+ * tolerances.
11
+ */
12
+ function find3Prong(δs, extreme) {
13
+ const bezierPiecess = δs.map(getBoundaryPieceBeziers);
14
+ if (typeof _debug_ !== 'undefined') {
15
+ const threeProngs = _debug_.generated.elems.threeProng;
16
+ threeProngs.push(createEmptyThreeProngForDebugging());
17
+ const d = threeProngs[threeProngs.length - 1];
18
+ d.boundaries = [];
19
+ for (const bezierPieces of bezierPiecess) {
20
+ const boundary = [];
21
+ d.boundaries.push(boundary);
22
+ for (const bezierPiece of bezierPieces) {
23
+ /* qqq
24
+ let bezier = fromTo(bezierPiece.curve.ps)(
25
+ bezierPiece.ts[0], bezierPiece.ts[1]
26
+ );
27
+ */
28
+ const bezier = fromTo(bezierPiece.curve.ps, bezierPiece.ts[0], bezierPiece.ts[1]);
29
+ boundary.push(bezier);
30
+ }
31
+ }
32
+ d.traces = [];
33
+ }
34
+ const candidateThreeProngs = [];
35
+ // The best candidate amongst the different 'permutations' of the given δs.
36
+ let threeProng;
37
+ let bestIndx = undefined;
38
+ let smallestError = Number.POSITIVE_INFINITY;
39
+ for (let i = 1; i < δs.length - 1; i++) {
40
+ for (let k = 0; k < 3; k++) {
41
+ if (typeof _debug_ !== 'undefined') {
42
+ const threeProngs = _debug_.generated.elems.threeProng;
43
+ const d = threeProngs[threeProngs.length - 1];
44
+ const trace = [];
45
+ d.traces.push(trace);
46
+ }
47
+ const threeProngInfo = find3ProngForDelta3s(δs, i, k, bezierPiecess, extreme);
48
+ if (!threeProngInfo) {
49
+ continue;
50
+ }
51
+ const { circle, ps, error, δ3s } = threeProngInfo;
52
+ if (typeof _debug_ !== 'undefined') {
53
+ candidateThreeProngs.push({ circle, ps });
54
+ }
55
+ if (error < smallestError) {
56
+ smallestError = error;
57
+ bestIndx = i - 1;
58
+ threeProng = { circle, ps, δ3s };
59
+ }
60
+ }
61
+ }
62
+ //threeProng.δ3s = [δs[0], δs[bestIndx+1], δs[δs.length-1]];
63
+ if (typeof _debug_ !== 'undefined') {
64
+ const threeProngs = _debug_.generated.elems.threeProng;
65
+ const d = threeProngs[threeProngs.length - 1];
66
+ d.generated = _debug_.generated;
67
+ d.circle = threeProng.circle;
68
+ d.poss = threeProng.ps;
69
+ d.cp3ss = threeProng.δ3s;
70
+ d.cpss = δs;
71
+ d.bestIndx = bestIndx;
72
+ d.candidateThreeProngs = candidateThreeProngs;
73
+ }
74
+ return threeProng;
75
+ }
76
+ export { find3Prong };
77
77
  //# sourceMappingURL=find-3-prong.js.map
@@ -1,9 +1,9 @@
1
- import { BezierPiece } from '../../bezier-piece.js';
2
- /**
3
- * @hidden
4
- * @param x
5
- * @param bezierPiece3s
6
- * @param extreme
7
- */
8
- declare function getClosestPoints(x: number[], bezierPiece3s: BezierPiece[][]): import("../../../point-on-shape.js").IPointOnShape[];
9
- export { getClosestPoints };
1
+ import { BezierPiece } from '../../bezier-piece.js';
2
+ /**
3
+ * @hidden
4
+ * @param x
5
+ * @param bezierPiece3s
6
+ * @param extreme
7
+ */
8
+ declare function getClosestPoints(x: number[], bezierPiece3s: BezierPiece[][]): import("../../../point-on-shape.js").IPointOnShape[];
9
+ export { getClosestPoints };
@@ -1,17 +1,17 @@
1
- import { getClosestBoundaryPoint } from '../../closest-boundary-point/get-closest-boundary-point.js';
2
- /**
3
- * @hidden
4
- * @param x
5
- * @param bezierPiece3s
6
- * @param extreme
7
- */
8
- function getClosestPoints(x, bezierPiece3s) {
9
- return bezierPiece3s.map(bezierPieces => {
10
- const posInfo = getClosestBoundaryPoint(bezierPieces, x, undefined, // curve
11
- undefined // t
12
- );
13
- return posInfo ? posInfo.pos : undefined;
14
- });
15
- }
16
- export { getClosestPoints };
1
+ import { getClosestBoundaryPoint } from '../../closest-boundary-point/get-closest-boundary-point.js';
2
+ /**
3
+ * @hidden
4
+ * @param x
5
+ * @param bezierPiece3s
6
+ * @param extreme
7
+ */
8
+ function getClosestPoints(x, bezierPiece3s) {
9
+ return bezierPiece3s.map(bezierPieces => {
10
+ const posInfo = getClosestBoundaryPoint(bezierPieces, x, undefined, // curve
11
+ undefined // t
12
+ );
13
+ return posInfo ? posInfo.pos : undefined;
14
+ });
15
+ }
16
+ export { getClosestPoints };
17
17
  //# sourceMappingURL=get-closest-points.js.map
@@ -1,15 +1,15 @@
1
- import { LlRbTree } from 'flo-ll-rb-tree';
2
- import { Loop } from '../../loop.js';
3
- import { CpNode } from '../../cp-node.js';
4
- import { IPointOnShape } from '../../point-on-shape.js';
5
- /**
6
- * @hidden
7
- * Add 2 prongs. See comments on the add2Prong function.
8
- * @param loops
9
- * @param cpGraphs
10
- * @param for2Prongss
11
- * @param extreme The maximum coordinate value used to calculate floating point
12
- * tolerances.
13
- */
14
- declare function findAndAdd2ProngsOnAllPaths(loops: Loop[], cpGraphs: Map<Loop, LlRbTree<CpNode>>, for2Prongss: IPointOnShape[][], extreme: number): CpNode;
15
- export { findAndAdd2ProngsOnAllPaths };
1
+ import { LlRbTree } from 'flo-ll-rb-tree';
2
+ import { Loop } from '../../loop.js';
3
+ import { CpNode } from '../../cp-node.js';
4
+ import { IPointOnShape } from '../../point-on-shape.js';
5
+ /**
6
+ * @hidden
7
+ * Add 2 prongs. See comments on the add2Prong function.
8
+ * @param loops
9
+ * @param cpGraphs
10
+ * @param for2Prongss
11
+ * @param extreme The maximum coordinate value used to calculate floating point
12
+ * tolerances.
13
+ */
14
+ declare function findAndAdd2ProngsOnAllPaths(loops: Loop[], cpGraphs: Map<Loop, LlRbTree<CpNode>>, for2Prongss: IPointOnShape[][], extreme: number): CpNode;
15
+ export { findAndAdd2ProngsOnAllPaths };
@@ -1,21 +1,21 @@
1
- import { findAndAdd2Prongs } from './find-and-add-2-prongs.js';
2
- /**
3
- * @hidden
4
- * Add 2 prongs. See comments on the add2Prong function.
5
- * @param loops
6
- * @param cpGraphs
7
- * @param for2Prongss
8
- * @param extreme The maximum coordinate value used to calculate floating point
9
- * tolerances.
10
- */
11
- function findAndAdd2ProngsOnAllPaths(loops, cpGraphs, for2Prongss, extreme) {
12
- let cpNode;
13
- for (let k = 0; k < for2Prongss.length; k++) {
14
- const for2Prongs = for2Prongss[k];
15
- const _cpNode = findAndAdd2Prongs(loops, cpGraphs, k, for2Prongs, extreme);
16
- cpNode = cpNode || _cpNode;
17
- }
18
- return cpNode;
19
- }
20
- export { findAndAdd2ProngsOnAllPaths };
1
+ import { findAndAdd2Prongs } from './find-and-add-2-prongs.js';
2
+ /**
3
+ * @hidden
4
+ * Add 2 prongs. See comments on the add2Prong function.
5
+ * @param loops
6
+ * @param cpGraphs
7
+ * @param for2Prongss
8
+ * @param extreme The maximum coordinate value used to calculate floating point
9
+ * tolerances.
10
+ */
11
+ function findAndAdd2ProngsOnAllPaths(loops, cpGraphs, for2Prongss, extreme) {
12
+ let cpNode;
13
+ for (let k = 0; k < for2Prongss.length; k++) {
14
+ const for2Prongs = for2Prongss[k];
15
+ const _cpNode = findAndAdd2Prongs(loops, cpGraphs, k, for2Prongs, extreme);
16
+ cpNode = cpNode || _cpNode;
17
+ }
18
+ return cpNode;
19
+ }
20
+ export { findAndAdd2ProngsOnAllPaths };
21
21
  //# sourceMappingURL=find-and-add-2-prongs-on-all-paths.js.map