EllipseGeometryLibrary-dac95924.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-9e1c22e2', './ComponentDatatype-4eeb6d9b', './Transforms-273eeb44'], (function (exports, Matrix2, ComponentDatatype, Transforms) { 'use strict';
  26. const EllipseGeometryLibrary = {};
  27. const rotAxis = new Matrix2.Cartesian3();
  28. const tempVec = new Matrix2.Cartesian3();
  29. const unitQuat = new Transforms.Quaternion();
  30. const rotMtx = new Matrix2.Matrix3();
  31. function pointOnEllipsoid(
  32. theta,
  33. rotation,
  34. northVec,
  35. eastVec,
  36. aSqr,
  37. ab,
  38. bSqr,
  39. mag,
  40. unitPos,
  41. result
  42. ) {
  43. const azimuth = theta + rotation;
  44. Matrix2.Cartesian3.multiplyByScalar(eastVec, Math.cos(azimuth), rotAxis);
  45. Matrix2.Cartesian3.multiplyByScalar(northVec, Math.sin(azimuth), tempVec);
  46. Matrix2.Cartesian3.add(rotAxis, tempVec, rotAxis);
  47. let cosThetaSquared = Math.cos(theta);
  48. cosThetaSquared = cosThetaSquared * cosThetaSquared;
  49. let sinThetaSquared = Math.sin(theta);
  50. sinThetaSquared = sinThetaSquared * sinThetaSquared;
  51. const radius =
  52. ab / Math.sqrt(bSqr * cosThetaSquared + aSqr * sinThetaSquared);
  53. const angle = radius / mag;
  54. // Create the quaternion to rotate the position vector to the boundary of the ellipse.
  55. Transforms.Quaternion.fromAxisAngle(rotAxis, angle, unitQuat);
  56. Matrix2.Matrix3.fromQuaternion(unitQuat, rotMtx);
  57. Matrix2.Matrix3.multiplyByVector(rotMtx, unitPos, result);
  58. Matrix2.Cartesian3.normalize(result, result);
  59. Matrix2.Cartesian3.multiplyByScalar(result, mag, result);
  60. return result;
  61. }
  62. const scratchCartesian1 = new Matrix2.Cartesian3();
  63. const scratchCartesian2 = new Matrix2.Cartesian3();
  64. const scratchCartesian3 = new Matrix2.Cartesian3();
  65. const scratchNormal = new Matrix2.Cartesian3();
  66. /**
  67. * Returns the positions raised to the given heights
  68. * @private
  69. */
  70. EllipseGeometryLibrary.raisePositionsToHeight = function (
  71. positions,
  72. options,
  73. extrude
  74. ) {
  75. const ellipsoid = options.ellipsoid;
  76. const height = options.height;
  77. const extrudedHeight = options.extrudedHeight;
  78. const size = extrude ? (positions.length / 3) * 2 : positions.length / 3;
  79. const finalPositions = new Float64Array(size * 3);
  80. const length = positions.length;
  81. const bottomOffset = extrude ? length : 0;
  82. for (let i = 0; i < length; i += 3) {
  83. const i1 = i + 1;
  84. const i2 = i + 2;
  85. const position = Matrix2.Cartesian3.fromArray(positions, i, scratchCartesian1);
  86. ellipsoid.scaleToGeodeticSurface(position, position);
  87. const extrudedPosition = Matrix2.Cartesian3.clone(position, scratchCartesian2);
  88. const normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  89. const scaledNormal = Matrix2.Cartesian3.multiplyByScalar(
  90. normal,
  91. height,
  92. scratchCartesian3
  93. );
  94. Matrix2.Cartesian3.add(position, scaledNormal, position);
  95. if (extrude) {
  96. Matrix2.Cartesian3.multiplyByScalar(normal, extrudedHeight, scaledNormal);
  97. Matrix2.Cartesian3.add(extrudedPosition, scaledNormal, extrudedPosition);
  98. finalPositions[i + bottomOffset] = extrudedPosition.x;
  99. finalPositions[i1 + bottomOffset] = extrudedPosition.y;
  100. finalPositions[i2 + bottomOffset] = extrudedPosition.z;
  101. }
  102. finalPositions[i] = position.x;
  103. finalPositions[i1] = position.y;
  104. finalPositions[i2] = position.z;
  105. }
  106. return finalPositions;
  107. };
  108. const unitPosScratch = new Matrix2.Cartesian3();
  109. const eastVecScratch = new Matrix2.Cartesian3();
  110. const northVecScratch = new Matrix2.Cartesian3();
  111. /**
  112. * Returns an array of positions that make up the ellipse.
  113. * @private
  114. */
  115. EllipseGeometryLibrary.computeEllipsePositions = function (
  116. options,
  117. addFillPositions,
  118. addEdgePositions
  119. ) {
  120. const semiMinorAxis = options.semiMinorAxis;
  121. const semiMajorAxis = options.semiMajorAxis;
  122. const rotation = options.rotation;
  123. const center = options.center;
  124. // Computing the arc-length of the ellipse is too expensive to be practical. Estimating it using the
  125. // arc length of the sphere is too inaccurate and creates sharp edges when either the semi-major or
  126. // semi-minor axis is much bigger than the other. Instead, scale the angle delta to make
  127. // the distance along the ellipse boundary more closely match the granularity.
  128. const granularity = options.granularity * 8.0;
  129. const aSqr = semiMinorAxis * semiMinorAxis;
  130. const bSqr = semiMajorAxis * semiMajorAxis;
  131. const ab = semiMajorAxis * semiMinorAxis;
  132. const mag = Matrix2.Cartesian3.magnitude(center);
  133. const unitPos = Matrix2.Cartesian3.normalize(center, unitPosScratch);
  134. let eastVec = Matrix2.Cartesian3.cross(Matrix2.Cartesian3.UNIT_Z, center, eastVecScratch);
  135. eastVec = Matrix2.Cartesian3.normalize(eastVec, eastVec);
  136. const northVec = Matrix2.Cartesian3.cross(unitPos, eastVec, northVecScratch);
  137. // The number of points in the first quadrant
  138. let numPts = 1 + Math.ceil(ComponentDatatype.CesiumMath.PI_OVER_TWO / granularity);
  139. const deltaTheta = ComponentDatatype.CesiumMath.PI_OVER_TWO / (numPts - 1);
  140. let theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - numPts * deltaTheta;
  141. if (theta < 0.0) {
  142. numPts -= Math.ceil(Math.abs(theta) / deltaTheta);
  143. }
  144. // If the number of points were three, the ellipse
  145. // would be tessellated like below:
  146. //
  147. // *---*
  148. // / | \ | \
  149. // *---*---*---*
  150. // / | \ | \ | \ | \
  151. // / .*---*---*---*. \
  152. // * ` | \ | \ | \ | `*
  153. // \`.*---*---*---*.`/
  154. // \ | \ | \ | \ | /
  155. // *---*---*---*
  156. // \ | \ | /
  157. // *---*
  158. // The first and last column have one position and fan to connect to the adjacent column.
  159. // Each other vertical column contains an even number of positions.
  160. const size = 2 * (numPts * (numPts + 2));
  161. const positions = addFillPositions ? new Array(size * 3) : undefined;
  162. let positionIndex = 0;
  163. let position = scratchCartesian1;
  164. let reflectedPosition = scratchCartesian2;
  165. const outerPositionsLength = numPts * 4 * 3;
  166. let outerRightIndex = outerPositionsLength - 1;
  167. let outerLeftIndex = 0;
  168. const outerPositions = addEdgePositions
  169. ? new Array(outerPositionsLength)
  170. : undefined;
  171. let i;
  172. let j;
  173. let numInterior;
  174. let t;
  175. let interiorPosition;
  176. // Compute points in the 'eastern' half of the ellipse
  177. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO;
  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. if (addFillPositions) {
  191. positions[positionIndex++] = position.x;
  192. positions[positionIndex++] = position.y;
  193. positions[positionIndex++] = position.z;
  194. }
  195. if (addEdgePositions) {
  196. outerPositions[outerRightIndex--] = position.z;
  197. outerPositions[outerRightIndex--] = position.y;
  198. outerPositions[outerRightIndex--] = position.x;
  199. }
  200. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - deltaTheta;
  201. for (i = 1; i < numPts + 1; ++i) {
  202. position = pointOnEllipsoid(
  203. theta,
  204. rotation,
  205. northVec,
  206. eastVec,
  207. aSqr,
  208. ab,
  209. bSqr,
  210. mag,
  211. unitPos,
  212. position
  213. );
  214. reflectedPosition = pointOnEllipsoid(
  215. Math.PI - theta,
  216. rotation,
  217. northVec,
  218. eastVec,
  219. aSqr,
  220. ab,
  221. bSqr,
  222. mag,
  223. unitPos,
  224. reflectedPosition
  225. );
  226. if (addFillPositions) {
  227. positions[positionIndex++] = position.x;
  228. positions[positionIndex++] = position.y;
  229. positions[positionIndex++] = position.z;
  230. numInterior = 2 * i + 2;
  231. for (j = 1; j < numInterior - 1; ++j) {
  232. t = j / (numInterior - 1);
  233. interiorPosition = Matrix2.Cartesian3.lerp(
  234. position,
  235. reflectedPosition,
  236. t,
  237. scratchCartesian3
  238. );
  239. positions[positionIndex++] = interiorPosition.x;
  240. positions[positionIndex++] = interiorPosition.y;
  241. positions[positionIndex++] = interiorPosition.z;
  242. }
  243. positions[positionIndex++] = reflectedPosition.x;
  244. positions[positionIndex++] = reflectedPosition.y;
  245. positions[positionIndex++] = reflectedPosition.z;
  246. }
  247. if (addEdgePositions) {
  248. outerPositions[outerRightIndex--] = position.z;
  249. outerPositions[outerRightIndex--] = position.y;
  250. outerPositions[outerRightIndex--] = position.x;
  251. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  252. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  253. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  254. }
  255. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - (i + 1) * deltaTheta;
  256. }
  257. // Compute points in the 'western' half of the ellipse
  258. for (i = numPts; i > 1; --i) {
  259. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO - (i - 1) * deltaTheta;
  260. position = pointOnEllipsoid(
  261. -theta,
  262. rotation,
  263. northVec,
  264. eastVec,
  265. aSqr,
  266. ab,
  267. bSqr,
  268. mag,
  269. unitPos,
  270. position
  271. );
  272. reflectedPosition = pointOnEllipsoid(
  273. theta + Math.PI,
  274. rotation,
  275. northVec,
  276. eastVec,
  277. aSqr,
  278. ab,
  279. bSqr,
  280. mag,
  281. unitPos,
  282. reflectedPosition
  283. );
  284. if (addFillPositions) {
  285. positions[positionIndex++] = position.x;
  286. positions[positionIndex++] = position.y;
  287. positions[positionIndex++] = position.z;
  288. numInterior = 2 * (i - 1) + 2;
  289. for (j = 1; j < numInterior - 1; ++j) {
  290. t = j / (numInterior - 1);
  291. interiorPosition = Matrix2.Cartesian3.lerp(
  292. position,
  293. reflectedPosition,
  294. t,
  295. scratchCartesian3
  296. );
  297. positions[positionIndex++] = interiorPosition.x;
  298. positions[positionIndex++] = interiorPosition.y;
  299. positions[positionIndex++] = interiorPosition.z;
  300. }
  301. positions[positionIndex++] = reflectedPosition.x;
  302. positions[positionIndex++] = reflectedPosition.y;
  303. positions[positionIndex++] = reflectedPosition.z;
  304. }
  305. if (addEdgePositions) {
  306. outerPositions[outerRightIndex--] = position.z;
  307. outerPositions[outerRightIndex--] = position.y;
  308. outerPositions[outerRightIndex--] = position.x;
  309. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  310. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  311. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  312. }
  313. }
  314. theta = ComponentDatatype.CesiumMath.PI_OVER_TWO;
  315. position = pointOnEllipsoid(
  316. -theta,
  317. rotation,
  318. northVec,
  319. eastVec,
  320. aSqr,
  321. ab,
  322. bSqr,
  323. mag,
  324. unitPos,
  325. position
  326. );
  327. const r = {};
  328. if (addFillPositions) {
  329. positions[positionIndex++] = position.x;
  330. positions[positionIndex++] = position.y;
  331. positions[positionIndex++] = position.z;
  332. r.positions = positions;
  333. r.numPts = numPts;
  334. }
  335. if (addEdgePositions) {
  336. outerPositions[outerRightIndex--] = position.z;
  337. outerPositions[outerRightIndex--] = position.y;
  338. outerPositions[outerRightIndex--] = position.x;
  339. r.outerPositions = outerPositions;
  340. }
  341. return r;
  342. };
  343. exports.EllipseGeometryLibrary = EllipseGeometryLibrary;
  344. }));
  345. //# sourceMappingURL=EllipseGeometryLibrary-dac95924.js.map