RectangleGeometryLibrary-b5624ac4.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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', './defaultValue-81eec7ed', './RuntimeError-8952249c', './Transforms-f0a54c7b', './ComponentDatatype-9e86ac8f'], (function (exports, Matrix2, defaultValue, RuntimeError, Transforms, ComponentDatatype) { 'use strict';
  24. const cos = Math.cos;
  25. const sin = Math.sin;
  26. const sqrt = Math.sqrt;
  27. /**
  28. * @private
  29. */
  30. const RectangleGeometryLibrary = {};
  31. /**
  32. * @private
  33. */
  34. RectangleGeometryLibrary.computePosition = function (
  35. computedOptions,
  36. ellipsoid,
  37. computeST,
  38. row,
  39. col,
  40. position,
  41. st
  42. ) {
  43. const radiiSquared = ellipsoid.radiiSquared;
  44. const nwCorner = computedOptions.nwCorner;
  45. const rectangle = computedOptions.boundingRectangle;
  46. let stLatitude =
  47. nwCorner.latitude -
  48. computedOptions.granYCos * row +
  49. col * computedOptions.granXSin;
  50. const cosLatitude = cos(stLatitude);
  51. const nZ = sin(stLatitude);
  52. const kZ = radiiSquared.z * nZ;
  53. let stLongitude =
  54. nwCorner.longitude +
  55. row * computedOptions.granYSin +
  56. col * computedOptions.granXCos;
  57. const nX = cosLatitude * cos(stLongitude);
  58. const nY = cosLatitude * sin(stLongitude);
  59. const kX = radiiSquared.x * nX;
  60. const kY = radiiSquared.y * nY;
  61. const gamma = sqrt(kX * nX + kY * nY + kZ * nZ);
  62. position.x = kX / gamma;
  63. position.y = kY / gamma;
  64. position.z = kZ / gamma;
  65. if (computeST) {
  66. const stNwCorner = computedOptions.stNwCorner;
  67. if (defaultValue.defined(stNwCorner)) {
  68. stLatitude =
  69. stNwCorner.latitude -
  70. computedOptions.stGranYCos * row +
  71. col * computedOptions.stGranXSin;
  72. stLongitude =
  73. stNwCorner.longitude +
  74. row * computedOptions.stGranYSin +
  75. col * computedOptions.stGranXCos;
  76. st.x = (stLongitude - computedOptions.stWest) * computedOptions.lonScalar;
  77. st.y = (stLatitude - computedOptions.stSouth) * computedOptions.latScalar;
  78. } else {
  79. st.x = (stLongitude - rectangle.west) * computedOptions.lonScalar;
  80. st.y = (stLatitude - rectangle.south) * computedOptions.latScalar;
  81. }
  82. }
  83. };
  84. const rotationMatrixScratch = new Matrix2.Matrix2();
  85. let nwCartesian = new Matrix2.Cartesian3();
  86. const centerScratch = new Matrix2.Cartographic();
  87. let centerCartesian = new Matrix2.Cartesian3();
  88. const proj = new Transforms.GeographicProjection();
  89. function getRotationOptions(
  90. nwCorner,
  91. rotation,
  92. granularityX,
  93. granularityY,
  94. center,
  95. width,
  96. height
  97. ) {
  98. const cosRotation = Math.cos(rotation);
  99. const granYCos = granularityY * cosRotation;
  100. const granXCos = granularityX * cosRotation;
  101. const sinRotation = Math.sin(rotation);
  102. const granYSin = granularityY * sinRotation;
  103. const granXSin = granularityX * sinRotation;
  104. nwCartesian = proj.project(nwCorner, nwCartesian);
  105. nwCartesian = Matrix2.Cartesian3.subtract(nwCartesian, centerCartesian, nwCartesian);
  106. const rotationMatrix = Matrix2.Matrix2.fromRotation(rotation, rotationMatrixScratch);
  107. nwCartesian = Matrix2.Matrix2.multiplyByVector(
  108. rotationMatrix,
  109. nwCartesian,
  110. nwCartesian
  111. );
  112. nwCartesian = Matrix2.Cartesian3.add(nwCartesian, centerCartesian, nwCartesian);
  113. nwCorner = proj.unproject(nwCartesian, nwCorner);
  114. width -= 1;
  115. height -= 1;
  116. const latitude = nwCorner.latitude;
  117. const latitude0 = latitude + width * granXSin;
  118. const latitude1 = latitude - granYCos * height;
  119. const latitude2 = latitude - granYCos * height + width * granXSin;
  120. const north = Math.max(latitude, latitude0, latitude1, latitude2);
  121. const south = Math.min(latitude, latitude0, latitude1, latitude2);
  122. const longitude = nwCorner.longitude;
  123. const longitude0 = longitude + width * granXCos;
  124. const longitude1 = longitude + height * granYSin;
  125. const longitude2 = longitude + height * granYSin + width * granXCos;
  126. const east = Math.max(longitude, longitude0, longitude1, longitude2);
  127. const west = Math.min(longitude, longitude0, longitude1, longitude2);
  128. return {
  129. north: north,
  130. south: south,
  131. east: east,
  132. west: west,
  133. granYCos: granYCos,
  134. granYSin: granYSin,
  135. granXCos: granXCos,
  136. granXSin: granXSin,
  137. nwCorner: nwCorner,
  138. };
  139. }
  140. /**
  141. * @private
  142. */
  143. RectangleGeometryLibrary.computeOptions = function (
  144. rectangle,
  145. granularity,
  146. rotation,
  147. stRotation,
  148. boundingRectangleScratch,
  149. nwCornerResult,
  150. stNwCornerResult
  151. ) {
  152. let east = rectangle.east;
  153. let west = rectangle.west;
  154. let north = rectangle.north;
  155. let south = rectangle.south;
  156. let northCap = false;
  157. let southCap = false;
  158. if (north === ComponentDatatype.CesiumMath.PI_OVER_TWO) {
  159. northCap = true;
  160. }
  161. if (south === -ComponentDatatype.CesiumMath.PI_OVER_TWO) {
  162. southCap = true;
  163. }
  164. let dx;
  165. const dy = north - south;
  166. if (west > east) {
  167. dx = ComponentDatatype.CesiumMath.TWO_PI - west + east;
  168. } else {
  169. dx = east - west;
  170. }
  171. const width = Math.ceil(dx / granularity) + 1;
  172. const height = Math.ceil(dy / granularity) + 1;
  173. const granularityX = dx / (width - 1);
  174. const granularityY = dy / (height - 1);
  175. const nwCorner = Matrix2.Rectangle.northwest(rectangle, nwCornerResult);
  176. const center = Matrix2.Rectangle.center(rectangle, centerScratch);
  177. if (rotation !== 0 || stRotation !== 0) {
  178. if (center.longitude < nwCorner.longitude) {
  179. center.longitude += ComponentDatatype.CesiumMath.TWO_PI;
  180. }
  181. centerCartesian = proj.project(center, centerCartesian);
  182. }
  183. const granYCos = granularityY;
  184. const granXCos = granularityX;
  185. const granYSin = 0.0;
  186. const granXSin = 0.0;
  187. const boundingRectangle = Matrix2.Rectangle.clone(
  188. rectangle,
  189. boundingRectangleScratch
  190. );
  191. const computedOptions = {
  192. granYCos: granYCos,
  193. granYSin: granYSin,
  194. granXCos: granXCos,
  195. granXSin: granXSin,
  196. nwCorner: nwCorner,
  197. boundingRectangle: boundingRectangle,
  198. width: width,
  199. height: height,
  200. northCap: northCap,
  201. southCap: southCap,
  202. };
  203. if (rotation !== 0) {
  204. const rotationOptions = getRotationOptions(
  205. nwCorner,
  206. rotation,
  207. granularityX,
  208. granularityY,
  209. center,
  210. width,
  211. height
  212. );
  213. north = rotationOptions.north;
  214. south = rotationOptions.south;
  215. east = rotationOptions.east;
  216. west = rotationOptions.west;
  217. //>>includeStart('debug', pragmas.debug);
  218. if (
  219. north < -ComponentDatatype.CesiumMath.PI_OVER_TWO ||
  220. north > ComponentDatatype.CesiumMath.PI_OVER_TWO ||
  221. south < -ComponentDatatype.CesiumMath.PI_OVER_TWO ||
  222. south > ComponentDatatype.CesiumMath.PI_OVER_TWO
  223. ) {
  224. throw new RuntimeError.DeveloperError(
  225. "Rotated rectangle is invalid. It crosses over either the north or south pole."
  226. );
  227. }
  228. //>>includeEnd('debug')
  229. computedOptions.granYCos = rotationOptions.granYCos;
  230. computedOptions.granYSin = rotationOptions.granYSin;
  231. computedOptions.granXCos = rotationOptions.granXCos;
  232. computedOptions.granXSin = rotationOptions.granXSin;
  233. boundingRectangle.north = north;
  234. boundingRectangle.south = south;
  235. boundingRectangle.east = east;
  236. boundingRectangle.west = west;
  237. }
  238. if (stRotation !== 0) {
  239. rotation = rotation - stRotation;
  240. const stNwCorner = Matrix2.Rectangle.northwest(boundingRectangle, stNwCornerResult);
  241. const stRotationOptions = getRotationOptions(
  242. stNwCorner,
  243. rotation,
  244. granularityX,
  245. granularityY,
  246. center,
  247. width,
  248. height
  249. );
  250. computedOptions.stGranYCos = stRotationOptions.granYCos;
  251. computedOptions.stGranXCos = stRotationOptions.granXCos;
  252. computedOptions.stGranYSin = stRotationOptions.granYSin;
  253. computedOptions.stGranXSin = stRotationOptions.granXSin;
  254. computedOptions.stNwCorner = stNwCorner;
  255. computedOptions.stWest = stRotationOptions.west;
  256. computedOptions.stSouth = stRotationOptions.south;
  257. }
  258. return computedOptions;
  259. };
  260. exports.RectangleGeometryLibrary = RectangleGeometryLibrary;
  261. }));
  262. //# sourceMappingURL=RectangleGeometryLibrary-b5624ac4.js.map