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

MetadataTableProperty.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import ComponentDatatype from "../Core/ComponentDatatype.js";
  4. import Frozen from "../Core/Frozen.js";
  5. import defined from "../Core/defined.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import FeatureDetection from "../Core/FeatureDetection.js";
  8. import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
  9. import oneTimeWarning from "../Core/oneTimeWarning.js";
  10. import MetadataComponentType from "./MetadataComponentType.js";
  11. import MetadataClassProperty from "./MetadataClassProperty.js";
  12. import MetadataType from "./MetadataType.js";
  13. import addAllToArray from "../Core/addAllToArray.js";
  14. /**
  15. * A binary property in a {@MetadataTable}
  16. * <p>
  17. * For 3D Tiles Next details, see the {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_metadata|3DTILES_metadata Extension}
  18. * for 3D Tiles, as well as the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata Extension}
  19. * for glTF. For the legacy glTF extension, see {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension}
  20. * </p>
  21. *
  22. * @param {object} options Object with the following properties:
  23. * @param {number} options.count The number of elements in each property array.
  24. * @param {object} options.property The property JSON object.
  25. * @param {MetadataClassProperty} options.classProperty The class property.
  26. * @param {Object<string, Uint8Array>} options.bufferViews An object mapping bufferView IDs to Uint8Array objects.
  27. *
  28. * @alias MetadataTableProperty
  29. * @constructor
  30. *
  31. * @private
  32. * @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.
  33. */
  34. function MetadataTableProperty(options) {
  35. options = options ?? Frozen.EMPTY_OBJECT;
  36. const count = options.count;
  37. const property = options.property;
  38. const classProperty = options.classProperty;
  39. const bufferViews = options.bufferViews;
  40. //>>includeStart('debug', pragmas.debug);
  41. Check.typeOf.number.greaterThan("options.count", count, 0);
  42. Check.typeOf.object("options.property", property);
  43. Check.typeOf.object("options.classProperty", classProperty);
  44. Check.typeOf.object("options.bufferViews", bufferViews);
  45. //>>includeEnd('debug');
  46. const type = classProperty.type;
  47. const isArray = classProperty.isArray;
  48. const isVariableLengthArray = classProperty.isVariableLengthArray;
  49. let valueType = classProperty.valueType;
  50. const enumType = classProperty.enumType;
  51. const hasStrings = type === MetadataType.STRING;
  52. const hasBooleans = type === MetadataType.BOOLEAN;
  53. let byteLength = 0;
  54. let arrayOffsets;
  55. if (isVariableLengthArray) {
  56. // EXT_structural_metadata uses arrayOffsetType.
  57. // EXT_feature_metadata uses offsetType for both arrays and strings
  58. let arrayOffsetType = property.arrayOffsetType ?? property.offsetType;
  59. arrayOffsetType =
  60. MetadataComponentType[arrayOffsetType] ?? MetadataComponentType.UINT32;
  61. // EXT_structural_metadata uses arrayOffsets.
  62. // EXT_feature_metadata uses arrayOffsetBufferView
  63. const arrayOffsetBufferView =
  64. property.arrayOffsets ?? property.arrayOffsetBufferView;
  65. arrayOffsets = new BufferView(
  66. bufferViews[arrayOffsetBufferView],
  67. arrayOffsetType,
  68. count + 1,
  69. );
  70. byteLength += arrayOffsets.typedArray.byteLength;
  71. }
  72. const vectorComponentCount = MetadataType.getComponentCount(type);
  73. let arrayComponentCount;
  74. if (isVariableLengthArray) {
  75. arrayComponentCount = arrayOffsets.get(count) - arrayOffsets.get(0);
  76. } else if (isArray) {
  77. arrayComponentCount = count * classProperty.arrayLength;
  78. } else {
  79. arrayComponentCount = count;
  80. }
  81. const componentCount = vectorComponentCount * arrayComponentCount;
  82. let stringOffsets;
  83. if (hasStrings) {
  84. // EXT_structural_metadata uses stringOffsetType, EXT_feature_metadata uses offsetType for both arrays and strings
  85. let stringOffsetType = property.stringOffsetType ?? property.offsetType;
  86. stringOffsetType =
  87. MetadataComponentType[stringOffsetType] ?? MetadataComponentType.UINT32;
  88. // EXT_structural_metadata uses stringOffsets.
  89. // EXT_feature_metadata uses stringOffsetBufferView
  90. const stringOffsetBufferView =
  91. property.stringOffsets ?? property.stringOffsetBufferView;
  92. stringOffsets = new BufferView(
  93. bufferViews[stringOffsetBufferView],
  94. stringOffsetType,
  95. componentCount + 1,
  96. );
  97. byteLength += stringOffsets.typedArray.byteLength;
  98. }
  99. if (hasStrings || hasBooleans) {
  100. // STRING and BOOLEAN types need to be parsed differently than other types
  101. valueType = MetadataComponentType.UINT8;
  102. }
  103. let valueCount;
  104. if (hasStrings) {
  105. valueCount = stringOffsets.get(componentCount) - stringOffsets.get(0);
  106. } else if (hasBooleans) {
  107. valueCount = Math.ceil(componentCount / 8);
  108. } else {
  109. valueCount = componentCount;
  110. }
  111. // EXT_structural_metadata uses values
  112. // EXT_feature_metadata uses bufferView
  113. const valuesBufferView = property.values ?? property.bufferView;
  114. const values = new BufferView(
  115. bufferViews[valuesBufferView],
  116. valueType,
  117. valueCount,
  118. );
  119. byteLength += values.typedArray.byteLength;
  120. let offset = property.offset;
  121. let scale = property.scale;
  122. // This needs to be set before handling default values
  123. const hasValueTransform =
  124. classProperty.hasValueTransform || defined(offset) || defined(scale);
  125. // If the table does not define an offset/scale, it inherits from the
  126. // class property. The class property handles setting the default of identity:
  127. // (offset 0, scale 1) with the same array shape as the property's type
  128. // information.
  129. offset = offset ?? classProperty.offset;
  130. scale = scale ?? classProperty.scale;
  131. // Since metadata table properties are stored as packed typed
  132. // arrays, flatten the offset/scale to make it easier to apply the
  133. // transformation by iteration.
  134. offset = flatten(offset);
  135. scale = flatten(scale);
  136. let getValueFunction;
  137. let setValueFunction;
  138. const that = this;
  139. if (hasStrings) {
  140. getValueFunction = function (index) {
  141. return getString(index, that._values, that._stringOffsets);
  142. };
  143. } else if (hasBooleans) {
  144. getValueFunction = function (index) {
  145. return getBoolean(index, that._values);
  146. };
  147. setValueFunction = function (index, value) {
  148. setBoolean(index, that._values, value);
  149. };
  150. } else if (defined(enumType)) {
  151. getValueFunction = function (index) {
  152. const integer = that._values.get(index);
  153. return enumType.namesByValue[integer];
  154. };
  155. setValueFunction = function (index, value) {
  156. const integer = enumType.valuesByName[value];
  157. that._values.set(index, integer);
  158. };
  159. } else {
  160. getValueFunction = function (index) {
  161. return that._values.get(index);
  162. };
  163. setValueFunction = function (index, value) {
  164. that._values.set(index, value);
  165. };
  166. }
  167. this._arrayOffsets = arrayOffsets;
  168. this._stringOffsets = stringOffsets;
  169. this._values = values;
  170. this._classProperty = classProperty;
  171. this._count = count;
  172. this._vectorComponentCount = vectorComponentCount;
  173. this._min = property.min;
  174. this._max = property.max;
  175. this._offset = offset;
  176. this._scale = scale;
  177. this._hasValueTransform = hasValueTransform;
  178. this._getValue = getValueFunction;
  179. this._setValue = setValueFunction;
  180. this._unpackedValues = undefined;
  181. this._extras = property.extras;
  182. this._extensions = property.extensions;
  183. this._byteLength = byteLength;
  184. }
  185. Object.defineProperties(MetadataTableProperty.prototype, {
  186. /**
  187. * True if offset/scale should be applied. If both offset/scale were
  188. * undefined, they default to identity so this property is set false
  189. *
  190. * @memberof MetadataClassProperty.prototype
  191. * @type {boolean}
  192. * @readonly
  193. * @private
  194. */
  195. hasValueTransform: {
  196. get: function () {
  197. return this._hasValueTransform;
  198. },
  199. },
  200. /**
  201. * The offset to be added to property values as part of the value transform.
  202. *
  203. * @memberof MetadataClassProperty.prototype
  204. * @type {number|number[]|number[][]}
  205. * @readonly
  206. * @private
  207. */
  208. offset: {
  209. get: function () {
  210. return this._offset;
  211. },
  212. },
  213. /**
  214. * The scale to be multiplied to property values as part of the value transform.
  215. *
  216. * @memberof MetadataClassProperty.prototype
  217. * @type {number|number[]|number[][]}
  218. * @readonly
  219. * @private
  220. */
  221. scale: {
  222. get: function () {
  223. return this._scale;
  224. },
  225. },
  226. /**
  227. * Extra user-defined properties.
  228. *
  229. * @memberof MetadataTableProperty.prototype
  230. * @type {*}
  231. * @readonly
  232. * @private
  233. */
  234. extras: {
  235. get: function () {
  236. return this._extras;
  237. },
  238. },
  239. /**
  240. * An object containing extensions.
  241. *
  242. * @memberof MetadataTableProperty.prototype
  243. * @type {*}
  244. * @readonly
  245. * @private
  246. */
  247. extensions: {
  248. get: function () {
  249. return this._extensions;
  250. },
  251. },
  252. /**
  253. * Size of all typed arrays used by this table property
  254. *
  255. * @memberof MetadataTableProperty.prototype
  256. * @type {Normal}
  257. * @readonly
  258. * @private
  259. */
  260. byteLength: {
  261. get: function () {
  262. return this._byteLength;
  263. },
  264. },
  265. /**
  266. * The class property that this table property corresponds to.
  267. *
  268. * @memberof MetadataTableProperty.prototype
  269. * @type {MetadataClassProperty}
  270. * @readonly
  271. * @private
  272. */
  273. classProperty: {
  274. get: function () {
  275. return this._classProperty;
  276. },
  277. },
  278. });
  279. /**
  280. * Returns a copy of the value at the given index.
  281. *
  282. * @param {number} index The index.
  283. * @returns {*} The value of the property.
  284. *
  285. * @private
  286. */
  287. MetadataTableProperty.prototype.get = function (index) {
  288. //>>includeStart('debug', pragmas.debug);
  289. checkIndex(this, index);
  290. //>>includeEnd('debug');
  291. let value = get(this, index);
  292. // handle noData and default
  293. value = this._classProperty.handleNoData(value);
  294. if (!defined(value)) {
  295. value = this._classProperty.default;
  296. return this._classProperty.unpackVectorAndMatrixTypes(value);
  297. }
  298. value = this._classProperty.normalize(value);
  299. value = applyValueTransform(this, value);
  300. return this._classProperty.unpackVectorAndMatrixTypes(value);
  301. };
  302. /**
  303. * Sets the value at the given index.
  304. *
  305. * @param {number} index The index.
  306. * @param {*} value The value of the property.
  307. *
  308. * @private
  309. */
  310. MetadataTableProperty.prototype.set = function (index, value) {
  311. const classProperty = this._classProperty;
  312. //>>includeStart('debug', pragmas.debug);
  313. Check.defined("value", value);
  314. checkIndex(this, index);
  315. const errorMessage = classProperty.validate(value);
  316. if (defined(errorMessage)) {
  317. throw new DeveloperError(errorMessage);
  318. }
  319. //>>includeEnd('debug');
  320. value = classProperty.packVectorAndMatrixTypes(value);
  321. value = unapplyValueTransform(this, value);
  322. value = classProperty.unnormalize(value);
  323. set(this, index, value);
  324. };
  325. /**
  326. * Returns a typed array containing the property values.
  327. *
  328. * @returns {*} The typed array containing the property values or <code>undefined</code> if the property values are not stored in a typed array.
  329. *
  330. * @private
  331. */
  332. MetadataTableProperty.prototype.getTypedArray = function () {
  333. // Note: depending on the class definition some properties are unpacked to
  334. // JS arrays when first accessed and values will be undefined. Generally not
  335. // a concern for fixed-length arrays of numbers.
  336. if (defined(this._values)) {
  337. return this._values.typedArray;
  338. }
  339. return undefined;
  340. };
  341. function flatten(values) {
  342. if (!Array.isArray(values)) {
  343. return values;
  344. }
  345. const result = [];
  346. for (let i = 0; i < values.length; i++) {
  347. const value = values[i];
  348. if (Array.isArray(value)) {
  349. addAllToArray(result, value);
  350. } else {
  351. result.push(value);
  352. }
  353. }
  354. return result;
  355. }
  356. function checkIndex(table, index) {
  357. const count = table._count;
  358. if (!defined(index) || index < 0 || index >= count) {
  359. const maximumIndex = count - 1;
  360. throw new DeveloperError(
  361. `index is required and between zero and count - 1. Actual value: ${maximumIndex}`,
  362. );
  363. }
  364. }
  365. function get(property, index) {
  366. if (requiresUnpackForGet(property)) {
  367. unpackProperty(property);
  368. }
  369. const classProperty = property._classProperty;
  370. const isArray = classProperty.isArray;
  371. const type = classProperty.type;
  372. const componentCount = MetadataType.getComponentCount(type);
  373. if (defined(property._unpackedValues)) {
  374. const value = property._unpackedValues[index];
  375. if (isArray) {
  376. return clone(value, true);
  377. }
  378. return value;
  379. }
  380. // handle single values
  381. if (!isArray && componentCount === 1) {
  382. return property._getValue(index);
  383. }
  384. return getArrayValues(property, classProperty, index);
  385. }
  386. function getArrayValues(property, classProperty, index) {
  387. let offset;
  388. let length;
  389. if (classProperty.isVariableLengthArray) {
  390. offset = property._arrayOffsets.get(index);
  391. length = property._arrayOffsets.get(index + 1) - offset;
  392. // for vectors and matrices, the offset and length need to be multiplied
  393. // by the component count
  394. const componentCount = MetadataType.getComponentCount(classProperty.type);
  395. offset *= componentCount;
  396. length *= componentCount;
  397. } else {
  398. const arrayLength = classProperty.arrayLength ?? 1;
  399. const componentCount = arrayLength * property._vectorComponentCount;
  400. offset = index * componentCount;
  401. length = componentCount;
  402. }
  403. const values = new Array(length);
  404. for (let i = 0; i < length; i++) {
  405. values[i] = property._getValue(offset + i);
  406. }
  407. return values;
  408. }
  409. function set(property, index, value) {
  410. if (requiresUnpackForSet(property, index, value)) {
  411. unpackProperty(property);
  412. }
  413. const classProperty = property._classProperty;
  414. const isArray = classProperty.isArray;
  415. const type = classProperty.type;
  416. const componentCount = MetadataType.getComponentCount(type);
  417. if (defined(property._unpackedValues)) {
  418. if (classProperty.isArray) {
  419. value = clone(value, true);
  420. }
  421. property._unpackedValues[index] = value;
  422. return;
  423. }
  424. // Values are unpacked if the length of a variable-size array changes or the
  425. // property has strings. No need to handle these cases below.
  426. // Handle single values
  427. if (!isArray && componentCount === 1) {
  428. property._setValue(index, value);
  429. return;
  430. }
  431. let offset;
  432. let length;
  433. if (classProperty.isVariableLengthArray) {
  434. offset = property._arrayOffsets.get(index);
  435. length = property._arrayOffsets.get(index + 1) - offset;
  436. } else {
  437. const arrayLength = classProperty.arrayLength ?? 1;
  438. const componentCount = arrayLength * property._vectorComponentCount;
  439. offset = index * componentCount;
  440. length = componentCount;
  441. }
  442. for (let i = 0; i < length; ++i) {
  443. property._setValue(offset + i, value[i]);
  444. }
  445. }
  446. function getString(index, values, stringOffsets) {
  447. const stringByteOffset = stringOffsets.get(index);
  448. const stringByteLength = stringOffsets.get(index + 1) - stringByteOffset;
  449. return getStringFromTypedArray(
  450. values.typedArray,
  451. stringByteOffset,
  452. stringByteLength,
  453. );
  454. }
  455. function getBoolean(index, values) {
  456. // byteIndex is floor(index / 8)
  457. const byteIndex = index >> 3;
  458. const bitIndex = index % 8;
  459. return ((values.typedArray[byteIndex] >> bitIndex) & 1) === 1;
  460. }
  461. function setBoolean(index, values, value) {
  462. // byteIndex is floor(index / 8)
  463. const byteIndex = index >> 3;
  464. const bitIndex = index % 8;
  465. if (value) {
  466. values.typedArray[byteIndex] |= 1 << bitIndex;
  467. } else {
  468. values.typedArray[byteIndex] &= ~(1 << bitIndex);
  469. }
  470. }
  471. function getInt64NumberFallback(index, values) {
  472. const dataView = values.dataView;
  473. const byteOffset = index * 8;
  474. let value = 0;
  475. const isNegative = (dataView.getUint8(byteOffset + 7) & 0x80) > 0;
  476. let carrying = true;
  477. for (let i = 0; i < 8; ++i) {
  478. let byte = dataView.getUint8(byteOffset + i);
  479. if (isNegative) {
  480. if (carrying) {
  481. if (byte !== 0x00) {
  482. byte = ~(byte - 1) & 0xff;
  483. carrying = false;
  484. }
  485. } else {
  486. byte = ~byte & 0xff;
  487. }
  488. }
  489. value += byte * Math.pow(256, i);
  490. }
  491. if (isNegative) {
  492. value = -value;
  493. }
  494. return value;
  495. }
  496. function getInt64BigIntFallback(index, values) {
  497. const dataView = values.dataView;
  498. const byteOffset = index * 8;
  499. let value = BigInt(0);
  500. const isNegative = (dataView.getUint8(byteOffset + 7) & 0x80) > 0;
  501. let carrying = true;
  502. for (let i = 0; i < 8; ++i) {
  503. let byte = dataView.getUint8(byteOffset + i);
  504. if (isNegative) {
  505. if (carrying) {
  506. if (byte !== 0x00) {
  507. byte = ~(byte - 1) & 0xff;
  508. carrying = false;
  509. }
  510. } else {
  511. byte = ~byte & 0xff;
  512. }
  513. }
  514. value += BigInt(byte) * (BigInt(1) << BigInt(i * 8));
  515. }
  516. if (isNegative) {
  517. value = -value;
  518. }
  519. return value;
  520. }
  521. function getUint64NumberFallback(index, values) {
  522. const dataView = values.dataView;
  523. const byteOffset = index * 8;
  524. // Split 64-bit number into two 32-bit (4-byte) parts
  525. const left = dataView.getUint32(byteOffset, true);
  526. const right = dataView.getUint32(byteOffset + 4, true);
  527. // Combine the two 32-bit values
  528. const value = left + 4294967296 * right;
  529. return value;
  530. }
  531. function getUint64BigIntFallback(index, values) {
  532. const dataView = values.dataView;
  533. const byteOffset = index * 8;
  534. // Split 64-bit number into two 32-bit (4-byte) parts
  535. const left = BigInt(dataView.getUint32(byteOffset, true));
  536. const right = BigInt(dataView.getUint32(byteOffset + 4, true));
  537. // Combine the two 32-bit values
  538. const value = left + BigInt(4294967296) * right;
  539. return value;
  540. }
  541. function getComponentDatatype(componentType) {
  542. switch (componentType) {
  543. case MetadataComponentType.INT8:
  544. return ComponentDatatype.BYTE;
  545. case MetadataComponentType.UINT8:
  546. return ComponentDatatype.UNSIGNED_BYTE;
  547. case MetadataComponentType.INT16:
  548. return ComponentDatatype.SHORT;
  549. case MetadataComponentType.UINT16:
  550. return ComponentDatatype.UNSIGNED_SHORT;
  551. case MetadataComponentType.INT32:
  552. return ComponentDatatype.INT;
  553. case MetadataComponentType.UINT32:
  554. return ComponentDatatype.UNSIGNED_INT;
  555. case MetadataComponentType.FLOAT32:
  556. return ComponentDatatype.FLOAT;
  557. case MetadataComponentType.FLOAT64:
  558. return ComponentDatatype.DOUBLE;
  559. }
  560. }
  561. function requiresUnpackForGet(property) {
  562. if (defined(property._unpackedValues)) {
  563. return false;
  564. }
  565. const classProperty = property._classProperty;
  566. const type = classProperty.type;
  567. const valueType = classProperty.valueType;
  568. if (type === MetadataType.STRING) {
  569. // Unpack since UTF-8 decoding is expensive
  570. return true;
  571. }
  572. if (
  573. valueType === MetadataComponentType.INT64 &&
  574. !FeatureDetection.supportsBigInt64Array()
  575. ) {
  576. // Unpack since the fallback INT64 getters are expensive
  577. return true;
  578. }
  579. if (
  580. valueType === MetadataComponentType.UINT64 &&
  581. !FeatureDetection.supportsBigUint64Array()
  582. ) {
  583. // Unpack since the fallback UINT64 getters are expensive
  584. return true;
  585. }
  586. return false;
  587. }
  588. function requiresUnpackForSet(property, index, value) {
  589. if (requiresUnpackForGet(property)) {
  590. return true;
  591. }
  592. const arrayOffsets = property._arrayOffsets;
  593. if (defined(arrayOffsets)) {
  594. // Unpacking is required if a variable-size array changes length since it
  595. // would be expensive to repack the binary data
  596. const oldLength = arrayOffsets.get(index + 1) - arrayOffsets.get(index);
  597. const newLength = value.length;
  598. if (oldLength !== newLength) {
  599. return true;
  600. }
  601. }
  602. return false;
  603. }
  604. function unpackProperty(property) {
  605. property._unpackedValues = unpackValues(property);
  606. // Free memory
  607. property._arrayOffsets = undefined;
  608. property._stringOffsets = undefined;
  609. property._values = undefined;
  610. }
  611. function unpackValues(property) {
  612. const count = property._count;
  613. const unpackedValues = new Array(count);
  614. const classProperty = property._classProperty;
  615. const isArray = classProperty.isArray;
  616. const type = classProperty.type;
  617. const componentCount = MetadataType.getComponentCount(type);
  618. // Handle single values
  619. if (!isArray && componentCount === 1) {
  620. for (let i = 0; i < count; ++i) {
  621. unpackedValues[i] = property._getValue(i);
  622. }
  623. return unpackedValues;
  624. }
  625. for (let i = 0; i < count; i++) {
  626. unpackedValues[i] = getArrayValues(property, classProperty, i);
  627. }
  628. return unpackedValues;
  629. }
  630. function applyValueTransform(property, value) {
  631. const classProperty = property._classProperty;
  632. const isVariableLengthArray = classProperty.isVariableLengthArray;
  633. if (!property._hasValueTransform || isVariableLengthArray) {
  634. return value;
  635. }
  636. return MetadataClassProperty.valueTransformInPlace(
  637. value,
  638. property._offset,
  639. property._scale,
  640. MetadataComponentType.applyValueTransform,
  641. );
  642. }
  643. function unapplyValueTransform(property, value) {
  644. const classProperty = property._classProperty;
  645. const isVariableLengthArray = classProperty.isVariableLengthArray;
  646. if (!property._hasValueTransform || isVariableLengthArray) {
  647. return value;
  648. }
  649. return MetadataClassProperty.valueTransformInPlace(
  650. value,
  651. property._offset,
  652. property._scale,
  653. MetadataComponentType.unapplyValueTransform,
  654. );
  655. }
  656. function BufferView(bufferView, componentType, length) {
  657. const that = this;
  658. let typedArray;
  659. let getFunction;
  660. let setFunction;
  661. if (componentType === MetadataComponentType.INT64) {
  662. if (!FeatureDetection.supportsBigInt()) {
  663. oneTimeWarning(
  664. "INT64 type is not fully supported on this platform. Values greater than 2^53 - 1 or less than -(2^53 - 1) may lose precision when read.",
  665. );
  666. typedArray = new Uint8Array(
  667. bufferView.buffer,
  668. bufferView.byteOffset,
  669. length * 8,
  670. );
  671. getFunction = function (index) {
  672. return getInt64NumberFallback(index, that);
  673. };
  674. } else if (!FeatureDetection.supportsBigInt64Array()) {
  675. typedArray = new Uint8Array(
  676. bufferView.buffer,
  677. bufferView.byteOffset,
  678. length * 8,
  679. );
  680. getFunction = function (index) {
  681. return getInt64BigIntFallback(index, that);
  682. };
  683. } else {
  684. typedArray = new BigInt64Array(
  685. bufferView.buffer,
  686. bufferView.byteOffset,
  687. length,
  688. );
  689. setFunction = function (index, value) {
  690. // Convert the number to a BigInt before setting the value in the typed array
  691. that.typedArray[index] = BigInt(value);
  692. };
  693. }
  694. } else if (componentType === MetadataComponentType.UINT64) {
  695. if (!FeatureDetection.supportsBigInt()) {
  696. oneTimeWarning(
  697. "UINT64 type is not fully supported on this platform. Values greater than 2^53 - 1 may lose precision when read.",
  698. );
  699. typedArray = new Uint8Array(
  700. bufferView.buffer,
  701. bufferView.byteOffset,
  702. length * 8,
  703. );
  704. getFunction = function (index) {
  705. return getUint64NumberFallback(index, that);
  706. };
  707. } else if (!FeatureDetection.supportsBigUint64Array()) {
  708. typedArray = new Uint8Array(
  709. bufferView.buffer,
  710. bufferView.byteOffset,
  711. length * 8,
  712. );
  713. getFunction = function (index) {
  714. return getUint64BigIntFallback(index, that);
  715. };
  716. } else {
  717. typedArray = new BigUint64Array(
  718. bufferView.buffer,
  719. bufferView.byteOffset,
  720. length,
  721. );
  722. setFunction = function (index, value) {
  723. // Convert the number to a BigInt before setting the value in the typed array
  724. that.typedArray[index] = BigInt(value);
  725. };
  726. }
  727. } else {
  728. const componentDatatype = getComponentDatatype(componentType);
  729. typedArray = ComponentDatatype.createArrayBufferView(
  730. componentDatatype,
  731. bufferView.buffer,
  732. bufferView.byteOffset,
  733. length,
  734. );
  735. setFunction = function (index, value) {
  736. that.typedArray[index] = value;
  737. };
  738. }
  739. if (!defined(getFunction)) {
  740. getFunction = function (index) {
  741. return that.typedArray[index];
  742. };
  743. }
  744. this.typedArray = typedArray;
  745. this.dataView = new DataView(typedArray.buffer, typedArray.byteOffset);
  746. this.get = getFunction;
  747. this.set = setFunction;
  748. // for unit testing
  749. this._componentType = componentType;
  750. }
  751. export default MetadataTableProperty;