@ac-kit/math-geometry 0.3.0 → 0.4.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.
- package/CHANGELOG.md +104 -0
- package/README.md +43 -3
- package/dist/_polygon3-cross.d.ts +23 -0
- package/dist/_polygon3-cross.d.ts.map +1 -0
- package/dist/_polygon3-cross.js +418 -0
- package/dist/_polygon3-cross.js.map +1 -0
- package/dist/_triangle3-cross.d.ts +66 -0
- package/dist/_triangle3-cross.d.ts.map +1 -0
- package/dist/_triangle3-cross.js +396 -0
- package/dist/_triangle3-cross.js.map +1 -0
- package/dist/line2.d.ts +4 -0
- package/dist/line2.d.ts.map +1 -1
- package/dist/line2.js +26 -5
- package/dist/line2.js.map +1 -1
- package/dist/mesh/_index-kernels-u16.d.ts +51 -9
- package/dist/mesh/_index-kernels-u16.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u16.js +905 -124
- package/dist/mesh/_index-kernels-u16.js.map +1 -1
- package/dist/mesh/_index-kernels-u32.d.ts +51 -9
- package/dist/mesh/_index-kernels-u32.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u32.js +905 -124
- package/dist/mesh/_index-kernels-u32.js.map +1 -1
- package/dist/mesh/mesh3-kernels.d.ts +56 -16
- package/dist/mesh/mesh3-kernels.d.ts.map +1 -1
- package/dist/mesh/mesh3-kernels.js +56 -16
- package/dist/mesh/mesh3-kernels.js.map +1 -1
- package/dist/plane3.d.ts +4 -3
- package/dist/plane3.d.ts.map +1 -1
- package/dist/plane3.js +18 -35
- package/dist/plane3.js.map +1 -1
- package/dist/polygon2.d.ts +54 -4
- package/dist/polygon2.d.ts.map +1 -1
- package/dist/polygon2.js +297 -7
- package/dist/polygon2.js.map +1 -1
- package/dist/polygon3.d.ts +19 -3
- package/dist/polygon3.d.ts.map +1 -1
- package/dist/polygon3.js +40 -16
- package/dist/polygon3.js.map +1 -1
- package/dist/quadtree.d.ts +40 -23
- package/dist/quadtree.d.ts.map +1 -1
- package/dist/quadtree.js +196 -119
- package/dist/quadtree.js.map +1 -1
- package/dist/segment2.d.ts +6 -1
- package/dist/segment2.d.ts.map +1 -1
- package/dist/segment2.js +20 -4
- package/dist/segment2.js.map +1 -1
- package/dist/triangle3.d.ts +20 -1
- package/dist/triangle3.d.ts.map +1 -1
- package/dist/triangle3.js +38 -18
- package/dist/triangle3.js.map +1 -1
- package/package.json +7 -7
|
@@ -1,71 +1,367 @@
|
|
|
1
1
|
// Generated from scripts/templates/index-kernels.liquid by scripts/generate-index-kernels.ts. Do not edit.
|
|
2
|
+
import { binaryExponent, PreciseSum, scaleByPowerOfTwoInPlace, } from "@ac-kit/core";
|
|
2
3
|
import { vec2Create, vec3Create, } from "@ac-kit/math-linear";
|
|
4
|
+
import { DotExactAccumulator, twoProductInRangeInPlace, twoSumInPlace, } from "@ac-kit/math-scalar";
|
|
5
|
+
import { triangle3Cross_, writeScaledComponents_, } from "../_triangle3-cross.js";
|
|
3
6
|
import { ray3IntersectTriangle3 } from "../ray3.js";
|
|
4
7
|
import { triangle3Create } from "../triangle3-record.js";
|
|
5
8
|
import { triangle3Normal } from "../triangle3.js";
|
|
6
9
|
// Scratch values, made by their creators and never handed out (S23).
|
|
7
10
|
const TRIANGLE_ = triangle3Create();
|
|
11
|
+
/** A face's corners a, b and c, for `triangle3Cross_`. */
|
|
12
|
+
const CORNERS_ = new Float64Array(9);
|
|
8
13
|
const BARYCENTRIC_ = vec2Create();
|
|
9
14
|
const NORMAL_ = vec3Create();
|
|
15
|
+
const AREA_ = new PreciseSum();
|
|
16
|
+
const SIX_VOLUMES_ = new PreciseSum();
|
|
17
|
+
/** Three times the total weight, then the sums of its products with x, y and z. */
|
|
18
|
+
const CENTROID_SUMS_ = [
|
|
19
|
+
new PreciseSum(),
|
|
20
|
+
new PreciseSum(),
|
|
21
|
+
new PreciseSum(),
|
|
22
|
+
new PreciseSum(),
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* The frame of the area, volume and centroid kernels, per axis: the anchor
|
|
26
|
+
* (0–2), the exponent e of the axis's scale 2^−e (3–5), the factor a difference
|
|
27
|
+
* along the axis is multiplied by (6–8: 2^−e, or 2^(1−e) where the halves are
|
|
28
|
+
* subtracted), 1 when some extent is so wide that a difference of two
|
|
29
|
+
* coordinates may overflow (9), and a second factor where the first alone would
|
|
30
|
+
* leave the doubles (10–12, otherwise 1).
|
|
31
|
+
*/
|
|
32
|
+
const FRAME_ = new Float64Array(13);
|
|
33
|
+
/** A corner relative to the anchor, scaled: x, y, z, then their low parts. */
|
|
34
|
+
const VERTEX_ = new Float64Array(6);
|
|
35
|
+
/**
|
|
36
|
+
* One face's doubled area as `[m, k]`, the area being m · 2^k: k is 0 when m is
|
|
37
|
+
* the area itself, otherwise m is between 1 and 4 (0 for a degenerate face,
|
|
38
|
+
* `NaN` for a non-finite one).
|
|
39
|
+
*/
|
|
40
|
+
const FACE_AREA_ = new Float64Array(2);
|
|
41
|
+
/** One face's cross product `[x, y, z, k]`, the product being (x, y, z) · 2^k. */
|
|
42
|
+
const FACE_CROSS_ = new Float64Array(4);
|
|
43
|
+
/** A double computed and scaled in place, never boxed by a call. */
|
|
44
|
+
const VALUE_ = new Float64Array(1);
|
|
45
|
+
/** A face's scaled edges `b − a` and `c − a`. */
|
|
46
|
+
const EDGES_ = new Float64Array(6);
|
|
47
|
+
/** The exponents bounding a face's `a − o`, `b − a` and `c − a`. */
|
|
48
|
+
const BOUNDS_ = new Float64Array(3);
|
|
49
|
+
/** Two numbers TwoSum replaces by their sum and its error. */
|
|
50
|
+
const PAIR_ = new Float64Array(2);
|
|
51
|
+
/** The volume filter's operands, TwoSum and TwoProduct pairs in place. */
|
|
52
|
+
const FILTER_ = new Float64Array(36);
|
|
53
|
+
const CROSS_ = new Float64Array(12);
|
|
54
|
+
const TERMS_ = new Float64Array(48);
|
|
55
|
+
/**
|
|
56
|
+
* The accuracy a vertex's sum must be certified to: each component's bound
|
|
57
|
+
* within 2^−50 of the largest component, so that the unit normal stays within
|
|
58
|
+
* 2^−48 of the exact sum's direction.
|
|
59
|
+
*/
|
|
60
|
+
const VERTEX_ACCURACY_ = 2 ** -50;
|
|
61
|
+
/**
|
|
62
|
+
* The bound, in units of u, on what a scaled product loses underflowing (each
|
|
63
|
+
* term below 2^−1070), far above it.
|
|
64
|
+
*/
|
|
65
|
+
const SCALED_TERM_BOUND_ = 2 ** -1000;
|
|
66
|
+
/** The least exponent of a term a `DotExactAccumulator` holds. */
|
|
67
|
+
const EXACT_TERM_MIN_EXPONENT_ = -2166;
|
|
68
|
+
/** The exact sums of a vertex's components. */
|
|
69
|
+
const EXACT_SUMS_ = [
|
|
70
|
+
new DotExactAccumulator(),
|
|
71
|
+
new DotExactAccumulator(),
|
|
72
|
+
new DotExactAccumulator(),
|
|
73
|
+
];
|
|
74
|
+
/** A term added to an exact sum, as a one-product dot product. */
|
|
75
|
+
const TERM_ = new Float64Array(1);
|
|
76
|
+
const ONE_ = Float64Array.of(1);
|
|
77
|
+
/** An exact sum, as significand and exponent. */
|
|
78
|
+
const SCALED_ = new Float64Array(2);
|
|
79
|
+
/** A vertex's exact sum, each `SUM_COMPONENTS_[i] · 2^SUM_EXPONENTS_[i]`. */
|
|
80
|
+
const SUM_COMPONENTS_ = new Float64Array(3);
|
|
81
|
+
const SUM_EXPONENTS_ = new Int32Array(3);
|
|
82
|
+
/** A vertex's exact sum as `[x, y, z, k]`. */
|
|
83
|
+
const VERTEX_SUM_ = new Float64Array(4);
|
|
10
84
|
/**
|
|
11
85
|
* The `Uint16Array` twin of the area-weighted vertex normals: the public
|
|
12
86
|
* function checks the lengths and dispatches on the index type once. Writes
|
|
13
|
-
* `positions.length` numbers into `out`.
|
|
87
|
+
* `positions.length` numbers into `out`. Each face adds its cross product,
|
|
88
|
+
* twice its area times its unit normal (`faceCross_`: within 2^−49 of its
|
|
89
|
+
* largest component, zero only for a degenerate face). While every face's
|
|
90
|
+
* product is plain (k = 0, its largest component in [2^−400, 2^402)), the sums
|
|
91
|
+
* are those products' as they come; otherwise a first pass finds, per vertex,
|
|
92
|
+
* the largest product among its faces, and each face adds its product scaled by
|
|
93
|
+
* the power of two that brings that vertex's largest to [1, 2): the weights
|
|
94
|
+
* keep their ratios, nothing overflows, and a vertex whose faces are all tiny
|
|
95
|
+
* beside other vertices' still gets its normal. Each sum keeps a running bound
|
|
96
|
+
* on its rounding errors (Higham, "Accuracy and Stability of Numerical
|
|
97
|
+
* Algorithms", §4.2: within u/(1 − u) of the partial sums' magnitudes, u =
|
|
98
|
+
* 2^−53); a vertex whose bound is not within 2^−50 of its largest component
|
|
99
|
+
* (its faces cancel, or it has very many) is summed again exactly
|
|
100
|
+
* (`exactVertexSums_`). O(vertices + triangles); no allocation once the
|
|
101
|
+
* per-vertex scratch has grown to the largest mesh.
|
|
14
102
|
*/
|
|
15
103
|
export function mesh3VertexNormalsU16(out, positions, indices) {
|
|
104
|
+
const cross = FACE_CROSS_;
|
|
105
|
+
const vertexCount = out.length / 3;
|
|
106
|
+
if (vertexErrorsU16_.length < vertexCount) {
|
|
107
|
+
vertexErrorsU16_ = new Float64Array(Math.max(vertexCount, 2 * vertexErrorsU16_.length));
|
|
108
|
+
}
|
|
109
|
+
const errors = vertexErrorsU16_;
|
|
110
|
+
errors.fill(0, 0, vertexCount);
|
|
16
111
|
out.fill(0);
|
|
112
|
+
let isPlain = true;
|
|
17
113
|
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const crossX =
|
|
30
|
-
const crossY =
|
|
31
|
-
const crossZ =
|
|
32
|
-
// An index past the end reads NaN and writes nothing
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
out[
|
|
38
|
-
out[
|
|
39
|
-
out[
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
114
|
+
const vertexA = indices[corner];
|
|
115
|
+
const vertexB = indices[corner + 1];
|
|
116
|
+
const vertexC = indices[corner + 2];
|
|
117
|
+
const a = vertexA * 3;
|
|
118
|
+
const b = vertexB * 3;
|
|
119
|
+
const c = vertexC * 3;
|
|
120
|
+
faceCrossU16_(positions, a, b, c);
|
|
121
|
+
if (cross[3] !== 0) {
|
|
122
|
+
isPlain = false;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
const crossX = cross[0];
|
|
126
|
+
const crossY = cross[1];
|
|
127
|
+
const crossZ = cross[2];
|
|
128
|
+
// An index past the end reads NaN and writes nothing (or NaN in the
|
|
129
|
+
// scratch's unused tail).
|
|
130
|
+
const sumAX = out[a] + crossX;
|
|
131
|
+
const sumAY = out[a + 1] + crossY;
|
|
132
|
+
const sumAZ = out[a + 2] + crossZ;
|
|
133
|
+
out[a] = sumAX;
|
|
134
|
+
out[a + 1] = sumAY;
|
|
135
|
+
out[a + 2] = sumAZ;
|
|
136
|
+
errors[vertexA] =
|
|
137
|
+
errors[vertexA] +
|
|
138
|
+
Math.max(Math.abs(sumAX), Math.abs(sumAY), Math.abs(sumAZ));
|
|
139
|
+
const sumBX = out[b] + crossX;
|
|
140
|
+
const sumBY = out[b + 1] + crossY;
|
|
141
|
+
const sumBZ = out[b + 2] + crossZ;
|
|
142
|
+
out[b] = sumBX;
|
|
143
|
+
out[b + 1] = sumBY;
|
|
144
|
+
out[b + 2] = sumBZ;
|
|
145
|
+
errors[vertexB] =
|
|
146
|
+
errors[vertexB] +
|
|
147
|
+
Math.max(Math.abs(sumBX), Math.abs(sumBY), Math.abs(sumBZ));
|
|
148
|
+
const sumCX = out[c] + crossX;
|
|
149
|
+
const sumCY = out[c + 1] + crossY;
|
|
150
|
+
const sumCZ = out[c + 2] + crossZ;
|
|
151
|
+
out[c] = sumCX;
|
|
152
|
+
out[c + 1] = sumCY;
|
|
153
|
+
out[c + 2] = sumCZ;
|
|
154
|
+
errors[vertexC] =
|
|
155
|
+
errors[vertexC] +
|
|
156
|
+
Math.max(Math.abs(sumCX), Math.abs(sumCY), Math.abs(sumCZ));
|
|
157
|
+
}
|
|
158
|
+
if (!isPlain) {
|
|
159
|
+
scaledVertexSumsU16_(out, positions, indices);
|
|
160
|
+
}
|
|
161
|
+
// u / (1 − u) per partial sum, widened by 4nu for the bound's own
|
|
162
|
+
// roundings over the n numbers it adds.
|
|
163
|
+
const factor = 1.0001 * UNIT_ROUNDOFF_ * (1 + 4 * indices.length * UNIT_ROUNDOFF_);
|
|
164
|
+
let uncertain = 0;
|
|
165
|
+
for (let vertex = 0; vertex < vertexCount; vertex++) {
|
|
166
|
+
const at = 3 * vertex;
|
|
167
|
+
const largest = Math.max(Math.abs(out[at]), Math.abs(out[at + 1]), Math.abs(out[at + 2]));
|
|
168
|
+
if (errors[vertex] * factor > largest * VERTEX_ACCURACY_) {
|
|
169
|
+
errors[vertex] = -1;
|
|
170
|
+
uncertain++;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
normalizeVertex_(out, at);
|
|
174
|
+
}
|
|
175
|
+
if (uncertain > 0) {
|
|
176
|
+
exactVertexSumsU16_(out, positions, indices);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Per vertex, the sum of the largest magnitudes of {@link mesh3VertexNormals}'s
|
|
181
|
+
* partial sums, the bound on every component's rounding errors in units of u,
|
|
182
|
+
* then −1 where that bound is too wide: grown to the largest mesh seen, then
|
|
183
|
+
* reused.
|
|
184
|
+
*/
|
|
185
|
+
let vertexErrorsU16_ = new Float64Array(0);
|
|
186
|
+
/**
|
|
187
|
+
* Per vertex, the exponent of the largest cross product among its faces, for
|
|
188
|
+
* {@link scaledVertexSums_}: grown to the largest mesh seen, then reused.
|
|
189
|
+
*/
|
|
190
|
+
let vertexExponentsU16_ = new Int32Array(0);
|
|
191
|
+
/**
|
|
192
|
+
* Rewrites `out` with each vertex's sum of its faces' cross products, every
|
|
193
|
+
* product scaled by the power of two that brings the vertex's largest to [1,
|
|
194
|
+
* 2): the same direction as the plain sum, with no product that overflows or
|
|
195
|
+
* underflows on the way. Rewrites `vertexErrors_` with the bound on the sums'
|
|
196
|
+
* roundings, an absolute 2^−1000 per term added for what a scaled product loses
|
|
197
|
+
* underflowing (each is below 2^−1070).
|
|
198
|
+
*/
|
|
199
|
+
function scaledVertexSumsU16_(out, positions, indices) {
|
|
200
|
+
const cross = FACE_CROSS_;
|
|
201
|
+
const vertexCount = out.length / 3;
|
|
202
|
+
if (vertexExponentsU16_.length < vertexCount) {
|
|
203
|
+
vertexExponentsU16_ = new Int32Array(Math.max(vertexCount, 2 * vertexExponentsU16_.length));
|
|
204
|
+
}
|
|
205
|
+
const exponents = vertexExponentsU16_;
|
|
206
|
+
const errors = vertexErrorsU16_;
|
|
207
|
+
exponents.fill(NONE_, 0, vertexCount);
|
|
208
|
+
for (let face = 0; face < indices.length; face += 3) {
|
|
209
|
+
faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
|
|
210
|
+
const largest = Math.max(Math.abs(cross[0]), Math.abs(cross[1]), Math.abs(cross[2]));
|
|
211
|
+
if (largest > 0 && largest < Number.POSITIVE_INFINITY) {
|
|
212
|
+
const exponent = cross[3] + binaryExponent(largest);
|
|
213
|
+
for (let corner = face; corner < face + 3; corner++) {
|
|
214
|
+
// A corner past the last vertex reads and writes nothing.
|
|
215
|
+
const vertex = indices[corner];
|
|
216
|
+
if (vertex < vertexCount) {
|
|
217
|
+
exponents[vertex] = Math.max(exponents[vertex], exponent);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
out.fill(0);
|
|
223
|
+
errors.fill(0, 0, vertexCount);
|
|
224
|
+
for (let face = 0; face < indices.length; face += 3) {
|
|
225
|
+
faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
|
|
226
|
+
const largest = Math.max(Math.abs(cross[0]), Math.abs(cross[1]), Math.abs(cross[2]));
|
|
227
|
+
if (largest === 0) {
|
|
53
228
|
continue;
|
|
54
229
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
230
|
+
for (let corner = face; corner < face + 3; corner++) {
|
|
231
|
+
const vertex = indices[corner];
|
|
232
|
+
if (vertex >= vertexCount) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
// A NaN product stays NaN; a product 2^1080 below the vertex's largest
|
|
236
|
+
// weighs nothing beside it.
|
|
237
|
+
const factor = largest < Number.POSITIVE_INFINITY
|
|
238
|
+
? 2 ** Math.max(cross[3] - exponents[vertex], -1080)
|
|
239
|
+
: 1;
|
|
240
|
+
const at = vertex * 3;
|
|
241
|
+
out[at] = out[at] + cross[0] * factor;
|
|
242
|
+
out[at + 1] = out[at + 1] + cross[1] * factor;
|
|
243
|
+
out[at + 2] = out[at + 2] + cross[2] * factor;
|
|
244
|
+
errors[vertex] =
|
|
245
|
+
errors[vertex] +
|
|
246
|
+
Math.max(Math.abs(out[at]), Math.abs(out[at + 1]), Math.abs(out[at + 2])) +
|
|
247
|
+
SCALED_TERM_BOUND_;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Per vertex, where its faces start in `vertexFaces_`, for
|
|
253
|
+
* {@link exactVertexSums_}: grown to the largest mesh seen, then reused.
|
|
254
|
+
*/
|
|
255
|
+
let vertexStartsU16_ = new Int32Array(0);
|
|
256
|
+
/** The faces (their first corner) of each vertex summed exactly, in turn. */
|
|
257
|
+
let vertexFacesU16_ = new Int32Array(0);
|
|
258
|
+
/**
|
|
259
|
+
* Rewrites, at each vertex `vertexErrors_` marks with −1, the unit normal of
|
|
260
|
+
* the exact sum of its faces' cross products (`faceCross_`'s, a face counted
|
|
261
|
+
* once per corner it has there), each component summed by a long accumulator
|
|
262
|
+
* and rounded once (`DotExactAccumulator`), at any exponent; the zero vector
|
|
263
|
+
* where that sum is zero. A component below 2^−2166, which no accumulator
|
|
264
|
+
* holds, is 2^1040 below its face's largest and well within that product's own
|
|
265
|
+
* bound, and is left out. The faces of those vertices are listed first
|
|
266
|
+
* (counted, then placed), so each is read once per corner at a marked vertex.
|
|
267
|
+
*/
|
|
268
|
+
function exactVertexSumsU16_(out, positions, indices) {
|
|
269
|
+
const vertexCount = out.length / 3;
|
|
270
|
+
const errors = vertexErrorsU16_;
|
|
271
|
+
if (vertexStartsU16_.length < vertexCount + 2) {
|
|
272
|
+
vertexStartsU16_ = new Int32Array(Math.max(vertexCount + 2, 2 * vertexStartsU16_.length));
|
|
273
|
+
}
|
|
274
|
+
const starts = vertexStartsU16_;
|
|
275
|
+
starts.fill(0, 0, vertexCount + 2);
|
|
276
|
+
for (let corner = 0; corner < indices.length; corner++) {
|
|
277
|
+
const vertex = indices[corner];
|
|
278
|
+
if (vertex < vertexCount && errors[vertex] === -1) {
|
|
279
|
+
starts[vertex + 1] = starts[vertex + 1] + 1;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Each vertex's end, then, placing its faces backwards, its start.
|
|
283
|
+
for (let vertex = 0; vertex < vertexCount; vertex++) {
|
|
284
|
+
starts[vertex + 1] = starts[vertex + 1] + starts[vertex];
|
|
285
|
+
}
|
|
286
|
+
const total = starts[vertexCount];
|
|
287
|
+
if (vertexFacesU16_.length < total) {
|
|
288
|
+
vertexFacesU16_ = new Int32Array(Math.max(total, 2 * vertexFacesU16_.length));
|
|
289
|
+
}
|
|
290
|
+
const faces = vertexFacesU16_;
|
|
291
|
+
for (let corner = 0; corner < indices.length; corner++) {
|
|
292
|
+
const vertex = indices[corner];
|
|
293
|
+
if (vertex < vertexCount && errors[vertex] === -1) {
|
|
294
|
+
const at = starts[vertex + 1] - 1;
|
|
295
|
+
starts[vertex + 1] = at;
|
|
296
|
+
faces[at] = corner - (corner % 3);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// Now vertex v's faces are faces[starts[v + 1]] to faces[starts[v + 2]].
|
|
300
|
+
starts[vertexCount + 1] = total;
|
|
301
|
+
const cross = FACE_CROSS_;
|
|
302
|
+
for (let vertex = 0; vertex < vertexCount; vertex++) {
|
|
303
|
+
if (errors[vertex] !== -1) {
|
|
58
304
|
continue;
|
|
59
305
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
306
|
+
EXACT_SUMS_[0].reset();
|
|
307
|
+
EXACT_SUMS_[1].reset();
|
|
308
|
+
EXACT_SUMS_[2].reset();
|
|
309
|
+
const end = starts[vertex + 2];
|
|
310
|
+
for (let at = starts[vertex + 1]; at < end; at++) {
|
|
311
|
+
const face = faces[at];
|
|
312
|
+
faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
|
|
313
|
+
const exponent = cross[3];
|
|
314
|
+
for (let component = 0; component < 3; component++) {
|
|
315
|
+
const value = cross[component];
|
|
316
|
+
if (value !== 0 &&
|
|
317
|
+
binaryExponent(value) + exponent >= EXACT_TERM_MIN_EXPONENT_) {
|
|
318
|
+
TERM_[0] = value;
|
|
319
|
+
EXACT_SUMS_[component].addProducts(TERM_, ONE_, exponent);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
for (let component = 0; component < 3; component++) {
|
|
324
|
+
EXACT_SUMS_[component].writeScaled(SCALED_, 0);
|
|
325
|
+
SUM_COMPONENTS_[component] = SCALED_[0];
|
|
326
|
+
SUM_EXPONENTS_[component] = SCALED_[1];
|
|
327
|
+
}
|
|
328
|
+
writeScaledComponents_(VERTEX_SUM_, SUM_COMPONENTS_, SUM_EXPONENTS_);
|
|
329
|
+
const at = 3 * vertex;
|
|
330
|
+
out[at] = VERTEX_SUM_[0];
|
|
331
|
+
out[at + 1] = VERTEX_SUM_[1];
|
|
332
|
+
out[at + 2] = VERTEX_SUM_[2];
|
|
333
|
+
normalizeVertex_(out, at);
|
|
67
334
|
}
|
|
68
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Replaces the vector at `out[at]` by its direction: unchanged when it is zero,
|
|
338
|
+
* `NaN`s when a component is.
|
|
339
|
+
*/
|
|
340
|
+
function normalizeVertex_(out, at) {
|
|
341
|
+
const x = out[at];
|
|
342
|
+
const y = out[at + 1];
|
|
343
|
+
const z = out[at + 2];
|
|
344
|
+
const length = Math.sqrt(x * x + y * y + z * z);
|
|
345
|
+
if (length > 0 && length < Number.POSITIVE_INFINITY) {
|
|
346
|
+
const inverse = 1 / length;
|
|
347
|
+
out[at] = x * inverse;
|
|
348
|
+
out[at + 1] = y * inverse;
|
|
349
|
+
out[at + 2] = z * inverse;
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
// The squares underflowed or overflowed, or the vertex has no normal.
|
|
353
|
+
const scale = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
|
|
354
|
+
if (scale === 0) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const scaledX = x / scale;
|
|
358
|
+
const scaledY = y / scale;
|
|
359
|
+
const scaledZ = z / scale;
|
|
360
|
+
const scaledLength = Math.sqrt(scaledX * scaledX + scaledY * scaledY + scaledZ * scaledZ);
|
|
361
|
+
out[at] = scaledX / scaledLength;
|
|
362
|
+
out[at + 1] = scaledY / scaledLength;
|
|
363
|
+
out[at + 2] = scaledZ / scaledLength;
|
|
364
|
+
}
|
|
69
365
|
/**
|
|
70
366
|
* The `Uint16Array` twin of the face normals: one unit normal per triangle, the
|
|
71
367
|
* zero vector for a degenerate one. O(triangles).
|
|
@@ -90,104 +386,555 @@ export function mesh3FaceNormalsU16(out, positions, indices) {
|
|
|
90
386
|
out[corner + 2] = NORMAL_[2];
|
|
91
387
|
}
|
|
92
388
|
}
|
|
93
|
-
/**
|
|
389
|
+
/** An exponent below every one a face can have: "no difference". */
|
|
390
|
+
const NONE_ = -4096;
|
|
391
|
+
/**
|
|
392
|
+
* Fills `FRAME_` for the triangles of `indices`. The anchor is the least x, y
|
|
393
|
+
* and z of the vertices they use: a corner of their bounding box, the same in
|
|
394
|
+
* every face order, and a vertex no triangle uses is never read. Each axis has
|
|
395
|
+
* its own e, 2^e above the box's extent along it (0 for a flat or non-finite
|
|
396
|
+
* one), at least −1021 so that 2^−e is a double: a corner relative to the
|
|
397
|
+
* anchor, scaled by its axis's 2^−e, lies in [0, 1) on every axis however thin
|
|
398
|
+
* the box is along the others. When an extent reaches 2^1023, so that a
|
|
399
|
+
* difference of two coordinates may overflow, every difference subtracts
|
|
400
|
+
* halves, which is exact for normal coordinates.
|
|
401
|
+
*/
|
|
402
|
+
function frameU16_(positions, indices) {
|
|
403
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
404
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
405
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
406
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
407
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
408
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
409
|
+
for (let corner = 0; corner < indices.length; corner++) {
|
|
410
|
+
const vertex = indices[corner] * 3;
|
|
411
|
+
const x = positions[vertex];
|
|
412
|
+
const y = positions[vertex + 1];
|
|
413
|
+
const z = positions[vertex + 2];
|
|
414
|
+
minX = Math.min(minX, x);
|
|
415
|
+
minY = Math.min(minY, y);
|
|
416
|
+
minZ = Math.min(minZ, z);
|
|
417
|
+
maxX = Math.max(maxX, x);
|
|
418
|
+
maxY = Math.max(maxY, y);
|
|
419
|
+
maxZ = Math.max(maxZ, z);
|
|
420
|
+
}
|
|
421
|
+
// Halves, so that the extent of a box across ±Number.MAX_VALUE is finite.
|
|
422
|
+
const halfX = maxX * 0.5 - minX * 0.5;
|
|
423
|
+
const halfY = maxY * 0.5 - minY * 0.5;
|
|
424
|
+
const halfZ = maxZ * 0.5 - minZ * 0.5;
|
|
425
|
+
const frame = FRAME_;
|
|
426
|
+
frame[0] = minX;
|
|
427
|
+
frame[1] = minY;
|
|
428
|
+
frame[2] = minZ;
|
|
429
|
+
frame[3] = halfX;
|
|
430
|
+
frame[4] = halfY;
|
|
431
|
+
frame[5] = halfZ;
|
|
432
|
+
frame[9] =
|
|
433
|
+
halfX >= 2 ** 1022 || halfY >= 2 ** 1022 || halfZ >= 2 ** 1022 ? 1 : 0;
|
|
434
|
+
for (let axis = 3; axis < 6; axis++) {
|
|
435
|
+
// The half extent becomes e: 2^e is above the extent.
|
|
436
|
+
const half = frame[axis];
|
|
437
|
+
frame[axis] =
|
|
438
|
+
half > 0 && half < Number.POSITIVE_INFINITY
|
|
439
|
+
? Math.max(binaryExponent(half) + 2, -1021)
|
|
440
|
+
: 0;
|
|
441
|
+
}
|
|
442
|
+
scaleFrameU16_(0);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Sets the factors of `FRAME_` to 2^−(e + extra) per axis, 2^(1 − e − extra)
|
|
446
|
+
* where halves are subtracted. `extra` is 0 or the volume's common rescaling,
|
|
447
|
+
* never so far below −e that the factor leaves the doubles: from −1020 to 0.
|
|
448
|
+
*/
|
|
449
|
+
function scaleFrameU16_(extra) {
|
|
450
|
+
const shift = FRAME_[9] === 1 ? 1 : 0;
|
|
451
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
452
|
+
// Beyond 2^1023 the factor is two: only an axis whose differences are
|
|
453
|
+
// all below 1 gets there, and those stay finite after the first.
|
|
454
|
+
const exponent = shift - FRAME_[3 + axis] - extra;
|
|
455
|
+
FRAME_[6 + axis] = 2 ** Math.min(exponent, 1023);
|
|
456
|
+
FRAME_[10 + axis] = 2 ** Math.max(exponent - 1023, 0);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Writes the edges `b − a` (0–2) and `c − a` (3–5) of a face into `EDGES_`,
|
|
461
|
+
* scaled by the frame's factors: exact where the differences are (Sterbenz) and
|
|
462
|
+
* the results are not subnormal. When the frame is wide, `p − q` may overflow,
|
|
463
|
+
* so the halves are subtracted, which is exact for every normal `p` and `q`. No
|
|
464
|
+
* double crosses the call, so it allocates nothing, inlined or not.
|
|
465
|
+
*/
|
|
466
|
+
function scaledEdgesU16_(positions, a, b, c, isWide) {
|
|
467
|
+
const frame = FRAME_;
|
|
468
|
+
const edges = EDGES_;
|
|
469
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
470
|
+
const factor = frame[6 + axis];
|
|
471
|
+
const second = frame[10 + axis];
|
|
472
|
+
const p = positions[a + axis];
|
|
473
|
+
const q = positions[b + axis];
|
|
474
|
+
const r = positions[c + axis];
|
|
475
|
+
edges[axis] = (isWide ? q * 0.5 - p * 0.5 : q - p) * factor * second;
|
|
476
|
+
edges[axis + 3] = (isWide ? r * 0.5 - p * 0.5 : r - p) * factor * second;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Writes into `BOUNDS_` the exponents, as {@link binaryExponent} reads them, of
|
|
481
|
+
* the largest component of `a − o`, `b − a` and `c − a` scaled into the frame
|
|
482
|
+
* (`NONE_` for a zero or non-finite vector): each is below 2^(exponent + 1).
|
|
483
|
+
*/
|
|
484
|
+
function faceBoundsU16_(positions, a, b, c, isWide) {
|
|
485
|
+
const frame = FRAME_;
|
|
486
|
+
const bounds = BOUNDS_;
|
|
487
|
+
const shift = isWide ? 1 : 0;
|
|
488
|
+
bounds[0] = NONE_;
|
|
489
|
+
bounds[1] = NONE_;
|
|
490
|
+
bounds[2] = NONE_;
|
|
491
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
492
|
+
const p = positions[a + axis];
|
|
493
|
+
const anchored = isWide ? p * 0.5 - frame[axis] * 0.5 : p - frame[axis];
|
|
494
|
+
const ab = isWide
|
|
495
|
+
? positions[b + axis] * 0.5 - p * 0.5
|
|
496
|
+
: positions[b + axis] - p;
|
|
497
|
+
const ac = isWide
|
|
498
|
+
? positions[c + axis] * 0.5 - p * 0.5
|
|
499
|
+
: positions[c + axis] - p;
|
|
500
|
+
const offset = shift - frame[3 + axis];
|
|
501
|
+
if (anchored !== 0 && anchored - anchored === 0) {
|
|
502
|
+
bounds[0] = Math.max(bounds[0], binaryExponent(anchored) + offset);
|
|
503
|
+
}
|
|
504
|
+
if (ab !== 0 && ab - ab === 0) {
|
|
505
|
+
bounds[1] = Math.max(bounds[1], binaryExponent(ab) + offset);
|
|
506
|
+
}
|
|
507
|
+
if (ac !== 0 && ac - ac === 0) {
|
|
508
|
+
bounds[2] = Math.max(bounds[2], binaryExponent(ac) + offset);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Writes the corner at `vertex` relative to the anchor, scaled by the frame's
|
|
514
|
+
* factors, into `VERTEX_` as high parts (0–2) and the low parts TwoSum recovers
|
|
515
|
+
* (3–5): their sum is the scaled difference exactly, wherever it is not
|
|
516
|
+
* subnormal.
|
|
517
|
+
*/
|
|
518
|
+
function anchoredCornerU16_(positions, vertex, isWide) {
|
|
519
|
+
const pair = PAIR_;
|
|
520
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
521
|
+
const factor = FRAME_[6 + axis];
|
|
522
|
+
const second = FRAME_[10 + axis];
|
|
523
|
+
pair[0] = isWide
|
|
524
|
+
? positions[vertex + axis] * 0.5
|
|
525
|
+
: positions[vertex + axis];
|
|
526
|
+
pair[1] = isWide ? -(FRAME_[axis] * 0.5) : -FRAME_[axis];
|
|
527
|
+
twoSumInPlace(pair, 0);
|
|
528
|
+
VERTEX_[axis] = pair[0] * factor * second;
|
|
529
|
+
VERTEX_[axis + 3] = pair[1] * factor * second;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Writes the cross product `(b − a) × (c − a)` of the triangle at `a`, `b` and
|
|
534
|
+
* `c` into `FACE_CROSS_` as `[x, y, z, k]`, the true product being `(x, y, z) ·
|
|
535
|
+
* 2^k` (`triangle3Cross_`): each component within 2^−49 of the largest of the
|
|
536
|
+
* exact product, with its sign wherever that bound cannot reach 0, for any
|
|
537
|
+
* finite corners, zeros exactly for a degenerate face and `NaN`s for a
|
|
538
|
+
* non-finite one. k is 0 while the largest component lies in [2^−400, 2^402);
|
|
539
|
+
* otherwise the largest lies in [1, 2).
|
|
540
|
+
*/
|
|
541
|
+
function faceCrossU16_(positions, a, b, c) {
|
|
542
|
+
const corners = CORNERS_;
|
|
543
|
+
corners[0] = +positions[a];
|
|
544
|
+
corners[1] = +positions[a + 1];
|
|
545
|
+
corners[2] = +positions[a + 2];
|
|
546
|
+
corners[3] = +positions[b];
|
|
547
|
+
corners[4] = +positions[b + 1];
|
|
548
|
+
corners[5] = +positions[b + 2];
|
|
549
|
+
corners[6] = +positions[c];
|
|
550
|
+
corners[7] = +positions[c + 1];
|
|
551
|
+
corners[8] = +positions[c + 2];
|
|
552
|
+
triangle3Cross_(FACE_CROSS_, corners);
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Writes the doubled area of the triangle at `a`, `b` and `c` into `FACE_AREA_`
|
|
556
|
+
* as `[m, k]`, the area being m · 2^k: the length of {@link faceCross_}'s
|
|
557
|
+
* product, whose components never square out of range.
|
|
558
|
+
*/
|
|
559
|
+
function faceAreaU16_(positions, a, b, c) {
|
|
560
|
+
faceCrossU16_(positions, a, b, c);
|
|
561
|
+
const cross = FACE_CROSS_;
|
|
562
|
+
const x = cross[0];
|
|
563
|
+
const y = cross[1];
|
|
564
|
+
const z = cross[2];
|
|
565
|
+
FACE_AREA_[0] = Math.sqrt(x * x + y * y + z * z);
|
|
566
|
+
FACE_AREA_[1] = cross[3];
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* The `Uint16Array` twin of the surface area: each face's area from its edges
|
|
570
|
+
* and cross product scaled by powers of two (`faceArea_`), so no square
|
|
571
|
+
* overflows or underflows at any size or distance from the origin, rounded once
|
|
572
|
+
* per face and summed exactly: the same number in every face order.
|
|
573
|
+
* O(triangles), no allocation.
|
|
574
|
+
*/
|
|
94
575
|
export function mesh3SurfaceAreaU16(positions, indices) {
|
|
95
|
-
|
|
576
|
+
const sum = AREA_;
|
|
577
|
+
const area = FACE_AREA_;
|
|
578
|
+
const value = VALUE_;
|
|
579
|
+
sum.reset();
|
|
580
|
+
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
581
|
+
faceAreaU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3);
|
|
582
|
+
if (area[0] === 0) {
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
value[0] = area[0];
|
|
586
|
+
if (area[1] === 0) {
|
|
587
|
+
value[0] = area[0] * 0.5;
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
scaleByPowerOfTwoInPlace(value, 0, area[1] - 1);
|
|
591
|
+
}
|
|
592
|
+
sum.addRange(value, 0, 1);
|
|
593
|
+
}
|
|
594
|
+
sum.writeValue(value, 0);
|
|
595
|
+
// `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
|
|
596
|
+
return value[0] + 0;
|
|
597
|
+
}
|
|
598
|
+
/** The unit roundoff, 2^−53. */
|
|
599
|
+
const UNIT_ROUNDOFF_ = 2 ** -53;
|
|
600
|
+
/**
|
|
601
|
+
* The adaptive filter of {@link mesh3Volume}: six times the volume, over the
|
|
602
|
+
* same terms as the exact sum (`a − o` exactly as TwoSum's pair, the edges as
|
|
603
|
+
* rounded), in doubled precision. Each cross component is the difference of two
|
|
604
|
+
* TwoProducts with their errors; each term's high products are TwoProducts too
|
|
605
|
+
* and summed by TwoSum, and every remaining part goes into one plain sum. What
|
|
606
|
+
* that drops is bounded rigorously: per face, below 12u² Σ |a − o|ₖ (|p|
|
|
607
|
+
*
|
|
608
|
+
* - |q|)ₖ over the products p, q of each cross component (u = 2^−53, the
|
|
609
|
+
* neglected low-by-low products and the roundings of the low parts), plus the
|
|
610
|
+
* plain sum's rounding, γ_N Σ |parts| for its N parts (Higham, "Accuracy and
|
|
611
|
+
* Stability of Numerical Algorithms", §4.2), plus an absolute 2^−1066 per
|
|
612
|
+
* face for products in the subnormal range. When the rounded total r is
|
|
613
|
+
* normal and its distance to the double sum, plus that bound, stays short of
|
|
614
|
+
* the midpoint on that side (half its unit in the last place, a quarter below
|
|
615
|
+
* a power of two), the exact value rounds to r: written to `VALUE_[0]`, and
|
|
616
|
+
* `true`. Otherwise `false`, and the exact sum decides. The frame must be set
|
|
617
|
+
* and no extent may reach 2^300, so no product overflows.
|
|
618
|
+
*/
|
|
619
|
+
function certifiedSixVolumeU16_(positions, indices) {
|
|
620
|
+
// Every TwoSum and TwoProduct runs in place on this buffer: no double
|
|
621
|
+
// crosses a call, so nothing is boxed whether or not the engine inlines them.
|
|
622
|
+
const filter = FILTER_;
|
|
623
|
+
let high = 0;
|
|
624
|
+
let low = 0;
|
|
625
|
+
let lowMagnitude = 0;
|
|
626
|
+
let magnitude = 0;
|
|
96
627
|
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
97
628
|
const a = indices[corner] * 3;
|
|
98
629
|
const b = indices[corner + 1] * 3;
|
|
99
630
|
const c = indices[corner + 2] * 3;
|
|
631
|
+
// a − o as TwoSum's pairs, at 0–5.
|
|
632
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
633
|
+
filter[2 * axis] = positions[a + axis];
|
|
634
|
+
filter[2 * axis + 1] = -FRAME_[axis];
|
|
635
|
+
twoSumInPlace(filter, 2 * axis);
|
|
636
|
+
}
|
|
100
637
|
const abX = positions[b] - positions[a];
|
|
101
638
|
const abY = positions[b + 1] - positions[a + 1];
|
|
102
639
|
const abZ = positions[b + 2] - positions[a + 2];
|
|
103
640
|
const acX = positions[c] - positions[a];
|
|
104
641
|
const acY = positions[c + 1] - positions[a + 1];
|
|
105
642
|
const acZ = positions[c + 2] - positions[a + 2];
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
643
|
+
// Cross component k is p − q, p at 6 + 4k and q at 8 + 4k, each with its
|
|
644
|
+
// TwoProduct error after it.
|
|
645
|
+
filter[6] = abY;
|
|
646
|
+
filter[7] = acZ;
|
|
647
|
+
filter[8] = abZ;
|
|
648
|
+
filter[9] = acY;
|
|
649
|
+
filter[10] = abZ;
|
|
650
|
+
filter[11] = acX;
|
|
651
|
+
filter[12] = abX;
|
|
652
|
+
filter[13] = acZ;
|
|
653
|
+
filter[14] = abX;
|
|
654
|
+
filter[15] = acY;
|
|
655
|
+
filter[16] = abY;
|
|
656
|
+
filter[17] = acX;
|
|
657
|
+
// One call site for all nine TwoProducts, so the engine inlines it once:
|
|
658
|
+
// the six of the cross components first, then the three of the terms
|
|
659
|
+
// (a − o)ₖ · crossₖ at 24–29, with each cross component's low part at
|
|
660
|
+
// 30 + k and the component itself at 33 + k.
|
|
661
|
+
let end = 18;
|
|
662
|
+
for (let phase = 0; phase < 2; phase++) {
|
|
663
|
+
for (let at = phase === 0 ? 6 : 24; at < end; at += 2) {
|
|
664
|
+
twoProductInRangeInPlace(filter, at);
|
|
665
|
+
}
|
|
666
|
+
if (phase === 1) {
|
|
667
|
+
break;
|
|
668
|
+
}
|
|
669
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
670
|
+
const first = filter[6 + 4 * axis];
|
|
671
|
+
const second = filter[8 + 4 * axis];
|
|
672
|
+
filter[18] = first;
|
|
673
|
+
filter[19] = -second;
|
|
674
|
+
twoSumInPlace(filter, 18);
|
|
675
|
+
const anchored = filter[2 * axis];
|
|
676
|
+
filter[30 + axis] =
|
|
677
|
+
filter[19] + filter[7 + 4 * axis] - filter[9 + 4 * axis];
|
|
678
|
+
filter[33 + axis] = filter[18];
|
|
679
|
+
filter[24 + 2 * axis] = anchored;
|
|
680
|
+
filter[25 + 2 * axis] = filter[18];
|
|
681
|
+
magnitude += Math.abs(anchored) * (Math.abs(first) + Math.abs(second));
|
|
682
|
+
}
|
|
683
|
+
end = 30;
|
|
684
|
+
}
|
|
685
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
686
|
+
filter[22] = high;
|
|
687
|
+
filter[23] = filter[24 + 2 * axis];
|
|
688
|
+
twoSumInPlace(filter, 22);
|
|
689
|
+
high = filter[22];
|
|
690
|
+
const carry = filter[23];
|
|
691
|
+
const error = filter[25 + 2 * axis];
|
|
692
|
+
const mixed = filter[2 * axis] * filter[30 + axis];
|
|
693
|
+
const mixedLow = filter[2 * axis + 1] * filter[33 + axis];
|
|
694
|
+
low += carry + error + mixed + mixedLow;
|
|
695
|
+
lowMagnitude +=
|
|
696
|
+
Math.abs(carry) +
|
|
697
|
+
Math.abs(error) +
|
|
698
|
+
Math.abs(mixed) +
|
|
699
|
+
Math.abs(mixedLow);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
filter[22] = high;
|
|
703
|
+
filter[23] = low;
|
|
704
|
+
filter[0] = magnitude;
|
|
705
|
+
filter[1] = lowMagnitude;
|
|
706
|
+
return certifySixVolumeU16_(indices.length);
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* The end of {@link certifiedSixVolume_}, from the sums it leaves in `FILTER_`
|
|
710
|
+
* (the high and low parts at 22 and 23, the magnitudes of the products and of
|
|
711
|
+
* the low parts at 0 and 1), for `cornerCount` corners: kept apart so that
|
|
712
|
+
* `binaryExponent` is inlined here whatever budget the accumulation spent (a
|
|
713
|
+
* double passed to a call the engine does not inline is boxed).
|
|
714
|
+
*/
|
|
715
|
+
function certifySixVolumeU16_(cornerCount) {
|
|
716
|
+
const filter = FILTER_;
|
|
717
|
+
const magnitude = filter[0];
|
|
718
|
+
const lowMagnitude = filter[1];
|
|
719
|
+
twoSumInPlace(filter, 22);
|
|
720
|
+
const total = filter[22];
|
|
721
|
+
const difference = filter[23];
|
|
722
|
+
// N parts: four per axis of each face, summed into one plain sum.
|
|
723
|
+
const parts = (cornerCount / 3) * 12 + 2;
|
|
724
|
+
const gamma = (parts * UNIT_ROUNDOFF_) / (1 - parts * UNIT_ROUNDOFF_);
|
|
725
|
+
if (!(gamma < 0.01)) {
|
|
726
|
+
return false;
|
|
110
727
|
}
|
|
111
|
-
|
|
728
|
+
const bound = 12 * UNIT_ROUNDOFF_ * UNIT_ROUNDOFF_ * magnitude * (1 + 2 * gamma) +
|
|
729
|
+
gamma * lowMagnitude * (1 + 2 * gamma) +
|
|
730
|
+
(cornerCount / 3) * 2 ** -1066;
|
|
731
|
+
const size = Math.abs(total);
|
|
732
|
+
if (!(size >= 2 ** -960 && size < Number.POSITIVE_INFINITY)) {
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
// The exact value lies within `bound` of total + difference; it rounds to
|
|
736
|
+
// `total` while that stays short of the midpoint on difference's side: half
|
|
737
|
+
// a unit in the last place, a quarter below a power of two.
|
|
738
|
+
const exponent = binaryExponent(total);
|
|
739
|
+
const isTowardZero = difference !== 0 && difference < 0 !== total < 0;
|
|
740
|
+
const isPowerOfTwo = size === 2 ** exponent;
|
|
741
|
+
const half = 2 ** (exponent - (isTowardZero && isPowerOfTwo ? 54 : 53));
|
|
742
|
+
if (!(Math.abs(difference) + bound < half)) {
|
|
743
|
+
return false;
|
|
744
|
+
}
|
|
745
|
+
VALUE_[0] = total;
|
|
746
|
+
return true;
|
|
112
747
|
}
|
|
113
748
|
/**
|
|
114
|
-
* The `Uint16Array` twin of the signed volume
|
|
115
|
-
*
|
|
116
|
-
*
|
|
749
|
+
* The `Uint16Array` twin of the signed volume: six times it is `Σ (a − o) · ((b
|
|
750
|
+
* − a) × (c − a))` over the faces, o being the frame's anchor (any point gives
|
|
751
|
+
* the same volume for a closed mesh). The sum is a determinant per face, so
|
|
752
|
+
* each axis may take its own power-of-two scale: the frame brings every axis's
|
|
753
|
+
* extent below 1, then a first pass over the faces bounds their terms and one
|
|
754
|
+
* common power of two brings the largest near 2^1000, so that no product
|
|
755
|
+
* overflows and small faces keep their digits, however thin, wide or far from
|
|
756
|
+
* the origin the mesh is. Each face's term is then written as exact products:
|
|
757
|
+
* `a − o` as TwoSum's pair, the cross product as an expansion (each component
|
|
758
|
+
* two products and their rounding errors, Ogita, Rump & Oishi's TwoProduct),
|
|
759
|
+
* and the whole summed exactly (`PreciseSum`). With the face-local edge
|
|
760
|
+
* differences exact, which they are wherever a face is small beside its
|
|
761
|
+
* distance from the origin (Sterbenz), the result is six times the volume
|
|
762
|
+
* rounded once, then divided by 6: the same number in every face order and at
|
|
763
|
+
* every offset, and a vertex no triangle uses is never read. A term below about
|
|
764
|
+
* 2^−1900 of the largest may lose digits to the subnormal range; so may a
|
|
765
|
+
* volume that small beside the largest term. O(triangles), no allocation.
|
|
117
766
|
*/
|
|
118
767
|
export function mesh3VolumeU16(positions, indices) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
768
|
+
const sum = SIX_VOLUMES_;
|
|
769
|
+
const vertex = VERTEX_;
|
|
770
|
+
const cross = CROSS_;
|
|
771
|
+
const terms = TERMS_;
|
|
772
|
+
const frame = FRAME_;
|
|
773
|
+
sum.reset();
|
|
774
|
+
frameU16_(positions, indices);
|
|
775
|
+
const isWide = frame[9] === 1;
|
|
776
|
+
// The filter: an ordinary mesh (no extent from 2^300 up) is summed in
|
|
777
|
+
// doubled precision with a rigorous error bound, and the exact sums below
|
|
778
|
+
// run only when that bound cannot certify the rounded result.
|
|
779
|
+
if (!isWide &&
|
|
780
|
+
frame[3] <= 300 &&
|
|
781
|
+
frame[4] <= 300 &&
|
|
782
|
+
frame[5] <= 300 &&
|
|
783
|
+
certifiedSixVolumeU16_(positions, indices)) {
|
|
784
|
+
// `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
|
|
785
|
+
return VALUE_[0] / 6 + 0;
|
|
786
|
+
}
|
|
787
|
+
const bounds = BOUNDS_;
|
|
788
|
+
let termMax = NONE_;
|
|
789
|
+
let crossMax = NONE_;
|
|
790
|
+
let differenceMax = NONE_;
|
|
791
|
+
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
792
|
+
faceBoundsU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3, isWide);
|
|
793
|
+
const anchored = bounds[0];
|
|
794
|
+
const ab = bounds[1];
|
|
795
|
+
const ac = bounds[2];
|
|
796
|
+
differenceMax = Math.max(differenceMax, anchored, ab, ac);
|
|
797
|
+
if (ab > NONE_ && ac > NONE_) {
|
|
798
|
+
crossMax = Math.max(crossMax, ab + ac);
|
|
799
|
+
if (anchored > NONE_) {
|
|
800
|
+
termMax = Math.max(termMax, anchored + ab + ac);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
// A scaled difference is below 2^(d + 1), a cross product component below
|
|
805
|
+
// 2^(x + 3), a term below 2^(t + 6): the common exponent keeps the three
|
|
806
|
+
// below 2^1021, 2^1020 and 2^1002, where TwoProduct is exact.
|
|
807
|
+
// Integers kept small integers (`| 0`), so that no exponent is boxed on its
|
|
808
|
+
// way into a call.
|
|
809
|
+
const extra = termMax > NONE_
|
|
810
|
+
? Math.min(Math.max(Math.ceil((termMax - 996) / 3), Math.ceil((crossMax - 1017) / 2), differenceMax - 1020, -1020), 0) | 0
|
|
811
|
+
: 0;
|
|
812
|
+
scaleFrameU16_(extra);
|
|
126
813
|
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
127
814
|
const a = indices[corner] * 3;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
815
|
+
scaledEdgesU16_(positions, a, indices[corner + 1] * 3, indices[corner + 2] * 3, isWide);
|
|
816
|
+
anchoredCornerU16_(positions, a, isWide);
|
|
817
|
+
// Component k of the cross product is cross[4k] + cross[4k + 1] +
|
|
818
|
+
// cross[4k + 2] + cross[4k + 3], exactly: edges 0–2 are b − a, 3–5 c − a.
|
|
819
|
+
crossTermsU16_(cross, 0, 1, 5, 2, 4);
|
|
820
|
+
crossTermsU16_(cross, 4, 2, 3, 0, 5);
|
|
821
|
+
crossTermsU16_(cross, 8, 0, 4, 1, 3);
|
|
822
|
+
for (let term = 0; term < 12; term++) {
|
|
823
|
+
productTermsU16_(terms, 4 * term, vertex, term >> 2, cross, term);
|
|
824
|
+
productTermsU16_(terms, 4 * term + 2, vertex, (term >> 2) + 3, cross, term);
|
|
825
|
+
}
|
|
826
|
+
sum.addRange(terms, 0, 48);
|
|
827
|
+
}
|
|
828
|
+
const value = VALUE_;
|
|
829
|
+
sum.writeValue(value, 0);
|
|
830
|
+
value[0] = value[0] / 6;
|
|
831
|
+
scaleByPowerOfTwoInPlace(value, 0, (frame[3] + frame[4] + frame[5] + 3 * extra) | 0);
|
|
832
|
+
// `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
|
|
833
|
+
return value[0] + 0;
|
|
145
834
|
}
|
|
146
835
|
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
836
|
+
* Writes `x[i] · y[j]` into `out` at `at` as two numbers that sum to it exactly
|
|
837
|
+
* (TwoProduct, for products from 2^−969 to below 2^1023; within 5 · 2^−1074
|
|
838
|
+
* below). No double crosses the call.
|
|
839
|
+
*/
|
|
840
|
+
function productTermsU16_(out, at, x, i, y, j) {
|
|
841
|
+
out[at] = x[i];
|
|
842
|
+
out[at + 1] = y[j];
|
|
843
|
+
twoProductInRangeInPlace(out, at);
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Writes `e[i] · e[j] − e[k] · e[l]`, `e` being `EDGES_`, into `out` at `at` as
|
|
847
|
+
* four numbers that sum to it exactly: both products and their rounding errors,
|
|
848
|
+
* negated for the second.
|
|
849
|
+
*/
|
|
850
|
+
function crossTermsU16_(out, at, i, j, k, l) {
|
|
851
|
+
const edges = EDGES_;
|
|
852
|
+
out[at] = edges[i];
|
|
853
|
+
out[at + 1] = edges[j];
|
|
854
|
+
twoProductInRangeInPlace(out, at);
|
|
855
|
+
out[at + 2] = edges[k];
|
|
856
|
+
out[at + 3] = edges[l];
|
|
857
|
+
twoProductInRangeInPlace(out, at + 2);
|
|
858
|
+
out[at + 2] = -out[at + 2];
|
|
859
|
+
out[at + 3] = -out[at + 3];
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* The `Uint16Array` twin of the surface centroid: `o + Σ A · (a + b + c − 3o) /
|
|
863
|
+
* 3` over `Σ A`, o being the frame's anchor and A each face's doubled area
|
|
864
|
+
* (`faceArea_`), scaled by one power of two that brings the largest near 1, so
|
|
865
|
+
* no weight overflows and only one below 2^−1070 of the largest is lost. Every
|
|
866
|
+
* corner minus o is TwoSum's exact pair scaled into the frame per axis, every
|
|
867
|
+
* product split exactly (TwoProduct) and every sum exact (`PreciseSum`), so the
|
|
868
|
+
* result is the same in every face order, nothing overflows at any size or
|
|
869
|
+
* distance from the origin, and a vertex no triangle uses is never read. A zero
|
|
870
|
+
* total area divides zero by zero: `NaN`. O(triangles), no allocation.
|
|
150
871
|
*/
|
|
151
872
|
export function mesh3CentroidU16(out, positions, indices) {
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
let
|
|
159
|
-
|
|
873
|
+
const weights = CENTROID_SUMS_[0];
|
|
874
|
+
const terms = TERMS_;
|
|
875
|
+
const vertex = VERTEX_;
|
|
876
|
+
const area = FACE_AREA_;
|
|
877
|
+
const value = VALUE_;
|
|
878
|
+
const frame = FRAME_;
|
|
879
|
+
for (let sum = 0; sum < CENTROID_SUMS_.length; sum++) {
|
|
880
|
+
CENTROID_SUMS_[sum].reset();
|
|
881
|
+
}
|
|
882
|
+
frameU16_(positions, indices);
|
|
883
|
+
const isWide = frame[9] === 1;
|
|
884
|
+
let weightExponent = NONE_;
|
|
885
|
+
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
886
|
+
faceAreaU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3);
|
|
887
|
+
if (area[0] > 0) {
|
|
888
|
+
weightExponent = Math.max(weightExponent, area[1] + binaryExponent(area[0]));
|
|
889
|
+
}
|
|
890
|
+
}
|
|
160
891
|
for (let corner = 0; corner < indices.length; corner += 3) {
|
|
161
892
|
const a = indices[corner] * 3;
|
|
162
893
|
const b = indices[corner + 1] * 3;
|
|
163
894
|
const c = indices[corner + 2] * 3;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
895
|
+
faceAreaU16_(positions, a, b, c);
|
|
896
|
+
if (area[0] === 0) {
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
value[0] = area[0];
|
|
900
|
+
if (area[0] > 0) {
|
|
901
|
+
// The largest weight is in [1, 2); a power within the normal range is
|
|
902
|
+
// one multiplication.
|
|
903
|
+
const power = area[1] - weightExponent;
|
|
904
|
+
if (power >= -1022) {
|
|
905
|
+
value[0] = area[0] * 2 ** power;
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
scaleByPowerOfTwoInPlace(value, 0, power);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
// Three times the weight, as w + 2w exactly: the sums then divide once.
|
|
912
|
+
terms[0] = value[0];
|
|
913
|
+
terms[1] = 2 * value[0];
|
|
914
|
+
weights.addRange(terms, 0, 2);
|
|
915
|
+
for (let index = 0; index < 3; index++) {
|
|
916
|
+
anchoredCornerU16_(positions, index === 0 ? a : index === 1 ? b : c, isWide);
|
|
917
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
918
|
+
productTermsU16_(terms, 4 * index + 12 * axis, value, 0, vertex, axis);
|
|
919
|
+
productTermsU16_(terms, 4 * index + 12 * axis + 2, value, 0, vertex, axis + 3);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
923
|
+
CENTROID_SUMS_[axis + 1].addRange(terms, 12 * axis, 12 * axis + 12);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
weights.writeValue(value, 0);
|
|
927
|
+
const thrice = value[0];
|
|
928
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
929
|
+
CENTROID_SUMS_[axis + 1].writeValue(value, 0);
|
|
930
|
+
value[0] = value[0] / thrice;
|
|
931
|
+
// Halves where the frame is wide: the anchor plus a scaled offset may
|
|
932
|
+
// pass Number.MAX_VALUE on the way to a centroid that does not.
|
|
933
|
+
scaleByPowerOfTwoInPlace(value, 0, frame[3 + axis] - (isWide ? 1 : 0));
|
|
934
|
+
out[axis] = isWide
|
|
935
|
+
? 2 * (frame[axis] * 0.5 + value[0])
|
|
936
|
+
: frame[axis] + value[0];
|
|
937
|
+
}
|
|
191
938
|
}
|
|
192
939
|
/**
|
|
193
940
|
* The `Uint16Array` twin of the nearest ray hit: the triangle index, `−1` for a
|
|
@@ -226,7 +973,9 @@ export function mesh3IntersectRay3U16(out, ray, positions, indices, cullBackFace
|
|
|
226
973
|
}
|
|
227
974
|
/**
|
|
228
975
|
* The `Uint16Array` twin of the triangle depths: `z / w` of each triangle's
|
|
229
|
-
* centroid under `viewProjection`.
|
|
976
|
+
* centroid under `viewProjection`. A centroid whose coordinates overflow when
|
|
977
|
+
* summed takes halves; one whose transformed z or w overflows is scaled by a
|
|
978
|
+
* power of two first, which leaves `z / w` unchanged. O(triangles).
|
|
230
979
|
*/
|
|
231
980
|
export function mesh3TriangleDepthsU16(out, positions, indices, viewProjection) {
|
|
232
981
|
const m2 = viewProjection[2];
|
|
@@ -241,11 +990,43 @@ export function mesh3TriangleDepthsU16(out, positions, indices, viewProjection)
|
|
|
241
990
|
const a = indices[corner] * 3;
|
|
242
991
|
const b = indices[corner + 1] * 3;
|
|
243
992
|
const c = indices[corner + 2] * 3;
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
993
|
+
let x = (positions[a] + positions[b] + positions[c]) / 3;
|
|
994
|
+
let y = (positions[a + 1] + positions[b + 1] + positions[c + 1]) / 3;
|
|
995
|
+
let z = (positions[a + 2] + positions[b + 2] + positions[c + 2]) / 3;
|
|
996
|
+
if (x - x !== 0 || y - y !== 0 || z - z !== 0) {
|
|
997
|
+
// Quarters, when the sum overflowed: the centroid a wider sum would give.
|
|
998
|
+
x =
|
|
999
|
+
((positions[a] * 0.25 + positions[b] * 0.25 + positions[c] * 0.25) /
|
|
1000
|
+
3) *
|
|
1001
|
+
4;
|
|
1002
|
+
y =
|
|
1003
|
+
((positions[a + 1] * 0.25 +
|
|
1004
|
+
positions[b + 1] * 0.25 +
|
|
1005
|
+
positions[c + 1] * 0.25) /
|
|
1006
|
+
3) *
|
|
1007
|
+
4;
|
|
1008
|
+
z =
|
|
1009
|
+
((positions[a + 2] * 0.25 +
|
|
1010
|
+
positions[b + 2] * 0.25 +
|
|
1011
|
+
positions[c + 2] * 0.25) /
|
|
1012
|
+
3) *
|
|
1013
|
+
4;
|
|
1014
|
+
}
|
|
1015
|
+
let depth = m2 * x + m6 * y + m10 * z + m14;
|
|
1016
|
+
let w = m3 * x + m7 * y + m11 * z + m15;
|
|
1017
|
+
if (depth - depth !== 0 || w - w !== 0) {
|
|
1018
|
+
// The homogeneous point (x, y, z, 1) scaled by a power of two, so that
|
|
1019
|
+
// its largest coordinate is below 1: the ratio is the same.
|
|
1020
|
+
const largest = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
|
|
1021
|
+
if (largest > 1 && largest < Number.POSITIVE_INFINITY) {
|
|
1022
|
+
const scale = 2 ** -(binaryExponent(largest) + 1);
|
|
1023
|
+
depth =
|
|
1024
|
+
m2 * (x * scale) + m6 * (y * scale) + m10 * (z * scale) + m14 * scale;
|
|
1025
|
+
w =
|
|
1026
|
+
m3 * (x * scale) + m7 * (y * scale) + m11 * (z * scale) + m15 * scale;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
out[triangle] = depth / w;
|
|
249
1030
|
}
|
|
250
1031
|
}
|
|
251
1032
|
/**
|