createPolylineGeometry.js 20 KB

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