@bitbybit-dev/base 1.4.0 → 1.5.0-rc.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 (121) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/color-space.d.ts +14 -0
  3. package/lib/api/color-space.js +24 -0
  4. package/lib/api/index.d.ts +2 -0
  5. package/lib/api/index.js +2 -0
  6. package/lib/api/inputs/base-inputs.d.ts +382 -5
  7. package/lib/api/inputs/base-inputs.js +0 -3
  8. package/lib/api/inputs/color-inputs.d.ts +1 -1
  9. package/lib/api/inputs/color-inputs.js +0 -1
  10. package/lib/api/inputs/dates-inputs.js +0 -1
  11. package/lib/api/inputs/frame-inputs.d.ts +1 -1
  12. package/lib/api/inputs/inputs.js +0 -1
  13. package/lib/api/inputs/io-inputs.d.ts +1 -1
  14. package/lib/api/inputs/line-inputs.d.ts +1 -1
  15. package/lib/api/inputs/lists-inputs.js +0 -1
  16. package/lib/api/inputs/logic-inputs.d.ts +1 -1
  17. package/lib/api/inputs/logic-inputs.js +0 -1
  18. package/lib/api/inputs/math-inputs.js +0 -1
  19. package/lib/api/inputs/mesh-inputs.d.ts +29 -1
  20. package/lib/api/inputs/mesh-inputs.js +25 -1
  21. package/lib/api/inputs/point-inputs.d.ts +30 -1
  22. package/lib/api/inputs/point-inputs.js +28 -0
  23. package/lib/api/inputs/polyline-inputs.d.ts +1 -1
  24. package/lib/api/inputs/transforms-inputs.d.ts +1 -1
  25. package/lib/api/inputs/vector-inputs.d.ts +1 -1
  26. package/lib/api/kernel-calls/describe-failure.d.ts +3 -2
  27. package/lib/api/kernel-calls/describe-failure.js +9 -17
  28. package/lib/api/kernel-calls/failure-message.d.ts +1 -1
  29. package/lib/api/kernel-calls/hashing.d.ts +14 -0
  30. package/lib/api/kernel-calls/hashing.js +47 -0
  31. package/lib/api/kernel-calls/index.d.ts +3 -0
  32. package/lib/api/kernel-calls/index.js +3 -0
  33. package/lib/api/kernel-calls/input-rules.d.ts +1 -1
  34. package/lib/api/kernel-calls/input-rules.js +10 -5
  35. package/lib/api/kernel-calls/input-validation.d.ts +3 -3
  36. package/lib/api/kernel-calls/input-validation.js +24 -14
  37. package/lib/api/kernel-calls/kernel-steps.d.ts +19 -0
  38. package/lib/api/kernel-calls/kernel-steps.js +19 -0
  39. package/lib/api/kernel-calls/resolve-dto.d.ts +5 -3
  40. package/lib/api/kernel-calls/resolve-dto.js +9 -4
  41. package/lib/api/kernel-calls/unknown-values.d.ts +12 -0
  42. package/lib/api/kernel-calls/unknown-values.js +16 -0
  43. package/lib/api/kernel-calls/with-defaults.d.ts +1 -1
  44. package/lib/api/models/point/hex-grid-data.d.ts +1 -1
  45. package/lib/api/models/text/vector-char-data.d.ts +1 -1
  46. package/lib/api/models/text/vector-text-data.d.ts +1 -1
  47. package/lib/api/recipes/checker-types.d.ts +14 -0
  48. package/lib/api/recipes/checker-types.js +1 -0
  49. package/lib/api/recipes/index.d.ts +4 -0
  50. package/lib/api/recipes/index.js +3 -0
  51. package/lib/api/recipes/limits.constants.d.ts +23 -0
  52. package/lib/api/recipes/limits.constants.js +23 -0
  53. package/lib/api/recipes/recipe-types.d.ts +48 -0
  54. package/lib/api/recipes/recipe-types.js +1 -0
  55. package/lib/api/recipes/sources.d.ts +16 -0
  56. package/lib/api/recipes/sources.js +23 -0
  57. package/lib/api/recipes/surface-mesh.d.ts +16 -0
  58. package/lib/api/recipes/surface-mesh.js +86 -0
  59. package/lib/api/recipes/validate.d.ts +36 -0
  60. package/lib/api/recipes/validate.js +427 -0
  61. package/lib/api/resolved-inputs/index.d.ts +3 -0
  62. package/lib/api/services/color.d.ts +1 -1
  63. package/lib/api/services/frame.d.ts +3 -95
  64. package/lib/api/services/frame.js +24 -135
  65. package/lib/api/services/geometry-helper.d.ts +1 -1
  66. package/lib/api/services/helpers/arcs.d.ts +6 -0
  67. package/lib/api/services/helpers/arcs.js +30 -0
  68. package/lib/api/services/helpers/crc32.d.ts +1 -0
  69. package/lib/api/services/helpers/crc32.js +21 -0
  70. package/lib/api/services/helpers/dxf/dxf-generator.d.ts +0 -83
  71. package/lib/api/services/helpers/dxf/dxf-generator.js +5 -86
  72. package/lib/api/services/helpers/frame-axes.d.ts +9 -1
  73. package/lib/api/services/helpers/frame-axes.js +34 -0
  74. package/lib/api/services/helpers/helper-types.d.ts +34 -0
  75. package/lib/api/services/helpers/helper-types.js +1 -0
  76. package/lib/api/services/helpers/inflate.constants.d.ts +41 -0
  77. package/lib/api/services/helpers/inflate.constants.js +41 -0
  78. package/lib/api/services/helpers/inflate.d.ts +3 -0
  79. package/lib/api/services/helpers/inflate.js +228 -0
  80. package/lib/api/services/helpers/matrices.constants.d.ts +2 -0
  81. package/lib/api/services/helpers/matrices.constants.js +2 -0
  82. package/lib/api/services/helpers/matrices.d.ts +9 -1
  83. package/lib/api/services/helpers/matrices.js +18 -0
  84. package/lib/api/services/helpers/mesh-measures.d.ts +1 -0
  85. package/lib/api/services/helpers/mesh-measures.js +28 -0
  86. package/lib/api/services/helpers/mesh-normals.d.ts +2 -0
  87. package/lib/api/services/helpers/mesh-normals.js +118 -26
  88. package/lib/api/services/helpers/mesh-primitives.constants.d.ts +2 -0
  89. package/lib/api/services/helpers/mesh-primitives.constants.js +12 -0
  90. package/lib/api/services/helpers/mesh-primitives.d.ts +3 -0
  91. package/lib/api/services/helpers/mesh-primitives.js +46 -0
  92. package/lib/api/services/helpers/nurbs.d.ts +7 -0
  93. package/lib/api/services/helpers/nurbs.js +50 -0
  94. package/lib/api/services/helpers/paths.d.ts +8 -0
  95. package/lib/api/services/helpers/paths.js +96 -0
  96. package/lib/api/services/helpers/polygons.d.ts +8 -0
  97. package/lib/api/services/helpers/polygons.js +59 -0
  98. package/lib/api/services/helpers/surface-triangulation.d.ts +6 -0
  99. package/lib/api/services/helpers/surface-triangulation.js +298 -0
  100. package/lib/api/services/helpers/triangulation.d.ts +2 -0
  101. package/lib/api/services/helpers/triangulation.js +295 -0
  102. package/lib/api/services/helpers/vectors.d.ts +18 -0
  103. package/lib/api/services/helpers/vectors.js +17 -0
  104. package/lib/api/services/helpers/zip.constants.d.ts +34 -0
  105. package/lib/api/services/helpers/zip.constants.js +34 -0
  106. package/lib/api/services/helpers/zip.d.ts +4 -0
  107. package/lib/api/services/helpers/zip.js +89 -0
  108. package/lib/api/services/line.d.ts +3 -3
  109. package/lib/api/services/mesh.d.ts +37 -2
  110. package/lib/api/services/mesh.js +60 -3
  111. package/lib/api/services/point.d.ts +22 -10
  112. package/lib/api/services/point.js +42 -13
  113. package/lib/api/services/polyline.d.ts +39 -4
  114. package/lib/api/services/polyline.js +56 -6
  115. package/lib/api/services/text.d.ts +2 -2
  116. package/lib/api/services/text.js +10 -5
  117. package/lib/api/services/transforms.d.ts +3 -9
  118. package/lib/api/services/transforms.js +0 -6
  119. package/lib/api/services/vector.d.ts +2 -2
  120. package/lib/api/services/vector.js +2 -5
  121. package/package.json +1 -1
