yzt
2023-05-05 4c558c77a6a9d23f057f094c4dc3e315eabef497
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartesian4 from "../Core/Cartesian4.js";
import Check from "../Core/Check.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import Matrix2 from "../Core/Matrix2.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import MetadataType from "./MetadataType.js";
import MetadataComponentType from "./MetadataComponentType.js";
 
/**
 * A metadata property, as part of a {@link MetadataClass}
 *
 * @param {Object} options Object with the following properties:
 * @param {String} options.id The ID of the property.
 * @param {Object} options.property The property JSON object.
 * @param {Object.<String, MetadataEnum>} [options.enums] A dictionary of enums.
 *
 * @alias MetadataClassProperty
 * @constructor
 * @private
 * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
 */
function MetadataClassProperty(options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  var id = options.id;
  var property = options.property;
 
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("options.id", id);
  Check.typeOf.object("options.property", property);
  //>>includeEnd('debug');
 
  var enumType;
  if (defined(property.enumType)) {
    enumType = options.enums[property.enumType];
  }
 
  var type = property.type;
  var componentType;
  if (MetadataComponentType.hasOwnProperty(type)) {
    // For EXT_feature_metadata, single values were part of type, not
    // componentType. Transcode to the newer format.
    componentType = MetadataComponentType[type];
    type = MetadataType.SINGLE;
  } else {
    type = defaultValue(MetadataType[type], MetadataType.SINGLE);
    componentType = MetadataComponentType[property.componentType];
  }
  var valueType = getValueType(componentType, enumType);
 
  var normalized =
    MetadataComponentType.isIntegerType(componentType) &&
    defaultValue(property.normalized, false);
 
  var componentCount =
    type === MetadataType.ARRAY
      ? property.componentCount
      : MetadataType.getComponentCount(type);
 
  this._id = id;
  this._name = property.name;
  this._description = property.description;
  this._type = type;
  this._enumType = enumType;
  this._valueType = valueType;
  this._componentType = componentType;
  this._componentCount = componentCount;
  this._normalized = normalized;
  this._min = property.min;
  this._max = property.max;
  this._default = property.default;
  this._optional = defaultValue(property.optional, false);
  this._semantic = property.semantic;
  this._extras = property.extras;
  this._extensions = property.extensions;
}
 
Object.defineProperties(MetadataClassProperty.prototype, {
  /**
   * The ID of the property.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {String}
   * @readonly
   * @private
   */
  id: {
    get: function () {
      return this._id;
    },
  },
 
  /**
   * The name of the property.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {String}
   * @readonly
   * @private
   */
  name: {
    get: function () {
      return this._name;
    },
  },
 
  /**
   * The description of the property.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {String}
   * @readonly
   * @private
   */
  description: {
    get: function () {
      return this._description;
    },
  },
 
  /**
   * The type of the property. Each type holds one (SINGLE) or more components
   * (ARRAY, VECN, MATN).
   *
   *
   * @memberof MetadataClassProperty.prototype
   * @type {MetadataType}
   * @readonly
   * @private
   */
  type: {
    get: function () {
      return this._type;
    },
  },
 
  /**
   * The enum type of the property. Only defined when componentType is ENUM.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {MetadataEnum}
   * @readonly
   * @private
   */
  enumType: {
    get: function () {
      return this._enumType;
    },
  },
 
  /**
   * The component type of the property. This includes integer
   * (e.g. INT8 or UINT16), floating point (FLOAT32 and FLOAT64), STRING,
   * BOOLEAN, and ENUM component types.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {MetadataComponentType}
   * @readonly
   * @private
   */
  componentType: {
    get: function () {
      return this._componentType;
    },
  },
 
  /**
   * The datatype used for storing each component of the property. This
   * is usually the same as componentType except for ENUM, where this
   * returns an integer type
   *
   * @memberof MetadataClassProperty.prototype
   * @type {MetadataComponentType}
   * @readonly
   * @private
   */
  valueType: {
    get: function () {
      return this._valueType;
    },
  },
 
  /**
   * The number of components per element. Only defined when type is a fixed size ARRAY.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Number}
   * @readonly
   * @private
   */
  componentCount: {
    get: function () {
      return this._componentCount;
    },
  },
 
  /**
   * Whether the property is normalized.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Boolean}
   * @readonly
   * @private
   */
  normalized: {
    get: function () {
      return this._normalized;
    },
  },
 
  /**
   * A number or an array of numbers storing the maximum allowable value of this property. Only defined when type or componentType is a numeric type.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Number|Number[]}
   * @readonly
   * @private
   */
  max: {
    get: function () {
      return this._max;
    },
  },
 
  /**
   * A number or an array of numbers storing the minimum allowable value of this property. Only defined when type or componentType is a numeric type.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Number|Number[]}
   * @readonly
   * @private
   */
  min: {
    get: function () {
      return this._min;
    },
  },
 
  /**
   * A default value to use when an entity's property value is not defined.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Boolean|Number|String|Array}
   * @readonly
   * @private
   */
  default: {
    get: function () {
      return this._default;
    },
  },
 
  /**
   * Whether the property is optional.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Boolean}
   * @readonly
   * @private
   */
  optional: {
    get: function () {
      return this._optional;
    },
  },
 
  /**
   * An identifier that describes how this property should be interpreted.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {String}
   * @readonly
   * @private
   */
  semantic: {
    get: function () {
      return this._semantic;
    },
  },
 
  /**
   * Extras in the JSON object.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {*}
   * @readonly
   * @private
   */
  extras: {
    get: function () {
      return this._extras;
    },
  },
 
  /**
   * Extensions in the JSON object.
   *
   * @memberof MetadataClassProperty.prototype
   * @type {Object}
   * @readonly
   * @private
   */
  extensions: {
    get: function () {
      return this._extensions;
    },
  },
});
 
