@flighthq/spatial 0.4.0-next.1663.d9c5746 → 0.4.0-next.1694.598ef6f
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/dist/contract.d.ts +1 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +1 -1
- package/dist/contract.js.map +1 -1
- package/dist/explainSpatialIndexing2D.d.ts +3 -0
- package/dist/explainSpatialIndexing2D.d.ts.map +1 -0
- package/dist/{explainSpatialIndexing.js → explainSpatialIndexing2D.js} +2 -2
- package/dist/explainSpatialIndexing2D.js.map +1 -0
- package/dist/formatSpatialIndexingNotice.js +7 -7
- package/dist/formatSpatialIndexingNotice.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/spatialIndex.d.ts +10 -10
- package/dist/spatialIndex.d.ts.map +1 -1
- package/dist/spatialIndex.js +14 -14
- package/dist/spatialIndex.js.map +1 -1
- package/dist/uniformGrid.d.ts +2 -2
- package/dist/uniformGrid.d.ts.map +1 -1
- package/dist/uniformGrid.js +3 -3
- package/dist/uniformGrid.js.map +1 -1
- package/package.json +3 -3
- package/src/explainSpatialIndexing2D.test.ts +81 -0
- package/src/formatSpatialIndexingNotice.test.ts +8 -4
- package/src/spatialIndex.test.ts +65 -65
- package/src/uniformGrid.test.ts +65 -58
- package/dist/explainSpatialIndexing.d.ts +0 -3
- package/dist/explainSpatialIndexing.d.ts.map +0 -1
- package/dist/explainSpatialIndexing.js.map +0 -1
- package/src/explainSpatialIndexing.test.ts +0 -76
package/src/spatialIndex.test.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SpatialIndex2D, SpatialObjectId, SpatialPair } from '@flighthq/types/contract';
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
clearSpatialIndex2D,
|
|
6
|
+
createSpatialIndex2D,
|
|
7
|
+
insertSpatialObject2D,
|
|
8
|
+
querySpatialPairs2D,
|
|
9
|
+
querySpatialPoint2D,
|
|
10
|
+
querySpatialRay2D,
|
|
11
|
+
querySpatialRegion2D,
|
|
12
|
+
removeSpatialObject2D,
|
|
13
|
+
updateSpatialObject2D,
|
|
14
14
|
} from './spatialIndex';
|
|
15
|
-
import {
|
|
15
|
+
import { createUniformGridSpatialBackend2D } from './uniformGrid';
|
|
16
16
|
|
|
17
|
-
function makeIndex(cellSize = 10):
|
|
18
|
-
return
|
|
17
|
+
function makeIndex(cellSize = 10): SpatialIndex2D {
|
|
18
|
+
return createSpatialIndex2D(createUniformGridSpatialBackend2D(cellSize));
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
// Sorts pairs into canonical "a-b" strings so a result can be compared as a set regardless of order.
|
|
@@ -23,58 +23,58 @@ function pairKeys(pairs: readonly SpatialPair[]): string[] {
|
|
|
23
23
|
return pairs.map((p) => `${Math.min(p.a, p.b)}-${Math.max(p.a, p.b)}`).sort();
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
describe('
|
|
26
|
+
describe('clearSpatialIndex2D', () => {
|
|
27
27
|
it('empties every query after clearing', () => {
|
|
28
28
|
const index = makeIndex();
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
30
|
+
insertSpatialObject2D(index, 2, { minX: 2, minY: 2, maxX: 6, maxY: 6 });
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
clearSpatialIndex2D(index);
|
|
33
33
|
|
|
34
34
|
const pairs: SpatialPair[] = [];
|
|
35
|
-
|
|
35
|
+
querySpatialPairs2D(index, pairs);
|
|
36
36
|
expect(pairs).toHaveLength(0);
|
|
37
37
|
|
|
38
38
|
const region: SpatialObjectId[] = [];
|
|
39
|
-
|
|
39
|
+
querySpatialRegion2D(index, { minX: 0, minY: 0, maxX: 10, maxY: 10 }, region);
|
|
40
40
|
expect(region).toHaveLength(0);
|
|
41
41
|
|
|
42
42
|
const point: SpatialObjectId[] = [];
|
|
43
|
-
|
|
43
|
+
querySpatialPoint2D(index, 3, 3, point);
|
|
44
44
|
expect(point).toHaveLength(0);
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
describe('
|
|
48
|
+
describe('createSpatialIndex2D', () => {
|
|
49
49
|
it('defaults to a working uniform grid when given no backend', () => {
|
|
50
|
-
const index =
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const index = createSpatialIndex2D();
|
|
51
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
52
|
+
insertSpatialObject2D(index, 2, { minX: 1, minY: 1, maxX: 5, maxY: 5 });
|
|
53
53
|
|
|
54
54
|
const pairs: SpatialPair[] = [];
|
|
55
|
-
|
|
55
|
+
querySpatialPairs2D(index, pairs);
|
|
56
56
|
expect(pairKeys(pairs)).toEqual(['1-2']);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
it('uses an explicitly supplied backend', () => {
|
|
60
|
-
const index =
|
|
61
|
-
|
|
60
|
+
const index = createSpatialIndex2D(createUniformGridSpatialBackend2D(64));
|
|
61
|
+
insertSpatialObject2D(index, 7, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
62
62
|
|
|
63
63
|
const point: SpatialObjectId[] = [];
|
|
64
|
-
|
|
64
|
+
querySpatialPoint2D(index, 2, 2, point);
|
|
65
65
|
expect(point).toEqual([7]);
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
describe('
|
|
69
|
+
describe('insertSpatialObject2D', () => {
|
|
70
70
|
it('yields exactly the one overlapping pair from two overlapping objects and one far object', () => {
|
|
71
71
|
const index = makeIndex();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
73
|
+
insertSpatialObject2D(index, 2, { minX: 2, minY: 2, maxX: 6, maxY: 6 });
|
|
74
|
+
insertSpatialObject2D(index, 3, { minX: 100, minY: 100, maxX: 104, maxY: 104 });
|
|
75
75
|
|
|
76
76
|
const pairs: SpatialPair[] = [];
|
|
77
|
-
|
|
77
|
+
querySpatialPairs2D(index, pairs);
|
|
78
78
|
|
|
79
79
|
expect(pairs).toHaveLength(1);
|
|
80
80
|
expect(pairKeys(pairs)).toEqual(['1-2']);
|
|
@@ -82,103 +82,103 @@ describe('insertSpatialObject', () => {
|
|
|
82
82
|
});
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
-
describe('
|
|
85
|
+
describe('querySpatialPairs2D', () => {
|
|
86
86
|
it('reuses and clears the out array across calls', () => {
|
|
87
87
|
const index = makeIndex();
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
89
|
+
insertSpatialObject2D(index, 2, { minX: 2, minY: 2, maxX: 6, maxY: 6 });
|
|
90
90
|
|
|
91
91
|
const out: SpatialPair[] = [];
|
|
92
|
-
|
|
92
|
+
querySpatialPairs2D(index, out);
|
|
93
93
|
expect(out).toHaveLength(1);
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
removeSpatialObject2D(index, 2);
|
|
96
|
+
querySpatialPairs2D(index, out);
|
|
97
97
|
expect(out).toHaveLength(0);
|
|
98
98
|
});
|
|
99
99
|
});
|
|
100
100
|
|
|
101
|
-
describe('
|
|
101
|
+
describe('querySpatialPoint2D', () => {
|
|
102
102
|
it('returns an object at an interior point and nothing at an empty point', () => {
|
|
103
103
|
const index = makeIndex();
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
insertSpatialObject2D(index, 1, { minX: 1, minY: 1, maxX: 3, maxY: 3 });
|
|
105
|
+
insertSpatialObject2D(index, 2, { minX: 8, minY: 8, maxX: 9, maxY: 9 });
|
|
106
106
|
|
|
107
107
|
const inside: SpatialObjectId[] = [];
|
|
108
|
-
|
|
108
|
+
querySpatialPoint2D(index, 2, 2, inside);
|
|
109
109
|
expect(inside).toEqual([1]);
|
|
110
110
|
|
|
111
111
|
const outside: SpatialObjectId[] = [];
|
|
112
|
-
|
|
112
|
+
querySpatialPoint2D(index, 6, 6, outside);
|
|
113
113
|
expect(outside).toHaveLength(0);
|
|
114
114
|
});
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
-
describe('
|
|
117
|
+
describe('querySpatialRay2D', () => {
|
|
118
118
|
it('returns an object the ray crosses and nothing for a ray that misses', () => {
|
|
119
119
|
const index = makeIndex();
|
|
120
|
-
|
|
120
|
+
insertSpatialObject2D(index, 1, { minX: 20, minY: 20, maxX: 24, maxY: 24 });
|
|
121
121
|
|
|
122
122
|
const hit: SpatialObjectId[] = [];
|
|
123
|
-
|
|
123
|
+
querySpatialRay2D(index, 0, 22, 1, 0, hit);
|
|
124
124
|
expect(hit).toEqual([1]);
|
|
125
125
|
|
|
126
126
|
const miss: SpatialObjectId[] = [];
|
|
127
|
-
|
|
127
|
+
querySpatialRay2D(index, 0, 0, 1, 0, miss);
|
|
128
128
|
expect(miss).toHaveLength(0);
|
|
129
129
|
});
|
|
130
130
|
});
|
|
131
131
|
|
|
132
|
-
describe('
|
|
132
|
+
describe('querySpatialRegion2D', () => {
|
|
133
133
|
it('includes overlapping bounds and excludes a cell-mate whose bounds miss the region', () => {
|
|
134
134
|
const index = makeIndex();
|
|
135
135
|
// Both objects share grid cell (0,0), but only object 1 actually overlaps the query region.
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
insertSpatialObject2D(index, 1, { minX: 1, minY: 1, maxX: 3, maxY: 3 });
|
|
137
|
+
insertSpatialObject2D(index, 2, { minX: 8, minY: 8, maxX: 9, maxY: 9 });
|
|
138
138
|
|
|
139
139
|
const out: SpatialObjectId[] = [];
|
|
140
|
-
|
|
140
|
+
querySpatialRegion2D(index, { minX: 0, minY: 0, maxX: 4, maxY: 4 }, out);
|
|
141
141
|
|
|
142
142
|
expect(out).toEqual([1]);
|
|
143
143
|
});
|
|
144
144
|
});
|
|
145
145
|
|
|
146
|
-
describe('
|
|
146
|
+
describe('removeSpatialObject2D', () => {
|
|
147
147
|
it('drops the object from pair, region, and point queries', () => {
|
|
148
148
|
const index = makeIndex();
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
150
|
+
insertSpatialObject2D(index, 2, { minX: 2, minY: 2, maxX: 6, maxY: 6 });
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
removeSpatialObject2D(index, 2);
|
|
153
153
|
|
|
154
154
|
const pairs: SpatialPair[] = [];
|
|
155
|
-
|
|
155
|
+
querySpatialPairs2D(index, pairs);
|
|
156
156
|
expect(pairs).toHaveLength(0);
|
|
157
157
|
|
|
158
158
|
const region: SpatialObjectId[] = [];
|
|
159
|
-
|
|
159
|
+
querySpatialRegion2D(index, { minX: 0, minY: 0, maxX: 6, maxY: 6 }, region);
|
|
160
160
|
expect(region).toEqual([1]);
|
|
161
161
|
|
|
162
162
|
const point: SpatialObjectId[] = [];
|
|
163
|
-
|
|
163
|
+
querySpatialPoint2D(index, 5, 5, point);
|
|
164
164
|
expect(point).toHaveLength(0);
|
|
165
165
|
});
|
|
166
166
|
});
|
|
167
167
|
|
|
168
|
-
describe('
|
|
168
|
+
describe('updateSpatialObject2D', () => {
|
|
169
169
|
it('removes a pair once the moved object leaves the shared region', () => {
|
|
170
170
|
const index = makeIndex();
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
insertSpatialObject2D(index, 1, { minX: 0, minY: 0, maxX: 4, maxY: 4 });
|
|
172
|
+
insertSpatialObject2D(index, 2, { minX: 2, minY: 2, maxX: 6, maxY: 6 });
|
|
173
173
|
|
|
174
174
|
const before: SpatialPair[] = [];
|
|
175
|
-
|
|
175
|
+
querySpatialPairs2D(index, before);
|
|
176
176
|
expect(before).toHaveLength(1);
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
updateSpatialObject2D(index, 2, { minX: 200, minY: 200, maxX: 204, maxY: 204 });
|
|
179
179
|
|
|
180
180
|
const after: SpatialPair[] = [];
|
|
181
|
-
|
|
181
|
+
querySpatialPairs2D(index, after);
|
|
182
182
|
expect(after).toHaveLength(0);
|
|
183
183
|
});
|
|
184
184
|
});
|