MetadataEntity.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. /**
  6. * An entity containing metadata.
  7. * <p>
  8. * This type describes an interface and is not intended to be instantiated directly.
  9. * </p>
  10. * <p>
  11. * See the {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_metadata|3DTILES_metadata Extension} for 3D Tiles
  12. * </p>
  13. *
  14. * @alias MetadataEntity
  15. * @constructor
  16. *
  17. * @private
  18. * @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.
  19. */
  20. function MetadataEntity() {}
  21. Object.defineProperties(MetadataEntity.prototype, {
  22. /**
  23. * The class that properties conform to.
  24. *
  25. * @memberof MetadataEntity.prototype
  26. * @type {MetadataClass}
  27. * @readonly
  28. * @private
  29. */
  30. class: {
  31. // eslint-disable-next-line getter-return
  32. get: function () {
  33. DeveloperError.throwInstantiationError();
  34. },
  35. },
  36. });
  37. /**
  38. * Returns whether the entity has this property.
  39. *
  40. * @param {String} propertyId The case-sensitive ID of the property.
  41. * @returns {Boolean} Whether the entity has this property.
  42. * @private
  43. */
  44. MetadataEntity.prototype.hasProperty = function (propertyId) {
  45. DeveloperError.throwInstantiationError();
  46. };
  47. /**
  48. * Returns whether the entity has a property with the given semantic.
  49. *
  50. * @param {String} semantic The case-sensitive semantic of the property.
  51. * @returns {Boolean} Whether the entity has a property with the given semantic.
  52. * @private
  53. */
  54. MetadataEntity.prototype.hasPropertyBySemantic = function (semantic) {
  55. DeveloperError.throwInstantiationError();
  56. };
  57. /**
  58. * Returns an array of property IDs.
  59. *
  60. * @param {String[]} [results] An array into which to store the results.
  61. * @returns {String[]} The property IDs.
  62. * @private
  63. */
  64. MetadataEntity.prototype.getPropertyIds = function (results) {
  65. DeveloperError.throwInstantiationError();
  66. };
  67. /**
  68. * Returns a copy of the value of the property with the given ID.
  69. * <p>
  70. * If the property is normalized the normalized value is returned.
  71. * </p>
  72. *
  73. * @param {String} propertyId The case-sensitive ID of the property.
  74. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  75. * @private
  76. */
  77. MetadataEntity.prototype.getProperty = function (propertyId) {
  78. DeveloperError.throwInstantiationError();
  79. };
  80. /**
  81. * Sets the value of the property with the given ID.
  82. * <p>
  83. * If the property is normalized a normalized value must be provided to this function.
  84. * </p>
  85. *
  86. * @param {String} propertyId The case-sensitive ID of the property.
  87. * @param {*} value The value of the property that will be copied.
  88. * @returns {Boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  89. * @private
  90. */
  91. MetadataEntity.prototype.setProperty = function (propertyId, value) {
  92. DeveloperError.throwInstantiationError();
  93. };
  94. /**
  95. * Returns a copy of the value of the property with the given semantic.
  96. *
  97. * @param {String} semantic The case-sensitive semantic of the property.
  98. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  99. * @private
  100. */
  101. MetadataEntity.prototype.getPropertyBySemantic = function (semantic) {
  102. DeveloperError.throwInstantiationError();
  103. };
  104. /**
  105. * Sets the value of the property with the given semantic.
  106. *
  107. * @param {String} semantic The case-sensitive semantic of the property.
  108. * @param {*} value The value of the property that will be copied.
  109. * @returns {Boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  110. * @private
  111. */
  112. MetadataEntity.prototype.setPropertyBySemantic = function (semantic, value) {
  113. DeveloperError.throwInstantiationError();
  114. };
  115. /**
  116. * Returns whether the entity has this property.
  117. *
  118. * @param {String} propertyId The case-sensitive ID of the property.
  119. * @param {Object} properties The dictionary containing properties.
  120. * @param {MetadataClass} classDefinition The class.
  121. * @returns {Boolean} Whether the entity has this property.
  122. *
  123. * @private
  124. */
  125. MetadataEntity.hasProperty = function (
  126. propertyId,
  127. properties,
  128. classDefinition
  129. ) {
  130. //>>includeStart('debug', pragmas.debug);
  131. Check.typeOf.string("propertyId", propertyId);
  132. Check.typeOf.object("properties", properties);
  133. Check.typeOf.object("classDefinition", classDefinition);
  134. //>>includeEnd('debug');
  135. if (defined(properties[propertyId])) {
  136. return true;
  137. }
  138. const classProperties = classDefinition.properties;
  139. if (!defined(classProperties)) {
  140. return false;
  141. }
  142. const classProperty = classProperties[propertyId];
  143. if (defined(classProperty) && defined(classProperty.default)) {
  144. return true;
  145. }
  146. return false;
  147. };
  148. /**
  149. * Returns whether the entity has a property with the given semantic.
  150. *
  151. * @param {String} semantic The case-sensitive semantic of the property.
  152. * @param {Object} properties The dictionary containing properties.
  153. * @param {MetadataClass} classDefinition The class.
  154. * @returns {Boolean} Whether the entity has a property with the given semantic.
  155. *
  156. * @private
  157. */
  158. MetadataEntity.hasPropertyBySemantic = function (
  159. semantic,
  160. properties,
  161. classDefinition
  162. ) {
  163. //>>includeStart('debug', pragmas.debug);
  164. Check.typeOf.string("semantic", semantic);
  165. Check.typeOf.object("properties", properties);
  166. Check.typeOf.object("classDefinition", classDefinition);
  167. //>>includeEnd('debug');
  168. const propertiesBySemantic = classDefinition.propertiesBySemantic;
  169. if (!defined(propertiesBySemantic)) {
  170. return false;
  171. }
  172. const property = propertiesBySemantic[semantic];
  173. return defined(property);
  174. };
  175. /**
  176. * Returns an array of property IDs.
  177. *
  178. * @param {Object} properties The dictionary containing properties.
  179. * @param {MetadataClass} classDefinition The class.
  180. * @param {String[]} [results] An array into which to store the results.
  181. * @returns {String[]} The property IDs.
  182. *
  183. * @private
  184. */
  185. MetadataEntity.getPropertyIds = function (
  186. properties,
  187. classDefinition,
  188. results
  189. ) {
  190. //>>includeStart('debug', pragmas.debug);
  191. Check.typeOf.object("properties", properties);
  192. Check.typeOf.object("classDefinition", classDefinition);
  193. //>>includeEnd('debug');
  194. results = defined(results) ? results : [];
  195. results.length = 0;
  196. // Add entity properties
  197. for (const propertyId in properties) {
  198. if (
  199. properties.hasOwnProperty(propertyId) &&
  200. defined(properties[propertyId])
  201. ) {
  202. results.push(propertyId);
  203. }
  204. }
  205. // Add default properties
  206. const classProperties = classDefinition.properties;
  207. if (defined(classProperties)) {
  208. for (const classPropertyId in classProperties) {
  209. if (
  210. classProperties.hasOwnProperty(classPropertyId) &&
  211. !defined(properties[classPropertyId]) &&
  212. defined(classProperties[classPropertyId].default)
  213. ) {
  214. results.push(classPropertyId);
  215. }
  216. }
  217. }
  218. return results;
  219. };
  220. /**
  221. * Returns a copy of the value of the property with the given ID.
  222. * <p>
  223. * If the property is normalized the normalized value is returned.
  224. * </p>
  225. *
  226. * @param {String} propertyId The case-sensitive ID of the property.
  227. * @param {Object} properties The dictionary containing properties.
  228. * @param {MetadataClass} classDefinition The class.
  229. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  230. *
  231. * @private
  232. */
  233. MetadataEntity.getProperty = function (
  234. propertyId,
  235. properties,
  236. classDefinition
  237. ) {
  238. //>>includeStart('debug', pragmas.debug);
  239. Check.typeOf.string("propertyId", propertyId);
  240. Check.typeOf.object("properties", properties);
  241. Check.typeOf.object("classDefinition", classDefinition);
  242. if (!defined(classDefinition.properties[propertyId])) {
  243. throw new DeveloperError(`Class definition missing property ${propertyId}`);
  244. }
  245. //>>includeEnd('debug');
  246. const classProperty = classDefinition.properties[propertyId];
  247. let value = properties[propertyId];
  248. // Clone array values
  249. if (Array.isArray(value)) {
  250. value = value.slice();
  251. }
  252. // Arrays of vectors are represented as nested arrays in JSON
  253. const enableNestedArrays = true;
  254. // Handle noData and default
  255. value = classProperty.handleNoData(value);
  256. if (!defined(value) && defined(classProperty.default)) {
  257. value = clone(classProperty.default, true);
  258. return classProperty.unpackVectorAndMatrixTypes(value, enableNestedArrays);
  259. }
  260. if (!defined(value)) {
  261. return undefined;
  262. }
  263. value = classProperty.normalize(value);
  264. value = classProperty.applyValueTransform(value);
  265. return classProperty.unpackVectorAndMatrixTypes(value, enableNestedArrays);
  266. };
  267. /**
  268. * Sets the value of the property with the given ID.
  269. * <p>
  270. * If the property is normalized a normalized value must be provided to this function.
  271. * </p>
  272. *
  273. * @param {String} propertyId The case-sensitive ID of the property.
  274. * @param {*} value The value of the property that will be copied.
  275. * @param {Object} properties The dictionary containing properties.
  276. * @param {MetadataClass} classDefinition The class.
  277. * @returns {Boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  278. *
  279. * @private
  280. */
  281. MetadataEntity.setProperty = function (
  282. propertyId,
  283. value,
  284. properties,
  285. classDefinition
  286. ) {
  287. //>>includeStart('debug', pragmas.debug);
  288. Check.typeOf.string("propertyId", propertyId);
  289. Check.defined("value", value);
  290. Check.typeOf.object("properties", properties);
  291. Check.typeOf.object("classDefinition", classDefinition);
  292. //>>includeEnd('debug');
  293. if (!defined(properties[propertyId])) {
  294. return false;
  295. }
  296. if (Array.isArray(value)) {
  297. value = value.slice(); // clone
  298. }
  299. let classProperty;
  300. const classProperties = classDefinition.properties;
  301. if (defined(classProperties)) {
  302. classProperty = classProperties[propertyId];
  303. }
  304. // arrays of vectors are represented as nested arrays in JSON
  305. const enableNestedArrays = true;
  306. if (defined(classProperty)) {
  307. value = classProperty.packVectorAndMatrixTypes(value, enableNestedArrays);
  308. value = classProperty.unapplyValueTransform(value);
  309. value = classProperty.unnormalize(value);
  310. }
  311. properties[propertyId] = value;
  312. return true;
  313. };
  314. /**
  315. * Returns a copy of the value of the property with the given semantic.
  316. *
  317. * @param {String} semantic The case-sensitive semantic of the property.
  318. * @param {Object} properties The dictionary containing properties.
  319. * @param {MetadataClass} classDefinition The class.
  320. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  321. *
  322. * @private
  323. */
  324. MetadataEntity.getPropertyBySemantic = function (
  325. semantic,
  326. properties,
  327. classDefinition
  328. ) {
  329. //>>includeStart('debug', pragmas.debug);
  330. Check.typeOf.string("semantic", semantic);
  331. Check.typeOf.object("properties", properties);
  332. Check.typeOf.object("classDefinition", classDefinition);
  333. //>>includeEnd('debug');
  334. const propertiesBySemantic = classDefinition.propertiesBySemantic;
  335. if (!defined(propertiesBySemantic)) {
  336. return undefined;
  337. }
  338. const property = propertiesBySemantic[semantic];
  339. if (defined(property)) {
  340. return MetadataEntity.getProperty(property.id, properties, classDefinition);
  341. }
  342. return undefined;
  343. };
  344. /**
  345. * Sets the value of the property with the given semantic.
  346. *
  347. * @param {String} semantic The case-sensitive semantic of the property.
  348. * @param {*} value The value of the property that will be copied.
  349. * @param {Object} properties The dictionary containing properties.
  350. * @param {MetadataClass} classDefinition The class.
  351. * @returns {Boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  352. * @private
  353. */
  354. MetadataEntity.setPropertyBySemantic = function (
  355. semantic,
  356. value,
  357. properties,
  358. classDefinition
  359. ) {
  360. //>>includeStart('debug', pragmas.debug);
  361. Check.typeOf.string("semantic", semantic);
  362. Check.defined("value", value);
  363. Check.typeOf.object("properties", properties);
  364. Check.typeOf.object("classDefinition", classDefinition);
  365. //>>includeEnd('debug');
  366. const propertiesBySemantic = classDefinition.propertiesBySemantic;
  367. if (!defined(propertiesBySemantic)) {
  368. return false;
  369. }
  370. const property = classDefinition.propertiesBySemantic[semantic];
  371. if (defined(property)) {
  372. return MetadataEntity.setProperty(
  373. property.id,
  374. value,
  375. properties,
  376. classDefinition
  377. );
  378. }
  379. return false;
  380. };
  381. export default MetadataEntity;