This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
TileGeometryLoader.ts
323 lines (283 loc) · 10.8 KB
/
TileGeometryLoader.ts
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
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import {
DecodedTile,
GeometryKind,
GeometryKindSet,
isExtrudedLineTechnique,
isExtrudedPolygonTechnique,
isFillTechnique,
isLineMarkerTechnique,
isLineTechnique,
isPoiTechnique,
isSegmentsTechnique,
isSolidLineTechnique,
isTextTechnique,
Technique
} from "@here/harp-datasource-protocol";
import { PerformanceTimer } from "@here/harp-utils";
import { PerformanceStatistics } from "../Statistics";
import { Tile } from "../Tile";
import { TileGeometryCreator } from "./TileGeometryCreator";
/**
* Loads the geometry for its [[Tile]]. Derived classes allow for different loading strategies.
*/
export interface TileGeometryLoader {
/**
* The [[Tile]] this `TileGeometryLoader` is managing.
*/
tile: Tile;
/**
* `True` if all geometry of the `Tile` has been loaded and the loading process is finished.
*/
isFinished: boolean;
/**
* `True` if the basic geometry has been loaded, and the `Tile` is ready for display.
*/
basicGeometryLoaded: boolean;
/**
* `True` if all geometry of the `Tile` has been loaded.
*/
allGeometryLoaded: boolean;
/**
* The kinds of geometry stored in this [[Tile]].
*/
availableGeometryKinds: GeometryKindSet | undefined;
/**
* Start with or continue with loading geometry. Called repeatedly until `isFinished` is `true`.
*/
update(
enabledKinds: GeometryKindSet | undefined,
disabledKinds: GeometryKindSet | undefined
): void;
/**
* Dispose of any resources.
*/
dispose(): void;
/**
* Reset the loader to its initial state and cancels any asynchronous work.
*/
reset(): void;
}
export namespace TileGeometryLoader {
/**
* Make sure that all technique have their geometryKind set, either from the theme or their
* default value.
*
* Also gather set of the [[GeometryKind]]s found in the techniques and return it.
*
* @param {DecodedTile} decodedTile
* @returns {GeometryKindSet} The set of kinds used in the decodeTile.
*/
export function prepareDecodedTile(decodedTile: DecodedTile): GeometryKindSet {
const foundSet: GeometryKindSet = new GeometryKindSet();
for (const technique of decodedTile.techniques) {
let geometryKind = technique.kind;
// Set default kind based on technique.
if (geometryKind === undefined) {
geometryKind = setDefaultGeometryKind(technique);
}
if (Array.isArray(geometryKind)) {
geometryKind = new GeometryKindSet(geometryKind);
}
if (geometryKind instanceof Set) {
for (const kind of geometryKind) {
foundSet.add(kind);
}
} else {
foundSet.add(geometryKind);
}
}
return foundSet;
}
/**
* Make sure that the technique has its geometryKind set, either from the theme or their default
* value.
*
* @param {Technique} technique
*/
export function setDefaultGeometryKind(technique: Technique): GeometryKind | GeometryKindSet {
let geometryKind = technique.kind;
// Set default kind based on technique.
if (geometryKind === undefined) {
if (isFillTechnique(technique)) {
geometryKind = GeometryKind.Area;
} else if (
isLineTechnique(technique) ||
isSolidLineTechnique(technique) ||
isSegmentsTechnique(technique) ||
isExtrudedLineTechnique(technique)
) {
geometryKind = GeometryKind.Line;
} else if (isExtrudedPolygonTechnique(technique)) {
geometryKind = GeometryKind.Building;
} else if (
isPoiTechnique(technique) ||
isLineMarkerTechnique(technique) ||
isTextTechnique(technique)
) {
geometryKind = GeometryKind.Label;
} else {
geometryKind = GeometryKind.All;
}
technique.kind = geometryKind;
}
return geometryKind;
}
}
/**
* Simplest implementation of a [[TileGeometryLoader]]. It loads all geometry in a single step.
*/
export class SimpleTileGeometryLoader implements TileGeometryLoader {
private m_decodedTile?: DecodedTile;
private m_isFinished: boolean = false;
private m_availableGeometryKinds: GeometryKindSet | undefined;
private m_timeout: any;
constructor(private m_tile: Tile) {}
get tile(): Tile {
return this.m_tile;
}
get isFinished(): boolean {
return this.m_isFinished;
}
get basicGeometryLoaded(): boolean {
return this.m_tile.hasGeometry;
}
get allGeometryLoaded(): boolean {
return this.m_isFinished;
}
/**
* Set the [[DecodedTile]] of the tile. Is called after the decoded tile has been loaded, and
* prepares its content for later processing in the 'updateXXX' methods.
*
* @param {DecodedTile} decodedTile The decoded tile with the flat geometry data belonging to
* this tile.
* @returns {DecodedTile} The processed decoded tile.
*/
setDecodedTile(decodedTile: DecodedTile): DecodedTile {
this.m_decodedTile = decodedTile;
if (this.m_decodedTile !== undefined) {
this.m_availableGeometryKinds = TileGeometryLoader.prepareDecodedTile(
this.m_decodedTile
);
}
return this.m_decodedTile;
}
get availableGeometryKinds(): GeometryKindSet | undefined {
return this.m_availableGeometryKinds;
}
update(
enabledKinds: GeometryKindSet | undefined,
disabledKinds: GeometryKindSet | undefined
): void {
const tile = this.tile;
// First time this tile is handled:
if (this.m_decodedTile === undefined && tile.decodedTile !== undefined) {
TileGeometryCreator.instance.processTechniques(
tile.decodedTile,
enabledKinds,
disabledKinds
);
this.setDecodedTile(tile.decodedTile);
this.prepareForRender(enabledKinds, disabledKinds);
}
}
dispose(): void {
this.m_decodedTile = undefined;
}
reset(): void {
this.m_decodedTile = undefined;
this.m_isFinished = false;
if (this.m_availableGeometryKinds !== undefined) {
this.m_availableGeometryKinds.clear();
}
if (this.m_timeout !== undefined) {
clearTimeout(this.m_timeout);
}
}
private finish() {
this.m_tile.loadingFinished();
this.m_tile.removeDecodedTile();
this.m_isFinished = true;
this.m_timeout = undefined;
}
/**
* Called by [[VisibleTileSet]] to mark that [[Tile]] is visible and it should prepare geometry.
*/
private prepareForRender(
enabledKinds: GeometryKindSet | undefined,
disabledKinds: GeometryKindSet | undefined
) {
// If the tile is not ready for display, or if it has become invisible while being loaded,
// for example by moving the camera, the tile is not finished and its geometry is not
// created. This is an optimization for fast camera movements and zooms.
const tile = this.tile;
const decodedTile = this.m_decodedTile;
this.m_decodedTile = undefined;
if (decodedTile === undefined || tile.disposed || !tile.isVisible) {
this.finish();
return;
}
this.m_timeout = setTimeout(() => {
const stats = PerformanceStatistics.instance;
// If the tile has become invisible while being loaded, for example by moving the
// camera, the tile is not finished and its geometry is not created. This is an
// optimization for fast camera movements and zooms.
if (!tile.isVisible) {
// Dispose the tile from the visible set, so it can be reloaded properly next time
// it is needed.
tile.mapView.visibleTileSet.disposeTile(tile);
if (stats.enabled) {
stats.currentFrame.addMessage(
// tslint:disable-next-line: max-line-length
`Decoded tile: ${tile.dataSource.name} # lvl=${tile.tileKey.level} col=${tile.tileKey.column} row=${tile.tileKey.row} DISCARDED - invisible`
);
}
this.finish();
return;
}
let now = 0;
if (stats.enabled) {
now = PerformanceTimer.now();
}
const geometryCreator = TileGeometryCreator.instance;
tile.clear();
geometryCreator.initDecodedTile(decodedTile, enabledKinds, disabledKinds);
geometryCreator.createAllGeometries(tile, decodedTile);
if (stats.enabled) {
const geometryCreationTime = PerformanceTimer.now() - now;
const currentFrame = stats.currentFrame;
currentFrame.addValue("geometry.geometryCreationTime", geometryCreationTime);
currentFrame.addValue("geometryCount.numGeometries", decodedTile.geometries.length);
currentFrame.addValue("geometryCount.numTechniques", decodedTile.techniques.length);
currentFrame.addValue(
"geometryCount.numPoiGeometries",
decodedTile.poiGeometries !== undefined ? decodedTile.poiGeometries.length : 0
);
currentFrame.addValue(
"geometryCount.numTextGeometries",
decodedTile.textGeometries !== undefined ? decodedTile.textGeometries.length : 0
);
currentFrame.addValue(
"geometryCount.numTextPathGeometries",
decodedTile.textPathGeometries !== undefined
? decodedTile.textPathGeometries.length
: 0
);
currentFrame.addValue(
"geometryCount.numPathGeometries",
decodedTile.pathGeometries !== undefined ? decodedTile.pathGeometries.length : 0
);
currentFrame.addMessage(
// tslint:disable-next-line: max-line-length
`Decoded tile: ${tile.dataSource.name} # lvl=${tile.tileKey.level} col=${tile.tileKey.column} row=${tile.tileKey.row}`
);
}
this.finish();
tile.dataSource.requestUpdate();
}, 0);
}
}