智慧水务管理系统 - 精河县供水工程综合管理平台

MetadataTable.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import Frozen from "../Core/Frozen.js";
  4. import defined from "../Core/defined.js";
  5. import MetadataEntity from "./MetadataEntity.js";
  6. import MetadataTableProperty from "./MetadataTableProperty.js";
  7. /**
  8. * A table containing binary metadata for a collection of entities. This is
  9. * used for representing binary properties of a batch table, as well as binary
  10. * metadata in 3D Tiles next extensions.
  11. * <p>
  12. * For 3D Tiles Next details, see the {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_metadata|3DTILES_metadata Extension} for 3D Tiles, as well as the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension} for glTF.
  13. * </p>
  14. *
  15. * @param {object} options Object with the following properties:
  16. * @param {number} options.count The number of entities in the table.
  17. * @param {object} [options.properties] A dictionary containing properties.
  18. * @param {MetadataClass} options.class The class that properties conform to.
  19. * @param {Object<string, Uint8Array>} [options.bufferViews] An object mapping bufferView IDs to Uint8Array objects.
  20. *
  21. * @alias MetadataTable
  22. * @constructor
  23. *
  24. * @private
  25. * @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.
  26. */
  27. function MetadataTable(options) {
  28. options = options ?? Frozen.EMPTY_OBJECT;
  29. const count = options.count;
  30. const metadataClass = options.class;
  31. //>>includeStart('debug', pragmas.debug);
  32. Check.typeOf.number.greaterThan("options.count", count, 0);
  33. Check.typeOf.object("options.class", metadataClass);
  34. //>>includeEnd('debug');
  35. let byteLength = 0;
  36. const properties = {};
  37. if (defined(options.properties)) {
  38. for (const propertyId in options.properties) {
  39. if (options.properties.hasOwnProperty(propertyId)) {
  40. const property = new MetadataTableProperty({
  41. count: count,
  42. property: options.properties[propertyId],
  43. classProperty: metadataClass.properties[propertyId],
  44. bufferViews: options.bufferViews,
  45. });
  46. properties[propertyId] = property;
  47. byteLength += property.byteLength;
  48. }
  49. }
  50. }
  51. this._count = count;
  52. this._class = metadataClass;
  53. this._properties = properties;
  54. this._byteLength = byteLength;
  55. }
  56. Object.defineProperties(MetadataTable.prototype, {
  57. /**
  58. * The number of entities in the table.
  59. *
  60. * @memberof MetadataTable.prototype
  61. * @type {number}
  62. * @readonly
  63. * @private
  64. */
  65. count: {
  66. get: function () {
  67. return this._count;
  68. },
  69. },
  70. /**
  71. * The class that properties conform to.
  72. *
  73. * @memberof MetadataTable.prototype
  74. * @type {MetadataClass}
  75. * @readonly
  76. * @private
  77. */
  78. class: {
  79. get: function () {
  80. return this._class;
  81. },
  82. },
  83. /**
  84. * The size of all typed arrays used in this table.
  85. *
  86. * @memberof MetadataTable.prototype
  87. * @type {number}
  88. * @readonly
  89. * @private
  90. */
  91. byteLength: {
  92. get: function () {
  93. return this._byteLength;
  94. },
  95. },
  96. /**
  97. * The properties of the table.
  98. *
  99. * @memberof MetadataTable.prototype
  100. * @type {Object<string, MetadataTableProperty>}
  101. * @readonly
  102. * @private
  103. */
  104. properties: {
  105. get: function () {
  106. return this._properties;
  107. },
  108. },
  109. });
  110. /**
  111. * Returns whether the table has this property.
  112. *
  113. * @param {string} propertyId The case-sensitive ID of the property.
  114. * @returns {boolean} Whether the table has this property.
  115. * @private
  116. */
  117. MetadataTable.prototype.hasProperty = function (propertyId) {
  118. return MetadataEntity.hasProperty(propertyId, this._properties, this._class);
  119. };
  120. /**
  121. * Returns whether the table has a property with the given semantic.
  122. *
  123. * @param {string} semantic The case-sensitive semantic of the property.
  124. * @returns {boolean} Whether the table has a property with the given semantic.
  125. * @private
  126. */
  127. MetadataTable.prototype.hasPropertyBySemantic = function (semantic) {
  128. return MetadataEntity.hasPropertyBySemantic(
  129. semantic,
  130. this._properties,
  131. this._class,
  132. );
  133. };
  134. /**
  135. * Returns an array of property IDs.
  136. *
  137. * @param {string[]} [results] An array into which to store the results.
  138. * @returns {string[]} The property IDs.
  139. * @private
  140. */
  141. MetadataTable.prototype.getPropertyIds = function (results) {
  142. return MetadataEntity.getPropertyIds(this._properties, this._class, results);
  143. };
  144. /**
  145. * Returns a copy of the value of the property with the given ID.
  146. * <p>
  147. * If the property is an enum the name of the enum is returned.
  148. * </p>
  149. * <p>
  150. * If the property is normalized the normalized value is returned. The value is
  151. * in the range [-1.0, 1.0] for signed integer types and [0.0, 1.0] for unsigned
  152. * integer types.
  153. * </p>
  154. * <p>
  155. * If the property is not normalized and type or componentType is INT64 or
  156. * UINT64 a BigInt will be returned. On platforms that don't support BigInt a
  157. * number will be returned instead. Note that numbers only support up to 52 bits
  158. * of integer precision. Values greater than 2^53 - 1 or less than -(2^53 - 1)
  159. * may lose precision when read.
  160. * </p>
  161. *
  162. * @param {number} index The index of the entity.
  163. * @param {string} propertyId The case-sensitive ID of the property.
  164. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this property.
  165. *
  166. * @exception {DeveloperError} index is required and between zero and count - 1
  167. * @private
  168. */
  169. MetadataTable.prototype.getProperty = function (index, propertyId) {
  170. //>>includeStart('debug', pragmas.debug);
  171. Check.typeOf.string("propertyId", propertyId);
  172. //>>includeEnd('debug');
  173. const property = this._properties[propertyId];
  174. let value;
  175. if (defined(property)) {
  176. value = property.get(index);
  177. } else {
  178. value = getDefault(this._class, propertyId);
  179. }
  180. return value;
  181. };
  182. /**
  183. * Sets the value of the property with the given ID.
  184. * <p>
  185. * If the property is an enum the name of the enum must be provided, not the
  186. * integer value.
  187. * </p>
  188. * <p>
  189. * If the property is normalized a normalized value must be provided to this
  190. * function. The value must be in the range [-1.0, 1.0] for signed integer
  191. * types and [0.0, 1.0] for unsigned integer types.
  192. * </p>
  193. * <p>
  194. * If the property is not normalized and type or componentType is INT64 or
  195. * UINT64 a BigInt may be provided. On platforms that don't support BigInt a
  196. * number may be provided instead. Note that numbers only support up to 52 bits
  197. * of integer precision. Values greater than 2^53 - 1 or less than -(2^53 - 1)
  198. * may lose precision when set."
  199. * </p>
  200. *
  201. * @param {number} index The index of the entity.
  202. * @param {string} propertyId The case-sensitive ID of the property.
  203. * @param {*} value The value of the property that will be copied.
  204. * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  205. *
  206. * @exception {DeveloperError} index is required and between zero and count - 1
  207. * @exception {DeveloperError} value does not match type
  208. * @exception {DeveloperError} value is out of range for type
  209. * @exception {DeveloperError} Array length does not match componentCount
  210. * @private
  211. */
  212. MetadataTable.prototype.setProperty = function (index, propertyId, value) {
  213. //>>includeStart('debug', pragmas.debug);
  214. Check.typeOf.string("propertyId", propertyId);
  215. //>>includeEnd('debug');
  216. const property = this._properties[propertyId];
  217. if (defined(property)) {
  218. property.set(index, value);
  219. return true;
  220. }
  221. return false;
  222. };
  223. /**
  224. * Returns a copy of the value of the property with the given semantic.
  225. *
  226. * @param {number} index The index of the entity.
  227. * @param {string} semantic The case-sensitive semantic of the property.
  228. * @returns {*} The value of the property or <code>undefined</code> if the entity does not have this semantic.
  229. *
  230. * @exception {DeveloperError} index is required and between zero and count - 1
  231. * @private
  232. */
  233. MetadataTable.prototype.getPropertyBySemantic = function (index, semantic) {
  234. //>>includeStart('debug', pragmas.debug);
  235. Check.typeOf.string("semantic", semantic);
  236. //>>includeEnd('debug');
  237. let property;
  238. const propertiesBySemantic = this._class.propertiesBySemantic;
  239. if (defined(propertiesBySemantic)) {
  240. property = propertiesBySemantic[semantic];
  241. }
  242. if (defined(property)) {
  243. return this.getProperty(index, property.id);
  244. }
  245. return undefined;
  246. };
  247. /**
  248. * Sets the value of the property with the given semantic.
  249. *
  250. * @param {number} index The index of the entity.
  251. * @param {string} semantic The case-sensitive semantic of the property.
  252. * @param {*} value The value of the property that will be copied.
  253. * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  254. *
  255. * @exception {DeveloperError} index is required and between zero and count - 1
  256. * @exception {DeveloperError} value does not match type
  257. * @exception {DeveloperError} value is out of range for type
  258. * @exception {DeveloperError} Array length does not match componentCount
  259. * @private
  260. */
  261. MetadataTable.prototype.setPropertyBySemantic = function (
  262. index,
  263. semantic,
  264. value,
  265. ) {
  266. //>>includeStart('debug', pragmas.debug);
  267. Check.typeOf.string("semantic", semantic);
  268. //>>includeEnd('debug');
  269. let property;
  270. const propertiesBySemantic = this._class.propertiesBySemantic;
  271. if (defined(propertiesBySemantic)) {
  272. property = propertiesBySemantic[semantic];
  273. }
  274. if (defined(property)) {
  275. return this.setProperty(index, property.id, value);
  276. }
  277. return false;
  278. };
  279. /**
  280. * Returns a typed array containing the property values for a given propertyId.
  281. *
  282. * @param {string} propertyId The case-sensitive ID of the property.
  283. * @returns {*} The typed array containing the property values or <code>undefined</code> if the property values are not stored in a typed array.
  284. *
  285. * @private
  286. */
  287. MetadataTable.prototype.getPropertyTypedArray = function (propertyId) {
  288. //>>includeStart('debug', pragmas.debug);
  289. Check.typeOf.string("propertyId", propertyId);
  290. //>>includeEnd('debug');
  291. const property = this._properties[propertyId];
  292. if (defined(property)) {
  293. return property.getTypedArray();
  294. }
  295. return undefined;
  296. };
  297. /**
  298. * Returns a typed array containing the property values for the property with the given semantic.
  299. *
  300. * @param {string} semantic The case-sensitive semantic of the property.
  301. * @returns {*} The typed array containing the property values or <code>undefined</code> if the property values are not stored in a typed array.
  302. *
  303. * @private
  304. */
  305. MetadataTable.prototype.getPropertyTypedArrayBySemantic = function (semantic) {
  306. //>>includeStart('debug', pragmas.debug);
  307. Check.typeOf.string("semantic", semantic);
  308. //>>includeEnd('debug');
  309. let property;
  310. const propertiesBySemantic = this._class.propertiesBySemantic;
  311. if (defined(propertiesBySemantic)) {
  312. property = propertiesBySemantic[semantic];
  313. }
  314. if (defined(property)) {
  315. return this.getPropertyTypedArray(property.id);
  316. }
  317. return undefined;
  318. };
  319. function getDefault(classDefinition, propertyId) {
  320. const classProperties = classDefinition.properties;
  321. if (!defined(classProperties)) {
  322. return undefined;
  323. }
  324. const classProperty = classProperties[propertyId];
  325. if (defined(classProperty) && defined(classProperty.default)) {
  326. let value = classProperty.default;
  327. if (classProperty.isArray) {
  328. value = clone(value, true);
  329. }
  330. value = classProperty.normalize(value);
  331. return classProperty.unpackVectorAndMatrixTypes(value);
  332. }
  333. }
  334. export default MetadataTable;