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

BufferPolygon.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // @ts-check
  2. import assert from "../Core/assert.js";
  3. import Check from "../Core/Check.js";
  4. import defined from "../Core/defined.js";
  5. import BufferPrimitive from "./BufferPrimitive.js";
  6. import BufferPrimitiveCollection from "./BufferPrimitiveCollection.js";
  7. /** @import { TypedArray, TypedArrayConstructor } from "../Core/globalTypes.js"; */
  8. /** @import BufferPolygonCollection from "./BufferPolygonCollection.js"; */
  9. const { ERR_CAPACITY, ERR_RESIZE, ERR_OUT_OF_RANGE } =
  10. BufferPrimitiveCollection.Error;
  11. /**
  12. * View bound to the underlying buffer data of a {@link BufferPolygonCollection}.
  13. *
  14. * <p>BufferPolygon instances are {@link https://en.wikipedia.org/wiki/Flyweight_pattern|flyweights}:
  15. * a single BufferPolygon instance can be temporarily bound to any conceptual
  16. * "polygon" in a BufferPolygonCollection, allowing very large collections to be
  17. * iterated and updated with a minimal memory footprint.</p>
  18. *
  19. * <p>Represented as one (1) external linear ring of three (3) or more positions.
  20. * May optionally define one or more internal linear rings ("holes") within the
  21. * polygon. Each hole is represented as a single index into the positions array,
  22. * where the vertex at that index is the start of an internal linear ring that
  23. * continues along the following vertices until reaching either the vertex
  24. * index of the next hole, or the end of the vertex list. Stores a precomputed
  25. * triangulation, represented as three vertex indices per triangle.</p>
  26. *
  27. * @see BufferPolygonCollection
  28. * @see BufferPolygonMaterial
  29. * @see BufferPrimitive
  30. * @extends BufferPrimitive
  31. * @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
  32. */
  33. class BufferPolygon extends BufferPrimitive {
  34. /**
  35. * @type {BufferPolygonCollection}
  36. * @ignore
  37. */
  38. _collection = null;
  39. /** @ignore */
  40. static Layout = {
  41. ...BufferPrimitive.Layout,
  42. /**
  43. * Offset in collection position array to first vertex in polygon, number
  44. * of VEC3 elements.
  45. * @type {number}
  46. * @ignore
  47. */
  48. POSITION_OFFSET_U32: BufferPrimitive.Layout.__BYTE_LENGTH,
  49. /**
  50. * Count of positions (vertices) in this polygon, number of VEC3 elements.
  51. * @type {number}
  52. * @ignore
  53. */
  54. POSITION_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 4,
  55. /**
  56. * Offset in collection holes array to first hole in polygon, number of
  57. * integer elements.
  58. * @type {number}
  59. * @ignore
  60. */
  61. HOLE_OFFSET_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 8,
  62. /**
  63. * Count of holes (indices) in this polygon.
  64. * @type {number}
  65. * @ignore
  66. */
  67. HOLE_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 12,
  68. /**
  69. * Offset in collection triangles array to first triangle in polygon,
  70. * number of VEC3 elements.
  71. * @type {number}
  72. * @ignore
  73. */
  74. TRIANGLE_OFFSET_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 16,
  75. /**
  76. * Count of triangles in this polygon, number of VEC3 elements.
  77. * @type {number}
  78. * @ignore
  79. */
  80. TRIANGLE_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 20,
  81. /**
  82. * @type {number}
  83. * @ignore
  84. */
  85. __BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 24,
  86. };
  87. /////////////////////////////////////////////////////////////////////////////
  88. // LIFECYCLE
  89. /**
  90. * Copies data from source polygon to result. If the result polygon is not
  91. * new (the last polygon in the collection) then source and result polygons
  92. * must have the same vertex counts, hole counts, and triangle counts.
  93. *
  94. * @param {BufferPolygon} polygon
  95. * @param {BufferPolygon} result
  96. * @return {BufferPolygon}
  97. * @override
  98. */
  99. static clone(polygon, result) {
  100. super.clone(polygon, result);
  101. result.setPositions(polygon.getPositions());
  102. result.setHoles(polygon.getHoles());
  103. result.setTriangles(polygon.getTriangles());
  104. return result;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // GEOMETRY
  108. /**
  109. * Offset in collection position array to first vertex in polygon, number
  110. * of VEC3 elements.
  111. *
  112. * @type {number}
  113. * @readonly
  114. * @ignore
  115. */
  116. get vertexOffset() {
  117. return this._getUint32(BufferPolygon.Layout.POSITION_OFFSET_U32);
  118. }
  119. /**
  120. * Count of positions (vertices) in this polygon, including both outer ring and
  121. * internal rings (holes), number of VEC3 elements.
  122. *
  123. * @type {number}
  124. * @readonly
  125. */
  126. get vertexCount() {
  127. return this._getUint32(BufferPolygon.Layout.POSITION_COUNT_U32);
  128. }
  129. /**
  130. * Returns an array view of this polygon's vertex positions. If 'result'
  131. * argument is given, vertex positions are written to that array and returned.
  132. * Otherwise, returns an ArrayView on collection memory — changes to this array
  133. * will not trigger render updates, which requires `.setPositions()`.
  134. *
  135. * @param {TypedArray} [result]
  136. * return {TypedArray}
  137. */
  138. getPositions(result) {
  139. return this._getPositionsRange(0, this.vertexCount, result);
  140. }
  141. /** @param {TypedArray} positions */
  142. setPositions(positions) {
  143. const collection = this._collection;
  144. const vertexOffset = this.vertexOffset;
  145. const srcCount = this.vertexCount;
  146. const dstCount = positions.length / 3;
  147. const collectionCount = collection.vertexCount + dstCount - srcCount;
  148. //>>includeStart('debug', pragmas.debug);
  149. assert(srcCount === dstCount || this._isResizable(), ERR_RESIZE);
  150. assert(collectionCount <= collection.vertexCountMax, ERR_CAPACITY);
  151. //>>includeEnd('debug');
  152. collection._positionCount = collectionCount;
  153. this._setUint32(BufferPolygon.Layout.POSITION_COUNT_U32, dstCount);
  154. const positionView = collection._positionView;
  155. for (let i = 0; i < dstCount; i++) {
  156. positionView[(vertexOffset + i) * 3] = positions[i * 3];
  157. positionView[(vertexOffset + i) * 3 + 1] = positions[i * 3 + 1];
  158. positionView[(vertexOffset + i) * 3 + 2] = positions[i * 3 + 2];
  159. }
  160. this._dirty = true;
  161. collection._makeDirtyBoundingVolume();
  162. }
  163. /**
  164. * Offset in collection position array to first vertex in polygon's outer
  165. * linear ring, number of VEC3 elements.
  166. *
  167. * @type {number}
  168. * @readonly
  169. */
  170. get outerVertexOffset() {
  171. return this.vertexOffset;
  172. }
  173. /**
  174. * Count of positions (vertices) in this polygon's outer linear ring, number
  175. * of VEC3 elements.
  176. *
  177. * @type {number}
  178. * @readonly
  179. */
  180. get outerVertexCount() {
  181. if (this.holeCount > 0) {
  182. return this.getHoles()[0];
  183. }
  184. return this.vertexCount;
  185. }
  186. /**
  187. * Returns an array view of this polygon's outer linear ring vertex positions.
  188. * If 'result' argument is given, vertex positions are written to that array
  189. * and returned. Otherwise, returns an ArrayView on collection memory —
  190. * changes to this array will not trigger render updates, which requires
  191. * `.setPositions()`.
  192. *
  193. * @param {TypedArray} [result]
  194. * @returns {TypedArray}
  195. */
  196. getOuterPositions(result) {
  197. return this._getPositionsRange(0, this.outerVertexCount, result);
  198. }
  199. /**
  200. * Offset in collection holes array to first hole in polygon, number of
  201. * integer elements.
  202. *
  203. * @type {number}
  204. * @readonly
  205. * @ignore
  206. */
  207. get holeOffset() {
  208. return this._getUint32(BufferPolygon.Layout.HOLE_OFFSET_U32);
  209. }
  210. /**
  211. * Count of holes (indices) in this polygon.
  212. *
  213. * @type {number}
  214. * @readonly
  215. */
  216. get holeCount() {
  217. return this._getUint32(BufferPolygon.Layout.HOLE_COUNT_U32);
  218. }
  219. /**
  220. * Gets this polygon's hole indices, with each hole represented as a single
  221. * offset into this polygon's positions array. Each hole implicitly
  222. * continues along an internal linear ring from that vertex offset until
  223. * reaching either the end of the positions array, or the next hole offset.
  224. *
  225. * If 'result' argument is given, hole indices are written to that array and
  226. * returned. Otherwise, returns an ArrayView on collection memory — changes
  227. * to this array will not trigger render updates, which requires `.setHoles()`.
  228. *
  229. * @param {TypedArray} [result]
  230. * @returns {TypedArray}
  231. */
  232. getHoles(result) {
  233. const { holeOffset, holeCount } = this;
  234. const holeIndexView = this._collection._holeIndexView;
  235. if (!defined(result)) {
  236. const byteOffset =
  237. holeIndexView.byteOffset + holeOffset * holeIndexView.BYTES_PER_ELEMENT;
  238. const TypedArray = /** @type {TypedArrayConstructor} */ (
  239. holeIndexView.constructor
  240. );
  241. return new TypedArray(
  242. /** @type {ArrayBuffer} */ (holeIndexView.buffer),
  243. byteOffset,
  244. holeCount,
  245. );
  246. }
  247. for (let i = 0; i < holeCount; i++) {
  248. result[i] = holeIndexView[holeOffset + i];
  249. }
  250. return result;
  251. }
  252. /**
  253. * Sets this polygon's hole indices, with holes represented as a single
  254. * offset into this polygon's positions array. Each hole implicitly
  255. * continues along an internal linear ring from that vertex offset until
  256. * reaching either the end of the positions array, or the next hole offset.
  257. *
  258. * @param {TypedArray} holes
  259. */
  260. setHoles(holes) {
  261. const collection = this._collection;
  262. const holeOffset = this.holeOffset;
  263. const srcCount = this.holeCount;
  264. const dstCount = holes.length;
  265. const collectionCount = collection.holeCount + dstCount - srcCount;
  266. //>>includeStart('debug', pragmas.debug);
  267. assert(srcCount === dstCount || this._isResizable(), ERR_RESIZE);
  268. assert(collectionCount <= collection.holeCountMax, ERR_CAPACITY);
  269. //>>includeEnd('debug');
  270. collection._holeCount = collectionCount;
  271. this._setUint32(BufferPolygon.Layout.HOLE_COUNT_U32, dstCount);
  272. const holeIndexView = collection._holeIndexView;
  273. for (let i = 0; i < dstCount; i++) {
  274. holeIndexView[holeOffset + i] = holes[i];
  275. }
  276. this._dirty = true;
  277. }
  278. /**
  279. * Returns the number of (VEC3) vertices in the specified hole.
  280. *
  281. * @param {number} holeIndex
  282. * @returns {number}
  283. */
  284. getHoleVertexCount(holeIndex) {
  285. const holes = this.getHoles();
  286. //>>includeStart('debug', pragmas.debug);
  287. Check.typeOf.number.greaterThanOrEquals("holeIndex", holeIndex, 0);
  288. Check.typeOf.number.lessThan("holeIndex", holeIndex, holes.length);
  289. //>>includeEnd('debug');
  290. const holeVertexOffset = holes[holeIndex];
  291. return holeIndex === holes.length - 1
  292. ? this.vertexCount - holeVertexOffset
  293. : holes[holeIndex + 1] - holeVertexOffset;
  294. }
  295. /**
  296. * Returns an array view of the inner linear ring vertex positions for the
  297. * specified hole. If 'result' argument is given, vertex positions are written
  298. * to that array and returned. Otherwise, returns an ArrayView on collection
  299. * memory — changes to this array will not trigger render updates, which
  300. * requires `.setPositions()`.
  301. *
  302. * @param {number} holeIndex
  303. * @param {TypedArray} [result]
  304. * return {TypedArray}
  305. */
  306. getHolePositions(holeIndex, result) {
  307. const holes = this.getHoles();
  308. //>>includeStart('debug', pragmas.debug);
  309. Check.typeOf.number.greaterThanOrEquals("holeIndex", holeIndex, 0);
  310. Check.typeOf.number.lessThan("holeIndex", holeIndex, holes.length);
  311. //>>includeEnd('debug');
  312. const holeVertexOffset = holes[holeIndex];
  313. const holeVertexCount = this.getHoleVertexCount(holeIndex);
  314. return this._getPositionsRange(holeVertexOffset, holeVertexCount, result);
  315. }
  316. /**
  317. * Internal helper for accessing vertex positions. 'vertexOffset' argument
  318. * is relative to the start of the polygon's vertex block, with 0 being
  319. * the first vertex in the polygon. If 'result' argument is given, the
  320. * requested range of vertices are written to the result array and returned.
  321. * Otherwise, returns an ArrayView on collection memory.
  322. *
  323. * @param {number} vertexOffset
  324. * @param {number} vertexCount
  325. * @param {TypedArray} [result]
  326. * @returns {TypedArray}
  327. * @private
  328. */
  329. _getPositionsRange(vertexOffset, vertexCount, result) {
  330. const collection = this._collection;
  331. const positionView = this._collection._positionView;
  332. const collectionVertexOffset = this.vertexOffset + vertexOffset;
  333. //>>includeStart('debug', pragmas.debug);
  334. assert(collectionVertexOffset >= 0, ERR_OUT_OF_RANGE);
  335. assert(collectionVertexOffset < collection.vertexCount, ERR_OUT_OF_RANGE);
  336. assert(vertexCount > 0, ERR_OUT_OF_RANGE);
  337. assert(vertexCount <= this.vertexCount, ERR_OUT_OF_RANGE);
  338. //>>includeEnd('debug');
  339. if (!defined(result)) {
  340. const byteOffset =
  341. positionView.byteOffset +
  342. collectionVertexOffset * 3 * positionView.BYTES_PER_ELEMENT;
  343. const TypedArray = /** @type {TypedArrayConstructor} */ (
  344. positionView.constructor
  345. );
  346. return new TypedArray(
  347. /** @type {ArrayBuffer} */ (positionView.buffer),
  348. byteOffset,
  349. vertexCount * 3,
  350. );
  351. }
  352. for (let i = 0; i < vertexCount; i++) {
  353. result[i * 3] = positionView[(collectionVertexOffset + i) * 3];
  354. result[i * 3 + 1] = positionView[(collectionVertexOffset + i) * 3 + 1];
  355. result[i * 3 + 2] = positionView[(collectionVertexOffset + i) * 3 + 2];
  356. }
  357. return result;
  358. }
  359. /**
  360. * Offset in collection triangles array to first triangle in polygon,
  361. * number of VEC3 elements.
  362. * @type {number}
  363. * @readonly
  364. * @ignore
  365. */
  366. get triangleOffset() {
  367. return this._getUint32(BufferPolygon.Layout.TRIANGLE_OFFSET_U32);
  368. }
  369. /**
  370. * Count of triangles in this polygon, number of VEC3 elements.
  371. *
  372. * @type {number}
  373. * @readonly
  374. */
  375. get triangleCount() {
  376. return this._getUint32(BufferPolygon.Layout.TRIANGLE_COUNT_U32);
  377. }
  378. /**
  379. * Returns an array view of this polygon's triangle indices, represented as
  380. * three vertex indices per triangle.
  381. *
  382. * If 'result' argument is given, triangle indices are written to that array
  383. * and returned. Otherwise, returns an ArrayView on collection memory —
  384. * changes to this array will not trigger render updates, which requires
  385. * `.setTriangles()`.
  386. *
  387. * @param {TypedArray} [result]
  388. * @returns {TypedArray}
  389. */
  390. getTriangles(result) {
  391. const { triangleOffset, triangleCount } = this;
  392. const indices = this._collection._triangleIndexView;
  393. if (!defined(result)) {
  394. const byteOffset =
  395. indices.byteOffset + triangleOffset * 3 * indices.BYTES_PER_ELEMENT;
  396. const TypedArray = /** @type {TypedArrayConstructor} */ (
  397. indices.constructor
  398. );
  399. return new TypedArray(
  400. /** @type {ArrayBuffer} */ (indices.buffer),
  401. byteOffset,
  402. triangleCount * 3,
  403. );
  404. }
  405. for (let i = 0; i < triangleCount; i++) {
  406. result[i * 3] = indices[(triangleOffset + i) * 3];
  407. result[i * 3 + 1] = indices[(triangleOffset + i) * 3 + 1];
  408. result[i * 3 + 2] = indices[(triangleOffset + i) * 3 + 2];
  409. }
  410. return result;
  411. }
  412. /**
  413. * Sets this polygon's triangle indices, represented as three vertex indices
  414. * per triangle.
  415. *
  416. * @param {TypedArray} indices
  417. */
  418. setTriangles(indices) {
  419. const collection = this._collection;
  420. const triangleOffset = this.triangleOffset;
  421. const srcCount = this.triangleCount;
  422. const dstCount = indices.length / 3;
  423. const collectionCount = collection.triangleCount + dstCount - srcCount;
  424. //>>includeStart('debug', pragmas.debug);
  425. assert(srcCount === dstCount || this._isResizable(), ERR_RESIZE);
  426. assert(collectionCount <= collection.triangleCountMax, ERR_CAPACITY);
  427. //>>includeEnd('debug');
  428. collection._triangleCount += dstCount - srcCount;
  429. this._setUint32(BufferPolygon.Layout.TRIANGLE_COUNT_U32, dstCount);
  430. const dstIndices = collection._triangleIndexView;
  431. for (let i = 0; i < dstCount; i++) {
  432. dstIndices[(triangleOffset + i) * 3] = indices[i * 3];
  433. dstIndices[(triangleOffset + i) * 3 + 1] = indices[i * 3 + 1];
  434. dstIndices[(triangleOffset + i) * 3 + 2] = indices[i * 3 + 2];
  435. }
  436. this._dirty = true;
  437. }
  438. /////////////////////////////////////////////////////////////////////////////
  439. // DEBUG
  440. /**
  441. * Returns a JSON-serializable object representing the polygon. This encoding
  442. * is not memory-efficient, and should generally be used for debugging and
  443. * testing.
  444. *
  445. * @returns {Object} JSON-serializable object.
  446. * @override
  447. */
  448. toJSON() {
  449. return {
  450. ...super.toJSON(),
  451. positions: Array.from(this.getPositions()),
  452. holes: Array.from(this.getHoles()),
  453. triangles: Array.from(this.getTriangles()),
  454. };
  455. }
  456. }
  457. export default BufferPolygon;