@@ -0,0 +1,34 @@
1
+ export const CRC32_POLYNOMIAL = 0xedb88320;
2
+ export const CRC32_ALL_ONES = 0xffffffff;
3
+ export const BYTE_MASK = 0xff;
4
+ export const BYTE_BITS = 8;
5
+ export const BYTE_VALUES = 256;
6
+ export const LOCAL_HEADER_SIGNATURE = 0x04034b50;
7
+ export const CENTRAL_HEADER_SIGNATURE = 0x02014b50;
8
+ export const END_OF_CENTRAL_SIGNATURE = 0x06054b50;
9
+ export const END_OF_CENTRAL_SIZE = 22;
10
+ export const LONGEST_ARCHIVE_COMMENT = 0xffff;
11
+ export const ZIP64_COUNT = 0xffff;
12
+ export const ZIP64_SIZE = 0xffffffff;
13
+ export const END_ENTRY_COUNT = 10;
14
+ export const END_DIRECTORY_SIZE = 12;
15
+ export const END_DIRECTORY_OFFSET = 16;
16
+ export const CENTRAL_FLAGS = 8;
17
+ export const CENTRAL_METHOD = 10;
18
+ export const CENTRAL_CRC32 = 16;
19
+ export const CENTRAL_COMPRESSED_SIZE = 20;
20
+ export const CENTRAL_SIZE = 24;
21
+ export const CENTRAL_NAME_LENGTH = 28;
22
+ export const CENTRAL_EXTRA_LENGTH = 30;
23
+ export const CENTRAL_COMMENT_LENGTH = 32;
24
+ export const CENTRAL_LOCAL_OFFSET = 42;
25
+ export const CENTRAL_HEADER_SIZE = 46;
26
+ export const LOCAL_NAME_LENGTH = 26;
27
+ export const LOCAL_EXTRA_LENGTH = 28;
28
+ export const LOCAL_HEADER_SIZE = 30;
29
+ export const ENCRYPTED_FLAG = 1;
30
+ export const UTF8_NAME_FLAG = 0x800;
31
+ export const STORED_METHOD = 0;
32
+ export const DEFLATED_METHOD = 8;
33
+ export const UINT16_BYTES = 2;
34
+ export const UINT32_BYTES = 4;
@@ -0,0 +1,4 @@
1
+ import type { ZipEntry } from "./helper-types";
2
+ export declare function isZip(bytes: Uint8Array): boolean;
3
+ export declare function zipEntries(bytes: Uint8Array): ZipEntry[];
4
+ export declare function zipEntryData(bytes: Uint8Array, entry: ZipEntry): Uint8Array;
@@ -0,0 +1,89 @@
1
+ import { crc32 } from "./crc32";
2
+ import { inflateRaw } from "./inflate";
3
+ import { BYTE_BITS, CENTRAL_COMMENT_LENGTH, CENTRAL_COMPRESSED_SIZE, CENTRAL_CRC32, CENTRAL_EXTRA_LENGTH, CENTRAL_FLAGS, CENTRAL_HEADER_SIGNATURE, CENTRAL_HEADER_SIZE, CENTRAL_LOCAL_OFFSET, CENTRAL_METHOD, CENTRAL_NAME_LENGTH, CENTRAL_SIZE, DEFLATED_METHOD, ENCRYPTED_FLAG, END_DIRECTORY_OFFSET, END_DIRECTORY_SIZE, END_ENTRY_COUNT, END_OF_CENTRAL_SIGNATURE, END_OF_CENTRAL_SIZE, LOCAL_EXTRA_LENGTH, LOCAL_HEADER_SIGNATURE, LOCAL_HEADER_SIZE, LOCAL_NAME_LENGTH, LONGEST_ARCHIVE_COMMENT, STORED_METHOD, UINT16_BYTES, UINT32_BYTES, UTF8_NAME_FLAG, ZIP64_COUNT, ZIP64_SIZE, } from "./zip.constants";
4
+ const utf8 = new TextDecoder("utf-8");
5
+ const windows1252 = new TextDecoder("windows-1252");
6
+ function uint16(bytes, at) {
7
+ if (at + UINT16_BYTES > bytes.length) {
8
+ throw new Error("The zip archive ends in the middle of a header");
9
+ }
10
+ return bytes[at] | (bytes[at + 1] << BYTE_BITS);
11
+ }
12
+ function uint32(bytes, at) {
13
+ if (at + UINT32_BYTES > bytes.length) {
14
+ throw new Error("The zip archive ends in the middle of a header");
15
+ }
16
+ return (uint16(bytes, at) | (uint16(bytes, at + UINT16_BYTES) << (BYTE_BITS * UINT16_BYTES))) >>> 0;
17
+ }
18
+ export function isZip(bytes) {
19
+ return bytes.length >= UINT32_BYTES && uint32(bytes, 0) === LOCAL_HEADER_SIGNATURE;
20
+ }
21
+ function endOfCentralDirectory(bytes) {
22
+ const last = bytes.length - END_OF_CENTRAL_SIZE;
23
+ for (let at = last; at >= Math.max(0, last - LONGEST_ARCHIVE_COMMENT); at--) {
24
+ if (uint32(bytes, at) === END_OF_CENTRAL_SIGNATURE) {
25
+ return at;
26
+ }
27
+ }
28
+ throw new Error("The zip archive has no end of central directory record");
29
+ }
30
+ export function zipEntries(bytes) {
31
+ const end = endOfCentralDirectory(bytes);
32
+ const count = uint16(bytes, end + END_ENTRY_COUNT);
33
+ const directorySize = uint32(bytes, end + END_DIRECTORY_SIZE);
34
+ const directory = uint32(bytes, end + END_DIRECTORY_OFFSET);
35
+ if (count === ZIP64_COUNT || directorySize === ZIP64_SIZE || directory === ZIP64_SIZE) {
36
+ throw new Error("The zip archive is a ZIP64 archive, which is not read");
37
+ }
38
+ const entries = [];
39
+ let at = directory;
40
+ for (let index = 0; index < count; index++) {
41
+ if (uint32(bytes, at) !== CENTRAL_HEADER_SIGNATURE) {
42
+ throw new Error(`The zip archive's central directory is corrupt at entry ${index}`);
43
+ }
44
+ const flags = uint16(bytes, at + CENTRAL_FLAGS);
45
+ const nameLength = uint16(bytes, at + CENTRAL_NAME_LENGTH);
46
+ const nameBytes = bytes.subarray(at + CENTRAL_HEADER_SIZE, at + CENTRAL_HEADER_SIZE + nameLength);
47
+ entries.push({
48
+ name: (flags & UTF8_NAME_FLAG ? utf8 : windows1252).decode(nameBytes),
49
+ method: uint16(bytes, at + CENTRAL_METHOD),
50
+ crc32: uint32(bytes, at + CENTRAL_CRC32),
51
+ compressedSize: uint32(bytes, at + CENTRAL_COMPRESSED_SIZE),
52
+ size: uint32(bytes, at + CENTRAL_SIZE),
53
+ localOffset: uint32(bytes, at + CENTRAL_LOCAL_OFFSET),
54
+ encrypted: (flags & ENCRYPTED_FLAG) !== 0,
55
+ });
56
+ at += CENTRAL_HEADER_SIZE + nameLength + uint16(bytes, at + CENTRAL_EXTRA_LENGTH) + uint16(bytes, at + CENTRAL_COMMENT_LENGTH);
57
+ }
58
+ return entries;
59
+ }
60
+ export function zipEntryData(bytes, entry) {
61
+ if (entry.encrypted) {
62
+ throw new Error(`${entry.name} in the zip archive is encrypted`);
63
+ }
64
+ if (uint32(bytes, entry.localOffset) !== LOCAL_HEADER_SIGNATURE) {
65
+ throw new Error(`The zip archive's local header of ${entry.name} is missing`);
66
+ }
67
+ const start = entry.localOffset + LOCAL_HEADER_SIZE + uint16(bytes, entry.localOffset + LOCAL_NAME_LENGTH) + uint16(bytes, entry.localOffset + LOCAL_EXTRA_LENGTH);
68
+ if (start + entry.compressedSize > bytes.length) {
69
+ throw new Error(`${entry.name} runs past the end of the zip archive`);
70
+ }
71
+ const stored = bytes.subarray(start, start + entry.compressedSize);
72
+ let data;
73
+ if (entry.method === STORED_METHOD) {
74
+ data = stored;
75
+ }
76
+ else if (entry.method === DEFLATED_METHOD) {
77
+ data = inflateRaw(stored, entry.size);
78
+ }
79
+ else {
80
+ throw new Error(`${entry.name} is compressed with zip method ${entry.method}; only stored and deflated entries are read`);
81
+ }
82
+ if (data.length !== entry.size) {
83
+ throw new Error(`${entry.name} holds ${data.length} bytes, not the ${entry.size} its zip archive records`);
84
+ }
85
+ if (crc32(data) !== entry.crc32) {
86
+ throw new Error(`${entry.name} fails its CRC-32 check`);
87
+ }
88
+ return data;
89
+ }
@@ -1,7 +1,7 @@
1
- import { GeometryHelper } from "./geometry-helper";
1
+ import type { GeometryHelper } from "./geometry-helper";
2
2
  import * as Inputs from "../inputs";
