SimplePolylineGeometry.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import ArcType from './ArcType.js';
  2. import BoundingSphere from './BoundingSphere.js';
  3. import Cartesian3 from './Cartesian3.js';
  4. import Color from './Color.js';
  5. import ComponentDatatype from './ComponentDatatype.js';
  6. import defaultValue from './defaultValue.js';
  7. import defined from './defined.js';
  8. import DeveloperError from './DeveloperError.js';
  9. import Ellipsoid from './Ellipsoid.js';
  10. import Geometry from './Geometry.js';
  11. import GeometryAttribute from './GeometryAttribute.js';
  12. import GeometryAttributes from './GeometryAttributes.js';
  13. import IndexDatatype from './IndexDatatype.js';
  14. import CesiumMath from './Math.js';
  15. import PolylinePipeline from './PolylinePipeline.js';
  16. import PrimitiveType from './PrimitiveType.js';
  17. function interpolateColors(p0, p1, color0, color1, minDistance, array, offset) {
  18. var numPoints = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
  19. var i;
  20. var r0 = color0.red;
  21. var g0 = color0.green;
  22. var b0 = color0.blue;
  23. var a0 = color0.alpha;
  24. var r1 = color1.red;
  25. var g1 = color1.green;
  26. var b1 = color1.blue;
  27. var a1 = color1.alpha;
  28. if (Color.equals(color0, color1)) {
  29. for (i = 0; i < numPoints; i++) {
  30. array[offset++] = Color.floatToByte(r0);
  31. array[offset++] = Color.floatToByte(g0);
  32. array[offset++] = Color.floatToByte(b0);
  33. array[offset++] = Color.floatToByte(a0);
  34. }
  35. return offset;
  36. }
  37. var redPerVertex = (r1 - r0) / numPoints;
  38. var greenPerVertex = (g1 - g0) / numPoints;
  39. var bluePerVertex = (b1 - b0) / numPoints;
  40. var alphaPerVertex = (a1 - a0) / numPoints;
  41. var index = offset;
  42. for (i = 0; i < numPoints; i++) {
  43. array[index++] = Color.floatToByte(r0 + i * redPerVertex);
  44. array[index++] = Color.floatToByte(g0 + i * greenPerVertex);
  45. array[index++] = Color.floatToByte(b0 + i * bluePerVertex);
  46. array[index++] = Color.floatToByte(a0 + i * alphaPerVertex);
  47. }
  48. return index;
  49. }
  50. /**
  51. * A description of a polyline modeled as a line strip; the first two positions define a line segment,
  52. * and each additional position defines a line segment from the previous position.
  53. *
  54. * @alias SimplePolylineGeometry
  55. * @constructor
  56. *
  57. * @param {Object} options Object with the following properties:
  58. * @param {Cartesian3[]} options.positions An array of {@link Cartesian3} defining the positions in the polyline as a line strip.
  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 {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  64. *
  65. * @exception {DeveloperError} At least two positions are required.
  66. * @exception {DeveloperError} colors has an invalid length.
  67. *
  68. * @see SimplePolylineGeometry#createGeometry
  69. *
  70. * @example
  71. * // A polyline with two connected line segments
  72. * var polyline = new Cesium.SimplePolylineGeometry({
  73. * positions : Cesium.Cartesian3.fromDegreesArray([
  74. * 0.0, 0.0,
  75. * 5.0, 0.0,
  76. * 5.0, 5.0
  77. * ])
  78. * });
  79. * var geometry = Cesium.SimplePolylineGeometry.createGeometry(polyline);
  80. */
  81. function SimplePolylineGeometry(options) {
  82. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  83. var positions = options.positions;
  84. var colors = options.colors;
  85. var colorsPerVertex = defaultValue(options.colorsPerVertex, false);
  86. //>>includeStart('debug', pragmas.debug);
  87. if ((!defined(positions)) || (positions.length < 2)) {
  88. throw new DeveloperError('At least two positions are required.');
  89. }
  90. if (defined(colors) && ((colorsPerVertex && colors.length < positions.length) || (!colorsPerVertex && colors.length < positions.length - 1))) {
  91. throw new DeveloperError('colors has an invalid length.');
  92. }
  93. //>>includeEnd('debug');
  94. this._positions = positions;
  95. this._colors = colors;
  96. this._colorsPerVertex = colorsPerVertex;
  97. this._arcType = defaultValue(options.arcType, ArcType.GEODESIC);
  98. this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  99. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  100. this._workerName = 'createSimplePolylineGeometry';
  101. var numComponents = 1 + positions.length * Cartesian3.packedLength;
  102. numComponents += defined(colors) ? 1 + colors.length * Color.packedLength : 1;
  103. /**
  104. * The number of elements used to pack the object into an array.
  105. * @type {Number}
  106. */
  107. this.packedLength = numComponents + Ellipsoid.packedLength + 3;
  108. }
  109. /**
  110. * Stores the provided instance into the provided array.
  111. *
  112. * @param {SimplePolylineGeometry} value The value to pack.
  113. * @param {Number[]} array The array to pack into.
  114. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  115. *
  116. * @returns {Number[]} The array that was packed into
  117. */
  118. SimplePolylineGeometry.pack = function(value, array, startingIndex) {
  119. //>>includeStart('debug', pragmas.debug);
  120. if (!defined(value)) {
  121. throw new DeveloperError('value is required');
  122. }
  123. if (!defined(array)) {
  124. throw new DeveloperError('array is required');
  125. }
  126. //>>includeEnd('debug');
  127. startingIndex = defaultValue(startingIndex, 0);
  128. var i;
  129. var positions = value._positions;
  130. var length = positions.length;
  131. array[startingIndex++] = length;
  132. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  133. Cartesian3.pack(positions[i], array, startingIndex);
  134. }
  135. var colors = value._colors;
  136. length = defined(colors) ? colors.length : 0.0;
  137. array[startingIndex++] = length;
  138. for (i = 0; i < length; ++i, startingIndex += Color.packedLength) {
  139. Color.pack(colors[i], array, startingIndex);
  140. }
  141. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  142. startingIndex += Ellipsoid.packedLength;
  143. array[startingIndex++] = value._colorsPerVertex ? 1.0 : 0.0;
  144. array[startingIndex++] = value._arcType;
  145. array[startingIndex] = value._granularity;
  146. return array;
  147. };
  148. /**
  149. * Retrieves an instance from a packed array.
  150. *
  151. * @param {Number[]} array The packed array.
  152. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  153. * @param {SimplePolylineGeometry} [result] The object into which to store the result.
  154. * @returns {SimplePolylineGeometry} The modified result parameter or a new SimplePolylineGeometry instance if one was not provided.
  155. */
  156. SimplePolylineGeometry.unpack = function(array, startingIndex, result) {
  157. //>>includeStart('debug', pragmas.debug);
  158. if (!defined(array)) {
  159. throw new DeveloperError('array is required');
  160. }
  161. //>>includeEnd('debug');
  162. startingIndex = defaultValue(startingIndex, 0);
  163. var i;
  164. var length = array[startingIndex++];
  165. var positions = new Array(length);
  166. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  167. positions[i] = Cartesian3.unpack(array, startingIndex);
  168. }
  169. length = array[startingIndex++];
  170. var colors = length > 0 ? new Array(length) : undefined;
  171. for (i = 0; i < length; ++i, startingIndex += Color.packedLength) {
  172. colors[i] = Color.unpack(array, startingIndex);
  173. }
  174. var ellipsoid = Ellipsoid.unpack(array, startingIndex);
  175. startingIndex += Ellipsoid.packedLength;
  176. var colorsPerVertex = array[startingIndex++] === 1.0;
  177. var arcType = array[startingIndex++];
  178. var granularity = array[startingIndex];
  179. if (!defined(result)) {
  180. return new SimplePolylineGeometry({
  181. positions : positions,
  182. colors : colors,
  183. ellipsoid : ellipsoid,
  184. colorsPerVertex : colorsPerVertex,
  185. arcType : arcType,
  186. granularity : granularity
  187. });
  188. }
  189. result._positions = positions;
  190. result._colors = colors;
  191. result._ellipsoid = ellipsoid;
  192. result._colorsPerVertex = colorsPerVertex;
  193. result._arcType = arcType;
  194. result._granularity = granularity;
  195. return result;
  196. };
  197. var scratchArray1 = new Array(2);
  198. var scratchArray2 = new Array(2);
  199. var generateArcOptionsScratch = {
  200. positions : scratchArray1,
  201. height: scratchArray2,
  202. ellipsoid: undefined,
  203. minDistance : undefined,
  204. granularity : undefined
  205. };
  206. /**
  207. * Computes the geometric representation of a simple polyline, including its vertices, indices, and a bounding sphere.
  208. *
  209. * @param {SimplePolylineGeometry} simplePolylineGeometry A description of the polyline.
  210. * @returns {Geometry} The computed vertices and indices.
  211. */
  212. SimplePolylineGeometry.createGeometry = function(simplePolylineGeometry) {
  213. var positions = simplePolylineGeometry._positions;
  214. var colors = simplePolylineGeometry._colors;
  215. var colorsPerVertex = simplePolylineGeometry._colorsPerVertex;
  216. var arcType = simplePolylineGeometry._arcType;
  217. var granularity = simplePolylineGeometry._granularity;
  218. var ellipsoid = simplePolylineGeometry._ellipsoid;
  219. var minDistance = CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  220. var perSegmentColors = defined(colors) && !colorsPerVertex;
  221. var i;
  222. var length = positions.length;
  223. var positionValues;
  224. var numberOfPositions;
  225. var colorValues;
  226. var color;
  227. var offset = 0;
  228. if (arcType === ArcType.GEODESIC || arcType === ArcType.RHUMB) {
  229. var subdivisionSize;
  230. var numberOfPointsFunction;
  231. var generateArcFunction;
  232. if (arcType === ArcType.GEODESIC) {
  233. subdivisionSize = CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  234. numberOfPointsFunction = PolylinePipeline.numberOfPoints;
  235. generateArcFunction = PolylinePipeline.generateArc;
  236. } else {
  237. subdivisionSize = granularity;
  238. numberOfPointsFunction = PolylinePipeline.numberOfPointsRhumbLine;
  239. generateArcFunction = PolylinePipeline.generateRhumbArc;
  240. }
  241. var heights = PolylinePipeline.extractHeights(positions, ellipsoid);
  242. var generateArcOptions = generateArcOptionsScratch;
  243. if (arcType === ArcType.GEODESIC) {
  244. generateArcOptions.minDistance = minDistance;
  245. } else {
  246. generateArcOptions.granularity = granularity;
  247. }
  248. generateArcOptions.ellipsoid = ellipsoid;
  249. if (perSegmentColors) {
  250. var positionCount = 0;
  251. for (i = 0; i < length - 1; i++) {
  252. positionCount += numberOfPointsFunction(positions[i], positions[i+1], subdivisionSize) + 1;
  253. }
  254. positionValues = new Float64Array(positionCount * 3);
  255. colorValues = new Uint8Array(positionCount * 4);
  256. generateArcOptions.positions = scratchArray1;
  257. generateArcOptions.height= scratchArray2;
  258. var ci = 0;
  259. for (i = 0; i < length - 1; ++i) {
  260. scratchArray1[0] = positions[i];
  261. scratchArray1[1] = positions[i + 1];
  262. scratchArray2[0] = heights[i];
  263. scratchArray2[1] = heights[i + 1];
  264. var pos = generateArcFunction(generateArcOptions);
  265. if (defined(colors)) {
  266. var segLen = pos.length / 3;
  267. color = colors[i];
  268. for(var k = 0; k < segLen; ++k) {
  269. colorValues[ci++] = Color.floatToByte(color.red);
  270. colorValues[ci++] = Color.floatToByte(color.green);
  271. colorValues[ci++] = Color.floatToByte(color.blue);
  272. colorValues[ci++] = Color.floatToByte(color.alpha);
  273. }
  274. }
  275. positionValues.set(pos, offset);
  276. offset += pos.length;
  277. }
  278. } else {
  279. generateArcOptions.positions = positions;
  280. generateArcOptions.height= heights;
  281. positionValues = new Float64Array(generateArcFunction(generateArcOptions));
  282. if (defined(colors)) {
  283. colorValues = new Uint8Array(positionValues.length / 3 * 4);
  284. for (i = 0; i < length - 1; ++i) {
  285. var p0 = positions[i];
  286. var p1 = positions[i + 1];
  287. var c0 = colors[i];
  288. var c1 = colors[i + 1];
  289. offset = interpolateColors(p0, p1, c0, c1, minDistance, colorValues, offset);
  290. }
  291. var lastColor = colors[length - 1];
  292. colorValues[offset++] = Color.floatToByte(lastColor.red);
  293. colorValues[offset++] = Color.floatToByte(lastColor.green);
  294. colorValues[offset++] = Color.floatToByte(lastColor.blue);
  295. colorValues[offset++] = Color.floatToByte(lastColor.alpha);
  296. }
  297. }
  298. } else {
  299. numberOfPositions = perSegmentColors ? length * 2 - 2 : length;
  300. positionValues = new Float64Array(numberOfPositions * 3);
  301. colorValues = defined(colors) ? new Uint8Array(numberOfPositions * 4) : undefined;
  302. var positionIndex = 0;
  303. var colorIndex = 0;
  304. for (i = 0; i < length; ++i) {
  305. var p = positions[i];
  306. if (perSegmentColors && i > 0) {
  307. Cartesian3.pack(p, positionValues, positionIndex);
  308. positionIndex += 3;
  309. color = colors[i - 1];
  310. colorValues[colorIndex++] = Color.floatToByte(color.red);
  311. colorValues[colorIndex++] = Color.floatToByte(color.green);
  312. colorValues[colorIndex++] = Color.floatToByte(color.blue);
  313. colorValues[colorIndex++] = Color.floatToByte(color.alpha);
  314. }
  315. if (perSegmentColors && i === length - 1) {
  316. break;
  317. }
  318. Cartesian3.pack(p, positionValues, positionIndex);
  319. positionIndex += 3;
  320. if (defined(colors)) {
  321. color = colors[i];
  322. colorValues[colorIndex++] = Color.floatToByte(color.red);
  323. colorValues[colorIndex++] = Color.floatToByte(color.green);
  324. colorValues[colorIndex++] = Color.floatToByte(color.blue);
  325. colorValues[colorIndex++] = Color.floatToByte(color.alpha);
  326. }
  327. }
  328. }
  329. var attributes = new GeometryAttributes();
  330. attributes.position = new GeometryAttribute({
  331. componentDatatype : ComponentDatatype.DOUBLE,
  332. componentsPerAttribute : 3,
  333. values : positionValues
  334. });
  335. if (defined(colors)) {
  336. attributes.color = new GeometryAttribute({
  337. componentDatatype : ComponentDatatype.UNSIGNED_BYTE,
  338. componentsPerAttribute : 4,
  339. values : colorValues,
  340. normalize : true
  341. });
  342. }
  343. numberOfPositions = positionValues.length / 3;
  344. var numberOfIndices = (numberOfPositions - 1) * 2;
  345. var indices = IndexDatatype.createTypedArray(numberOfPositions, numberOfIndices);
  346. var index = 0;
  347. for (i = 0; i < numberOfPositions - 1; ++i) {
  348. indices[index++] = i;
  349. indices[index++] = i + 1;
  350. }
  351. return new Geometry({
  352. attributes : attributes,
  353. indices : indices,
  354. primitiveType : PrimitiveType.LINES,
  355. boundingSphere : BoundingSphere.fromPoints(positions)
  356. });
  357. };
  358. export default SimplePolylineGeometry;