@ac-kit/math-geometry 0.3.0 → 0.5.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 (75) hide show
  1. package/CHANGELOG.md +177 -0
  2. package/README.md +76 -5
  3. package/dist/_polygon3-cross.d.ts +23 -0
  4. package/dist/_polygon3-cross.d.ts.map +1 -0
  5. package/dist/_polygon3-cross.js +418 -0
  6. package/dist/_polygon3-cross.js.map +1 -0
  7. package/dist/_polyline-simplify.d.ts +11 -0
  8. package/dist/_polyline-simplify.d.ts.map +1 -0
  9. package/dist/_polyline-simplify.js +92 -0
  10. package/dist/_polyline-simplify.js.map +1 -0
  11. package/dist/_triangle3-cross.d.ts +66 -0
  12. package/dist/_triangle3-cross.d.ts.map +1 -0
  13. package/dist/_triangle3-cross.js +405 -0
  14. package/dist/_triangle3-cross.js.map +1 -0
  15. package/dist/convex-hull2.d.ts +26 -0
  16. package/dist/convex-hull2.d.ts.map +1 -0
  17. package/dist/convex-hull2.js +88 -0
  18. package/dist/convex-hull2.js.map +1 -0
  19. package/dist/index.d.ts +2 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/line2.d.ts +4 -0
  24. package/dist/line2.d.ts.map +1 -1
  25. package/dist/line2.js +26 -5
  26. package/dist/line2.js.map +1 -1
  27. package/dist/mesh/_index-kernels-u16.d.ts +46 -11
  28. package/dist/mesh/_index-kernels-u16.d.ts.map +1 -1
  29. package/dist/mesh/_index-kernels-u16.js +1442 -132
  30. package/dist/mesh/_index-kernels-u16.js.map +1 -1
  31. package/dist/mesh/_index-kernels-u32.d.ts +46 -11
  32. package/dist/mesh/_index-kernels-u32.d.ts.map +1 -1
  33. package/dist/mesh/_index-kernels-u32.js +1442 -132
  34. package/dist/mesh/_index-kernels-u32.js.map +1 -1
  35. package/dist/mesh/mesh3-kernels.d.ts +104 -20
  36. package/dist/mesh/mesh3-kernels.d.ts.map +1 -1
  37. package/dist/mesh/mesh3-kernels.js +123 -25
  38. package/dist/mesh/mesh3-kernels.js.map +1 -1
  39. package/dist/plane3.d.ts +4 -3
  40. package/dist/plane3.d.ts.map +1 -1
  41. package/dist/plane3.js +18 -35
  42. package/dist/plane3.js.map +1 -1
  43. package/dist/polygon2.d.ts +76 -4
  44. package/dist/polygon2.d.ts.map +1 -1
  45. package/dist/polygon2.js +366 -7
  46. package/dist/polygon2.js.map +1 -1
  47. package/dist/polygon3.d.ts +19 -3
  48. package/dist/polygon3.d.ts.map +1 -1
  49. package/dist/polygon3.js +40 -16
  50. package/dist/polygon3.js.map +1 -1
  51. package/dist/polyline2.d.ts +20 -0
  52. package/dist/polyline2.d.ts.map +1 -1
  53. package/dist/polyline2.js +25 -0
  54. package/dist/polyline2.js.map +1 -1
  55. package/dist/polyline3.d.ts +17 -0
  56. package/dist/polyline3.d.ts.map +1 -1
  57. package/dist/polyline3.js +22 -0
  58. package/dist/polyline3.js.map +1 -1
  59. package/dist/predicates2.d.ts +51 -0
  60. package/dist/predicates2.d.ts.map +1 -0
  61. package/dist/predicates2.js +172 -0
  62. package/dist/predicates2.js.map +1 -0
  63. package/dist/quadtree.d.ts +40 -23
  64. package/dist/quadtree.d.ts.map +1 -1
  65. package/dist/quadtree.js +196 -119
  66. package/dist/quadtree.js.map +1 -1
  67. package/dist/segment2.d.ts +6 -1
  68. package/dist/segment2.d.ts.map +1 -1
  69. package/dist/segment2.js +20 -4
  70. package/dist/segment2.js.map +1 -1
  71. package/dist/triangle3.d.ts +20 -1
  72. package/dist/triangle3.d.ts.map +1 -1
  73. package/dist/triangle3.js +38 -18
  74. package/dist/triangle3.js.map +1 -1
  75. package/package.json +8 -8
