EllipseGeometryLibrary-4199bc89.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './Matrix2-fc7e9822', './ComponentDatatype-4a60b8d6', './Transforms-a076dbe6'], (function (exports, Matrix2, ComponentDatatype, Transforms) { 'use strict';
  3. const EllipseGeometryLibrary = {};
  4. const rotAxis = new Matrix2.Cartesian3();
  5. const tempVec = new Matrix2.Cartesian3();
  6. const unitQuat = new Transforms.Quaternion();
  7. const rotMtx = new Matrix2.Matrix3();
  8. function pointOnEllipsoid(
  9. theta,
  10. rotation,
  11. northVec,
  12. eastVec,
  13. aSqr,
  14. ab,
  15. bSqr,
  16. mag,
  17. unitPos,
  18. result
  19. ) {
  20. const azimuth = theta + rotation;
  21. Matrix2.Cartesian3.multiplyByScalar(eastVec, Math.cos(azimuth), rotAxis);
  22. Matrix2.Cartesian3.multiplyByScalar(northVec, Math.sin(azimuth), tempVec);
  23. Matrix2.Cartesian3.add(rotAxis, tempVec, rotAxis);
  24. let cosThetaSquared = Math.cos(theta);
  25. cosThetaSquared = cosThetaSquared * cosThetaSquared;
  26. let sinThetaSquared = Math.sin(theta);
  27. sinThetaSquared = sinThetaSquared * sinThetaSquared;
  28. const radius =
  29. ab / Math.sqrt(bSqr * cosThetaSquared + aSqr * sinThetaSquared);
  30. const angle = radius / mag;
  31. // Create the quaternion to rotate the position vector to the boundary of the ellipse.
  32. Transforms.Quaternion.fromAxisAngle(rotAxis, angle, unitQuat);
  33. Matrix2.Matrix3.fromQuaternion(unitQuat, rotMtx);
  34. Matrix2.Matrix3.multiplyByVector(rotMtx, unitPos, result);
  35. Matrix2.Cartesian3.normalize(result, result);
  36. Matrix2.Cartesian3.multiplyByScalar(result, mag, result);
  37. return result;
  38. }
  39. const scratchCartesian1 = new Matrix2.Cartesian3();
  40. const scratchCartesian2 = new Matrix2.Cartesian3();
  41. const scratchCartesian3 = new Matrix2.Cartesian3();
  42. const scratchNormal = new Matrix2.Cartesian3();
  43. /**
  44. * Returns the positions raised to the given heights
  45. * @private
  46. */
  47. EllipseGeometryLibrary.raisePositionsToHeight = function (
  48. positions,
  49. options,
  50. extrude
  51. ) {
  52. const ellipsoid = options.ellipsoid;
  53. const height = options.height;
  54. const extrudedHeight = options.extrudedHeight;
  55. const size = extrude ? (positions.length / 3) * 2 : positions.length / 3;
  56. const finalPositions = new Float64Array(size * 3);
  57. const length = positions.length;
  58. const bottomOffset = extrude ? length : 0;
  59. for (let i = 0; i < length; i += 3) {
  60. const i1 = i + 1;
  61. const i2 = i + 2;
  62. const position = Matrix2.Cartesian3.fromArray(positions, i, scratchCartesian1);
  63. ellipsoid.scaleToGeodeticSurface(position, position);
  64. const extrudedPosition = Matrix2.Cartesian3.clone(position, scratchCartesian2);
  65. const normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  66. const scaledNormal = Matrix2.Cartesian3.multiplyByScalar(
  67. normal,
  68. height,
  69. scratchCartesian3
  70. );
  71. Matrix2.Cartesian3.add(position, scaledNormal, position);
  72. if (extrude) {
  73. Matrix2.Cartesian3.multiplyByScalar(normal, extrudedHeight, scaledNormal);
  74. Matrix2.Cartesian3.add(extrudedPosition, scaledNormal, extrudedPosition);
  75. finalPositions[i + bottomOffset] = extrudedPosition.x;
  76. finalPositions[i1 + bottomOffset] = extrudedPosition.y;
  77. finalPositions[i2 + bottomOffset] = extrudedPosition.z;
  78. }
  79. finalPositions[i] = position.x;
  80. finalPositions[i1] = position.y;
  81. finalPositions[i2] = position.z;
  82. }
  83. return finalPositions;
  84. };
  85. const unitPosScratch = new Matrix2.Cartesian3();
  86. const eastVecScratch = new Matrix2.Cartesian3();
  87. const northVecScratch = new Matrix2.Cartesian3();
  88. /**
  89. * Returns an array of positions that make up the ellipse.
  90. * @private
  91. */
  92. EllipseGeometryLibrary.computeEllipsePositions = function (
  93. options,
  94. addFillPositions,
  95. addEdgePositions
  96. ) {
  97. const semiMinorAxis = options.semiMinorAxis;
  98. const semiMajorAxis = options.semiMajorAxis;
  99. const rotation = options.rotation;
  100. const center = options.center;
  101. // Computing the arc-length of the ellipse is too expensive to be practical. Estimating it using the
  102. // arc length of the sphere is too inaccurate and creates sharp edges when either the semi-major or
  103. // semi-minor axis is much bigger than the other. Instead, scale the angle delta to make
  104. // the distance along the ellipse boundary more closely match the granularity.
  105. const granularity = options.granularity * 8.0;
  106. const aSqr = semiMinorAxis * semiMinorAxis;
  107. const bSqr = semiMajorAxis * semiMajorAxis;
  108. const ab = semiMajorAxis * semiMinorAxis;
  109. const mag = Matrix2.Cartesian3.magnitude(center);
  110. const unitPos = Matrix2.Cartesian3.normalize(center, unitPosScratch);
  111. let eastVec = Matrix2.Cartesian3.cross(Matrix2.Cartesian3.UNIT_Z, center, eastVecScratch);
  112. eastVec = Matrix2.Cartesian3.normalize(eastVec, eastVec);
  113. const northVec = Matrix2.Cartesian3.cross(unitPos, eastVec, northVecScratch);
  114. // The number of points in the first quadrant
  115. let numPts = 1 + Math.ceil(ComponentDatatype.CesiumMath.PI_OVER_TWO / granularity);
  116. const deltaTheta = ComponentDatatype.CesiumMath.PI_OVER_TWO / (numPts - 1);
  117. let theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - numPts * deltaTheta;
  118. if (theta < 0.0) {
  119. numPts -= Math.ceil(Math.abs(theta) / deltaTheta);
  120. }
  121. // If the number of points were three, the ellipse
  122. // would be tessellated like below:
  123. //
  124. // *---*
  125. // / | \ | \
  126. // *---*---*---*
  127. // / | \ | \ | \ | \
  128. // / .*---*---*---*. \
  129. // * ` | \ | \ | \ | `*
  130. // \`.*---*---*---*.`/
  131. // \ | \ | \ | \ | /
  132. // *---*---*---*
  133. // \ | \ | /
  134. // *---*
  135. // The first and last column have one position and fan to connect to the adjacent column.
  136. // Each other vertical column contains an even number of positions.
  137. const size = 2 * (numPts * (numPts + 2));
  138. const positions = addFillPositions ? new Array(size * 3) : undefined;
  139. let positionIndex = 0;
  140. let position = scratchCartesian1;
  141. let reflectedPosition = scratchCartesian2;
  142. const outerPositionsLength = numPts * 4 * 3;
  143. let outerRightIndex = outerPositionsLength - 1;
  144. let outerLeftIndex = 0;
  145. const outerPositions = addEdgePositions
  146. ? new Array(outerPositionsLength)
  147. : undefined;
  148. let i;
  149. let j;
  150. let numInterior;
  151. let t;
  152. let interiorPosition;
  153. // Compute points in the 'eastern' half of the ellipse
  154. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO;
  155. position = pointOnEllipsoid(
  156. theta,
  157. rotation,
  158. northVec,
  159. eastVec,
  160. aSqr,
  161. ab,
  162. bSqr,
  163. mag,
  164. unitPos,
  165. position
  166. );
  167. if (addFillPositions) {
  168. positions[positionIndex++] = position.x;
  169. positions[positionIndex++] = position.y;
  170. positions[positionIndex++] = position.z;
  171. }
  172. if (addEdgePositions) {
  173. outerPositions[outerRightIndex--] = position.z;
  174. outerPositions[outerRightIndex--] = position.y;
  175. outerPositions[outerRightIndex--] = position.x;
  176. }
  177. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - deltaTheta;
  178. for (i = 1; i < numPts + 1; ++i) {
  179. position = pointOnEllipsoid(
  180. theta,
  181. rotation,
  182. northVec,
  183. eastVec,
  184. aSqr,
  185. ab,
  186. bSqr,
  187. mag,
  188. unitPos,
  189. position
  190. );
  191. reflectedPosition = pointOnEllipsoid(
  192. Math.PI - theta,
  193. rotation,
  194. northVec,
  195. eastVec,
  196. aSqr,
  197. ab,
  198. bSqr,
  199. mag,
  200. unitPos,
  201. reflectedPosition
  202. );
  203. if (addFillPositions) {
  204. positions[positionIndex++] = position.x;
  205. positions[positionIndex++] = position.y;
  206. positions[positionIndex++] = position.z;
  207. numInterior = 2 * i + 2;
  208. for (j = 1; j < numInterior - 1; ++j) {
  209. t = j / (numInterior - 1);
  210. interiorPosition = Matrix2.Cartesian3.lerp(
  211. position,
  212. reflectedPosition,
  213. t,
  214. scratchCartesian3
  215. );
  216. positions[positionIndex++] = interiorPosition.x;
  217. positions[positionIndex++] = interiorPosition.y;
  218. positions[positionIndex++] = interiorPosition.z;
  219. }
  220. positions[positionIndex++] = reflectedPosition.x;
  221. positions[positionIndex++] = reflectedPosition.y;
  222. positions[positionIndex++] = reflectedPosition.z;
  223. }
  224. if (addEdgePositions) {
  225. outerPositions[outerRightIndex--] = position.z;
  226. outerPositions[outerRightIndex--] = position.y;
  227. outerPositions[outerRightIndex--] = position.x;
  228. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  229. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  230. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  231. }
  232. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - (i + 1) * deltaTheta;
  233. }
  234. // Compute points in the 'western' half of the ellipse
  235. for (i = numPts; i > 1; --i) {
  236. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - (i - 1) * deltaTheta;
  237. position = pointOnEllipsoid(
  238. -theta,
  239. rotation,
  240. northVec,
  241. eastVec,
  242. aSqr,
  243. ab,
  244. bSqr,
  245. mag,
  246. unitPos,
  247. position
  248. );
  249. reflectedPosition = pointOnEllipsoid(
  250. theta + Math.PI,
  251. rotation,
  252. northVec,
  253. eastVec,
  254. aSqr,
  255. ab,
  256. bSqr,
  257. mag,
  258. unitPos,
  259. reflectedPosition
  260. );
  261. if (addFillPositions) {
  262. positions[positionIndex++] = position.x;
  263. positions[positionIndex++] = position.y;
  264. positions[positionIndex++] = position.z;
  265. numInterior = 2 * (i - 1) + 2;
  266. for (j = 1; j < numInterior - 1; ++j) {
  267. t = j / (numInterior - 1);
  268. interiorPosition = Matrix2.Cartesian3.lerp(
  269. position,
  270. reflectedPosition,
  271. t,
  272. scratchCartesian3
  273. );
  274. positions[positionIndex++] = interiorPosition.x;
  275. positions[positionIndex++] = interiorPosition.y;
  276. positions[positionIndex++] = interiorPosition.z;
  277. }
  278. positions[positionIndex++] = reflectedPosition.x;
  279. positions[positionIndex++] = reflectedPosition.y;
  280. positions[positionIndex++] = reflectedPosition.z;
  281. }
  282. if (addEdgePositions) {
  283. outerPositions[outerRightIndex--] = position.z;
  284. outerPositions[outerRightIndex--] = position.y;
  285. outerPositions[outerRightIndex--] = position.x;
  286. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  287. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  288. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  289. }
  290. }
  291. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO;
  292. position = pointOnEllipsoid(
  293. -theta,
  294. rotation,
  295. northVec,
  296. eastVec,
  297. aSqr,
  298. ab,
  299. bSqr,
  300. mag,
  301. unitPos,
  302. position
  303. );
  304. const r = {};
  305. if (addFillPositions) {
  306. positions[positionIndex++] = position.x;
  307. positions[positionIndex++] = position.y;
  308. positions[positionIndex++] = position.z;
  309. r.positions = positions;
  310. r.numPts = numPts;
  311. }
  312. if (addEdgePositions) {
  313. outerPositions[outerRightIndex--] = position.z;
  314. outerPositions[outerRightIndex--] = position.y;
  315. outerPositions[outerRightIndex--] = position.x;
  316. r.outerPositions = outerPositions;
  317. }
  318. return r;
  319. };
  320. exports.EllipseGeometryLibrary = EllipseGeometryLibrary;
  321. }));