createSimplePolylineGeometry.js 20 KB

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