All files / engine/Source/Scene MetadataEnum.js

100% Statements 42/42
66.66% Branches 4/6
100% Functions 12/12
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                    115x 115x 115x     115x 115x     115x 115x   115x 115x 346x 346x 346x     115x   115x 115x 115x 115x 115x 115x 115x 115x 115x                             1x 117x 117x 117x     117x 116x     115x 346x     115x                     1x                   6x                             65x                             121x                         68x                         6x                         2x                         2x                         2x                         2x            
import Check from "../Core/Check.js";
import clone from "../Core/clone.js";
import Frozen from "../Core/Frozen.js";
import MetadataEnumValue from "./MetadataEnumValue.js";
import MetadataComponentType from "./MetadataComponentType.js";
 
/**
 * A metadata enum.
 * <p>
 * See the {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata|3D Metadata Specification} for 3D Tiles
 * </p>
 *
 * @param {object} options Object with the following properties:
 * @param {string} options.id The ID of the enum.
 * @param {MetadataEnumValue[]} options.values The enum values.
 * @param {MetadataComponentType} [options.valueType=MetadataComponentType.UINT16] The enum value type.
 * @param {string} [options.name] The name of the enum.
 * @param {string} [options.description] The description of the enum.
 * @param {*} [options.extras] Extra user-defined properties.
 * @param {object} [options.extensions] An object containing extensions.
 *
 * @alias MetadataEnum
 * @constructor
 * @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 MetadataEnum(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const id = options.id;
  const values = options.values;
 
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("options.id", id);
  Check.defined("options.values", values);
  //>>includeEnd('debug');
 
  const namesByValue = {};
  const valuesByName = {};
 
  const valuesLength = values.length;
  for (let i = 0; i < valuesLength; ++i) {
    const value = values[i];
    namesByValue[value.value] = value.name;
    valuesByName[value.name] = value.value;
  }
 
  const valueType = options.valueType ?? MetadataComponentType.UINT16;
 
  this._values = values;
  this._namesByValue = namesByValue;
  this._valuesByName = valuesByName;
  this._valueType = valueType;
  this._id = id;
  this._name = options.name;
  this._description = options.description;
  this._extras = clone(options.extras, true);
  this._extensions = clone(options.extensions, true);
}
 
/**
 * Creates a {@link MetadataEnum} from either 3D Tiles 1.1, 3DTILES_metadata, EXT_structural_metadata, or EXT_feature_metadata.
 *
 * @param {object} options Object with the following properties:
 * @param {string} options.id The ID of the enum.
 * @param {object} options.enum The enum JSON object.
 *
 * @returns {MetadataEnum} The newly created metadata enum.
 *
 * @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.
 */
MetadataEnum.fromJson = function (options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const id = options.id;
  const enumDefinition = options.enum;
 
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("options.id", id);
  Check.typeOf.object("options.enum", enumDefinition);
  //>>includeEnd('debug');
 
  const values = enumDefinition.values.map(function (value) {
    return MetadataEnumValue.fromJson(value);
  });
 
  return new MetadataEnum({
    id: id,
    values: values,
    valueType: MetadataComponentType[enumDefinition.valueType],
    name: enumDefinition.name,
    description: enumDefinition.description,
    extras: enumDefinition.extras,
    extensions: enumDefinition.extensions,
  });
};
 
Object.defineProperties(MetadataEnum.prototype, {
  /**
   * The enum values.
   *
   * @memberof MetadataEnum.prototype
   * @type {MetadataEnumValue[]}
   * @readonly
   */
  values: {
    get: function () {
      return this._values;
    },
  },
 
  /**
   * A dictionary mapping enum integer values to names.
   *
   * @memberof MetadataEnum.prototype
   * @type {Object<number, string>}
   * @readonly
   *
   * @private
   */
  namesByValue: {
    get: function () {
      return this._namesByValue;
    },
  },
 
  /**
   * A dictionary mapping enum names to integer values.
   *
   * @memberof MetadataEnum.prototype
   * @type {Object<string, number>}
   * @readonly
   *
   * @private
   */
  valuesByName: {
    get: function () {
      return this._valuesByName;
    },
  },
 
  /**
   * The enum value type.
   *
   * @memberof MetadataEnum.prototype
   * @type {MetadataComponentType}
   * @readonly
   */
  valueType: {
    get: function () {
      return this._valueType;
    },
  },
 
  /**
   * The ID of the enum.
   *
   * @memberof MetadataEnum.prototype
   * @type {string}
   * @readonly
   */
  id: {
    get: function () {
      return this._id;
    },
  },
 
  /**
   * The name of the enum.
   *
   * @memberof MetadataEnum.prototype
   * @type {string}
   * @readonly
   */
  name: {
    get: function () {
      return this._name;
    },
  },
 
  /**
   * The description of the enum.
   *
   * @memberof MetadataEnum.prototype
   * @type {string}
   * @readonly
   */
  description: {
    get: function () {
      return this._description;
    },
  },
 
  /**
   * Extra user-defined properties.
   *
   * @memberof MetadataEnum.prototype
   * @type {*}
   * @readonly
   */
  extras: {
    get: function () {
      return this._extras;
    },
  },
 
  /**
   * An object containing extensions.
   *
   * @memberof MetadataEnum.prototype
   * @type {object}
   * @readonly
   */
  extensions: {
    get: function () {
      return this._extensions;
    },
  },
});
 
export default MetadataEnum;