3
- import { Point } from "./point";
4
- import { Vector } from "./vector";
3
+ import type { Point } from "./point";
4
+ import type { Vector } from "./vector";
5
5
  /**
6
6
  * Straight lines between two points, held as plain objects of the form `{ start, end }`, and the
7
7
  * segment form `[start, end]` of the same thing. The methods here build lines, measure and
@@ -1,6 +1,6 @@
1
1
  import * as Inputs from "../inputs";
2
- import { Polyline } from "./polyline";
3
- import { Vector } from "./vector";
2
+ import type { Polyline } from "./polyline";
3
+ import type { Vector } from "./vector";
4
4
  /**
5
5
  * Geometry on plain triangle meshes: a mesh is a list of triangles, each three points. The methods
6
6
  * here work out the plane of a triangle, the distance from a point to a plane, and where two meshes
@@ -117,4 +117,39 @@ export declare class MeshBitByBit {
117
117
  */
118
118
  meshMeshIntersectionPoints(inputs: Inputs.Mesh.MeshMeshToleranceDto): Inputs.Base.Point3[][];
119
119
  private computeIntersectionPoint;
120
+ /**
121
+ * Cuts a flat polygon, with any holes through it, into triangles.
122
+ *
123
+ * Every corner stays a corner of a triangle, so neighbouring polygons give a mesh closed along
124
+ * the edges they share. The triangles face the way the outline turns, by the right-hand rule;
125
+ * holes may run either way.
126
+ * Example: a 4 by 4 square -> two triangles
127
+ * @param inputs - The outline and its holes
128
+ * @returns The triangles, each three points
129
+ * @group polygon
130
+ * @shortname triangulate polygon
131
+ * @drawable false
132
+ * @example
133
+ * ```typescript
134
+ * const triangles = bitbybit.mesh.triangulatePolygon({ points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]], holes: [[[1, 1, 0], [3, 1, 0], [3, 3, 0], [1, 3, 0]]] });
135
+ * ```
136
+ */
137
+ triangulatePolygon(inputs: Inputs.Mesh.PolygonWithHolesDto): Inputs.Base.Mesh3;
138
+ /**
139
+ * Measures the volume a closed triangle mesh encloses, with a sign: positive when its triangles
140
+ * face outwards by the right-hand rule, negative when they all face inwards.
141
+ *
142
+ * A mesh that is not closed has no inside, and the number it gives means nothing.
143
+ * Example: a unit cube facing outwards -> 1
144
+ * @param inputs - The mesh
145
+ * @returns The enclosed volume in cubic model units
146
+ * @group measure
147
+ * @shortname signed volume
148
+ * @drawable false
149
+ * @example
150
+ * ```typescript
151
+ * const volume = bitbybit.mesh.signedVolume({ mesh: [[[0, 0, 0], [0, 1, 0], [1, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 1]], [[0, 0, 0], [0, 0, 1], [0, 1, 0]], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]] });
152
+ * ```
153
+ */
154
+ signedVolume(inputs: Inputs.Mesh.MeshDto): number;
120
155
  }
