@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,26 @@
1
+ import type * as Inputs from "../../../inputs";
2
+ import type { NurbsCurveForm } from "./nurbs-forms";
3
+ import type { EllipseArc2 } from "../geometry/ellipse-arcs";
4
+ import type { WorkBudget } from "../files/work-budget";
5
+ type Point3 = Inputs.Base.Point3;
6
+ type Vec3 = Inputs.Base.Vector3;
7
+ export interface EllipseFrame {
8
+ readonly center: Point3;
9
+ readonly xAxis: Vec3;
10
+ readonly yAxis: Vec3;
11
+ }
12
+ export interface ConicArc {
13
+ readonly frame: EllipseFrame;
14
+ readonly start: number;
15
+ readonly sweep: number;
16
+ }
17
+ export declare const angleWithinTurn: (angle: number, turn: number) => number;
18
+ export declare const ellipseFramePoint: (frame: EllipseFrame, angle: number) => Point3;
19
+ export declare const conicArcOfEllipseArc: (arc: EllipseArc2) => ConicArc;
20
+ export declare const conicArcSamples: (arc: ConicArc, tolerance: number, budget?: WorkBudget) => Point3[];
21
+ export declare const conicArcAsNurbs: (arc: ConicArc) => NurbsCurveForm;
22
+ export declare const bulgeArcOf: (start: Point3, end: Point3, bulge: number) => ConicArc;
23
+ export declare const bulgeOfSweep: (sweep: number) => number;
24
+ export declare const conicArcExtremes: (arc: ConicArc, axes?: readonly number[]) => Point3[];
25
+ export declare const principalArcOf: (arc: ConicArc) => ConicArc;
26
+ export {};
@@ -0,0 +1,87 @@
1
+ import { add3, distance2, dot3, length3, scale3 } from "../geometry/vectors";
2
+ import { BULGE_TURN_SHARE, FRAME_AXES, LEAST_RELATIVE_TOLERANCE, PIECE_SLACK, QUARTER_TURN } from "./conic-arcs.constants";
3
+ export const angleWithinTurn = (angle, turn) => ((angle % turn) + turn) % turn;
4
+ export const ellipseFramePoint = (frame, angle) => add3(frame.center, add3(scale3(frame.xAxis, Math.cos(angle)), scale3(frame.yAxis, Math.sin(angle))));
5
+ export const conicArcOfEllipseArc = (arc) => {
6
+ const cos = Math.cos(arc.xAxisRotation);
7
+ const sin = Math.sin(arc.xAxisRotation);
8
+ return {
9
+ frame: { center: [arc.center[0], arc.center[1], 0], xAxis: [arc.rx * cos, arc.rx * sin, 0], yAxis: [-arc.ry * sin, arc.ry * cos, 0] },
10
+ start: arc.startAngle,
11
+ sweep: arc.deltaAngle,
12
+ };
13
+ };
14
+ export const conicArcSamples = (arc, tolerance, budget) => {
15
+ const radius = Math.max(length3(arc.frame.xAxis), length3(arc.frame.yAxis));
16
+ const relative = Math.max(tolerance / radius, LEAST_RELATIVE_TOLERANCE);
17
+ const step = relative >= 1 ? QUARTER_TURN : Math.min(QUARTER_TURN, 2 * Math.acos(1 - relative));
18
+ const wanted = Math.max(1, Math.ceil(Math.abs(arc.sweep) / step));
19
+ const count = budget === undefined || budget.spend(wanted + 1) ? wanted : 1;
20
+ return Array.from({ length: count + 1 }, (_, at) => ellipseFramePoint(arc.frame, arc.start + (arc.sweep * at) / count));
21
+ };
22
+ export const conicArcAsNurbs = (arc) => {
23
+ const pieces = Math.max(1, Math.ceil(Math.abs(arc.sweep) / QUARTER_TURN - PIECE_SLACK));
24
+ const step = arc.sweep / pieces;
25
+ const middleWeight = Math.cos(step / 2);
26
+ const points = [ellipseFramePoint(arc.frame, arc.start)];
27
+ const weights = [1];
28
+ const knots = [0, 0, 0];
29
+ for (let piece = 0; piece < pieces; piece++) {
30
+ const middle = arc.start + ((2 * piece + 1) * step) / 2;
31
+ points.push(add3(arc.frame.center, scale3(add3(scale3(arc.frame.xAxis, Math.cos(middle)), scale3(arc.frame.yAxis, Math.sin(middle))), 1 / middleWeight)));
32
+ points.push(ellipseFramePoint(arc.frame, arc.start + (piece + 1) * step));
33
+ weights.push(middleWeight, 1);
34
+ knots.push(piece + 1, piece + 1);
35
+ }
36
+ knots.push(pieces);
37
+ return { degree: 2, points, weights, knots };
38
+ };
39
+ export const bulgeArcOf = (start, end, bulge) => {
40
+ const dx = end[0] - start[0];
41
+ const dy = end[1] - start[1];
42
+ const offset = (1 - bulge * bulge) / (BULGE_TURN_SHARE * bulge);
43
+ const center = [(start[0] + end[0]) / 2 - dy * offset, (start[1] + end[1]) / 2 + dx * offset, start[2]];
44
+ const radius = distance2([start[0], start[1]], [center[0], center[1]]);
45
+ return {
46
+ frame: { center, xAxis: [radius, 0, 0], yAxis: [0, radius, 0] },
47
+ start: Math.atan2(start[1] - center[1], start[0] - center[0]),
48
+ sweep: BULGE_TURN_SHARE * Math.atan(bulge),
49
+ };
50
+ };
51
+ export const bulgeOfSweep = (sweep) => Math.tan(sweep / BULGE_TURN_SHARE);
52
+ export const conicArcExtremes = (arc, axes = FRAME_AXES) => {
53
+ const from = Math.min(arc.start, arc.start + arc.sweep);
54
+ const to = Math.min(Math.max(arc.start, arc.start + arc.sweep), from + 2 * Math.PI);
55
+ const halfTurnsAcross = Math.ceil(Math.min(Math.abs(arc.sweep), 2 * Math.PI) / Math.PI) + 1;
56
+ const turns = [];
57
+ for (const axis of axes) {
58
+ const flat = Math.atan2(arc.frame.yAxis[axis], arc.frame.xAxis[axis]);
59
+ const firstHalfTurn = Math.ceil((from - flat) / Math.PI);
60
+ for (let step = 0; step < halfTurnsAcross; step++) {
61
+ const angle = flat + (firstHalfTurn + step) * Math.PI;
62
+ if (angle > to) {
63
+ break;
64
+ }
65
+ turns.push(angle);
66
+ }
67
+ }
68
+ const along = turns.filter(angle => angle > from && angle < to).sort((first, second) => (first - second) * Math.sign(arc.sweep));
69
+ return [arc.start, ...along, arc.start + arc.sweep].map(angle => ellipseFramePoint(arc.frame, angle));
70
+ };
71
+ export const principalArcOf = (arc) => {
72
+ const { center, xAxis, yAxis } = arc.frame;
73
+ const xx = dot3(xAxis, xAxis);
74
+ const yy = dot3(yAxis, yAxis);
75
+ const xy = dot3(xAxis, yAxis);
76
+ if (Math.abs(xy) <= Number.EPSILON * (xx + yy)) {
77
+ return xx >= yy ? arc : { frame: { center, xAxis: yAxis, yAxis: scale3(xAxis, -1) }, start: arc.start - Math.PI / 2, sweep: arc.sweep };
78
+ }
79
+ const shift = Math.atan2(2 * xy, xx - yy) / 2;
80
+ const cos = Math.cos(shift);
81
+ const sin = Math.sin(shift);
82
+ return {
83
+ frame: { center, xAxis: add3(scale3(xAxis, cos), scale3(yAxis, sin)), yAxis: add3(scale3(xAxis, -sin), scale3(yAxis, cos)) },
84
+ start: arc.start - shift,
85
+ sweep: arc.sweep,
86
+ };
87
+ };
@@ -0,0 +1,20 @@
1
+ import type * as Inputs from "../../../inputs";
2
+ import type { WorkBudget } from "../files/work-budget";
3
+ type Point3 = Inputs.Base.Point3;
4
+ export interface NurbsCurveForm {
5
+ readonly degree: number;
6
+ readonly points: readonly Point3[];
7
+ readonly weights: readonly number[];
8
+ readonly knots: readonly number[];
9
+ }
10
+ export interface BezierPiece {
11
+ readonly points: readonly Point3[];
12
+ readonly weights: readonly number[];
13
+ }
14
+ export declare const clampedUniformKnots: (pointCount: number, degree: number) => number[];
15
+ export declare const periodicUniformForm: (points: readonly Point3[], weights: readonly number[], degree: number) => NurbsCurveForm;
16
+ export declare const insertKnot: (form: NurbsCurveForm, parameter: number, times?: number) => NurbsCurveForm;
17
+ export declare const clampedForm: (form: NurbsCurveForm) => NurbsCurveForm;
18
+ export declare const bezierPiecesOf: (form: NurbsCurveForm, budget?: WorkBudget) => BezierPiece[];
19
+ export declare const isRationalPiece: (piece: BezierPiece) => boolean;
20
+ export {};
@@ -0,0 +1,112 @@
1
+ import { blendedPoint, knotDomain, liftedPoint, projectedPoint } from "./nurbs";
2
+ const homogeneousOf = (form) => form.points.map((point, at) => { var _a; return liftedPoint(point, (_a = form.weights[at]) !== null && _a !== void 0 ? _a : 1); });
3
+ const pieceOf = (homogeneous) => ({ points: homogeneous.map(projectedPoint), weights: homogeneous.map(point => point[3]) });
4
+ export const clampedUniformKnots = (pointCount, degree) => {
5
+ const spans = pointCount - degree;
6
+ return Array.from({ length: pointCount + degree + 1 }, (_, at) => Math.min(Math.max(at - degree, 0), spans));
7
+ };
8
+ export const periodicUniformForm = (points, weights, degree) => {
9
+ const wrapped = [...points, ...points.slice(0, degree)];
10
+ const wrappedWeights = [...weights, ...weights.slice(0, degree)];
11
+ return { degree, points: wrapped, weights: wrappedWeights, knots: Array.from({ length: wrapped.length + degree + 1 }, (_, at) => at) };
12
+ };
13
+ const insertionSpan = (degree, pointCount, knots, parameter) => {
14
+ let span = degree;
15
+ while (span < pointCount - 1 && knots[span + 1] <= parameter) {
16
+ span++;
17
+ }
18
+ return span;
19
+ };
20
+ const knotAfterRounds = (knots, span, parameter, rounds, at) => {
21
+ if (at <= span) {
22
+ return knots[at];
23
+ }
24
+ return at <= span + rounds ? parameter : knots[at - rounds];
25
+ };
26
+ export const insertKnot = (form, parameter, times = 1) => {
27
+ const { degree, knots, points, weights } = form;
28
+ const span = insertionSpan(degree, points.length, knots, parameter);
29
+ const base = span - degree;
30
+ const window = points.slice(base, span + 1).map((point, at) => { var _a; return liftedPoint(point, (_a = weights[base + at]) !== null && _a !== void 0 ? _a : 1); });
31
+ for (let round = 0; round < times; round++) {
32
+ window.push(window[degree + round]);
33
+ for (let at = degree + round; at > round; at--) {
34
+ const left = knotAfterRounds(knots, span, parameter, round, base + at);
35
+ const reach = knotAfterRounds(knots, span, parameter, round, base + at + degree) - left;
36
+ window[at] = blendedPoint(window[at - 1], window[at], reach > 0 ? (parameter - left) / reach : 0);
37
+ }
38
+ }
39
+ const inserted = Array.from({ length: Math.max(times, 0) }, () => parameter);
40
+ return {
41
+ degree,
42
+ points: [...points.slice(0, base), ...window.map(projectedPoint), ...points.slice(span + 1)],
43
+ weights: [...weights.slice(0, base), ...window.map(point => point[3]), ...weights.slice(span + 1)],
44
+ knots: [...knots.slice(0, span + 1), ...inserted, ...knots.slice(span + 1)],
45
+ };
46
+ };
47
+ const multiplicityOf = (knots, value) => knots.filter(knot => knot === value).length;
48
+ export const clampedForm = (form) => {
49
+ const [first, last] = knotDomain(form.degree, form.knots);
50
+ let clamped = form;
51
+ for (const end of [first, last]) {
52
+ const missing = form.degree - multiplicityOf(clamped.knots, end);
53
+ if (missing > 0) {
54
+ clamped = insertKnot(clamped, end, missing);
55
+ }
56
+ }
57
+ const lastAtStart = clamped.knots.lastIndexOf(first);
58
+ const firstAtEnd = clamped.knots.indexOf(last);
59
+ const from = lastAtStart - form.degree;
60
+ const interior = clamped.knots.slice(lastAtStart + 1, firstAtEnd);
61
+ const ends = Array.from({ length: form.degree + 1 });
62
+ return {
63
+ degree: form.degree,
64
+ points: clamped.points.slice(from, firstAtEnd),
65
+ weights: clamped.weights.slice(from, firstAtEnd),
66
+ knots: [...ends.map(() => first), ...interior, ...ends.map(() => last)],
67
+ };
68
+ };
69
+ export const bezierPiecesOf = (form, budget) => {
70
+ const clamped = clampedForm(form);
71
+ const { degree, knots } = clamped;
72
+ if (budget !== undefined && !budget.spend(((clamped.points.length - degree) * (degree + 1) * (degree + 2)) / 2)) {
73
+ return [];
74
+ }
75
+ const lastKnot = knots.length - 1;
76
+ const control = homogeneousOf(clamped);
77
+ const pieces = [];
78
+ let piece = control.slice(0, degree + 1);
79
+ let spanStart = degree;
80
+ let spanEnd = degree + 1;
81
+ while (spanEnd < lastKnot) {
82
+ const groupStart = spanEnd;
83
+ while (spanEnd < lastKnot && knots[spanEnd + 1] === knots[spanEnd]) {
84
+ spanEnd++;
85
+ }
86
+ const multiplicity = spanEnd - groupStart + 1;
87
+ const next = [];
88
+ if (multiplicity < degree) {
89
+ const reach = knots[spanEnd] - knots[spanStart];
90
+ const shares = Array.from({ length: degree - multiplicity }, (_, at) => reach / (knots[spanStart + multiplicity + 1 + at] - knots[spanStart]));
91
+ const insertions = degree - multiplicity;
92
+ for (let round = 1; round <= insertions; round++) {
93
+ const from = multiplicity + round;
94
+ for (let at = degree; at >= from; at--) {
95
+ piece[at] = blendedPoint(piece[at - 1], piece[at], shares[at - from]);
96
+ }
97
+ next[insertions - round] = piece[degree];
98
+ }
99
+ }
100
+ pieces.push(pieceOf(piece));
101
+ if (spanEnd < lastKnot) {
102
+ for (let at = degree - multiplicity; at <= degree; at++) {
103
+ next[at] = control[spanEnd - degree + at];
104
+ }
105
+ piece = next;
106
+ spanStart = spanEnd;
107
+ spanEnd++;
108
+ }
109
+ }
110
+ return pieces;
111
+ };
112
+ export const isRationalPiece = (piece) => piece.weights.some(weight => weight !== piece.weights[0]);
@@ -1,5 +1,9 @@
1
1
  import type * as Inputs from "../../../inputs";
