@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,298 @@
1
+ import { triangulateFace } from "./triangulation";
2
+ import { add3, cross3, dot3, length3, scale3, subtract3 } from "./vectors";
3
+ const SEGMENTS_PER_TURN = 32;
4
+ const TURN_SLACK = 1e-6;
5
+ const MAX_TURN = ((2 * Math.PI) / SEGMENTS_PER_TURN) * (1 + TURN_SLACK);
6
+ const SAGITTA_PER_LENGTH = Math.tan(MAX_TURN / (2 * 2)) / 2;
7
+ const MAX_VERTICES = 20000;
8
+ const MAX_PASSES = 24;
9
+ const MAX_FLIP_ROUNDS = 1000;
10
+ const NORMAL_STEP = 1e-5;
11
+ const FLIP_SLACK = 1e-9;
12
+ const FLAT_RATIO = 1e-12;
13
+ const METRIC_SAMPLES = 64;
14
+ const METRIC_WIDTH = 1000;
15
+ const KEY_BITS = 22;
16
+ const KEY_BASE = Math.pow(2, KEY_BITS);
17
+ const CORNERS = 3;
18
+ const keyOf = (a, b) => (a < b ? a * KEY_BASE + b : b * KEY_BASE + a);
19
+ const unitOrUndefined = (vector) => {
20
+ const length = length3(vector);
21
+ return length > 0 ? scale3(vector, 1 / length) : undefined;
22
+ };
23
+ const angleAt = (corner, a, b) => {
24
+ const toA = [a[0] - corner[0], a[1] - corner[1]];
25
+ const toB = [b[0] - corner[0], b[1] - corner[1]];
26
+ const lengths = Math.hypot(toA[0], toA[1]) * Math.hypot(toB[0], toB[1]);
27
+ return lengths > 0 ? Math.acos(Math.max(-1, Math.min(1, (toA[0] * toB[0] + toA[1] * toB[1]) / lengths))) : 0;
28
+ };
29
+ const orient2 = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
30
+ class SurfaceMesh {
31
+ constructor(uv, positions, loops, pointAt) {
32
+ this.pointAt = pointAt;
33
+ this.normals = [];
34
+ this.triangles = [];
35
+ this.edges = new Map();
36
+ this.constrained = new Set();
37
+ this.uv = uv.map(([u, v]) => [u, v]);
38
+ this.xyz = positions.map(([x, y, z]) => [x, y, z]);
39
+ const us = this.uv.map(([u]) => u);
40
+ const vs = this.uv.map(([, v]) => v);
41
+ this.step = [(Math.max(...us) - Math.min(...us)) * NORMAL_STEP || NORMAL_STEP, (Math.max(...vs) - Math.min(...vs)) * NORMAL_STEP || NORMAL_STEP];
42
+ loops.forEach((loop) => loop.forEach((vertex, at) => this.constrained.add(keyOf(vertex, loop[(at + 1) % loop.length]))));
43
+ this.metric = this.metricOf();
44
+ const flat = this.uv.flatMap(([u, v]) => [u, v, 0]);
45
+ const corners = triangulateFace(flat, loops);
46
+ let winding = 0;
47
+ for (let at = 0; at + 2 < corners.length; at += CORNERS) {
48
+ const triangle = [corners[at], corners[at + 1], corners[at + 2]];
49
+ winding += orient2(this.uv[triangle[0]], this.uv[triangle[1]], this.uv[triangle[2]]);
50
+ this.add(triangle);
51
+ }
52
+ this.side = Math.sign(winding);
53
+ this.flatArea = FLAT_RATIO * (Math.max(...us) - Math.min(...us)) * (Math.max(...vs) - Math.min(...vs));
54
+ }
55
+ refine() {
56
+ this.removeSlivers();
57
+ for (let pass = 0; pass < MAX_PASSES; pass++) {
58
+ this.flipToDelaunay();
59
+ const split = this.splitBentEdges();
60
+ const filled = this.fillBentTriangles();
61
+ if (!split && !filled) {
62
+ break;
63
+ }
64
+ }
65
+ this.flipToDelaunay();
66
+ return { positions: this.xyz, triangles: this.triangles.flatMap((triangle) => (triangle ? [...triangle] : [])) };
67
+ }
68
+ metricOf() {
69
+ const [hu, hv] = this.step;
70
+ const xs = this.xyz.map(([x]) => x);
71
+ const ys = this.xyz.map(([, y]) => y);
72
+ const zs = this.xyz.map(([, , z]) => z);
73
+ const size = Math.hypot(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys), Math.max(...zs) - Math.min(...zs)) || 1;
74
+ const every = Math.max(1, Math.floor(this.uv.length / METRIC_SAMPLES));
75
+ const [wu, wv] = [hu * METRIC_WIDTH, hv * METRIC_WIDTH];
76
+ let alongU = 0;
77
+ let alongV = 0;
78
+ let count = 0;
79
+ for (let at = 0; at < this.uv.length; at += every) {
80
+ const [u, v] = this.uv[at];
81
+ const stretchU = length3(subtract3(this.pointAt(u + hu, v), this.pointAt(u - hu, v))) / (2 * hu);
82
+ const stretchV = length3(subtract3(this.pointAt(u, v + hv), this.pointAt(u, v - hv))) / (2 * hv);
83
+ alongU += this.normalTurn([u - wu, v], [u + wu, v]) / (2 * wu) / MAX_TURN + stretchU / size;
84
+ alongV += this.normalTurn([u, v - wv], [u, v + wv]) / (2 * wv) / MAX_TURN + stretchV / size;
85
+ count++;
86
+ }
87
+ return [alongU / count || 1, alongV / count || 1];
88
+ }
89
+ normalAt(u, v) {
90
+ const [hu, hv] = this.step;
91
+ return unitOrUndefined(cross3(subtract3(this.pointAt(u + hu, v), this.pointAt(u - hu, v)), subtract3(this.pointAt(u, v + hv), this.pointAt(u, v - hv))));
92
+ }
93
+ normalTurn(from, to) {
94
+ const first = this.normalAt(from[0], from[1]);
95
+ const second = this.normalAt(to[0], to[1]);
96
+ return first && second ? Math.acos(Math.max(-1, Math.min(1, dot3(first, second)))) : 0;
97
+ }
98
+ scaled(vertex) {
99
+ const [u, v] = this.uv[vertex];
100
+ return [u * this.metric[0], v * this.metric[1]];
101
+ }
102
+ normal(vertex) {
103
+ const known = this.normals[vertex];
104
+ if (known !== undefined) {
105
+ return known !== null && known !== void 0 ? known : undefined;
106
+ }
107
+ const [u, v] = this.uv[vertex];
108
+ const normal = this.normalAt(u, v);
109
+ this.normals[vertex] = normal !== null && normal !== void 0 ? normal : null;
110
+ return normal;
111
+ }
112
+ turn(a, b) {
113
+ const first = this.normal(a);
114
+ const second = this.normal(b);
115
+ return first && second ? Math.acos(Math.max(-1, Math.min(1, dot3(first, second)))) : 0;
116
+ }
117
+ bendingChord(a, b) {
118
+ const first = this.normal(a);
119
+ const second = this.normal(b);
120
+ const across = first && second ? unitOrUndefined(subtract3(second, first)) : undefined;
121
+ return across ? Math.abs(dot3(subtract3(this.xyz[b], this.xyz[a]), across)) : 0;
122
+ }
123
+ bent(corners) {
124
+ const sides = corners.map((vertex, at) => [vertex, corners[(at + 1) % corners.length]]);
125
+ if (!sides.some(([a, b]) => this.turn(a, b) > MAX_TURN)) {
126
+ return undefined;
127
+ }
128
+ const chord = Math.max(...sides.map(([a, b]) => this.bendingChord(a, b)));
129
+ const uv = [0, 1].map((axis) => corners.reduce((sum, vertex) => sum + this.uv[vertex][axis], 0) / corners.length);
130
+ const flat = scale3(corners.reduce((sum, vertex) => add3(sum, this.xyz[vertex]), [0, 0, 0]), 1 / corners.length);
131
+ return length3(subtract3(this.pointAt(uv[0], uv[1]), flat)) > SAGITTA_PER_LENGTH * chord ? uv : undefined;
132
+ }
133
+ vertex(uv) {
134
+ this.uv.push(uv);
135
+ this.xyz.push(this.pointAt(uv[0], uv[1]));
136
+ return this.uv.length - 1;
137
+ }
138
+ add(triangle) {
139
+ const index = this.triangles.length;
140
+ this.triangles.push(triangle);
141
+ triangle.forEach((vertex, at) => {
142
+ const key = keyOf(vertex, triangle[(at + 1) % CORNERS]);
143
+ const list = this.edges.get(key);
144
+ if (list) {
145
+ list.push(index);
146
+ }
147
+ else {
148
+ this.edges.set(key, [index]);
149
+ }
150
+ });
151
+ }
152
+ remove(index) {
153
+ const triangle = this.triangles[index];
154
+ this.triangles[index] = undefined;
155
+ triangle.forEach((vertex, at) => {
156
+ const key = keyOf(vertex, triangle[(at + 1) % CORNERS]);
157
+ const list = this.edges.get(key).filter((other) => other !== index);
158
+ if (list.length) {
159
+ this.edges.set(key, list);
160
+ }
161
+ else {
162
+ this.edges.delete(key);
163
+ }
164
+ });
165
+ return triangle;
166
+ }
167
+ alongEdge(index, a, b) {
168
+ const [x, y, z] = this.triangles[index];
169
+ if ((x === a && y === b) || (x === b && y === a)) {
170
+ return [x, y, z];
171
+ }
172
+ if ((y === a && z === b) || (y === b && z === a)) {
173
+ return [y, z, x];
174
+ }
175
+ return [z, x, y];
176
+ }
177
+ splitBentEdges() {
178
+ let changed = false;
179
+ for (const key of [...this.edges.keys()]) {
180
+ if (this.uv.length >= MAX_VERTICES) {
181
+ break;
182
+ }
183
+ const sharing = this.edges.get(key);
184
+ if (this.constrained.has(key) || (sharing === null || sharing === void 0 ? void 0 : sharing.length) !== 2) {
185
+ continue;
186
+ }
187
+ const a = Math.floor(key / KEY_BASE);
188
+ const b = key % KEY_BASE;
189
+ const at = this.bent([a, b]);
190
+ if (at === undefined || sharing.some((index) => this.isSliver(this.triangles[index]) || this.isAt(at, this.alongEdge(index, a, b)[2]))) {
191
+ continue;
192
+ }
193
+ const middle = this.vertex(at);
194
+ for (const index of sharing) {
195
+ const [p, q, r] = this.alongEdge(index, a, b);
196
+ this.remove(index);
197
+ this.add([p, middle, r]);
198
+ this.add([middle, q, r]);
199
+ }
200
+ changed = true;
201
+ }
202
+ return changed;
203
+ }
204
+ fillBentTriangles() {
205
+ let changed = false;
206
+ const count = this.triangles.length;
207
+ for (let index = 0; index < count && this.uv.length < MAX_VERTICES; index++) {
208
+ const triangle = this.triangles[index];
209
+ if (!triangle || !triangle.every((vertex, at) => this.constrained.has(keyOf(vertex, triangle[(at + 1) % CORNERS])))) {
210
+ continue;
211
+ }
212
+ const [a, b, c] = triangle;
213
+ const at = this.bent(triangle);
214
+ if (at === undefined) {
215
+ continue;
216
+ }
217
+ const centre = this.vertex(at);
218
+ this.remove(index);
219
+ this.add([a, b, centre]);
220
+ this.add([b, c, centre]);
221
+ this.add([c, a, centre]);
222
+ changed = true;
223
+ }
224
+ return changed;
225
+ }
226
+ isAt(uv, vertex) {
227
+ const [u, v] = this.uv[vertex];
228
+ return Math.abs(u - uv[0]) <= this.step[0] && Math.abs(v - uv[1]) <= this.step[1];
229
+ }
230
+ isSliver(triangle) {
231
+ const [a, b, c] = triangle.map((vertex) => this.uv[vertex]);
232
+ return Math.abs(orient2(a, b, c)) <= this.flatArea;
233
+ }
234
+ removeSlivers() {
235
+ const rounds = this.triangles.length;
236
+ for (let round = 0; round < rounds; round++) {
237
+ let flipped = false;
238
+ for (let index = 0; index < this.triangles.length; index++) {
239
+ const triangle = this.triangles[index];
240
+ if (!triangle || !this.isSliver(triangle)) {
241
+ continue;
242
+ }
243
+ const lengths = triangle.map((vertex, at) => {
244
+ const next = triangle[(at + 1) % CORNERS];
245
+ return Math.hypot(this.uv[next][0] - this.uv[vertex][0], this.uv[next][1] - this.uv[vertex][1]);
246
+ });
247
+ const longest = lengths.indexOf(Math.max(...lengths));
248
+ flipped = this.flip(triangle[longest], triangle[(longest + 1) % CORNERS]) || flipped;
249
+ }
250
+ if (!flipped) {
251
+ return;
252
+ }
253
+ }
254
+ }
255
+ flip(a, b) {
256
+ const key = keyOf(a, b);
257
+ const sharing = this.edges.get(key);
258
+ if (this.constrained.has(key) || (sharing === null || sharing === void 0 ? void 0 : sharing.length) !== 2) {
259
+ return false;
260
+ }
261
+ const [p, q, c] = this.alongEdge(sharing[0], a, b);
262
+ const [, , d] = this.alongEdge(sharing[1], a, b);
263
+ const uv = this.uv;
264
+ const first = orient2(uv[p], uv[d], uv[c]);
265
+ const second = orient2(uv[d], uv[q], uv[c]);
266
+ if (c === d || this.edges.has(keyOf(c, d)) || Math.sign(first) !== this.side || Math.sign(second) !== this.side || Math.abs(first) <= this.flatArea || Math.abs(second) <= this.flatArea) {
267
+ return false;
268
+ }
269
+ this.remove(sharing[0]);
270
+ this.remove(sharing[1]);
271
+ this.add([p, d, c]);
272
+ this.add([d, q, c]);
273
+ return true;
274
+ }
275
+ flipToDelaunay() {
276
+ for (let round = 0; round < MAX_FLIP_ROUNDS; round++) {
277
+ let flipped = false;
278
+ for (const key of [...this.edges.keys()]) {
279
+ const sharing = this.edges.get(key);
280
+ if (this.constrained.has(key) || (sharing === null || sharing === void 0 ? void 0 : sharing.length) !== 2) {
281
+ continue;
282
+ }
283
+ const a = Math.floor(key / KEY_BASE);
284
+ const b = key % KEY_BASE;
285
+ const [p, q, c] = this.alongEdge(sharing[0], a, b);
286
+ const [, , d] = this.alongEdge(sharing[1], a, b);
287
+ const [sp, sq, sc, sd] = [p, q, c, d].map((vertex) => this.scaled(vertex));
288
+ if (angleAt(sc, sp, sq) + angleAt(sd, sp, sq) > Math.PI + FLIP_SLACK) {
289
+ flipped = this.flip(a, b) || flipped;
290
+ }
291
+ }
292
+ if (!flipped) {
293
+ return;
294
+ }
295
+ }
296
+ }
297
+ }
298
+ export const refinedSurfaceMesh = (uv, positions, loops, pointAt) => new SurfaceMesh(uv, positions, loops, pointAt).refine();
@@ -0,0 +1,2 @@
1
+ export declare function newellNormal(positions: ArrayLike<number>, loop: readonly number[]): [number, number, number];
2
+ export declare function triangulateFace(positions: ArrayLike<number>, loops: readonly (readonly number[])[]): number[];
@@ -0,0 +1,295 @@
1
+ const TRIANGLE = 3;
2
+ const NO_NODE = -1;
3
+ const STRICT_EAR = 0;
4
+ const FLAT_EAR = 1;
5
+ const ANY_EAR = 2;
6
+ function withoutRepeats(loop) {
7
+ const kept = loop.filter((vertex, at) => vertex !== loop[at === 0 ? loop.length - 1 : at - 1]);
8
+ return kept.length === 0 && loop.length > 0 ? [loop[0]] : kept;
9
+ }
10
+ export function newellNormal(positions, loop) {
11
+ let nx = 0;
12
+ let ny = 0;
13
+ let nz = 0;
14
+ for (let at = 0; at < loop.length; at++) {
15
+ const i = loop[at] * TRIANGLE;
16
+ const j = loop[(at + 1) % loop.length] * TRIANGLE;
17
+ nx += (positions[i + 1] - positions[j + 1]) * (positions[i + 2] + positions[j + 2]);
18
+ ny += (positions[i + 2] - positions[j + 2]) * (positions[i] + positions[j]);
19
+ nz += (positions[i] - positions[j]) * (positions[i + 1] + positions[j + 1]);
20
+ }
21
+ return [nx, ny, nz];
22
+ }
23
+ function fan(loop) {
24
+ const triangles = [];
25
+ for (let at = 1; at + 1 < loop.length; at++) {
26
+ triangles.push(loop[0], loop[at], loop[at + 1]);
27
+ }
28
+ return triangles;
29
+ }
30
+ function turn(ax, ay, bx, by, cx, cy) {
31
+ return (by - ay) * (cx - bx) - (bx - ax) * (cy - by);
32
+ }
33
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
34
+ return (cx - px) * (ay - py) >= (ax - px) * (cy - py)
35
+ && (ax - px) * (by - py) >= (bx - px) * (ay - py)
36
+ && (bx - px) * (cy - py) >= (cx - px) * (by - py);
37
+ }
38
+ function signedArea(xs, ys) {
39
+ let twice = 0;
40
+ for (let at = 0, previous = xs.length - 1; at < xs.length; previous = at++) {
41
+ twice += xs[previous] * ys[at] - xs[at] * ys[previous];
42
+ }
43
+ return twice / 2;
44
+ }
45
+ class Ring {
46
+ constructor() {
47
+ this.x = [];
48
+ this.y = [];
49
+ this.vertex = [];
50
+ this.prev = [];
51
+ this.next = [];
52
+ this.triangles = [];
53
+ }
54
+ loop(vertices, xs, ys, counterClockwise) {
55
+ const reversed = (signedArea(xs, ys) > 0) !== counterClockwise;
56
+ let last = NO_NODE;
57
+ for (let step = 0; step < vertices.length; step++) {
58
+ const at = reversed ? vertices.length - 1 - step : step;
59
+ last = this.insert(vertices[at], xs[at], ys[at], last);
60
+ }
61
+ return last;
62
+ }
63
+ withHoles(outer, holes) {
64
+ const leftmost = holes.map((hole) => this.leftmost(hole)).sort((a, b) => this.x[a] - this.x[b] || this.y[a] - this.y[b]);
65
+ let start = outer;
66
+ for (const hole of leftmost) {
67
+ const bridge = this.bridgeTo(hole, start);
68
+ this.split(bridge, hole);
69
+ start = bridge;
70
+ }
71
+ return start;
72
+ }
73
+ clip(start) {
74
+ let ear = start;
75
+ let stop = ear;
76
+ let level = STRICT_EAR;
77
+ while (this.prev[ear] !== this.next[ear]) {
78
+ const previous = this.prev[ear];
79
+ const following = this.next[ear];
80
+ if (this.isEar(ear, level)) {
81
+ this.emit(previous, ear, following);
82
+ this.remove(ear);
83
+ ear = this.next[following];
84
+ stop = ear;
85
+ level = STRICT_EAR;
86
+ continue;
87
+ }
88
+ ear = following;
89
+ if (ear === stop) {
90
+ level = Math.min(level + 1, ANY_EAR);
91
+ }
92
+ }
93
+ return this.triangles;
94
+ }
95
+ insert(vertex, x, y, last) {
96
+ const node = this.x.length;
97
+ this.x.push(x);
98
+ this.y.push(y);
99
+ this.vertex.push(vertex);
100
+ if (last === NO_NODE) {
101
+ this.prev.push(node);
102
+ this.next.push(node);
103
+ return node;
104
+ }
105
+ const after = this.next[last];
106
+ this.prev.push(last);
107
+ this.next.push(after);
108
+ this.prev[after] = node;
109
+ this.next[last] = node;
110
+ return node;
111
+ }
112
+ remove(node) {
113
+ const before = this.prev[node];
114
+ const after = this.next[node];
115
+ this.next[before] = after;
116
+ this.prev[after] = before;
117
+ }
118
+ emit(a, b, c) {
119
+ const va = this.vertex[a];
120
+ const vb = this.vertex[b];
121
+ const vc = this.vertex[c];
122
+ if (va !== vb && vb !== vc && vc !== va) {
123
+ this.triangles.push(va, vb, vc);
124
+ }
125
+ }
126
+ area(a, b, c) {
127
+ return turn(this.x[a], this.y[a], this.x[b], this.y[b], this.x[c], this.y[c]);
128
+ }
129
+ isEar(ear, level) {
130
+ if (level === ANY_EAR) {
131
+ return true;
132
+ }
133
+ const a = this.prev[ear];
134
+ const c = this.next[ear];
135
+ const corner = this.area(a, ear, c);
136
+ if (corner > 0 || (corner === 0 && level < FLAT_EAR)) {
137
+ return false;
138
+ }
139
+ const ax = this.x[a];
140
+ const ay = this.y[a];
141
+ const bx = this.x[ear];
142
+ const by = this.y[ear];
143
+ const cx = this.x[c];
144
+ const cy = this.y[c];
145
+ const minX = Math.min(ax, bx, cx);
146
+ const minY = Math.min(ay, by, cy);
147
+ const maxX = Math.max(ax, bx, cx);
148
+ const maxY = Math.max(ay, by, cy);
149
+ for (let p = this.next[c]; p !== a; p = this.next[p]) {
150
+ const px = this.x[p];
151
+ const py = this.y[p];
152
+ if (!(px === ax && py === ay) && px >= minX && px <= maxX && py >= minY && py <= maxY
153
+ && pointInTriangle(ax, ay, bx, by, cx, cy, px, py) && this.area(this.prev[p], p, this.next[p]) >= 0) {
154
+ return false;
155
+ }
156
+ }
157
+ return true;
158
+ }
159
+ leftmost(start) {
160
+ let best = start;
161
+ for (let p = this.next[start]; p !== start; p = this.next[p]) {
162
+ if (this.x[p] < this.x[best] || (this.x[p] === this.x[best] && this.y[p] < this.y[best])) {
163
+ best = p;
164
+ }
165
+ }
166
+ return best;
167
+ }
168
+ locallyInside(a, b) {
169
+ const before = this.prev[a];
170
+ const after = this.next[a];
171
+ return this.area(before, a, after) < 0
172
+ ? this.area(a, b, after) >= 0 && this.area(a, before, b) >= 0
173
+ : this.area(a, b, before) < 0 || this.area(a, after, b) < 0;
174
+ }
175
+ sectorContainsSector(m, p) {
176
+ return this.area(this.prev[m], m, this.prev[p]) < 0 && this.area(this.next[p], m, this.next[m]) < 0;
177
+ }
178
+ bridgeTo(hole, outer) {
179
+ const hx = this.x[hole];
180
+ const hy = this.y[hole];
181
+ let qx = -Infinity;
182
+ let m = NO_NODE;
183
+ let p = outer;
184
+ do {
185
+ const after = this.next[p];
186
+ const py = this.y[p];
187
+ const ny = this.y[after];
188
+ if (this.x[p] === hx && py === hy) {
189
+ return p;
190
+ }
191
+ if (hy <= py && hy >= ny && ny !== py) {
192
+ const x = this.x[p] + (hy - py) * (this.x[after] - this.x[p]) / (ny - py);
193
+ if (x <= hx && x > qx) {
194
+ qx = x;
195
+ m = this.x[p] < this.x[after] ? p : after;
196
+ if (x === hx) {
197
+ return m;
198
+ }
199
+ }
200
+ }
201
+ p = after;
202
+ } while (p !== outer);
203
+ return m === NO_NODE ? this.nearest(hole, outer) : this.sharpest(hole, m, qx);
204
+ }
205
+ sharpest(hole, candidate, qx) {
206
+ const hx = this.x[hole];
207
+ const hy = this.y[hole];
208
+ const mx = this.x[candidate];
209
+ const my = this.y[candidate];
210
+ let m = candidate;
211
+ let tanMin = Infinity;
212
+ let p = candidate;
213
+ do {
214
+ const px = this.x[p];
215
+ const py = this.y[p];
216
+ if (hx >= px && px >= mx && hx !== px && pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, px, py)) {
217
+ const tan = Math.abs(hy - py) / (hx - px);
218
+ if (this.locallyInside(p, hole) && (tan < tanMin || (tan === tanMin && (px > this.x[m] || (px === this.x[m] && this.sectorContainsSector(m, p)))))) {
219
+ m = p;
220
+ tanMin = tan;
221
+ }
222
+ }
223
+ p = this.next[p];
224
+ } while (p !== candidate);
225
+ return m;
226
+ }
227
+ nearest(hole, outer) {
228
+ let best = outer;
229
+ let bestDistance = Infinity;
230
+ let p = outer;
231
+ do {
232
+ const distance = Math.pow((this.x[p] - this.x[hole]), 2) + Math.pow((this.y[p] - this.y[hole]), 2);
233
+ if (distance < bestDistance) {
234
+ best = p;
235
+ bestDistance = distance;
236
+ }
237
+ p = this.next[p];
238
+ } while (p !== outer);
239
+ return best;
240
+ }
241
+ split(a, b) {
242
+ const a2 = this.insert(this.vertex[a], this.x[a], this.y[a], NO_NODE);
243
+ const b2 = this.insert(this.vertex[b], this.x[b], this.y[b], NO_NODE);
244
+ const an = this.next[a];
245
+ const bp = this.prev[b];
246
+ this.next[a] = b;
247
+ this.prev[b] = a;
248
+ this.next[a2] = an;
249
+ this.prev[an] = a2;
250
+ this.next[b2] = a2;
251
+ this.prev[a2] = b2;
252
+ this.next[bp] = b2;
253
+ this.prev[b2] = bp;
254
+ }
255
+ }
256
+ function isConvex(xs, ys) {
257
+ for (let at = 0; at < xs.length; at++) {
258
+ const b = (at + 1) % xs.length;
259
+ const c = (at + 2) % xs.length;
260
+ if (turn(xs[at], ys[at], xs[b], ys[b], xs[c], ys[c]) > 0) {
261
+ return false;
262
+ }
263
+ }
264
+ return true;
265
+ }
266
+ export function triangulateFace(positions, loops) {
267
+ const [outer, ...rest] = loops.map(withoutRepeats);
268
+ if (!outer || outer.length < TRIANGLE) {
269
+ return [];
270
+ }
271
+ const holes = rest.filter((hole) => hole.length >= TRIANGLE);
272
+ if (outer.length === TRIANGLE && holes.length === 0) {
273
+ return [...outer];
274
+ }
275
+ const normal = newellNormal(positions, outer);
276
+ const magnitudes = normal.map(Math.abs);
277
+ const dropped = magnitudes.indexOf(Math.max(...magnitudes));
278
+ if (!(magnitudes[dropped] > 0)) {
279
+ return fan(outer);
280
+ }
281
+ const u = (dropped + 1) % TRIANGLE;
282
+ const v = (dropped + 2) % TRIANGLE;
283
+ const sense = normal[dropped] < 0 ? -1 : 1;
284
+ const xsOf = (loop) => loop.map((vertex) => positions[vertex * TRIANGLE + u] * sense);
285
+ const ysOf = (loop) => loop.map((vertex) => positions[vertex * TRIANGLE + v]);
286
+ const xs = xsOf(outer);
287
+ const ys = ysOf(outer);
288
+ if (holes.length === 0 && isConvex(xs, ys)) {
289
+ return fan(outer);
290
+ }
291
+ const ring = new Ring();
292
+ const start = ring.loop(outer, xs, ys, true);
293
+ const holeStarts = holes.map((hole) => ring.loop(hole, xsOf(hole), ysOf(hole), false));
294
+ return ring.clip(ring.withHoles(start, holeStarts));
295
+ }
@@ -0,0 +1,18 @@
1
+ import type * as Inputs from "../../inputs";
2
+ type Vec2 = Inputs.Base.Vector2;
3
+ type Vec3 = Inputs.Base.Vector3;
4
+ export declare const add2: (a: Vec2, b: Vec2) => Vec2;
5
+ export declare const subtract2: (a: Vec2, b: Vec2) => Vec2;
6
+ export declare const scale2: (a: Vec2, factor: number) => Vec2;
7
+ export declare const dot2: (a: Vec2, b: Vec2) => number;
8
+ export declare const cross2: (a: Vec2, b: Vec2) => number;
9
+ export declare const length2: (a: Vec2) => number;
10
+ export declare const leftOf2: (a: Vec2) => Vec2;
11
+ export declare const unit2Of: (a: Vec2) => Vec2 | undefined;
12
+ export declare const add3: (a: Vec3, b: Vec3) => Vec3;
13
+ export declare const subtract3: (a: Vec3, b: Vec3) => Vec3;
14
+ export declare const scale3: (a: Vec3, factor: number) => Vec3;
15
+ export declare const dot3: (a: Vec3, b: Vec3) => number;
16
+ export declare const cross3: (a: Vec3, b: Vec3) => Vec3;
17
+ export declare const length3: (a: Vec3) => number;
18
+ export {};
@@ -0,0 +1,17 @@
1
+ export const add2 = (a, b) => [a[0] + b[0], a[1] + b[1]];
2
+ export const subtract2 = (a, b) => [a[0] - b[0], a[1] - b[1]];
3
+ export const scale2 = (a, factor) => [a[0] * factor, a[1] * factor];
4
+ export const dot2 = (a, b) => a[0] * b[0] + a[1] * b[1];
5
+ export const cross2 = (a, b) => a[0] * b[1] - a[1] * b[0];
6
+ export const length2 = (a) => Math.hypot(a[0], a[1]);
7
+ export const leftOf2 = (a) => [-a[1], a[0]];
8
+ export const unit2Of = (a) => {
9
+ const length = length2(a);
10
+ return length > 0 && length < Infinity ? [a[0] / length, a[1] / length] : undefined;
11
+ };
12
+ export const add3 = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
13
+ export const subtract3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
14
+ export const scale3 = (a, factor) => [a[0] * factor, a[1] * factor, a[2] * factor];
15
+ export const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
16
+ export const cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
17
+ export const length3 = (a) => Math.hypot(a[0], a[1], a[2]);
@@ -0,0 +1,34 @@
1
+ export declare const CRC32_POLYNOMIAL = 3988292384;
2
+ export declare const CRC32_ALL_ONES = 4294967295;
3
+ export declare const BYTE_MASK = 255;
4
+ export declare const BYTE_BITS = 8;
5
+ export declare const BYTE_VALUES = 256;
6
+ export declare const LOCAL_HEADER_SIGNATURE = 67324752;
7
+ export declare const CENTRAL_HEADER_SIGNATURE = 33639248;
8
+ export declare const END_OF_CENTRAL_SIGNATURE = 101010256;
9
+ export declare const END_OF_CENTRAL_SIZE = 22;
10
+ export declare const LONGEST_ARCHIVE_COMMENT = 65535;
11
+ export declare const ZIP64_COUNT = 65535;
12
+ export declare const ZIP64_SIZE = 4294967295;
13
+ export declare const END_ENTRY_COUNT = 10;
14
+ export declare const END_DIRECTORY_SIZE = 12;
15
+ export declare const END_DIRECTORY_OFFSET = 16;
16
+ export declare const CENTRAL_FLAGS = 8;
17
+ export declare const CENTRAL_METHOD = 10;
18
+ export declare const CENTRAL_CRC32 = 16;
19
+ export declare const CENTRAL_COMPRESSED_SIZE = 20;
20
+ export declare const CENTRAL_SIZE = 24;
21
+ export declare const CENTRAL_NAME_LENGTH = 28;
22
+ export declare const CENTRAL_EXTRA_LENGTH = 30;
23
+ export declare const CENTRAL_COMMENT_LENGTH = 32;
24
+ export declare const CENTRAL_LOCAL_OFFSET = 42;
25
+ export declare const CENTRAL_HEADER_SIZE = 46;
26
+ export declare const LOCAL_NAME_LENGTH = 26;
27
+ export declare const LOCAL_EXTRA_LENGTH = 28;
28
+ export declare const LOCAL_HEADER_SIZE = 30;
29
+ export declare const ENCRYPTED_FLAG = 1;
30
+ export declare const UTF8_NAME_FLAG = 2048;
31
+ export declare const STORED_METHOD = 0;
32
+ export declare const DEFLATED_METHOD = 8;
33
+ export declare const UINT16_BYTES = 2;
34
+ export declare const UINT32_BYTES = 4;