@@ -1,5 +1,8 @@
1
1
  import * as Inputs from "../inputs";
2
2
  import { resolveDto } from "../kernel-calls";
3
+ import { signedVolumeOf } from "./helpers/mesh-measures";
4
+ import { triangulateFace } from "./helpers/triangulation";
5
+ const TRIANGLE_CORNERS = 3;
3
6
  /**
4
7
  * Geometry on plain triangle meshes: a mesh is a list of triangles, each three points. The methods
5
8
  * here work out the plane of a triangle, the distance from a point to a plane, and where two meshes
@@ -88,8 +91,9 @@ export class MeshBitByBit {
88
91
  const q1 = t2[0], q2 = t2[1], q3 = t2[2];
89
92
  const plane1 = this.calculateTrianglePlane({ triangle: t1, tolerance: EPSILON });
90
93
  const plane2 = this.calculateTrianglePlane({ triangle: t2, tolerance: EPSILON });
91
- if (!plane1 || !plane2)
94
+ if (!plane1 || !plane2) {
92
95
  return undefined;
96
+ }
93
97
  const distQ_Plane1 = [
94
98
  this.signedDistanceToPlane({ point: q1, plane: plane1 }),
95
99
  this.signedDistanceToPlane({ point: q2, plane: plane1 }),
@@ -127,8 +131,9 @@ export class MeshBitByBit {
127
131
  const v = edges1[i][1];
128
132
  const du = dists1[i];
129
133
  const dv = dists1[(i + 1) % 3];
130
- if (Math.abs(du) < EPSILON)
134
+ if (Math.abs(du) < EPSILON) {
131
135
  t1_intersection_points_3d.push(u);
136
+ }
132
137
  if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) {
133
138
  const t = du / (du - dv);
134
139
  t1_intersection_points_3d.push(this.computeIntersectionPoint(u, v, t));
@@ -141,8 +146,9 @@ export class MeshBitByBit {
141
146
  const v = edges2[i][1];
142
147
  const du = dists2[i];
143
148
  const dv = dists2[(i + 1) % 3];
144
- if (Math.abs(du) < EPSILON)
149
+ if (Math.abs(du) < EPSILON) {
145
150
  t2_intersection_points_3d.push(u);
151
+ }
146
152
  if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) {
147
153
  const t = du / (du - dv);
148
154
  t2_intersection_points_3d.push(this.computeIntersectionPoint(u, v, t));
@@ -274,4 +280,55 @@ export class MeshBitByBit {
274
280
  })
275
281
  });
276
282
  }
283
+ /**
284
+ * Cuts a flat polygon, with any holes through it, into triangles.
285
+ *
286
+ * Every corner stays a corner of a triangle, so neighbouring polygons give a mesh closed along
287
+ * the edges they share. The triangles face the way the outline turns, by the right-hand rule;
288
+ * holes may run either way.
289
+ * Example: a 4 by 4 square -> two triangles
290
+ * @param inputs - The outline and its holes
291
+ * @returns The triangles, each three points
292
+ * @group polygon
293
+ * @shortname triangulate polygon
294
+ * @drawable false
295
+ * @example
296
+ * ```typescript
297
+ * const triangles = bitbybit.mesh.triangulatePolygon({ points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]], holes: [[[1, 1, 0], [3, 1, 0], [3, 3, 0], [1, 3, 0]]] });
298
+ * ```
299
+ */
300
+ triangulatePolygon(inputs) {
301
+ var _a;
302
+ const loops = [inputs.points, ...((_a = inputs.holes) !== null && _a !== void 0 ? _a : [])];
303
+ const corners = loops.flat();
304
+ const positions = corners.flat();
305
+ let next = 0;
306
+ const indices = loops.map((loop) => loop.map(() => next++));
307
+ const triangles = triangulateFace(positions, indices);
308
+ const mesh = [];
309
+ for (let at = 0; at + 2 < triangles.length; at += TRIANGLE_CORNERS) {
310
+ mesh.push([[...corners[triangles[at]]], [...corners[triangles[at + 1]]], [...corners[triangles[at + 2]]]]);
311
+ }
312
+ return mesh;
313
+ }
314
+ /**
315
+ * Measures the volume a closed triangle mesh encloses, with a sign: positive when its triangles
316
+ * face outwards by the right-hand rule, negative when they all face inwards.
317
+ *
318
+ * A mesh that is not closed has no inside, and the number it gives means nothing.
319
+ * Example: a unit cube facing outwards -> 1
320
+ * @param inputs - The mesh
321
+ * @returns The enclosed volume in cubic model units
322
+ * @group measure
323
+ * @shortname signed volume
324
+ * @drawable false
325
+ * @example
326
+ * ```typescript
327
+ * const volume = bitbybit.mesh.signedVolume({ mesh: [[[0, 0, 0], [0, 1, 0], [1, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 1]], [[0, 0, 0], [0, 0, 1], [0, 1, 0]], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]] });
328
+ * ```
329
+ */
330
+ signedVolume(inputs) {
331
+ const positions = inputs.mesh.flat(2);
332
+ return signedVolumeOf(positions, Array.from({ length: positions.length / TRIANGLE_CORNERS }, (_, at) => at));
333
+ }
277
334
  }