/**
 * Normalizes integer property values. If the property is not normalized
 * the value is returned unmodified.
 *
 * @param {*} value The integer value or array of integer values.
 * @returns {*} The normalized value or array of normalized values.
 *
 * @private
 */
MetadataClassProperty.prototype.normalize = function (value) {
  return normalize(this, value, MetadataComponentType.normalize);
};
 
/**
 * Unnormalizes integer property values. If the property is not normalized
 * the value is returned unmodified.
 *
 * @param {*} value The normalized value or array of normalized values.
 * @returns {*} The integer value or array of integer values.
 *
 * @private
 */
MetadataClassProperty.prototype.unnormalize = function (value) {
  return normalize(this, value, MetadataComponentType.unnormalize);
};
 
/**
 * Unpack VECN values into {@link Cartesian2}, {@link Cartesian3}, or
 * {@link Cartesian4} and MATN values into {@link Matrix2}, {@link Matrix3}, or
 * {@link Matrix4} depending on N. All other values (including arrays of
 * other sizes) are passed through unaltered.
 *
 * @param {*} value the original, normalized values.
 * @returns {*} The appropriate vector or matrix type if the value is a vector or matrix type, respectively. Otherwise, the value is returned unaltered.
 * @private
 */
MetadataClassProperty.prototype.unpackVectorAndMatrixTypes = function (value) {
  switch (this._type) {
    case MetadataType.VEC2:
      return Cartesian2.unpack(value);
    case MetadataType.VEC3:
      return Cartesian3.unpack(value);
    case MetadataType.VEC4:
      return Cartesian4.unpack(value);
    case MetadataType.MAT2:
      return Matrix2.unpack(value);
    case MetadataType.MAT3:
      return Matrix3.unpack(value);
    case MetadataType.MAT4:
      return Matrix4.unpack(value);
    default:
      return value;
  }
};
 
/**
 * Pack a {@link Cartesian2}, {@link Cartesian3}, or {@link Cartesian4} into an
 * array if this property is an <code>VECN</code>.
 * Pack a {@link Matrix2}, {@link Matrix3}, or {@link Matrix4} into an
 * array if this property is an <code>MATN</code>.
 * All other values (including arrays of other sizes) are passed through unaltered.
 *
 * @param {*} value The value of this property
 * @returns {*} An array of the appropriate length if the property is a vector or matrix type. Otherwise, the value is returned unaltered.
 * @private
 */
MetadataClassProperty.prototype.packVectorAndMatrixTypes = function (value) {
  switch (this._type) {
    case MetadataType.VEC2:
      return Cartesian2.pack(value, []);
    case MetadataType.VEC3:
      return Cartesian3.pack(value, []);
    case MetadataType.VEC4:
      return Cartesian4.pack(value, []);
    case MetadataType.MAT2:
      return Matrix2.pack(value, []);
    case MetadataType.MAT3:
      return Matrix3.pack(value, []);
    case MetadataType.MAT4:
      return Matrix4.pack(value, []);
    default:
      return value;
  }
};
 
/**
 * Validates whether the given value conforms to the property.
 *
 * @param {*} value The value.
 * @returns {String|undefined} An error message if the value does not conform to the property, otherwise undefined.
 * @private
 */
MetadataClassProperty.prototype.validate = function (value) {
  var type = this._type;
  var componentType = this._componentType;
 
  if (MetadataType.isVectorType(type) || MetadataType.isMatrixType(type)) {
    return validateVectorOrMatrix(value, type, componentType);
  } else if (type === MetadataType.ARRAY) {
    return validateArray(this, value, this._componentCount);
  }
 
  return checkValue(this, value);
};
 
function validateArray(classProperty, value, componentCount) {
  if (!Array.isArray(value)) {
    return getTypeErrorMessage(value, MetadataType.ARRAY);
  }
  var length = value.length;
  if (defined(componentCount) && componentCount !== length) {
    return "Array length does not match componentCount";
  }
  for (var i = 0; i < length; ++i) {
    var message = checkValue(classProperty, value[i]);
    if (defined(message)) {
      return message;
    }
  }
}
 
