createSimplePolylineGeometry.js 17 KB

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