@@ -1,9 +1,9 @@
1
- import { GeometryHelper } from "./geometry-helper";
1
+ import type { GeometryHelper } from "./geometry-helper";
2
2
  import * as Inputs from "../inputs";
3
- import { Transforms } from "./transforms";
4
- import { Vector } from "./vector";
5
- import * as Models from "../models";
6
- import { Lists } from "./lists";
3
+ import type { Transforms } from "./transforms";
4
+ import type { Vector } from "./vector";
5
+ import type * as Models from "../models";
6
+ import type { Lists } from "./lists";
7
7
  /**
8
8
  * Points as plain number arrays. A point is `[x, y, z]` with Y pointing up, the same shape as a
9
9
  * vector, so the two can be passed to each other's methods; a 2D point is `[x, y]`. Every method
@@ -578,11 +578,23 @@ export declare class Point {
578
578
  * ```
579
579
  */
580
580
  sortPoints(inputs: Inputs.Point.PointsDto): Inputs.Base.Point3[];
581
+ private getRegularHexagonVertices;
581
582
  /**
582
- * Calculates the 6 vertices of a regular flat-top hexagon.
583
- * @param center The center point [x, y, z].
584
- * @param radius The radius (distance from center to vertex).
585
- * @returns An array of 6 Point3 vertices in counter-clockwise order.
583
+ * Lays out points along the circular arc through three points, from the first through the
584
+ * second to the third.
585
+ *
586
+ * The arc is divided into `segments` equal steps between the given ends. Points on one line fix
587
+ * no arc and come back as they are.
588
+ * Example: [1,0,0], [0,1,0] and [-1,0,0] in 2 segments -> [[1,0,0], [0,1,0], [-1,0,0]]
589
+ * @param inputs - The three points and the number of steps
590
+ * @returns The points along the arc, its ends included
591
+ * @group create
592
+ * @shortname arc 3 points
593
+ * @drawable true
594
+ * @example
595
+ * ```typescript
596
+ * const points = bitbybit.point.arcThroughThreePoints({ start: [5, 0, 0], middle: [0, 5, 0], end: [-5, 0, 0], segments: 16 });
597
+ * ```
586
598
  */
