createPolylineVolumeOutlineGeometry.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './Cartesian4-5af5bb24', './RuntimeError-ba10bc3e', './WebGLConstants-4c11ee5f', './ComponentDatatype-5862616f', './GeometryAttribute-91704ebb', './PrimitiveType-97893bc7', './FeatureDetection-7bd32c34', './Transforms-b2e71640', './buildModuleUrl-14bfe498', './GeometryAttributes-aacecde6', './IndexDatatype-9435b55f', './IntersectionTests-397d9494', './Plane-8390418f', './arrayRemoveDuplicates-f0b089b1', './BoundingRectangle-dc808c42', './EllipsoidTangentPlane-a815c96f', './EllipsoidRhumbLine-f161e674', './earcut-2.2.1-b404d9e6', './PolygonPipeline-6a35d737', './PolylineVolumeGeometryLibrary-934b338a', './EllipsoidGeodesic-84507801', './PolylinePipeline-a9f32196'], function (when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, Cartesian4, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, PrimitiveType, FeatureDetection, Transforms, buildModuleUrl, GeometryAttributes, IndexDatatype, IntersectionTests, Plane, arrayRemoveDuplicates, BoundingRectangle, EllipsoidTangentPlane, EllipsoidRhumbLine, earcut2_2_1, PolygonPipeline, PolylineVolumeGeometryLibrary, EllipsoidGeodesic, PolylinePipeline) { 'use strict';
  24. function computeAttributes(positions, shape) {
  25. var attributes = new GeometryAttributes.GeometryAttributes();
  26. attributes.position = new GeometryAttribute.GeometryAttribute({
  27. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  28. componentsPerAttribute : 3,
  29. values : positions
  30. });
  31. var shapeLength = shape.length;
  32. var vertexCount = attributes.position.values.length / 3;
  33. var positionLength = positions.length / 3;
  34. var shapeCount = positionLength / shapeLength;
  35. var indices = IndexDatatype.IndexDatatype.createTypedArray(vertexCount, 2 * shapeLength * (shapeCount + 1));
  36. var i, j;
  37. var index = 0;
  38. i = 0;
  39. var offset = i * shapeLength;
  40. for (j = 0; j < shapeLength - 1; j++) {
  41. indices[index++] = j + offset;
  42. indices[index++] = j + offset + 1;
  43. }
  44. indices[index++] = shapeLength - 1 + offset;
  45. indices[index++] = offset;
  46. i = shapeCount - 1;
  47. offset = i * shapeLength;
  48. for (j = 0; j < shapeLength - 1; j++) {
  49. indices[index++] = j + offset;
  50. indices[index++] = j + offset + 1;
  51. }
  52. indices[index++] = shapeLength - 1 + offset;
  53. indices[index++] = offset;
  54. for (i = 0; i < shapeCount - 1; i++) {
  55. var firstOffset = shapeLength * i;
  56. var secondOffset = firstOffset + shapeLength;
  57. for (j = 0; j < shapeLength; j++) {
  58. indices[index++] = j + firstOffset;
  59. indices[index++] = j + secondOffset;
  60. }
  61. }
  62. var geometry = new GeometryAttribute.Geometry({
  63. attributes : attributes,
  64. indices : IndexDatatype.IndexDatatype.createTypedArray(vertexCount, indices),
  65. boundingSphere : BoundingSphere.BoundingSphere.fromVertices(positions),
  66. primitiveType : PrimitiveType.PrimitiveType.LINES
  67. });
  68. return geometry;
  69. }
  70. /**
  71. * A description of a polyline with a volume (a 2D shape extruded along a polyline).
  72. *
  73. * @alias PolylineVolumeOutlineGeometry
  74. * @constructor
  75. *
  76. * @param {Object} options Object with the following properties:
  77. * @param {Cartesian3[]} options.polylinePositions An array of positions that define the center of the polyline volume.
  78. * @param {Cartesian2[]} options.shapePositions An array of positions that define the shape to be extruded along the polyline
  79. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  80. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  81. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  82. *
  83. * @see PolylineVolumeOutlineGeometry#createGeometry
  84. *
  85. * @example
  86. * function computeCircle(radius) {
  87. * var positions = [];
  88. * for (var i = 0; i < 360; i++) {
  89. * var radians = Cesium.Math.toRadians(i);
  90. * positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
  91. * }
  92. * return positions;
  93. * }
  94. *
  95. * var volumeOutline = new Cesium.PolylineVolumeOutlineGeometry({
  96. * polylinePositions : Cesium.Cartesian3.fromDegreesArray([
  97. * -72.0, 40.0,
  98. * -70.0, 35.0
  99. * ]),
  100. * shapePositions : computeCircle(100000.0)
  101. * });
  102. */
  103. function PolylineVolumeOutlineGeometry(options) {
  104. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  105. var positions = options.polylinePositions;
  106. var shape = options.shapePositions;
  107. //>>includeStart('debug', pragmas.debug);
  108. if (!when.defined(positions)) {
  109. throw new Check.DeveloperError('options.polylinePositions is required.');
  110. }
  111. if (!when.defined(shape)) {
  112. throw new Check.DeveloperError('options.shapePositions is required.');
  113. }
  114. //>>includeEnd('debug');
  115. this._positions = positions;
  116. this._shape = shape;
  117. this._ellipsoid = Cartesian2.Ellipsoid.clone(when.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84));
  118. this._cornerType = when.defaultValue(options.cornerType, PolylineVolumeGeometryLibrary.CornerType.ROUNDED);
  119. this._granularity = when.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  120. this._workerName = 'createPolylineVolumeOutlineGeometry';
  121. var numComponents = 1 + positions.length * Cartographic.Cartesian3.packedLength;
  122. numComponents += 1 + shape.length * Cartesian2.Cartesian2.packedLength;
  123. /**
  124. * The number of elements used to pack the object into an array.
  125. * @type {Number}
  126. */
  127. this.packedLength = numComponents + Cartesian2.Ellipsoid.packedLength + 2;
  128. }
  129. /**
  130. * Stores the provided instance into the provided array.
  131. *
  132. * @param {PolylineVolumeOutlineGeometry} value The value to pack.
  133. * @param {Number[]} array The array to pack into.
  134. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  135. *
  136. * @returns {Number[]} The array that was packed into
  137. */
  138. PolylineVolumeOutlineGeometry.pack = function(value, array, startingIndex) {
  139. //>>includeStart('debug', pragmas.debug);
  140. if (!when.defined(value)) {
  141. throw new Check.DeveloperError('value is required');
  142. }
  143. if (!when.defined(array)) {
  144. throw new Check.DeveloperError('array is required');
  145. }
  146. //>>includeEnd('debug');
  147. startingIndex = when.defaultValue(startingIndex, 0);
  148. var i;
  149. var positions = value._positions;
  150. var length = positions.length;
  151. array[startingIndex++] = length;
  152. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  153. Cartographic.Cartesian3.pack(positions[i], array, startingIndex);
  154. }
  155. var shape = value._shape;
  156. length = shape.length;
  157. array[startingIndex++] = length;
  158. for (i = 0; i < length; ++i, startingIndex += Cartesian2.Cartesian2.packedLength) {
  159. Cartesian2.Cartesian2.pack(shape[i], array, startingIndex);
  160. }
  161. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  162. startingIndex += Cartesian2.Ellipsoid.packedLength;
  163. array[startingIndex++] = value._cornerType;
  164. array[startingIndex] = value._granularity;
  165. return array;
  166. };
  167. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  168. var scratchOptions = {
  169. polylinePositions : undefined,
  170. shapePositions : undefined,
  171. ellipsoid : scratchEllipsoid,
  172. height : undefined,
  173. cornerType : undefined,
  174. granularity : undefined
  175. };
  176. /**
  177. * Retrieves an instance from a packed array.
  178. *
  179. * @param {Number[]} array The packed array.
  180. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  181. * @param {PolylineVolumeOutlineGeometry} [result] The object into which to store the result.
  182. * @returns {PolylineVolumeOutlineGeometry} The modified result parameter or a new PolylineVolumeOutlineGeometry instance if one was not provided.
  183. */
  184. PolylineVolumeOutlineGeometry.unpack = function(array, startingIndex, result) {
  185. //>>includeStart('debug', pragmas.debug);
  186. if (!when.defined(array)) {
  187. throw new Check.DeveloperError('array is required');
  188. }
  189. //>>includeEnd('debug');
  190. startingIndex = when.defaultValue(startingIndex, 0);
  191. var i;
  192. var length = array[startingIndex++];
  193. var positions = new Array(length);
  194. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  195. positions[i] = Cartographic.Cartesian3.unpack(array, startingIndex);
  196. }
  197. length = array[startingIndex++];
  198. var shape = new Array(length);
  199. for (i = 0; i < length; ++i, startingIndex += Cartesian2.Cartesian2.packedLength) {
  200. shape[i] = Cartesian2.Cartesian2.unpack(array, startingIndex);
  201. }
  202. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  203. startingIndex += Cartesian2.Ellipsoid.packedLength;
  204. var cornerType = array[startingIndex++];
  205. var granularity = array[startingIndex];
  206. if (!when.defined(result)) {
  207. scratchOptions.polylinePositions = positions;
  208. scratchOptions.shapePositions = shape;
  209. scratchOptions.cornerType = cornerType;
  210. scratchOptions.granularity = granularity;
  211. return new PolylineVolumeOutlineGeometry(scratchOptions);
  212. }
  213. result._positions = positions;
  214. result._shape = shape;
  215. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  216. result._cornerType = cornerType;
  217. result._granularity = granularity;
  218. return result;
  219. };
  220. var brScratch = new BoundingRectangle.BoundingRectangle();
  221. /**
  222. * Computes the geometric representation of the outline of a polyline with a volume, including its vertices, indices, and a bounding sphere.
  223. *
  224. * @param {PolylineVolumeOutlineGeometry} polylineVolumeOutlineGeometry A description of the polyline volume outline.
  225. * @returns {Geometry|undefined} The computed vertices and indices.
  226. */
  227. PolylineVolumeOutlineGeometry.createGeometry = function(polylineVolumeOutlineGeometry) {
  228. var positions = polylineVolumeOutlineGeometry._positions;
  229. var cleanPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(positions, Cartographic.Cartesian3.equalsEpsilon);
  230. var shape2D = polylineVolumeOutlineGeometry._shape;
  231. shape2D = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.removeDuplicatesFromShape(shape2D);
  232. if (cleanPositions.length < 2 || shape2D.length < 3) {
  233. return undefined;
  234. }
  235. if (PolygonPipeline.PolygonPipeline.computeWindingOrder2D(shape2D) === PolygonPipeline.WindingOrder.CLOCKWISE) {
  236. shape2D.reverse();
  237. }
  238. var boundingRectangle = BoundingRectangle.BoundingRectangle.fromPoints(shape2D, brScratch);
  239. var computedPositions = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.computePositions(cleanPositions, shape2D, boundingRectangle, polylineVolumeOutlineGeometry, false);
  240. return computeAttributes(computedPositions, shape2D);
  241. };
  242. function createPolylineVolumeOutlineGeometry(polylineVolumeOutlineGeometry, offset) {
  243. if (when.defined(offset)) {
  244. polylineVolumeOutlineGeometry = PolylineVolumeOutlineGeometry.unpack(polylineVolumeOutlineGeometry, offset);
  245. }
  246. polylineVolumeOutlineGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(polylineVolumeOutlineGeometry._ellipsoid);
  247. return PolylineVolumeOutlineGeometry.createGeometry(polylineVolumeOutlineGeometry);
  248. }
  249. return createPolylineVolumeOutlineGeometry;
  250. });