2
2
  type Point3 = Inputs.Base.Point3;
3
+ export type Homogeneous = [number, number, number, number];
4
+ export declare const liftedPoint: (point: Point3, weight: number) => Homogeneous;
5
+ export declare const projectedPoint: (point: Homogeneous) => Point3;
6
+ export declare const blendedPoint: (from: Homogeneous, to: Homogeneous, share: number) => Homogeneous;
3
7
  export declare const expandedKnots: (knots: readonly number[], multiplicities: readonly number[]) => number[];
4
8
  export declare const knotDomain: (degree: number, knots: readonly number[]) => [number, number];
5
9
  export declare const nurbsCurvePoint: (degree: number, points: readonly Point3[], weights: readonly number[] | undefined, knots: readonly number[], t: number) => Point3;
@@ -1,3 +1,9 @@
1
+ export const liftedPoint = (point, weight) => [point[0] * weight, point[1] * weight, point[2] * weight, weight];
2
+ export const projectedPoint = (point) => [point[0] / point[3], point[1] / point[3], point[2] / point[3]];
3
+ export const blendedPoint = (from, to, share) => {
4
+ const keep = 1 - share;
5
+ return [keep * from[0] + share * to[0], keep * from[1] + share * to[1], keep * from[2] + share * to[2], keep * from[3] + share * to[3]];
6
+ };
1
7
  export const expandedKnots = (knots, multiplicities) => knots.flatMap((knot, at) => { var _a; return Array.from({ length: (_a = multiplicities[at]) !== null && _a !== void 0 ? _a : 0 }, () => knot); });
