EllipseGeometryLibrary.js 11 KB

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