-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathterrain.test.ts
More file actions
395 lines (349 loc) · 16.8 KB
/
terrain.test.ts
File metadata and controls
395 lines (349 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import {describe, beforeEach, afterEach, test, expect, vi} from 'vitest';
import Point from '@mapbox/point-geometry';
import {mat4} from 'gl-matrix';
import {Terrain} from './terrain';
import {Context} from '../webgl/context';
import {RGBAImage} from '../util/image';
import {OverscaledTileID} from '../tile/tile_id';
import {Tile} from '../tile/tile';
import {LngLat} from '../geo/lng_lat';
import {EXTENT} from '../data/extent';
import {MAX_TILE_ZOOM, MIN_TILE_ZOOM} from '../util/util';
import {MercatorTransform} from '../geo/projection/mercator_transform';
import type {TileManager} from '../tile/tile_manager';
import type {TerrainSpecification} from '@maplibre/maplibre-gl-style-spec';
import type {DEMData} from '../data/dem_data';
import type {Painter} from './painter';
import {createNullGL} from '../util/test/null_gl';
describe('Terrain', () => {
let gl: WebGL2RenderingContext;
beforeEach(() => {
gl = createNullGL();
vi.spyOn(gl, 'readPixels').mockImplementation((_1, _2, _3, _4, _5, _6, rgba) => {
rgba[0] = 0;
rgba[1] = 0;
rgba[2] = 255;
rgba[3] = 255;
});
});
afterEach(() => {
vi.restoreAllMocks();
});
test('pointCoordinate should not return null', () => {
expect.assertions(3);
const painter = {
context: new Context(gl),
width: 1,
height: 1,
pixelRatio: 1,
transform: {center: {lng: 0}},
maybeDrawDepth: vi.fn(),
maybeDrawCoords: vi.fn(),
} as any as Painter;
const tileManager = {_source: {tileSize: 512}} as TileManager;
const getTileByID = (tileID) : Tile => {
if (tileID !== 'abcd') {
return null as any as Tile;
}
return {
tileID: new OverscaledTileID(0, 0, 0, 0, 0),
} as any as Tile;
};
const terrain = new Terrain(painter, tileManager, {} as any as TerrainSpecification);
terrain.tileManager.getTileByID = getTileByID;
terrain.coordsIndex.push('abcd');
const coordinate = terrain.pointCoordinate(new Point(0, 0));
expect(coordinate).not.toBeNull();
expect(painter.maybeDrawDepth).toHaveBeenCalled();
expect(painter.maybeDrawCoords).toHaveBeenCalled();
});
const setupMercatorOverflow = (pixelRatio: number = 1) => {
const WORLD_WIDTH = 4;
const painter = {
context: new Context(gl),
width: WORLD_WIDTH,
height: 1,
maybeDrawDepth: vi.fn(),
maybeDrawCoords: vi.fn(),
pixelRatio,
} as any as Painter;
const tileManager = {_source: {tileSize: 512}} as TileManager;
const terrain = new Terrain(painter, tileManager, {} as any as TerrainSpecification);
const tileIdsToWraps = {a: -1, b: 0, c: 1, d: 2};
terrain.tileManager.getTileByID = (id) => {
return {
tileID: {
canonical: {x: 0, y: 0, z: 0},
wrap: tileIdsToWraps[id]
}
} as any as Tile;
};
terrain.getElevation = () => 0;
terrain.coordsIndex = Object.keys(tileIdsToWraps);
vi.spyOn(gl, 'readPixels').mockImplementation((x, _2, _3, _4, _5, _6, rgba) => {
rgba[0] = 0;
rgba[1] = 0;
rgba[2] = 0;
rgba[3] = 255 - x / pixelRatio;
});
return terrain;
};
test(
`pointCoordinate should return negative mercator x
if the point is on the LEFT outside the central globe`,
() => {
const terrain = setupMercatorOverflow();
const coordinate = terrain.pointCoordinate(new Point(0, 0));
expect(coordinate.x).toBe(-1);
});
test(
`pointCoordinate should return mercator x greater than 1
if the point is on the RIGHT outside the central globe`,
() => {
const terrain = setupMercatorOverflow();
const coordinate = terrain.pointCoordinate(new Point(3, 0));
expect(coordinate.x).toBe(2);
});
test(
'pointCoordinate should respect painter.pixelRatio',
() => {
const terrain = setupMercatorOverflow(2);
expect(terrain.pointCoordinate(new Point(0, 0)).x).toBe(-1);
expect(terrain.pointCoordinate(new Point(3, 0)).x).toBe(2);
});
test('Calculate tile minimum and maximum elevation', () => {
const tileID = new OverscaledTileID(5, 0, 5, 17, 11);
const tile = new Tile(tileID, 256);
tile.dem = {
min: 0,
max: 100,
getPixels: () => new RGBAImage({width: 1, height: 1}, new Uint8Array(1 * 4)),
getUnpackVector: () => [6553.6, 25.6, 0.1, 10000.0],
} as any as DEMData;
const painter = {
context: new Context(gl),
width: 1,
height: 1,
getTileTexture: () => null
} as any as Painter;
const tileManager = {
_source: {maxzoom: 12, tileSize: 512},
_cache: {max: 10},
getTileByID: () => {
return tile;
},
} as any as TileManager;
const terrain = new Terrain(
painter,
tileManager,
{exaggeration: 2} as any as TerrainSpecification,
);
terrain.tileManager._tiles[tileID.key] = tile;
const {minElevation, maxElevation} = terrain.getMinMaxElevation(tileID);
expect(minElevation).toBe(0);
expect(maxElevation).toBe(200);
});
test('Return null elevation values when no tile', () => {
const tileID = new OverscaledTileID(5, 0, 5, 17, 11);
const painter = {
context: new Context(gl),
width: 1,
height: 1,
getTileTexture: () => null
} as any as Painter;
const tileManager = {
_source: {maxzoom: 12, tileSize: 512},
_cache: {max: 10},
getTileByID: () => null,
_outOfViewCache: {
getByKey: () => null,
},
} as any as TileManager;
const terrain = new Terrain(
painter,
tileManager,
{exaggeration: 2} as any as TerrainSpecification,
);
const minMaxNoTile = terrain.getMinMaxElevation(tileID);
expect(minMaxNoTile.minElevation).toBeNull();
expect(minMaxNoTile.maxElevation).toBeNull();
});
test('Return null elevation values when no DEM', () => {
const tileID = new OverscaledTileID(5, 0, 5, 17, 11);
const tile = new Tile(tileID, 256);
tile.dem = null as any as DEMData;
const painter = {
context: new Context(gl),
width: 1,
height: 1,
getTileTexture: () => null
} as any as Painter;
const tileManager = {
_source: {maxzoom: 12, tileSize: 512},
_cache: {max: 10},
getTileByID: () => {
return tile;
},
} as any as TileManager;
const terrain = new Terrain(
painter,
tileManager,
{exaggeration: 2} as any as TerrainSpecification,
);
const minMaxNoDEM = terrain.getMinMaxElevation(tileID);
expect(minMaxNoDEM.minElevation).toBeNull();
expect(minMaxNoDEM.maxElevation).toBeNull();
});
test('create mesh with border', () => {
let actualIndexArray;
let actualVertexArray;
const painter = {
context: {
createIndexBuffer: array => { actualIndexArray = Array.from(array.uint16); },
createVertexBuffer: array => { actualVertexArray = Array.from(array.int16); }
},
width: 1,
height: 1,
style: {
projection: {
transitionState: 0,
}
}
} as any as Painter;
const tileManager = {
_source: {maxzoom: 12, tileSize: 512},
_cache: {max: 10}
} as any as TileManager;
const terrain = new Terrain(
painter,
tileManager,
{exaggeration: 1} as any as TerrainSpecification,
);
terrain.meshSize = 4;
terrain.getTerrainMesh(new OverscaledTileID(2, 0, 2, 1, 1));
expect(terrain.getMeshFrameDelta(16)).toBe(122.16256373312942);
expect(actualIndexArray).toStrictEqual([0, 5, 6, 0, 6, 1, 1, 6, 7, 1, 7, 2, 2, 7, 8, 2, 8, 3, 3, 8, 9, 3, 9, 4, 5, 10, 11, 5, 11, 6, 6, 11, 12, 6, 12, 7, 7, 12, 13, 7, 13, 8, 8, 13, 14, 8, 14, 9, 10, 15, 16, 10, 16, 11, 11, 16, 17, 11, 17, 12, 12, 17, 18, 12, 18, 13, 13, 18, 19, 13, 19, 14, 15, 20, 21, 15, 21, 16, 16, 21, 22, 16, 22, 17, 17, 22, 23, 17, 23, 18, 18, 23, 24, 18, 24, 19, 20, 30, 31, 20, 31, 21, 0, 26, 25, 0, 1, 26, 21, 31, 32, 21, 32, 22, 1, 27, 26, 1, 2, 27, 22, 32, 33, 22, 33, 23, 2, 28, 27, 2, 3, 28, 23, 33, 34, 23, 34, 24, 3, 29, 28, 3, 4, 29, 35, 36, 38, 35, 38, 37, 45, 48, 46, 45, 47, 48, 37, 38, 40, 37, 40, 39, 47, 50, 48, 47, 49, 50, 39, 40, 42, 39, 42, 41, 49, 52, 50, 49, 51, 52, 41, 42, 44, 41, 44, 43, 51, 54, 52, 51, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
expect(actualVertexArray).toStrictEqual([0, 0, 0, 2048, 0, 0, 4096, 0, 0, 6144, 0, 0, 8192, 0, 0, 0, 2048, 0, 2048, 2048, 0, 4096, 2048, 0, 6144, 2048, 0, 8192, 2048, 0, 0, 4096, 0, 2048, 4096, 0, 4096, 4096, 0, 6144, 4096, 0, 8192, 4096, 0, 0, 6144, 0, 2048, 6144, 0, 4096, 6144, 0, 6144, 6144, 0, 8192, 6144, 0, 0, 8192, 0, 2048, 8192, 0, 4096, 8192, 0, 6144, 8192, 0, 8192, 8192, 0, 0, 0, 1, 2048, 0, 1, 4096, 0, 1, 6144, 0, 1, 8192, 0, 1, 0, 8192, 1, 2048, 8192, 1, 4096, 8192, 1, 6144, 8192, 1, 8192, 8192, 1, 0, 0, 0, 0, 0, 1, 0, 2048, 0, 0, 2048, 1, 0, 4096, 0, 0, 4096, 1, 0, 6144, 0, 0, 6144, 1, 0, 8192, 0, 0, 8192, 1, 8192, 0, 0, 8192, 0, 1, 8192, 2048, 0, 8192, 2048, 1, 8192, 4096, 0, 8192, 4096, 1, 8192, 6144, 0, 8192, 6144, 1, 8192, 8192, 0, 8192, 8192, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
});
test('interpolation works', () => {
const mockTerrain = {
getDEMElevation: Terrain.prototype.getDEMElevation,
getTerrainData() {
return {
u_terrain_matrix: mat4.create(),
tile: {
dem: {
dim: 1,
get(x:number, y:number) {
expect(x % 1).toBe(0);
expect(y % 1).toBe(0);
return 100 * x + 10 * y;
}
}
}
};
}
};
const tileID = new OverscaledTileID(0, 0, 0, 0, 0);
expect(mockTerrain.getDEMElevation(tileID, 0, 0)).toBeCloseTo(0);
expect(mockTerrain.getDEMElevation(tileID, 1, 1)).toBeCloseTo(110);
expect(mockTerrain.getDEMElevation(tileID, 0, 0.5)).toBeCloseTo(5);
expect(mockTerrain.getDEMElevation(tileID, 1, 0.5)).toBeCloseTo(105);
expect(mockTerrain.getDEMElevation(tileID, 0.5, 0)).toBeCloseTo(50);
expect(mockTerrain.getDEMElevation(tileID, 0.5, 1)).toBeCloseTo(60);
expect(mockTerrain.getDEMElevation(tileID, 0.4, 0.2)).toBeCloseTo(42);
});
test('getElevationForLngLat uses covering tiles to get the right zoom', () => {
const zoom = 10;
const painter = {
context: new Context(gl),
width: 1,
height: 1,
getTileTexture: () => null
} as any as Painter;
const tileManager = {
_source: {minzoom: 3, maxzoom: 22, tileSize: 512},
_cache: {max: 10},
getTileByID: () => {
return new Tile(new OverscaledTileID(zoom, 0, 0, 0, 0), 256);
},
} as any as TileManager;
const terrain = new Terrain(
painter,
tileManager,
{exaggeration: 2} as any as TerrainSpecification,
);
const spy = vi.fn();
terrain.getElevation = spy;
const transform = new MercatorTransform({minZoom: 3, maxZoom: 22, minPitch: 0, maxPitch: 85, renderWorldCopies: true});
transform.resize(200, 200);
transform.setZoom(zoom);
terrain.getElevationForLngLat(new LngLat(0, 0), transform);
expect(spy).toHaveBeenCalled();
expect((spy.mock.calls[0][0] as OverscaledTileID).canonical.z).toBe(zoom);
});
test('getElevationForLngLatZoom with lng less than -180 wraps correctly', () => {
const terrain = new Terrain(null, {_source: {tileSize: 512}} as any, {} as any);
terrain.getElevation = () => 1;
expect(terrain.getElevationForLngLatZoom(new LngLat(-183, 40), 0)).toBe(1);
});
test('getMinTileElevationForLngLatZoom with lng less than -180 wraps correctly', () => {
const terrain = new Terrain(null, {_source: {tileSize: 512}} as any, {} as any);
terrain.getMinMaxElevation = () => ({minElevation: 1, maxElevation: 42});
expect(terrain.getMinTileElevationForLngLatZoom(new LngLat(-183, 40), 0)).toBe(1);
});
test('getDEMElevation normalizes out-of-bounds coordinates to neighbor tile', () => {
const terrain = new Terrain(null, {_source: {tileSize: 512}} as any, {} as any);
const spy = vi.fn().mockReturnValue({tile: null});
terrain.getTerrainData = spy;
// tile (0,0,1) with x beyond EXTENT should normalize to tile (1,0,1)
const tileID = new OverscaledTileID(1, 0, 1, 0, 0);
terrain.getDEMElevation(tileID, EXTENT + 100, 50);
expect(spy).toHaveBeenCalledOnce();
const [calledTileID] = spy.mock.calls[0];
expect(calledTileID.canonical.x).toBe(1);
expect(calledTileID.canonical.y).toBe(0);
expect(calledTileID.canonical.z).toBe(1);
});
test('getDEMElevation returns 0 for coordinates beyond tile grid', () => {
const terrain = new Terrain(null, {_source: {tileSize: 512}} as any, {} as any);
const spy = vi.fn();
terrain.getTerrainData = spy;
// tile (0,0,0) with y beyond EXTENT — no tile exists below at z=0
const tileID = new OverscaledTileID(0, 0, 0, 0, 0);
const result = terrain.getDEMElevation(tileID, 100, EXTENT + 100);
expect(result).toBe(0);
expect(spy).not.toHaveBeenCalled();
});
describe('getElevationForLngLatZoom returns 0 for out of bounds', () => {
const terrain = new Terrain(null, {_source: {tileSize: 512}} as any, {} as any);
test('lng', () => {
expect(terrain.getElevationForLngLatZoom(new LngLat(180, 0), 0)).toBe(0);
});
test('lat', () => {
expect(terrain.getElevationForLngLatZoom(new LngLat(0, 88), 0)).toBe(0);
});
test('zoom', () => {
expect(terrain.getElevationForLngLatZoom(new LngLat(0, 0), MIN_TILE_ZOOM - 1)).toBe(0);
});
test('zoom', () => {
expect(terrain.getElevationForLngLatZoom(new LngLat(0, 0), MAX_TILE_ZOOM + 1)).toBe(0);
});
});
test('destroy does not throw', () => {
const context = new Context(gl);
const painter = {
context,
width: 100,
height: 100,
pixelRatio: 1,
style: {projection: null},
} as any as Painter;
const tileManager = {_source: {tileSize: 512}, destruct: vi.fn()} as any as TileManager;
const terrain = new Terrain(painter, tileManager, {} as any as TerrainSpecification);
terrain.getCoordsTexture();
terrain.getFramebuffer('coords');
terrain.getTerrainMesh(new OverscaledTileID(0, 0, 0, 0, 0));
expect(() => terrain.destroy()).not.toThrow();
});
});