MetadataEnum.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import MetadataEnumValue from "./MetadataEnumValue.js";
  5. import MetadataComponentType from "./MetadataComponentType.js";
  6. /**
  7. * A metadata enum.
  8. * <p>
  9. * See the {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata|3D Metadata Specification} for 3D Tiles
  10. * </p>
  11. *
  12. * @param {object} options Object with the following properties:
  13. * @param {string} options.id The ID of the enum.
  14. * @param {MetadataEnumValue[]} options.values The enum values.
  15. * @param {MetadataComponentType} [options.valueType=MetadataComponentType.UINT16] The enum value type.
  16. * @param {string} [options.name] The name of the enum.
  17. * @param {string} [options.description] The description of the enum.
  18. * @param {*} [options.extras] Extra user-defined properties.
  19. * @param {object} [options.extensions] An object containing extensions.
  20. *
  21. * @alias MetadataEnum
  22. * @constructor
  23. * @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.
  24. */
  25. function MetadataEnum(options) {
  26. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  27. const id = options.id;
  28. const values = options.values;
  29. //>>includeStart('debug', pragmas.debug);
  30. Check.typeOf.string("options.id", id);
  31. Check.defined("options.values", values);
  32. //>>includeEnd('debug');
  33. const namesByValue = {};
  34. const valuesByName = {};
  35. const valuesLength = values.length;
  36. for (let i = 0; i < valuesLength; ++i) {
  37. const value = values[i];
  38. namesByValue[value.value] = value.name;
  39. valuesByName[value.name] = value.value;
  40. }
  41. const valueType = defaultValue(
  42. options.valueType,
  43. MetadataComponentType.UINT16
  44. );
  45. this._values = values;
  46. this._namesByValue = namesByValue;
  47. this._valuesByName = valuesByName;
  48. this._valueType = valueType;
  49. this._id = id;
  50. this._name = options.name;
  51. this._description = options.description;
  52. this._extras = clone(options.extras, true);
  53. this._extensions = clone(options.extensions, true);
  54. }
  55. /**
  56. * Creates a {@link MetadataEnum} from either 3D Tiles 1.1, 3DTILES_metadata, EXT_structural_metadata, or EXT_feature_metadata.
  57. *
  58. * @param {object} options Object with the following properties:
  59. * @param {string} options.id The ID of the enum.
  60. * @param {object} options.enum The enum JSON object.
  61. *
  62. * @returns {MetadataEnum} The newly created metadata enum.
  63. *
  64. * @private
  65. * @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.
  66. */
  67. MetadataEnum.fromJson = function (options) {
  68. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  69. const id = options.id;
  70. const enumDefinition = options.enum;
  71. //>>includeStart('debug', pragmas.debug);
  72. Check.typeOf.string("options.id", id);
  73. Check.typeOf.object("options.enum", enumDefinition);
  74. //>>includeEnd('debug');
  75. const values = enumDefinition.values.map(function (value) {
  76. return MetadataEnumValue.fromJson(value);
  77. });
  78. return new MetadataEnum({
  79. id: id,
  80. values: values,
  81. valueType: MetadataComponentType[enumDefinition.valueType],
  82. name: enumDefinition.name,
  83. description: enumDefinition.description,
  84. extras: enumDefinition.extras,
  85. extensions: enumDefinition.extensions,
  86. });
  87. };
  88. Object.defineProperties(MetadataEnum.prototype, {
  89. /**
  90. * The enum values.
  91. *
  92. * @memberof MetadataEnum.prototype
  93. * @type {MetadataEnumValue[]}
  94. * @readonly
  95. */
  96. values: {
  97. get: function () {
  98. return this._values;
  99. },
  100. },
  101. /**
  102. * A dictionary mapping enum integer values to names.
  103. *
  104. * @memberof MetadataEnum.prototype
  105. * @type {Object<number, string>}
  106. * @readonly
  107. *
  108. * @private
  109. */
  110. namesByValue: {
  111. get: function () {
  112. return this._namesByValue;
  113. },
  114. },
  115. /**
  116. * A dictionary mapping enum names to integer values.
  117. *
  118. * @memberof MetadataEnum.prototype
  119. * @type {Object<string, number>}
  120. * @readonly
  121. *
  122. * @private
  123. */
  124. valuesByName: {
  125. get: function () {
  126. return this._valuesByName;
  127. },
  128. },
  129. /**
  130. * The enum value type.
  131. *
  132. * @memberof MetadataEnum.prototype
  133. * @type {MetadataComponentType}
  134. * @readonly
  135. */
  136. valueType: {
  137. get: function () {
  138. return this._valueType;
  139. },
  140. },
  141. /**
  142. * The ID of the enum.
  143. *
  144. * @memberof MetadataEnum.prototype
  145. * @type {string}
  146. * @readonly
  147. */
  148. id: {
  149. get: function () {
  150. return this._id;
  151. },
  152. },
  153. /**
  154. * The name of the enum.
  155. *
  156. * @memberof MetadataEnum.prototype
  157. * @type {string}
  158. * @readonly
  159. */
  160. name: {
  161. get: function () {
  162. return this._name;
  163. },
  164. },
  165. /**
  166. * The description of the enum.
  167. *
  168. * @memberof MetadataEnum.prototype
  169. * @type {string}
  170. * @readonly
  171. */
  172. description: {
  173. get: function () {
  174. return this._description;
  175. },
  176. },
  177. /**
  178. * Extra user-defined properties.
  179. *
  180. * @memberof MetadataEnum.prototype
  181. * @type {*}
  182. * @readonly
  183. */
  184. extras: {
  185. get: function () {
  186. return this._extras;
  187. },
  188. },
  189. /**
  190. * An object containing extensions.
  191. *
  192. * @memberof MetadataEnum.prototype
  193. * @type {object}
  194. * @readonly
  195. */
  196. extensions: {
  197. get: function () {
  198. return this._extensions;
  199. },
  200. },
  201. });
  202. export default MetadataEnum;