@typeonce/ritmo-math 0.13.0 → 0.15.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 +149 -0
- package/README.md +18 -13
- package/dist/Angle.d.ts +44 -0
- package/dist/Angle.d.ts.map +1 -0
- package/dist/Angle.js +62 -0
- package/dist/Angle.js.map +1 -0
- package/dist/Box2.d.ts +16 -4
- package/dist/Box2.d.ts.map +1 -1
- package/dist/Box2.js +16 -1
- package/dist/Box2.js.map +1 -1
- package/dist/Collide2.d.ts +59 -15
- package/dist/Collide2.d.ts.map +1 -1
- package/dist/Collide2.js +156 -18
- package/dist/Collide2.js.map +1 -1
- package/dist/FlowField.d.ts +24 -38
- package/dist/FlowField.d.ts.map +1 -1
- package/dist/FlowField.js +18 -5
- package/dist/FlowField.js.map +1 -1
- package/dist/Scalar.d.ts +19 -166
- package/dist/Scalar.d.ts.map +1 -1
- package/dist/Scalar.js +51 -51
- package/dist/Scalar.js.map +1 -1
- package/dist/Segment2.d.ts +56 -0
- package/dist/Segment2.d.ts.map +1 -0
- package/dist/Segment2.js +32 -0
- package/dist/Segment2.js.map +1 -0
- package/dist/Vec2.d.ts +47 -7
- package/dist/Vec2.d.ts.map +1 -1
- package/dist/Vec2.js +56 -8
- package/dist/Vec2.js.map +1 -1
- package/dist/index.d.ts +10 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -21
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Angle.ts +60 -0
- package/src/Box2.ts +20 -4
- package/src/Collide2.ts +171 -24
- package/src/FlowField.ts +34 -10
- package/src/Scalar.ts +43 -165
- package/src/Segment2.ts +71 -0
- package/src/Vec2.ts +58 -8
- package/src/index.ts +10 -21
- package/dist/Aabb.d.ts +0 -182
- package/dist/Aabb.d.ts.map +0 -1
- package/dist/Aabb.js +0 -109
- package/dist/Aabb.js.map +0 -1
- package/dist/InputAxis.d.ts +0 -44
- package/dist/InputAxis.d.ts.map +0 -1
- package/dist/InputAxis.js +0 -71
- package/dist/InputAxis.js.map +0 -1
- package/dist/Ops.d.ts +0 -209
- package/dist/Ops.d.ts.map +0 -1
- package/dist/Ops.js +0 -210
- package/dist/Ops.js.map +0 -1
- package/dist/Random.d.ts +0 -124
- package/dist/Random.d.ts.map +0 -1
- package/dist/Random.js +0 -126
- package/dist/Random.js.map +0 -1
- package/dist/Size2.d.ts +0 -118
- package/dist/Size2.d.ts.map +0 -1
- package/dist/Size2.js +0 -54
- package/dist/Size2.js.map +0 -1
- package/dist/SpatialGrid.d.ts +0 -68
- package/dist/SpatialGrid.d.ts.map +0 -1
- package/dist/SpatialGrid.js +0 -112
- package/dist/SpatialGrid.js.map +0 -1
- package/dist/Vector2.d.ts +0 -214
- package/dist/Vector2.d.ts.map +0 -1
- package/dist/Vector2.js +0 -134
- package/dist/Vector2.js.map +0 -1
- package/dist/internal/aabb.d.ts +0 -27
- package/dist/internal/aabb.d.ts.map +0 -1
- package/dist/internal/aabb.js +0 -52
- package/dist/internal/aabb.js.map +0 -1
- package/dist/internal/codec.d.ts +0 -9
- package/dist/internal/codec.d.ts.map +0 -1
- package/dist/internal/codec.js +0 -16
- package/dist/internal/codec.js.map +0 -1
- package/dist/internal/fdlibm.d.ts +0 -43
- package/dist/internal/fdlibm.d.ts.map +0 -1
- package/dist/internal/fdlibm.js +0 -1291
- package/dist/internal/fdlibm.js.map +0 -1
- package/dist/internal/inputAxis.d.ts +0 -11
- package/dist/internal/inputAxis.d.ts.map +0 -1
- package/dist/internal/inputAxis.js +0 -13
- package/dist/internal/inputAxis.js.map +0 -1
- package/dist/internal/scalar.d.ts +0 -11
- package/dist/internal/scalar.d.ts.map +0 -1
- package/dist/internal/scalar.js +0 -68
- package/dist/internal/scalar.js.map +0 -1
- package/dist/internal/size2.d.ts +0 -22
- package/dist/internal/size2.d.ts.map +0 -1
- package/dist/internal/size2.js +0 -38
- package/dist/internal/size2.js.map +0 -1
- package/dist/internal/vector2.d.ts +0 -39
- package/dist/internal/vector2.d.ts.map +0 -1
- package/dist/internal/vector2.js +0 -78
- package/dist/internal/vector2.js.map +0 -1
- package/src/Aabb.ts +0 -180
- package/src/InputAxis.ts +0 -81
- package/src/Ops.ts +0 -232
- package/src/Random.ts +0 -225
- package/src/Size2.ts +0 -122
- package/src/SpatialGrid.ts +0 -172
- package/src/Vector2.ts +0 -216
- package/src/internal/aabb.ts +0 -81
- package/src/internal/codec.ts +0 -21
- package/src/internal/fdlibm.ts +0 -1256
- package/src/internal/inputAxis.ts +0 -21
- package/src/internal/scalar.ts +0 -104
- package/src/internal/size2.ts +0 -54
- package/src/internal/vector2.ts +0 -121
package/dist/Random.d.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deterministic random numbers (mulberry32) whose whole state is one 32-bit
|
|
3
|
-
* integer.
|
|
4
|
-
*
|
|
5
|
-
* Keep the state in a resource, so a run replays exactly and saves with the
|
|
6
|
-
* world: declare it with `Random.codec` as its schema, start it with
|
|
7
|
-
* `seed`, and draw in systems through `draw` over its write cell, which writes
|
|
8
|
-
* every draw back (rolled back with a failed system). Two generators made
|
|
9
|
-
* from the same state draw the same sequence on every platform. `make` is for
|
|
10
|
-
* a state that lives outside the world (a level generator seeded once).
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const Rng = Descriptor.ConstructedResource(Random.codec)("Game/Rng")
|
|
15
|
-
* const runtime = Game.Runtime.make({ services, resources: { Rng: Random.seed(0x5eed) } })
|
|
16
|
-
*
|
|
17
|
-
* Game.System("Game/Spawn", { resources: { rng: Game.System.writeResource(Rng) } }, ({ resources }) => {
|
|
18
|
-
* const random = Random.draw(resources.rng)
|
|
19
|
-
* const angle = random.range(0, Math.PI * 2)
|
|
20
|
-
* const kind = random.pick(kinds) ?? "grunt"
|
|
21
|
-
* })
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @module Random
|
|
25
|
-
* @docGroup helpers
|
|
26
|
-
*
|
|
27
|
-
* @categoryDescription Random Types
|
|
28
|
-
* The generator interface.
|
|
29
|
-
*
|
|
30
|
-
* @categoryDescription Construction
|
|
31
|
-
* Making a generator from a stored state.
|
|
32
|
-
*
|
|
33
|
-
* @categoryDescription Resource
|
|
34
|
-
* Keeping the state in a world resource.
|
|
35
|
-
*/
|
|
36
|
-
import type * as Brand from "@typeonce/ritmo/Brand";
|
|
37
|
-
import type { WriteCell } from "@typeonce/ritmo/Query";
|
|
38
|
-
import type { StandardSchema } from "@typeonce/ritmo/Descriptor";
|
|
39
|
-
import * as Result from "@typeonce/ritmo/Result";
|
|
40
|
-
/**
|
|
41
|
-
* A generator. Every draw advances `state`.
|
|
42
|
-
*
|
|
43
|
-
* @category Random Types
|
|
44
|
-
*/
|
|
45
|
-
export interface Random {
|
|
46
|
-
/** The current state: an unsigned 32-bit integer. Store it to continue the sequence later. */
|
|
47
|
-
readonly state: number;
|
|
48
|
-
/** Uniform in [0, 1). */
|
|
49
|
-
next(): number;
|
|
50
|
-
/** Uniform in [min, max), or `min` when they are equal. Throws when `min > max` or a bound is not finite. */
|
|
51
|
-
range(min: number, max: number): number;
|
|
52
|
-
/** A uniform integer in [min, maxExclusive). Throws unless both bounds are integers with min < maxExclusive. */
|
|
53
|
-
integer(min: number, maxExclusive: number): number;
|
|
54
|
-
/** A uniform element of `items`, or `undefined` when it is empty (no draw is made then). */
|
|
55
|
-
pick<A>(items: ReadonlyArray<A>): A | undefined;
|
|
56
|
-
/** A shuffled copy of `items` (Fisher–Yates). */
|
|
57
|
-
shuffle<A>(items: ReadonlyArray<A>): Array<A>;
|
|
58
|
-
/**
|
|
59
|
-
* An element of `items` chosen with probability proportional to `weight`,
|
|
60
|
-
* or `undefined` when no element has a positive finite weight (no draw is
|
|
61
|
-
* made then). Elements with zero, negative, or non-finite weight are never
|
|
62
|
-
* chosen. `weight` is called once per element.
|
|
63
|
-
*/
|
|
64
|
-
weighted<A>(items: ReadonlyArray<A>, weight: (item: A) => number): A | undefined;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* A generator continuing from `state`. Any number is accepted and reduced to
|
|
68
|
-
* an unsigned 32-bit integer (`state >>> 0`), so a stored state round-trips
|
|
69
|
-
* exactly.
|
|
70
|
-
*
|
|
71
|
-
* @category Construction
|
|
72
|
-
*/
|
|
73
|
-
export declare const make: (state: number) => Random;
|
|
74
|
-
/**
|
|
75
|
-
* A generator's state as a resource holds it: an unsigned 32-bit integer.
|
|
76
|
-
* Only `seed`, `result`, and `codec` make one, so `draw` takes the cell of a
|
|
77
|
-
* random resource and nothing else.
|
|
78
|
-
*
|
|
79
|
-
* @category Resource
|
|
80
|
-
*/
|
|
81
|
-
export type State = Brand.Branded<number, "Random/State">;
|
|
82
|
-
/** Why a value is not a generator state. @category Resource */
|
|
83
|
-
export interface InvalidState {
|
|
84
|
-
readonly _tag: "InvalidRandomState";
|
|
85
|
-
readonly value: unknown;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Validates a state, for game code that builds one from a number.
|
|
89
|
-
*
|
|
90
|
-
* @category Resource
|
|
91
|
-
*/
|
|
92
|
-
export declare const result: (raw: number) => Result.Result<State, InvalidState>;
|
|
93
|
-
/**
|
|
94
|
-
* Whether any value is a generator state: a guard, also for building a
|
|
95
|
-
* state field in your own schema (`Schema.declare(Random.is)`).
|
|
96
|
-
*
|
|
97
|
-
* @category Resource
|
|
98
|
-
*/
|
|
99
|
-
export declare const is: (raw: unknown) => raw is State;
|
|
100
|
-
/**
|
|
101
|
-
* The codec of a random resource's state (a Standard Schema, see
|
|
102
|
-
* `Descriptor.Codec`): `Descriptor.ConstructedResource(Random.codec)`. Its
|
|
103
|
-
* saved form is the number itself, so it needs no `encode`.
|
|
104
|
-
*
|
|
105
|
-
* @category Resource
|
|
106
|
-
*/
|
|
107
|
-
export declare const codec: StandardSchema<number, State>;
|
|
108
|
-
/**
|
|
109
|
-
* The starting state for a seed, reduced to an unsigned 32-bit integer like
|
|
110
|
-
* `make`: the value to give a random resource in `Game.Runtime.make`.
|
|
111
|
-
*
|
|
112
|
-
* @category Resource
|
|
113
|
-
*/
|
|
114
|
-
export declare const seed: (value: number) => State;
|
|
115
|
-
/**
|
|
116
|
-
* A generator over a random resource's write cell: every draw continues from
|
|
117
|
-
* the stored state and writes the new one back, so the system needs no
|
|
118
|
-
* `set` of its own, a failed run rolls the draws back, and generators made
|
|
119
|
-
* from the same cell in one run continue one sequence.
|
|
120
|
-
*
|
|
121
|
-
* @category Resource
|
|
122
|
-
*/
|
|
123
|
-
export declare const draw: (cell: WriteCell<State>) => Random;
|
|
124
|
-
//# sourceMappingURL=Random.d.ts.map
|
package/dist/Random.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Random.d.ts","sourceRoot":"","sources":["../src/Random.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,KAAK,KAAK,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAChE,OAAO,KAAK,MAAM,MAAM,wBAAwB,CAAA;AAGhD;;;;GAIG;AACH,MAAM,WAAW,MAAM;IACrB,8FAA8F;IAC9F,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,yBAAyB;IACzB,IAAI,IAAI,MAAM,CAAA;IACd,6GAA6G;IAC7G,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;IACvC,gHAAgH;IAChH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAAA;IAClD,4FAA4F;IAC5F,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,CAAA;IAC/C,iDAAiD;IACjD,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IAC7C;;;;;OAKG;IACH,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,SAAS,CAAA;CACjF;AAsED;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,UAAW,MAAM,KAAG,MAA+B,CAAA;AAEpE;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAEzD,+DAA+D;AAC/D,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAA;IACnC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CACxB;AAKD;;;;GAIG;AACH,eAAO,MAAM,MAAM,QAAS,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,CAC2B,CAAA;AAEjG;;;;;GAKG;AACH,eAAO,MAAM,EAAE,QAAS,OAAO,KAAG,GAAG,IAAI,KAAqB,CAAA;AAE9D;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,KAAK,CAG/C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,IAAI,UAAW,MAAM,KAAG,KAA+B,CAAA;AAmBpE;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,SAAU,SAAS,CAAC,KAAK,CAAC,KAAG,MAA8B,CAAA"}
|
package/dist/Random.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import * as Result from "@typeonce/ritmo/Result";
|
|
2
|
-
import { codecOf } from "./internal/codec.js";
|
|
3
|
-
const usable = (weight) => weight > 0 && weight < Infinity;
|
|
4
|
-
class Mulberry32 {
|
|
5
|
-
state;
|
|
6
|
-
constructor(state) {
|
|
7
|
-
this.state = state >>> 0;
|
|
8
|
-
}
|
|
9
|
-
next() {
|
|
10
|
-
this.state = (this.state + 0x6d2b79f5) >>> 0;
|
|
11
|
-
let t = this.state;
|
|
12
|
-
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
13
|
-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
14
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
15
|
-
}
|
|
16
|
-
range(min, max) {
|
|
17
|
-
if (!(Number.isFinite(min) && Number.isFinite(max) && min <= max)) {
|
|
18
|
-
throw new Error(`Random.range needs finite bounds with min <= max, got [${min}, ${max})`);
|
|
19
|
-
}
|
|
20
|
-
return min + (max - min) * this.next();
|
|
21
|
-
}
|
|
22
|
-
integer(min, maxExclusive) {
|
|
23
|
-
if (!(Number.isSafeInteger(min) && Number.isSafeInteger(maxExclusive) && min < maxExclusive)) {
|
|
24
|
-
throw new Error(`Random.integer needs whole bounds with min below maxExclusive, got [${min}, ${maxExclusive})`);
|
|
25
|
-
}
|
|
26
|
-
return min + Math.floor(this.next() * (maxExclusive - min));
|
|
27
|
-
}
|
|
28
|
-
pick(items) {
|
|
29
|
-
return items.length === 0 ? undefined : items[Math.floor(this.next() * items.length)];
|
|
30
|
-
}
|
|
31
|
-
shuffle(items) {
|
|
32
|
-
const shuffled = items.slice();
|
|
33
|
-
for (let index = shuffled.length - 1; index > 0; index--) {
|
|
34
|
-
const other = Math.floor(this.next() * (index + 1));
|
|
35
|
-
const swap = shuffled[index];
|
|
36
|
-
shuffled[index] = shuffled[other];
|
|
37
|
-
shuffled[other] = swap;
|
|
38
|
-
}
|
|
39
|
-
return shuffled;
|
|
40
|
-
}
|
|
41
|
-
weighted(items, weight) {
|
|
42
|
-
// `weight` is called once per item: it may be costly or not pure.
|
|
43
|
-
const weights = items.map(weight);
|
|
44
|
-
let total = 0;
|
|
45
|
-
for (const value of weights) {
|
|
46
|
-
if (usable(value))
|
|
47
|
-
total += value;
|
|
48
|
-
}
|
|
49
|
-
if (!usable(total))
|
|
50
|
-
return undefined;
|
|
51
|
-
let roll = this.next() * total;
|
|
52
|
-
let last;
|
|
53
|
-
for (let index = 0; index < items.length; index++) {
|
|
54
|
-
const value = weights[index];
|
|
55
|
-
if (!usable(value))
|
|
56
|
-
continue;
|
|
57
|
-
last = items[index];
|
|
58
|
-
roll -= value;
|
|
59
|
-
if (roll < 0)
|
|
60
|
-
return items[index];
|
|
61
|
-
}
|
|
62
|
-
// Rounding can leave a sliver of the roll past the last bucket.
|
|
63
|
-
return last;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* A generator continuing from `state`. Any number is accepted and reduced to
|
|
68
|
-
* an unsigned 32-bit integer (`state >>> 0`), so a stored state round-trips
|
|
69
|
-
* exactly.
|
|
70
|
-
*
|
|
71
|
-
* @category Construction
|
|
72
|
-
*/
|
|
73
|
-
export const make = (state) => new Mulberry32(state);
|
|
74
|
-
const isState = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 0xffffffff;
|
|
75
|
-
/**
|
|
76
|
-
* Validates a state, for game code that builds one from a number.
|
|
77
|
-
*
|
|
78
|
-
* @category Resource
|
|
79
|
-
*/
|
|
80
|
-
export const result = (raw) => isState(raw) ? Result.success(raw) : Result.failure({ _tag: "InvalidRandomState", value: raw });
|
|
81
|
-
/**
|
|
82
|
-
* Whether any value is a generator state: a guard, also for building a
|
|
83
|
-
* state field in your own schema (`Schema.declare(Random.is)`).
|
|
84
|
-
*
|
|
85
|
-
* @category Resource
|
|
86
|
-
*/
|
|
87
|
-
export const is = (raw) => isState(raw);
|
|
88
|
-
/**
|
|
89
|
-
* The codec of a random resource's state (a Standard Schema, see
|
|
90
|
-
* `Descriptor.Codec`): `Descriptor.ConstructedResource(Random.codec)`. Its
|
|
91
|
-
* saved form is the number itself, so it needs no `encode`.
|
|
92
|
-
*
|
|
93
|
-
* @category Resource
|
|
94
|
-
*/
|
|
95
|
-
export const codec = codecOf((raw) => isState(raw) ? Result.success(raw) : Result.failure({ _tag: "InvalidRandomState", value: raw }), "a generator state: an unsigned 32-bit integer");
|
|
96
|
-
/**
|
|
97
|
-
* The starting state for a seed, reduced to an unsigned 32-bit integer like
|
|
98
|
-
* `make`: the value to give a random resource in `Game.Runtime.make`.
|
|
99
|
-
*
|
|
100
|
-
* @category Resource
|
|
101
|
-
*/
|
|
102
|
-
export const seed = (value) => (value >>> 0);
|
|
103
|
-
class CellRandom extends Mulberry32 {
|
|
104
|
-
#cell;
|
|
105
|
-
constructor(cell) {
|
|
106
|
-
super(cell.get());
|
|
107
|
-
this.#cell = cell;
|
|
108
|
-
}
|
|
109
|
-
next() {
|
|
110
|
-
// Read the cell each time, so two generators over one cell share one sequence.
|
|
111
|
-
this.state = this.#cell.get();
|
|
112
|
-
const value = super.next();
|
|
113
|
-
this.#cell.set(this.state);
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* A generator over a random resource's write cell: every draw continues from
|
|
119
|
-
* the stored state and writes the new one back, so the system needs no
|
|
120
|
-
* `set` of its own, a failed run rolls the draws back, and generators made
|
|
121
|
-
* from the same cell in one run continue one sequence.
|
|
122
|
-
*
|
|
123
|
-
* @category Resource
|
|
124
|
-
*/
|
|
125
|
-
export const draw = (cell) => new CellRandom(cell);
|
|
126
|
-
//# sourceMappingURL=Random.js.map
|
package/dist/Random.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Random.js","sourceRoot":"","sources":["../src/Random.ts"],"names":[],"mappings":"AAsCA,OAAO,KAAK,MAAM,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AA6B7C,MAAM,MAAM,GAAG,CAAC,MAAc,EAAW,EAAE,CAAC,MAAM,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AAC,MAAM,EAAE,GAAG,CAAC,GAAY,EAAgB,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AAE9D;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,KAAK,GAAkC,OAAO,CACzD,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,oBAAoB,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,EACxG,+CAA+C,CAChD,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,KAAa,EAAS,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,CAAU,CAAA;AAEpE,MAAM,UAAW,SAAQ,UAAU;IACxB,KAAK,CAAkB;IAEhC,YAAY,IAAsB;QAChC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACnB,CAAC;IAEQ,IAAI;QACX,+EAA+E;QAC/E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAA;QAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;QAC1B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,KAAc,CAAC,CAAA;QACnC,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,IAAsB,EAAU,EAAE,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAA"}
|
package/dist/Size2.d.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Branded two-dimensional sizes with explicit validation.
|
|
3
|
-
*
|
|
4
|
-
* Sizes model non-negative width/height pairs and are commonly used for
|
|
5
|
-
* colliders, viewports, and AABB dimensions.
|
|
6
|
-
*
|
|
7
|
-
* Reach for this module when authored or host-provided dimensions should be
|
|
8
|
-
* validated once and then carried through gameplay code, runtime bootstrap, or
|
|
9
|
-
* geometry helpers without repeating non-negative checks everywhere.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* // Validate raw viewport dimensions before storing or reusing them.
|
|
14
|
-
* const viewport = Size2.result({ width: 800, height: 600 })
|
|
15
|
-
* if (!viewport.ok) return
|
|
16
|
-
*
|
|
17
|
-
* // Read the branded size later without rechecking width/height validity.
|
|
18
|
-
* const width = Size2.width(viewport.value)
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* @module Size2
|
|
22
|
-
* @docGroup data-structures
|
|
23
|
-
*
|
|
24
|
-
* @categoryDescription Size Types
|
|
25
|
-
* Raw, branded, and error shapes for validated two-dimensional sizes.
|
|
26
|
-
*
|
|
27
|
-
* @categoryDescription Construction
|
|
28
|
-
* Non-throwing constructor boundaries that validate raw width and height input.
|
|
29
|
-
*
|
|
30
|
-
* @categoryDescription Accessors
|
|
31
|
-
* Pure readers and raw views over already-validated size values.
|
|
32
|
-
*/
|
|
33
|
-
import type * as Brand from "@typeonce/ritmo/Brand";
|
|
34
|
-
import type { StandardSchema } from "@typeonce/ritmo/Descriptor";
|
|
35
|
-
import type * as Result from "@typeonce/ritmo/Result";
|
|
36
|
-
import type * as Scalar from "./Scalar.ts";
|
|
37
|
-
/**
|
|
38
|
-
* Raw size input accepted by the public constructors.
|
|
39
|
-
*
|
|
40
|
-
* @category Size Types
|
|
41
|
-
*/
|
|
42
|
-
export type Raw = {
|
|
43
|
-
readonly width: number;
|
|
44
|
-
readonly height: number;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Validated non-negative width/height pair.
|
|
48
|
-
*
|
|
49
|
-
* @category Size Types
|
|
50
|
-
*/
|
|
51
|
-
export type Size2 = Brand.Branded<{
|
|
52
|
-
readonly width: Scalar.NonNegative;
|
|
53
|
-
readonly height: Scalar.NonNegative;
|
|
54
|
-
}, "Size2">;
|
|
55
|
-
/**
|
|
56
|
-
* Structured size-construction failure.
|
|
57
|
-
*
|
|
58
|
-
* @category Size Types
|
|
59
|
-
*/
|
|
60
|
-
export type Error = {
|
|
61
|
-
readonly tag: "Size2/Invalid";
|
|
62
|
-
readonly width: Scalar.Error | null;
|
|
63
|
-
readonly height: Scalar.Error | null;
|
|
64
|
-
readonly input: Raw;
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* Validates one raw size and returns an explicit result.
|
|
68
|
-
*
|
|
69
|
-
* @category Construction
|
|
70
|
-
*/
|
|
71
|
-
export declare const result: (raw: Raw) => Result.Result<Size2, Error>;
|
|
72
|
-
/**
|
|
73
|
-
* The codec of `Size2` (a Standard Schema, see `Descriptor.Codec`): give it to
|
|
74
|
-
* `Descriptor.ConstructedComponent(...)` so raw input and saves are validated.
|
|
75
|
-
* Its saved form is the raw shape, so it needs no `encode`. To put a `Size2`
|
|
76
|
-
* inside your own object schema, use {@link is} with your library's guard
|
|
77
|
-
* schema (`Schema.declare`, `z.custom`).
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* const Position = Descriptor.ConstructedComponent(Size2.codec)("Game/Position")
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* @category Construction
|
|
85
|
-
*/
|
|
86
|
-
export declare const codec: StandardSchema<Raw, Size2>;
|
|
87
|
-
/**
|
|
88
|
-
* Validates one raw size and returns `null` on failure.
|
|
89
|
-
*
|
|
90
|
-
* @category Construction
|
|
91
|
-
*/
|
|
92
|
-
export declare const option: (raw: Raw) => Size2 | null;
|
|
93
|
-
/**
|
|
94
|
-
* Whether any value is a valid `Size2`: a guard, also for building a
|
|
95
|
-
* `Size2` field in your own schema (`Schema.declare(Size2.is)`).
|
|
96
|
-
*
|
|
97
|
-
* @category Construction
|
|
98
|
-
*/
|
|
99
|
-
export declare const is: (raw: unknown) => raw is Size2;
|
|
100
|
-
/**
|
|
101
|
-
* Converts one branded size back to a fresh raw object.
|
|
102
|
-
*
|
|
103
|
-
* @category Accessors
|
|
104
|
-
*/
|
|
105
|
-
export declare const toRaw: (size: Size2) => Raw;
|
|
106
|
-
/**
|
|
107
|
-
* Reads the width component of one branded size.
|
|
108
|
-
*
|
|
109
|
-
* @category Accessors
|
|
110
|
-
*/
|
|
111
|
-
export declare const width: (size: Size2) => Scalar.NonNegative;
|
|
112
|
-
/**
|
|
113
|
-
* Reads the height component of one branded size.
|
|
114
|
-
*
|
|
115
|
-
* @category Accessors
|
|
116
|
-
*/
|
|
117
|
-
export declare const height: (size: Size2) => Scalar.NonNegative;
|
|
118
|
-
//# sourceMappingURL=Size2.d.ts.map
|
package/dist/Size2.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Size2.d.ts","sourceRoot":"","sources":["../src/Size2.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,KAAK,KAAK,KAAK,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAEhE,OAAO,KAAK,KAAK,MAAM,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAAK,KAAK,MAAM,MAAM,aAAa,CAAA;AAE1C;;;;GAIG;AACH,MAAM,MAAM,GAAG,GAAG;IAChB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAA;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAA;CACpC,EAAE,OAAO,CAAC,CAAA;AAEX;;;;GAIG;AACH,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,GAAG,EAAE,eAAe,CAAA;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI,CAAA;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI,CAAA;IACpC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAA;CACpB,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAmB,CAAA;AAChF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,KAAK,EAAE,cAAc,CAAC,GAAG,EAAE,KAAK,CAAkB,CAAA;AAC/D;;;;GAIG;AACH,eAAO,MAAM,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,KAAK,GAAG,IAAsB,CAAA;AACjE;;;;;GAKG;AACH,eAAO,MAAM,EAAE,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,GAAG,IAAI,KAAmB,CAAA;AAC7D;;;;GAIG;AACH,eAAO,MAAM,KAAK,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,GAAoB,CAAA;AACzD;;;;GAIG;AACH,eAAO,MAAM,KAAK,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC,WAA4B,CAAA;AACxE;;;;GAIG;AACH,eAAO,MAAM,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC,WAA6B,CAAA"}
|
package/dist/Size2.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import * as internal from "./internal/size2.js";
|
|
2
|
-
/**
|
|
3
|
-
* Validates one raw size and returns an explicit result.
|
|
4
|
-
*
|
|
5
|
-
* @category Construction
|
|
6
|
-
*/
|
|
7
|
-
export const result = internal.result;
|
|
8
|
-
/**
|
|
9
|
-
* The codec of `Size2` (a Standard Schema, see `Descriptor.Codec`): give it to
|
|
10
|
-
* `Descriptor.ConstructedComponent(...)` so raw input and saves are validated.
|
|
11
|
-
* Its saved form is the raw shape, so it needs no `encode`. To put a `Size2`
|
|
12
|
-
* inside your own object schema, use {@link is} with your library's guard
|
|
13
|
-
* schema (`Schema.declare`, `z.custom`).
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const Position = Descriptor.ConstructedComponent(Size2.codec)("Game/Position")
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @category Construction
|
|
21
|
-
*/
|
|
22
|
-
export const codec = internal.codec;
|
|
23
|
-
/**
|
|
24
|
-
* Validates one raw size and returns `null` on failure.
|
|
25
|
-
*
|
|
26
|
-
* @category Construction
|
|
27
|
-
*/
|
|
28
|
-
export const option = internal.option;
|
|
29
|
-
/**
|
|
30
|
-
* Whether any value is a valid `Size2`: a guard, also for building a
|
|
31
|
-
* `Size2` field in your own schema (`Schema.declare(Size2.is)`).
|
|
32
|
-
*
|
|
33
|
-
* @category Construction
|
|
34
|
-
*/
|
|
35
|
-
export const is = internal.is;
|
|
36
|
-
/**
|
|
37
|
-
* Converts one branded size back to a fresh raw object.
|
|
38
|
-
*
|
|
39
|
-
* @category Accessors
|
|
40
|
-
*/
|
|
41
|
-
export const toRaw = internal.toRaw;
|
|
42
|
-
/**
|
|
43
|
-
* Reads the width component of one branded size.
|
|
44
|
-
*
|
|
45
|
-
* @category Accessors
|
|
46
|
-
*/
|
|
47
|
-
export const width = internal.width;
|
|
48
|
-
/**
|
|
49
|
-
* Reads the height component of one branded size.
|
|
50
|
-
*
|
|
51
|
-
* @category Accessors
|
|
52
|
-
*/
|
|
53
|
-
export const height = internal.height;
|
|
54
|
-
//# sourceMappingURL=Size2.js.map
|
package/dist/Size2.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Size2.js","sourceRoot":"","sources":["../src/Size2.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAoC/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAA8C,QAAQ,CAAC,MAAM,CAAA;AAChF;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,KAAK,GAA+B,QAAQ,CAAC,KAAK,CAAA;AAC/D;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAA+B,QAAQ,CAAC,MAAM,CAAA;AACjE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,EAAE,GAAmC,QAAQ,CAAC,EAAE,CAAA;AAC7D;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAyB,QAAQ,CAAC,KAAK,CAAA;AACzD;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAwC,QAAQ,CAAC,KAAK,CAAA;AACxE;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAwC,QAAQ,CAAC,MAAM,CAAA"}
|
package/dist/SpatialGrid.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A uniform spatial hash for broad-phase queries over circles.
|
|
3
|
-
*
|
|
4
|
-
* Rebuilt every step: `clear`, then `insert` each circle under the caller's
|
|
5
|
-
* own index (its position in an array the caller keeps). Queries visit each
|
|
6
|
-
* candidate whose cells overlap the query box once; the caller runs the
|
|
7
|
-
* exact test. Cell arrays are reused between rebuilds, so a steady state
|
|
8
|
-
* allocates nothing per step.
|
|
9
|
-
*
|
|
10
|
-
* Cells are keyed by packing their coordinates into 16 bits per axis, so
|
|
11
|
-
* cells from -32768 to 32767 on each axis are distinct. Coordinates beyond
|
|
12
|
-
* that share keys with other cells: a query then visits extra candidates,
|
|
13
|
-
* but never misses one.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const grid = SpatialGrid.make(cellSize)
|
|
18
|
-
* // Each step:
|
|
19
|
-
* grid.clear()
|
|
20
|
-
* bodies.forEach((body, index) => grid.insert(index, body.x, body.y, body.r))
|
|
21
|
-
* grid.forEachIn(x - range, y - range, x + range, y + range, (index) => {
|
|
22
|
-
* const other = bodies[index]!
|
|
23
|
-
* // exact test against `other`
|
|
24
|
-
* })
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @module SpatialGrid
|
|
28
|
-
* @docGroup helpers
|
|
29
|
-
*
|
|
30
|
-
* @categoryDescription Grid Types
|
|
31
|
-
* The grid interface.
|
|
32
|
-
*
|
|
33
|
-
* @categoryDescription Construction
|
|
34
|
-
* Making a grid.
|
|
35
|
-
*/
|
|
36
|
-
import type * as Scalar from "./Scalar.ts";
|
|
37
|
-
/**
|
|
38
|
-
* A spatial hash over circles.
|
|
39
|
-
*
|
|
40
|
-
* @category Grid Types
|
|
41
|
-
*/
|
|
42
|
-
export interface SpatialGrid {
|
|
43
|
-
/** The side of one square cell, in world units. */
|
|
44
|
-
readonly cellSize: Scalar.Positive;
|
|
45
|
-
/** Removes every circle, keeping the storage for the next rebuild. */
|
|
46
|
-
clear(): void;
|
|
47
|
-
/**
|
|
48
|
-
* Adds circle `index` (a non-negative integer) covering `[x ± r] × [y ± r]`.
|
|
49
|
-
* A non-finite coordinate or radius throws: it would cover no cell or all of them.
|
|
50
|
-
*/
|
|
51
|
-
insert(index: number, x: number, y: number, r: number): void;
|
|
52
|
-
/**
|
|
53
|
-
* Calls `visit` once for every circle whose cells overlap the box, in no
|
|
54
|
-
* particular order. `visit` must not insert into or query this grid. A
|
|
55
|
-
* non-finite bound throws (an unbounded box would never finish); a box
|
|
56
|
-
* covering more cells than are occupied scans the occupied cells instead.
|
|
57
|
-
*/
|
|
58
|
-
forEachIn(minX: number, minY: number, maxX: number, maxY: number, visit: (index: number) => void): void;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* An empty grid with square cells of `cellSize` world units. Pick a size near
|
|
62
|
-
* the typical query box: much smaller cells make every insert and query
|
|
63
|
-
* touch many cells, much larger ones return many far candidates.
|
|
64
|
-
*
|
|
65
|
-
* @category Construction
|
|
66
|
-
*/
|
|
67
|
-
export declare const make: (cellSize: Scalar.Positive) => SpatialGrid;
|
|
68
|
-
//# sourceMappingURL=SpatialGrid.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SpatialGrid.d.ts","sourceRoot":"","sources":["../src/SpatialGrid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,KAAK,MAAM,MAAM,aAAa,CAAA;AAE1C;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAA;IAClC,sEAAsE;IACtE,KAAK,IAAI,IAAI,CAAA;IACb;;;OAGG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5D;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAAA;CACxG;AAyGD;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,aAAc,MAAM,CAAC,QAAQ,KAAG,WAAiC,CAAA"}
|
package/dist/SpatialGrid.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
const key = (cx, cy) => ((cx + 32768) << 16) | ((cy + 32768) & 0xffff);
|
|
2
|
-
// Keys repeat every 65,536 cells, so a longer span visits nothing new.
|
|
3
|
-
const AXIS_CELLS = 65_536;
|
|
4
|
-
const lastCell = (first, last) => Math.min(last, first + AXIS_CELLS - 1);
|
|
5
|
-
/** Whether a cell coordinate packed in a key falls in `[first, last]`, counting the keys' wrap-around. */
|
|
6
|
-
const coveredBy = (coordinate, first, last) => (((coordinate - first) % AXIS_CELLS) + AXIS_CELLS) % AXIS_CELLS <= last - first;
|
|
7
|
-
const notFinite = (operation, ...values) => {
|
|
8
|
-
const value = values.find((value) => !Number.isFinite(value));
|
|
9
|
-
throw new Error(`SpatialGrid.${operation} needs finite numbers, got ${value}`);
|
|
10
|
-
};
|
|
11
|
-
class Grid {
|
|
12
|
-
cellSize;
|
|
13
|
-
cells = new Map();
|
|
14
|
-
used = [];
|
|
15
|
-
// `stamp[index] === query` marks a circle already visited by this query.
|
|
16
|
-
stamp = new Int32Array(64);
|
|
17
|
-
query = 0;
|
|
18
|
-
constructor(cellSize) {
|
|
19
|
-
this.cellSize = cellSize;
|
|
20
|
-
}
|
|
21
|
-
clear() {
|
|
22
|
-
for (const cell of this.used)
|
|
23
|
-
cell.length = 0;
|
|
24
|
-
this.used.length = 0;
|
|
25
|
-
}
|
|
26
|
-
insert(index, x, y, r) {
|
|
27
|
-
// `x - x === 0` is false only for NaN and ±Infinity, without a call per number.
|
|
28
|
-
if (x - x !== 0 || y - y !== 0 || r - r !== 0)
|
|
29
|
-
notFinite("insert", x, y, r);
|
|
30
|
-
const size = this.cellSize;
|
|
31
|
-
const minX = Math.floor((x - r) / size);
|
|
32
|
-
const maxX = lastCell(minX, Math.floor((x + r) / size));
|
|
33
|
-
const minY = Math.floor((y - r) / size);
|
|
34
|
-
const maxY = lastCell(minY, Math.floor((y + r) / size));
|
|
35
|
-
for (let cx = minX; cx <= maxX; cx++) {
|
|
36
|
-
for (let cy = minY; cy <= maxY; cy++) {
|
|
37
|
-
const cellKey = key(cx, cy);
|
|
38
|
-
let cell = this.cells.get(cellKey);
|
|
39
|
-
if (cell === undefined) {
|
|
40
|
-
cell = [];
|
|
41
|
-
this.cells.set(cellKey, cell);
|
|
42
|
-
}
|
|
43
|
-
if (cell.length === 0)
|
|
44
|
-
this.used.push(cell);
|
|
45
|
-
cell.push(index);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (index >= this.stamp.length) {
|
|
49
|
-
const grown = new Int32Array(Math.max(index + 1, this.stamp.length * 2));
|
|
50
|
-
grown.set(this.stamp);
|
|
51
|
-
this.stamp = grown;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
forEachIn(minX, minY, maxX, maxY, visit) {
|
|
55
|
-
if (minX - minX !== 0 || minY - minY !== 0 || maxX - maxX !== 0 || maxY - maxY !== 0)
|
|
56
|
-
notFinite("forEachIn", minX, minY, maxX, maxY);
|
|
57
|
-
const size = this.cellSize;
|
|
58
|
-
// Start the marks over before the counter leaves the stamps' range (after
|
|
59
|
-
// about two billion queries), so old marks never match a new query.
|
|
60
|
-
if (this.query === 0x7fffffff) {
|
|
61
|
-
this.stamp.fill(0);
|
|
62
|
-
this.query = 0;
|
|
63
|
-
}
|
|
64
|
-
this.query += 1;
|
|
65
|
-
const query = this.query;
|
|
66
|
-
const stamp = this.stamp;
|
|
67
|
-
const x0 = Math.floor(minX / size);
|
|
68
|
-
const x1 = lastCell(x0, Math.floor(maxX / size));
|
|
69
|
-
const y0 = Math.floor(minY / size);
|
|
70
|
-
const y1 = lastCell(y0, Math.floor(maxY / size));
|
|
71
|
-
if ((x1 - x0 + 1) * (y1 - y0 + 1) > this.cells.size) {
|
|
72
|
-
// A box larger than the occupied area: scan the cells that exist.
|
|
73
|
-
for (const [cellKey, cell] of this.cells) {
|
|
74
|
-
if (cell.length === 0)
|
|
75
|
-
continue;
|
|
76
|
-
if (!coveredBy(((cellKey >> 16) & 0xffff) - 32768, x0, x1) || !coveredBy((cellKey & 0xffff) - 32768, y0, y1))
|
|
77
|
-
continue;
|
|
78
|
-
for (let offset = 0; offset < cell.length; offset++) {
|
|
79
|
-
const index = cell[offset];
|
|
80
|
-
if (stamp[index] === query)
|
|
81
|
-
continue;
|
|
82
|
-
stamp[index] = query;
|
|
83
|
-
visit(index);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
for (let cx = x0; cx <= x1; cx++) {
|
|
89
|
-
for (let cy = y0; cy <= y1; cy++) {
|
|
90
|
-
const cell = this.cells.get(key(cx, cy));
|
|
91
|
-
if (cell === undefined)
|
|
92
|
-
continue;
|
|
93
|
-
for (let offset = 0; offset < cell.length; offset++) {
|
|
94
|
-
const index = cell[offset];
|
|
95
|
-
if (stamp[index] === query)
|
|
96
|
-
continue;
|
|
97
|
-
stamp[index] = query;
|
|
98
|
-
visit(index);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* An empty grid with square cells of `cellSize` world units. Pick a size near
|
|
106
|
-
* the typical query box: much smaller cells make every insert and query
|
|
107
|
-
* touch many cells, much larger ones return many far candidates.
|
|
108
|
-
*
|
|
109
|
-
* @category Construction
|
|
110
|
-
*/
|
|
111
|
-
export const make = (cellSize) => new Grid(cellSize);
|
|
112
|
-
//# sourceMappingURL=SpatialGrid.js.map
|
package/dist/SpatialGrid.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SpatialGrid.js","sourceRoot":"","sources":["../src/SpatialGrid.ts"],"names":[],"mappings":"AA6DA,MAAM,GAAG,GAAG,CAAC,EAAU,EAAE,EAAU,EAAU,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC,CAAA;AAE9F,uEAAuE;AACvE,MAAM,UAAU,GAAG,MAAM,CAAA;AACzB,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,IAAY,EAAU,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,GAAG,UAAU,GAAG,CAAC,CAAC,CAAA;AAChG,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