-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathTraversalStatsDemo.ts
252 lines (220 loc) · 7.22 KB
/
TraversalStatsDemo.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
import path from "path";
import { readJsonUnchecked } from "./readJsonUnchecked";
import { ResourceResolvers } from "../src/io/ResourceResolvers";
import { TilesetTraverser } from "../src/traversal/TilesetTraverser";
import { TraversedTile } from "../src/traversal/TraversedTile";
import { BufferedContentData } from "../src/contentTypes/BufferedContentData";
import { ContentDataTypeChecks } from "../src/contentTypes/ContentDataTypeChecks";
import { ContentDataTypes } from "../src/contentTypes/ContentDataTypes";
// A small demo that traverses a tileset, passes all
// traversed tiles to a "StatsCollector" (defined below),
// and creates a short JSON summary of some statistics.
async function tilesetTraversalDemo(filePath: string) {
const statsCollector = new StatsCollector();
// Create a check that determines whether content
// data should count as "tile content"
const isTileFileContent = ContentDataTypeChecks.createIncludedCheck(
ContentDataTypes.CONTENT_TYPE_GLB,
ContentDataTypes.CONTENT_TYPE_B3DM,
ContentDataTypes.CONTENT_TYPE_I3DM,
ContentDataTypes.CONTENT_TYPE_CMPT,
ContentDataTypes.CONTENT_TYPE_PNTS,
ContentDataTypes.CONTENT_TYPE_GEOM,
ContentDataTypes.CONTENT_TYPE_VCTR,
ContentDataTypes.CONTENT_TYPE_GEOJSON,
ContentDataTypes.CONTENT_TYPE_GLTF
);
// A `TraversalCallback` that will be passed to the
// tileset traverser, and store information about the
// traversed tiles in the `StatsCollector`
const statsTraversalCallback = async (traversedTile: TraversedTile) => {
{
const indent = " ".repeat(traversedTile.level);
const contentUris = traversedTile.getFinalContents().map((c) => c.uri);
const geometricError = traversedTile.asFinalTile().geometricError;
const message =
indent +
"Level " +
traversedTile.level +
", geometricError " +
geometricError +
", contents " +
contentUris;
console.log(message);
}
statsCollector.increment("totalNumberOfTiles");
const subtreeUri = traversedTile.getSubtreeUri();
if (subtreeUri !== undefined) {
statsCollector.increment("totalNumberOfSubtrees");
}
if (!traversedTile.isImplicitTilesetRoot()) {
// Obtain all content URIs, resolve the associated data,
// and store the size in the "tileFileSize" summary if
// the data is one of the known tile content types
const contentUris = traversedTile.getFinalContents().map((c) => c.uri);
const tileResourceResolver = traversedTile.getResourceResolver();
for (const contentUri of contentUris) {
const data = await tileResourceResolver.resolveData(contentUri);
if (!data) {
statsCollector.increment("unresolvableContents");
} else {
const contentData = new BufferedContentData(contentUri, data);
const isTileFile = await isTileFileContent(contentData);
if (isTileFile) {
statsCollector.acceptEntry("tileFileSize", data.length);
statsCollector.acceptEntry(
"tileFileSize_" + traversedTile.level,
data.length
);
}
}
}
}
// Store the geometric error in the "geometricError" summary
const finalTile = traversedTile.asFinalTile();
const geometricError = finalTile.geometricError;
statsCollector.acceptEntry("geometricError", geometricError);
statsCollector.acceptEntry(
"geometricError_" + traversedTile.level,
geometricError
);
return true;
};
// Read the tileset from the input path
const directory = path.dirname(filePath);
const resourceResolver =
ResourceResolvers.createFileResourceResolver(directory);
const tileset = await readJsonUnchecked(filePath);
// Create the TilesetTraverser and traverse the tileset,
// passing each tile to the callback that stores the
// information in the StatsCollector
console.log("Traversing tileset");
const tilesetTraverser = new TilesetTraverser(directory, resourceResolver, {
depthFirst: false,
traverseExternalTilesets: true,
});
await tilesetTraverser.traverse(tileset, statsTraversalCallback);
console.log("Traversing tileset DONE");
// Print the statistics summary to the console
console.log("Stats:");
const json = statsCollector.createJson();
const jsonString = JSON.stringify(json, null, 2);
console.log(jsonString);
}
// A simple class to collect statistical information
class StatsCollector {
// A mapping from value names to counters
private readonly counters: {
[key: string]: Counter;
} = {};
// A mapping from value names to statistical summaries
private readonly summaries: {
[key: string]: Summary;
} = {};
// Add one entry to a summary, creating it when necessary
acceptEntry(name: string, value: number) {
let summary = this.summaries[name];
if (!summary) {
summary = new Summary();
this.summaries[name] = summary;
}
summary.accept(value);
}
// Increment a counter, creating it when necessary
increment(name: string) {
let counter = this.counters[name];
if (!counter) {
counter = new Counter();
this.counters[name] = counter;
}
counter.increment();
}
// Create a short JSON representation of the collected data
createJson(): any {
const json: any = {};
for (const key of Object.keys(this.counters)) {
const counter = this.counters[key];
json[key] = counter.getCount();
}
for (const key of Object.keys(this.summaries)) {
const summary = this.summaries[key];
json[key] = {
count: summary.getCount(),
sum: summary.getSum(),
min: summary.getMinimum(),
max: summary.getMaximum(),
avg: summary.getMean(),
stdDev: summary.getStandardDeviation(),
};
}
return json;
}
}
/**
* A class that serves as a counter in the `StatsCollector`
*/
class Counter {
private count: number;
public constructor() {
this.count = 0;
}
increment() {
this.count++;
}
getCount() {
return this.count;
}
}
/**
* A class that can accept numbers, and collects statistical
* information for these numbers.
*/
class Summary {
private count: number;
private sum: number;
private min: number;
private max: number;
private varianceTracker: number;
public constructor() {
this.count = 0;
this.sum = 0.0;
this.min = Number.POSITIVE_INFINITY;
this.max = Number.NEGATIVE_INFINITY;
this.varianceTracker = 0.0;
}
accept(value: number) {
const deviation = value - this.getMean();
this.sum += value;
this.min = Math.min(this.min, value);
this.max = Math.max(this.max, value);
this.count++;
if (this.count > 1) {
this.varianceTracker +=
(deviation * deviation * (this.count - 1)) / this.count;
}
}
getCount() {
return this.count;
}
getSum() {
return this.sum;
}
getMinimum() {
return this.min;
}
getMaximum() {
return this.max;
}
getMean() {
return this.sum / this.count;
}
getStandardDeviation() {
return Math.sqrt(this.varianceTracker / this.count);
}
}
async function runDemo() {
const tilesetFileName =
"./specs/data/tilesetProcessing/implicitProcessing/tileset.json";
await tilesetTraversalDemo(tilesetFileName);
}
runDemo();