EllipsoidOutlineGeometry.js 19 KB

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