@bitbybit-dev/base 1.6.2 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/NOTICE +24 -0
  2. package/README.md +3 -1
  3. package/lib/api/GlobalCDNProvider.js +1 -1
  4. package/lib/api/inputs/base-inputs.d.ts +40 -7
  5. package/lib/api/inputs/base-inputs.js +32 -0
  6. package/lib/api/inputs/io-inputs.d.ts +927 -59
  7. package/lib/api/inputs/io-inputs.js +769 -40
  8. package/lib/api/inputs/line-inputs.d.ts +17 -0
  9. package/lib/api/inputs/line-inputs.js +15 -0
  10. package/lib/api/inputs/mesh-inputs.d.ts +20 -1
  11. package/lib/api/inputs/mesh-inputs.js +23 -0
  12. package/lib/api/inputs/recipes-inputs.d.ts +13 -5
  13. package/lib/api/inputs/recipes-inputs.js +10 -5
  14. package/lib/api/kernel-calls/index.d.ts +2 -0
  15. package/lib/api/kernel-calls/index.js +2 -0
  16. package/lib/api/kernel-calls/resolve-dto.d.ts +3 -1
  17. package/lib/api/kernel-calls/resolve-dto.js +7 -2
  18. package/lib/api/kernel-calls/settings.d.ts +101 -0
  19. package/lib/api/kernel-calls/settings.js +123 -0
  20. package/lib/api/kernel-calls/up-axis-aliases.d.ts +25 -0
  21. package/lib/api/kernel-calls/up-axis-aliases.js +70 -0
  22. package/lib/api/models/simplex.d.ts +2 -0
  23. package/lib/api/models/simplex.js +10 -1
  24. package/lib/api/recipes/surface-mesh.d.ts +2 -1
  25. package/lib/api/recipes/surface-mesh.js +5 -3
  26. package/lib/api/resolved-inputs/index.d.ts +32 -3
  27. package/lib/api/services/base-bitbybit.d.ts +12 -0
  28. package/lib/api/services/base-bitbybit.js +19 -3
  29. package/lib/api/services/color.js +2 -3
  30. package/lib/api/services/helpers/color/color-forms.constants.d.ts +3 -0
  31. package/lib/api/services/helpers/color/color-forms.constants.js +3 -0
  32. package/lib/api/services/helpers/color/color-forms.d.ts +1 -0
  33. package/lib/api/services/helpers/color/color-forms.js +4 -1
  34. package/lib/api/services/helpers/color/css-colors.constants.d.ts +9 -0
  35. package/lib/api/services/helpers/color/css-colors.constants.js +158 -0
  36. package/lib/api/services/helpers/color/css-colors.d.ts +1 -0
  37. package/lib/api/services/helpers/color/css-colors.js +61 -0
  38. package/lib/api/services/helpers/curves/bezier-pieces.constants.d.ts +5 -0
  39. package/lib/api/services/helpers/curves/bezier-pieces.constants.js +5 -0
  40. package/lib/api/services/helpers/curves/bezier-pieces.d.ts +24 -0
  41. package/lib/api/services/helpers/curves/bezier-pieces.js +149 -0
  42. package/lib/api/services/helpers/curves/conic-arcs.constants.d.ts +5 -0
  43. package/lib/api/services/helpers/curves/conic-arcs.constants.js +5 -0
  44. package/lib/api/services/helpers/curves/conic-arcs.d.ts +26 -0
  45. package/lib/api/services/helpers/curves/conic-arcs.js +87 -0
  46. package/lib/api/services/helpers/curves/nurbs-forms.d.ts +20 -0
  47. package/lib/api/services/helpers/curves/nurbs-forms.js +112 -0
  48. package/lib/api/services/helpers/curves/nurbs.d.ts +4 -0
  49. package/lib/api/services/helpers/curves/nurbs.js +10 -9
  50. package/lib/api/services/helpers/files/drawing/drawing-budget.d.ts +3 -0
  51. package/lib/api/services/helpers/files/drawing/drawing-budget.js +34 -0
  52. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.d.ts +14 -0
  53. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.js +14 -0
  54. package/lib/api/services/helpers/files/drawing/drawing-dimension.d.ts +4 -0
  55. package/lib/api/services/helpers/files/drawing/drawing-dimension.js +271 -0
  56. package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +21 -0
  57. package/lib/api/services/helpers/files/drawing/drawing-geometry.js +101 -0
  58. package/lib/api/services/helpers/files/drawing/drawing-hatch.d.ts +10 -0
  59. package/lib/api/services/helpers/files/drawing/drawing-hatch.js +123 -0
  60. package/lib/api/services/helpers/files/drawing/drawing-loops.d.ts +3 -0
  61. package/lib/api/services/helpers/files/drawing/drawing-loops.js +91 -0
  62. package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +10 -0
  63. package/lib/api/services/helpers/files/drawing/drawing-model.js +336 -0
  64. package/lib/api/services/helpers/files/drawing/drawing-scale.d.ts +2 -0
  65. package/lib/api/services/helpers/files/drawing/drawing-scale.js +47 -0
  66. package/lib/api/services/helpers/files/drawing/drawing-text.d.ts +9 -0
  67. package/lib/api/services/helpers/files/drawing/drawing-text.js +25 -0
  68. package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +117 -0
  69. package/lib/api/services/helpers/files/drawing/drawing-types.js +1 -0
  70. package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +41 -0
  71. package/lib/api/services/helpers/files/drawing/drawing.constants.js +46 -0
  72. package/lib/api/services/helpers/files/dxf/dxf-annotations.d.ts +5 -0
  73. package/lib/api/services/helpers/files/dxf/dxf-annotations.js +96 -0
  74. package/lib/api/services/helpers/files/dxf/dxf-colors.d.ts +5 -0
  75. package/lib/api/services/helpers/files/dxf/dxf-colors.js +45 -0
  76. package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +21 -0
  77. package/lib/api/services/helpers/files/dxf/dxf-entities.js +196 -0
  78. package/lib/api/services/helpers/files/dxf/dxf-file-text.d.ts +1 -0
  79. package/lib/api/services/helpers/files/dxf/dxf-file-text.js +50 -0
  80. package/lib/api/services/helpers/files/dxf/dxf-generator.d.ts +2 -36
  81. package/lib/api/services/helpers/files/dxf/dxf-generator.js +97 -523
  82. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.d.ts +9 -0
  83. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.js +312 -0
  84. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.d.ts +6 -0
  85. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +115 -0
  86. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.d.ts +21 -0
  87. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.js +67 -0
  88. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.d.ts +9 -0
  89. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.js +151 -0
  90. package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +96 -0
  91. package/lib/api/services/helpers/files/dxf/dxf-reader-types.js +1 -0
  92. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +134 -0
  93. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +95 -0
  94. package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +2 -0
  95. package/lib/api/services/helpers/files/dxf/dxf-reader.js +446 -0
  96. package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +29 -0
  97. package/lib/api/services/helpers/files/dxf/dxf-tables.js +248 -0
  98. package/lib/api/services/helpers/files/dxf/dxf-text.d.ts +26 -0
  99. package/lib/api/services/helpers/files/dxf/dxf-text.js +97 -0
  100. package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +11 -0
  101. package/lib/api/services/helpers/files/dxf/dxf-tokens.js +89 -0
  102. package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +43 -1
  103. package/lib/api/services/helpers/files/dxf/dxf.constants.js +47 -1
  104. package/lib/api/services/helpers/files/file-bytes.d.ts +1 -0
  105. package/lib/api/services/helpers/files/file-bytes.js +15 -0
  106. package/lib/api/services/helpers/files/ground-axes.d.ts +2 -0
  107. package/lib/api/services/helpers/files/ground-axes.js +4 -0
  108. package/lib/api/services/helpers/files/mesh-file-axes.d.ts +7 -0
  109. package/lib/api/services/helpers/files/mesh-file-axes.js +17 -0
  110. package/lib/api/services/helpers/files/stl-axes.d.ts +4 -0
  111. package/lib/api/services/helpers/files/stl-axes.js +22 -0
  112. package/lib/api/services/helpers/files/stl-types.d.ts +7 -0
  113. package/lib/api/services/helpers/files/stl-types.js +1 -0
  114. package/lib/api/services/helpers/files/stl.constants.d.ts +11 -0
  115. package/lib/api/services/helpers/files/stl.constants.js +11 -0
  116. package/lib/api/services/helpers/files/stl.d.ts +3 -0
  117. package/lib/api/services/helpers/files/stl.js +93 -0
  118. package/lib/api/services/helpers/files/svg/svg-css.d.ts +48 -0
  119. package/lib/api/services/helpers/files/svg/svg-css.js +444 -0
  120. package/lib/api/services/helpers/files/svg/svg-dash-patterns.d.ts +2 -0
  121. package/lib/api/services/helpers/files/svg/svg-dash-patterns.js +35 -0
  122. package/lib/api/services/helpers/files/svg/svg-document.d.ts +26 -0
  123. package/lib/api/services/helpers/files/svg/svg-document.js +89 -0
  124. package/lib/api/services/helpers/files/svg/svg-memo.d.ts +1 -0
  125. package/lib/api/services/helpers/files/svg/svg-memo.js +8 -0
  126. package/lib/api/services/helpers/files/svg/svg-normalizer.d.ts +6 -0
  127. package/lib/api/services/helpers/files/svg/svg-normalizer.js +433 -0
  128. package/lib/api/services/helpers/files/svg/svg-numbers.d.ts +15 -0
  129. package/lib/api/services/helpers/files/svg/svg-numbers.js +61 -0
  130. package/lib/api/services/helpers/files/svg/svg-path-data.constants.d.ts +8 -0
  131. package/lib/api/services/helpers/files/svg/svg-path-data.constants.js +10 -0
  132. package/lib/api/services/helpers/files/svg/svg-path-data.d.ts +9 -0
  133. package/lib/api/services/helpers/files/svg/svg-path-data.js +203 -0
  134. package/lib/api/services/helpers/files/svg/svg-path-types.d.ts +33 -0
  135. package/lib/api/services/helpers/files/svg/svg-path-types.js +1 -0
  136. package/lib/api/services/helpers/files/svg/svg-reader.constants.d.ts +35 -0
  137. package/lib/api/services/helpers/files/svg/svg-reader.constants.js +35 -0
  138. package/lib/api/services/helpers/files/svg/svg-reading.constants.d.ts +9 -0
  139. package/lib/api/services/helpers/files/svg/svg-reading.constants.js +9 -0
  140. package/lib/api/services/helpers/files/svg/svg-reading.d.ts +6 -0
  141. package/lib/api/services/helpers/files/svg/svg-reading.js +233 -0
  142. package/lib/api/services/helpers/files/svg/svg-scene-types.d.ts +54 -0
  143. package/lib/api/services/helpers/files/svg/svg-scene-types.js +1 -0
  144. package/lib/api/services/helpers/files/svg/svg-shapes.d.ts +6 -0
  145. package/lib/api/services/helpers/files/svg/svg-shapes.js +102 -0
  146. package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +38 -0
  147. package/lib/api/services/helpers/files/svg/svg-strokes.js +132 -0
  148. package/lib/api/services/helpers/files/svg/svg-style.d.ts +26 -0
  149. package/lib/api/services/helpers/files/svg/svg-style.js +211 -0
  150. package/lib/api/services/helpers/files/svg/svg-transform.d.ts +10 -0
  151. package/lib/api/services/helpers/files/svg/svg-transform.js +76 -0
  152. package/lib/api/services/helpers/files/svg/svg-writer.d.ts +18 -0
  153. package/lib/api/services/helpers/files/svg/svg-writer.js +230 -0
  154. package/lib/api/services/helpers/files/svg/svg-xml.d.ts +11 -0
  155. package/lib/api/services/helpers/files/svg/svg-xml.js +322 -0
  156. package/lib/api/services/helpers/files/svg/svg.constants.d.ts +14 -0
  157. package/lib/api/services/helpers/files/svg/svg.constants.js +14 -0
  158. package/lib/api/services/helpers/files/work-budget.d.ts +11 -0
  159. package/lib/api/services/helpers/files/work-budget.js +26 -0
  160. package/lib/api/services/helpers/geometry/affine2.d.ts +8 -0
  161. package/lib/api/services/helpers/geometry/affine2.js +18 -0
  162. package/lib/api/services/helpers/geometry/ellipse-arcs.d.ts +14 -0
  163. package/lib/api/services/helpers/geometry/ellipse-arcs.js +96 -0
  164. package/lib/api/services/helpers/geometry/frame-axes.d.ts +3 -1
  165. package/lib/api/services/helpers/geometry/loop-fill.d.ts +2 -0
  166. package/lib/api/services/helpers/geometry/loop-fill.js +37 -0
  167. package/lib/api/services/helpers/geometry/polygon-nesting.constants.d.ts +6 -0
  168. package/lib/api/services/helpers/geometry/polygon-nesting.constants.js +6 -0
  169. package/lib/api/services/helpers/geometry/polygon-nesting.d.ts +31 -0
  170. package/lib/api/services/helpers/geometry/polygon-nesting.js +251 -0
  171. package/lib/api/services/helpers/geometry/vectors.d.ts +1 -0
  172. package/lib/api/services/helpers/geometry/vectors.js +6 -0
  173. package/lib/api/services/helpers/helper-types.d.ts +33 -0
  174. package/lib/api/services/helpers/text/stroke-font.d.ts +6 -0
  175. package/lib/api/services/helpers/text/stroke-font.js +177 -0
  176. package/lib/api/services/helpers/text/stroke-letters.constants.d.ts +2 -0
  177. package/lib/api/services/helpers/text/stroke-letters.constants.js +18 -0
  178. package/lib/api/services/helpers/text/stroke-marks.constants.d.ts +14 -0
  179. package/lib/api/services/helpers/text/stroke-marks.constants.js +34 -0
  180. package/lib/api/services/io/drawing.d.ts +368 -0
  181. package/lib/api/services/io/drawing.js +454 -0
  182. package/lib/api/services/io/dxf.d.ts +134 -0
  183. package/lib/api/services/io/dxf.js +167 -0
  184. package/lib/api/services/io/index.d.ts +4 -0
  185. package/lib/api/services/io/index.js +4 -0
  186. package/lib/api/services/io/io.d.ts +14 -0
  187. package/lib/api/services/io/io.js +15 -0
  188. package/lib/api/services/io/svg.d.ts +66 -0
  189. package/lib/api/services/io/svg.js +105 -0
  190. package/lib/api/services/line.d.ts +17 -0
  191. package/lib/api/services/line.js +20 -1
  192. package/lib/api/services/mesh.d.ts +19 -0
  193. package/lib/api/services/mesh.js +40 -0
  194. package/lib/api/services/point.d.ts +4 -1
  195. package/lib/api/services/point.js +9 -4
  196. package/lib/api/services/recipes.d.ts +3 -0
  197. package/lib/api/services/recipes.js +6 -2
  198. package/lib/api/services/text.d.ts +7 -4
  199. package/lib/api/services/text.js +14 -27
  200. package/package.json +1 -1
  201. package/lib/api/services/helpers/files/dxf/dxf.d.ts +0 -87
  202. package/lib/api/services/helpers/files/dxf/dxf.js +0 -111
  203. package/lib/api/services/io.d.ts +0 -9
  204. package/lib/api/services/io.js +0 -10
