@flighthq/spatial 0.3.1-next.975.01aea94 → 0.4.0-edge.1908.751021c

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 (57) hide show
  1. package/dist/bvh3D.d.ts +3 -0
  2. package/dist/bvh3D.d.ts.map +1 -0
  3. package/dist/bvh3D.js +575 -0
  4. package/dist/bvh3D.js.map +1 -0
  5. package/dist/contract.d.ts +6 -1
  6. package/dist/contract.d.ts.map +1 -1
  7. package/dist/contract.js +6 -1
  8. package/dist/contract.js.map +1 -1
  9. package/dist/explainSpatialIndexing2D.d.ts +3 -0
  10. package/dist/explainSpatialIndexing2D.d.ts.map +1 -0
  11. package/dist/{explainSpatialIndexing.js → explainSpatialIndexing2D.js} +2 -2
  12. package/dist/explainSpatialIndexing2D.js.map +1 -0
  13. package/dist/explainSpatialIndexing3D.d.ts +3 -0
  14. package/dist/explainSpatialIndexing3D.d.ts.map +1 -0
  15. package/dist/explainSpatialIndexing3D.js +15 -0
  16. package/dist/explainSpatialIndexing3D.js.map +1 -0
  17. package/dist/formatSpatialIndexingNotice.d.ts.map +1 -1
  18. package/dist/formatSpatialIndexingNotice.js +8 -2
  19. package/dist/formatSpatialIndexingNotice.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/spatialIndex.d.ts +10 -10
  25. package/dist/spatialIndex.d.ts.map +1 -1
  26. package/dist/spatialIndex.js +14 -14
  27. package/dist/spatialIndex.js.map +1 -1
  28. package/dist/spatialIndex3D.d.ts +13 -0
  29. package/dist/spatialIndex3D.d.ts.map +1 -0
  30. package/dist/spatialIndex3D.js +166 -0
  31. package/dist/spatialIndex3D.js.map +1 -0
  32. package/dist/spatialIndexingGuard.d.ts +4 -0
  33. package/dist/spatialIndexingGuard.d.ts.map +1 -0
  34. package/dist/spatialIndexingGuard.js +23 -0
  35. package/dist/spatialIndexingGuard.js.map +1 -0
  36. package/dist/uniformGrid.d.ts +2 -3
  37. package/dist/uniformGrid.d.ts.map +1 -1
  38. package/dist/uniformGrid.js +5 -15
  39. package/dist/uniformGrid.js.map +1 -1
  40. package/dist/uniformGrid3D.d.ts +3 -0
  41. package/dist/uniformGrid3D.d.ts.map +1 -0
  42. package/dist/uniformGrid3D.js +627 -0
  43. package/dist/uniformGrid3D.js.map +1 -0
  44. package/package.json +3 -3
  45. package/src/bvh3D.test.ts +294 -0
  46. package/src/explainSpatialIndexing2D.test.ts +81 -0
  47. package/src/explainSpatialIndexing3D.test.ts +59 -0
  48. package/src/formatSpatialIndexingNotice.test.ts +30 -4
  49. package/src/spatialIndex.test.ts +65 -65
  50. package/src/spatialIndex3D.test.ts +273 -0
  51. package/src/spatialIndexingGuard.test.ts +188 -0
  52. package/src/uniformGrid.test.ts +54 -182
  53. package/src/uniformGrid3D.test.ts +358 -0
  54. package/dist/explainSpatialIndexing.d.ts +0 -3
  55. package/dist/explainSpatialIndexing.d.ts.map +0 -1
  56. package/dist/explainSpatialIndexing.js.map +0 -1
  57. package/src/explainSpatialIndexing.test.ts +0 -76
