createVectorTilePolylines.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  22. */
  23. define(['./Matrix2-d35cf4b5', './combine-3c023bda', './AttributeCompression-d0b97a83', './ComponentDatatype-9e86ac8f', './IndexDatatype-bed3935d', './createTaskProcessorWorker', './RuntimeError-8952249c', './defaultValue-81eec7ed', './WebGLConstants-508b9636'], (function (Matrix2, combine, AttributeCompression, ComponentDatatype, IndexDatatype, createTaskProcessorWorker, RuntimeError, defaultValue, WebGLConstants) { 'use strict';
  24. const maxShort = 32767;
  25. const scratchBVCartographic = new Matrix2.Cartographic();
  26. const scratchEncodedPosition = new Matrix2.Cartesian3();
  27. function decodeVectorPolylinePositions(
  28. positions,
  29. rectangle,
  30. minimumHeight,
  31. maximumHeight,
  32. ellipsoid
  33. ) {
  34. const positionsLength = positions.length / 3;
  35. const uBuffer = positions.subarray(0, positionsLength);
  36. const vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  37. const heightBuffer = positions.subarray(
  38. 2 * positionsLength,
  39. 3 * positionsLength
  40. );
  41. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer, heightBuffer);
  42. const decoded = new Float64Array(positions.length);
  43. for (let i = 0; i < positionsLength; ++i) {
  44. const u = uBuffer[i];
  45. const v = vBuffer[i];
  46. const h = heightBuffer[i];
  47. const lon = ComponentDatatype.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  48. const lat = ComponentDatatype.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  49. const alt = ComponentDatatype.CesiumMath.lerp(minimumHeight, maximumHeight, h / maxShort);
  50. const cartographic = Matrix2.Cartographic.fromRadians(
  51. lon,
  52. lat,
  53. alt,
  54. scratchBVCartographic
  55. );
  56. const decodedPosition = ellipsoid.cartographicToCartesian(
  57. cartographic,
  58. scratchEncodedPosition
  59. );
  60. Matrix2.Cartesian3.pack(decodedPosition, decoded, i * 3);
  61. }
  62. return decoded;
  63. }
  64. const scratchRectangle = new Matrix2.Rectangle();
  65. const scratchEllipsoid = new Matrix2.Ellipsoid();
  66. const scratchCenter = new Matrix2.Cartesian3();
  67. const scratchMinMaxHeights = {
  68. min: undefined,
  69. max: undefined,
  70. };
  71. function unpackBuffer(packedBuffer) {
  72. packedBuffer = new Float64Array(packedBuffer);
  73. let offset = 0;
  74. scratchMinMaxHeights.min = packedBuffer[offset++];
  75. scratchMinMaxHeights.max = packedBuffer[offset++];
  76. Matrix2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  77. offset += Matrix2.Rectangle.packedLength;
  78. Matrix2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  79. offset += Matrix2.Ellipsoid.packedLength;
  80. Matrix2.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  81. }
  82. function getPositionOffsets(counts) {
  83. const countsLength = counts.length;
  84. const positionOffsets = new Uint32Array(countsLength + 1);
  85. let offset = 0;
  86. for (let i = 0; i < countsLength; ++i) {
  87. positionOffsets[i] = offset;
  88. offset += counts[i];
  89. }
  90. positionOffsets[countsLength] = offset;
  91. return positionOffsets;
  92. }
  93. const scratchP0 = new Matrix2.Cartesian3();
  94. const scratchP1 = new Matrix2.Cartesian3();
  95. const scratchPrev = new Matrix2.Cartesian3();
  96. const scratchCur = new Matrix2.Cartesian3();
  97. const scratchNext = new Matrix2.Cartesian3();
  98. function createVectorTilePolylines(parameters, transferableObjects) {
  99. const encodedPositions = new Uint16Array(parameters.positions);
  100. const widths = new Uint16Array(parameters.widths);
  101. const counts = new Uint32Array(parameters.counts);
  102. const batchIds = new Uint16Array(parameters.batchIds);
  103. unpackBuffer(parameters.packedBuffer);
  104. const rectangle = scratchRectangle;
  105. const ellipsoid = scratchEllipsoid;
  106. const center = scratchCenter;
  107. const minimumHeight = scratchMinMaxHeights.min;
  108. const maximumHeight = scratchMinMaxHeights.max;
  109. const positions = decodeVectorPolylinePositions(
  110. encodedPositions,
  111. rectangle,
  112. minimumHeight,
  113. maximumHeight,
  114. ellipsoid
  115. );
  116. const positionsLength = positions.length / 3;
  117. const size = positionsLength * 4 - 4;
  118. const curPositions = new Float32Array(size * 3);
  119. const prevPositions = new Float32Array(size * 3);
  120. const nextPositions = new Float32Array(size * 3);
  121. const expandAndWidth = new Float32Array(size * 2);
  122. const vertexBatchIds = new Uint16Array(size);
  123. let positionIndex = 0;
  124. let expandAndWidthIndex = 0;
  125. let batchIdIndex = 0;
  126. let i;
  127. let offset = 0;
  128. let length = counts.length;
  129. for (i = 0; i < length; ++i) {
  130. const count = counts[i];
  131. const width = widths[i];
  132. const batchId = batchIds[i];
  133. for (let j = 0; j < count; ++j) {
  134. let previous;
  135. if (j === 0) {
  136. const p0 = Matrix2.Cartesian3.unpack(positions, offset * 3, scratchP0);
  137. const p1 = Matrix2.Cartesian3.unpack(positions, (offset + 1) * 3, scratchP1);
  138. previous = Matrix2.Cartesian3.subtract(p0, p1, scratchPrev);
  139. Matrix2.Cartesian3.add(p0, previous, previous);
  140. } else {
  141. previous = Matrix2.Cartesian3.unpack(
  142. positions,
  143. (offset + j - 1) * 3,
  144. scratchPrev
  145. );
  146. }
  147. const current = Matrix2.Cartesian3.unpack(
  148. positions,
  149. (offset + j) * 3,
  150. scratchCur
  151. );
  152. let next;
  153. if (j === count - 1) {
  154. const p2 = Matrix2.Cartesian3.unpack(
  155. positions,
  156. (offset + count - 1) * 3,
  157. scratchP0
  158. );
  159. const p3 = Matrix2.Cartesian3.unpack(
  160. positions,
  161. (offset + count - 2) * 3,
  162. scratchP1
  163. );
  164. next = Matrix2.Cartesian3.subtract(p2, p3, scratchNext);
  165. Matrix2.Cartesian3.add(p2, next, next);
  166. } else {
  167. next = Matrix2.Cartesian3.unpack(positions, (offset + j + 1) * 3, scratchNext);
  168. }
  169. Matrix2.Cartesian3.subtract(previous, center, previous);
  170. Matrix2.Cartesian3.subtract(current, center, current);
  171. Matrix2.Cartesian3.subtract(next, center, next);
  172. const startK = j === 0 ? 2 : 0;
  173. const endK = j === count - 1 ? 2 : 4;
  174. for (let k = startK; k < endK; ++k) {
  175. Matrix2.Cartesian3.pack(current, curPositions, positionIndex);
  176. Matrix2.Cartesian3.pack(previous, prevPositions, positionIndex);
  177. Matrix2.Cartesian3.pack(next, nextPositions, positionIndex);
  178. positionIndex += 3;
  179. const direction = k - 2 < 0 ? -1.0 : 1.0;
  180. expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1;
  181. expandAndWidth[expandAndWidthIndex++] = direction * width;
  182. vertexBatchIds[batchIdIndex++] = batchId;
  183. }
  184. }
  185. offset += count;
  186. }
  187. const indices = IndexDatatype.IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
  188. let index = 0;
  189. let indicesIndex = 0;
  190. length = positionsLength - 1;
  191. for (i = 0; i < length; ++i) {
  192. indices[indicesIndex++] = index;
  193. indices[indicesIndex++] = index + 2;
  194. indices[indicesIndex++] = index + 1;
  195. indices[indicesIndex++] = index + 1;
  196. indices[indicesIndex++] = index + 2;
  197. indices[indicesIndex++] = index + 3;
  198. index += 4;
  199. }
  200. transferableObjects.push(
  201. curPositions.buffer,
  202. prevPositions.buffer,
  203. nextPositions.buffer
  204. );
  205. transferableObjects.push(
  206. expandAndWidth.buffer,
  207. vertexBatchIds.buffer,
  208. indices.buffer
  209. );
  210. let results = {
  211. indexDatatype:
  212. indices.BYTES_PER_ELEMENT === 2
  213. ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT
  214. : IndexDatatype.IndexDatatype.UNSIGNED_INT,
  215. currentPositions: curPositions.buffer,
  216. previousPositions: prevPositions.buffer,
  217. nextPositions: nextPositions.buffer,
  218. expandAndWidth: expandAndWidth.buffer,
  219. batchIds: vertexBatchIds.buffer,
  220. indices: indices.buffer,
  221. };
  222. if (parameters.keepDecodedPositions) {
  223. const positionOffsets = getPositionOffsets(counts);
  224. transferableObjects.push(positions.buffer, positionOffsets.buffer);
  225. results = combine.combine(results, {
  226. decodedPositions: positions.buffer,
  227. decodedPositionOffsets: positionOffsets.buffer,
  228. });
  229. }
  230. return results;
  231. }
  232. var createVectorTilePolylines$1 = createTaskProcessorWorker(createVectorTilePolylines);
  233. return createVectorTilePolylines$1;
  234. }));
  235. //# sourceMappingURL=createVectorTilePolylines.js.map