createVectorTilePolylines.js 8.3 KB

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