SimplePolylineGeometry.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. const numPoints = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
  19. let i;
  20. const r0 = color0.red;
  21. const g0 = color0.green;
  22. const b0 = color0.blue;
  23. const a0 = color0.alpha;
  24. const r1 = color1.red;
  25. const g1 = color1.green;
  26. const b1 = color1.blue;
  27. const 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. const redPerVertex = (r1 - r0) / numPoints;
  38. const greenPerVertex = (g1 - g0) / numPoints;
  39. const bluePerVertex = (b1 - b0) / numPoints;
  40. const alphaPerVertex = (a1 - a0) / numPoints;
  41. let 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. * const 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. * const geometry = Cesium.SimplePolylineGeometry.createGeometry(polyline);
  80. */
  81. function SimplePolylineGeometry(options) {
  82. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  83. const positions = options.positions;
  84. const colors = options.colors;
  85. const 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 (
  91. defined(colors) &&
  92. ((colorsPerVertex && colors.length < positions.length) ||
  93. (!colorsPerVertex && colors.length < positions.length - 1))
  94. ) {
  95. throw new DeveloperError("colors has an invalid length.");
  96. }
  97. //>>includeEnd('debug');
  98. this._positions = positions;
  99. this._colors = colors;
  100. this._colorsPerVertex = colorsPerVertex;
  101. this._arcType = defaultValue(options.arcType, ArcType.GEODESIC);
  102. this._granularity = defaultValue(
  103. options.granularity,
  104. CesiumMath.RADIANS_PER_DEGREE
  105. );
  106. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  107. this._workerName = "createSimplePolylineGeometry";
  108. let numComponents = 1 + positions.length * Cartesian3.packedLength;
  109. numComponents += defined(colors) ? 1 + colors.length * Color.packedLength : 1;
  110. /**
  111. * The number of elements used to pack the object into an array.
  112. * @type {number}
  113. */
  114. this.packedLength = numComponents + Ellipsoid.packedLength + 3;
  115. }
  116. /**
  117. * Stores the provided instance into the provided array.
  118. *
  119. * @param {SimplePolylineGeometry} value The value to pack.
  120. * @param {number[]} array The array to pack into.
  121. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  122. *
  123. * @returns {number[]} The array that was packed into
  124. */
  125. SimplePolylineGeometry.pack = function (value, array, startingIndex) {
  126. //>>includeStart('debug', pragmas.debug);
  127. if (!defined(value)) {
  128. throw new DeveloperError("value is required");
  129. }
  130. if (!defined(array)) {
  131. throw new DeveloperError("array is required");
  132. }
  133. //>>includeEnd('debug');
  134. startingIndex = defaultValue(startingIndex, 0);
  135. let i;
  136. const positions = value._positions;
  137. let length = positions.length;
  138. array[startingIndex++] = length;
  139. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  140. Cartesian3.pack(positions[i], array, startingIndex);
  141. }
  142. const colors = value._colors;
  143. length = defined(colors) ? colors.length : 0.0;
  144. array[startingIndex++] = length;
  145. for (i = 0; i < length; ++i, startingIndex += Color.packedLength) {
  146. Color.pack(colors[i], array, startingIndex);
  147. }
  148. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  149. startingIndex += Ellipsoid.packedLength;
  150. array[startingIndex++] = value._colorsPerVertex ? 1.0 : 0.0;
  151. array[startingIndex++] = value._arcType;
  152. array[startingIndex] = value._granularity;
  153. return array;
  154. };
  155. /**
  156. * Retrieves an instance from a packed array.
  157. *
  158. * @param {number[]} array The packed array.
  159. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  160. * @param {SimplePolylineGeometry} [result] The object into which to store the result.
  161. * @returns {SimplePolylineGeometry} The modified result parameter or a new SimplePolylineGeometry instance if one was not provided.
  162. */
  163. SimplePolylineGeometry.unpack = function (array, startingIndex, result) {
  164. //>>includeStart('debug', pragmas.debug);
  165. if (!defined(array)) {
  166. throw new DeveloperError("array is required");
  167. }
  168. //>>includeEnd('debug');
  169. startingIndex = defaultValue(startingIndex, 0);
  170. let i;
  171. let length = array[startingIndex++];
  172. const positions = new Array(length);
  173. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  174. positions[i] = Cartesian3.unpack(array, startingIndex);
  175. }
  176. length = array[startingIndex++];
  177. const colors = length > 0 ? new Array(length) : undefined;
  178. for (i = 0; i < length; ++i, startingIndex += Color.packedLength) {
  179. colors[i] = Color.unpack(array, startingIndex);
  180. }
  181. const ellipsoid = Ellipsoid.unpack(array, startingIndex);
  182. startingIndex += Ellipsoid.packedLength;
  183. const colorsPerVertex = array[startingIndex++] === 1.0;
  184. const arcType = array[startingIndex++];
  185. const granularity = array[startingIndex];
  186. if (!defined(result)) {
  187. return new SimplePolylineGeometry({
  188. positions: positions,
  189. colors: colors,
  190. ellipsoid: ellipsoid,
  191. colorsPerVertex: colorsPerVertex,
  192. arcType: arcType,
  193. granularity: granularity,
  194. });
  195. }
  196. result._positions = positions;
  197. result._colors = colors;
  198. result._ellipsoid = ellipsoid;
  199. result._colorsPerVertex = colorsPerVertex;
  200. result._arcType = arcType;
  201. result._granularity = granularity;
  202. return result;
  203. };
  204. const scratchArray1 = new Array(2);
  205. const scratchArray2 = new Array(2);
  206. const generateArcOptionsScratch = {
  207. positions: scratchArray1,
  208. height: scratchArray2,
  209. ellipsoid: undefined,
  210. minDistance: undefined,
  211. granularity: undefined,
  212. };
  213. /**
  214. * Computes the geometric representation of a simple polyline, including its vertices, indices, and a bounding sphere.
  215. *
  216. * @param {SimplePolylineGeometry} simplePolylineGeometry A description of the polyline.
  217. * @returns {Geometry|undefined} The computed vertices and indices.
  218. */
  219. SimplePolylineGeometry.createGeometry = function (simplePolylineGeometry) {
  220. const positions = simplePolylineGeometry._positions;
  221. const colors = simplePolylineGeometry._colors;
  222. const colorsPerVertex = simplePolylineGeometry._colorsPerVertex;
  223. const arcType = simplePolylineGeometry._arcType;
  224. const granularity = simplePolylineGeometry._granularity;
  225. const ellipsoid = simplePolylineGeometry._ellipsoid;
  226. const minDistance = CesiumMath.chordLength(
  227. granularity,
  228. ellipsoid.maximumRadius
  229. );
  230. const perSegmentColors = defined(colors) && !colorsPerVertex;
  231. let i;
  232. const length = positions.length;
  233. let positionValues;
  234. let numberOfPositions;
  235. let colorValues;
  236. let color;
  237. let offset = 0;
  238. if (arcType === ArcType.GEODESIC || arcType === ArcType.RHUMB) {
  239. let subdivisionSize;
  240. let numberOfPointsFunction;
  241. let generateArcFunction;
  242. if (arcType === ArcType.GEODESIC) {
  243. subdivisionSize = CesiumMath.chordLength(
  244. granularity,
  245. ellipsoid.maximumRadius
  246. );
  247. numberOfPointsFunction = PolylinePipeline.numberOfPoints;
  248. generateArcFunction = PolylinePipeline.generateArc;
  249. } else {
  250. subdivisionSize = granularity;
  251. numberOfPointsFunction = PolylinePipeline.numberOfPointsRhumbLine;
  252. generateArcFunction = PolylinePipeline.generateRhumbArc;
  253. }
  254. const heights = PolylinePipeline.extractHeights(positions, ellipsoid);
  255. const generateArcOptions = generateArcOptionsScratch;
  256. if (arcType === ArcType.GEODESIC) {
  257. generateArcOptions.minDistance = minDistance;
  258. } else {
  259. generateArcOptions.granularity = granularity;
  260. }
  261. generateArcOptions.ellipsoid = ellipsoid;
  262. if (perSegmentColors) {
  263. let positionCount = 0;
  264. for (i = 0; i < length - 1; i++) {
  265. positionCount +=
  266. numberOfPointsFunction(
  267. positions[i],
  268. positions[i + 1],
  269. subdivisionSize
  270. ) + 1;
  271. }
  272. positionValues = new Float64Array(positionCount * 3);
  273. colorValues = new Uint8Array(positionCount * 4);
  274. generateArcOptions.positions = scratchArray1;
  275. generateArcOptions.height = scratchArray2;
  276. let ci = 0;
  277. for (i = 0; i < length - 1; ++i) {
  278. scratchArray1[0] = positions[i];
  279. scratchArray1[1] = positions[i + 1];
  280. scratchArray2[0] = heights[i];
  281. scratchArray2[1] = heights[i + 1];
  282. const pos = generateArcFunction(generateArcOptions);
  283. if (defined(colors)) {
  284. const segLen = pos.length / 3;
  285. color = colors[i];
  286. for (let k = 0; k < segLen; ++k) {
  287. colorValues[ci++] = Color.floatToByte(color.red);
  288. colorValues[ci++] = Color.floatToByte(color.green);
  289. colorValues[ci++] = Color.floatToByte(color.blue);
  290. colorValues[ci++] = Color.floatToByte(color.alpha);
  291. }
  292. }
  293. positionValues.set(pos, offset);
  294. offset += pos.length;
  295. }
  296. } else {
  297. generateArcOptions.positions = positions;
  298. generateArcOptions.height = heights;
  299. positionValues = new Float64Array(
  300. generateArcFunction(generateArcOptions)
  301. );
  302. if (defined(colors)) {
  303. colorValues = new Uint8Array((positionValues.length / 3) * 4);
  304. for (i = 0; i < length - 1; ++i) {
  305. const p0 = positions[i];
  306. const p1 = positions[i + 1];
  307. const c0 = colors[i];
  308. const c1 = colors[i + 1];
  309. offset = interpolateColors(
  310. p0,
  311. p1,
  312. c0,
  313. c1,
  314. minDistance,
  315. colorValues,
  316. offset
  317. );
  318. }
  319. const lastColor = colors[length - 1];
  320. colorValues[offset++] = Color.floatToByte(lastColor.red);
  321. colorValues[offset++] = Color.floatToByte(lastColor.green);
  322. colorValues[offset++] = Color.floatToByte(lastColor.blue);
  323. colorValues[offset++] = Color.floatToByte(lastColor.alpha);
  324. }
  325. }
  326. } else {
  327. numberOfPositions = perSegmentColors ? length * 2 - 2 : length;
  328. positionValues = new Float64Array(numberOfPositions * 3);
  329. colorValues = defined(colors)
  330. ? new Uint8Array(numberOfPositions * 4)
  331. : undefined;
  332. let positionIndex = 0;
  333. let colorIndex = 0;
  334. for (i = 0; i < length; ++i) {
  335. const p = positions[i];
  336. if (perSegmentColors && i > 0) {
  337. Cartesian3.pack(p, positionValues, positionIndex);
  338. positionIndex += 3;
  339. color = colors[i - 1];
  340. colorValues[colorIndex++] = Color.floatToByte(color.red);
  341. colorValues[colorIndex++] = Color.floatToByte(color.green);
  342. colorValues[colorIndex++] = Color.floatToByte(color.blue);
  343. colorValues[colorIndex++] = Color.floatToByte(color.alpha);
  344. }
  345. if (perSegmentColors && i === length - 1) {
  346. break;
  347. }
  348. Cartesian3.pack(p, positionValues, positionIndex);
  349. positionIndex += 3;
  350. if (defined(colors)) {
  351. color = colors[i];
  352. colorValues[colorIndex++] = Color.floatToByte(color.red);
  353. colorValues[colorIndex++] = Color.floatToByte(color.green);
  354. colorValues[colorIndex++] = Color.floatToByte(color.blue);
  355. colorValues[colorIndex++] = Color.floatToByte(color.alpha);
  356. }
  357. }
  358. }
  359. const attributes = new GeometryAttributes();
  360. attributes.position = new GeometryAttribute({
  361. componentDatatype: ComponentDatatype.DOUBLE,
  362. componentsPerAttribute: 3,
  363. values: positionValues,
  364. });
  365. if (defined(colors)) {
  366. attributes.color = new GeometryAttribute({
  367. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  368. componentsPerAttribute: 4,
  369. values: colorValues,
  370. normalize: true,
  371. });
  372. }
  373. numberOfPositions = positionValues.length / 3;
  374. const numberOfIndices = (numberOfPositions - 1) * 2;
  375. const indices = IndexDatatype.createTypedArray(
  376. numberOfPositions,
  377. numberOfIndices
  378. );
  379. let index = 0;
  380. for (i = 0; i < numberOfPositions - 1; ++i) {
  381. indices[index++] = i;
  382. indices[index++] = i + 1;
  383. }
  384. return new Geometry({
  385. attributes: attributes,
  386. indices: indices,
  387. primitiveType: PrimitiveType.LINES,
  388. boundingSphere: BoundingSphere.fromPoints(positions),
  389. });
  390. };
  391. export default SimplePolylineGeometry;