function validateVectorOrMatrix(value, type, componentType) {
  if (!MetadataComponentType.isVectorCompatible(componentType)) {
    var message = "componentType " + componentType + " is incompatible with ";
    if (MetadataType.isVectorType(type)) {
      return message + "vector type " + type;
    }
 
    return message + "matrix type " + type;
  }
 
  if (type === MetadataType.VEC2 && !(value instanceof Cartesian2)) {
    return "vector value " + value + " must be a Cartesian2";
  }
 
  if (type === MetadataType.VEC3 && !(value instanceof Cartesian3)) {
    return "vector value " + value + " must be a Cartesian3";
  }
 
  if (type === MetadataType.VEC4 && !(value instanceof Cartesian4)) {
    return "vector value " + value + " must be a Cartesian4";
  }
 
  if (type === MetadataType.MAT2 && !(value instanceof Matrix2)) {
    return "matrix value " + value + " must be a Matrix2";
  }
 
  if (type === MetadataType.MAT3 && !(value instanceof Matrix3)) {
    return "matrix value " + value + " must be a Matrix3";
  }
 
  if (type === MetadataType.MAT4 && !(value instanceof Matrix4)) {
    return "matrix value " + value + " must be a Matrix4";
  }
}
 
function getTypeErrorMessage(value, type) {
  return "value " + value + " does not match type " + type;
}
 
function getOutOfRangeErrorMessage(value, type, normalized) {
  var errorMessage = "value " + value + " is out of range for type " + type;
  if (normalized) {
    errorMessage += " (normalized)";
  }
  return errorMessage;
}
 
function checkInRange(value, componentType, normalized) {
  if (normalized) {
    var min = MetadataComponentType.isUnsignedIntegerType(componentType)
      ? 0.0
      : -1.0;
    var max = 1.0;
    if (value < min || value > max) {
      return getOutOfRangeErrorMessage(value, componentType, normalized);
    }
    return;
  }
 
  if (
    value < MetadataComponentType.getMinimum(componentType) ||
    value > MetadataComponentType.getMaximum(componentType)
  ) {
    return getOutOfRangeErrorMessage(value, componentType, normalized);
  }
}
 
function getNonFiniteErrorMessage(value, type) {
  return "value " + value + " of type " + type + " must be finite";
}
 
function checkValue(classProperty, value) {
  var javascriptType = typeof value;
 
  var enumType = classProperty._enumType;
  if (defined(enumType)) {
    if (javascriptType !== "string" || !defined(enumType.valuesByName[value])) {
      return "value " + value + " is not a valid enum name for " + enumType.id;
    }
    return;
  }
 
  var valueType = classProperty._valueType;
  var normalized = classProperty._normalized;
 
  switch (valueType) {
    case MetadataComponentType.INT8:
    case MetadataComponentType.UINT8:
    case MetadataComponentType.INT16:
    case MetadataComponentType.UINT16:
    case MetadataComponentType.INT32:
    case MetadataComponentType.UINT32:
      if (javascriptType !== "number") {
        return getTypeErrorMessage(value, valueType);
      }
      return checkInRange(value, valueType, normalized);
    case MetadataComponentType.INT64:
    case MetadataComponentType.UINT64:
      if (javascriptType !== "number" && javascriptType !== "bigint") {
        return getTypeErrorMessage(value, valueType);
      }
      return checkInRange(value, valueType, normalized);
    case MetadataComponentType.FLOAT32:
      if (javascriptType !== "number") {
        return getTypeErrorMessage(value, valueType);
      }
      if (isFinite(value)) {
        return checkInRange(value, valueType, normalized);
      }
      return getNonFiniteErrorMessage(value, valueType);
    case MetadataComponentType.FLOAT64:
      if (javascriptType !== "number") {
        return getTypeErrorMessage(value, valueType);
      }
      if (isFinite(value)) {
        return checkInRange(value, valueType, normalized);
      }
      return getNonFiniteErrorMessage(value, valueType);
    case MetadataComponentType.BOOLEAN:
      if (javascriptType !== "boolean") {
        return getTypeErrorMessage(value, valueType);
      }
      break;
    case MetadataComponentType.STRING:
      if (javascriptType !== "string") {
        return getTypeErrorMessage(value, valueType);
      }
      break;
  }
}
 
function normalize(classProperty, value, normalizeFunction) {
  var normalized = classProperty._normalized;
  if (!normalized) {
    return value;
  }
 
  var type = classProperty._type;
  var valueType = classProperty._valueType;
 
  var i;
  var length;
  if (type === MetadataType.ARRAY) {
    length = value.length;
    for (i = 0; i < length; ++i) {
      value[i] = normalizeFunction(value[i], valueType);
    }
  } else if (
    MetadataType.isVectorType(type) ||
    MetadataType.isMatrixType(type)
  ) {
    length = MetadataType.getComponentCount(type);
    for (i = 0; i < length; ++i) {
      value[i] = normalizeFunction(value[i], valueType);
    }
  } else {
    value = normalizeFunction(value, valueType);
  }
 
  return value;
}
 
function getValueType(componentType, enumType) {
  if (componentType === MetadataComponentType.ENUM) {
    return enumType.valueType;
  }
 
  return componentType;
}
 
export default MetadataClassProperty;