2
8
  export const knotDomain = (degree, knots) => [knots[degree], knots[knots.length - degree - 1]];
3
9
  const spanOf = (degree, knots, t) => {
@@ -31,20 +37,15 @@ const deBoor = (degree, knots, parameter, at) => {
31
37
  const left = knots[span - degree + j];
32
38
  const right = knots[span + 1 + j - r];
33
39
  const alpha = right === left ? 0 : (t - left) / (right - left);
34
- const previous = points[j - 1];
35
- const current = points[j];
36
- const keep = 1 - alpha;
37
- points[j] = [keep * previous[0] + alpha * current[0], keep * previous[1] + alpha * current[1], keep * previous[2] + alpha * current[2], keep * previous[3] + alpha * current[3]];
40
+ points[j] = blendedPoint(points[j - 1], points[j], alpha);
38
41
  }
39
42
  }
40
43
  return points[degree];
41
44
  };
42
- const lifted = (point, weight) => [point[0] * weight, point[1] * weight, point[2] * weight, weight];
43
- const projected = (point) => [point[0] / point[3], point[1] / point[3], point[2] / point[3]];
44
- export const nurbsCurvePoint = (degree, points, weights, knots, t) => projected(deBoor(degree, knots, t, (index) => { var _a; return lifted(points[index], (_a = weights === null || weights === void 0 ? void 0 : weights[index]) !== null && _a !== void 0 ? _a : 1); }));
45
+ export const nurbsCurvePoint = (degree, points, weights, knots, t) => projectedPoint(deBoor(degree, knots, t, (index) => { var _a; return liftedPoint(points[index], (_a = weights === null || weights === void 0 ? void 0 : weights[index]) !== null && _a !== void 0 ? _a : 1); }));
45
46
  export const nurbsSurfacePoint = (degrees, net, weights, knots, u, v) => {
46
47
  const [uDegree, vDegree] = degrees;
47
48
  const [uKnots, vKnots] = knots;
48
- const row = (index) => deBoor(vDegree, vKnots, v, (column) => { var _a, _b; return lifted(net[index][column], (_b = (_a = weights === null || weights === void 0 ? void 0 : weights[index]) === null || _a === void 0 ? void 0 : _a[column]) !== null && _b !== void 0 ? _b : 1); });
49
- return projected(deBoor(uDegree, uKnots, u, row));
49
+ const row = (index) => deBoor(vDegree, vKnots, v, (column) => { var _a, _b; return liftedPoint(net[index][column], (_b = (_a = weights === null || weights === void 0 ? void 0 : weights[index]) === null || _a === void 0 ? void 0 : _a[column]) !== null && _b !== void 0 ? _b : 1); });
50
+ return projectedPoint(deBoor(uDegree, uKnots, u, row));
50
51
  };
