createSimplePolylineGeometry.js 17 KB

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