All files / engine/Source/Scene MetadataTableProperty.js

100% Statements 346/346
99.36% Branches 157/158
100% Functions 45/45
100% Lines 339/339

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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