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

ImplicitAvailabilityBitstream.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import RuntimeError from "../Core/RuntimeError.js";
  5. /**
  6. * An availability bitstream for use in an {@link ImplicitSubtree}. This handles
  7. * both Uint8Array bitstreams and constant values.
  8. *
  9. * @alias ImplicitAvailabilityBitstream
  10. * @constructor
  11. *
  12. * @param {object} options An object with the following properties:
  13. * @param {number} options.lengthBits The length of the bitstream in bits
  14. * @param {boolean} [options.constant] A single boolean value indicating the value of all the bits in the bitstream if they are all the same
  15. * @param {Uint8Array} [options.bitstream] An array of bytes storing the bitstream in binary
  16. * @param {number} [options.availableCount] A number indicating how many 1 bits are found in the bitstream
  17. * @param {boolean} [options.computeAvailableCountEnabled=false] If true, and options.availableCount is undefined, the availableCount will be computed from the bitstream.
  18. * @private
  19. * @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.
  20. */
  21. function ImplicitAvailabilityBitstream(options) {
  22. const lengthBits = options.lengthBits;
  23. let availableCount = options.availableCount;
  24. //>>includeStart('debug', pragmas.debug);
  25. Check.typeOf.number("options.lengthBits", lengthBits);
  26. //>>includeEnd('debug');
  27. const constant = options.constant;
  28. const bitstream = options.bitstream;
  29. if (defined(constant)) {
  30. // if defined, constant must be 1 which means all tiles are available
  31. availableCount = lengthBits;
  32. } else {
  33. const expectedLength = Math.ceil(lengthBits / 8);
  34. if (bitstream.length !== expectedLength) {
  35. throw new RuntimeError(
  36. `Availability bitstream must be exactly ${expectedLength} bytes long to store ${lengthBits} bits. Actual bitstream was ${bitstream.length} bytes long.`,
  37. );
  38. }
  39. // Only compute the available count if requested, as this involves looping
  40. // over the bitstream.
  41. const computeAvailableCountEnabled =
  42. options.computeAvailableCountEnabled ?? false;
  43. if (!defined(availableCount) && computeAvailableCountEnabled) {
  44. availableCount = count1Bits(bitstream, lengthBits);
  45. }
  46. }
  47. this._lengthBits = lengthBits;
  48. this._availableCount = availableCount;
  49. this._constant = constant;
  50. this._bitstream = bitstream;
  51. }
  52. /**
  53. * Count the number of bits with value 1 in the bitstream. This is used for
  54. * computing availableCount if not precomputed
  55. *
  56. * @param {Uint8Array} bitstream The bitstream typed array
  57. * @param {number} lengthBits How many bits are in the bitstream
  58. * @private
  59. */
  60. function count1Bits(bitstream, lengthBits) {
  61. let count = 0;
  62. for (let i = 0; i < lengthBits; i++) {
  63. const byteIndex = i >> 3;
  64. const bitIndex = i % 8;
  65. count += (bitstream[byteIndex] >> bitIndex) & 1;
  66. }
  67. return count;
  68. }
  69. Object.defineProperties(ImplicitAvailabilityBitstream.prototype, {
  70. /**
  71. * The length of the bitstream in bits.
  72. *
  73. * @memberof ImplicitAvailabilityBitstream.prototype
  74. *
  75. * @type {number}
  76. * @readonly
  77. * @private
  78. */
  79. lengthBits: {
  80. get: function () {
  81. return this._lengthBits;
  82. },
  83. },
  84. /**
  85. * The number of bits in the bitstream with value <code>1</code>.
  86. *
  87. * @memberof ImplicitAvailabilityBitstream.prototype
  88. *
  89. * @type {number}
  90. * @readonly
  91. * @private
  92. */
  93. availableCount: {
  94. get: function () {
  95. return this._availableCount;
  96. },
  97. },
  98. });
  99. /**
  100. * Get a bit from the availability bitstream as a Boolean. If the bitstream
  101. * is a constant, the constant value is returned instead.
  102. *
  103. * @param {number} index The integer index of the bit.
  104. * @returns {boolean} The value of the bit
  105. * @private
  106. */
  107. ImplicitAvailabilityBitstream.prototype.getBit = function (index) {
  108. //>>includeStart('debug', pragmas.debug);
  109. if (index < 0 || index >= this._lengthBits) {
  110. throw new DeveloperError("Bit index out of bounds.");
  111. }
  112. //>>includeEnd('debug');
  113. if (defined(this._constant)) {
  114. return this._constant;
  115. }
  116. // byteIndex is floor(index / 8)
  117. const byteIndex = index >> 3;
  118. const bitIndex = index % 8;
  119. return ((this._bitstream[byteIndex] >> bitIndex) & 1) === 1;
  120. };
  121. export default ImplicitAvailabilityBitstream;