@@ -0,0 +1,3 @@
1
+ import { WorkBudget } from "../work-budget";
2
+ import type { DrawingPart } from "./drawing-types";
3
+ export declare const writerBudgetOf: (parts: readonly DrawingPart[]) => WorkBudget;
@@ -0,0 +1,34 @@
1
+ import { WorkBudget } from "../work-budget";
2
+ import { NUMBERS_PER_CHARACTER, NUMBERS_PER_CURVE, NUMBERS_PER_HATCH, NUMBERS_PER_PART, NUMBERS_PER_TEXT, NUMBERS_PER_VERTEX, WRITER_WORK_CEILING, WRITER_WORK_FLOOR, WRITER_WORK_PER_NUMBER } from "./drawing.constants";
3
+ const segmentNumbers = (segment) => {
4
+ switch (segment.kind) {
5
+ case "polyline":
6
+ return segment.points.length * NUMBERS_PER_VERTEX;
7
+ case "spline":
8
+ return segment.curve.points.length * NUMBERS_PER_VERTEX + segment.curve.knots.length;
9
+ default:
10
+ return NUMBERS_PER_CURVE[segment.kind];
11
+ }
12
+ };
13
+ const pathNumbers = (paths) => {
14
+ let numbers = 0;
15
+ for (const path of paths) {
16
+ for (const segment of path.segments) {
17
+ numbers += segmentNumbers(segment);
18
+ }
19
+ }
20
+ return numbers;
21
+ };
22
+ export const writerBudgetOf = (parts) => {
23
+ let numbers = 0;
24
+ for (const part of parts) {
25
+ numbers += NUMBERS_PER_PART + pathNumbers(part.paths);
26
+ for (const hatch of part.hatches) {
27
+ numbers += NUMBERS_PER_HATCH + pathNumbers(hatch.boundaries);
28
+ }
29
+ for (const text of part.texts) {
30
+ numbers += NUMBERS_PER_TEXT + text.value.length * NUMBERS_PER_CHARACTER;
31
+ }
32
+ }
33
+ return WorkBudget.forInput(numbers, WRITER_WORK_PER_NUMBER, WRITER_WORK_FLOOR, WRITER_WORK_CEILING);
34
+ };
@@ -0,0 +1,14 @@
1
+ export declare const ARROW_WIDTH_SHARE: number;
2
+ export declare const TEXT_GAP_SHARE: number;
3
+ export declare const EXTENSION_GAP_SHARE: number;
4
+ export declare const EXTENSION_OVERSHOOT_SHARE: number;
5
+ export declare const ARROWS_INSIDE_ROOM: number;
6
+ export declare const ARROW_TAIL_SHARE: number;
7
+ export declare const ARC_TOLERANCE_SHARE: number;
8
+ export declare const MOST_DECIMAL_PLACES = 15;
9
+ export declare const TOLERANCE_DECIMAL_PLACES = 6;
10
+ export declare const UPRIGHT_SLACK = 1e-9;
11
+ export declare const DIAMETER_SIGN = "\u2300";
12
+ export declare const RADIUS_SIGN = "R";
13
+ export declare const DEGREE_SIGN = "\u00B0";
14
+ export declare const PLUS_MINUS_SIGN = "\u00B1";
@@ -0,0 +1,14 @@
1
+ export const ARROW_WIDTH_SHARE = 1 / 3;
2
+ export const TEXT_GAP_SHARE = 1 / 4;
3
+ export const EXTENSION_GAP_SHARE = 1 / 4;
4
+ export const EXTENSION_OVERSHOOT_SHARE = 1 / 2;
5
+ export const ARROWS_INSIDE_ROOM = 5 / 2;
6
+ export const ARROW_TAIL_SHARE = 3 / 2;
7
+ export const ARC_TOLERANCE_SHARE = 1 / 100;
8
+ export const MOST_DECIMAL_PLACES = 15;
9
+ export const TOLERANCE_DECIMAL_PLACES = 6;
10
+ export const UPRIGHT_SLACK = 1e-9;
11
+ export const DIAMETER_SIGN = "⌀";
12
+ export const RADIUS_SIGN = "R";
13
+ export const DEGREE_SIGN = "°";
14
+ export const PLUS_MINUS_SIGN = "±";
@@ -0,0 +1,4 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import type * as Resolved from "../../../../resolved-inputs";
3
+ export declare const dimensionExtentOf: (dimension: Resolved.IO.DrawingDimensionDto) => Inputs.IO.DrawingExtentDto;
4
+ export declare const dimensionPictureOf: (dimension: Resolved.IO.DrawingDimensionDto) => Inputs.IO.DrawingDimensionPictureDto;
@@ -0,0 +1,271 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import { InputError } from "../../../../kernel-calls";
3
+ import { simplex } from "../../../../models/simplex";
4
+ import { conicArcSamples } from "../../curves/conic-arcs";
5
+ import { degreesOf, radiansOf, roundedTo } from "../../geometry/scalars";
6
+ import { add2, between2, distance2, dot2, leftOf2, polar2, scale2, subtract2, unit2Of } from "../../geometry/vectors";
7
+ import { strokeTextAdvance, strokeTextStrokes } from "../../text/stroke-font";
8
+ import { drawingTextOf, finiteNumber, isDrawable, positiveNumber } from "./drawing-model";
9
+ import { textCornersOf, textStrokesOf } from "./drawing-text";
10
+ import { FULL_TURN_DEGREES, SAME_POINT } from "./drawing.constants";
11
+ import { ARC_TOLERANCE_SHARE, ARROWS_INSIDE_ROOM, ARROW_TAIL_SHARE, ARROW_WIDTH_SHARE, DEGREE_SIGN, DIAMETER_SIGN, EXTENSION_GAP_SHARE, EXTENSION_OVERSHOOT_SHARE, MOST_DECIMAL_PLACES, PLUS_MINUS_SIGN, RADIUS_SIGN, TEXT_GAP_SHARE, TOLERANCE_DECIMAL_PLACES, UPRIGHT_SLACK, } from "./drawing-dimension.constants";
12
+ const KINDS = new Set(Object.values(Inputs.IO.dimensionKindEnum));
13
+ const DEFAULTS = new Inputs.IO.DrawingDimensionDto();
14
+ const QUARTER_TURN = Math.PI / 2;
15
+ const point2Of = (value, property) => {
16
+ if (!Array.isArray(value) || value.length !== 2 || !value.every(isDrawable)) {
17
+ throw new InputError(`\`${property}\` must be a point of 2 finite numbers.`, property);
18
+ }
19
+ return [value[0], value[1]];
20
+ };
21
+ const amountOf = (value, property) => {
22
+ if (value === undefined) {
23
+ return undefined;
24
+ }
25
+ const amount = finiteNumber(value, property);
26
+ if (amount < 0) {
27
+ throw new InputError(`\`${property}\` is an amount and must not be below 0; it is ${amount}.`, property);
28
+ }
29
+ return amount;
30
+ };
31
+ const decimalPlacesOf = (value) => {
32
+ const places = finiteNumber(value, "decimalPlaces");
33
+ if (!Number.isInteger(places) || places < 0 || places > MOST_DECIMAL_PLACES) {
34
+ throw new InputError(`\`decimalPlaces\` must be a whole number from 0 to ${MOST_DECIMAL_PLACES}; it is ${places}.`, "decimalPlaces");
35
+ }
36
+ return places;
37
+ };
38
+ const writtenNumber = (value, places) => {
39
+ const fixed = roundedTo(value, places).toFixed(places);
40
+ const trimmed = fixed.includes(".") ? fixed.replace(/0+$/, "").replace(/\.$/, "") : fixed;
41
+ return trimmed === "-0" ? "0" : trimmed;
42
+ };
43
+ const toleranceText = (upper, lower) => {
44
+ if (upper === undefined && lower === undefined) {
45
+ return "";
46
+ }
47
+ const above = writtenNumber(upper !== null && upper !== void 0 ? upper : 0, TOLERANCE_DECIMAL_PLACES);
48
+ const below = writtenNumber(lower !== null && lower !== void 0 ? lower : 0, TOLERANCE_DECIMAL_PLACES);
49
+ return above === below ? ` ${PLUS_MINUS_SIGN}${above}` : ` +${above}/-${below}`;
50
+ };
51
+ const radiusOf = (start, end) => {
52
+ const radius = distance2(start, end);
53
+ if (radius <= SAME_POINT) {
54
+ throw new InputError("`end` lies on `start`, so the dimension has no circle or arm to measure.", "end");
55
+ }
56
+ return radius;
57
+ };
58
+ const sweepOf = (value) => {
59
+ const degrees = finiteNumber(value, "angle");
60
+ if (degrees <= 0 || degrees >= FULL_TURN_DEGREES) {
61
+ throw new InputError(`\`angle\` must lie between 0 and ${FULL_TURN_DEGREES} degrees, both left out; it is ${degrees}.`, "angle");
62
+ }
63
+ return radiansOf(degrees);
64
+ };
65
+ const linearMeasureOf = (start, end, direction) => {
66
+ const along = direction !== null && direction !== void 0 ? direction : unit2Of(subtract2(end, start));
67
+ const measured = along === undefined ? 0 : Math.abs(dot2(subtract2(end, start), along));
68
+ if (measured <= SAME_POINT) {
69
+ throw new InputError("`end` lies level with `start` along the measured direction, so the dimension measures no distance.", "end");
70
+ }
71
+ return measured;
72
+ };
73
+ const directionOf = (value) => {
74
+ if (value === undefined) {
75
+ return undefined;
76
+ }
77
+ const direction = unit2Of(point2Of(value, "direction"));
78
+ if (direction === undefined) {
79
+ throw new InputError("`direction` must not be a zero vector.", "direction");
80
+ }
81
+ return direction;
82
+ };
83
+ const dimensionOf = (dimension) => {
84
+ var _a, _b, _c, _d, _e;
85
+ const kind = (_a = dimension.kind) !== null && _a !== void 0 ? _a : DEFAULTS.kind;
86
+ if (typeof kind !== "string" || !KINDS.has(kind)) {
87
+ throw new InputError(`\`kind\` must be one of ${[...KINDS].join(", ")}.`, "kind");
88
+ }
89
+ const start = point2Of(dimension.start, "start");
90
+ const end = point2Of(dimension.end, "end");
91
+ const textHeight = positiveNumber((_b = dimension.textHeight) !== null && _b !== void 0 ? _b : DEFAULTS.textHeight, "textHeight");
92
+ const arrowLength = dimension.arrowLength === undefined ? textHeight : positiveNumber(dimension.arrowLength, "arrowLength");
93
+ const places = decimalPlacesOf((_c = dimension.decimalPlaces) !== null && _c !== void 0 ? _c : DEFAULTS.decimalPlaces);
94
+ const upper = amountOf(dimension.upperTolerance, "upperTolerance");
95
+ const lower = amountOf(dimension.lowerTolerance, "lowerTolerance");
96
+ const given = dimension.value === undefined ? undefined : finiteNumber(dimension.value, "value");
97
+ const direction = kind === Inputs.IO.dimensionKindEnum.linear ? directionOf(dimension.direction) : undefined;
98
+ const offset = finiteNumber((_d = dimension.offset) !== null && _d !== void 0 ? _d : DEFAULTS.offset, "offset");
99
+ const sweep = kind === Inputs.IO.dimensionKindEnum.angular ? sweepOf((_e = dimension.angle) !== null && _e !== void 0 ? _e : DEFAULTS.angle) : 0;
100
+ let measured;
101
+ let prefix = "";
102
+ let suffix = "";
103
+ if (kind === Inputs.IO.dimensionKindEnum.linear) {
104
+ measured = linearMeasureOf(start, end, direction);
105
+ }
106
+ else if (kind === Inputs.IO.dimensionKindEnum.angular) {
107
+ const radius = radiusOf(start, end);
108
+ if (radius < arrowLength) {
109
+ throw new InputError(`\`end\` lies ${radius} from \`start\`, nearer than an arrowhead is long (${arrowLength}), so the arc has no room for its arrows.`, "end");
110
+ }
111
+ measured = degreesOf(sweep);
112
+ suffix = DEGREE_SIGN;
113
+ }
114
+ else {
115
+ const radius = radiusOf(start, end);
116
+ measured = kind === Inputs.IO.dimensionKindEnum.diameter ? 2 * radius : radius;
117
+ prefix = kind === Inputs.IO.dimensionKindEnum.diameter ? DIAMETER_SIGN : RADIUS_SIGN;
118
+ }
119
+ return {
120
+ kind,
121
+ start,
122
+ end,
123
+ direction,
124
+ offset,
125
+ sweep,
126
+ text: `${prefix}${writtenNumber(given !== null && given !== void 0 ? given : measured, places)}${suffix}${toleranceText(upper, lower)}`,
127
+ textHeight,
128
+ arrowLength,
129
+ };
130
+ };
131
+ const arrowhead = (tip, intoBody, length) => {
132
+ const base = add2(tip, scale2(intoBody, length));
133
+ const side = scale2(leftOf2(intoBody), length * ARROW_WIDTH_SHARE / 2);
134
+ return [tip, subtract2(base, side), add2(base, side)];
135
+ };
136
+ const readableTurn = (along) => {
137
+ const turn = Math.atan2(along[1], along[0]);
138
+ if (turn > QUARTER_TURN + UPRIGHT_SLACK) {
139
+ return turn - Math.PI;
140
+ }
141
+ return turn <= -QUARTER_TURN + UPRIGHT_SLACK ? turn + Math.PI : turn;
142
+ };
143
+ const inkOf = (value) => {
144
+ let low = 0;
145
+ let high = 0;
146
+ for (const stroke of strokeTextStrokes(simplex, value)) {
147
+ for (const point of stroke) {
148
+ low = Math.min(low, point[1]);
149
+ high = Math.max(high, point[1]);
150
+ }
151
+ }
152
+ return { low, high };
153
+ };
154
+ const textAlong = (dimension, centre, along, outward) => {
155
+ const turn = readableTurn(along);
156
+ const up = leftOf2(polar2(1, turn));
157
+ const ink = inkOf(dimension.text);
158
+ const gap = dimension.textHeight * TEXT_GAP_SHARE;
159
+ const away = outward !== null && outward !== void 0 ? outward : up;
160
+ const clearance = dot2(up, away) >= 0 ? gap - ink.low * dimension.textHeight : gap + ink.high * dimension.textHeight;
161
+ const position = add2(centre, scale2(away, clearance));
162
+ return drawingTextOf({ text: dimension.text, position, height: dimension.textHeight, anchor: "middle", rotation: degreesOf(turn) }, "");
163
+ };
164
+ const textWidthOf = (dimension) => strokeTextAdvance(simplex, dimension.text) * dimension.textHeight;
165
+ const straightLayout = (dimension, line, extensions) => {
166
+ const length = dimension.arrowLength;
167
+ const span = distance2(line.from, line.to);
168
+ const along = scale2(subtract2(line.to, line.from), 1 / span);
169
+ const arrows = line.arrowAtFrom ? 2 : 1;
170
+ const inside = span >= ARROWS_INSIDE_ROOM * length * arrows / 2;
171
+ const tail = inside ? 0 : ARROW_TAIL_SHARE * length;
172
+ const fills = [arrowhead(line.to, scale2(along, inside ? -1 : 1), length)];
173
+ if (line.arrowAtFrom) {
174
+ fills.unshift(arrowhead(line.from, scale2(along, inside ? 1 : -1), length));
175
+ }
176
+ const width = textWidthOf(dimension);
177
+ const gap = dimension.textHeight * TEXT_GAP_SHARE;
178
+ const room = span - (inside ? arrows * length : 0);
179
+ const fits = width + 2 * gap <= room;
180
+ const centre = fits ? between2(line.from, line.to, 1 / 2) : add2(line.to, scale2(along, tail + gap + width / 2));
181
+ const first = line.arrowAtFrom ? subtract2(line.from, scale2(along, tail)) : line.from;
182
+ const last = fits ? add2(line.to, scale2(along, tail)) : add2(centre, scale2(along, width / 2));
183
+ return { lines: [[first, last], ...extensions], fills, text: textAlong(dimension, centre, along, undefined) };
184
+ };
185
+ const extensionLine = (point, foot, side, dimension) => {
186
+ const toward = subtract2(foot, point);
187
+ const reach = distance2(foot, point);
188
+ const along = reach > SAME_POINT ? scale2(toward, 1 / reach) : side;
189
+ const gap = dimension.textHeight * EXTENSION_GAP_SHARE;
190
+ const from = reach > gap ? add2(point, scale2(along, gap)) : point;
191
+ return [from, add2(foot, scale2(along, dimension.textHeight * EXTENSION_OVERSHOOT_SHARE))];
192
+ };
193
+ const linearLayout = (dimension) => {
194
+ var _a;
195
+ const along = (_a = dimension.direction) !== null && _a !== void 0 ? _a : unit2Of(subtract2(dimension.end, dimension.start));
196
+ const side = leftOf2(along);
197
+ const height = dot2(dimension.start, side) + dimension.offset;
198
+ const footOf = (point) => add2(scale2(along, dot2(point, along)), scale2(side, height));
199
+ const firstFoot = footOf(dimension.start);
200
+ const secondFoot = footOf(dimension.end);
201
+ const outward = dimension.offset < 0 ? scale2(side, -1) : side;
202
+ const extensions = [extensionLine(dimension.start, firstFoot, outward, dimension), extensionLine(dimension.end, secondFoot, outward, dimension)];
203
+ return straightLayout(dimension, { from: firstFoot, to: secondFoot, arrowAtFrom: true }, extensions);
204
+ };
205
+ const circleLayout = (dimension) => {
206
+ if (dimension.kind === Inputs.IO.dimensionKindEnum.radius) {
207
+ return straightLayout(dimension, { from: dimension.start, to: dimension.end, arrowAtFrom: false }, []);
208
+ }
209
+ const opposite = subtract2(scale2(dimension.start, 2), dimension.end);
210
+ return straightLayout(dimension, { from: opposite, to: dimension.end, arrowAtFrom: true }, []);
211
+ };
212
+ const angularLayout = (dimension) => {
213
+ const centre = dimension.start;
214
+ const radius = distance2(dimension.start, dimension.end);
215
+ const first = Math.atan2(dimension.end[1] - centre[1], dimension.end[0] - centre[0]);
216
+ const last = first + dimension.sweep;
217
+ const length = dimension.arrowLength;
218
+ const inside = radius * dimension.sweep >= ARROWS_INSIDE_ROOM * length;
219
+ const arrowTurn = length / radius;
220
+ const tailTurn = inside ? 0 : ARROW_TAIL_SHARE * length / radius;
221
+ const onArc = (turn) => add2(centre, polar2(radius, turn));
222
+ const arrowAt = (turn, bodyTurn) => {
223
+ const tip = onArc(turn);
224
+ return arrowhead(tip, unit2Of(subtract2(onArc(bodyTurn), tip)), length);
225
+ };
226
+ const fills = inside
227
+ ? [arrowAt(first, first + arrowTurn), arrowAt(last, last - arrowTurn)]
228
+ : [arrowAt(first, first - arrowTurn), arrowAt(last, last + arrowTurn)];
229
+ const arc = conicArcSamples({ frame: { center: [centre[0], centre[1], 0], xAxis: [radius, 0, 0], yAxis: [0, radius, 0] }, start: first - tailTurn, sweep: dimension.sweep + 2 * tailTurn }, dimension.textHeight * ARC_TOLERANCE_SHARE)
230
+ .map((point) => [point[0], point[1]]);
231
+ const gap = dimension.textHeight * EXTENSION_GAP_SHARE;
232
+ const overshoot = dimension.textHeight * EXTENSION_OVERSHOOT_SHARE;
233
+ const arm = (turn) => [add2(centre, polar2(radius > gap ? gap : 0, turn)), add2(centre, polar2(radius + overshoot, turn))];
234
+ const middle = first + dimension.sweep / 2;
235
+ const text = textAlong(dimension, onArc(middle), polar2(1, middle + QUARTER_TURN), polar2(1, middle));
236
+ return { lines: [arc, arm(first), arm(last)], fills, text };
237
+ };
238
+ const layoutOf = (dimension) => {
239
+ if (dimension.kind === Inputs.IO.dimensionKindEnum.linear) {
240
+ return linearLayout(dimension);
241
+ }
242
+ return dimension.kind === Inputs.IO.dimensionKindEnum.angular ? angularLayout(dimension) : circleLayout(dimension);
243
+ };
244
+ const extentOf = (layout) => {
245
+ const min = [Infinity, Infinity];
246
+ const max = [-Infinity, -Infinity];
247
+ const corners = textCornersOf(layout.text).map((corner) => [corner[0], corner[1]]);
248
+ for (const point of [...layout.lines.flat(), ...layout.fills.flat(), ...corners]) {
249
+ min[0] = Math.min(min[0], point[0]);
250
+ min[1] = Math.min(min[1], point[1]);
251
+ max[0] = Math.max(max[0], point[0]);
252
+ max[1] = Math.max(max[1], point[1]);
253
+ }
254
+ return { min, max };
255
+ };
256
+ const withoutRepeats = (strokes) => strokes
257
+ .map(stroke => stroke.filter((point, at) => at === 0 || distance2(point, stroke[at - 1]) > SAME_POINT))
258
+ .filter(stroke => stroke.length > 1);
259
+ export const dimensionExtentOf = (dimension) => extentOf(layoutOf(dimensionOf(dimension)));
260
+ export const dimensionPictureOf = (dimension) => {
261
+ const layout = layoutOf(dimensionOf(dimension));
262
+ const corners = textCornersOf(layout.text);
263
+ return {
264
+ lines: withoutRepeats(layout.lines),
265
+ fills: layout.fills,
266
+ textStrokes: withoutRepeats(textStrokesOf(layout.text).map(stroke => stroke.map((point) => [point[0], point[1]]))),
267
+ text: { text: layout.text.value, position: [layout.text.position[0], layout.text.position[1]], height: layout.text.height, anchor: layout.text.anchor, rotation: layout.text.rotationDegrees, widthFactor: layout.text.widthFactor },
268
+ textPoint: between2([corners[0][0], corners[0][1]], [corners[2][0], corners[2][1]], 1 / 2),
269
+ extent: extentOf(layout),
270
+ };
271
+ };