587
- private getRegularHexagonVertices;
599
+ arcThroughThreePoints(inputs: Inputs.Point.ArcThroughThreePointsDto): Inputs.Base.Point3[];
588
600
  }
@@ -1,5 +1,6 @@
1
1
  import * as Inputs from "../inputs";
2
- import { resolveDto } from "../kernel-calls";
2
+ import { InputError, resolveDto } from "../kernel-calls";
3
+ import { arcThroughThreePoints, pointsOnArc } from "./helpers/arcs";
3
4
  /**
4
5
  * Points as plain number arrays. A point is `[x, y, z]` with Y pointing up, the same shape as a
5
6
  * vector, so the two can be passed to each other's methods; a 2D point is `[x, y]`. Every method
@@ -621,14 +622,18 @@ export class Point {
621
622
  let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
622
623
  for (const hex of unscaledHexagons) {
623
624
  for (const vertex of hex) {
624
- if (vertex[0] < minX)
625
+ if (vertex[0] < minX) {
625
626
  minX = vertex[0];
626
- if (vertex[0] > maxX)
627
+ }
628
+ if (vertex[0] > maxX) {
627
629
  maxX = vertex[0];
628
- if (vertex[1] < minY)
630
+ }
631
+ if (vertex[1] < minY) {
629
632
  minY = vertex[1];
630
- if (vertex[1] > maxY)
633
+ }
634
+ if (vertex[1] > maxY) {
631
635
  maxY = vertex[1];
636
+ }
632
637
  }
633
638
  }
634
639
  const unscaledWidth = maxX - minX;
@@ -1147,19 +1152,15 @@ export class Point {
1147
1152
  */
1148
1153
  sortPoints(inputs) {
1149
1154
  return [...inputs.points].sort((a, b) => {
1150
- if (a[0] !== b[0])
1155
+ if (a[0] !== b[0]) {
1151
1156
  return a[0] - b[0];
1152
- if (a[1] !== b[1])
1157
+ }
1158
+ if (a[1] !== b[1]) {
1153
1159
  return a[1] - b[1];
1160
+ }
1154
1161
  return a[2] - b[2];
1155
1162
  });
1156
1163
  }
1157
- /**
1158
- * Calculates the 6 vertices of a regular flat-top hexagon.
1159
- * @param center The center point [x, y, z].
1160
- * @param radius The radius (distance from center to vertex).
1161
- * @returns An array of 6 Point3 vertices in counter-clockwise order.
1162
- */
1163
1164
  getRegularHexagonVertices(center, radius) {
1164
1165
  const vertices = [];
1165
1166
  const cx = center[0];
@@ -1176,4 +1177,32 @@ export class Point {
1176
1177
  }
1177
1178
  return vertices;
1178
1179
  }
1180
+ /**
1181
+ * Lays out points along the circular arc through three points, from the first through the
1182
+ * second to the third.
1183
+ *
1184
+ * The arc is divided into `segments` equal steps between the given ends. Points on one line fix
1185
+ * no arc and come back as they are.
1186
+ * Example: [1,0,0], [0,1,0] and [-1,0,0] in 2 segments -> [[1,0,0], [0,1,0], [-1,0,0]]
1187
+ * @param inputs - The three points and the number of steps
1188
+ * @returns The points along the arc, its ends included
1189
+ * @group create
1190
+ * @shortname arc 3 points
1191
+ * @drawable true
1192
+ * @example
1193
+ * ```typescript
1194
+ * const points = bitbybit.point.arcThroughThreePoints({ start: [5, 0, 0], middle: [0, 5, 0], end: [-5, 0, 0], segments: 16 });
1195
+ * ```
1196
+ */
1197
+ arcThroughThreePoints(inputs) {
1198
+ const resolved = resolveDto(Inputs.Point.ArcThroughThreePointsDto, inputs);
1199
+ if (!Number.isInteger(resolved.segments) || resolved.segments < 1) {
1200
+ throw new InputError(`\`segments\` must be a whole number of at least 1; it is ${String(resolved.segments)}.`, "segments");
1201
+ }
1202
+ const start = [...resolved.start];
1203
+ const middle = [...resolved.middle];
1204
+ const end = [...resolved.end];
1205
+ const arc = arcThroughThreePoints(start, middle, end);
1206
+ return arc ? pointsOnArc(arc, start, end, resolved.segments) : [start, middle, end];
1207
+ }
1179
1208
  }
@@ -1,8 +1,8 @@
1
- import { GeometryHelper } from "./geometry-helper";
1
+ import type { GeometryHelper } from "./geometry-helper";
2
2
  import * as Inputs from "../inputs";
