createVectorTilePolylines.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './createTaskProcessorWorker', './Cartesian2-85064f09', './WebGLConstants-4c11ee5f', './AttributeCompression-84a90a13', './IndexDatatype-9435b55f'], function (when, Check, _Math, Cartographic, createTaskProcessorWorker, Cartesian2, WebGLConstants, AttributeCompression, IndexDatatype) { 'use strict';
  24. var maxShort = 32767;
  25. var scratchBVCartographic = new Cartographic.Cartographic();
  26. var scratchEncodedPosition = new Cartographic.Cartesian3();
  27. function decodePositions(positions, rectangle, minimumHeight, maximumHeight, ellipsoid) {
  28. var positionsLength = positions.length / 3;
  29. var uBuffer = positions.subarray(0, positionsLength);
  30. var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  31. var heightBuffer = positions.subarray(2 * positionsLength, 3 * positionsLength);
  32. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer, heightBuffer);
  33. var decoded = new Float32Array(positions.length);
  34. for (var i = 0; i < positionsLength; ++i) {
  35. var u = uBuffer[i];
  36. var v = vBuffer[i];
  37. var h = heightBuffer[i];
  38. var lon = _Math.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  39. var lat = _Math.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  40. var alt = _Math.CesiumMath.lerp(minimumHeight, maximumHeight, h / maxShort);
  41. var cartographic = Cartographic.Cartographic.fromRadians(lon, lat, alt, scratchBVCartographic);
  42. var decodedPosition = ellipsoid.cartographicToCartesian(cartographic, scratchEncodedPosition);
  43. Cartographic.Cartesian3.pack(decodedPosition, decoded, i * 3);
  44. }
  45. return decoded;
  46. }
  47. var scratchRectangle = new Cartesian2.Rectangle();
  48. var scratchEllipsoid = new Cartesian2.Ellipsoid();
  49. var scratchCenter = new Cartographic.Cartesian3();
  50. var scratchMinMaxHeights = {
  51. min : undefined,
  52. max : undefined
  53. };
  54. function unpackBuffer(packedBuffer) {
  55. packedBuffer = new Float64Array(packedBuffer);
  56. var offset = 0;
  57. scratchMinMaxHeights.min = packedBuffer[offset++];
  58. scratchMinMaxHeights.max = packedBuffer[offset++];
  59. Cartesian2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  60. offset += Cartesian2.Rectangle.packedLength;
  61. Cartesian2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  62. offset += Cartesian2.Ellipsoid.packedLength;
  63. Cartographic.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  64. }
  65. var scratchP0 = new Cartographic.Cartesian3();
  66. var scratchP1 = new Cartographic.Cartesian3();
  67. var scratchPrev = new Cartographic.Cartesian3();
  68. var scratchCur = new Cartographic.Cartesian3();
  69. var scratchNext = new Cartographic.Cartesian3();
  70. function createVectorTilePolylines(parameters, transferableObjects) {
  71. var encodedPositions = new Uint16Array(parameters.positions);
  72. var widths = new Uint16Array(parameters.widths);
  73. var counts = new Uint32Array(parameters.counts);
  74. var batchIds = new Uint16Array(parameters.batchIds);
  75. unpackBuffer(parameters.packedBuffer);
  76. var rectangle = scratchRectangle;
  77. var ellipsoid = scratchEllipsoid;
  78. var center = scratchCenter;
  79. var minimumHeight = scratchMinMaxHeights.min;
  80. var maximumHeight = scratchMinMaxHeights.max;
  81. var positions = decodePositions(encodedPositions, rectangle, minimumHeight, maximumHeight, ellipsoid);
  82. var positionsLength = positions.length / 3;
  83. var size = positionsLength * 4 - 4;
  84. var curPositions = new Float32Array(size * 3);
  85. var prevPositions = new Float32Array(size * 3);
  86. var nextPositions = new Float32Array(size * 3);
  87. var expandAndWidth = new Float32Array(size * 2);
  88. var vertexBatchIds = new Uint16Array(size);
  89. var positionIndex = 0;
  90. var expandAndWidthIndex = 0;
  91. var batchIdIndex = 0;
  92. var i;
  93. var offset = 0;
  94. var length = counts.length;
  95. for (i = 0; i < length; ++i) {
  96. var count = counts [i];
  97. var width = widths[i];
  98. var batchId = batchIds[i];
  99. for (var j = 0; j < count; ++j) {
  100. var previous;
  101. if (j === 0) {
  102. var p0 = Cartographic.Cartesian3.unpack(positions, offset * 3, scratchP0);
  103. var p1 = Cartographic.Cartesian3.unpack(positions, (offset + 1) * 3, scratchP1);
  104. previous = Cartographic.Cartesian3.subtract(p0, p1, scratchPrev);
  105. Cartographic.Cartesian3.add(p0, previous, previous);
  106. } else {
  107. previous = Cartographic.Cartesian3.unpack(positions, (offset + j - 1) * 3, scratchPrev);
  108. }
  109. var current = Cartographic.Cartesian3.unpack(positions, (offset + j) * 3, scratchCur);
  110. var next;
  111. if (j === count - 1) {
  112. var p2 = Cartographic.Cartesian3.unpack(positions, (offset + count - 1) * 3, scratchP0);
  113. var p3 = Cartographic.Cartesian3.unpack(positions, (offset + count - 2) * 3, scratchP1);
  114. next = Cartographic.Cartesian3.subtract(p2, p3, scratchNext);
  115. Cartographic.Cartesian3.add(p2, next, next);
  116. } else {
  117. next = Cartographic.Cartesian3.unpack(positions, (offset + j + 1) * 3, scratchNext);
  118. }
  119. Cartographic.Cartesian3.subtract(previous, center, previous);
  120. Cartographic.Cartesian3.subtract(current, center, current);
  121. Cartographic.Cartesian3.subtract(next, center, next);
  122. var startK = j === 0 ? 2 : 0;
  123. var endK = j === count - 1 ? 2 : 4;
  124. for (var k = startK; k < endK; ++k) {
  125. Cartographic.Cartesian3.pack(current, curPositions, positionIndex);
  126. Cartographic.Cartesian3.pack(previous, prevPositions, positionIndex);
  127. Cartographic.Cartesian3.pack(next, nextPositions, positionIndex);
  128. positionIndex += 3;
  129. var direction = (k - 2 < 0) ? -1.0 : 1.0;
  130. expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1;
  131. expandAndWidth[expandAndWidthIndex++] = direction * width;
  132. vertexBatchIds[batchIdIndex++] = batchId;
  133. }
  134. }
  135. offset += count;
  136. }
  137. var indices = IndexDatatype.IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
  138. var index = 0;
  139. var indicesIndex = 0;
  140. length = positionsLength - 1;
  141. for (i = 0; i < length; ++i) {
  142. indices[indicesIndex++] = index;
  143. indices[indicesIndex++] = index + 2;
  144. indices[indicesIndex++] = index + 1;
  145. indices[indicesIndex++] = index + 1;
  146. indices[indicesIndex++] = index + 2;
  147. indices[indicesIndex++] = index + 3;
  148. index += 4;
  149. }
  150. transferableObjects.push(curPositions.buffer, prevPositions.buffer, nextPositions.buffer);
  151. transferableObjects.push(expandAndWidth.buffer, vertexBatchIds.buffer, indices.buffer);
  152. return {
  153. indexDatatype : (indices.BYTES_PER_ELEMENT === 2) ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT : IndexDatatype.IndexDatatype.UNSIGNED_INT,
  154. currentPositions : curPositions.buffer,
  155. previousPositions : prevPositions.buffer,
  156. nextPositions : nextPositions.buffer,
  157. expandAndWidth : expandAndWidth.buffer,
  158. batchIds : vertexBatchIds.buffer,
  159. indices : indices.buffer
  160. };
  161. }
  162. var createVectorTilePolylines$1 = createTaskProcessorWorker(createVectorTilePolylines);
  163. return createVectorTilePolylines$1;
  164. });