@@ -0,0 +1,188 @@
1
+ import type { SpatialIndexingNotice, SpatialObjectId } from '@flighthq/types/contract';
2
+ import { afterEach, describe, expect, it } from 'vitest';
3
+
4
+ import { reportSpatialIndexing, setSpatialIndexingGuard } from './spatialIndexingGuard';
5
+ import { createUniformGridSpatialBackend2D } from './uniformGrid';
6
+ import { createUniformGridSpatialBackend3D } from './uniformGrid3D';
7
+
8
+ afterEach(() => {
9
+ setSpatialIndexingGuard(null);
10
+ });
11
+
12
+ describe('reportSpatialIndexing', () => {
13
+ it('hands the notice to the installed guard unchanged', () => {
14
+ const notices: SpatialIndexingNotice[] = [];
15
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
16
+ const notice: SpatialIndexingNotice = {
17
+ cellSize: 4,
18
+ id: 3,
19
+ mode: 'overflow',
20
+ operation: 'insert',
21
+ reason: 'invalid-cell-size',
22
+ wouldOccupyBucketCount: 9,
23
+ };
24
+ reportSpatialIndexing(notice);
25
+ expect(notices).toEqual([notice]);
26
+ });
27
+
28
+ it('is a no-op with no guard installed', () => {
29
+ expect(() =>
30
+ reportSpatialIndexing({
31
+ cellSize: 1,
32
+ id: 0,
33
+ mode: 'absent',
34
+ operation: 'remove',
35
+ reason: 'missing-id',
36
+ wouldOccupyBucketCount: 0,
37
+ }),
38
+ ).not.toThrow();
39
+ });
40
+
41
+ it('serves both dimensions from the one installed guard', () => {
42
+ const notices: SpatialIndexingNotice[] = [];
43
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
44
+ createUniformGridSpatialBackend2D(10).insertSpatialObject(1, { minX: NaN, minY: 0, maxX: 1, maxY: 1 });
45
+ createUniformGridSpatialBackend3D(10).insertSpatialObject(2, {
46
+ minX: 0,
47
+ minY: NaN,
48
+ minZ: 0,
49
+ maxX: 1,
50
+ maxY: 1,
51
+ maxZ: 1,
52
+ });
53
+ expect(notices.map((notice) => notice.id)).toEqual([1, 2]);
54
+ expect(notices.every((notice) => notice.reason === 'non-finite-bounds')).toBe(true);
55
+ });
56
+ });
57
+
58
+ describe('setSpatialIndexingGuard', () => {
59
+ it('reports a decline with its reason, and no span', () => {
60
+ const notices: SpatialIndexingNotice[] = [];
61
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
62
+ const grid = createUniformGridSpatialBackend2D(10);
63
+ grid.insertSpatialObject(7, { minX: NaN, minY: 0, maxX: 10, maxY: 10 });
64
+ expect(notices).toEqual([
65
+ {
66
+ cellSize: 10,
67
+ id: 7,
68
+ mode: 'declined',
69
+ operation: 'insert',
70
+ reason: 'non-finite-bounds',
71
+ wouldOccupyBucketCount: 0,
72
+ },
73
+ ]);
74
+ });
75
+
76
+ it('reports an overflow with the span the bound refused to walk', () => {
77
+ const notices: SpatialIndexingNotice[] = [];
78
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
79
+ const grid = createUniformGridSpatialBackend2D(1);
80
+ grid.insertSpatialObject(7, { minX: 0, minY: 0, maxX: 199, maxY: 199 });
81
+ expect(notices).toEqual([
82
+ {
83
+ cellSize: 1,
84
+ id: 7,
85
+ mode: 'overflow',
86
+ operation: 'insert',
87
+ reason: null,
88
+ wouldOccupyBucketCount: 40000,
89
+ },
90
+ ]);
91
+ });
92
+
93
+ it('reports an invalid cell size and keeps results correct through the bounded overflow path', () => {
94
+ const notices: SpatialIndexingNotice[] = [];
95
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
96
+ for (const cellSize of [0, -1]) {
97
+ const grid = createUniformGridSpatialBackend2D(cellSize);
98
+ expect(grid.insertSpatialObject(7, { minX: 0, minY: 0, maxX: 10, maxY: 10 })).toBe(true);
99
+ const point: SpatialObjectId[] = [];
100
+ grid.querySpatialPoint(5, 5, point);
101
+ expect(point).toEqual([7]);
102
+ }
103
+ expect(notices).toEqual(
104
+ [0, -1].map((cellSize) => ({
105
+ cellSize,
106
+ id: 7,
107
+ mode: 'overflow',
108
+ operation: 'insert',
109
+ reason: 'invalid-cell-size',
110
+ wouldOccupyBucketCount: 0,
111
+ })),
112
+ );
113
+ });
114
+
115
+ it('declines and reports inverted bounds', () => {
116
+ const notices: SpatialIndexingNotice[] = [];
117
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
118
+ const grid = createUniformGridSpatialBackend2D(10);
119
+ expect(grid.insertSpatialObject(7, { minX: 10, minY: 0, maxX: 0, maxY: 10 })).toBe(false);
120
+ expect(grid.explainSpatialIndexing(7)).toEqual({
121
+ bucketCount: 0,
122
+ id: 7,
123
+ mode: 'declined',
124
+ reason: 'inverted-bounds',
125
+ });
126
+ expect(notices).toEqual([
127
+ {
128
+ cellSize: 10,
129
+ id: 7,
130
+ mode: 'declined',
131
+ operation: 'insert',
132
+ reason: 'inverted-bounds',
133
+ wouldOccupyBucketCount: 0,
134
+ },
135
+ ]);
136
+ });
137
+
138
+ it('reports update and remove operations whose id was never inserted', () => {
139
+ const notices: SpatialIndexingNotice[] = [];
140
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
141
+ const grid = createUniformGridSpatialBackend2D(10);
142
+ expect(grid.updateSpatialObject(7, { minX: 0, minY: 0, maxX: 5, maxY: 5 })).toBe(true);
143
+ grid.removeSpatialObject(8);
144
+ expect(notices).toEqual([
145
+ {
146
+ cellSize: 10,
147
+ id: 7,
148
+ mode: 'cells',
149
+ operation: 'update',
150
+ reason: 'missing-id',
151
+ wouldOccupyBucketCount: 0,
152
+ },
153
+ {
154
+ cellSize: 10,
155
+ id: 8,
156
+ mode: 'absent',
157
+ operation: 'remove',
158
+ reason: 'missing-id',
159
+ wouldOccupyBucketCount: 0,
160
+ },
161
+ ]);
162
+ });
163
+
164
+ it('stays silent on the ordinary path', () => {
165
+ const notices: SpatialIndexingNotice[] = [];
166
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
167
+ const grid = createUniformGridSpatialBackend2D(10);
168
+ grid.insertSpatialObject(1, { minX: 0, minY: 0, maxX: 10, maxY: 10 });
169
+ grid.removeSpatialObject(1);
170
+ expect(notices).toEqual([]);
171
+ });
172
+
173
+ it('null uninstalls it', () => {
174
+ const notices: SpatialIndexingNotice[] = [];
175
+ setSpatialIndexingGuard((notice) => notices.push({ ...notice }));
176
+ setSpatialIndexingGuard(null);
177
+ const grid = createUniformGridSpatialBackend2D(10);
178
+ grid.insertSpatialObject(1, { minX: NaN, minY: 0, maxX: 10, maxY: 10 });
179
+ expect(notices).toEqual([]);
180
+ });
181
+
182
+ it('does not change what insert returns', () => {
183
+ setSpatialIndexingGuard(() => {});
184
+ const grid = createUniformGridSpatialBackend2D(10);
185
+ expect(grid.insertSpatialObject(1, { minX: NaN, minY: 0, maxX: 10, maxY: 10 })).toBe(false);
186
+ expect(grid.insertSpatialObject(2, { minX: 0, minY: 0, maxX: 10, maxY: 10 })).toBe(true);
187
+ });
188
+ });