createPolylineGeometry.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './GeometryAttribute-ed359d71', './when-ee12a2cb', './GeometryAttributes-eecc9f43', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './VertexFormat-fbb91dc7', './arrayRemoveDuplicates-dd708d81', './ArcType-d521909b', './EllipsoidRhumbLine-c6cdbfd3', './EllipsoidGeodesic-53e988a6', './PolylinePipeline-b4161aaf', './Color-63c0bcb4'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, IndexDatatype, IntersectionTests, Plane, VertexFormat, arrayRemoveDuplicates, ArcType, EllipsoidRhumbLine, EllipsoidGeodesic, PolylinePipeline, Color) { 'use strict';
  3. var scratchInterpolateColorsArray = [];
  4. function interpolateColors(p0, p1, color0, color1, numPoints) {
  5. var colors = scratchInterpolateColorsArray;
  6. colors.length = numPoints;
  7. var i;
  8. var r0 = color0.red;
  9. var g0 = color0.green;
  10. var b0 = color0.blue;
  11. var a0 = color0.alpha;
  12. var r1 = color1.red;
  13. var g1 = color1.green;
  14. var b1 = color1.blue;
  15. var a1 = color1.alpha;
  16. if (Color.Color.equals(color0, color1)) {
  17. for (i = 0; i < numPoints; i++) {
  18. colors[i] = Color.Color.clone(color0);
  19. }
  20. return colors;
  21. }
  22. var redPerVertex = (r1 - r0) / numPoints;
  23. var greenPerVertex = (g1 - g0) / numPoints;
  24. var bluePerVertex = (b1 - b0) / numPoints;
  25. var alphaPerVertex = (a1 - a0) / numPoints;
  26. for (i = 0; i < numPoints; i++) {
  27. colors[i] = new Color.Color(r0 + i * redPerVertex, g0 + i * greenPerVertex, b0 + i * bluePerVertex, a0 + i * alphaPerVertex);
  28. }
  29. return colors;
  30. }
  31. /**
  32. * A description of a polyline modeled as a line strip; the first two positions define a line segment,
  33. * and each additional position defines a line segment from the previous position. The polyline is capable of
  34. * displaying with a material.
  35. *
  36. * @alias PolylineGeometry
  37. * @constructor
  38. *
  39. * @param {Object} options Object with the following properties:
  40. * @param {Cartesian3[]} options.positions An array of {@link Cartesian3} defining the positions in the polyline as a line strip.
  41. * @param {Number} [options.width=1.0] The width in pixels.
  42. * @param {Color[]} [options.colors] An Array of {@link Color} defining the per vertex or per segment colors.
  43. * @param {Boolean} [options.colorsPerVertex=false] A boolean that determines whether the colors will be flat across each segment of the line or interpolated across the vertices.
  44. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polyline segments must follow.
  45. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude if options.arcType is not ArcType.NONE. Determines the number of positions in the buffer.
  46. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  47. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  48. *
  49. * @exception {DeveloperError} At least two positions are required.
  50. * @exception {DeveloperError} width must be greater than or equal to one.
  51. * @exception {DeveloperError} colors has an invalid length.
  52. *
  53. * @see PolylineGeometry#createGeometry
  54. *
  55. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polyline.html|Cesium Sandcastle Polyline Demo}
  56. *
  57. * @example
  58. * // A polyline with two connected line segments
  59. * var polyline = new Cesium.PolylineGeometry({
  60. * positions : Cesium.Cartesian3.fromDegreesArray([
  61. * 0.0, 0.0,
  62. * 5.0, 0.0,
  63. * 5.0, 5.0
  64. * ]),
  65. * width : 10.0
  66. * });
  67. * var geometry = Cesium.PolylineGeometry.createGeometry(polyline);
  68. */
  69. function PolylineGeometry(options) {
  70. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  71. var positions = options.positions;
  72. var colors = options.colors;
  73. var width = defaultValue.defaultValue(options.width, 1.0);
  74. var colorsPerVertex = defaultValue.defaultValue(options.colorsPerVertex, false);
  75. //>>includeStart('debug', pragmas.debug);
  76. if ((!defined.defined(positions)) || (positions.length < 2)) {
  77. throw new Check.DeveloperError('At least two positions are required.');
  78. }
  79. if (typeof width !== 'number') {
  80. throw new Check.DeveloperError('width must be a number');
  81. }
  82. if (defined.defined(colors) && ((colorsPerVertex && colors.length < positions.length) || (!colorsPerVertex && colors.length < positions.length - 1))) {
  83. throw new Check.DeveloperError('colors has an invalid length.');
  84. }
  85. //>>includeEnd('debug');
  86. this._positions = positions;
  87. this._colors = colors;
  88. this._width = width;
  89. this._colorsPerVertex = colorsPerVertex;
  90. this._vertexFormat = VertexFormat.VertexFormat.clone(defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT));
  91. this._arcType = defaultValue.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  92. this._granularity = defaultValue.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  93. this._ellipsoid = Cartesian2.Ellipsoid.clone(defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84));
  94. this._workerName = 'createPolylineGeometry';
  95. var numComponents = 1 + positions.length * Cartesian2.Cartesian3.packedLength;
  96. numComponents += defined.defined(colors) ? 1 + colors.length * Color.Color.packedLength : 1;
  97. /**
  98. * The number of elements used to pack the object into an array.
  99. * @type {Number}
  100. */
  101. this.packedLength = numComponents + Cartesian2.Ellipsoid.packedLength + VertexFormat.VertexFormat.packedLength + 4;
  102. }
  103. /**
  104. * Stores the provided instance into the provided array.
  105. *
  106. * @param {PolylineGeometry} value The value to pack.
  107. * @param {Number[]} array The array to pack into.
  108. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  109. *
  110. * @returns {Number[]} The array that was packed into
  111. */
  112. PolylineGeometry.pack = function(value, array, startingIndex) {
  113. //>>includeStart('debug', pragmas.debug);
  114. if (!defined.defined(value)) {
  115. throw new Check.DeveloperError('value is required');
  116. }
  117. if (!defined.defined(array)) {
  118. throw new Check.DeveloperError('array is required');
  119. }
  120. //>>includeEnd('debug');
  121. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  122. var i;
  123. var positions = value._positions;
  124. var length = positions.length;
  125. array[startingIndex++] = length;
  126. for (i = 0; i < length; ++i, startingIndex += Cartesian2.Cartesian3.packedLength) {
  127. Cartesian2.Cartesian3.pack(positions[i], array, startingIndex);
  128. }
  129. var colors = value._colors;
  130. length = defined.defined(colors) ? colors.length : 0.0;
  131. array[startingIndex++] = length;
  132. for (i = 0; i < length; ++i, startingIndex += Color.Color.packedLength) {
  133. Color.Color.pack(colors[i], array, startingIndex);
  134. }
  135. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  136. startingIndex += Cartesian2.Ellipsoid.packedLength;
  137. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  138. startingIndex += VertexFormat.VertexFormat.packedLength;
  139. array[startingIndex++] = value._width;
  140. array[startingIndex++] = value._colorsPerVertex ? 1.0 : 0.0;
  141. array[startingIndex++] = value._arcType;
  142. array[startingIndex] = value._granularity;
  143. return array;
  144. };
  145. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  146. var scratchVertexFormat = new VertexFormat.VertexFormat();
  147. var scratchOptions = {
  148. positions : undefined,
  149. colors : undefined,
  150. ellipsoid : scratchEllipsoid,
  151. vertexFormat : scratchVertexFormat,
  152. width : undefined,
  153. colorsPerVertex : undefined,
  154. arcType : undefined,
  155. granularity : undefined
  156. };
  157. /**
  158. * Retrieves an instance from a packed array.
  159. *
  160. * @param {Number[]} array The packed array.
  161. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  162. * @param {PolylineGeometry} [result] The object into which to store the result.
  163. * @returns {PolylineGeometry} The modified result parameter or a new PolylineGeometry instance if one was not provided.
  164. */
  165. PolylineGeometry.unpack = function(array, startingIndex, result) {
  166. //>>includeStart('debug', pragmas.debug);
  167. if (!defined.defined(array)) {
  168. throw new Check.DeveloperError('array is required');
  169. }
  170. //>>includeEnd('debug');
  171. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  172. var i;
  173. var length = array[startingIndex++];
  174. var positions = new Array(length);
  175. for (i = 0; i < length; ++i, startingIndex += Cartesian2.Cartesian3.packedLength) {
  176. positions[i] = Cartesian2.Cartesian3.unpack(array, startingIndex);
  177. }
  178. length = array[startingIndex++];
  179. var colors = length > 0 ? new Array(length) : undefined;
  180. for (i = 0; i < length; ++i, startingIndex += Color.Color.packedLength) {
  181. colors[i] = Color.Color.unpack(array, startingIndex);
  182. }
  183. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  184. startingIndex += Cartesian2.Ellipsoid.packedLength;
  185. var vertexFormat = VertexFormat.VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  186. startingIndex += VertexFormat.VertexFormat.packedLength;
  187. var width = array[startingIndex++];
  188. var colorsPerVertex = array[startingIndex++] === 1.0;
  189. var arcType = array[startingIndex++];
  190. var granularity = array[startingIndex];
  191. if (!defined.defined(result)) {
  192. scratchOptions.positions = positions;
  193. scratchOptions.colors = colors;
  194. scratchOptions.width = width;
  195. scratchOptions.colorsPerVertex = colorsPerVertex;
  196. scratchOptions.arcType = arcType;
  197. scratchOptions.granularity = granularity;
  198. return new PolylineGeometry(scratchOptions);
  199. }
  200. result._positions = positions;
  201. result._colors = colors;
  202. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  203. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  204. result._width = width;
  205. result._colorsPerVertex = colorsPerVertex;
  206. result._arcType = arcType;
  207. result._granularity = granularity;
  208. return result;
  209. };
  210. var scratchCartesian3 = new Cartesian2.Cartesian3();
  211. var scratchPosition = new Cartesian2.Cartesian3();
  212. var scratchPrevPosition = new Cartesian2.Cartesian3();
  213. var scratchNextPosition = new Cartesian2.Cartesian3();
  214. /**
  215. * Computes the geometric representation of a polyline, including its vertices, indices, and a bounding sphere.
  216. *
  217. * @param {PolylineGeometry} polylineGeometry A description of the polyline.
  218. * @returns {Geometry|undefined} The computed vertices and indices.
  219. */
  220. PolylineGeometry.createGeometry = function(polylineGeometry) {
  221. var width = polylineGeometry._width;
  222. var vertexFormat = polylineGeometry._vertexFormat;
  223. var colors = polylineGeometry._colors;
  224. var colorsPerVertex = polylineGeometry._colorsPerVertex;
  225. var arcType = polylineGeometry._arcType;
  226. var granularity = polylineGeometry._granularity;
  227. var ellipsoid = polylineGeometry._ellipsoid;
  228. var i;
  229. var j;
  230. var k;
  231. var positions = arrayRemoveDuplicates.arrayRemoveDuplicates(polylineGeometry._positions, Cartesian2.Cartesian3.equalsEpsilon);
  232. var positionsLength = positions.length;
  233. // A width of a pixel or less is not a valid geometry, but in order to support external data
  234. // that may have errors we treat this as an empty geometry.
  235. if (positionsLength < 2 || width <= 0.0) {
  236. return undefined;
  237. }
  238. if (arcType === ArcType.ArcType.GEODESIC || arcType === ArcType.ArcType.RHUMB) {
  239. var subdivisionSize;
  240. var numberOfPointsFunction;
  241. if (arcType === ArcType.ArcType.GEODESIC) {
  242. subdivisionSize = _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  243. numberOfPointsFunction = PolylinePipeline.PolylinePipeline.numberOfPoints;
  244. } else {
  245. subdivisionSize = granularity;
  246. numberOfPointsFunction = PolylinePipeline.PolylinePipeline.numberOfPointsRhumbLine;
  247. }
  248. var heights = PolylinePipeline.PolylinePipeline.extractHeights(positions, ellipsoid);
  249. if (defined.defined(colors)) {
  250. var colorLength = 1;
  251. for (i = 0; i < positionsLength - 1; ++i) {
  252. colorLength += numberOfPointsFunction(positions[i], positions[i + 1], subdivisionSize);
  253. }
  254. var newColors = new Array(colorLength);
  255. var newColorIndex = 0;
  256. for (i = 0; i < positionsLength - 1; ++i) {
  257. var p0 = positions[i];
  258. var p1 = positions[i + 1];
  259. var c0 = colors[i];
  260. var numColors = numberOfPointsFunction(p0, p1, subdivisionSize);
  261. if (colorsPerVertex && i < colorLength) {
  262. var c1 = colors[i + 1];
  263. var interpolatedColors = interpolateColors(p0, p1, c0, c1, numColors);
  264. var interpolatedColorsLength = interpolatedColors.length;
  265. for (j = 0; j < interpolatedColorsLength; ++j) {
  266. newColors[newColorIndex++] = interpolatedColors[j];
  267. }
  268. } else {
  269. for (j = 0; j < numColors; ++j) {
  270. newColors[newColorIndex++] = Color.Color.clone(c0);
  271. }
  272. }
  273. }
  274. newColors[newColorIndex] = Color.Color.clone(colors[colors.length - 1]);
  275. colors = newColors;
  276. scratchInterpolateColorsArray.length = 0;
  277. }
  278. if (arcType === ArcType.ArcType.GEODESIC) {
  279. positions = PolylinePipeline.PolylinePipeline.generateCartesianArc({
  280. positions: positions,
  281. minDistance: subdivisionSize,
  282. ellipsoid: ellipsoid,
  283. height: heights
  284. });
  285. } else {
  286. positions = PolylinePipeline.PolylinePipeline.generateCartesianRhumbArc({
  287. positions: positions,
  288. granularity: subdivisionSize,
  289. ellipsoid: ellipsoid,
  290. height: heights
  291. });
  292. }
  293. }
  294. positionsLength = positions.length;
  295. var size = positionsLength * 4.0 - 4.0;
  296. var finalPositions = new Float64Array(size * 3);
  297. var prevPositions = new Float64Array(size * 3);
  298. var nextPositions = new Float64Array(size * 3);
  299. var expandAndWidth = new Float32Array(size * 2);
  300. var st = vertexFormat.st ? new Float32Array(size * 2) : undefined;
  301. var finalColors = defined.defined(colors) ? new Uint8Array(size * 4) : undefined;
  302. var positionIndex = 0;
  303. var expandAndWidthIndex = 0;
  304. var stIndex = 0;
  305. var colorIndex = 0;
  306. var position;
  307. for (j = 0; j < positionsLength; ++j) {
  308. if (j === 0) {
  309. position = scratchCartesian3;
  310. Cartesian2.Cartesian3.subtract(positions[0], positions[1], position);
  311. Cartesian2.Cartesian3.add(positions[0], position, position);
  312. } else {
  313. position = positions[j - 1];
  314. }
  315. Cartesian2.Cartesian3.clone(position, scratchPrevPosition);
  316. Cartesian2.Cartesian3.clone(positions[j], scratchPosition);
  317. if (j === positionsLength - 1) {
  318. position = scratchCartesian3;
  319. Cartesian2.Cartesian3.subtract(positions[positionsLength - 1], positions[positionsLength - 2], position);
  320. Cartesian2.Cartesian3.add(positions[positionsLength - 1], position, position);
  321. } else {
  322. position = positions[j + 1];
  323. }
  324. Cartesian2.Cartesian3.clone(position, scratchNextPosition);
  325. var color0, color1;
  326. if (defined.defined(finalColors)) {
  327. if (j !== 0 && !colorsPerVertex) {
  328. color0 = colors[j - 1];
  329. } else {
  330. color0 = colors[j];
  331. }
  332. if (j !== positionsLength - 1) {
  333. color1 = colors[j];
  334. }
  335. }
  336. var startK = j === 0 ? 2 : 0;
  337. var endK = j === positionsLength - 1 ? 2 : 4;
  338. for (k = startK; k < endK; ++k) {
  339. Cartesian2.Cartesian3.pack(scratchPosition, finalPositions, positionIndex);
  340. Cartesian2.Cartesian3.pack(scratchPrevPosition, prevPositions, positionIndex);
  341. Cartesian2.Cartesian3.pack(scratchNextPosition, nextPositions, positionIndex);
  342. positionIndex += 3;
  343. var direction = (k - 2 < 0) ? -1.0 : 1.0;
  344. expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1; // expand direction
  345. expandAndWidth[expandAndWidthIndex++] = direction * width;
  346. if (vertexFormat.st) {
  347. st[stIndex++] = j / (positionsLength - 1);
  348. st[stIndex++] = Math.max(expandAndWidth[expandAndWidthIndex - 2], 0.0);
  349. }
  350. if (defined.defined(finalColors)) {
  351. var color = (k < 2) ? color0 : color1;
  352. finalColors[colorIndex++] = Color.Color.floatToByte(color.red);
  353. finalColors[colorIndex++] = Color.Color.floatToByte(color.green);
  354. finalColors[colorIndex++] = Color.Color.floatToByte(color.blue);
  355. finalColors[colorIndex++] = Color.Color.floatToByte(color.alpha);
  356. }
  357. }
  358. }
  359. var attributes = new GeometryAttributes.GeometryAttributes();
  360. attributes.position = new GeometryAttribute.GeometryAttribute({
  361. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  362. componentsPerAttribute : 3,
  363. values : finalPositions
  364. });
  365. attributes.prevPosition = new GeometryAttribute.GeometryAttribute({
  366. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  367. componentsPerAttribute : 3,
  368. values : prevPositions
  369. });
  370. attributes.nextPosition = new GeometryAttribute.GeometryAttribute({
  371. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  372. componentsPerAttribute : 3,
  373. values : nextPositions
  374. });
  375. attributes.expandAndWidth = new GeometryAttribute.GeometryAttribute({
  376. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  377. componentsPerAttribute : 2,
  378. values : expandAndWidth
  379. });
  380. if (vertexFormat.st) {
  381. attributes.st = new GeometryAttribute.GeometryAttribute({
  382. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  383. componentsPerAttribute : 2,
  384. values : st
  385. });
  386. }
  387. if (defined.defined(finalColors)) {
  388. attributes.color = new GeometryAttribute.GeometryAttribute({
  389. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  390. componentsPerAttribute : 4,
  391. values : finalColors,
  392. normalize : true
  393. });
  394. }
  395. var indices = IndexDatatype.IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
  396. var index = 0;
  397. var indicesIndex = 0;
  398. var length = positionsLength - 1.0;
  399. for (j = 0; j < length; ++j) {
  400. indices[indicesIndex++] = index;
  401. indices[indicesIndex++] = index + 2;
  402. indices[indicesIndex++] = index + 1;
  403. indices[indicesIndex++] = index + 1;
  404. indices[indicesIndex++] = index + 2;
  405. indices[indicesIndex++] = index + 3;
  406. index += 4;
  407. }
  408. return new GeometryAttribute.Geometry({
  409. attributes : attributes,
  410. indices : indices,
  411. primitiveType : GeometryAttribute.PrimitiveType.TRIANGLES,
  412. boundingSphere : Transforms.BoundingSphere.fromPoints(positions),
  413. geometryType : GeometryAttribute.GeometryType.POLYLINES
  414. });
  415. };
  416. function createPolylineGeometry(polylineGeometry, offset) {
  417. if (defined.defined(offset)) {
  418. polylineGeometry = PolylineGeometry.unpack(polylineGeometry, offset);
  419. }
  420. polylineGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(polylineGeometry._ellipsoid);
  421. return PolylineGeometry.createGeometry(polylineGeometry);
  422. }
  423. return createPolylineGeometry;
  424. });