@@ -0,0 +1,172 @@
1
+ import { float64ToParts } from "@ac-kit/core";
2
+ /** `ε = 2⁻⁵³`, the unit roundoff of binary64. */
3
+ const EPSILON_ = 2 ** -53;
4
+ /**
5
+ * Shewchuk's first-stage error bounds ("Adaptive Precision Floating-Point
6
+ * Arithmetic and Fast Robust Geometric Predicates", 1997, §4.3): when the
7
+ * rounded determinant exceeds the bound times the permanent, its sign is the
8
+ * exact one.
9
+ */
10
+ const ORIENT_BOUND_ = (3 + 16 * EPSILON_) * EPSILON_;
11
+ const IN_CIRCLE_BOUND_ = (10 + 96 * EPSILON_) * EPSILON_;
12
+ /**
13
+ * Below these permanents a product may have underflowed, which the bounds do
14
+ * not account for; the exact evaluation takes over.
15
+ */
16
+ const ORIENT_TINY_ = 2 ** -900;
17
+ const IN_CIRCLE_TINY_ = 2 ** -700;
18
+ /**
19
+ * The orientation of three points: `1` when `c` lies to the left of the line
20
+ * from `a` to `b` (the triangle `a, b, c` turns counter-clockwise in a y-up
21
+ * frame), `-1` when it lies to the right, `0` when the three are exactly
22
+ * collinear — the sign of `(bx − ax)(cy − ay) − (by − ay)(cx − ax)`, exact for
23
+ * every finite input.
24
+ *
25
+ * A plain floating-point evaluation of that determinant can return the wrong
26
+ * sign, or `0`, for nearly collinear points, and an algorithm that asks the
27
+ * question twice about the same points can get two answers: hulls with crossing
28
+ * edges, triangulations with flipped triangles. Here the rounded determinant is
29
+ * accepted only when Shewchuk's error bound proves its sign; otherwise — nearly
30
+ * collinear points, products that underflow or overflow — the determinant is
31
+ * evaluated exactly in integer arithmetic. The common case costs a few
32
+ * operations more than the plain formula and allocates nothing; the exact path
33
+ * allocates a few `bigint`s.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * orient2(0, 0, 1, 0, 0, 1); // 1 — counter-clockwise
38
+ * orient2(0, 0, 1, 1, 2, 2); // 0 — collinear
39
+ * orient2(0.5, 0.5 + 2 ** -53, 12, 12, 24, 24); // 1, where the plain formula gives 0
40
+ * ```;
41
+ *
42
+ * @returns `1`, `-1` or `0`; `NaN` when a coordinate is `NaN` or infinite.
43
+ * @see {@link inCircle2} for the in-circle test triangulations need.
44
+ */
45
+ export function orient2(ax, ay, bx, by, cx, cy) {
46
+ const acx = ax - cx;
47
+ const bcx = bx - cx;
48
+ const acy = ay - cy;
49
+ const bcy = by - cy;
50
+ const left = acx * bcy;
51
+ const right = acy * bcx;
52
+ const determinant = left - right;
53
+ const permanent = Math.abs(left) + Math.abs(right);
54
+ if (permanent >= ORIENT_TINY_ && permanent - permanent === 0) {
55
+ if (Math.abs(determinant) >= ORIENT_BOUND_ * permanent) {
56
+ return determinant > 0 ? 1 : -1;
57
+ }
58
+ }
59
+ else if (permanent === 0 &&
60
+ (acx === 0 || bcy === 0) &&
61
+ (acy === 0 || bcx === 0)) {
62
+ // Each product has an exactly zero factor: a difference of binary64
63
+ // numbers is zero only when they are equal, so the determinant is 0.
64
+ return 0;
65
+ }
66
+ return exactSign_(ORIENT_, [ax, ay, bx, by, cx, cy]);
67
+ }
68
+ /**
69
+ * The in-circle test: `1` when `d` lies strictly inside the circle through `a`,
70
+ * `b` and `c`, `-1` when it lies outside, `0` when the four are exactly
71
+ * cocircular — for `a, b, c` in counter-clockwise order; the signs swap when
72
+ * they are clockwise, as they are the sign of the determinant of the points
73
+ * lifted onto the paraboloid `z = x² + y²`. Exact for every finite input.
74
+ *
75
+ * The test Delaunay triangulation flips edges on. As with {@link orient2}, the
76
+ * rounded determinant is accepted only when Shewchuk's error bound proves its
77
+ * sign; otherwise it is evaluated exactly in integer arithmetic. The common
78
+ * case allocates nothing; the exact path allocates a few `bigint`s.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * inCircle2(0, 0, 1, 0, 0, 1, 0.5, 0.5); // 1 — inside
83
+ * inCircle2(0, 0, 1, 0, 0, 1, 1, 1); // 0 — on the circle
84
+ * inCircle2(0, 0, 1, 0, 0, 1, 2, 2); // -1 — outside
85
+ * ```;
86
+ *
87
+ * @returns `1`, `-1` or `0`; `NaN` when a coordinate is `NaN` or infinite.
88
+ */
89
+ export function inCircle2(ax, ay, bx, by, cx, cy, dx, dy) {
90
+ const adx = ax - dx;
91
+ const bdx = bx - dx;
92
+ const cdx = cx - dx;
93
+ const ady = ay - dy;
94
+ const bdy = by - dy;
95
+ const cdy = cy - dy;
96
+ const bdxcdy = bdx * cdy;
97
+ const cdxbdy = cdx * bdy;
98
+ const aLift = adx * adx + ady * ady;
99
+ const cdxady = cdx * ady;
100
+ const adxcdy = adx * cdy;
101
+ const bLift = bdx * bdx + bdy * bdy;
102
+ const adxbdy = adx * bdy;
103
+ const bdxady = bdx * ady;
104
+ const cLift = cdx * cdx + cdy * cdy;
105
+ const determinant = aLift * (bdxcdy - cdxbdy) +
106
+ bLift * (cdxady - adxcdy) +
107
+ cLift * (adxbdy - bdxady);
108
+ const permanent = (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * aLift +
109
+ (Math.abs(cdxady) + Math.abs(adxcdy)) * bLift +
110
+ (Math.abs(adxbdy) + Math.abs(bdxady)) * cLift;
111
+ if (permanent >= IN_CIRCLE_TINY_ &&
112
+ permanent - permanent === 0 &&
113
+ Math.abs(determinant) >= IN_CIRCLE_BOUND_ * permanent) {
114
+ return determinant > 0 ? 1 : -1;
115
+ }
116
+ return exactSign_(IN_CIRCLE_, [ax, ay, bx, by, cx, cy, dx, dy]);
117
+ }
118
+ /** Which determinant {@link exactSign_} evaluates. */
119
+ const ORIENT_ = 0;
120
+ const IN_CIRCLE_ = 1;
121
+ /**
122
+ * The exact sign of a predicate's determinant: every coordinate as an integer
123
+ * times a power of two, all brought to the smallest exponent, so the
124
+ * differences and products are exact `bigint` arithmetic. The common scale is
125
+ * positive and does not change the sign.
126
+ */
127
+ function exactSign_(predicate, coordinates) {
128
+ let minimumExponent = Infinity;
129
+ for (const coordinate of coordinates) {
130
+ if (coordinate - coordinate !== 0) {
131
+ return Number.NaN;
132
+ }
133
+ if (coordinate !== 0) {
134
+ minimumExponent = Math.min(minimumExponent, float64ToParts(coordinate).exponent);
135
+ }
136
+ }
137
+ if (minimumExponent === Infinity) {
138
+ return 0;
139
+ }
140
+ const scaled = coordinates.map((coordinate) => {
141
+ if (coordinate === 0) {
142
+ return 0n;
143
+ }
144
+ const parts = float64ToParts(coordinate);
145
+ return parts.significand << BigInt(parts.exponent - minimumExponent);
146
+ });
147
+ let determinant;
148
+ if (predicate === ORIENT_) {
149
+ const acx = scaled[0] - scaled[4];
150
+ const bcx = scaled[2] - scaled[4];
151
+ const acy = scaled[1] - scaled[5];
152
+ const bcy = scaled[3] - scaled[5];
153
+ determinant = acx * bcy - acy * bcx;
154
+ }
155
+ else {
156
+ const adx = scaled[0] - scaled[6];
157
+ const ady = scaled[1] - scaled[7];
158
+ const bdx = scaled[2] - scaled[6];
159
+ const bdy = scaled[3] - scaled[7];
160
+ const cdx = scaled[4] - scaled[6];
161
+ const cdy = scaled[5] - scaled[7];
162
+ determinant =
163
+ (adx * adx + ady * ady) * (bdx * cdy - cdx * bdy) +
164
+ (bdx * bdx + bdy * bdy) * (cdx * ady - adx * cdy) +
165
+ (cdx * cdx + cdy * cdy) * (adx * bdy - bdx * ady);
166
+ }
167
+ if (determinant === 0n) {
168
+ return 0;
169
+ }
170
+ return determinant > 0n ? 1 : -1;
171
+ }
172
+ //# sourceMappingURL=predicates2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"predicates2.js","sourceRoot":"","sources":["../src/predicates2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,iDAAiD;AACjD,MAAM,QAAQ,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,aAAa,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC;AACrD,MAAM,gBAAgB,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAEzD;;;GAGG;AACH,MAAM,YAAY,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;AAC/B,MAAM,eAAe,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU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C,WAAW;YACV,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;gBACjD,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;gBACjD,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,WAAW,KAAK,EAAE,EAAE,CAAC;QACxB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,OAAO,WAAW,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAClC,CAAC"}
@@ -46,12 +46,14 @@ export type QuadtreeVisitor = Callable<[
46
46
  *
47
47
  * Aggregates (Barnes–Hut charge centres, collision radii, counts) are the
48
48
  * caller's: allocate `Float64Array(tree.nodeCount() * k)` and fill it in
49
- * `visitAfter`. The visitor gets each cell as a box the traversal reuses rather
50
- * than four numbers, so the traversal itself allocates nothing per node whether
51
- * or not the engine inlines the visitor. This is not a hard zero-allocation
52
- * guarantee: before the engine optimises the traversal, and in some warmed runs
53
- * that have not been explained, a call can still allocate a few kilobytes or
54
- * more. Node numbers are dense (`0 … nodeCount() − 1`) and **valid until the
49
+ * `visitAfter`. Storage is struct-of-arrays typed columns grown geometrically
50
+ * and reused, the root cell lives in a `Float64Array`, and every traversal runs
51
+ * on a preallocated typed-array stack; the visitor gets each cell as a box the
52
+ * traversal reuses rather than four numbers. So once the engine has optimised
53
+ * the calls and the columns have reached the size a workload needs, `assign`,
54
+ * `visit`, `visitAfter`, `find` and `forEachInBox` allocate 0 B. (A double the
55
+ * caller passes to `find` or `add` may still be boxed by the caller's own
56
+ * code.) Node numbers are dense (`0 … nodeCount() − 1`) and **valid until the
55
57
  * next mutation** (`assign`, `add`, `remove`, `clear`); mutating the tree from
56
58
  * inside a visitor is not supported. Queries and traversals may nest freely.
57
59
  *
@@ -86,13 +88,23 @@ export declare class Quadtree {
86
88
  private parents;
87
89
  private children;
88
90
  private nodes;
89
- private x0;
90
- private y0;
91
- private x1;
92
- private y1;
91
+ /**
92
+ * Root cell: minX, minY, maxX, maxY; NaN when empty. A typed array, so
93
+ * writing a bound never boxes a double.
94
+ */
95
+ private readonly root;
96
+ /**
97
+ * Whether `root` holds a cell. A flag rather than a NaN test: an engine tier
98
+ * that calls `Number.isNaN` out of line boxes the double it passes.
99
+ */
100
+ private hasRoot;
101
+ /** The point `cover` reaches, then the box `reroot` holds. */
102
+ private readonly target;
93
103
  private readonly walks;
94
104
  private walkDepth;
95
- private readonly dead;
105
+ /** Nodes being freed by `remove`: `deadCount` of them. */
106
+ private dead;
107
+ private deadCount;
96
108
  private readonly childKeys;
97
109
  /** Live ids gathered for a re-rooting rebuild. */
98
110
  private rebuildIds;
@@ -183,29 +195,33 @@ export declare class Quadtree {
183
195
  private hasOtherChild;
184
196
  /** The single child of `node`, or −1 when it has none or several. */
185
197
  private onlyChild;
198
+ private pushDead;
186
199
  /**
187
200
  * Frees the nodes in `dead` in O(their number), keeping node numbers dense by
188
201
  * moving the last live node into each hole.
189
202
  */
190
203
  private releaseDead;
191
204
  /**
192
- * Grows the root cell until it holds (x, y): d3-quadtree's `cover`, as long
193
- * as the doubled cell has the old root as an exact quadrant; otherwise the
194
- * tree is re-rooted on exactly representable edges and rebuilt.
205
+ * Grows the root cell until it holds the point in `target`: d3-quadtree's
206
+ * `cover`, as long as the doubled cell has the old root as an exact quadrant;
207
+ * otherwise the tree is re-rooted on exactly representable edges and rebuilt.
208
+ * The point comes through `target`, not as arguments: a call that is not
209
+ * inlined would box both doubles.
195
210
  */
196
211
  private cover;
197
212
  /**
198
- * Doubles the root cell toward (x, y), anchored on the opposite corner.
199
- * Refused when rounding would make the old root anything but the new root's
200
- * quadrant as every traversal computes it: the existing cells would move.
213
+ * Doubles the root cell toward the point in `target`, anchored on the
214
+ * opposite corner. Refused when rounding would make the old root anything but
215
+ * the new root's quadrant as every traversal computes it: the existing cells
216
+ * would move.
201
217
  */
202
218
  private double;
203
219
  /**
204
- * Replaces the root by a square cell holding the closed box, whose edges are
205
- * multiples of a quarter of its power-of-two size, every one exactly
206
- * representable; later doublings then stay exact until the root has grown
207
- * about 2⁵⁰-fold or past the box's magnitude. Reinserts the points, in
208
- * ascending id order.
220
+ * Replaces the root by a square cell holding the closed box in `target` (loX,
221
+ * loY, hiX, hiY), whose edges are multiples of a quarter of its power-of-two
222
+ * size, every one exactly representable; later doublings then stay exact
223
+ * until the root has grown about 2⁵⁰-fold or past the box's magnitude.
224
+ * Reinserts the points, in ascending id order.
209
225
  */
210
226
  private reroot;
211
227
  /**
@@ -227,7 +243,8 @@ export declare class Quadtree {
227
243
  private acquireWalk;
228
244
  /**
229
245
  * Branch and bound, nearest cell first; `walk.keys` holds each pending cell's
230
- * distance.
246
+ * distance. The query is `walk.point` and the radius `walk.keys[0]`, not
247
+ * arguments: a call that is not inlined would box the doubles.
231
248
  */
232
249
  private findWith;
233
250
  private setRoot;
@@ -1 +1 @@
1
- {"version":3,"file":"quadtree.d.ts","sourceRoot":"","sources":["../src/quadtree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,cAAc,CAAC;AAQ5D,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAG5E;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,CACrC;IAAC,IAAI,EAAE,MAAM;IAAE,IAAI,EAAE,YAAY;CAAC,EAClC,OAAO,GAAG,IAAI,CACd,CAAC;AAkHF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,qBAAa,QAAQ;IACpB,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