EllipsoidOutlineGeometry.js 17 KB

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