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

BufferPolyline.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // @ts-check
  2. import BufferPrimitive from "./BufferPrimitive.js";
  3. import assert from "../Core/assert.js";
  4. import BufferPrimitiveCollection from "./BufferPrimitiveCollection.js";
  5. import defined from "../Core/defined.js";
  6. /** @import { TypedArray, TypedArrayConstructor } from "../Core/globalTypes.js"; */
  7. /** @import BufferPolylineCollection from "./BufferPolylineCollection.js"; */
  8. const { ERR_RESIZE, ERR_CAPACITY } = BufferPrimitiveCollection.Error;
  9. /**
  10. * View bound to the underlying buffer data of a {@link BufferPolylineCollection}.
  11. *
  12. * <p>BufferPolyline instances are {@link https://en.wikipedia.org/wiki/Flyweight_pattern|flyweights}:
  13. * a single BufferPolyline instance can be temporarily bound to any conceptual
  14. * "polyline" in a BufferPolylineCollection, allowing very large collections to be
  15. * iterated and updated with a minimal memory footprint.</p>
  16. *
  17. * Represented as two (2) or more positions.
  18. *
  19. * @see BufferPolylineCollection
  20. * @see BufferPolylineMaterial
  21. * @see BufferPrimitive
  22. * @extends BufferPrimitive
  23. * @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
  24. */
  25. class BufferPolyline extends BufferPrimitive {
  26. /**
  27. * @type {BufferPolylineCollection}
  28. * @ignore
  29. */
  30. _collection = null;
  31. /** @ignore */
  32. static Layout = {
  33. ...BufferPrimitive.Layout,
  34. /**
  35. * Offset in position array to first vertex in polyline, number of VEC3 elements.
  36. * @type {number}
  37. * @ignore
  38. */
  39. POSITION_OFFSET_U32: BufferPrimitive.Layout.__BYTE_LENGTH,
  40. /**
  41. * Count of positions (vertices) in this polyline, number of VEC3 elements.
  42. * @type {number}
  43. * @ignore
  44. */
  45. POSITION_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 4,
  46. /**
  47. * @type {number}
  48. * @ignore
  49. */
  50. __BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 8,
  51. };
  52. /////////////////////////////////////////////////////////////////////////////
  53. // LIFECYCLE
  54. /**
  55. * Copies data from source polyline to result. If the result polyline is not
  56. * new (the last polyline in the collection) then source and result polylines
  57. * must have the same vertex counts.
  58. *
  59. * @param {BufferPolyline} polyline
  60. * @param {BufferPolyline} result
  61. * @return {BufferPolyline}
  62. * @override
  63. */
  64. static clone(polyline, result) {
  65. super.clone(polyline, result);
  66. result.setPositions(polyline.getPositions());
  67. return result;
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // GEOMETRY
  71. /**
  72. * Offset in collection position array to first vertex in polyline, number
  73. * of VEC3 elements.
  74. *
  75. * @type {number}
  76. * @readonly
  77. * @ignore
  78. */
  79. get vertexOffset() {
  80. return this._getUint32(BufferPolyline.Layout.POSITION_OFFSET_U32);
  81. }
  82. /**
  83. * Count of positions (vertices) in this polyline, number of VEC3 elements.
  84. *
  85. * @type {number}
  86. * @readonly
  87. */
  88. get vertexCount() {
  89. return this._getUint32(BufferPolyline.Layout.POSITION_COUNT_U32);
  90. }
  91. /**
  92. * Returns an array view of this polyline's vertex positions. If 'result'
  93. * argument is given, vertex positions are written to that array and returned.
  94. * Otherwise, returns an ArrayView on collection memory — changes to this array
  95. * will not trigger render updates, which requires `.setPositions()`.
  96. *
  97. * @param {TypedArray} [result]
  98. * return {TypedArray}
  99. */
  100. getPositions(result) {
  101. const { vertexOffset, vertexCount } = this;
  102. const positionView = this._collection._positionView;
  103. if (!defined(result)) {
  104. const byteOffset =
  105. positionView.byteOffset +
  106. vertexOffset * 3 * positionView.BYTES_PER_ELEMENT;
  107. const TypedArray = /** @type {TypedArrayConstructor} */ (
  108. positionView.constructor
  109. );
  110. return new TypedArray(
  111. /** @type {ArrayBuffer} */ (positionView.buffer),
  112. byteOffset,
  113. vertexCount * 3,
  114. );
  115. }
  116. for (let i = 0; i < vertexCount; i++) {
  117. result[i * 3] = positionView[(vertexOffset + i) * 3];
  118. result[i * 3 + 1] = positionView[(vertexOffset + i) * 3 + 1];
  119. result[i * 3 + 2] = positionView[(vertexOffset + i) * 3 + 2];
  120. }
  121. return result;
  122. }
  123. /** @param {TypedArray} positions */
  124. setPositions(positions) {
  125. const collection = this._collection;
  126. const vertexOffset = this.vertexOffset;
  127. const srcCount = this.vertexCount;
  128. const dstCount = positions.length / 3;
  129. const collectionCount = collection._positionCount + dstCount - srcCount;
  130. //>>includeStart('debug', pragmas.debug);
  131. assert(srcCount === dstCount || this._isResizable(), ERR_RESIZE);
  132. assert(collectionCount <= collection.vertexCountMax, ERR_CAPACITY);
  133. //>>includeEnd('debug');
  134. collection._positionCount = collectionCount;
  135. this._setUint32(BufferPolyline.Layout.POSITION_COUNT_U32, dstCount);
  136. const positionView = collection._positionView;
  137. for (let i = 0; i < dstCount; i++) {
  138. positionView[(vertexOffset + i) * 3] = positions[i * 3];
  139. positionView[(vertexOffset + i) * 3 + 1] = positions[i * 3 + 1];
  140. positionView[(vertexOffset + i) * 3 + 2] = positions[i * 3 + 2];
  141. }
  142. this._dirty = true;
  143. collection._makeDirtyBoundingVolume();
  144. }
  145. /////////////////////////////////////////////////////////////////////////////
  146. // DEBUG
  147. /**
  148. * Returns a JSON-serializable object representing the polyline. This encoding
  149. * is not memory-efficient, and should generally be used for debugging and
  150. * testing.
  151. *
  152. * @returns {Object} JSON-serializable object.
  153. * @override
  154. */
  155. toJSON() {
  156. return {
  157. ...super.toJSON(),
  158. positions: Array.from(this.getPositions()),
  159. };
  160. }
  161. }
  162. export default BufferPolyline;