EllipsoidOutlineGeometry.js 17 KB

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