@typeonce/ritmo-math 0.14.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 +104 -0
- package/README.md +17 -12
- 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 +11 -1
- package/dist/Collide2.d.ts.map +1 -1
- package/dist/Collide2.js +16 -1
- 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/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 +9 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -22
- 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 +18 -2
- package/src/FlowField.ts +34 -10
- package/src/Scalar.ts +43 -165
- package/src/Vec2.ts +58 -8
- package/src/index.ts +9 -22
- 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/src/SpatialGrid.ts
DELETED
|
@@ -1,172 +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
|
-
/**
|
|
39
|
-
* A spatial hash over circles.
|
|
40
|
-
*
|
|
41
|
-
* @category Grid Types
|
|
42
|
-
*/
|
|
43
|
-
export interface SpatialGrid {
|
|
44
|
-
/** The side of one square cell, in world units. */
|
|
45
|
-
readonly cellSize: Scalar.Positive
|
|
46
|
-
/** Removes every circle, keeping the storage for the next rebuild. */
|
|
47
|
-
clear(): void
|
|
48
|
-
/**
|
|
49
|
-
* Adds circle `index` (a non-negative integer) covering `[x ± r] × [y ± r]`.
|
|
50
|
-
* A non-finite coordinate or radius throws: it would cover no cell or all of them.
|
|
51
|
-
*/
|
|
52
|
-
insert(index: number, x: number, y: number, r: number): void
|
|
53
|
-
/**
|
|
54
|
-
* Calls `visit` once for every circle whose cells overlap the box, in no
|
|
55
|
-
* particular order. `visit` must not insert into or query this grid. A
|
|
56
|
-
* non-finite bound throws (an unbounded box would never finish); a box
|
|
57
|
-
* covering more cells than are occupied scans the occupied cells instead.
|
|
58
|
-
*/
|
|
59
|
-
forEachIn(minX: number, minY: number, maxX: number, maxY: number, visit: (index: number) => void): void
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const key = (cx: number, cy: number): number => ((cx + 32768) << 16) | ((cy + 32768) & 0xffff)
|
|
63
|
-
|
|
64
|
-
// Keys repeat every 65,536 cells, so a longer span visits nothing new.
|
|
65
|
-
const AXIS_CELLS = 65_536
|
|
66
|
-
const lastCell = (first: number, last: number): number => Math.min(last, first + AXIS_CELLS - 1)
|
|
67
|
-
/** Whether a cell coordinate packed in a key falls in `[first, last]`, counting the keys' wrap-around. */
|
|
68
|
-
const coveredBy = (coordinate: number, first: number, last: number): boolean =>
|
|
69
|
-
(((coordinate - first) % AXIS_CELLS) + AXIS_CELLS) % AXIS_CELLS <= last - first
|
|
70
|
-
|
|
71
|
-
const notFinite = (operation: string, ...values: ReadonlyArray<number>): never => {
|
|
72
|
-
const value = values.find((value) => !Number.isFinite(value))
|
|
73
|
-
throw new Error(`SpatialGrid.${operation} needs finite numbers, got ${value}`)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
class Grid implements SpatialGrid {
|
|
77
|
-
readonly cellSize: Scalar.Positive
|
|
78
|
-
private readonly cells = new Map<number, Array<number>>()
|
|
79
|
-
private readonly used: Array<Array<number>> = []
|
|
80
|
-
// `stamp[index] === query` marks a circle already visited by this query.
|
|
81
|
-
private stamp = new Int32Array(64)
|
|
82
|
-
private query = 0
|
|
83
|
-
|
|
84
|
-
constructor(cellSize: Scalar.Positive) {
|
|
85
|
-
this.cellSize = cellSize
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
clear(): void {
|
|
89
|
-
for (const cell of this.used) cell.length = 0
|
|
90
|
-
this.used.length = 0
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
insert(index: number, x: number, y: number, r: number): void {
|
|
94
|
-
// `x - x === 0` is false only for NaN and ±Infinity, without a call per number.
|
|
95
|
-
if (x - x !== 0 || y - y !== 0 || r - r !== 0) notFinite("insert", x, y, r)
|
|
96
|
-
const size = this.cellSize
|
|
97
|
-
const minX = Math.floor((x - r) / size)
|
|
98
|
-
const maxX = lastCell(minX, Math.floor((x + r) / size))
|
|
99
|
-
const minY = Math.floor((y - r) / size)
|
|
100
|
-
const maxY = lastCell(minY, Math.floor((y + r) / size))
|
|
101
|
-
for (let cx = minX; cx <= maxX; cx++) {
|
|
102
|
-
for (let cy = minY; cy <= maxY; cy++) {
|
|
103
|
-
const cellKey = key(cx, cy)
|
|
104
|
-
let cell = this.cells.get(cellKey)
|
|
105
|
-
if (cell === undefined) {
|
|
106
|
-
cell = []
|
|
107
|
-
this.cells.set(cellKey, cell)
|
|
108
|
-
}
|
|
109
|
-
if (cell.length === 0) this.used.push(cell)
|
|
110
|
-
cell.push(index)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (index >= this.stamp.length) {
|
|
114
|
-
const grown = new Int32Array(Math.max(index + 1, this.stamp.length * 2))
|
|
115
|
-
grown.set(this.stamp)
|
|
116
|
-
this.stamp = grown
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
forEachIn(minX: number, minY: number, maxX: number, maxY: number, visit: (index: number) => void): void {
|
|
121
|
-
if (minX - minX !== 0 || minY - minY !== 0 || maxX - maxX !== 0 || maxY - maxY !== 0) notFinite("forEachIn", minX, minY, maxX, maxY)
|
|
122
|
-
const size = this.cellSize
|
|
123
|
-
// Start the marks over before the counter leaves the stamps' range (after
|
|
124
|
-
// about two billion queries), so old marks never match a new query.
|
|
125
|
-
if (this.query === 0x7fffffff) {
|
|
126
|
-
this.stamp.fill(0)
|
|
127
|
-
this.query = 0
|
|
128
|
-
}
|
|
129
|
-
this.query += 1
|
|
130
|
-
const query = this.query
|
|
131
|
-
const stamp = this.stamp
|
|
132
|
-
const x0 = Math.floor(minX / size)
|
|
133
|
-
const x1 = lastCell(x0, Math.floor(maxX / size))
|
|
134
|
-
const y0 = Math.floor(minY / size)
|
|
135
|
-
const y1 = lastCell(y0, Math.floor(maxY / size))
|
|
136
|
-
if ((x1 - x0 + 1) * (y1 - y0 + 1) > this.cells.size) {
|
|
137
|
-
// A box larger than the occupied area: scan the cells that exist.
|
|
138
|
-
for (const [cellKey, cell] of this.cells) {
|
|
139
|
-
if (cell.length === 0) continue
|
|
140
|
-
if (!coveredBy(((cellKey >> 16) & 0xffff) - 32768, x0, x1) || !coveredBy((cellKey & 0xffff) - 32768, y0, y1)) continue
|
|
141
|
-
for (let offset = 0; offset < cell.length; offset++) {
|
|
142
|
-
const index = cell[offset]!
|
|
143
|
-
if (stamp[index] === query) continue
|
|
144
|
-
stamp[index] = query
|
|
145
|
-
visit(index)
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return
|
|
149
|
-
}
|
|
150
|
-
for (let cx = x0; cx <= x1; cx++) {
|
|
151
|
-
for (let cy = y0; cy <= y1; cy++) {
|
|
152
|
-
const cell = this.cells.get(key(cx, cy))
|
|
153
|
-
if (cell === undefined) continue
|
|
154
|
-
for (let offset = 0; offset < cell.length; offset++) {
|
|
155
|
-
const index = cell[offset]!
|
|
156
|
-
if (stamp[index] === query) continue
|
|
157
|
-
stamp[index] = query
|
|
158
|
-
visit(index)
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* An empty grid with square cells of `cellSize` world units. Pick a size near
|
|
167
|
-
* the typical query box: much smaller cells make every insert and query
|
|
168
|
-
* touch many cells, much larger ones return many far candidates.
|
|
169
|
-
*
|
|
170
|
-
* @category Construction
|
|
171
|
-
*/
|
|
172
|
-
export const make = (cellSize: Scalar.Positive): SpatialGrid => new Grid(cellSize)
|
package/src/Vector2.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Branded two-dimensional vectors with explicit validation.
|
|
3
|
-
*
|
|
4
|
-
* Raw `{ x, y }` input is validated once at the constructor boundary. All
|
|
5
|
-
* other helpers work on branded vectors only and remain pure and immutable.
|
|
6
|
-
*
|
|
7
|
-
* This is the common math value that game code threads through position,
|
|
8
|
-
* velocity, input direction, camera motion, and collision helpers. Use it when
|
|
9
|
-
* vectors should stay explicitly validated and reusable across ECS and helper
|
|
10
|
-
* modules. For per-frame arithmetic on numbers already known to be sane, use
|
|
11
|
-
* `Vec2` (a `Vector2` is a `Vec2`, so it passes without conversion).
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* // Validate one raw velocity before storing or reusing it elsewhere.
|
|
16
|
-
* const velocity = Vector2.result({ x: 3, y: 4 })
|
|
17
|
-
* if (!velocity.ok) return
|
|
18
|
-
*
|
|
19
|
-
* // Derive a movement direction from the branded vector.
|
|
20
|
-
* const direction = Vector2.normalizeOrZero(velocity.value)
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* @module Vector2
|
|
24
|
-
* @docGroup data-structures
|
|
25
|
-
*
|
|
26
|
-
* @categoryDescription Vector Types
|
|
27
|
-
* Raw, branded, and error shapes used to describe validated two-dimensional vectors.
|
|
28
|
-
*
|
|
29
|
-
* @categoryDescription Construction
|
|
30
|
-
* Non-throwing constructor boundaries that validate raw input and produce branded vectors.
|
|
31
|
-
*
|
|
32
|
-
* @categoryDescription Accessors
|
|
33
|
-
* Pure readers and views that expose branded vector components without mutation.
|
|
34
|
-
*
|
|
35
|
-
* @categoryDescription Operations
|
|
36
|
-
* Immutable vector math and normalization helpers on already-validated values.
|
|
37
|
-
*/
|
|
38
|
-
import type * as Brand from "@typeonce/ritmo/Brand"
|
|
39
|
-
import type { StandardSchema } from "@typeonce/ritmo/Descriptor"
|
|
40
|
-
import * as internal from "./internal/vector2.ts"
|
|
41
|
-
import type * as Result from "@typeonce/ritmo/Result"
|
|
42
|
-
import type * as Scalar from "./Scalar.ts"
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Raw vector input accepted by the public constructors.
|
|
46
|
-
*
|
|
47
|
-
* @category Vector Types
|
|
48
|
-
*/
|
|
49
|
-
export type Raw = {
|
|
50
|
-
readonly x: number
|
|
51
|
-
readonly y: number
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Validated two-dimensional vector.
|
|
56
|
-
*
|
|
57
|
-
* @category Vector Types
|
|
58
|
-
*/
|
|
59
|
-
export type Vector2 = Brand.Branded<{
|
|
60
|
-
readonly x: Scalar.Finite
|
|
61
|
-
readonly y: Scalar.Finite
|
|
62
|
-
}, "Vector2">
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Structured vector-construction failure.
|
|
66
|
-
*
|
|
67
|
-
* @category Vector Types
|
|
68
|
-
*/
|
|
69
|
-
export type Error = {
|
|
70
|
-
readonly tag: "Vector2/Invalid"
|
|
71
|
-
readonly x: Scalar.Error | null
|
|
72
|
-
readonly y: Scalar.Error | null
|
|
73
|
-
readonly input: Raw
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Explicit failure produced when a zero-length vector cannot be normalized.
|
|
78
|
-
*
|
|
79
|
-
* @category Vector Types
|
|
80
|
-
*/
|
|
81
|
-
export type NormalizeError = {
|
|
82
|
-
readonly tag: "Vector2/ZeroLength"
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Validates one raw vector and returns an explicit result.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```ts
|
|
90
|
-
* const position = Vector2.result({ x: 16, y: 24 })
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* @category Construction
|
|
94
|
-
*/
|
|
95
|
-
export const result: (raw: Raw) => Result.Result<Vector2, Error> = internal.result
|
|
96
|
-
/**
|
|
97
|
-
* The codec of `Vector2` (a Standard Schema, see `Descriptor.Codec`): give it to
|
|
98
|
-
* `Descriptor.ConstructedComponent(...)` so raw input and saves are validated.
|
|
99
|
-
* Its saved form is the raw shape, so it needs no `encode`. To put a `Vector2`
|
|
100
|
-
* inside your own object schema, use {@link is} with your library's guard
|
|
101
|
-
* schema (`Schema.declare`, `z.custom`).
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```ts
|
|
105
|
-
* const Position = Descriptor.ConstructedComponent(Vector2.codec)("Game/Position")
|
|
106
|
-
* ```
|
|
107
|
-
*
|
|
108
|
-
* @category Construction
|
|
109
|
-
*/
|
|
110
|
-
export const codec: StandardSchema<Raw, Vector2> = internal.codec
|
|
111
|
-
/**
|
|
112
|
-
* Validates one raw vector and returns `null` on failure.
|
|
113
|
-
*
|
|
114
|
-
* @category Construction
|
|
115
|
-
*/
|
|
116
|
-
export const option: (raw: Raw) => Vector2 | null = internal.option
|
|
117
|
-
/**
|
|
118
|
-
* Whether any value is a valid `Vector2`: a guard, also for building a
|
|
119
|
-
* `Vector2` field in your own schema (`Schema.declare(Vector2.is)`).
|
|
120
|
-
*
|
|
121
|
-
* @category Construction
|
|
122
|
-
*/
|
|
123
|
-
export const is: (raw: unknown) => raw is Vector2 = internal.is
|
|
124
|
-
/**
|
|
125
|
-
* Converts one branded vector back to a fresh raw object.
|
|
126
|
-
*
|
|
127
|
-
* @category Accessors
|
|
128
|
-
*/
|
|
129
|
-
export const toRaw: (vector: Vector2) => Raw = internal.toRaw
|
|
130
|
-
/**
|
|
131
|
-
* Exposes the branded components of one vector.
|
|
132
|
-
*
|
|
133
|
-
* @category Accessors
|
|
134
|
-
*/
|
|
135
|
-
export const components: (vector: Vector2) => { readonly x: Scalar.Finite; readonly y: Scalar.Finite } = internal.components
|
|
136
|
-
/**
|
|
137
|
-
* Reads the `x` component of one branded vector.
|
|
138
|
-
*
|
|
139
|
-
* @category Accessors
|
|
140
|
-
*/
|
|
141
|
-
export const x: (vector: Vector2) => Scalar.Finite = internal.x
|
|
142
|
-
/**
|
|
143
|
-
* Reads the `y` component of one branded vector.
|
|
144
|
-
*
|
|
145
|
-
* @category Accessors
|
|
146
|
-
*/
|
|
147
|
-
export const y: (vector: Vector2) => Scalar.Finite = internal.y
|
|
148
|
-
/**
|
|
149
|
-
* Returns the canonical zero vector.
|
|
150
|
-
*
|
|
151
|
-
* @category Construction
|
|
152
|
-
*/
|
|
153
|
-
export const zero: () => Vector2 = internal.zero
|
|
154
|
-
/**
|
|
155
|
-
* Returns the sum of two branded vectors.
|
|
156
|
-
*
|
|
157
|
-
* Keeps the brands without re-validating: finite while the result stays
|
|
158
|
-
* within `±Number.MAX_VALUE` (see `Scalar.Finite`).
|
|
159
|
-
*
|
|
160
|
-
* @category Operations
|
|
161
|
-
*/
|
|
162
|
-
export const add: (left: Vector2, right: Vector2) => Vector2 = internal.add
|
|
163
|
-
/**
|
|
164
|
-
* Returns the component-wise difference between two branded vectors.
|
|
165
|
-
*
|
|
166
|
-
* Keeps the brands without re-validating: finite while the result stays
|
|
167
|
-
* within `±Number.MAX_VALUE` (see `Scalar.Finite`).
|
|
168
|
-
*
|
|
169
|
-
* @category Operations
|
|
170
|
-
*/
|
|
171
|
-
export const subtract: (left: Vector2, right: Vector2) => Vector2 = internal.subtract
|
|
172
|
-
/**
|
|
173
|
-
* Scales one vector by one validated finite scalar.
|
|
174
|
-
*
|
|
175
|
-
* Keeps the brands without re-validating: finite while the result stays
|
|
176
|
-
* within `±Number.MAX_VALUE` (see `Scalar.Finite`).
|
|
177
|
-
*
|
|
178
|
-
* @category Operations
|
|
179
|
-
*/
|
|
180
|
-
export const scale: (vector: Vector2, scalar: Scalar.Finite) => Vector2 = internal.scale
|
|
181
|
-
/**
|
|
182
|
-
* Returns the squared length of one vector.
|
|
183
|
-
*
|
|
184
|
-
* Keeps the brands without re-validating: finite while the result stays
|
|
185
|
-
* within `±Number.MAX_VALUE` (see `Scalar.Finite`).
|
|
186
|
-
*
|
|
187
|
-
* @category Operations
|
|
188
|
-
*/
|
|
189
|
-
export const lengthSquared: (vector: Vector2) => Scalar.NonNegative = internal.lengthSquared
|
|
190
|
-
/**
|
|
191
|
-
* Returns the length of one vector.
|
|
192
|
-
*
|
|
193
|
-
* @category Operations
|
|
194
|
-
*/
|
|
195
|
-
export const length: (vector: Vector2) => Scalar.NonNegative = internal.length
|
|
196
|
-
/**
|
|
197
|
-
* Returns the normalized vector when its length is non-zero.
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```ts
|
|
201
|
-
* const direction = Vector2.normalize(Vector2.zero())
|
|
202
|
-
* if (!direction.ok) {
|
|
203
|
-
* return
|
|
204
|
-
* }
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* @category Operations
|
|
208
|
-
*/
|
|
209
|
-
export const normalize: (vector: Vector2) => Result.Result<Vector2, NormalizeError> = internal.normalize
|
|
210
|
-
/**
|
|
211
|
-
* Returns the normalized vector or the canonical zero vector when the input
|
|
212
|
-
* length is zero.
|
|
213
|
-
*
|
|
214
|
-
* @category Operations
|
|
215
|
-
*/
|
|
216
|
-
export const normalizeOrZero: (vector: Vector2) => Vector2 = internal.normalizeOrZero
|
package/src/internal/aabb.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { codecOf } from "./codec.ts"
|
|
2
|
-
import * as Result from "@typeonce/ritmo/Result"
|
|
3
|
-
import * as Size2 from "../Size2.ts"
|
|
4
|
-
import * as Vector2 from "../Vector2.ts"
|
|
5
|
-
import type * as Aabb from "../Aabb.ts"
|
|
6
|
-
import type * as Scalar from "../Scalar.ts"
|
|
7
|
-
import { isRawShape as isSize2Shape } from "./size2.ts"
|
|
8
|
-
import { isRawShape as isVector2Shape } from "./vector2.ts"
|
|
9
|
-
|
|
10
|
-
const make = (position: Vector2.Vector2, size: Size2.Size2): Aabb.Aabb =>
|
|
11
|
-
({ position, size }) as Aabb.Aabb
|
|
12
|
-
|
|
13
|
-
export const result = (raw: Aabb.Raw): Result.Result<Aabb.Aabb, Aabb.Error> => {
|
|
14
|
-
const position = Vector2.result(raw.position)
|
|
15
|
-
if (!position.ok) {
|
|
16
|
-
return Result.failure({
|
|
17
|
-
tag: "Aabb/InvalidPosition",
|
|
18
|
-
error: position.error,
|
|
19
|
-
input: raw
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const size = Size2.result(raw.size)
|
|
24
|
-
if (!size.ok) {
|
|
25
|
-
return Result.failure({
|
|
26
|
-
tag: "Aabb/InvalidSize",
|
|
27
|
-
error: size.error,
|
|
28
|
-
input: raw
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return Result.success(make(position.value, size.value))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const option = (raw: Aabb.Raw): Aabb.Aabb | null => {
|
|
36
|
-
const next = result(raw)
|
|
37
|
-
return next.ok ? next.value : null
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const is = (raw: unknown): raw is Aabb.Aabb => decode(raw).ok
|
|
41
|
-
|
|
42
|
-
export const position = (aabb: Aabb.Aabb): Vector2.Vector2 => aabb.position
|
|
43
|
-
export const size = (aabb: Aabb.Aabb): Size2.Size2 => aabb.size
|
|
44
|
-
|
|
45
|
-
export const left = (aabb: Aabb.Aabb): Scalar.Finite =>
|
|
46
|
-
(aabb.position.x - aabb.size.width * 0.5) as Scalar.Finite
|
|
47
|
-
|
|
48
|
-
export const right = (aabb: Aabb.Aabb): Scalar.Finite =>
|
|
49
|
-
(aabb.position.x + aabb.size.width * 0.5) as Scalar.Finite
|
|
50
|
-
|
|
51
|
-
export const top = (aabb: Aabb.Aabb): Scalar.Finite =>
|
|
52
|
-
(aabb.position.y - aabb.size.height * 0.5) as Scalar.Finite
|
|
53
|
-
|
|
54
|
-
export const bottom = (aabb: Aabb.Aabb): Scalar.Finite =>
|
|
55
|
-
(aabb.position.y + aabb.size.height * 0.5) as Scalar.Finite
|
|
56
|
-
|
|
57
|
-
export const intersects = (first: Aabb.Aabb, second: Aabb.Aabb): boolean =>
|
|
58
|
-
Math.abs(first.position.x - second.position.x) * 2 < first.size.width + second.size.width &&
|
|
59
|
-
Math.abs(first.position.y - second.position.y) * 2 < first.size.height + second.size.height
|
|
60
|
-
|
|
61
|
-
export const overlapsHorizontally = (first: Aabb.Aabb, second: Aabb.Aabb): boolean =>
|
|
62
|
-
right(first) > left(second) && left(first) < right(second)
|
|
63
|
-
|
|
64
|
-
export const overlapsVertically = (first: Aabb.Aabb, second: Aabb.Aabb): boolean =>
|
|
65
|
-
bottom(first) > top(second) && top(first) < bottom(second)
|
|
66
|
-
|
|
67
|
-
export const translate = (aabb: Aabb.Aabb, delta: Vector2.Vector2): Aabb.Aabb =>
|
|
68
|
-
make(Vector2.add(aabb.position, delta), aabb.size)
|
|
69
|
-
|
|
70
|
-
/** Untrusted input without the raw shape. */
|
|
71
|
-
type Malformed = { readonly tag: "Aabb/Malformed"; readonly input: unknown }
|
|
72
|
-
|
|
73
|
-
export const decode = (raw: unknown): Result.Result<Aabb.Aabb, Aabb.Error | Malformed> => {
|
|
74
|
-
if (typeof raw !== "object" || raw === null) return Result.failure({ tag: "Aabb/Malformed", input: raw })
|
|
75
|
-
const { position, size } = raw as Record<string, unknown>
|
|
76
|
-
return isVector2Shape(position) && isSize2Shape(size)
|
|
77
|
-
? result({ position, size })
|
|
78
|
-
: Result.failure({ tag: "Aabb/Malformed", input: raw })
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export const codec = codecOf<Aabb.Raw, Aabb.Aabb>(decode, "a box { x, y, width, height } of finite numbers with a non-negative size")
|
package/src/internal/codec.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { StandardSchema } from "@typeonce/ritmo/Descriptor"
|
|
2
|
-
import type * as Result from "@typeonce/ritmo/Result"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The codec of one math type, from its decode: a Standard Schema whose saved
|
|
6
|
-
* form is the raw shape. The value is already plain data, so it needs no
|
|
7
|
-
* `encode`.
|
|
8
|
-
*/
|
|
9
|
-
export const codecOf = <Raw, Value>(
|
|
10
|
-
decode: (raw: unknown) => Result.Result<Value, unknown>,
|
|
11
|
-
expected: string
|
|
12
|
-
): StandardSchema<Raw, Value> => ({
|
|
13
|
-
"~standard": {
|
|
14
|
-
version: 1,
|
|
15
|
-
vendor: "ritmo-math",
|
|
16
|
-
validate: (raw) => {
|
|
17
|
-
const value = decode(raw)
|
|
18
|
-
return value.ok ? { value: value.value } : { issues: [{ message: `Expected ${expected}` }] }
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
})
|