createSimplePolylineGeometry.js 18 KB

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