createVectorTilePolylines.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./Matrix2-69c32d33', './combine-761d9c3f', './AttributeCompression-3cfab808', './ComponentDatatype-b1ea011a', './IndexDatatype-c4099fe9', './createTaskProcessorWorker', './RuntimeError-c581ca93', './defaultValue-94c3e563', './WebGLConstants-7dccdc96'], (function (Matrix2, combine, AttributeCompression, ComponentDatatype, IndexDatatype, createTaskProcessorWorker, RuntimeError, defaultValue, WebGLConstants) { 'use strict';
  3. const maxShort = 32767;
  4. const scratchBVCartographic = new Matrix2.Cartographic();
  5. const scratchEncodedPosition = new Matrix2.Cartesian3();
  6. function decodeVectorPolylinePositions(
  7. positions,
  8. rectangle,
  9. minimumHeight,
  10. maximumHeight,
  11. ellipsoid
  12. ) {
  13. const positionsLength = positions.length / 3;
  14. const uBuffer = positions.subarray(0, positionsLength);
  15. const vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  16. const heightBuffer = positions.subarray(
  17. 2 * positionsLength,
  18. 3 * positionsLength
  19. );
  20. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer, heightBuffer);
  21. const decoded = new Float64Array(positions.length);
  22. for (let i = 0; i < positionsLength; ++i) {
  23. const u = uBuffer[i];
  24. const v = vBuffer[i];
  25. const h = heightBuffer[i];
  26. const lon = ComponentDatatype.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  27. const lat = ComponentDatatype.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  28. const alt = ComponentDatatype.CesiumMath.lerp(minimumHeight, maximumHeight, h / maxShort);
  29. const cartographic = Matrix2.Cartographic.fromRadians(
  30. lon,
  31. lat,
  32. alt,
  33. scratchBVCartographic
  34. );
  35. const decodedPosition = ellipsoid.cartographicToCartesian(
  36. cartographic,
  37. scratchEncodedPosition
  38. );
  39. Matrix2.Cartesian3.pack(decodedPosition, decoded, i * 3);
  40. }
  41. return decoded;
  42. }
  43. const scratchRectangle = new Matrix2.Rectangle();
  44. const scratchEllipsoid = new Matrix2.Ellipsoid();
  45. const scratchCenter = new Matrix2.Cartesian3();
  46. const scratchMinMaxHeights = {
  47. min: undefined,
  48. max: undefined,
  49. };
  50. function unpackBuffer(packedBuffer) {
  51. packedBuffer = new Float64Array(packedBuffer);
  52. let offset = 0;
  53. scratchMinMaxHeights.min = packedBuffer[offset++];
  54. scratchMinMaxHeights.max = packedBuffer[offset++];
  55. Matrix2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  56. offset += Matrix2.Rectangle.packedLength;
  57. Matrix2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  58. offset += Matrix2.Ellipsoid.packedLength;
  59. Matrix2.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  60. }
  61. function getPositionOffsets(counts) {
  62. const countsLength = counts.length;
  63. const positionOffsets = new Uint32Array(countsLength + 1);
  64. let offset = 0;
  65. for (let i = 0; i < countsLength; ++i) {
  66. positionOffsets[i] = offset;
  67. offset += counts[i];
  68. }
  69. positionOffsets[countsLength] = offset;
  70. return positionOffsets;
  71. }
  72. const scratchP0 = new Matrix2.Cartesian3();
  73. const scratchP1 = new Matrix2.Cartesian3();
  74. const scratchPrev = new Matrix2.Cartesian3();
  75. const scratchCur = new Matrix2.Cartesian3();
  76. const scratchNext = new Matrix2.Cartesian3();
  77. function createVectorTilePolylines(parameters, transferableObjects) {
  78. const encodedPositions = new Uint16Array(parameters.positions);
  79. const widths = new Uint16Array(parameters.widths);
  80. const counts = new Uint32Array(parameters.counts);
  81. const batchIds = new Uint16Array(parameters.batchIds);
  82. unpackBuffer(parameters.packedBuffer);
  83. const rectangle = scratchRectangle;
  84. const ellipsoid = scratchEllipsoid;
  85. const center = scratchCenter;
  86. const minimumHeight = scratchMinMaxHeights.min;
  87. const maximumHeight = scratchMinMaxHeights.max;
  88. const positions = decodeVectorPolylinePositions(
  89. encodedPositions,
  90. rectangle,
  91. minimumHeight,
  92. maximumHeight,
  93. ellipsoid
  94. );
  95. const positionsLength = positions.length / 3;
  96. const size = positionsLength * 4 - 4;
  97. const curPositions = new Float32Array(size * 3);
  98. const prevPositions = new Float32Array(size * 3);
  99. const nextPositions = new Float32Array(size * 3);
  100. const expandAndWidth = new Float32Array(size * 2);
  101. const vertexBatchIds = new Uint16Array(size);
  102. let positionIndex = 0;
  103. let expandAndWidthIndex = 0;
  104. let batchIdIndex = 0;
  105. let i;
  106. let offset = 0;
  107. let length = counts.length;
  108. for (i = 0; i < length; ++i) {
  109. const count = counts[i];
  110. const width = widths[i];
  111. const batchId = batchIds[i];
  112. for (let j = 0; j < count; ++j) {
  113. let previous;
  114. if (j === 0) {
  115. const p0 = Matrix2.Cartesian3.unpack(positions, offset * 3, scratchP0);
  116. const p1 = Matrix2.Cartesian3.unpack(positions, (offset + 1) * 3, scratchP1);
  117. previous = Matrix2.Cartesian3.subtract(p0, p1, scratchPrev);
  118. Matrix2.Cartesian3.add(p0, previous, previous);
  119. } else {
  120. previous = Matrix2.Cartesian3.unpack(
  121. positions,
  122. (offset + j - 1) * 3,
  123. scratchPrev
  124. );
  125. }
  126. const current = Matrix2.Cartesian3.unpack(
  127. positions,
  128. (offset + j) * 3,
  129. scratchCur
  130. );
  131. let next;
  132. if (j === count - 1) {
  133. const p2 = Matrix2.Cartesian3.unpack(
  134. positions,
  135. (offset + count - 1) * 3,
  136. scratchP0
  137. );
  138. const p3 = Matrix2.Cartesian3.unpack(
  139. positions,
  140. (offset + count - 2) * 3,
  141. scratchP1
  142. );
  143. next = Matrix2.Cartesian3.subtract(p2, p3, scratchNext);
  144. Matrix2.Cartesian3.add(p2, next, next);
  145. } else {
  146. next = Matrix2.Cartesian3.unpack(positions, (offset + j + 1) * 3, scratchNext);
  147. }
  148. Matrix2.Cartesian3.subtract(previous, center, previous);
  149. Matrix2.Cartesian3.subtract(current, center, current);
  150. Matrix2.Cartesian3.subtract(next, center, next);
  151. const startK = j === 0 ? 2 : 0;
  152. const endK = j === count - 1 ? 2 : 4;
  153. for (let k = startK; k < endK; ++k) {
  154. Matrix2.Cartesian3.pack(current, curPositions, positionIndex);
  155. Matrix2.Cartesian3.pack(previous, prevPositions, positionIndex);
  156. Matrix2.Cartesian3.pack(next, nextPositions, positionIndex);
  157. positionIndex += 3;
  158. const direction = k - 2 < 0 ? -1.0 : 1.0;
  159. expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1;
  160. expandAndWidth[expandAndWidthIndex++] = direction * width;
  161. vertexBatchIds[batchIdIndex++] = batchId;
  162. }
  163. }
  164. offset += count;
  165. }
  166. const indices = IndexDatatype.IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
  167. let index = 0;
  168. let indicesIndex = 0;
  169. length = positionsLength - 1;
  170. for (i = 0; i < length; ++i) {
  171. indices[indicesIndex++] = index;
  172. indices[indicesIndex++] = index + 2;
  173. indices[indicesIndex++] = index + 1;
  174. indices[indicesIndex++] = index + 1;
  175. indices[indicesIndex++] = index + 2;
  176. indices[indicesIndex++] = index + 3;
  177. index += 4;
  178. }
  179. transferableObjects.push(
  180. curPositions.buffer,
  181. prevPositions.buffer,
  182. nextPositions.buffer
  183. );
  184. transferableObjects.push(
  185. expandAndWidth.buffer,
  186. vertexBatchIds.buffer,
  187. indices.buffer
  188. );
  189. let results = {
  190. indexDatatype:
  191. indices.BYTES_PER_ELEMENT === 2
  192. ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT
  193. : IndexDatatype.IndexDatatype.UNSIGNED_INT,
  194. currentPositions: curPositions.buffer,
  195. previousPositions: prevPositions.buffer,
  196. nextPositions: nextPositions.buffer,
  197. expandAndWidth: expandAndWidth.buffer,
  198. batchIds: vertexBatchIds.buffer,
  199. indices: indices.buffer,
  200. };
  201. if (parameters.keepDecodedPositions) {
  202. const positionOffsets = getPositionOffsets(counts);
  203. transferableObjects.push(positions.buffer, positionOffsets.buffer);
  204. results = combine.combine(results, {
  205. decodedPositions: positions.buffer,
  206. decodedPositionOffsets: positionOffsets.buffer,
  207. });
  208. }
  209. return results;
  210. }
  211. var createVectorTilePolylines$1 = createTaskProcessorWorker(createVectorTilePolylines);
  212. return createVectorTilePolylines$1;
  213. }));