EllipseGeometryLibrary-de9b1bfe.js 12 KB

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