3
- import { Point } from "./point";
4
- import { Vector } from "./vector";
5
- import { Line } from "./line";
3
+ import type { Point } from "./point";
4
+ import type { Vector } from "./vector";
5
+ import type { Line } from "./line";
6
6
  /**
7
7
  * Polylines: chains of straight segments through a list of points, held as plain objects of the
8
8
  * form `{ points, isClosed }`. A closed polyline joins its last point back to its first. The
@@ -237,4 +237,39 @@ export declare class Polyline {
237
237
  * ```
238
238
  */
239
239
  safestFilletRadius(inputs: Inputs.Polyline.PolylineToleranceDto): number;
240
+ /**
241
+ * Finds the unit normal of the plane a polyline encloses, read as a closed outline.
242
+ *
243
+ * Newell's method makes it hold for an outline that is not convex or not quite flat. It points
244
+ * the way the outline turns by the right-hand rule; an outline with no area gives undefined.
245
+ * Example: [[0,0,0], [4,0,0], [4,4,0], [0,4,0]] -> [0, 0, 1]
246
+ * @param inputs - The polyline
247
+ * @returns The unit normal, or undefined
248
+ * @group polygon
249
+ * @shortname polygon normal
250
+ * @drawable false
251
+ * @example
252
+ * ```typescript
253
+ * const normal = bitbybit.polyline.polygonNormal({ polyline: { points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]] } });
254
+ * ```
255
+ */
256
+ polygonNormal(inputs: Inputs.Polyline.PolylineDto): Inputs.Base.Vector3 | undefined;
257
+ /**
258
+ * Measures the area a polyline encloses, read as a closed outline in its own plane.
259
+ *
260
+ * The polyline need not repeat its first point at its end. An outline that is not quite flat
261
+ * gives the area of its outline seen along its normal.
262
+ * Example: [[0,0,0], [4,0,0], [4,4,0], [0,4,0]] -> 16
263
+ * @param inputs - The polyline
264
+ * @returns The area in square model units
265
+ * @group polygon
266
+ * @shortname polygon area
267
+ * @drawable false
268
+ * @example
269
+ * ```typescript
270
+ * const area = bitbybit.polyline.polygonArea({ polyline: { points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]] } });
271
+ * ```
272
+ */
273
+ polygonArea(inputs: Inputs.Polyline.PolylineDto): number;
274
+ private newellOf;
240
275
  }
@@ -1,5 +1,7 @@
1
1
  import * as Inputs from "../inputs";
2
2
  import { resolveDto } from "../kernel-calls";
3
+ import { unitOf } from "./helpers/frame-axes";
4
+ import { newellNormal } from "./helpers/triangulation";
3
5
  /**
4
6
  * Polylines: chains of straight segments through a list of points, held as plain objects of the
5
7
  * form `{ points, isClosed }`. A closed polyline joins its last point back to its first. The
@@ -348,12 +350,14 @@ export class Polyline {
348
350
  const key1 = getGridKey(segment[1]);
349
351
  const info0 = { segmentIndex: i, endpointIndex: 0, coords: segment[0] };
350
352
  const info1 = { segmentIndex: i, endpointIndex: 1, coords: segment[1] };
351
- if (!endpointMap.has(key0))
353
+ if (!endpointMap.has(key0)) {
352
354
  endpointMap.set(key0, []);
355
+ }
353
356
  endpointMap.get(key0).push(info0);
354
357
  if (key1 !== key0) {
355
- if (!endpointMap.has(key1))
358
+ if (!endpointMap.has(key1)) {
356
359
  endpointMap.set(key1, []);
360
+ }
357
361
  endpointMap.get(key1).push(info1);
358
362
  }
359
363
  else {
@@ -376,8 +380,9 @@ export class Polyline {
376
380
  let minDistanceSq = toleranceSq;
377
381
  for (const searchKey of searchKeys) {
378
382
  const candidates = endpointMap.get(searchKey);
379
- if (!candidates)
383
+ if (!candidates) {
380
384
  continue;
385
+ }
381
386
  for (const candidate of candidates) {
382
387
  if (!used[candidate.segmentIndex]) {
383
388
  const diffVector = this.vector.sub({ first: candidate.coords, second: pointToMatch });
@@ -397,8 +402,9 @@ export class Polyline {
397
402
  return undefined;
398
403
  };
399
404
  for (let i = 0; i < numSegments; i++) {
400
- if (used[i])
405
+ if (used[i]) {
401
406
  continue;
407
+ }
402
408
  used[i] = true;
403
409
  const startSegment = segments[i];
404
410
  const currentPoints = [startSegment[0], startSegment[1]];
@@ -408,8 +414,9 @@ export class Polyline {
408
414
  let iterations = 0;
409
415
  while (iterations++ < numSegments) {
410
416
  const nextMatch = findConnection(currentTail);
411
- if (!nextMatch)
417
+ if (!nextMatch) {
412
418
  break;
419
+ }
413
420
  const nextSegment = segments[nextMatch.segmentIndex];
414
421
  const pointToAdd = (nextMatch.endpointIndex === 0) ? nextSegment[1] : nextSegment[0];
415
422
  if (this.point.twoPointsAlmostEqual({ point1: pointToAdd, point2: currentHead, tolerance: tolerance })) {
@@ -425,8 +432,9 @@ export class Polyline {
425
432
  if (!isClosed) {
426
433
  while (iterations++ < numSegments) {
427
434
  const prevMatch = findConnection(currentHead);
428
- if (!prevMatch)
435
+ if (!prevMatch) {
429
436
  break;
437
+ }
430
438
  const prevSegment = segments[prevMatch.segmentIndex];
431
439
  const pointToAdd = (prevMatch.endpointIndex === 0) ? prevSegment[1] : prevSegment[0];
432
440
  if (this.point.twoPointsAlmostEqual({ point1: pointToAdd, point2: currentTail, tolerance: tolerance })) {
@@ -508,4 +516,46 @@ export class Polyline {
508
516
  const safestRadius = Math.min(...allMaxRadii);
509
517
  return Math.max(0, safestRadius);
510
518
  }
519
+ /**
520
+ * Finds the unit normal of the plane a polyline encloses, read as a closed outline.
521
+ *
522
+ * Newell's method makes it hold for an outline that is not convex or not quite flat. It points
523
+ * the way the outline turns by the right-hand rule; an outline with no area gives undefined.
524
+ * Example: [[0,0,0], [4,0,0], [4,4,0], [0,4,0]] -> [0, 0, 1]
525
+ * @param inputs - The polyline
526
+ * @returns The unit normal, or undefined
527
+ * @group polygon
528
+ * @shortname polygon normal
529
+ * @drawable false
530
+ * @example
531
+ * ```typescript
532
+ * const normal = bitbybit.polyline.polygonNormal({ polyline: { points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]] } });
533
+ * ```
534
+ */
535
+ polygonNormal(inputs) {
536
+ return unitOf(this.newellOf(inputs.polyline.points));
537
+ }
538
+ /**
539
+ * Measures the area a polyline encloses, read as a closed outline in its own plane.
540
+ *
541
+ * The polyline need not repeat its first point at its end. An outline that is not quite flat
542
+ * gives the area of its outline seen along its normal.
543
+ * Example: [[0,0,0], [4,0,0], [4,4,0], [0,4,0]] -> 16
544
+ * @param inputs - The polyline
545
+ * @returns The area in square model units
546
+ * @group polygon
547
+ * @shortname polygon area
548
+ * @drawable false
549
+ * @example
550
+ * ```typescript
551
+ * const area = bitbybit.polyline.polygonArea({ polyline: { points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]] } });
552
+ * ```
553
+ */
554
+ polygonArea(inputs) {
555
+ const [x, y, z] = this.newellOf(inputs.polyline.points);
556
+ return Math.hypot(x, y, z) / 2;
557
+ }
558
+ newellOf(points) {
559
+ return newellNormal(points.flat(), points.map((_, at) => at));
560
+ }
511
561
  }
