-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathMetadataTester.js
512 lines (439 loc) · 14.2 KB
/
MetadataTester.js
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import {
defined,
defaultValue,
DeveloperError,
FeatureDetection,
PropertyTable,
MetadataClass,
MetadataComponentType,
MetadataEnum,
MetadataTable,
MetadataType,
} from "@cesium/engine";
function MetadataTester() {}
MetadataTester.isSupported = function () {
return (
FeatureDetection.supportsBigInt64Array() &&
FeatureDetection.supportsBigUint64Array() &&
FeatureDetection.supportsBigInt() &&
typeof TextEncoder !== "undefined"
);
};
MetadataTester.createProperty = function (options) {
const properties = {
propertyId: options.property,
};
const propertyValues = {
propertyId: options.values,
};
const table = MetadataTester.createMetadataTable({
properties: properties,
propertyValues: propertyValues,
// offsetType is for legacy EXT_feature_metadata, arrayOffsetType and
// stringOffsetType are for EXT_structural_metadata
offsetType: options.offsetType,
arrayOffsetType: options.arrayOffsetType,
stringOffsetType: options.stringOffsetType,
enums: options.enums,
disableBigIntSupport: options.disableBigIntSupport,
disableBigInt64ArraySupport: options.disableBigInt64ArraySupport,
disableBigUint64ArraySupport: options.disableBigUint64ArraySupport,
});
return table._properties.propertyId;
};
function createProperties(options) {
const schema = options.schema;
const classId = options.classId;
const propertyValues = options.propertyValues;
const offsetType = options.offsetType;
const stringOffsetType = options.stringOffsetType;
const arrayOffsetType = options.arrayOffsetType;
const bufferViews = defined(options.bufferViews) ? options.bufferViews : {};
const enums = defined(schema.enums) ? schema.enums : {};
const enumDefinitions = {};
for (const enumId in enums) {
if (enums.hasOwnProperty(enumId)) {
enumDefinitions[enumId] = MetadataEnum.fromJson({
id: enumId,
enum: enums[enumId],
});
}
}
const classDefinition = MetadataClass.fromJson({
id: classId,
class: schema.classes[classId],
enums: enumDefinitions,
});
const properties = {};
let bufferViewIndex = Object.keys(bufferViews).length;
let count = 0;
for (const propertyId in propertyValues) {
if (propertyValues.hasOwnProperty(propertyId)) {
const classProperty = classDefinition.properties[propertyId];
const values = propertyValues[propertyId];
count = values.length;
const valuesBuffer = addPadding(
createValuesBuffer(values, classProperty)
);
const valuesBufferView = bufferViewIndex++;
bufferViews[valuesBufferView] = valuesBuffer;
const property = {
values: valuesBufferView,
};
properties[propertyId] = property;
// for legacy EXT_feature_metadata
if (defined(offsetType)) {
property.offsetType = offsetType;
}
if (defined(stringOffsetType)) {
property.stringOffsetType = stringOffsetType;
}
if (defined(arrayOffsetType)) {
property.arrayOffsetType = arrayOffsetType;
}
if (classProperty.isVariableLengthArray) {
const arrayOffsetBufferType = defaultValue(arrayOffsetType, offsetType);
const arrayOffsetBuffer = addPadding(
createArrayOffsetBuffer(
values,
classProperty.type,
arrayOffsetBufferType
)
);
const arrayOffsetBufferView = bufferViewIndex++;
bufferViews[arrayOffsetBufferView] = arrayOffsetBuffer;
property.arrayOffsets = arrayOffsetBufferView;
}
if (classProperty.type === MetadataType.STRING) {
const stringOffsetBufferType = defaultValue(
stringOffsetType,
offsetType
);
const stringOffsetBuffer = addPadding(
createStringOffsetBuffer(values, stringOffsetBufferType)
);
const stringOffsetBufferView = bufferViewIndex++;
bufferViews[stringOffsetBufferView] = stringOffsetBuffer;
property.stringOffsets = stringOffsetBufferView;
}
}
}
return {
count: count,
properties: properties,
class: classDefinition,
bufferViews: bufferViews,
};
}
MetadataTester.createMetadataTable = function (options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const disableBigIntSupport = options.disableBigIntSupport;
const disableBigInt64ArraySupport = options.disableBigInt64ArraySupport;
const disableBigUint64ArraySupport = options.disableBigUint64ArraySupport;
const schema = {
enums: options.enums,
classes: {
classId: {
properties: options.properties,
},
},
};
const propertyResults = createProperties({
schema: schema,
classId: "classId",
propertyValues: options.propertyValues,
offsetType: options.offsetType,
});
const count = propertyResults.count;
const properties = propertyResults.properties;
const classDefinition = propertyResults.class;
const bufferViews = propertyResults.bufferViews;
if (disableBigIntSupport) {
spyOn(FeatureDetection, "supportsBigInt").and.returnValue(false);
}
if (disableBigInt64ArraySupport) {
spyOn(FeatureDetection, "supportsBigInt64Array").and.returnValue(false);
}
if (disableBigUint64ArraySupport) {
spyOn(FeatureDetection, "supportsBigUint64Array").and.returnValue(false);
}
return new MetadataTable({
count: count,
properties: properties,
class: classDefinition,
bufferViews: bufferViews,
});
};
MetadataTester.createPropertyTable = function (options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const disableBigIntSupport = options.disableBigIntSupport;
const disableBigInt64ArraySupport = options.disableBigInt64ArraySupport;
const disableBigUint64ArraySupport = options.disableBigUint64ArraySupport;
const schema = {
enums: options.enums,
classes: {
classId: {
properties: options.properties,
},
},
};
const propertyResults = createProperties({
schema: schema,
classId: "classId",
propertyValues: options.propertyValues,
offsetType: options.offsetType,
});
const count = propertyResults.count;
const properties = propertyResults.properties;
const classDefinition = propertyResults.class;
const bufferViews = propertyResults.bufferViews;
if (disableBigIntSupport) {
spyOn(FeatureDetection, "supportsBigInt").and.returnValue(false);
}
if (disableBigInt64ArraySupport) {
spyOn(FeatureDetection, "supportsBigInt64Array").and.returnValue(false);
}
if (disableBigUint64ArraySupport) {
spyOn(FeatureDetection, "supportsBigUint64Array").and.returnValue(false);
}
const metadataTable = new MetadataTable({
count: count,
class: classDefinition,
bufferViews: bufferViews,
properties: properties,
});
return new PropertyTable({
metadataTable: metadataTable,
count: count,
extras: options.extras,
extensions: options.extensions,
});
};
// for EXT_structural_metadata
MetadataTester.createPropertyTables = function (options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const propertyTables = [];
const bufferViews = {};
for (let i = 0; i < options.propertyTables.length; i++) {
const propertyTable = options.propertyTables[i];
const tablePropertyResults = createProperties({
schema: options.schema,
classId: propertyTable.class,
propertyValues: propertyTable.properties,
bufferViews: bufferViews,
});
const count = tablePropertyResults.count;
const properties = tablePropertyResults.properties;
propertyTables.push({
name: propertyTable.name,
class: propertyTable.class,
count: count,
properties: properties,
});
}
return {
propertyTables: propertyTables,
bufferViews: bufferViews,
};
};
// For EXT_feature_metadata
MetadataTester.createFeatureTables = function (options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const featureTables = {};
const bufferViews = {};
for (const featureTableId in options.featureTables) {
if (options.featureTables.hasOwnProperty(featureTableId)) {
const featureTable = options.featureTables[featureTableId];
const propertyResults = createProperties({
schema: options.schema,
classId: featureTable.class,
propertyValues: featureTable.properties,
bufferViews: bufferViews,
});
const count = propertyResults.count;
const properties = propertyResults.properties;
featureTables[featureTableId] = {
class: featureTable.class,
count: count,
properties: properties,
};
}
}
return {
featureTables: featureTables,
bufferViews: bufferViews,
};
};
MetadataTester.createGltf = function (options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const propertyTableResults = MetadataTester.createPropertyTables(options);
let bufferByteLength = 0;
const bufferViewsMap = propertyTableResults.bufferViews;
const bufferViewsLength = Object.keys(bufferViewsMap).length;
const byteLengths = new Array(bufferViewsLength);
let bufferViewId;
let uint8Array;
for (bufferViewId in bufferViewsMap) {
if (bufferViewsMap.hasOwnProperty(bufferViewId)) {
uint8Array = bufferViewsMap[bufferViewId];
const remainder = uint8Array.byteLength % 8;
const padding = remainder === 0 ? 0 : 8 - remainder;
const byteLength = uint8Array.byteLength + padding;
bufferByteLength += byteLength;
byteLengths[bufferViewId] = byteLength;
}
}
const buffer = new Uint8Array(bufferByteLength);
const bufferViews = new Array(bufferViewsLength);
let byteOffset = 0;
for (bufferViewId in bufferViewsMap) {
if (bufferViewsMap.hasOwnProperty(bufferViewId)) {
uint8Array = bufferViewsMap[bufferViewId];
bufferViews[bufferViewId] = {
buffer: 0,
byteOffset: byteOffset,
byteLength: uint8Array.byteLength,
};
buffer.set(uint8Array, byteOffset);
byteOffset += byteLengths[bufferViewId];
}
}
const gltf = {
buffers: [
{
uri: "external.bin",
byteLength: buffer.byteLength,
},
],
images: options.images,
textures: options.textures,
bufferViews: bufferViews,
extensionsUsed: ["EXT_structural_metadata"],
extensions: {
EXT_structural_metadata: {
schema: options.schema,
propertyTables: propertyTableResults.propertyTables,
propertyTextures: options.propertyTextures,
},
},
};
return {
gltf: gltf,
buffer: buffer,
};
};
function createBuffer(values, componentType) {
let typedArray;
switch (componentType) {
case MetadataComponentType.INT8:
typedArray = new Int8Array(values);
break;
case MetadataComponentType.UINT8:
typedArray = new Uint8Array(values);
break;
case MetadataComponentType.INT16:
typedArray = new Int16Array(values);
break;
case MetadataComponentType.UINT16:
typedArray = new Uint16Array(values);
break;
case MetadataComponentType.INT32:
typedArray = new Int32Array(values);
break;
case MetadataComponentType.UINT32:
typedArray = new Uint32Array(values);
break;
case MetadataComponentType.INT64:
typedArray = new BigInt64Array(values); // eslint-disable-line
break;
case MetadataComponentType.UINT64:
typedArray = new BigUint64Array(values); // eslint-disable-line
break;
case MetadataComponentType.FLOAT32:
typedArray = new Float32Array(values);
break;
case MetadataComponentType.FLOAT64:
typedArray = new Float64Array(values);
break;
//>>includeStart('debug', pragmas.debug);
default:
throw new DeveloperError(
`${componentType} is not a valid component type`
);
//>>includeEnd('debug');
}
return new Uint8Array(typedArray.buffer);
}
function createStringBuffer(values) {
const encoder = new TextEncoder();
return encoder.encode(values.join(""));
}
function createBooleanBuffer(values) {
const length = Math.ceil(values.length / 8);
const typedArray = new Uint8Array(length); // Initialized as 0's
for (let i = 0; i < values.length; ++i) {
const byteIndex = i >> 3;
const bitIndex = i % 8;
if (values[i]) {
typedArray[byteIndex] |= 1 << bitIndex;
}
}
return typedArray;
}
function flatten(values) {
return [].concat.apply([], values);
}
function createValuesBuffer(values, classProperty) {
const type = classProperty.type;
const valueType = classProperty.valueType;
const enumType = classProperty.enumType;
const flattenedValues = flatten(values);
if (type === MetadataType.STRING) {
return createStringBuffer(flattenedValues);
}
if (type === MetadataType.BOOLEAN) {
return createBooleanBuffer(flattenedValues);
}
if (defined(enumType)) {
const length = flattenedValues.length;
for (let i = 0; i < length; ++i) {
flattenedValues[i] = enumType.valuesByName[flattenedValues[i]];
}
}
return createBuffer(flattenedValues, valueType);
}
function createStringOffsetBuffer(values, offsetType) {
const encoder = new TextEncoder();
const strings = flatten(values);
const length = strings.length;
const offsets = new Array(length + 1);
let offset = 0;
for (let i = 0; i < length; ++i) {
offsets[i] = offset;
offset += encoder.encode(strings[i]).length;
}
offsets[length] = offset;
offsetType = defaultValue(offsetType, MetadataComponentType.UINT32);
return createBuffer(offsets, offsetType);
}
function createArrayOffsetBuffer(values, type, offsetType) {
const componentCount = MetadataType.getComponentCount(type);
const length = values.length;
const offsets = new Array(length + 1);
let offset = 0;
for (let i = 0; i < length; ++i) {
offsets[i] = offset;
offset += values[i].length / componentCount;
}
offsets[length] = offset;
offsetType = defaultValue(offsetType, MetadataComponentType.UINT32);
return createBuffer(offsets, offsetType);
}
function addPadding(uint8Array) {
// This tests that MetadataTable uses the Uint8Array's byteOffset properly
const paddingBytes = 8;
const padded = new Uint8Array(paddingBytes + uint8Array.length);
padded.set(uint8Array, paddingBytes);
return new Uint8Array(padded.buffer, paddingBytes, uint8Array.length);
}
export default MetadataTester;