EllipseGeometryLibrary-08dbcdef.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './Math-61ede240', './Cartographic-fe4be337', './BoundingSphere-775c5788', './Transforms-b2e71640'], function (exports, _Math, Cartographic, BoundingSphere, Transforms) { 'use strict';
  24. var EllipseGeometryLibrary = {};
  25. var rotAxis = new Cartographic.Cartesian3();
  26. var tempVec = new Cartographic.Cartesian3();
  27. var unitQuat = new Transforms.Quaternion();
  28. var rotMtx = new BoundingSphere.Matrix3();
  29. function pointOnEllipsoid(theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, result) {
  30. var azimuth = theta + rotation;
  31. Cartographic.Cartesian3.multiplyByScalar(eastVec, Math.cos(azimuth), rotAxis);
  32. Cartographic.Cartesian3.multiplyByScalar(northVec, Math.sin(azimuth), tempVec);
  33. Cartographic.Cartesian3.add(rotAxis, tempVec, rotAxis);
  34. var cosThetaSquared = Math.cos(theta);
  35. cosThetaSquared = cosThetaSquared * cosThetaSquared;
  36. var sinThetaSquared = Math.sin(theta);
  37. sinThetaSquared = sinThetaSquared * sinThetaSquared;
  38. var radius = ab / Math.sqrt(bSqr * cosThetaSquared + aSqr * sinThetaSquared);
  39. var angle = radius / mag;
  40. // Create the quaternion to rotate the position vector to the boundary of the ellipse.
  41. Transforms.Quaternion.fromAxisAngle(rotAxis, angle, unitQuat);
  42. BoundingSphere.Matrix3.fromQuaternion(unitQuat, rotMtx);
  43. BoundingSphere.Matrix3.multiplyByVector(rotMtx, unitPos, result);
  44. Cartographic.Cartesian3.normalize(result, result);
  45. Cartographic.Cartesian3.multiplyByScalar(result, mag, result);
  46. return result;
  47. }
  48. var scratchCartesian1 = new Cartographic.Cartesian3();
  49. var scratchCartesian2 = new Cartographic.Cartesian3();
  50. var scratchCartesian3 = new Cartographic.Cartesian3();
  51. var scratchNormal = new Cartographic.Cartesian3();
  52. /**
  53. * Returns the positions raised to the given heights
  54. * @private
  55. */
  56. EllipseGeometryLibrary.raisePositionsToHeight = function(positions, options, extrude) {
  57. var ellipsoid = options.ellipsoid;
  58. var height = options.height;
  59. var extrudedHeight = options.extrudedHeight;
  60. var size = (extrude) ? positions.length / 3 * 2 : positions.length / 3;
  61. var finalPositions = new Float64Array(size * 3);
  62. var length = positions.length;
  63. var bottomOffset = (extrude) ? length : 0;
  64. for (var i = 0; i < length; i += 3) {
  65. var i1 = i + 1;
  66. var i2 = i + 2;
  67. var position = Cartographic.Cartesian3.fromArray(positions, i, scratchCartesian1);
  68. ellipsoid.scaleToGeodeticSurface(position, position);
  69. var extrudedPosition = Cartographic.Cartesian3.clone(position, scratchCartesian2);
  70. var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  71. var scaledNormal = Cartographic.Cartesian3.multiplyByScalar(normal, height, scratchCartesian3);
  72. Cartographic.Cartesian3.add(position, scaledNormal, position);
  73. if (extrude) {
  74. Cartographic.Cartesian3.multiplyByScalar(normal, extrudedHeight, scaledNormal);
  75. Cartographic.Cartesian3.add(extrudedPosition, scaledNormal, extrudedPosition);
  76. finalPositions[i + bottomOffset] = extrudedPosition.x;
  77. finalPositions[i1 + bottomOffset] = extrudedPosition.y;
  78. finalPositions[i2 + bottomOffset] = extrudedPosition.z;
  79. }
  80. finalPositions[i] = position.x;
  81. finalPositions[i1] = position.y;
  82. finalPositions[i2] = position.z;
  83. }
  84. return finalPositions;
  85. };
  86. var unitPosScratch = new Cartographic.Cartesian3();
  87. var eastVecScratch = new Cartographic.Cartesian3();
  88. var northVecScratch = new Cartographic.Cartesian3();
  89. /**
  90. * Returns an array of positions that make up the ellipse.
  91. * @private
  92. */
  93. EllipseGeometryLibrary.computeEllipsePositions = function(options, addFillPositions, addEdgePositions) {
  94. var semiMinorAxis = options.semiMinorAxis;
  95. var semiMajorAxis = options.semiMajorAxis;
  96. var rotation = options.rotation;
  97. var center = options.center;
  98. // Computing the arc-length of the ellipse is too expensive to be practical. Estimating it using the
  99. // arc length of the sphere is too inaccurate and creates sharp edges when either the semi-major or
  100. // semi-minor axis is much bigger than the other. Instead, scale the angle delta to make
  101. // the distance along the ellipse boundary more closely match the granularity.
  102. var granularity = options.granularity * 8.0;
  103. var aSqr = semiMinorAxis * semiMinorAxis;
  104. var bSqr = semiMajorAxis * semiMajorAxis;
  105. var ab = semiMajorAxis * semiMinorAxis;
  106. var mag = Cartographic.Cartesian3.magnitude(center);
  107. var unitPos = Cartographic.Cartesian3.normalize(center, unitPosScratch);
  108. var eastVec = Cartographic.Cartesian3.cross(Cartographic.Cartesian3.UNIT_Z, center, eastVecScratch);
  109. eastVec = Cartographic.Cartesian3.normalize(eastVec, eastVec);
  110. var northVec = Cartographic.Cartesian3.cross(unitPos, eastVec, northVecScratch);
  111. // The number of points in the first quadrant
  112. var numPts = 1 + Math.ceil(_Math.CesiumMath.PI_OVER_TWO / granularity);
  113. var deltaTheta = _Math.CesiumMath.PI_OVER_TWO / (numPts - 1);
  114. var theta = _Math.CesiumMath.PI_OVER_TWO - numPts * deltaTheta;
  115. if (theta < 0.0) {
  116. numPts -= Math.ceil(Math.abs(theta) / deltaTheta);
  117. }
  118. // If the number of points were three, the ellipse
  119. // would be tessellated like below:
  120. //
  121. // *---*
  122. // / | \ | \
  123. // *---*---*---*
  124. // / | \ | \ | \ | \
  125. // / .*---*---*---*. \
  126. // * ` | \ | \ | \ | `*
  127. // \`.*---*---*---*.`/
  128. // \ | \ | \ | \ | /
  129. // *---*---*---*
  130. // \ | \ | /
  131. // *---*
  132. // The first and last column have one position and fan to connect to the adjacent column.
  133. // Each other vertical column contains an even number of positions.
  134. var size = 2 * (numPts * (numPts + 2));
  135. var positions = (addFillPositions) ? new Array(size * 3) : undefined;
  136. var positionIndex = 0;
  137. var position = scratchCartesian1;
  138. var reflectedPosition = scratchCartesian2;
  139. var outerPositionsLength = (numPts * 4) * 3;
  140. var outerRightIndex = outerPositionsLength - 1;
  141. var outerLeftIndex = 0;
  142. var outerPositions = (addEdgePositions) ? new Array(outerPositionsLength) : undefined;
  143. var i;
  144. var j;
  145. var numInterior;
  146. var t;
  147. var interiorPosition;
  148. // Compute points in the 'eastern' half of the ellipse
  149. theta = _Math.CesiumMath.PI_OVER_TWO;
  150. position = pointOnEllipsoid(theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, position);
  151. if (addFillPositions) {
  152. positions[positionIndex++] = position.x;
  153. positions[positionIndex++] = position.y;
  154. positions[positionIndex++] = position.z;
  155. }
  156. if (addEdgePositions) {
  157. outerPositions[outerRightIndex--] = position.z;
  158. outerPositions[outerRightIndex--] = position.y;
  159. outerPositions[outerRightIndex--] = position.x;
  160. }
  161. theta = _Math.CesiumMath.PI_OVER_TWO - deltaTheta;
  162. for (i = 1; i < numPts + 1; ++i) {
  163. position = pointOnEllipsoid(theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, position);
  164. reflectedPosition = pointOnEllipsoid(Math.PI - theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, reflectedPosition);
  165. if (addFillPositions) {
  166. positions[positionIndex++] = position.x;
  167. positions[positionIndex++] = position.y;
  168. positions[positionIndex++] = position.z;
  169. numInterior = 2 * i + 2;
  170. for (j = 1; j < numInterior - 1; ++j) {
  171. t = j / (numInterior - 1);
  172. interiorPosition = Cartographic.Cartesian3.lerp(position, reflectedPosition, t, scratchCartesian3);
  173. positions[positionIndex++] = interiorPosition.x;
  174. positions[positionIndex++] = interiorPosition.y;
  175. positions[positionIndex++] = interiorPosition.z;
  176. }
  177. positions[positionIndex++] = reflectedPosition.x;
  178. positions[positionIndex++] = reflectedPosition.y;
  179. positions[positionIndex++] = reflectedPosition.z;
  180. }
  181. if (addEdgePositions) {
  182. outerPositions[outerRightIndex--] = position.z;
  183. outerPositions[outerRightIndex--] = position.y;
  184. outerPositions[outerRightIndex--] = position.x;
  185. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  186. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  187. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  188. }
  189. theta = _Math.CesiumMath.PI_OVER_TWO - (i + 1) * deltaTheta;
  190. }
  191. // Compute points in the 'western' half of the ellipse
  192. for (i = numPts; i > 1; --i) {
  193. theta = _Math.CesiumMath.PI_OVER_TWO - (i - 1) * deltaTheta;
  194. position = pointOnEllipsoid(-theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, position);
  195. reflectedPosition = pointOnEllipsoid(theta + Math.PI, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, reflectedPosition);
  196. if (addFillPositions) {
  197. positions[positionIndex++] = position.x;
  198. positions[positionIndex++] = position.y;
  199. positions[positionIndex++] = position.z;
  200. numInterior = 2 * (i - 1) + 2;
  201. for (j = 1; j < numInterior - 1; ++j) {
  202. t = j / (numInterior - 1);
  203. interiorPosition = Cartographic.Cartesian3.lerp(position, reflectedPosition, t, scratchCartesian3);
  204. positions[positionIndex++] = interiorPosition.x;
  205. positions[positionIndex++] = interiorPosition.y;
  206. positions[positionIndex++] = interiorPosition.z;
  207. }
  208. positions[positionIndex++] = reflectedPosition.x;
  209. positions[positionIndex++] = reflectedPosition.y;
  210. positions[positionIndex++] = reflectedPosition.z;
  211. }
  212. if (addEdgePositions) {
  213. outerPositions[outerRightIndex--] = position.z;
  214. outerPositions[outerRightIndex--] = position.y;
  215. outerPositions[outerRightIndex--] = position.x;
  216. outerPositions[outerLeftIndex++] = reflectedPosition.x;
  217. outerPositions[outerLeftIndex++] = reflectedPosition.y;
  218. outerPositions[outerLeftIndex++] = reflectedPosition.z;
  219. }
  220. }
  221. theta = _Math.CesiumMath.PI_OVER_TWO;
  222. position = pointOnEllipsoid(-theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, position);
  223. var r = {};
  224. if (addFillPositions) {
  225. positions[positionIndex++] = position.x;
  226. positions[positionIndex++] = position.y;
  227. positions[positionIndex++] = position.z;
  228. r.positions = positions;
  229. r.numPts = numPts;
  230. }
  231. if (addEdgePositions) {
  232. outerPositions[outerRightIndex--] = position.z;
  233. outerPositions[outerRightIndex--] = position.y;
  234. outerPositions[outerRightIndex--] = position.x;
  235. r.outerPositions = outerPositions;
  236. }
  237. return r;
  238. };
  239. exports.EllipseGeometryLibrary = EllipseGeometryLibrary;
  240. });