CircleGeometry.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import EllipseGeometry from "./EllipseGeometry.js";
  6. import Ellipsoid from "./Ellipsoid.js";
  7. import VertexFormat from "./VertexFormat.js";
  8. /**
  9. * A description of a circle on the ellipsoid. Circle geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  10. *
  11. * @alias CircleGeometry
  12. * @constructor
  13. *
  14. * @param {Object} options Object with the following properties:
  15. * @param {Cartesian3} options.center The circle's center point in the fixed frame.
  16. * @param {Number} options.radius The radius in meters.
  17. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid the circle will be on.
  18. * @param {Number} [options.height=0.0] The distance in meters between the circle and the ellipsoid surface.
  19. * @param {Number} [options.granularity=0.02] The angular distance between points on the circle in radians.
  20. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  21. * @param {Number} [options.extrudedHeight=0.0] The distance in meters between the circle's extruded face and the ellipsoid surface.
  22. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  23. *
  24. * @exception {DeveloperError} radius must be greater than zero.
  25. * @exception {DeveloperError} granularity must be greater than zero.
  26. *
  27. * @see CircleGeometry.createGeometry
  28. * @see Packable
  29. *
  30. * @example
  31. * // Create a circle.
  32. * const circle = new Cesium.CircleGeometry({
  33. * center : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
  34. * radius : 100000.0
  35. * });
  36. * const geometry = Cesium.CircleGeometry.createGeometry(circle);
  37. */
  38. function CircleGeometry(options) {
  39. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  40. const radius = options.radius;
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.typeOf.number("radius", radius);
  43. //>>includeEnd('debug');
  44. const ellipseGeometryOptions = {
  45. center: options.center,
  46. semiMajorAxis: radius,
  47. semiMinorAxis: radius,
  48. ellipsoid: options.ellipsoid,
  49. height: options.height,
  50. extrudedHeight: options.extrudedHeight,
  51. granularity: options.granularity,
  52. vertexFormat: options.vertexFormat,
  53. stRotation: options.stRotation,
  54. shadowVolume: options.shadowVolume,
  55. };
  56. this._ellipseGeometry = new EllipseGeometry(ellipseGeometryOptions);
  57. this._workerName = "createCircleGeometry";
  58. }
  59. /**
  60. * The number of elements used to pack the object into an array.
  61. * @type {Number}
  62. */
  63. CircleGeometry.packedLength = EllipseGeometry.packedLength;
  64. /**
  65. * Stores the provided instance into the provided array.
  66. *
  67. * @param {CircleGeometry} value The value to pack.
  68. * @param {Number[]} array The array to pack into.
  69. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  70. *
  71. * @returns {Number[]} The array that was packed into
  72. */
  73. CircleGeometry.pack = function (value, array, startingIndex) {
  74. //>>includeStart('debug', pragmas.debug);
  75. Check.typeOf.object("value", value);
  76. //>>includeEnd('debug');
  77. return EllipseGeometry.pack(value._ellipseGeometry, array, startingIndex);
  78. };
  79. const scratchEllipseGeometry = new EllipseGeometry({
  80. center: new Cartesian3(),
  81. semiMajorAxis: 1.0,
  82. semiMinorAxis: 1.0,
  83. });
  84. const scratchOptions = {
  85. center: new Cartesian3(),
  86. radius: undefined,
  87. ellipsoid: Ellipsoid.clone(Ellipsoid.UNIT_SPHERE),
  88. height: undefined,
  89. extrudedHeight: undefined,
  90. granularity: undefined,
  91. vertexFormat: new VertexFormat(),
  92. stRotation: undefined,
  93. semiMajorAxis: undefined,
  94. semiMinorAxis: undefined,
  95. shadowVolume: undefined,
  96. };
  97. /**
  98. * Retrieves an instance from a packed array.
  99. *
  100. * @param {Number[]} array The packed array.
  101. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  102. * @param {CircleGeometry} [result] The object into which to store the result.
  103. * @returns {CircleGeometry} The modified result parameter or a new CircleGeometry instance if one was not provided.
  104. */
  105. CircleGeometry.unpack = function (array, startingIndex, result) {
  106. const ellipseGeometry = EllipseGeometry.unpack(
  107. array,
  108. startingIndex,
  109. scratchEllipseGeometry
  110. );
  111. scratchOptions.center = Cartesian3.clone(
  112. ellipseGeometry._center,
  113. scratchOptions.center
  114. );
  115. scratchOptions.ellipsoid = Ellipsoid.clone(
  116. ellipseGeometry._ellipsoid,
  117. scratchOptions.ellipsoid
  118. );
  119. scratchOptions.height = ellipseGeometry._height;
  120. scratchOptions.extrudedHeight = ellipseGeometry._extrudedHeight;
  121. scratchOptions.granularity = ellipseGeometry._granularity;
  122. scratchOptions.vertexFormat = VertexFormat.clone(
  123. ellipseGeometry._vertexFormat,
  124. scratchOptions.vertexFormat
  125. );
  126. scratchOptions.stRotation = ellipseGeometry._stRotation;
  127. scratchOptions.shadowVolume = ellipseGeometry._shadowVolume;
  128. if (!defined(result)) {
  129. scratchOptions.radius = ellipseGeometry._semiMajorAxis;
  130. return new CircleGeometry(scratchOptions);
  131. }
  132. scratchOptions.semiMajorAxis = ellipseGeometry._semiMajorAxis;
  133. scratchOptions.semiMinorAxis = ellipseGeometry._semiMinorAxis;
  134. result._ellipseGeometry = new EllipseGeometry(scratchOptions);
  135. return result;
  136. };
  137. /**
  138. * Computes the geometric representation of a circle on an ellipsoid, including its vertices, indices, and a bounding sphere.
  139. *
  140. * @param {CircleGeometry} circleGeometry A description of the circle.
  141. * @returns {Geometry|undefined} The computed vertices and indices.
  142. */
  143. CircleGeometry.createGeometry = function (circleGeometry) {
  144. return EllipseGeometry.createGeometry(circleGeometry._ellipseGeometry);
  145. };
  146. /**
  147. * @private
  148. */
  149. CircleGeometry.createShadowVolume = function (
  150. circleGeometry,
  151. minHeightFunc,
  152. maxHeightFunc
  153. ) {
  154. const granularity = circleGeometry._ellipseGeometry._granularity;
  155. const ellipsoid = circleGeometry._ellipseGeometry._ellipsoid;
  156. const minHeight = minHeightFunc(granularity, ellipsoid);
  157. const maxHeight = maxHeightFunc(granularity, ellipsoid);
  158. return new CircleGeometry({
  159. center: circleGeometry._ellipseGeometry._center,
  160. radius: circleGeometry._ellipseGeometry._semiMajorAxis,
  161. ellipsoid: ellipsoid,
  162. stRotation: circleGeometry._ellipseGeometry._stRotation,
  163. granularity: granularity,
  164. extrudedHeight: minHeight,
  165. height: maxHeight,
  166. vertexFormat: VertexFormat.POSITION_ONLY,
  167. shadowVolume: true,
  168. });
  169. };
  170. Object.defineProperties(CircleGeometry.prototype, {
  171. /**
  172. * @private
  173. */
  174. rectangle: {
  175. get: function () {
  176. return this._ellipseGeometry.rectangle;
  177. },
  178. },
  179. /**
  180. * For remapping texture coordinates when rendering CircleGeometries as GroundPrimitives.
  181. * @private
  182. */
  183. textureCoordinateRotationPoints: {
  184. get: function () {
  185. return this._ellipseGeometry.textureCoordinateRotationPoints;
  186. },
  187. },
  188. });
  189. export default CircleGeometry;