PolylineGeometry.js 20 KB

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