@bitbybit-dev/base 1.4.1 → 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,23 @@
1
+ export const RECIPE_FORMAT = "bitbybit.recipe";
2
+ export const RECIPE_VERSION = 1;
3
+ export const MAX_RECIPE_NODES = 10000000;
4
+ export const MAX_RECIPE_ROOTS = 10000000;
5
+ export const MAX_RECIPE_GROUPS = 10000000;
6
+ export const MAX_RECIPE_DEPTH = 1000;
7
+ export const MAX_BUFFER_LENGTH = 1000000000;
8
+ export const MAX_REFERENCED_NUMBERS = 1000000000;
9
+ export const MAX_RECIPE_COORDINATE = 1e9;
10
+ export const MAX_RECIPE_PLACEMENT = 1e13;
11
+ export const MATRIX_SIZE = 16;
12
+ export const POINT2_SIZE = 2;
13
+ export const POINT3_SIZE = 3;
14
+ export const MIN_POLYGON_NUMBERS = 6;
15
+ export const TRIANGLE_CORNERS = 3;
16
+ export const DEGENERATE_DETERMINANT = 1e-12;
17
+ export const FULL_TURN = 2 * Math.PI;
18
+ export const MIN_LOFT_FRAMES = 2;
19
+ export const MIN_INTERSECTED = 2;
20
+ export const MAX_ISSUES = 100;
21
+ export const INT32_LOWEST = -(Math.pow(2, 31));
22
+ export const INT32_HIGHEST = Math.pow(2, 31) - 1;
23
+ export const ISSUES_IN_MESSAGE = 3;
@@ -0,0 +1,48 @@
1
+ import type { Base } from "../inputs/base-inputs";
2
+ /**
3
+ * One thing wrong with a recipe: where it is, as a path such as `nodes[3].depth`, and what is wrong.
4
+ * @beta
5
+ */
6
+ export interface RecipeIssue {
7
+ /**
8
+ * Where in the recipe the problem is.
9
+ */
10
+ readonly path: string;
11
+ /**
12
+ * What is wrong, in a sentence.
13
+ */
14
+ readonly message: string;
15
+ }
16
+ /**
17
+ * The triangles a recipe root describes, placed by its matrices: `x, y, z` per vertex and three
18
+ * vertex indices per triangle, wound so that a mirroring matrix keeps the outside out.
19
+ * @beta
20
+ */
21
+ export interface RecipeSurfaceMesh {
22
+ /**
23
+ * The vertex positions, three numbers each.
24
+ */
25
+ readonly positions: Float32Array;
26
+ /**
27
+ * The triangles, three vertex indices each.
28
+ */
29
+ readonly indices: Uint32Array;
30
+ }
31
+ export interface SurfacePiece {
32
+ readonly node: Base.RecipeTrianglesNode;
33
+ readonly matrix: Base.TransformMatrix;
34
+ }
35
+ /**
36
+ * The step a recipe node finally places and the matrix that places it there.
37
+ * @beta
38
+ */
39
+ export interface RecipeSource {
40
+ /**
41
+ * The index of the step, a node that is not a rigid transform.
42
+ */
43
+ readonly node: number;
44
+ /**
45
+ * The column-major 4 x 4 matrix taking the step's coordinates to where the node puts it.
46
+ */
47
+ readonly matrix: Base.TransformMatrix;
48
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { Base } from "../inputs/base-inputs";
2
+ import type { RecipeSource } from "./recipe-types";
3
+ /**
4
+ * The step a recipe node places, found by folding every transform step that only turns and moves
5
+ * into the matrix that places it, so roots that place one step through different moves share it.
6
+ *
7
+ * A transform that mirrors, scales or stretches stays a step of its own.
8
+ *
9
+ * Experimental: the recipe format may still change before it is declared stable.
10
+ * @param recipe - The recipe the node belongs to, already checked
11
+ * @param node - The node to follow, such as a root's
12
+ * @param matrix - What places the node, such as its root's matrix
13
+ * @returns The step reached and the matrix placing it
14
+ * @beta
15
+ */
16
+ export declare function recipeSourceOf(recipe: Base.Recipe, node: number, matrix: Base.TransformMatrix): RecipeSource;
@@ -0,0 +1,23 @@
1
+ import { followedBy, isRigidMatrix } from "../services/helpers/matrices";
2
+ /**
3
+ * The step a recipe node places, found by folding every transform step that only turns and moves
4
+ * into the matrix that places it, so roots that place one step through different moves share it.
5
+ *
6
+ * A transform that mirrors, scales or stretches stays a step of its own.
7
+ *
8
+ * Experimental: the recipe format may still change before it is declared stable.
9
+ * @param recipe - The recipe the node belongs to, already checked
10
+ * @param node - The node to follow, such as a root's
11
+ * @param matrix - What places the node, such as its root's matrix
12
+ * @returns The step reached and the matrix placing it
13
+ * @beta
14
+ */
15
+ export function recipeSourceOf(recipe, node, matrix) {
16
+ let source = node;
17
+ let placed = matrix;
18
+ for (let step = recipe.nodes[source]; (step === null || step === void 0 ? void 0 : step.op) === "transform" && isRigidMatrix(step.matrix); step = recipe.nodes[source]) {
19
+ placed = followedBy(step.matrix, placed);
20
+ source = step.of;
21
+ }
22
+ return { node: source, matrix: placed };
23
+ }
@@ -0,0 +1,16 @@
1
+ import type { RecipeSurfaceMesh } from "./recipe-types";
2
+ /**
3
+ * The triangle meshes a recipe's roots describe, for the roots built from triangle nodes alone
4
+ * (through transforms, compounds and unions), placed by their matrices; no kernel is needed. Openings cut
5
+ * through such a mesh are left uncut, since a surface has no inside to cut. A root that holds any
6
+ * other step, such as an extrusion or a cut, gives undefined. This is how an open mesh no solid
7
+ * kernel accepts, such as a building model's furniture surfaces, can still be drawn.
8
+ *
9
+ * Experimental: the recipe format may still change before it is declared stable.
10
+ * @param recipe - The recipe, checked as `assertRecipe` checks it
11
+ * @param roots - The positions of the roots to read, all of them when left out
12
+ * @param adjustZtoY - Whether `(x, y, z)` becomes `(x, z, -y)`, as a recipe build does with the same option
13
+ * @returns One mesh per asked root, in the order asked, or undefined where the root is not made of triangles alone
14
+ * @beta
15
+ */
16
+ export declare function recipeSurfaceMeshes(value: unknown, roots?: readonly number[], adjustZtoY?: boolean): (RecipeSurfaceMesh | undefined)[];
@@ -0,0 +1,86 @@
1
+ import { InputError } from "../kernel-calls/errors";
2
+ import { followedBy, linearDeterminant } from "../services/helpers/matrices";
3
+ import { POINT3_SIZE, TRIANGLE_CORNERS } from "./limits.constants";
4
+ import { toRecipe } from "./validate";
5
+ const Z_TO_Y = [1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1];
6
+ const X = 0;
7
+ const Y = 1;
8
+ const Z = 2;
9
+ const COLUMN = 4;
10
+ const TRANSLATION = 12;
11
+ function collectPieces(nodes, index, matrix, pieces) {
12
+ const node = nodes[index];
13
+ switch (node.op) {
14
+ case "triangles":
15
+ pieces.push({ node, matrix });
16
+ return true;
17
+ case "transform":
18
+ return collectPieces(nodes, node.of, followedBy(node.matrix, matrix), pieces);
19
+ case "compound":
20
+ case "union":
21
+ return node.of.every((child) => collectPieces(nodes, child, matrix, pieces));
22
+ case "voids":
23
+ return collectPieces(nodes, node.host, matrix, pieces);
24
+ default:
25
+ return false;
26
+ }
27
+ }
28
+ function meshOfPieces(buffers, pieces) {
29
+ const vertexCount = pieces.reduce((count, piece) => count + piece.node.positions[1] / POINT3_SIZE, 0);
30
+ const indexCount = pieces.reduce((count, piece) => count + piece.node.indices[1], 0);
31
+ const positions = new Float32Array(vertexCount * POINT3_SIZE);
32
+ const indices = new Uint32Array(indexCount);
33
+ let vertexAt = 0;
34
+ let indexAt = 0;
35
+ for (const { node, matrix } of pieces) {
36
+ const m = matrix;
37
+ const [start, length] = node.positions;
38
+ for (let at = start; at < start + length; at += POINT3_SIZE) {
39
+ const x = buffers.f64[at + X];
40
+ const y = buffers.f64[at + Y];
41
+ const z = buffers.f64[at + Z];
42
+ positions[vertexAt * POINT3_SIZE + X] = m[X] * x + m[COLUMN + X] * y + m[2 * COLUMN + X] * z + m[TRANSLATION + X];
43
+ positions[vertexAt * POINT3_SIZE + Y] = m[Y] * x + m[COLUMN + Y] * y + m[2 * COLUMN + Y] * z + m[TRANSLATION + Y];
44
+ positions[vertexAt * POINT3_SIZE + Z] = m[Z] * x + m[COLUMN + Z] * y + m[2 * COLUMN + Z] * z + m[TRANSLATION + Z];
45
+ vertexAt++;
46
+ }
47
+ const first = vertexAt - length / POINT3_SIZE;
48
+ const mirrored = linearDeterminant(matrix) < 0;
49
+ const [indexStart, indexLength] = node.indices;
50
+ for (let at = indexStart; at < indexStart + indexLength; at += TRIANGLE_CORNERS) {
51
+ indices[indexAt] = first + buffers.i32[at];
52
+ indices[indexAt + 1] = first + buffers.i32[at + (mirrored ? 2 : 1)];
53
+ indices[indexAt + 2] = first + buffers.i32[at + (mirrored ? 1 : 2)];
54
+ indexAt += TRIANGLE_CORNERS;
55
+ }
56
+ }
57
+ return { positions, indices };
58
+ }
59
+ /**
60
+ * The triangle meshes a recipe's roots describe, for the roots built from triangle nodes alone
61
+ * (through transforms, compounds and unions), placed by their matrices; no kernel is needed. Openings cut
62
+ * through such a mesh are left uncut, since a surface has no inside to cut. A root that holds any
63
+ * other step, such as an extrusion or a cut, gives undefined. This is how an open mesh no solid
64
+ * kernel accepts, such as a building model's furniture surfaces, can still be drawn.
65
+ *
66
+ * Experimental: the recipe format may still change before it is declared stable.
67
+ * @param recipe - The recipe, checked as `assertRecipe` checks it
68
+ * @param roots - The positions of the roots to read, all of them when left out
69
+ * @param adjustZtoY - Whether `(x, y, z)` becomes `(x, z, -y)`, as a recipe build does with the same option
70
+ * @returns One mesh per asked root, in the order asked, or undefined where the root is not made of triangles alone
71
+ * @beta
72
+ */
73
+ export function recipeSurfaceMeshes(value, roots, adjustZtoY = false) {
74
+ const recipe = toRecipe(value);
75
+ const asked = roots !== null && roots !== void 0 ? roots : recipe.roots.map((_, index) => index);
76
+ const bad = asked.find((index) => !Number.isSafeInteger(index) || index < 0 || index >= recipe.roots.length);
77
+ if (bad !== undefined) {
78
+ throw new InputError(`Root ${bad} is not one of the recipe's ${recipe.roots.length} roots`, "roots");
79
+ }
80
+ return asked.map((index) => {
81
+ const root = recipe.roots[index];
82
+ const pieces = [];
83
+ const placement = adjustZtoY ? followedBy(root.matrix, Z_TO_Y) : root.matrix;
84
+ return collectPieces(recipe.nodes, root.node, placement, pieces) ? meshOfPieces(recipe.buffers, pieces) : undefined;
85
+ });
86
+ }
@@ -0,0 +1,36 @@
1
+ import type { Base } from "../inputs/base-inputs";
2
+ import type { RecipeIssue } from "./recipe-types";
3
+ /**
4
+ * Checks that a value is a recipe an executor can safely run: every index refers to an earlier
5
+ * node of the right kind, every range lies inside its buffer, every coordinate of a profile or an
6
+ * extrusion is finite and between minus and plus one billion, every placement (a matrix or a
7
+ * half-space's origin) between minus and plus ten trillion, every matrix moves, turns, mirrors and
8
+ * scales without flattening, and no list, total or nesting exceeds its limit. A recipe that comes from another thread, a file or a caller is
9
+ * checked before any kernel touches it.
10
+ *
11
+ * Experimental: the recipe format may still change before it is declared stable.
12
+ * @param recipe - The value to check
13
+ * @returns What is wrong with it, at most a hundred issues, empty when nothing is
14
+ * @beta
15
+ */
16
+ export declare function checkRecipe(recipe: unknown): RecipeIssue[];
17
+ /**
18
+ * Throws an `InputError` when a value is not a recipe an executor can safely run, naming the first
19
+ * three problems `checkRecipe` finds.
20
+ *
21
+ * Experimental: the recipe format may still change before it is declared stable.
22
+ * @param recipe - The value to check
23
+ * @beta
24
+ */
25
+ export declare function assertRecipe(recipe: unknown): asserts recipe is Base.Recipe;
26
+ /**
27
+ * Reads a value as a recipe an executor can run, taking its buffers as lists of numbers too, as JSON
28
+ * carries them: such lists become the typed arrays the format holds, and the result is checked as
29
+ * `assertRecipe` checks it. The value it is given is left as it was.
30
+ *
31
+ * Experimental: the recipe format may still change before it is declared stable.
32
+ * @param value - The recipe, its buffers typed arrays or lists of numbers
33
+ * @returns The recipe with typed buffers
34
+ * @beta
35
+ */
36
+ export declare function toRecipe(value: unknown): Base.Recipe;