EllipseOutlineGeometry.js 15 KB

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