123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- import Check from "../Core/Check.js";
- import clone from "../Core/clone.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- /**
- * An entity containing metadata.
- * <p>
- * This type describes an interface and is not intended to be instantiated directly.
- * </p>
- * <p>
- * See the {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_metadata|3DTILES_metadata Extension} for 3D Tiles
- * </p>
- *
- * @alias MetadataEntity
- * @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 MetadataEntity() {}
- Object.defineProperties(MetadataEntity.prototype, {
- /**
- * The class that properties conform to.
- *
- * @memberof MetadataEntity.prototype
- * @type {MetadataClass}
- * @readonly
- * @private
- */
- class: {
- // eslint-disable-next-line getter-return
- get: function () {
- DeveloperError.throwInstantiationError();
- },
- },
- });
- /**
- * Returns whether the entity has this property.
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @returns {boolean} Whether the entity has this property.
- * @private
- */
- MetadataEntity.prototype.hasProperty = function (propertyId) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Returns whether the entity has a property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @returns {boolean} Whether the entity has a property with the given semantic.
- * @private
- */
- MetadataEntity.prototype.hasPropertyBySemantic = function (semantic) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Returns an array of property IDs.
- *
- * @param {string[]} [results] An array into which to store the results.
- * @returns {string[]} The property IDs.
- * @private
- */
- MetadataEntity.prototype.getPropertyIds = function (results) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Returns a copy of the value of the property with the given ID.
- * <p>
- * If the property is normalized the normalized value is returned.
- * </p>
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
- * @private
- */
- MetadataEntity.prototype.getProperty = function (propertyId) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Sets the value of the property with the given ID.
- * <p>
- * If the property is normalized a normalized value must be provided to this function.
- * </p>
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @param {*} value The value of the property that will be copied.
- * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
- * @private
- */
- MetadataEntity.prototype.setProperty = function (propertyId, value) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Returns a copy of the value of the property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
- * @private
- */
- MetadataEntity.prototype.getPropertyBySemantic = function (semantic) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Sets the value of the property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @param {*} value The value of the property that will be copied.
- * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
- * @private
- */
- MetadataEntity.prototype.setPropertyBySemantic = function (semantic, value) {
- DeveloperError.throwInstantiationError();
- };
- /**
- * Returns whether the entity has this property.
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {boolean} Whether the entity has this property.
- *
- * @private
- */
- MetadataEntity.hasProperty = function (
- propertyId,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("propertyId", propertyId);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- if (defined(properties[propertyId])) {
- return true;
- }
- const classProperties = classDefinition.properties;
- if (!defined(classProperties)) {
- return false;
- }
- const classProperty = classProperties[propertyId];
- if (defined(classProperty) && defined(classProperty.default)) {
- return true;
- }
- return false;
- };
- /**
- * Returns whether the entity has a property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {boolean} Whether the entity has a property with the given semantic.
- *
- * @private
- */
- MetadataEntity.hasPropertyBySemantic = function (
- semantic,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("semantic", semantic);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- const propertiesBySemantic = classDefinition.propertiesBySemantic;
- if (!defined(propertiesBySemantic)) {
- return false;
- }
- const property = propertiesBySemantic[semantic];
- return defined(property);
- };
- /**
- * Returns an array of property IDs.
- *
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @param {string[]} [results] An array into which to store the results.
- * @returns {string[]} The property IDs.
- *
- * @private
- */
- MetadataEntity.getPropertyIds = function (
- properties,
- classDefinition,
- results
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- results = defined(results) ? results : [];
- results.length = 0;
- // Add entity properties
- for (const propertyId in properties) {
- if (
- properties.hasOwnProperty(propertyId) &&
- defined(properties[propertyId])
- ) {
- results.push(propertyId);
- }
- }
- // Add default properties
- const classProperties = classDefinition.properties;
- if (defined(classProperties)) {
- for (const classPropertyId in classProperties) {
- if (
- classProperties.hasOwnProperty(classPropertyId) &&
- !defined(properties[classPropertyId]) &&
- defined(classProperties[classPropertyId].default)
- ) {
- results.push(classPropertyId);
- }
- }
- }
- return results;
- };
- /**
- * Returns a copy of the value of the property with the given ID.
- * <p>
- * If the property is normalized the normalized value is returned.
- * </p>
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
- *
- * @private
- */
- MetadataEntity.getProperty = function (
- propertyId,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("propertyId", propertyId);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- if (!defined(classDefinition.properties[propertyId])) {
- throw new DeveloperError(`Class definition missing property ${propertyId}`);
- }
- //>>includeEnd('debug');
- const classProperty = classDefinition.properties[propertyId];
- let value = properties[propertyId];
- // Clone array values
- if (Array.isArray(value)) {
- value = value.slice();
- }
- // Arrays of vectors are represented as nested arrays in JSON
- const enableNestedArrays = true;
- // Handle noData and default
- value = classProperty.handleNoData(value);
- if (!defined(value) && defined(classProperty.default)) {
- value = clone(classProperty.default, true);
- return classProperty.unpackVectorAndMatrixTypes(value, enableNestedArrays);
- }
- if (!defined(value)) {
- return undefined;
- }
- value = classProperty.normalize(value);
- value = classProperty.applyValueTransform(value);
- return classProperty.unpackVectorAndMatrixTypes(value, enableNestedArrays);
- };
- /**
- * Sets the value of the property with the given ID.
- * <p>
- * If the property is normalized a normalized value must be provided to this function.
- * </p>
- *
- * @param {string} propertyId The case-sensitive ID of the property.
- * @param {*} value The value of the property that will be copied.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
- *
- * @private
- */
- MetadataEntity.setProperty = function (
- propertyId,
- value,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("propertyId", propertyId);
- Check.defined("value", value);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- if (!defined(properties[propertyId])) {
- return false;
- }
- if (Array.isArray(value)) {
- value = value.slice(); // clone
- }
- let classProperty;
- const classProperties = classDefinition.properties;
- if (defined(classProperties)) {
- classProperty = classProperties[propertyId];
- }
- // arrays of vectors are represented as nested arrays in JSON
- const enableNestedArrays = true;
- if (defined(classProperty)) {
- value = classProperty.packVectorAndMatrixTypes(value, enableNestedArrays);
- value = classProperty.unapplyValueTransform(value);
- value = classProperty.unnormalize(value);
- }
- properties[propertyId] = value;
- return true;
- };
- /**
- * Returns a copy of the value of the property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
- *
- * @private
- */
- MetadataEntity.getPropertyBySemantic = function (
- semantic,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("semantic", semantic);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- const propertiesBySemantic = classDefinition.propertiesBySemantic;
- if (!defined(propertiesBySemantic)) {
- return undefined;
- }
- const property = propertiesBySemantic[semantic];
- if (defined(property)) {
- return MetadataEntity.getProperty(property.id, properties, classDefinition);
- }
- return undefined;
- };
- /**
- * Sets the value of the property with the given semantic.
- *
- * @param {string} semantic The case-sensitive semantic of the property.
- * @param {*} value The value of the property that will be copied.
- * @param {object} properties The dictionary containing properties.
- * @param {MetadataClass} classDefinition The class.
- * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
- * @private
- */
- MetadataEntity.setPropertyBySemantic = function (
- semantic,
- value,
- properties,
- classDefinition
- ) {
- //>>includeStart('debug', pragmas.debug);
- Check.typeOf.string("semantic", semantic);
- Check.defined("value", value);
- Check.typeOf.object("properties", properties);
- Check.typeOf.object("classDefinition", classDefinition);
- //>>includeEnd('debug');
- const propertiesBySemantic = classDefinition.propertiesBySemantic;
- if (!defined(propertiesBySemantic)) {
- return false;
- }
- const property = classDefinition.propertiesBySemantic[semantic];
- if (defined(property)) {
- return MetadataEntity.setProperty(
- property.id,
- value,
- properties,
- classDefinition
- );
- }
- return false;
- };
- export default MetadataEntity;
|