@@ -1,6 +1,6 @@
1
1
  import * as Inputs from "../inputs";
2
- import * as Models from "../models";
3
- import { Point } from "./point";
2
+ import type * as Models from "../models";
3
+ import type { Point } from "./point";
4
4
  /**
5
5
  * Working with text: splitting, joining, searching, trimming, padding, changing case, regular
6
6
  * expressions and formatting with placeholders. `vectorChar` and `vectorText` turn text into line
@@ -395,8 +395,9 @@ export class TextBitByBit {
395
395
  */
396
396
  toUpperCaseFirst(inputs) {
397
397
  const resolved = resolveDto(Inputs.Text.TextDto, inputs);
398
- if (!resolved.text)
398
+ if (!resolved.text) {
399
399
  return resolved.text;
400
+ }
400
401
  return resolved.text.charAt(0).toUpperCase() + resolved.text.slice(1);
401
402
  }
402
403
  /**
@@ -411,8 +412,9 @@ export class TextBitByBit {
411
412
  */
412
413
  toLowerCaseFirst(inputs) {
413
414
  const resolved = resolveDto(Inputs.Text.TextDto, inputs);
414
- if (!resolved.text)
415
+ if (!resolved.text) {
415
416
  return resolved.text;
417
+ }
416
418
  return resolved.text.charAt(0).toLowerCase() + resolved.text.slice(1);
417
419
  }
418
420
  /**
@@ -666,16 +668,18 @@ export class TextBitByBit {
666
668
  const resolved = resolveDto(Inputs.Text.VectorTextDto, inputs);
667
669
  const { xOffset, yOffset, height, align, extrudeOffset, lineSpacing, letterSpacing } = Object.assign({}, defaultsVectorParams, resolved);
668
670
  const text = resolved.text;
669
- if (typeof text !== "string")
671
+ if (typeof text !== "string") {
670
672
  throw new Error("text must be a string");
673
+ }
671
674
  const extraLetterSpacing = (height * letterSpacing);
672
675
  let maxWidth = 0;
673
676
  let line = { width: 0, height: 0, chars: [] };
674
677
  let lines = [];
675
678
  const pushLine = () => {
676
679
  maxWidth = Math.max(maxWidth, line.width);
677
- if (line.chars.length)
680
+ if (line.chars.length) {
678
681
  lines.push(line);
682
+ }
679
683
  line = { width: 0, height: 0, chars: [] };
680
684
  };
681
685
  let x = xOffset;
@@ -699,8 +703,9 @@ export class TextBitByBit {
699
703
  line.chars = line.chars.concat(vchar);
700
704
  }
701
705
  }
702
- if (line.chars.length)
706
+ if (line.chars.length) {
703
707
  pushLine();
708
+ }
704
709
  lines = lines.map((line) => {
705
710
  const diff = maxWidth - line.width;
706
711
  if (align === Inputs.Base.horizontalAlignEnum.right) {