EllipseGeometryLibrary-92ab6b1e.js 11 KB

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