EllipsoidOutlineGeometry-907e3eb7.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './ComponentDatatype-5862616f', './GeometryAttribute-91704ebb', './PrimitiveType-97893bc7', './GeometryAttributes-aacecde6', './IndexDatatype-9435b55f', './GeometryOffsetAttribute-ca302482'], function (exports, when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, ComponentDatatype, GeometryAttribute, PrimitiveType, GeometryAttributes, IndexDatatype, GeometryOffsetAttribute) { 'use strict';
  24. var defaultRadii = new Cartographic.Cartesian3(1.0, 1.0, 1.0);
  25. var cos = Math.cos;
  26. var sin = Math.sin;
  27. /**
  28. * A description of the outline of an ellipsoid centered at the origin.
  29. *
  30. * @alias EllipsoidOutlineGeometry
  31. * @constructor
  32. *
  33. * @param {Object} [options] Object with the following properties:
  34. * @param {Cartesian3} [options.radii=Cartesian3(1.0, 1.0, 1.0)] The radii of the ellipsoid in the x, y, and z directions.
  35. * @param {Cartesian3} [options.innerRadii=options.radii] The inner radii of the ellipsoid in the x, y, and z directions.
  36. * @param {Number} [options.minimumClock=0.0] The minimum angle lying in the xy-plane measured from the positive x-axis and toward the positive y-axis.
  37. * @param {Number} [options.maximumClock=2*PI] The maximum angle lying in the xy-plane measured from the positive x-axis and toward the positive y-axis.
  38. * @param {Number} [options.minimumCone=0.0] The minimum angle measured from the positive z-axis and toward the negative z-axis.
  39. * @param {Number} [options.maximumCone=PI] The maximum angle measured from the positive z-axis and toward the negative z-axis.
  40. * @param {Number} [options.stackPartitions=10] The count of stacks for the ellipsoid (1 greater than the number of parallel lines).
  41. * @param {Number} [options.slicePartitions=8] The count of slices for the ellipsoid (Equal to the number of radial lines).
  42. * @param {Number} [options.subdivisions=128] The number of points per line, determining the granularity of the curvature.
  43. *
  44. * @exception {DeveloperError} options.stackPartitions must be greater than or equal to one.
  45. * @exception {DeveloperError} options.slicePartitions must be greater than or equal to zero.
  46. * @exception {DeveloperError} options.subdivisions must be greater than or equal to zero.
  47. *
  48. * @example
  49. * var ellipsoid = new Cesium.EllipsoidOutlineGeometry({
  50. * radii : new Cesium.Cartesian3(1000000.0, 500000.0, 500000.0),
  51. * stackPartitions: 6,
  52. * slicePartitions: 5
  53. * });
  54. * var geometry = Cesium.EllipsoidOutlineGeometry.createGeometry(ellipsoid);
  55. */
  56. function EllipsoidOutlineGeometry(options) {
  57. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  58. var radii = when.defaultValue(options.radii, defaultRadii);
  59. var innerRadii = when.defaultValue(options.innerRadii, radii);
  60. var minimumClock = when.defaultValue(options.minimumClock, 0.0);
  61. var maximumClock = when.defaultValue(options.maximumClock, _Math.CesiumMath.TWO_PI);
  62. var minimumCone = when.defaultValue(options.minimumCone, 0.0);
  63. var maximumCone = when.defaultValue(options.maximumCone, _Math.CesiumMath.PI);
  64. var stackPartitions = Math.round(when.defaultValue(options.stackPartitions, 10));
  65. var slicePartitions = Math.round(when.defaultValue(options.slicePartitions, 8));
  66. var subdivisions = Math.round(when.defaultValue(options.subdivisions, 128));
  67. //>>includeStart('debug', pragmas.debug);
  68. if (stackPartitions < 1) {
  69. throw new Check.DeveloperError('options.stackPartitions cannot be less than 1');
  70. }
  71. if (slicePartitions < 0) {
  72. throw new Check.DeveloperError('options.slicePartitions cannot be less than 0');
  73. }
  74. if (subdivisions < 0) {
  75. throw new Check.DeveloperError('options.subdivisions must be greater than or equal to zero.');
  76. }
  77. if (when.defined(options.offsetAttribute) && options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  78. throw new Check.DeveloperError('GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry.');
  79. }
  80. //>>includeEnd('debug');
  81. this._radii = Cartographic.Cartesian3.clone(radii);
  82. this._innerRadii = Cartographic.Cartesian3.clone(innerRadii);
  83. this._minimumClock = minimumClock;
  84. this._maximumClock = maximumClock;
  85. this._minimumCone = minimumCone;
  86. this._maximumCone = maximumCone;
  87. this._stackPartitions = stackPartitions;
  88. this._slicePartitions = slicePartitions;
  89. this._subdivisions = subdivisions;
  90. this._offsetAttribute = options.offsetAttribute;
  91. this._workerName = 'createEllipsoidOutlineGeometry';
  92. }
  93. /**
  94. * The number of elements used to pack the object into an array.
  95. * @type {Number}
  96. */
  97. EllipsoidOutlineGeometry.packedLength = 2 * (Cartographic.Cartesian3.packedLength) + 8;
  98. /**
  99. * Stores the provided instance into the provided array.
  100. *
  101. * @param {EllipsoidOutlineGeometry} value The value to pack.
  102. * @param {Number[]} array The array to pack into.
  103. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  104. *
  105. * @returns {Number[]} The array that was packed into
  106. */
  107. EllipsoidOutlineGeometry.pack = function(value, array, startingIndex) {
  108. //>>includeStart('debug', pragmas.debug);
  109. if (!when.defined(value)) {
  110. throw new Check.DeveloperError('value is required');
  111. }
  112. if (!when.defined(array)) {
  113. throw new Check.DeveloperError('array is required');
  114. }
  115. //>>includeEnd('debug');
  116. startingIndex = when.defaultValue(startingIndex, 0);
  117. Cartographic.Cartesian3.pack(value._radii, array, startingIndex);
  118. startingIndex += Cartographic.Cartesian3.packedLength;
  119. Cartographic.Cartesian3.pack(value._innerRadii, array, startingIndex);
  120. startingIndex += Cartographic.Cartesian3.packedLength;
  121. array[startingIndex++] = value._minimumClock;
  122. array[startingIndex++] = value._maximumClock;
  123. array[startingIndex++] = value._minimumCone;
  124. array[startingIndex++] = value._maximumCone;
  125. array[startingIndex++] = value._stackPartitions;
  126. array[startingIndex++] = value._slicePartitions;
  127. array[startingIndex++] = value._subdivisions;
  128. array[startingIndex] = when.defaultValue(value._offsetAttribute, -1);
  129. return array;
  130. };
  131. var scratchRadii = new Cartographic.Cartesian3();
  132. var scratchInnerRadii = new Cartographic.Cartesian3();
  133. var scratchOptions = {
  134. radii : scratchRadii,
  135. innerRadii : scratchInnerRadii,
  136. minimumClock : undefined,
  137. maximumClock : undefined,
  138. minimumCone : undefined,
  139. maximumCone : undefined,
  140. stackPartitions : undefined,
  141. slicePartitions : undefined,
  142. subdivisions : undefined,
  143. offsetAttribute : undefined
  144. };
  145. /**
  146. * Retrieves an instance from a packed array.
  147. *
  148. * @param {Number[]} array The packed array.
  149. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  150. * @param {EllipsoidOutlineGeometry} [result] The object into which to store the result.
  151. * @returns {EllipsoidOutlineGeometry} The modified result parameter or a new EllipsoidOutlineGeometry instance if one was not provided.
  152. */
  153. EllipsoidOutlineGeometry.unpack = function(array, startingIndex, result) {
  154. //>>includeStart('debug', pragmas.debug);
  155. if (!when.defined(array)) {
  156. throw new Check.DeveloperError('array is required');
  157. }
  158. //>>includeEnd('debug');
  159. startingIndex = when.defaultValue(startingIndex, 0);
  160. var radii = Cartographic.Cartesian3.unpack(array, startingIndex, scratchRadii);
  161. startingIndex += Cartographic.Cartesian3.packedLength;
  162. var innerRadii = Cartographic.Cartesian3.unpack(array, startingIndex, scratchInnerRadii);
  163. startingIndex += Cartographic.Cartesian3.packedLength;
  164. var minimumClock = array[startingIndex++];
  165. var maximumClock = array[startingIndex++];
  166. var minimumCone = array[startingIndex++];
  167. var maximumCone = array[startingIndex++];
  168. var stackPartitions = array[startingIndex++];
  169. var slicePartitions = array[startingIndex++];
  170. var subdivisions = array[startingIndex++];
  171. var offsetAttribute = array[startingIndex];
  172. if (!when.defined(result)) {
  173. scratchOptions.minimumClock = minimumClock;
  174. scratchOptions.maximumClock = maximumClock;
  175. scratchOptions.minimumCone = minimumCone;
  176. scratchOptions.maximumCone = maximumCone;
  177. scratchOptions.stackPartitions = stackPartitions;
  178. scratchOptions.slicePartitions = slicePartitions;
  179. scratchOptions.subdivisions = subdivisions;
  180. scratchOptions.offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  181. return new EllipsoidOutlineGeometry(scratchOptions);
  182. }
  183. result._radii = Cartographic.Cartesian3.clone(radii, result._radii);
  184. result._innerRadii = Cartographic.Cartesian3.clone(innerRadii, result._innerRadii);
  185. result._minimumClock = minimumClock;
  186. result._maximumClock = maximumClock;
  187. result._minimumCone = minimumCone;
  188. result._maximumCone = maximumCone;
  189. result._stackPartitions = stackPartitions;
  190. result._slicePartitions = slicePartitions;
  191. result._subdivisions = subdivisions;
  192. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  193. return result;
  194. };
  195. /**
  196. * Computes the geometric representation of an outline of an ellipsoid, including its vertices, indices, and a bounding sphere.
  197. *
  198. * @param {EllipsoidOutlineGeometry} ellipsoidGeometry A description of the ellipsoid outline.
  199. * @returns {Geometry|undefined} The computed vertices and indices.
  200. */
  201. EllipsoidOutlineGeometry.createGeometry = function(ellipsoidGeometry) {
  202. var radii = ellipsoidGeometry._radii;
  203. if ((radii.x <= 0) || (radii.y <= 0) || (radii.z <= 0)) {
  204. return;
  205. }
  206. var innerRadii = ellipsoidGeometry._innerRadii;
  207. if ((innerRadii.x <= 0) || (innerRadii.y <= 0) || (innerRadii.z <= 0)) {
  208. return;
  209. }
  210. var minimumClock = ellipsoidGeometry._minimumClock;
  211. var maximumClock = ellipsoidGeometry._maximumClock;
  212. var minimumCone = ellipsoidGeometry._minimumCone;
  213. var maximumCone = ellipsoidGeometry._maximumCone;
  214. var subdivisions = ellipsoidGeometry._subdivisions;
  215. var ellipsoid = Cartesian2.Ellipsoid.fromCartesian3(radii);
  216. // Add an extra slice and stack to remain consistent with EllipsoidGeometry
  217. var slicePartitions = ellipsoidGeometry._slicePartitions + 1;
  218. var stackPartitions = ellipsoidGeometry._stackPartitions + 1;
  219. slicePartitions = Math.round(slicePartitions * Math.abs(maximumClock - minimumClock) / _Math.CesiumMath.TWO_PI);
  220. stackPartitions = Math.round(stackPartitions * Math.abs(maximumCone - minimumCone) / _Math.CesiumMath.PI);
  221. if (slicePartitions < 2) {
  222. slicePartitions = 2;
  223. }
  224. if (stackPartitions < 2) {
  225. stackPartitions = 2;
  226. }
  227. var extraIndices = 0;
  228. var vertexMultiplier = 1.0;
  229. var hasInnerSurface = ((innerRadii.x !== radii.x) || (innerRadii.y !== radii.y) || innerRadii.z !== radii.z);
  230. var isTopOpen = false;
  231. var isBotOpen = false;
  232. if (hasInnerSurface) {
  233. vertexMultiplier = 2.0;
  234. // Add 2x slicePartitions to connect the top/bottom of the outer to
  235. // the top/bottom of the inner
  236. if (minimumCone > 0.0) {
  237. isTopOpen = true;
  238. extraIndices += slicePartitions;
  239. }
  240. if (maximumCone < Math.PI) {
  241. isBotOpen = true;
  242. extraIndices += slicePartitions;
  243. }
  244. }
  245. var vertexCount = subdivisions * vertexMultiplier * (stackPartitions + slicePartitions);
  246. var positions = new Float64Array(vertexCount * 3);
  247. // Multiply by two because two points define each line segment
  248. var numIndices = 2 * (vertexCount + extraIndices - (slicePartitions + stackPartitions) * vertexMultiplier);
  249. var indices = IndexDatatype.IndexDatatype.createTypedArray(vertexCount, numIndices);
  250. var i;
  251. var j;
  252. var theta;
  253. var phi;
  254. var index = 0;
  255. // Calculate sin/cos phi
  256. var sinPhi = new Array(stackPartitions);
  257. var cosPhi = new Array(stackPartitions);
  258. for (i = 0; i < stackPartitions; i++) {
  259. phi = minimumCone + i * (maximumCone - minimumCone) / (stackPartitions - 1);
  260. sinPhi[i] = sin(phi);
  261. cosPhi[i] = cos(phi);
  262. }
  263. // Calculate sin/cos theta
  264. var sinTheta = new Array(subdivisions);
  265. var cosTheta = new Array(subdivisions);
  266. for (i = 0; i < subdivisions; i++) {
  267. theta = minimumClock + i * (maximumClock - minimumClock) / (subdivisions - 1);
  268. sinTheta[i] = sin(theta);
  269. cosTheta[i] = cos(theta);
  270. }
  271. // Calculate the latitude lines on the outer surface
  272. for (i = 0; i < stackPartitions; i++) {
  273. for (j = 0; j < subdivisions; j++) {
  274. positions[index++] = radii.x * sinPhi[i] * cosTheta[j];
  275. positions[index++] = radii.y * sinPhi[i] * sinTheta[j];
  276. positions[index++] = radii.z * cosPhi[i];
  277. }
  278. }
  279. // Calculate the latitude lines on the inner surface
  280. if (hasInnerSurface) {
  281. for (i = 0; i < stackPartitions; i++) {
  282. for (j = 0; j < subdivisions; j++) {
  283. positions[index++] = innerRadii.x * sinPhi[i] * cosTheta[j];
  284. positions[index++] = innerRadii.y * sinPhi[i] * sinTheta[j];
  285. positions[index++] = innerRadii.z * cosPhi[i];
  286. }
  287. }
  288. }
  289. // Calculate sin/cos phi
  290. sinPhi.length = subdivisions;
  291. cosPhi.length = subdivisions;
  292. for (i = 0; i < subdivisions; i++) {
  293. phi = minimumCone + i * (maximumCone - minimumCone) / (subdivisions - 1);
  294. sinPhi[i] = sin(phi);
  295. cosPhi[i] = cos(phi);
  296. }
  297. // Calculate sin/cos theta for each slice partition
  298. sinTheta.length = slicePartitions;
  299. cosTheta.length = slicePartitions;
  300. for (i = 0; i < slicePartitions; i++) {
  301. theta = minimumClock + i * (maximumClock - minimumClock) / (slicePartitions - 1);
  302. sinTheta[i] = sin(theta);
  303. cosTheta[i] = cos(theta);
  304. }
  305. // Calculate the longitude lines on the outer surface
  306. for (i = 0; i < subdivisions; i++) {
  307. for (j = 0; j < slicePartitions; j++) {
  308. positions[index++] = radii.x * sinPhi[i] * cosTheta[j];
  309. positions[index++] = radii.y * sinPhi[i] * sinTheta[j];
  310. positions[index++] = radii.z * cosPhi[i];
  311. }
  312. }
  313. // Calculate the longitude lines on the inner surface
  314. if (hasInnerSurface) {
  315. for (i = 0; i < subdivisions; i++) {
  316. for (j = 0; j < slicePartitions; j++) {
  317. positions[index++] = innerRadii.x * sinPhi[i] * cosTheta[j];
  318. positions[index++] = innerRadii.y * sinPhi[i] * sinTheta[j];
  319. positions[index++] = innerRadii.z * cosPhi[i];
  320. }
  321. }
  322. }
  323. // Create indices for the latitude lines
  324. index = 0;
  325. for (i = 0; i < stackPartitions * vertexMultiplier; i++) {
  326. var topOffset = i * subdivisions;
  327. for (j = 0; j < subdivisions - 1; j++) {
  328. indices[index++] = topOffset + j;
  329. indices[index++] = topOffset + j + 1;
  330. }
  331. }
  332. // Create indices for the outer longitude lines
  333. var offset = stackPartitions * subdivisions * vertexMultiplier;
  334. for (i = 0; i < slicePartitions; i++) {
  335. for (j = 0; j < subdivisions - 1; j++) {
  336. indices[index++] = offset + i + (j * slicePartitions);
  337. indices[index++] = offset + i + (j + 1) * slicePartitions;
  338. }
  339. }
  340. // Create indices for the inner longitude lines
  341. if (hasInnerSurface) {
  342. offset = stackPartitions * subdivisions * vertexMultiplier + slicePartitions * subdivisions;
  343. for (i = 0; i < slicePartitions; i++) {
  344. for (j = 0; j < subdivisions - 1; j++) {
  345. indices[index++] = offset + i + (j * slicePartitions);
  346. indices[index++] = offset + i + (j + 1) * slicePartitions;
  347. }
  348. }
  349. }
  350. if (hasInnerSurface) {
  351. var outerOffset = stackPartitions * subdivisions * vertexMultiplier;
  352. var innerOffset = outerOffset + (subdivisions * slicePartitions);
  353. if (isTopOpen) {
  354. // Draw lines from the top of the inner surface to the top of the outer surface
  355. for (i = 0; i < slicePartitions; i++) {
  356. indices[index++] = outerOffset + i;
  357. indices[index++] = innerOffset + i;
  358. }
  359. }
  360. if (isBotOpen) {
  361. // Draw lines from the top of the inner surface to the top of the outer surface
  362. outerOffset += (subdivisions * slicePartitions) - slicePartitions;
  363. innerOffset += (subdivisions * slicePartitions) - slicePartitions;
  364. for (i = 0; i < slicePartitions; i++) {
  365. indices[index++] = outerOffset + i;
  366. indices[index++] = innerOffset + i;
  367. }
  368. }
  369. }
  370. var attributes = new GeometryAttributes.GeometryAttributes({
  371. position : new GeometryAttribute.GeometryAttribute({
  372. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  373. componentsPerAttribute : 3,
  374. values : positions
  375. })
  376. });
  377. if (when.defined(ellipsoidGeometry._offsetAttribute)) {
  378. var length = positions.length;
  379. var applyOffset = new Uint8Array(length / 3);
  380. var offsetValue = ellipsoidGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  381. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  382. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  383. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  384. componentsPerAttribute : 1,
  385. values : applyOffset
  386. });
  387. }
  388. return new GeometryAttribute.Geometry({
  389. attributes : attributes,
  390. indices : indices,
  391. primitiveType : PrimitiveType.PrimitiveType.LINES,
  392. boundingSphere : BoundingSphere.BoundingSphere.fromEllipsoid(ellipsoid),
  393. offsetAttribute : ellipsoidGeometry._offsetAttribute
  394. });
  395. };
  396. exports.EllipsoidOutlineGeometry = EllipsoidOutlineGeometry;
  397. });