MetadataTableProperty.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import ComponentDatatype from "../Core/ComponentDatatype.js";
  4. import defaultValue from "../Core/defaultValue.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. /**
  14. * A binary property in a {@MetadataTable}
  15. * <p>
  16. * For 3D Tiles Next details, see the {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_metadata|3DTILES_metadata Extension}
  17. * 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}
  18. * 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}
  19. * </p>
  20. *
  21. * @param {object} options Object with the following properties:
  22. * @param {number} options.count The number of elements in each property array.
  23. * @param {object} options.property The property JSON object.
  24. * @param {MetadataClassProperty} options.classProperty The class property.
  25. * @param {Object<string, Uint8Array>} options.bufferViews An object mapping bufferView IDs to Uint8Array objects.
  26. *
  27. * @alias MetadataTableProperty
  28. * @constructor
  29. *
  30. * @private
  31. * @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.
  32. */
  33. function MetadataTableProperty(options) {
  34. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  35. const count = options.count;
  36. const property = options.property;
  37. const classProperty = options.classProperty;
  38. const bufferViews = options.bufferViews;
  39. //>>includeStart('debug', pragmas.debug);
  40. Check.typeOf.number.greaterThan("options.count", count, 0);
  41. Check.typeOf.object("options.property", property);
  42. Check.typeOf.object("options.classProperty", classProperty);
  43. Check.typeOf.object("options.bufferViews", bufferViews);
  44. //>>includeEnd('debug');
  45. const type = classProperty.type;
  46. const isArray = classProperty.isArray;
  47. const isVariableLengthArray = classProperty.isVariableLengthArray;
  48. let valueType = classProperty.valueType;
  49. const enumType = classProperty.enumType;
  50. const hasStrings = type === MetadataType.STRING;
  51. const hasBooleans = type === MetadataType.BOOLEAN;
  52. let byteLength = 0;
  53. let arrayOffsets;
  54. if (isVariableLengthArray) {
  55. // EXT_structural_metadata uses arrayOffsetType.
  56. // EXT_feature_metadata uses offsetType for both arrays and strings
  57. let arrayOffsetType = defaultValue(
  58. property.arrayOffsetType,
  59. property.offsetType
  60. );
  61. arrayOffsetType = defaultValue(
  62. MetadataComponentType[arrayOffsetType],
  63. MetadataComponentType.UINT32
  64. );
  65. // EXT_structural_metadata uses arrayOffsets.
  66. // EXT_feature_metadata uses arrayOffsetBufferView
  67. const arrayOffsetBufferView = defaultValue(
  68. property.arrayOffsets,
  69. property.arrayOffsetBufferView
  70. );
  71. arrayOffsets = new BufferView(
  72. bufferViews[arrayOffsetBufferView],
  73. arrayOffsetType,
  74. count + 1
  75. );
  76. byteLength += arrayOffsets.typedArray.byteLength;
  77. }
  78. const vectorComponentCount = MetadataType.getComponentCount(type);
  79. let arrayComponentCount;
  80. if (isVariableLengthArray) {
  81. arrayComponentCount = arrayOffsets.get(count) - arrayOffsets.get(0);
  82. } else if (isArray) {
  83. arrayComponentCount = count * classProperty.arrayLength;
  84. } else {
  85. arrayComponentCount = count;
  86. }
  87. const componentCount = vectorComponentCount * arrayComponentCount;
  88. let stringOffsets;
  89. if (hasStrings) {
  90. // EXT_structural_metadata uses stringOffsetType, EXT_feature_metadata uses offsetType for both arrays and strings
  91. let stringOffsetType = defaultValue(
  92. property.stringOffsetType,
  93. property.offsetType
  94. );
  95. stringOffsetType = defaultValue(
  96. MetadataComponentType[stringOffsetType],
  97. MetadataComponentType.UINT32
  98. );
  99. // EXT_structural_metadata uses stringOffsets.
  100. // EXT_feature_metadata uses stringOffsetBufferView
  101. const stringOffsetBufferView = defaultValue(
  102. property.stringOffsets,
  103. property.stringOffsetBufferView
  104. );
  105. stringOffsets = new BufferView(
  106. bufferViews[stringOffsetBufferView],
  107. stringOffsetType,
  108. componentCount + 1
  109. );
  110. byteLength += stringOffsets.typedArray.byteLength;
  111. }
  112. if (hasStrings || hasBooleans) {
  113. // STRING and BOOLEAN types need to be parsed differently than other types
  114. valueType = MetadataComponentType.UINT8;
  115. }
  116. let valueCount;
  117. if (hasStrings) {
  118. valueCount = stringOffsets.get(componentCount) - stringOffsets.get(0);
  119. } else if (hasBooleans) {
  120. valueCount = Math.ceil(componentCount / 8);
  121. } else {
  122. valueCount = componentCount;
  123. }
  124. // EXT_structural_metadata uses values
  125. // EXT_feature_metadata uses bufferView
  126. const valuesBufferView = defaultValue(property.values, property.bufferView);
  127. const values = new BufferView(
  128. bufferViews[valuesBufferView],
  129. valueType,
  130. valueCount
  131. );
  132. byteLength += values.typedArray.byteLength;
  133. let offset = property.offset;
  134. let scale = property.scale;
  135. // This needs to be set before handling default values
  136. const hasValueTransform =
  137. classProperty.hasValueTransform || defined(offset) || defined(scale);
  138. // If the table does not define an offset/scale, it inherits from the
  139. // class property. The class property handles setting the default of identity:
  140. // (offset 0, scale 1) with the same array shape as the property's type
  141. // information.
  142. offset = defaultValue(offset, classProperty.offset);
  143. scale = defaultValue(scale, classProperty.scale);
  144. // Since metadata table properties are stored as packed typed
  145. // arrays, flatten the offset/scale to make it easier to apply the
  146. // transformation by iteration.
  147. offset = flatten(offset);
  148. scale = flatten(scale);
  149. let getValueFunction;
  150. let setValueFunction;
  151. const that = this;
  152. if (hasStrings) {
  153. getValueFunction = function (index) {
  154. return getString(index, that._values, that._stringOffsets);
  155. };
  156. } else if (hasBooleans) {
  157. getValueFunction = function (index) {
  158. return getBoolean(index, that._values);
  159. };
  160. setValueFunction = function (index, value) {
  161. setBoolean(index, that._values, value);
  162. };
  163. } else if (defined(enumType)) {
  164. getValueFunction = function (index) {
  165. const integer = that._values.get(index);
  166. return enumType.namesByValue[integer];
  167. };
  168. setValueFunction = function (index, value) {
  169. const integer = enumType.valuesByName[value];
  170. that._values.set(index, integer);
  171. };
  172. } else {
  173. getValueFunction = function (index) {
  174. return that._values.get(index);
  175. };
  176. setValueFunction = function (index, value) {
  177. that._values.set(index, value);
  178. };
  179. }
  180. this._arrayOffsets = arrayOffsets;
  181. this._stringOffsets = stringOffsets;
  182. this._values = values;
  183. this._classProperty = classProperty;
  184. this._count = count;
  185. this._vectorComponentCount = vectorComponentCount;
  186. this._min = property.min;
  187. this._max = property.max;
  188. this._offset = offset;
  189. this._scale = scale;
  190. this._hasValueTransform = hasValueTransform;
  191. this._getValue = getValueFunction;
  192. this._setValue = setValueFunction;
  193. this._unpackedValues = undefined;
  194. this._extras = property.extras;
  195. this._extensions = property.extensions;
  196. this._byteLength = byteLength;
  197. }
  198. Object.defineProperties(MetadataTableProperty.prototype, {
  199. /**
  200. * True if offset/scale should be applied. If both offset/scale were
  201. * undefined, they default to identity so this property is set false
  202. *
  203. * @memberof MetadataClassProperty.prototype
  204. * @type {boolean}
  205. * @readonly
  206. * @private
  207. */
  208. hasValueTransform: {
  209. get: function () {
  210. return this._hasValueTransform;
  211. },
  212. },
  213. /**
  214. * The offset to be added 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. offset: {
  222. get: function () {
  223. return this._offset;
  224. },
  225. },
  226. /**
  227. * The scale to be multiplied to property values as part of the value transform.
  228. *
  229. * @memberof MetadataClassProperty.prototype
  230. * @type {number|number[]|number[][]}
  231. * @readonly
  232. * @private
  233. */
  234. scale: {
  235. get: function () {
  236. return this._scale;
  237. },
  238. },
  239. /**
  240. * Extra user-defined properties.
  241. *
  242. * @memberof MetadataTableProperty.prototype
  243. * @type {*}
  244. * @readonly
  245. * @private
  246. */
  247. extras: {
  248. get: function () {
  249. return this._extras;
  250. },
  251. },
  252. /**
  253. * An object containing extensions.
  254. *
  255. * @memberof MetadataTableProperty.prototype
  256. * @type {*}
  257. * @readonly
  258. * @private
  259. */
  260. extensions: {
  261. get: function () {
  262. return this._extensions;
  263. },
  264. },
  265. /**
  266. * Size of all typed arrays used by this table property
  267. *
  268. * @memberof MetadataTableProperty.prototype
  269. * @type {Normal}
  270. * @readonly
  271. * @private
  272. */
  273. byteLength: {
  274. get: function () {
  275. return this._byteLength;
  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. result.push.apply(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 = defaultValue(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 = defaultValue(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. // eslint-disable-next-line no-undef
  500. let value = BigInt(0);
  501. const isNegative = (dataView.getUint8(byteOffset + 7) & 0x80) > 0;
  502. let carrying = true;
  503. for (let i = 0; i < 8; ++i) {
  504. let byte = dataView.getUint8(byteOffset + i);
  505. if (isNegative) {
  506. if (carrying) {
  507. if (byte !== 0x00) {
  508. byte = ~(byte - 1) & 0xff;
  509. carrying = false;
  510. }
  511. } else {
  512. byte = ~byte & 0xff;
  513. }
  514. }
  515. value += BigInt(byte) * (BigInt(1) << BigInt(i * 8)); // eslint-disable-line
  516. }
  517. if (isNegative) {
  518. value = -value;
  519. }
  520. return value;
  521. }
  522. function getUint64NumberFallback(index, values) {
  523. const dataView = values.dataView;
  524. const byteOffset = index * 8;
  525. // Split 64-bit number into two 32-bit (4-byte) parts
  526. const left = dataView.getUint32(byteOffset, true);
  527. const right = dataView.getUint32(byteOffset + 4, true);
  528. // Combine the two 32-bit values
  529. const value = left + 4294967296 * right;
  530. return value;
  531. }
  532. function getUint64BigIntFallback(index, values) {
  533. const dataView = values.dataView;
  534. const byteOffset = index * 8;
  535. // Split 64-bit number into two 32-bit (4-byte) parts
  536. // eslint-disable-next-line no-undef
  537. const left = BigInt(dataView.getUint32(byteOffset, true));
  538. // eslint-disable-next-line no-undef
  539. const right = BigInt(dataView.getUint32(byteOffset + 4, true));
  540. // Combine the two 32-bit values
  541. // eslint-disable-next-line no-undef
  542. const value = left + BigInt(4294967296) * right;
  543. return value;
  544. }
  545. function getComponentDatatype(componentType) {
  546. switch (componentType) {
  547. case MetadataComponentType.INT8:
  548. return ComponentDatatype.BYTE;
  549. case MetadataComponentType.UINT8:
  550. return ComponentDatatype.UNSIGNED_BYTE;
  551. case MetadataComponentType.INT16:
  552. return ComponentDatatype.SHORT;
  553. case MetadataComponentType.UINT16:
  554. return ComponentDatatype.UNSIGNED_SHORT;
  555. case MetadataComponentType.INT32:
  556. return ComponentDatatype.INT;
  557. case MetadataComponentType.UINT32:
  558. return ComponentDatatype.UNSIGNED_INT;
  559. case MetadataComponentType.FLOAT32:
  560. return ComponentDatatype.FLOAT;
  561. case MetadataComponentType.FLOAT64:
  562. return ComponentDatatype.DOUBLE;
  563. }
  564. }
  565. function requiresUnpackForGet(property) {
  566. if (defined(property._unpackedValues)) {
  567. return false;
  568. }
  569. const classProperty = property._classProperty;
  570. const type = classProperty.type;
  571. const valueType = classProperty.valueType;
  572. if (type === MetadataType.STRING) {
  573. // Unpack since UTF-8 decoding is expensive
  574. return true;
  575. }
  576. if (
  577. valueType === MetadataComponentType.INT64 &&
  578. !FeatureDetection.supportsBigInt64Array()
  579. ) {
  580. // Unpack since the fallback INT64 getters are expensive
  581. return true;
  582. }
  583. if (
  584. valueType === MetadataComponentType.UINT64 &&
  585. !FeatureDetection.supportsBigUint64Array()
  586. ) {
  587. // Unpack since the fallback UINT64 getters are expensive
  588. return true;
  589. }
  590. return false;
  591. }
  592. function requiresUnpackForSet(property, index, value) {
  593. if (requiresUnpackForGet(property)) {
  594. return true;
  595. }
  596. const arrayOffsets = property._arrayOffsets;
  597. if (defined(arrayOffsets)) {
  598. // Unpacking is required if a variable-size array changes length since it
  599. // would be expensive to repack the binary data
  600. const oldLength = arrayOffsets.get(index + 1) - arrayOffsets.get(index);
  601. const newLength = value.length;
  602. if (oldLength !== newLength) {
  603. return true;
  604. }
  605. }
  606. return false;
  607. }
  608. function unpackProperty(property) {
  609. property._unpackedValues = unpackValues(property);
  610. // Free memory
  611. property._arrayOffsets = undefined;
  612. property._stringOffsets = undefined;
  613. property._values = undefined;
  614. }
  615. function unpackValues(property) {
  616. const count = property._count;
  617. const unpackedValues = new Array(count);
  618. const classProperty = property._classProperty;
  619. const isArray = classProperty.isArray;
  620. const type = classProperty.type;
  621. const componentCount = MetadataType.getComponentCount(type);
  622. // Handle single values
  623. if (!isArray && componentCount === 1) {
  624. for (let i = 0; i < count; ++i) {
  625. unpackedValues[i] = property._getValue(i);
  626. }
  627. return unpackedValues;
  628. }
  629. for (let i = 0; i < count; i++) {
  630. unpackedValues[i] = getArrayValues(property, classProperty, i);
  631. }
  632. return unpackedValues;
  633. }
  634. function applyValueTransform(property, value) {
  635. const classProperty = property._classProperty;
  636. const isVariableLengthArray = classProperty.isVariableLengthArray;
  637. if (!property._hasValueTransform || isVariableLengthArray) {
  638. return value;
  639. }
  640. return MetadataClassProperty.valueTransformInPlace(
  641. value,
  642. property._offset,
  643. property._scale,
  644. MetadataComponentType.applyValueTransform
  645. );
  646. }
  647. function unapplyValueTransform(property, value) {
  648. const classProperty = property._classProperty;
  649. const isVariableLengthArray = classProperty.isVariableLengthArray;
  650. if (!property._hasValueTransform || isVariableLengthArray) {
  651. return value;
  652. }
  653. return MetadataClassProperty.valueTransformInPlace(
  654. value,
  655. property._offset,
  656. property._scale,
  657. MetadataComponentType.unapplyValueTransform
  658. );
  659. }
  660. function BufferView(bufferView, componentType, length) {
  661. const that = this;
  662. let typedArray;
  663. let getFunction;
  664. let setFunction;
  665. if (componentType === MetadataComponentType.INT64) {
  666. if (!FeatureDetection.supportsBigInt()) {
  667. oneTimeWarning(
  668. "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."
  669. );
  670. typedArray = new Uint8Array(
  671. bufferView.buffer,
  672. bufferView.byteOffset,
  673. length * 8
  674. );
  675. getFunction = function (index) {
  676. return getInt64NumberFallback(index, that);
  677. };
  678. } else if (!FeatureDetection.supportsBigInt64Array()) {
  679. typedArray = new Uint8Array(
  680. bufferView.buffer,
  681. bufferView.byteOffset,
  682. length * 8
  683. );
  684. getFunction = function (index) {
  685. return getInt64BigIntFallback(index, that);
  686. };
  687. } else {
  688. // eslint-disable-next-line
  689. typedArray = new BigInt64Array(
  690. bufferView.buffer,
  691. bufferView.byteOffset,
  692. length
  693. );
  694. setFunction = function (index, value) {
  695. // Convert the number to a BigInt before setting the value in the typed array
  696. that.typedArray[index] = BigInt(value); // eslint-disable-line
  697. };
  698. }
  699. } else if (componentType === MetadataComponentType.UINT64) {
  700. if (!FeatureDetection.supportsBigInt()) {
  701. oneTimeWarning(
  702. "UINT64 type is not fully supported on this platform. Values greater than 2^53 - 1 may lose precision when read."
  703. );
  704. typedArray = new Uint8Array(
  705. bufferView.buffer,
  706. bufferView.byteOffset,
  707. length * 8
  708. );
  709. getFunction = function (index) {
  710. return getUint64NumberFallback(index, that);
  711. };
  712. } else if (!FeatureDetection.supportsBigUint64Array()) {
  713. typedArray = new Uint8Array(
  714. bufferView.buffer,
  715. bufferView.byteOffset,
  716. length * 8
  717. );
  718. getFunction = function (index) {
  719. return getUint64BigIntFallback(index, that);
  720. };
  721. } else {
  722. // eslint-disable-next-line
  723. typedArray = new BigUint64Array(
  724. bufferView.buffer,
  725. bufferView.byteOffset,
  726. length
  727. );
  728. setFunction = function (index, value) {
  729. // Convert the number to a BigInt before setting the value in the typed array
  730. that.typedArray[index] = BigInt(value); // eslint-disable-line
  731. };
  732. }
  733. } else {
  734. const componentDatatype = getComponentDatatype(componentType);
  735. typedArray = ComponentDatatype.createArrayBufferView(
  736. componentDatatype,
  737. bufferView.buffer,
  738. bufferView.byteOffset,
  739. length
  740. );
  741. setFunction = function (index, value) {
  742. that.typedArray[index] = value;
  743. };
  744. }
  745. if (!defined(getFunction)) {
  746. getFunction = function (index) {
  747. return that.typedArray[index];
  748. };
  749. }
  750. this.typedArray = typedArray;
  751. this.dataView = new DataView(typedArray.buffer, typedArray.byteOffset);
  752. this.get = getFunction;
  753. this.set = setFunction;
  754. // for unit testing
  755. this._componentType = componentType;
  756. }
  757. export default MetadataTableProperty;