createSimplePolylineGeometry.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './Cartesian4-5af5bb24', './RuntimeError-ba10bc3e', './WebGLConstants-4c11ee5f', './ComponentDatatype-5862616f', './GeometryAttribute-91704ebb', './PrimitiveType-97893bc7', './FeatureDetection-7bd32c34', './Transforms-b2e71640', './buildModuleUrl-14bfe498', './GeometryAttributes-aacecde6', './IndexDatatype-9435b55f', './IntersectionTests-397d9494', './Plane-8390418f', './ArcType-66bc286a', './EllipsoidRhumbLine-f161e674', './EllipsoidGeodesic-84507801', './PolylinePipeline-a9f32196', './Color-69f1845f'], function (when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, Cartesian4, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, PrimitiveType, FeatureDetection, Transforms, buildModuleUrl, GeometryAttributes, IndexDatatype, IntersectionTests, Plane, ArcType, EllipsoidRhumbLine, EllipsoidGeodesic, PolylinePipeline, Color) { 'use strict';
  24. function interpolateColors(p0, p1, color0, color1, minDistance, array, offset) {
  25. var numPoints = PolylinePipeline.PolylinePipeline.numberOfPoints(p0, p1, minDistance);
  26. var i;
  27. var r0 = color0.red;
  28. var g0 = color0.green;
  29. var b0 = color0.blue;
  30. var a0 = color0.alpha;
  31. var r1 = color1.red;
  32. var g1 = color1.green;
  33. var b1 = color1.blue;
  34. var a1 = color1.alpha;
  35. if (Color.Color.equals(color0, color1)) {
  36. for (i = 0; i < numPoints; i++) {
  37. array[offset++] = Color.Color.floatToByte(r0);
  38. array[offset++] = Color.Color.floatToByte(g0);
  39. array[offset++] = Color.Color.floatToByte(b0);
  40. array[offset++] = Color.Color.floatToByte(a0);
  41. }
  42. return offset;
  43. }
  44. var redPerVertex = (r1 - r0) / numPoints;
  45. var greenPerVertex = (g1 - g0) / numPoints;
  46. var bluePerVertex = (b1 - b0) / numPoints;
  47. var alphaPerVertex = (a1 - a0) / numPoints;
  48. var index = offset;
  49. for (i = 0; i < numPoints; i++) {
  50. array[index++] = Color.Color.floatToByte(r0 + i * redPerVertex);
  51. array[index++] = Color.Color.floatToByte(g0 + i * greenPerVertex);
  52. array[index++] = Color.Color.floatToByte(b0 + i * bluePerVertex);
  53. array[index++] = Color.Color.floatToByte(a0 + i * alphaPerVertex);
  54. }
  55. return index;
  56. }
  57. /**
  58. * A description of a polyline modeled as a line strip; the first two positions define a line segment,
  59. * and each additional position defines a line segment from the previous position.
  60. *
  61. * @alias SimplePolylineGeometry
  62. * @constructor
  63. *
  64. * @param {Object} options Object with the following properties:
  65. * @param {Cartesian3[]} options.positions An array of {@link Cartesian3} defining the positions in the polyline as a line strip.
  66. * @param {Color[]} [options.colors] An Array of {@link Color} defining the per vertex or per segment colors.
  67. * @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.
  68. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polyline segments must follow.
  69. * @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.
  70. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  71. *
  72. * @exception {DeveloperError} At least two positions are required.
  73. * @exception {DeveloperError} colors has an invalid length.
  74. *
  75. * @see SimplePolylineGeometry#createGeometry
  76. *
  77. * @example
  78. * // A polyline with two connected line segments
  79. * var polyline = new Cesium.SimplePolylineGeometry({
  80. * positions : Cesium.Cartesian3.fromDegreesArray([
  81. * 0.0, 0.0,
  82. * 5.0, 0.0,
  83. * 5.0, 5.0
  84. * ])
  85. * });
  86. * var geometry = Cesium.SimplePolylineGeometry.createGeometry(polyline);
  87. */
  88. function SimplePolylineGeometry(options) {
  89. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  90. var positions = options.positions;
  91. var colors = options.colors;
  92. var colorsPerVertex = when.defaultValue(options.colorsPerVertex, false);
  93. //>>includeStart('debug', pragmas.debug);
  94. if ((!when.defined(positions)) || (positions.length < 2)) {
  95. throw new Check.DeveloperError('At least two positions are required.');
  96. }
  97. if (when.defined(colors) && ((colorsPerVertex && colors.length < positions.length) || (!colorsPerVertex && colors.length < positions.length - 1))) {
  98. throw new Check.DeveloperError('colors has an invalid length.');
  99. }
  100. //>>includeEnd('debug');
  101. this._positions = positions;
  102. this._colors = colors;
  103. this._colorsPerVertex = colorsPerVertex;
  104. this._arcType = when.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  105. this._granularity = when.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  106. this._ellipsoid = when.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  107. this._workerName = 'createSimplePolylineGeometry';
  108. var numComponents = 1 + positions.length * Cartographic.Cartesian3.packedLength;
  109. numComponents += when.defined(colors) ? 1 + colors.length * Color.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 + Cartesian2.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 (!when.defined(value)) {
  128. throw new Check.DeveloperError('value is required');
  129. }
  130. if (!when.defined(array)) {
  131. throw new Check.DeveloperError('array is required');
  132. }
  133. //>>includeEnd('debug');
  134. startingIndex = when.defaultValue(startingIndex, 0);
  135. var i;
  136. var positions = value._positions;
  137. var length = positions.length;
  138. array[startingIndex++] = length;
  139. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  140. Cartographic.Cartesian3.pack(positions[i], array, startingIndex);
  141. }
  142. var colors = value._colors;
  143. length = when.defined(colors) ? colors.length : 0.0;
  144. array[startingIndex++] = length;
  145. for (i = 0; i < length; ++i, startingIndex += Color.Color.packedLength) {
  146. Color.Color.pack(colors[i], array, startingIndex);
  147. }
  148. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  149. startingIndex += Cartesian2.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 (!when.defined(array)) {
  166. throw new Check.DeveloperError('array is required');
  167. }
  168. //>>includeEnd('debug');
  169. startingIndex = when.defaultValue(startingIndex, 0);
  170. var i;
  171. var length = array[startingIndex++];
  172. var positions = new Array(length);
  173. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  174. positions[i] = Cartographic.Cartesian3.unpack(array, startingIndex);
  175. }
  176. length = array[startingIndex++];
  177. var colors = length > 0 ? new Array(length) : undefined;
  178. for (i = 0; i < length; ++i, startingIndex += Color.Color.packedLength) {
  179. colors[i] = Color.Color.unpack(array, startingIndex);
  180. }
  181. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex);
  182. startingIndex += Cartesian2.Ellipsoid.packedLength;
  183. var colorsPerVertex = array[startingIndex++] === 1.0;
  184. var arcType = array[startingIndex++];
  185. var granularity = array[startingIndex];
  186. if (!when.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. var scratchArray1 = new Array(2);
  205. var scratchArray2 = new Array(2);
  206. var 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} The computed vertices and indices.
  218. */
  219. SimplePolylineGeometry.createGeometry = function(simplePolylineGeometry) {
  220. var positions = simplePolylineGeometry._positions;
  221. var colors = simplePolylineGeometry._colors;
  222. var colorsPerVertex = simplePolylineGeometry._colorsPerVertex;
  223. var arcType = simplePolylineGeometry._arcType;
  224. var granularity = simplePolylineGeometry._granularity;
  225. var ellipsoid = simplePolylineGeometry._ellipsoid;
  226. var minDistance = _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  227. var perSegmentColors = when.defined(colors) && !colorsPerVertex;
  228. var i;
  229. var length = positions.length;
  230. var positionValues;
  231. var numberOfPositions;
  232. var colorValues;
  233. var color;
  234. var offset = 0;
  235. if (arcType === ArcType.ArcType.GEODESIC || arcType === ArcType.ArcType.RHUMB) {
  236. var subdivisionSize;
  237. var numberOfPointsFunction;
  238. var generateArcFunction;
  239. if (arcType === ArcType.ArcType.GEODESIC) {
  240. subdivisionSize = _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  241. numberOfPointsFunction = PolylinePipeline.PolylinePipeline.numberOfPoints;
  242. generateArcFunction = PolylinePipeline.PolylinePipeline.generateArc;
  243. } else {
  244. subdivisionSize = granularity;
  245. numberOfPointsFunction = PolylinePipeline.PolylinePipeline.numberOfPointsRhumbLine;
  246. generateArcFunction = PolylinePipeline.PolylinePipeline.generateRhumbArc;
  247. }
  248. var heights = PolylinePipeline.PolylinePipeline.extractHeights(positions, ellipsoid);
  249. var generateArcOptions = generateArcOptionsScratch;
  250. if (arcType === ArcType.ArcType.GEODESIC) {
  251. generateArcOptions.minDistance = minDistance;
  252. } else {
  253. generateArcOptions.granularity = granularity;
  254. }
  255. generateArcOptions.ellipsoid = ellipsoid;
  256. if (perSegmentColors) {
  257. var positionCount = 0;
  258. for (i = 0; i < length - 1; i++) {
  259. positionCount += numberOfPointsFunction(positions[i], positions[i+1], subdivisionSize) + 1;
  260. }
  261. positionValues = new Float64Array(positionCount * 3);
  262. colorValues = new Uint8Array(positionCount * 4);
  263. generateArcOptions.positions = scratchArray1;
  264. generateArcOptions.height= scratchArray2;
  265. var ci = 0;
  266. for (i = 0; i < length - 1; ++i) {
  267. scratchArray1[0] = positions[i];
  268. scratchArray1[1] = positions[i + 1];
  269. scratchArray2[0] = heights[i];
  270. scratchArray2[1] = heights[i + 1];
  271. var pos = generateArcFunction(generateArcOptions);
  272. if (when.defined(colors)) {
  273. var segLen = pos.length / 3;
  274. color = colors[i];
  275. for(var k = 0; k < segLen; ++k) {
  276. colorValues[ci++] = Color.Color.floatToByte(color.red);
  277. colorValues[ci++] = Color.Color.floatToByte(color.green);
  278. colorValues[ci++] = Color.Color.floatToByte(color.blue);
  279. colorValues[ci++] = Color.Color.floatToByte(color.alpha);
  280. }
  281. }
  282. positionValues.set(pos, offset);
  283. offset += pos.length;
  284. }
  285. } else {
  286. generateArcOptions.positions = positions;
  287. generateArcOptions.height= heights;
  288. positionValues = new Float64Array(generateArcFunction(generateArcOptions));
  289. if (when.defined(colors)) {
  290. colorValues = new Uint8Array(positionValues.length / 3 * 4);
  291. for (i = 0; i < length - 1; ++i) {
  292. var p0 = positions[i];
  293. var p1 = positions[i + 1];
  294. var c0 = colors[i];
  295. var c1 = colors[i + 1];
  296. offset = interpolateColors(p0, p1, c0, c1, minDistance, colorValues, offset);
  297. }
  298. var lastColor = colors[length - 1];
  299. colorValues[offset++] = Color.Color.floatToByte(lastColor.red);
  300. colorValues[offset++] = Color.Color.floatToByte(lastColor.green);
  301. colorValues[offset++] = Color.Color.floatToByte(lastColor.blue);
  302. colorValues[offset++] = Color.Color.floatToByte(lastColor.alpha);
  303. }
  304. }
  305. } else {
  306. numberOfPositions = perSegmentColors ? length * 2 - 2 : length;
  307. positionValues = new Float64Array(numberOfPositions * 3);
  308. colorValues = when.defined(colors) ? new Uint8Array(numberOfPositions * 4) : undefined;
  309. var positionIndex = 0;
  310. var colorIndex = 0;
  311. for (i = 0; i < length; ++i) {
  312. var p = positions[i];
  313. if (perSegmentColors && i > 0) {
  314. Cartographic.Cartesian3.pack(p, positionValues, positionIndex);
  315. positionIndex += 3;
  316. color = colors[i - 1];
  317. colorValues[colorIndex++] = Color.Color.floatToByte(color.red);
  318. colorValues[colorIndex++] = Color.Color.floatToByte(color.green);
  319. colorValues[colorIndex++] = Color.Color.floatToByte(color.blue);
  320. colorValues[colorIndex++] = Color.Color.floatToByte(color.alpha);
  321. }
  322. if (perSegmentColors && i === length - 1) {
  323. break;
  324. }
  325. Cartographic.Cartesian3.pack(p, positionValues, positionIndex);
  326. positionIndex += 3;
  327. if (when.defined(colors)) {
  328. color = colors[i];
  329. colorValues[colorIndex++] = Color.Color.floatToByte(color.red);
  330. colorValues[colorIndex++] = Color.Color.floatToByte(color.green);
  331. colorValues[colorIndex++] = Color.Color.floatToByte(color.blue);
  332. colorValues[colorIndex++] = Color.Color.floatToByte(color.alpha);
  333. }
  334. }
  335. }
  336. var attributes = new GeometryAttributes.GeometryAttributes();
  337. attributes.position = new GeometryAttribute.GeometryAttribute({
  338. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  339. componentsPerAttribute : 3,
  340. values : positionValues
  341. });
  342. if (when.defined(colors)) {
  343. attributes.color = new GeometryAttribute.GeometryAttribute({
  344. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  345. componentsPerAttribute : 4,
  346. values : colorValues,
  347. normalize : true
  348. });
  349. }
  350. numberOfPositions = positionValues.length / 3;
  351. var numberOfIndices = (numberOfPositions - 1) * 2;
  352. var indices = IndexDatatype.IndexDatatype.createTypedArray(numberOfPositions, numberOfIndices);
  353. var index = 0;
  354. for (i = 0; i < numberOfPositions - 1; ++i) {
  355. indices[index++] = i;
  356. indices[index++] = i + 1;
  357. }
  358. return new GeometryAttribute.Geometry({
  359. attributes : attributes,
  360. indices : indices,
  361. primitiveType : PrimitiveType.PrimitiveType.LINES,
  362. boundingSphere : BoundingSphere.BoundingSphere.fromPoints(positions)
  363. });
  364. };
  365. function createSimplePolylineGeometry(simplePolylineGeometry, offset) {
  366. if (when.defined(offset)) {
  367. simplePolylineGeometry = SimplePolylineGeometry.unpack(simplePolylineGeometry, offset);
  368. }
  369. simplePolylineGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(simplePolylineGeometry._ellipsoid);
  370. return SimplePolylineGeometry.createGeometry(simplePolylineGeometry);
  371. }
  372. return createSimplePolylineGeometry;
  373. });