@@ -0,0 +1,96 @@
1
+ import { angleOfCosine } from "./scalars";
2
+ import { cross2 } from "./vectors";
3
+ const FULL_TURN = 2 * Math.PI;
4
+ const turnBetween = (ux, uy, vx, vy) => {
5
+ const angle = angleOfCosine((ux * vx + uy * vy) / Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy)));
6
+ return ux * vy - uy * vx < 0 ? -angle : angle;
7
+ };
8
+ export const ellipseArcOfEndpoints2 = (from, to, radiusX, radiusY, xAxisRotation, largeArc, sweep) => {
9
+ let rx = Math.abs(radiusX);
10
+ let ry = Math.abs(radiusY);
11
+ if ((from[0] === to[0] && from[1] === to[1]) || rx === 0 || ry === 0) {
12
+ return undefined;
13
+ }
14
+ const cosPhi = Math.cos(xAxisRotation);
15
+ const sinPhi = Math.sin(xAxisRotation);
16
+ const dx = (from[0] - to[0]) / 2;
17
+ const dy = (from[1] - to[1]) / 2;
18
+ const x1p = cosPhi * dx + sinPhi * dy;
19
+ const y1p = -sinPhi * dx + cosPhi * dy;
20
+ const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
21
+ if (lambda > 1) {
22
+ const s = Math.sqrt(lambda);
23
+ rx *= s;
24
+ ry *= s;
25
+ }
26
+ const rx2 = rx * rx;
27
+ const ry2 = ry * ry;
28
+ const num = rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p;
29
+ const den = rx2 * y1p * y1p + ry2 * x1p * x1p;
30
+ const size = den === 0 ? 0 : Math.sqrt(Math.max(0, num / den));
31
+ const factor = largeArc === sweep ? -size : size;
32
+ const cxp = (factor * rx * y1p) / ry;
33
+ const cyp = (-factor * ry * x1p) / rx;
34
+ const startAngle = turnBetween(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
35
+ let deltaAngle = turnBetween((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
36
+ if (!sweep && deltaAngle > 0) {
37
+ deltaAngle -= FULL_TURN;
38
+ }
39
+ if (sweep && deltaAngle < 0) {
40
+ deltaAngle += FULL_TURN;
41
+ }
42
+ return {
43
+ center: [cosPhi * cxp - sinPhi * cyp + (from[0] + to[0]) / 2, sinPhi * cxp + cosPhi * cyp + (from[1] + to[1]) / 2],
44
+ rx,
45
+ ry,
46
+ xAxisRotation,
47
+ startAngle,
48
+ deltaAngle,
49
+ };
50
+ };
51
+ export const affineEllipseArc2 = (matrix, arc) => {
52
+ const [a, b, c, d, tx, ty] = matrix;
53
+ const cosP = Math.cos(arc.xAxisRotation);
54
+ const sinP = Math.sin(arc.xAxisRotation);
55
+ const r00 = cosP * arc.rx;
56
+ const r10 = sinP * arc.rx;
57
+ const r01 = -sinP * arc.ry;
58
+ const r11 = cosP * arc.ry;
59
+ const m00 = a * r00 + c * r10;
60
+ const m10 = b * r00 + d * r10;
61
+ const m01 = a * r01 + c * r11;
62
+ const m11 = b * r01 + d * r11;
63
+ const e = (m00 + m11) / 2;
64
+ const f = (m00 - m11) / 2;
65
+ const g = (m10 + m01) / 2;
66
+ const h = (m10 - m01) / 2;
67
+ const q = Math.hypot(e, h);
68
+ const r = Math.hypot(f, g);
69
+ const sx = q + r;
70
+ const sy = Math.abs(q - r);
71
+ const theta = (Math.atan2(h, e) + Math.atan2(g, f)) / 2;
72
+ const cosT = Math.cos(theta);
73
+ const sinT = Math.sin(theta);
74
+ const imageAngle = (angle) => {
75
+ const px = m00 * Math.cos(angle) + m01 * Math.sin(angle);
76
+ const py = m10 * Math.cos(angle) + m11 * Math.sin(angle);
77
+ return Math.atan2((-sinT * px + cosT * py) / (sy || 1), (cosT * px + sinT * py) / (sx || 1));
78
+ };
79
+ const startAngle = imageAngle(arc.startAngle);
80
+ let deltaAngle = imageAngle(arc.startAngle + arc.deltaAngle) - startAngle;
81
+ const sweepSign = cross2([a, b], [c, d]) < 0 ? -Math.sign(arc.deltaAngle) : Math.sign(arc.deltaAngle);
82
+ if (sweepSign > 0 && deltaAngle < 0) {
83
+ deltaAngle += FULL_TURN;
84
+ }
85
+ if (sweepSign < 0 && deltaAngle > 0) {
86
+ deltaAngle -= FULL_TURN;
87
+ }
88
+ return {
89
+ center: [a * arc.center[0] + c * arc.center[1] + tx, b * arc.center[0] + d * arc.center[1] + ty],
90
+ rx: sx,
91
+ ry: sy,
92
+ xAxisRotation: theta,
93
+ startAngle,
94
+ deltaAngle: sweepSign === 0 ? 0 : deltaAngle,
95
+ };
96
+ };
@@ -7,7 +7,9 @@ type Vec3 = Inputs.Base.Vector3;
7
7
  export declare const PARALLEL_SINE = 1e-9;
8
8
  /**
9
9
  * A frame as the four vectors the maths reads: the origin and three unit axes at right angles,
10
- * with `y` the cross product of `z` and `x`.
10
+ * with `y` the cross product of `z` and `x`. `vectorToWorld`, `pointToWorld` and `composeAxes`
11
+ * also map by axes of any length and angle, such as a scaled block insert's; the `ToLocal`
12
+ * functions and `relativeAxes` undo only unit axes at right angles.
11
13
  */
12
14
  export interface FrameAxes {
13
15
  readonly origin: Vec3;
@@ -0,0 +1,2 @@
1
+ import { type FillRule2 } from "./polygon-nesting";
2
+ export declare function filledLoopTriangles(positions: ArrayLike<number>, loops: readonly (readonly number[])[], rule: FillRule2): number[];
@@ -0,0 +1,37 @@
1
+ import { filledRegions2, nestedOutlines2, nestingOutlineOf2, overlappingBoxPairs2 } from "./polygon-nesting";
2
+ import { newellNormal, triangulateFace } from "./triangulation";
3
+ const POINT = 3;
4
+ const LEAST_CORNERS = 3;
5
+ const sameCorner = (positions, a, b) => positions[a * POINT] === positions[b * POINT]
6
+ && positions[a * POINT + 1] === positions[b * POINT + 1]
7
+ && positions[a * POINT + 2] === positions[b * POINT + 2];
8
+ const distinctCorners = (positions, loop) => loop.filter((vertex, at) => loop.length === 1 || !sameCorner(positions, vertex, loop[at === 0 ? loop.length - 1 : at - 1]));
9
+ const weightOf = ([x, y, z]) => x * x + y * y + z * z;
10
+ export function filledLoopTriangles(positions, loops, rule) {
11
+ const kept = loops.map((loop) => distinctCorners(positions, loop)).filter((loop) => loop.length >= LEAST_CORNERS);
12
+ const normals = kept.map((loop) => newellNormal(positions, loop));
13
+ const reference = normals.reduce((best, normal) => best === undefined || weightOf(normal) > weightOf(best) ? normal : best, undefined);
14
+ if (reference === undefined) {
15
+ return [];
16
+ }
17
+ const magnitudes = reference.map(Math.abs);
18
+ const dropped = magnitudes.indexOf(Math.max(...magnitudes));
19
+ if (!(magnitudes[dropped] > 0)) {
20
+ return [];
21
+ }
22
+ const u = (dropped + 1) % POINT;
23
+ const v = (dropped + 2) % POINT;
24
+ const sense = reference[dropped] < 0 ? -1 : 1;
25
+ const outlines = kept.map((loop) => nestingOutlineOf2(loop.map((vertex) => [positions[vertex * POINT + u] * sense, positions[vertex * POINT + v]])));
26
+ const ancestors = nestedOutlines2(outlines, overlappingBoxPairs2(outlines.map((outline) => outline.box)));
27
+ const triangles = [];
28
+ for (const region of filledRegions2(outlines, ancestors, rule)) {
29
+ const outer = kept[region.outer];
30
+ const facingReference = outlines[region.outer].area > 0 ? outer : [...outer].reverse();
31
+ const filled = triangulateFace(positions, [facingReference, ...region.holes.map((hole) => kept[hole])]);
32
+ for (const corner of filled) {
33
+ triangles.push(corner);
34
+ }
35
+ }
36
+ return triangles;
37
+ }
@@ -0,0 +1,6 @@
1
+ export declare const NESTING_BOX_MARGIN = 0.01;
2
+ export declare const INTERIOR_SCANLINES: number[];
3
+ export declare const COINCIDENT_AREA_SHARE = 1e-9;
4
+ export declare const SWEEP_CANDIDATES_PER_PAIR = 1000;
5
+ export declare const SWEEP_STEPS_PER_BOX = 20;
6
+ export declare const SWEEP_STEPS_PER_CANDIDATE = 2;
@@ -0,0 +1,6 @@
1
+ export const NESTING_BOX_MARGIN = 0.01;
2
+ export const INTERIOR_SCANLINES = [0.5, 0.25, 0.75, 0.125, 0.375, 0.625, 0.875];
3
+ export const COINCIDENT_AREA_SHARE = 1e-9;
4
+ export const SWEEP_CANDIDATES_PER_PAIR = 1000;
5
+ export const SWEEP_STEPS_PER_BOX = 20;
6
+ export const SWEEP_STEPS_PER_CANDIDATE = 2;
@@ -0,0 +1,31 @@
1
+ import type * as Inputs from "../../../inputs";
2
+ import { WorkBudget } from "../files/work-budget";
3
+ type Point2 = Inputs.Base.Point2;
4
+ export type FillRule2 = "nonzero" | "evenOdd";
5
+ export interface Box2 {
6
+ min: Point2;
7
+ max: Point2;
8
+ }
9
+ export interface NestingOutline2 {
10
+ points: Point2[];
11
+ area: number;
12
+ box: Box2;
13
+ inner?: Point2;
14
+ }
15
+ export interface FilledRegion2 {
16
+ outer: number;
17
+ holes: number[];
18
+ }
19
+ export declare const boxOf2: (points: readonly Point2[]) => Box2;
20
+ export declare const boxesOverlap2: (a: Box2, b: Box2) => boolean;
21
+ export declare const interiorPoint2: (points: readonly Point2[], box?: Box2) => Point2 | undefined;
22
+ export declare const nestingOutlineOf2: (points: Point2[]) => NestingOutline2;
23
+ export declare function overlappingBoxPairs2(boxes: readonly Box2[]): [number, number][];
24
+ export declare function overlappingBoxPairs2(boxes: readonly Box2[], most: number, budget?: WorkBudget): [number, number][] | undefined;
25
+ export declare const segmentsCross2: (p1: Point2, p2: Point2, q1: Point2, q2: Point2) => boolean;
26
+ export declare function outlinesCross2(outlines: readonly NestingOutline2[], pairs: readonly (readonly [number, number])[]): boolean;
27
+ export declare function outlinesCross2(outlines: readonly NestingOutline2[], pairs: readonly (readonly [number, number])[], budget: WorkBudget): boolean | undefined;
28
+ export declare function nestedOutlines2(outlines: readonly NestingOutline2[], pairs: readonly (readonly [number, number])[]): number[][];
29
+ export declare function nestedOutlines2(outlines: readonly NestingOutline2[], pairs: readonly (readonly [number, number])[], budget: WorkBudget): number[][] | undefined;
30
+ export declare const filledRegions2: (outlines: readonly NestingOutline2[], ancestors: readonly (readonly number[])[], rule: FillRule2) => FilledRegion2[];
31
+ export {};
@@ -0,0 +1,251 @@
1
+ import { WorkBudget } from "../files/work-budget";
2
+ import { isInsidePolygon2, signedArea2 } from "./polygons";
3
+ import { cross2, subtract2 } from "./vectors";
4
+ import { COINCIDENT_AREA_SHARE, INTERIOR_SCANLINES, NESTING_BOX_MARGIN, SWEEP_CANDIDATES_PER_PAIR, SWEEP_STEPS_PER_BOX, SWEEP_STEPS_PER_CANDIDATE, } from "./polygon-nesting.constants";
5
+ const SWEEP_AXES = [0, 1];
6
+ export const boxOf2 = (points) => {
7
+ const min = [Infinity, Infinity];
8
+ const max = [-Infinity, -Infinity];
9
+ for (const [x, y] of points) {
10
+ min[0] = Math.min(min[0], x);
11
+ min[1] = Math.min(min[1], y);
12
+ max[0] = Math.max(max[0], x);
13
+ max[1] = Math.max(max[1], y);
14
+ }
15
+ return { min, max };
16
+ };
17
+ export const boxesOverlap2 = (a, b) => a.min[0] <= b.max[0] && b.min[0] <= a.max[0] && a.min[1] <= b.max[1] && b.min[1] <= a.max[1];
18
+ const boxHolds = (outer, inner) => {
19
+ const margin = NESTING_BOX_MARGIN * Math.max(outer.max[0] - outer.min[0], outer.max[1] - outer.min[1]);
20
+ return inner.min[0] >= outer.min[0] - margin && inner.min[1] >= outer.min[1] - margin
21
+ && inner.max[0] <= outer.max[0] + margin && inner.max[1] <= outer.max[1] + margin;
22
+ };
23
+ const widestSpanAt = (points, y) => {
24
+ const xs = [];
25
+ for (let at = 0, previous = points.length - 1; at < points.length; previous = at++) {
26
+ const [x1, y1] = points[at];
27
+ const [x2, y2] = points[previous];
28
+ if ((y1 > y) !== (y2 > y)) {
29
+ xs.push(x1 + (y - y1) * (x2 - x1) / (y2 - y1));
30
+ }
31
+ }
32
+ xs.sort((a, b) => a - b);
33
+ let best;
34
+ for (let k = 0; k + 1 < xs.length; k += 2) {
35
+ const width = xs[k + 1] - xs[k];
36
+ if (width > 0 && (!best || width > best.width)) {
37
+ best = { width, middle: [(xs[k] + xs[k + 1]) / 2, y] };
38
+ }
39
+ }
40
+ return best;
41
+ };
42
+ export const interiorPoint2 = (points, box = boxOf2(points)) => {
43
+ let best;
44
+ for (const share of INTERIOR_SCANLINES) {
45
+ const span = widestSpanAt(points, box.min[1] + (box.max[1] - box.min[1]) * share);
46
+ if (span && (!best || span.width > best.width)) {
47
+ best = span;
48
+ }
49
+ }
50
+ return best === null || best === void 0 ? void 0 : best.middle;
51
+ };
52
+ export const nestingOutlineOf2 = (points) => {
53
+ const box = boxOf2(points);
54
+ const area = signedArea2(points);
55
+ const outline = { points, area, box };
56
+ const inner = area === 0 ? undefined : interiorPoint2(points, box);
57
+ if (inner) {
58
+ outline.inner = inner;
59
+ }
60
+ return outline;
61
+ };
62
+ const firstAbove = (sorted, value) => {
63
+ let low = 0;
64
+ let high = sorted.length;
65
+ while (low < high) {
66
+ const middle = (low + high) >> 1;
67
+ if (sorted[middle] > value) {
68
+ high = middle;
69
+ }
70
+ else {
71
+ low = middle + 1;
72
+ }
73
+ }
74
+ return low;
75
+ };
76
+ const sweepOrder = (boxes, axis) => boxes.map((_, i) => i).sort((a, b) => boxes[a].min[axis] - boxes[b].min[axis]);
77
+ const sweepCandidates = (boxes, order, axis) => {
78
+ const starts = order.map((i) => boxes[i].min[axis]);
79
+ return order.reduce((sum, i, k) => sum + Math.max(0, firstAbove(starts, boxes[i].max[axis]) - k - 1), 0);
80
+ };
81
+ const sweptPairs = (boxes, order, axis, most) => {
82
+ const pairs = [];
83
+ for (let k = 0; k < order.length; k++) {
84
+ const i = order[k];
85
+ const a = boxes[i];
86
+ for (let m = k + 1; m < order.length; m++) {
87
+ const j = order[m];
88
+ const b = boxes[j];
89
+ if (b.min[axis] > a.max[axis]) {
90
+ break;
91
+ }
92
+ if (boxesOverlap2(a, b)) {
93
+ if (pairs.length >= most) {
94
+ return undefined;
95
+ }
96
+ pairs.push([i, j]);
97
+ }
98
+ }
99
+ }
100
+ return pairs;
101
+ };
102
+ const unbounded = () => new WorkBudget(Number.POSITIVE_INFINITY);
103
+ export function overlappingBoxPairs2(boxes, most = Number.POSITIVE_INFINITY, budget = unbounded()) {
104
+ const examinable = most * SWEEP_CANDIDATES_PER_PAIR;
105
+ for (const axis of SWEEP_AXES) {
106
+ if (!budget.spend(boxes.length * SWEEP_STEPS_PER_BOX)) {
107
+ return undefined;
108
+ }
109
+ const order = sweepOrder(boxes, axis);
110
+ const candidates = sweepCandidates(boxes, order, axis);
111
+ if (candidates <= examinable) {
112
+ return budget.spend(candidates * SWEEP_STEPS_PER_CANDIDATE) ? sweptPairs(boxes, order, axis, most) : undefined;
113
+ }
114
+ }
115
+ return undefined;
116
+ }
117
+ export const segmentsCross2 = (p1, p2, q1, q2) => {
118
+ const d = subtract2(p2, p1);
119
+ const e = subtract2(q2, q1);
120
+ return cross2(d, subtract2(q1, p1)) * cross2(d, subtract2(q2, p1)) < 0 && cross2(e, subtract2(p1, q1)) * cross2(e, subtract2(p2, q1)) < 0;
121
+ };
122
+ const spanMeets = (p, q, low, high) => Math.min(p, q) <= high && low <= Math.max(p, q);
123
+ const edgesNear = (outline, box) => {
124
+ const { points } = outline;
125
+ const near = [];
126
+ for (let k = 0; k < points.length; k++) {
127
+ const p = points[k];
128
+ const q = points[(k + 1) % points.length];
129
+ if (spanMeets(p[0], q[0], box.min[0], box.max[0]) && spanMeets(p[1], q[1], box.min[1], box.max[1])) {
130
+ near.push(k);
131
+ }
132
+ }
133
+ return near;
134
+ };
135
+ const edgeBoxesOf = (outline, edges) => {
136
+ const boxes = {
137
+ minX: new Float64Array(edges.length), minY: new Float64Array(edges.length), maxX: new Float64Array(edges.length), maxY: new Float64Array(edges.length),
138
+ };
139
+ edges.forEach((k, at) => {
140
+ const p = outline.points[k];
141
+ const q = outline.points[(k + 1) % outline.points.length];
142
+ boxes.minX[at] = Math.min(p[0], q[0]);
143
+ boxes.minY[at] = Math.min(p[1], q[1]);
144
+ boxes.maxX[at] = Math.max(p[0], q[0]);
145
+ boxes.maxY[at] = Math.max(p[1], q[1]);
146
+ });
147
+ return boxes;
148
+ };
149
+ const edgeCrossesAny = (a, k, b, edges, boxes) => {
150
+ const p1 = a.points[k];
151
+ const p2 = a.points[(k + 1) % a.points.length];
152
+ const minX = Math.min(p1[0], p2[0]);
153
+ const minY = Math.min(p1[1], p2[1]);
154
+ const maxX = Math.max(p1[0], p2[0]);
155
+ const maxY = Math.max(p1[1], p2[1]);
156
+ for (let at = 0; at < edges.length; at++) {
157
+ if (boxes.minX[at] <= maxX && minX <= boxes.maxX[at] && boxes.minY[at] <= maxY && minY <= boxes.maxY[at]
158
+ && segmentsCross2(p1, p2, b.points[edges[at]], b.points[(edges[at] + 1) % b.points.length])) {
159
+ return true;
160
+ }
161
+ }
162
+ return false;
163
+ };
164
+ export function outlinesCross2(outlines, pairs, budget = unbounded()) {
165
+ for (const [first, second] of pairs) {
166
+ const a = outlines[first];
167
+ const b = outlines[second];
168
+ if (!budget.spend(a.points.length)) {
169
+ return undefined;
170
+ }
171
+ const ofA = edgesNear(a, b.box);
172
+ if (ofA.length === 0) {
173
+ continue;
174
+ }
175
+ if (!budget.spend(b.points.length)) {
176
+ return undefined;
177
+ }
178
+ const ofB = edgesNear(b, a.box);
179
+ const boxesOfB = edgeBoxesOf(b, ofB);
180
+ for (const k of ofA) {
181
+ if (!budget.spend(ofB.length)) {
182
+ return undefined;
183
+ }
184
+ if (edgeCrossesAny(a, k, b, ofB, boxesOfB)) {
185
+ return true;
186
+ }
187
+ }
188
+ }
189
+ return false;
190
+ }
191
+ const sameSize = (a, b) => Math.abs(a - b) <= COINCIDENT_AREA_SHARE * Math.max(a, b);
192
+ export function nestedOutlines2(outlines, pairs, budget = unbounded()) {
193
+ const ancestors = outlines.map(() => []);
194
+ let affordable = true;
195
+ const holds = (outer, inner) => {
196
+ if (inner.inner === undefined || !boxHolds(outer.box, inner.box)) {
197
+ return false;
198
+ }
199
+ affordable = budget.spend(outer.points.length);
200
+ return affordable && isInsidePolygon2(inner.inner, outer.points);
201
+ };
202
+ for (const [first, second] of pairs) {
203
+ const sizeOfFirst = Math.abs(outlines[first].area);
204
+ const sizeOfSecond = Math.abs(outlines[second].area);
205
+ if (sameSize(sizeOfFirst, sizeOfSecond)) {
206
+ if (holds(outlines[first], outlines[second]) && holds(outlines[second], outlines[first])) {
207
+ ancestors[Math.max(first, second)].push(Math.min(first, second));
208
+ }
209
+ }
210
+ else {
211
+ const [big, small] = sizeOfFirst > sizeOfSecond ? [first, second] : [second, first];
212
+ if (holds(outlines[big], outlines[small])) {
213
+ ancestors[small].push(big);
214
+ }
215
+ }
216
+ if (!affordable) {
217
+ return undefined;
218
+ }
219
+ }
220
+ return ancestors;
221
+ }
222
+ export const filledRegions2 = (outlines, ancestors, rule) => {
223
+ const depth = ancestors.map((a) => a.length);
224
+ const parent = ancestors.map((a) => a.reduce((best, j) => (best === -1 || depth[j] > depth[best] ? j : best), -1));
225
+ const sign = outlines.map((outline) => Math.sign(outline.area));
226
+ const filled = outlines.map((_, i) => rule === "evenOdd"
227
+ ? depth[i] % 2 === 0
228
+ : sign[i] + ancestors[i].reduce((sum, j) => sum + sign[j], 0) !== 0);
229
+ const effective = outlines.map((outline, i) => outline.inner !== undefined && filled[i] !== (parent[i] === -1 ? false : filled[parent[i]]));
230
+ const effectiveParent = (i) => {
231
+ let up = parent[i];
232
+ while (up !== -1 && !effective[up]) {
233
+ up = parent[up];
234
+ }
235
+ return up;
236
+ };
237
+ const regions = new Map();
238
+ outlines.forEach((_, i) => {
239
+ if (effective[i] && filled[i]) {
240
+ regions.set(i, { outer: i, holes: [] });
241
+ }
242
+ });
243
+ outlines.forEach((_, i) => {
244
+ var _a;
245
+ if (effective[i] && !filled[i]) {
246
+ (_a = regions.get(effectiveParent(i))) === null || _a === void 0 ? void 0 : _a.holes.push(i);
247
+ }
248
+ });
249
+ const sizeOf = (i) => Math.abs(outlines[i].area);
250
+ return [...regions.values()].filter((region) => !region.holes.some((hole) => sameSize(sizeOf(hole), sizeOf(region.outer))));
251
+ };
@@ -22,4 +22,5 @@ export declare const cross3: (a: Vec3, b: Vec3) => Vec3;
22
22
  export declare const length3: (a: Vec3) => number;
23
23
  export declare const between3: (a: Vec3, b: Vec3, share: number) => Vec3;
24
24
  export declare const distance3: (a: Vec3, b: Vec3) => number;
25
+ export declare const distanceToSegment3: (point: Vec3, start: Vec3, end: Vec3) => number;
25
26
  export {};
@@ -41,3 +41,9 @@ export const cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] *
41
41
  export const length3 = (a) => Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
42
42
  export const between3 = (a, b, share) => [a[0] + (b[0] - a[0]) * share, a[1] + (b[1] - a[1]) * share, a[2] + (b[2] - a[2]) * share];
43
43
  export const distance3 = (a, b) => length3(subtract3(a, b));
44
+ export const distanceToSegment3 = (point, start, end) => {
45
+ const along = subtract3(end, start);
46
+ const reach = dot3(along, along);
47
+ const share = reach > 0 ? Math.min(Math.max(dot3(subtract3(point, start), along) / reach, 0), 1) : 0;
48
+ return distance3(point, between3(start, end, share));
49
+ };
@@ -109,3 +109,36 @@ export interface GltfJson {
109
109
  readonly nodes?: readonly GltfNode[];
110
110
  }
111
111
  export type UnitOfLength = `${Inputs.Base.unitOfLengthEnum}`;
112
+ export type StrokePoint = readonly [number, number];
113
+ export interface StrokeGlyph {
114
+ readonly advance: number;
115
+ readonly strokes: readonly (readonly StrokePoint[])[];
116
+ }
117
+ export interface StrokeFontMarks {
118
+ readonly height: number;
119
+ readonly above: Readonly<Record<number, readonly (number | undefined)[]>>;
120
+ readonly below: Readonly<Record<number, readonly (number | undefined)[]>>;
121
+ }
122
+ export interface StrokeOverlay {
123
+ readonly base: number;
124
+ readonly strokes: readonly (number | undefined)[];
125
+ }
126
+ export interface StrokeFontLetters {
127
+ readonly height: number;
128
+ readonly overlaid: Readonly<Record<number, StrokeOverlay>>;
129
+ readonly drawn: Readonly<Record<number, readonly (number | undefined)[]>>;
130
+ }
131
+ export interface StrokeComposition {
132
+ readonly base: number;
133
+ readonly above: number | undefined;
134
+ readonly below: number | undefined;
135
+ }
136
+ export interface StrokeFontInk {
137
+ readonly lowest: number;
138
+ readonly highest: number;
139
+ }
140
+ export interface StrokeFontFacts {
141
+ readonly lowest: number;
142
+ readonly highest: number;
143
+ readonly glyphs: Map<number, StrokeGlyph>;
144
+ }
@@ -0,0 +1,6 @@
1
+ import type { VectorFont } from "../../../models/simplex";
2
+ import type { StrokeFontInk, StrokeGlyph, StrokePoint } from "../helper-types";
3
+ export declare function strokeGlyph(font: VectorFont, code: number): StrokeGlyph;
4
+ export declare function strokeFontInk(font: VectorFont): StrokeFontInk;
5
+ export declare function strokeTextAdvance(font: VectorFont, text: string): number;
6
+ export declare function strokeTextStrokes(font: VectorFont, text: string): StrokePoint[][];