@ac-kit/math-grid 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.
@@ -27,7 +27,13 @@ export type ShelfAllocation = {
27
27
  * ```
28
28
  */
29
29
  export declare class ShelfAllocator {
30
- #private;
30
+ private readonly width_;
31
+ private readonly height_;
32
+ private readonly shelves_;
33
+ private readonly entries_;
34
+ private nextId_;
35
+ private nextY_;
36
+ private usedArea_;
31
37
  /**
32
38
  * @throws {RangeError} When `width` or `height` is not a positive integer, or
33
39
  * when `width · height` is not a safe integer.
@@ -1 +1 @@
1
- {"version":3,"file":"shelf-allocator.d.ts","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAEA,oDAAoD;AACpD,MAAM,MAAM,eAAe,GAAG;IAC7B,iBAAiB;IACjB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB;IAChB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACpB,CAAC;AA0BF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,cAAc;;IAS1B;;;OAGG;IACH,YAAY,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAUxC;IAED,2EAA2E;IAC3E,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAmDnE;IAED;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAwCxB;IAED;;;OAGG;IACH,KAAK,IAAI,IAAI,CAKZ;CACD"}
1
+ {"version":3,"file":"shelf-allocator.d.ts","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAEA,oDAAoD;AACpD,MAAM,MAAM,eAAe,GAAG;IAC7B,iBAAiB;IACjB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB;IAChB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACpB,CAAC;AA0BF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,cAAc;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;IACrD,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,SAAS,CAAK;IAEtB;;;OAGG;IACH,YAAY,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAUxC;IAED,2EAA2E;IAC3E,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAmDnE;IAED;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAwCxB;IAED;;;OAGG;IACH,KAAK,IAAI,IAAI,CAKZ;CACD"}
@@ -24,13 +24,13 @@ function assertDimension(name, value) {
24
24
  * ```
25
25
  */
26
26
  export class ShelfAllocator {
27
- #width;
28
- #height;
29
- #shelves = [];
30
- #entries = new Map();
31
- #nextId = 0;
32
- #nextY = 0;
33
- #usedArea = 0;
27
+ width_;
28
+ height_;
29
+ shelves_ = [];
30
+ entries_ = new Map();
31
+ nextId_ = 0;
32
+ nextY_ = 0;
33
+ usedArea_ = 0;
34
34
  /**
35
35
  * @throws {RangeError} When `width` or `height` is not a positive integer, or
36
36
  * when `width · height` is not a safe integer.
@@ -41,12 +41,12 @@ export class ShelfAllocator {
41
41
  if (!Number.isSafeInteger(width * height)) {
42
42
  throw new RangeError(`area ${width}×${height} exceeds the safe integer range`);
43
43
  }
44
- this.#width = width;
45
- this.#height = height;
44
+ this.width_ = width;
45
+ this.height_ = height;
46
46
  }
47
47
  /** The area given to live allocations over the total area, in `[0, 1]`. */
48
48
  get occupancy() {
49
- return this.#usedArea / (this.#width * this.#height);
49
+ return this.usedArea_ / (this.width_ * this.height_);
50
50
  }
51
51
  /**
52
52
  * The top-left of a free area and its id, or `undefined` when none fits.
@@ -58,12 +58,12 @@ export class ShelfAllocator {
58
58
  allocate(width, height) {
59
59
  assertDimension("width", width);
60
60
  assertDimension("height", height);
61
- if (width > this.#width || height > this.#height) {
61
+ if (width > this.width_ || height > this.height_) {
62
62
  return undefined;
63
63
  }
64
64
  let best;
65
65
  let bestSpan = -1;
66
- for (const shelf of this.#shelves) {
66
+ for (const shelf of this.shelves_) {
67
67
  if (shelf.height < height ||
68
68
  (best !== undefined && shelf.height >= best.height)) {
69
69
  continue;
@@ -75,20 +75,20 @@ export class ShelfAllocator {
75
75
  }
76
76
  }
77
77
  if (best === undefined) {
78
- if (this.#height - this.#nextY < height) {
78
+ if (this.height_ - this.nextY_ < height) {
79
79
  return undefined;
80
80
  }
81
81
  best = {
82
- y: this.#nextY,
82
+ y: this.nextY_,
83
83
  height,
84
- free: [{ start: 0, length: this.#width }],
84
+ free: [{ start: 0, length: this.width_ }],
85
85
  live: 0,
86
86
  };
87
87
  bestSpan = 0;
88
- this.#shelves.push(best);
89
- this.#nextY += height;
88
+ this.shelves_.push(best);
89
+ this.nextY_ += height;
90
90
  }
91
- const id = this.#nextId++;
91
+ const id = this.nextId_++;
92
92
  const span = best.free[bestSpan];
93
93
  const x = span.start;
94
94
  if (span.length === width) {
@@ -99,8 +99,8 @@ export class ShelfAllocator {
99
99
  span.length -= width;
100
100
  }
101
101
  best.live += 1;
102
- this.#entries.set(id, { shelf: best, x, width, height });
103
- this.#usedArea += width * height;
102
+ this.entries_.set(id, { shelf: best, x, width, height });
103
+ this.usedArea_ += width * height;
104
104
  return { x, y: best.y, id };
105
105
  }
106
106
  /**
@@ -112,12 +112,12 @@ export class ShelfAllocator {
112
112
  * released or was cleared.
113
113
  */
114
114
  release(id) {
115
- const entry = this.#entries.get(id);
115
+ const entry = this.entries_.get(id);
116
116
  if (entry === undefined) {
117
117
  throw new RangeError(`unknown or already released id ${id}`);
118
118
  }
119
- this.#entries.delete(id);
120
- this.#usedArea -= entry.width * entry.height;
119
+ this.entries_.delete(id);
120
+ this.usedArea_ -= entry.width * entry.height;
121
121
  const shelf = entry.shelf;
122
122
  shelf.live -= 1;
123
123
  if (shelf.live > 0) {
@@ -141,12 +141,12 @@ export class ShelfAllocator {
141
141
  return;
142
142
  }
143
143
  shelf.free.length = 0;
144
- shelf.free.push({ start: 0, length: this.#width });
145
- let last = this.#shelves.at(-1);
144
+ shelf.free.push({ start: 0, length: this.width_ });
145
+ let last = this.shelves_.at(-1);
146
146
  while (last !== undefined && last.live === 0) {
147
- this.#shelves.pop();
148
- this.#nextY = last.y;
149
- last = this.#shelves.at(-1);
147
+ this.shelves_.pop();
148
+ this.nextY_ = last.y;
149
+ last = this.shelves_.at(-1);
150
150
  }
151
151
  }
152
152
  /**
@@ -154,10 +154,10 @@ export class ShelfAllocator {
154
154
  * entries.
155
155
  */
156
156
  clear() {
157
- this.#shelves.length = 0;
158
- this.#entries.clear();
159
- this.#nextY = 0;
160
- this.#usedArea = 0;
157
+ this.shelves_.length = 0;
158
+ this.entries_.clear();
159
+ this.nextY_ = 0;
160
+ this.usedArea_ = 0;
161
161
  }
162
162
  }
163
163
  //# sourceMappingURL=shelf-allocator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shelf-allocator.js","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AA4B7C,SAAS,eAAe,CAAC,IAAY,EAAE,KAAa;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AChB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACpB,CAAC;CACD"}
@@ -4,23 +4,63 @@
4
4
  * capped at `range`. Rows are stored contiguously, `index = y · width + x`,
5
5
  * texel centres at `(x, y)`.
6
6
  *
7
- * Each edge texel (coverage strictly between 0 and 255) locates the contour
8
- * inside it: the normal is the Sobel gradient of the coverage, and the offset
9
- * from the texel centre inverts the exact area coverage of a straight edge
10
- * through a unit square at that angle, so the field keeps sub-texel edges
11
- * instead of snapping to a threshold. Binary transitions (a full texel beside
12
- * an empty one, as hand-drawn masks have) put the contour midway. Each contour
13
- * piece lowers the distance of every texel within `range` of it, by the
14
- * distance to the piece's segment, which a bare point would overestimate by up
15
- * to a quarter texel between samples. Texels farther than `range` from any
16
- * contour saturate at `±range` (inside when coverage is at least 128).
7
+ * Gustavson & Strand's antialiased Euclidean distance transform (2011, _Pattern
8
+ * Recognition Letters_ 32(2)), seeded with sub-texel contour pieces and
9
+ * extended to straight runs and corners. Each edge texel (coverage strictly
10
+ * between 0 and 255) locates the contour inside it: the normal is the Sobel
11
+ * gradient of the coverage, and the offset from the texel centre inverts the
12
+ * exact area coverage of a straight edge through a unit square at that angle,
13
+ * so the field keeps sub-texel edges instead of snapping to a threshold. Binary
14
+ * transitions (a full texel beside an empty one, as hand-drawn masks have) put
15
+ * the contour midway. The pieces whose 3 × 3 coverage is one straight edge
16
+ * merge into straight runs, each one segment along the line fitted through
17
+ * them; where two runs end next to each other at an angle, the lines cross at a
18
+ * corner's vertex, which the coverage around it then places along the bisector
19
+ * (a sharp tip's crossing is ill-conditioned), the runs end there, and the
20
+ * texels around the vertex that the corner explains take it, so that no piece
21
+ * overshoots it. Every texel then learns its nearest piece of contour, measured
22
+ * to its segments rather than to a point, which would overestimate by up to a
23
+ * quarter texel between samples: from the pieces themselves, each measuring the
24
+ * texels within `range` of it, when that is cheaper (few edge texels, short
25
+ * range); else from its neighbours in two raster sweeps (vector propagation),
26
+ * then along the contour to a nearer piece, which leaves up to about 0.05 texel
27
+ * where pieces of the contour compete (its medial axis). A texel whose 3 × 3
28
+ * coverage is symmetric about it (no gradient) holds no straight edge: when a
29
+ * stripe thinner than a texel through its centre explains that coverage (a
30
+ * hairline, or a thin gap), the stripe's two edges are its contour, so that the
31
+ * stripe's centreline lies half its width inside (or outside, for a gap); else
32
+ * the coverage determines nothing and the contour is taken at its centre. The
33
+ * side of every texel is decided before its magnitude is capped: by its
34
+ * coverage (inside when at least 128), unless a corner it holds or is nearest
35
+ * to, or a stripe it holds, places its centre on the other side. Texels farther
36
+ * than `range` from any contour saturate at the cap.
17
37
  *
18
- * Accurate to 0.1 texel on discs of radius 6 and more; the TinySDF seeding of
19
- * {@link squaredDistanceTransform2} (a seed adds its offset to a squared
20
- * distance) is 0.5 texel off near the edge, so use this for fields that keep
21
- * sub-texel edges and the transform for exact distances between feature cells.
38
+ * The field is stored as 32-bit floats: magnitudes are capped at the largest
39
+ * 32-bit float not above `range` (`3.4028234663852886e38` for a larger
40
+ * `range`), so the field never exceeds `range` and never overflows to an
41
+ * infinity; below the smallest 32-bit float (`2⁻¹⁴⁹`) only the sign of zero is
42
+ * left.
22
43
  *
23
- * O(edge texels · range²) time, O(1) extra memory, no allocation.
44
+ * Within 0.1 texel of the analytic distance, up to 5 texels from the edge, on
45
+ * coverage sampled 16 × 16 per texel and rounded to bytes, for discs of radius
46
+ * 4 and more and polygons whose sides are at least 8 texels long and whose
47
+ * corners are at least 20°, convex or concave; and within 0.02 texel, on
48
+ * exact-area coverage rounded to bytes, for stripes from 0.1 to 0.8 texel wide,
49
+ * of ink or a gap, along a row, a column or a diagonal of texel centres.
50
+ * Smaller polygons and sharper tips leave their corners to the straight pieces,
51
+ * up to half a texel off near the vertex; a feature thinner than a texel that
52
+ * crosses texels off their centres (a hairline at another angle) can be up to
53
+ * half a texel off, as one texel's coverage cannot tell where in it such a
54
+ * feature lies.
55
+ *
56
+ * O(width · height) time: a pass over the grid, the runs grown once from the
57
+ * pieces, a corner tried only where two runs end next to each other, then the
58
+ * lesser of O(pieces · range²) and two sweeps that measure only the texels
59
+ * within `range + 2` of the contour. `8 · width · height` doubles of working
60
+ * memory, none allocated when `scratch` is given. Unlike TinySDF's seeding of
61
+ * {@link squaredDistanceTransform2} (a squared sub-texel offset added at the
62
+ * edge texel, half a texel off near the edge), the distance is measured to the
63
+ * contour itself.
24
64
  *
25
65
  * @example
26
66
  * const coverage = new Uint8Array([0, 0, 128, 255, 255]);
@@ -30,13 +70,15 @@
30
70
  * @param out - At least `width · height` entries; overwritten.
31
71
  * @param coverage - `width · height` bytes, 0 outside to 255 inside.
32
72
  * @param range - The most distance kept, in texels, positive and finite.
73
+ * @param scratch - At least `8 · width · height` entries of working memory,
74
+ * overwritten; allocated when omitted.
33
75
  * @returns `out`.
34
76
  * @throws {RangeError} When a size is not a positive integer, `range` is not
35
- * positive and finite, `coverage` or `out` is too short, or `out` shares
36
- * bytes with the first `width · height` entries of `coverage` (checked before
37
- * anything is written: the field is built in `out` while `coverage` is read).
38
- * Views over a `SharedArrayBuffer` and over any other shared buffer, a clone
39
- * of it included, are taken to share memory.
77
+ * positive and finite, `coverage`, `out` or `scratch` is too short, or two of
78
+ * `out`, `coverage` and `scratch` share bytes within the entries the call
79
+ * uses (checked before anything is written). Views over a `SharedArrayBuffer`
80
+ * and over any other shared buffer, a clone of it included, are taken to
81
+ * share memory.
40
82
  */
41
- export declare function signedDistanceFromCoverage2(out: Float32Array, coverage: Uint8Array, width: number, height: number, range: number): Float32Array;
83
+ export declare function signedDistanceFromCoverage2(out: Float32Array, coverage: Uint8Array, width: number, height: number, range: number, scratch?: Float64Array): Float32Array;
42
84
  //# sourceMappingURL=signed-distance-from-coverage2.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signed-distance-from-coverage2.d.ts","sourceRoot":"","sources":["../src/signed-distance-from-coverage2.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,YAAY,CA8Dd"}
1
+ {"version":3,"file":"signed-distance-from-coverage2.d.ts","sourceRoot":"","sources":["../src/signed-distance-from-coverage2.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiFG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,YAAY,GACpB,YAAY,CAkKd"}