EllipseOutlineGeometry.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 EllipseGeometryLibrary from "./EllipseGeometryLibrary.js";
  9. import Ellipsoid from "./Ellipsoid.js";
  10. import Geometry from "./Geometry.js";
  11. import GeometryAttribute from "./GeometryAttribute.js";
  12. import GeometryAttributes from "./GeometryAttributes.js";
  13. import GeometryOffsetAttribute from "./GeometryOffsetAttribute.js";
  14. import IndexDatatype from "./IndexDatatype.js";
  15. import CesiumMath from "./Math.js";
  16. import PrimitiveType from "./PrimitiveType.js";
  17. const scratchCartesian1 = new Cartesian3();
  18. let boundingSphereCenter = new Cartesian3();
  19. function computeEllipse(options) {
  20. const center = options.center;
  21. boundingSphereCenter = Cartesian3.multiplyByScalar(
  22. options.ellipsoid.geodeticSurfaceNormal(center, boundingSphereCenter),
  23. options.height,
  24. boundingSphereCenter
  25. );
  26. boundingSphereCenter = Cartesian3.add(
  27. center,
  28. boundingSphereCenter,
  29. boundingSphereCenter
  30. );
  31. const boundingSphere = new BoundingSphere(
  32. boundingSphereCenter,
  33. options.semiMajorAxis
  34. );
  35. const positions = EllipseGeometryLibrary.computeEllipsePositions(
  36. options,
  37. false,
  38. true
  39. ).outerPositions;
  40. const attributes = new GeometryAttributes({
  41. position: new GeometryAttribute({
  42. componentDatatype: ComponentDatatype.DOUBLE,
  43. componentsPerAttribute: 3,
  44. values: EllipseGeometryLibrary.raisePositionsToHeight(
  45. positions,
  46. options,
  47. false
  48. ),
  49. }),
  50. });
  51. const length = positions.length / 3;
  52. const indices = IndexDatatype.createTypedArray(length, length * 2);
  53. let index = 0;
  54. for (let i = 0; i < length; ++i) {
  55. indices[index++] = i;
  56. indices[index++] = (i + 1) % length;
  57. }
  58. return {
  59. boundingSphere: boundingSphere,
  60. attributes: attributes,
  61. indices: indices,
  62. };
  63. }
  64. const topBoundingSphere = new BoundingSphere();
  65. const bottomBoundingSphere = new BoundingSphere();
  66. function computeExtrudedEllipse(options) {
  67. const center = options.center;
  68. const ellipsoid = options.ellipsoid;
  69. const semiMajorAxis = options.semiMajorAxis;
  70. let scaledNormal = Cartesian3.multiplyByScalar(
  71. ellipsoid.geodeticSurfaceNormal(center, scratchCartesian1),
  72. options.height,
  73. scratchCartesian1
  74. );
  75. topBoundingSphere.center = Cartesian3.add(
  76. center,
  77. scaledNormal,
  78. topBoundingSphere.center
  79. );
  80. topBoundingSphere.radius = semiMajorAxis;
  81. scaledNormal = Cartesian3.multiplyByScalar(
  82. ellipsoid.geodeticSurfaceNormal(center, scaledNormal),
  83. options.extrudedHeight,
  84. scaledNormal
  85. );
  86. bottomBoundingSphere.center = Cartesian3.add(
  87. center,
  88. scaledNormal,
  89. bottomBoundingSphere.center
  90. );
  91. bottomBoundingSphere.radius = semiMajorAxis;
  92. let positions = EllipseGeometryLibrary.computeEllipsePositions(
  93. options,
  94. false,
  95. true
  96. ).outerPositions;
  97. const attributes = new GeometryAttributes({
  98. position: new GeometryAttribute({
  99. componentDatatype: ComponentDatatype.DOUBLE,
  100. componentsPerAttribute: 3,
  101. values: EllipseGeometryLibrary.raisePositionsToHeight(
  102. positions,
  103. options,
  104. true
  105. ),
  106. }),
  107. });
  108. positions = attributes.position.values;
  109. const boundingSphere = BoundingSphere.union(
  110. topBoundingSphere,
  111. bottomBoundingSphere
  112. );
  113. let length = positions.length / 3;
  114. if (defined(options.offsetAttribute)) {
  115. let applyOffset = new Uint8Array(length);
  116. if (options.offsetAttribute === GeometryOffsetAttribute.TOP) {
  117. applyOffset = arrayFill(applyOffset, 1, 0, length / 2);
  118. } else {
  119. const offsetValue =
  120. options.offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  121. applyOffset = arrayFill(applyOffset, offsetValue);
  122. }
  123. attributes.applyOffset = new GeometryAttribute({
  124. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  125. componentsPerAttribute: 1,
  126. values: applyOffset,
  127. });
  128. }
  129. let numberOfVerticalLines = defaultValue(options.numberOfVerticalLines, 16);
  130. numberOfVerticalLines = CesiumMath.clamp(
  131. numberOfVerticalLines,
  132. 0,
  133. length / 2
  134. );
  135. const indices = IndexDatatype.createTypedArray(
  136. length,
  137. length * 2 + numberOfVerticalLines * 2
  138. );
  139. length /= 2;
  140. let index = 0;
  141. let i;
  142. for (i = 0; i < length; ++i) {
  143. indices[index++] = i;
  144. indices[index++] = (i + 1) % length;
  145. indices[index++] = i + length;
  146. indices[index++] = ((i + 1) % length) + length;
  147. }
  148. let numSide;
  149. if (numberOfVerticalLines > 0) {
  150. const numSideLines = Math.min(numberOfVerticalLines, length);
  151. numSide = Math.round(length / numSideLines);
  152. const maxI = Math.min(numSide * numberOfVerticalLines, length);
  153. for (i = 0; i < maxI; i += numSide) {
  154. indices[index++] = i;
  155. indices[index++] = i + length;
  156. }
  157. }
  158. return {
  159. boundingSphere: boundingSphere,
  160. attributes: attributes,
  161. indices: indices,
  162. };
  163. }
  164. /**
  165. * A description of the outline of an ellipse on an ellipsoid.
  166. *
  167. * @alias EllipseOutlineGeometry
  168. * @constructor
  169. *
  170. * @param {Object} options Object with the following properties:
  171. * @param {Cartesian3} options.center The ellipse's center point in the fixed frame.
  172. * @param {Number} options.semiMajorAxis The length of the ellipse's semi-major axis in meters.
  173. * @param {Number} options.semiMinorAxis The length of the ellipse's semi-minor axis in meters.
  174. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid the ellipse will be on.
  175. * @param {Number} [options.height=0.0] The distance in meters between the ellipse and the ellipsoid surface.
  176. * @param {Number} [options.extrudedHeight] The distance in meters between the ellipse's extruded face and the ellipsoid surface.
  177. * @param {Number} [options.rotation=0.0] The angle from north (counter-clockwise) in radians.
  178. * @param {Number} [options.granularity=0.02] The angular distance between points on the ellipse in radians.
  179. * @param {Number} [options.numberOfVerticalLines=16] Number of lines to draw between the top and bottom surface of an extruded ellipse.
  180. *
  181. * @exception {DeveloperError} semiMajorAxis and semiMinorAxis must be greater than zero.
  182. * @exception {DeveloperError} semiMajorAxis must be greater than or equal to the semiMinorAxis.
  183. * @exception {DeveloperError} granularity must be greater than zero.
  184. *
  185. * @see EllipseOutlineGeometry.createGeometry
  186. *
  187. * @example
  188. * const ellipse = new Cesium.EllipseOutlineGeometry({
  189. * center : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
  190. * semiMajorAxis : 500000.0,
  191. * semiMinorAxis : 300000.0,
  192. * rotation : Cesium.Math.toRadians(60.0)
  193. * });
  194. * const geometry = Cesium.EllipseOutlineGeometry.createGeometry(ellipse);
  195. */
  196. function EllipseOutlineGeometry(options) {
  197. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  198. const center = options.center;
  199. const ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  200. const semiMajorAxis = options.semiMajorAxis;
  201. const semiMinorAxis = options.semiMinorAxis;
  202. const granularity = defaultValue(
  203. options.granularity,
  204. CesiumMath.RADIANS_PER_DEGREE
  205. );
  206. //>>includeStart('debug', pragmas.debug);
  207. if (!defined(center)) {
  208. throw new DeveloperError("center is required.");
  209. }
  210. if (!defined(semiMajorAxis)) {
  211. throw new DeveloperError("semiMajorAxis is required.");
  212. }
  213. if (!defined(semiMinorAxis)) {
  214. throw new DeveloperError("semiMinorAxis is required.");
  215. }
  216. if (semiMajorAxis < semiMinorAxis) {
  217. throw new DeveloperError(
  218. "semiMajorAxis must be greater than or equal to the semiMinorAxis."
  219. );
  220. }
  221. if (granularity <= 0.0) {
  222. throw new DeveloperError("granularity must be greater than zero.");
  223. }
  224. //>>includeEnd('debug');
  225. const height = defaultValue(options.height, 0.0);
  226. const extrudedHeight = defaultValue(options.extrudedHeight, height);
  227. this._center = Cartesian3.clone(center);
  228. this._semiMajorAxis = semiMajorAxis;
  229. this._semiMinorAxis = semiMinorAxis;
  230. this._ellipsoid = Ellipsoid.clone(ellipsoid);
  231. this._rotation = defaultValue(options.rotation, 0.0);
  232. this._height = Math.max(extrudedHeight, height);
  233. this._granularity = granularity;
  234. this._extrudedHeight = Math.min(extrudedHeight, height);
  235. this._numberOfVerticalLines = Math.max(
  236. defaultValue(options.numberOfVerticalLines, 16),
  237. 0
  238. );
  239. this._offsetAttribute = options.offsetAttribute;
  240. this._workerName = "createEllipseOutlineGeometry";
  241. }
  242. /**
  243. * The number of elements used to pack the object into an array.
  244. * @type {Number}
  245. */
  246. EllipseOutlineGeometry.packedLength =
  247. Cartesian3.packedLength + Ellipsoid.packedLength + 8;
  248. /**
  249. * Stores the provided instance into the provided array.
  250. *
  251. * @param {EllipseOutlineGeometry} value The value to pack.
  252. * @param {Number[]} array The array to pack into.
  253. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  254. *
  255. * @returns {Number[]} The array that was packed into
  256. */
  257. EllipseOutlineGeometry.pack = function (value, array, startingIndex) {
  258. //>>includeStart('debug', pragmas.debug);
  259. if (!defined(value)) {
  260. throw new DeveloperError("value is required");
  261. }
  262. if (!defined(array)) {
  263. throw new DeveloperError("array is required");
  264. }
  265. //>>includeEnd('debug');
  266. startingIndex = defaultValue(startingIndex, 0);
  267. Cartesian3.pack(value._center, array, startingIndex);
  268. startingIndex += Cartesian3.packedLength;
  269. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  270. startingIndex += Ellipsoid.packedLength;
  271. array[startingIndex++] = value._semiMajorAxis;
  272. array[startingIndex++] = value._semiMinorAxis;
  273. array[startingIndex++] = value._rotation;
  274. array[startingIndex++] = value._height;
  275. array[startingIndex++] = value._granularity;
  276. array[startingIndex++] = value._extrudedHeight;
  277. array[startingIndex++] = value._numberOfVerticalLines;
  278. array[startingIndex] = defaultValue(value._offsetAttribute, -1);
  279. return array;
  280. };
  281. const scratchCenter = new Cartesian3();
  282. const scratchEllipsoid = new Ellipsoid();
  283. const scratchOptions = {
  284. center: scratchCenter,
  285. ellipsoid: scratchEllipsoid,
  286. semiMajorAxis: undefined,
  287. semiMinorAxis: undefined,
  288. rotation: undefined,
  289. height: undefined,
  290. granularity: undefined,
  291. extrudedHeight: undefined,
  292. numberOfVerticalLines: undefined,
  293. offsetAttribute: undefined,
  294. };
  295. /**
  296. * Retrieves an instance from a packed array.
  297. *
  298. * @param {Number[]} array The packed array.
  299. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  300. * @param {EllipseOutlineGeometry} [result] The object into which to store the result.
  301. * @returns {EllipseOutlineGeometry} The modified result parameter or a new EllipseOutlineGeometry instance if one was not provided.
  302. */
  303. EllipseOutlineGeometry.unpack = function (array, startingIndex, result) {
  304. //>>includeStart('debug', pragmas.debug);
  305. if (!defined(array)) {
  306. throw new DeveloperError("array is required");
  307. }
  308. //>>includeEnd('debug');
  309. startingIndex = defaultValue(startingIndex, 0);
  310. const center = Cartesian3.unpack(array, startingIndex, scratchCenter);
  311. startingIndex += Cartesian3.packedLength;
  312. const ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  313. startingIndex += Ellipsoid.packedLength;
  314. const semiMajorAxis = array[startingIndex++];
  315. const semiMinorAxis = array[startingIndex++];
  316. const rotation = array[startingIndex++];
  317. const height = array[startingIndex++];
  318. const granularity = array[startingIndex++];
  319. const extrudedHeight = array[startingIndex++];
  320. const numberOfVerticalLines = array[startingIndex++];
  321. const offsetAttribute = array[startingIndex];
  322. if (!defined(result)) {
  323. scratchOptions.height = height;
  324. scratchOptions.extrudedHeight = extrudedHeight;
  325. scratchOptions.granularity = granularity;
  326. scratchOptions.rotation = rotation;
  327. scratchOptions.semiMajorAxis = semiMajorAxis;
  328. scratchOptions.semiMinorAxis = semiMinorAxis;
  329. scratchOptions.numberOfVerticalLines = numberOfVerticalLines;
  330. scratchOptions.offsetAttribute =
  331. offsetAttribute === -1 ? undefined : offsetAttribute;
  332. return new EllipseOutlineGeometry(scratchOptions);
  333. }
  334. result._center = Cartesian3.clone(center, result._center);
  335. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  336. result._semiMajorAxis = semiMajorAxis;
  337. result._semiMinorAxis = semiMinorAxis;
  338. result._rotation = rotation;
  339. result._height = height;
  340. result._granularity = granularity;
  341. result._extrudedHeight = extrudedHeight;
  342. result._numberOfVerticalLines = numberOfVerticalLines;
  343. result._offsetAttribute =
  344. offsetAttribute === -1 ? undefined : offsetAttribute;
  345. return result;
  346. };
  347. /**
  348. * Computes the geometric representation of an outline of an ellipse on an ellipsoid, including its vertices, indices, and a bounding sphere.
  349. *
  350. * @param {EllipseOutlineGeometry} ellipseGeometry A description of the ellipse.
  351. * @returns {Geometry|undefined} The computed vertices and indices.
  352. */
  353. EllipseOutlineGeometry.createGeometry = function (ellipseGeometry) {
  354. if (
  355. ellipseGeometry._semiMajorAxis <= 0.0 ||
  356. ellipseGeometry._semiMinorAxis <= 0.0
  357. ) {
  358. return;
  359. }
  360. const height = ellipseGeometry._height;
  361. const extrudedHeight = ellipseGeometry._extrudedHeight;
  362. const extrude = !CesiumMath.equalsEpsilon(
  363. height,
  364. extrudedHeight,
  365. 0,
  366. CesiumMath.EPSILON2
  367. );
  368. ellipseGeometry._center = ellipseGeometry._ellipsoid.scaleToGeodeticSurface(
  369. ellipseGeometry._center,
  370. ellipseGeometry._center
  371. );
  372. const options = {
  373. center: ellipseGeometry._center,
  374. semiMajorAxis: ellipseGeometry._semiMajorAxis,
  375. semiMinorAxis: ellipseGeometry._semiMinorAxis,
  376. ellipsoid: ellipseGeometry._ellipsoid,
  377. rotation: ellipseGeometry._rotation,
  378. height: height,
  379. granularity: ellipseGeometry._granularity,
  380. numberOfVerticalLines: ellipseGeometry._numberOfVerticalLines,
  381. };
  382. let geometry;
  383. if (extrude) {
  384. options.extrudedHeight = extrudedHeight;
  385. options.offsetAttribute = ellipseGeometry._offsetAttribute;
  386. geometry = computeExtrudedEllipse(options);
  387. } else {
  388. geometry = computeEllipse(options);
  389. if (defined(ellipseGeometry._offsetAttribute)) {
  390. const length = geometry.attributes.position.values.length;
  391. const applyOffset = new Uint8Array(length / 3);
  392. const offsetValue =
  393. ellipseGeometry._offsetAttribute === GeometryOffsetAttribute.NONE
  394. ? 0
  395. : 1;
  396. arrayFill(applyOffset, offsetValue);
  397. geometry.attributes.applyOffset = new GeometryAttribute({
  398. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  399. componentsPerAttribute: 1,
  400. values: applyOffset,
  401. });
  402. }
  403. }
  404. return new Geometry({
  405. attributes: geometry.attributes,
  406. indices: geometry.indices,
  407. primitiveType: PrimitiveType.LINES,
  408. boundingSphere: geometry.boundingSphere,
  409. offsetAttribute: ellipseGeometry._offsetAttribute,
  410. });
  411. };
  412. export default EllipseOutlineGeometry;