RectangleGeometryLibrary-fc273e18.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './GeometryAttribute-91704ebb'], function (exports, when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, GeometryAttribute) { 'use strict';
  24. var cos = Math.cos;
  25. var sin = Math.sin;
  26. var sqrt = Math.sqrt;
  27. /**
  28. * @private
  29. */
  30. var RectangleGeometryLibrary = {};
  31. /**
  32. * @private
  33. */
  34. RectangleGeometryLibrary.computePosition = function(computedOptions, ellipsoid, computeST, row, col, position, st) {
  35. var radiiSquared = ellipsoid.radiiSquared;
  36. var nwCorner = computedOptions.nwCorner;
  37. var rectangle = computedOptions.boundingRectangle;
  38. var stLatitude = nwCorner.latitude - computedOptions.granYCos * row + col * computedOptions.granXSin;
  39. var cosLatitude = cos(stLatitude);
  40. var nZ = sin(stLatitude);
  41. var kZ = radiiSquared.z * nZ;
  42. var stLongitude = nwCorner.longitude + row * computedOptions.granYSin + col * computedOptions.granXCos;
  43. var nX = cosLatitude * cos(stLongitude);
  44. var nY = cosLatitude * sin(stLongitude);
  45. var kX = radiiSquared.x * nX;
  46. var kY = radiiSquared.y * nY;
  47. var gamma = sqrt((kX * nX) + (kY * nY) + (kZ * nZ));
  48. position.x = kX / gamma;
  49. position.y = kY / gamma;
  50. position.z = kZ / gamma;
  51. if (computeST) {
  52. var stNwCorner = computedOptions.stNwCorner;
  53. if (when.defined(stNwCorner)) {
  54. stLatitude = stNwCorner.latitude - computedOptions.stGranYCos * row + col * computedOptions.stGranXSin;
  55. stLongitude = stNwCorner.longitude + row * computedOptions.stGranYSin + col * computedOptions.stGranXCos;
  56. st.x = (stLongitude - computedOptions.stWest) * computedOptions.lonScalar;
  57. st.y = (stLatitude - computedOptions.stSouth) * computedOptions.latScalar;
  58. } else {
  59. st.x = (stLongitude - rectangle.west) * computedOptions.lonScalar;
  60. st.y = (stLatitude - rectangle.south) * computedOptions.latScalar;
  61. }
  62. }
  63. };
  64. var rotationMatrixScratch = new GeometryAttribute.Matrix2();
  65. var nwCartesian = new Cartographic.Cartesian3();
  66. var centerScratch = new Cartographic.Cartographic();
  67. var centerCartesian = new Cartographic.Cartesian3();
  68. var proj = new BoundingSphere.GeographicProjection();
  69. function getRotationOptions(nwCorner, rotation, granularityX, granularityY, center, width, height) {
  70. var cosRotation = Math.cos(rotation);
  71. var granYCos = granularityY * cosRotation;
  72. var granXCos = granularityX * cosRotation;
  73. var sinRotation = Math.sin(rotation);
  74. var granYSin = granularityY * sinRotation;
  75. var granXSin = granularityX * sinRotation;
  76. nwCartesian = proj.project(nwCorner, nwCartesian);
  77. nwCartesian = Cartographic.Cartesian3.subtract(nwCartesian, centerCartesian, nwCartesian);
  78. var rotationMatrix = GeometryAttribute.Matrix2.fromRotation(rotation, rotationMatrixScratch);
  79. nwCartesian = GeometryAttribute.Matrix2.multiplyByVector(rotationMatrix, nwCartesian, nwCartesian);
  80. nwCartesian = Cartographic.Cartesian3.add(nwCartesian, centerCartesian, nwCartesian);
  81. nwCorner = proj.unproject(nwCartesian, nwCorner);
  82. width -= 1;
  83. height -= 1;
  84. var latitude = nwCorner.latitude;
  85. var latitude0 = latitude + width * granXSin;
  86. var latitude1 = latitude - granYCos * height;
  87. var latitude2 = latitude - granYCos * height + width * granXSin;
  88. var north = Math.max(latitude, latitude0, latitude1, latitude2);
  89. var south = Math.min(latitude, latitude0, latitude1, latitude2);
  90. var longitude = nwCorner.longitude;
  91. var longitude0 = longitude + width * granXCos;
  92. var longitude1 = longitude + height * granYSin;
  93. var longitude2 = longitude + height * granYSin + width * granXCos;
  94. var east = Math.max(longitude, longitude0, longitude1, longitude2);
  95. var west = Math.min(longitude, longitude0, longitude1, longitude2);
  96. return {
  97. north: north,
  98. south: south,
  99. east: east,
  100. west: west,
  101. granYCos : granYCos,
  102. granYSin : granYSin,
  103. granXCos : granXCos,
  104. granXSin : granXSin,
  105. nwCorner : nwCorner
  106. };
  107. }
  108. /**
  109. * @private
  110. */
  111. RectangleGeometryLibrary.computeOptions = function(rectangle, granularity, rotation, stRotation, boundingRectangleScratch, nwCornerResult, stNwCornerResult) {
  112. var east = rectangle.east;
  113. var west = rectangle.west;
  114. var north = rectangle.north;
  115. var south = rectangle.south;
  116. var northCap = false;
  117. var southCap = false;
  118. if (north === _Math.CesiumMath.PI_OVER_TWO) {
  119. northCap = true;
  120. }
  121. if (south === -_Math.CesiumMath.PI_OVER_TWO) {
  122. southCap = true;
  123. }
  124. var width;
  125. var height;
  126. var granularityX;
  127. var granularityY;
  128. var dx;
  129. var dy = north - south;
  130. if (west > east) {
  131. dx = (_Math.CesiumMath.TWO_PI - west + east);
  132. } else {
  133. dx = east - west;
  134. }
  135. width = Math.ceil(dx / granularity) + 1;
  136. height = Math.ceil(dy / granularity) + 1;
  137. granularityX = dx / (width - 1);
  138. granularityY = dy / (height - 1);
  139. var nwCorner = Cartesian2.Rectangle.northwest(rectangle, nwCornerResult);
  140. var center = Cartesian2.Rectangle.center(rectangle, centerScratch);
  141. if (rotation !== 0 || stRotation !== 0) {
  142. if (center.longitude < nwCorner.longitude) {
  143. center.longitude += _Math.CesiumMath.TWO_PI;
  144. }
  145. centerCartesian = proj.project(center, centerCartesian);
  146. }
  147. var granYCos = granularityY;
  148. var granXCos = granularityX;
  149. var granYSin = 0.0;
  150. var granXSin = 0.0;
  151. var boundingRectangle = Cartesian2.Rectangle.clone(rectangle, boundingRectangleScratch);
  152. var computedOptions = {
  153. granYCos : granYCos,
  154. granYSin : granYSin,
  155. granXCos : granXCos,
  156. granXSin : granXSin,
  157. nwCorner : nwCorner,
  158. boundingRectangle : boundingRectangle,
  159. width: width,
  160. height: height,
  161. northCap: northCap,
  162. southCap: southCap
  163. };
  164. if (rotation !== 0) {
  165. var rotationOptions = getRotationOptions(nwCorner, rotation, granularityX, granularityY, center, width, height);
  166. north = rotationOptions.north;
  167. south = rotationOptions.south;
  168. east = rotationOptions.east;
  169. west = rotationOptions.west;
  170. //>>includeStart('debug', pragmas.debug);
  171. if (north < -_Math.CesiumMath.PI_OVER_TWO || north > _Math.CesiumMath.PI_OVER_TWO ||
  172. south < -_Math.CesiumMath.PI_OVER_TWO || south > _Math.CesiumMath.PI_OVER_TWO) {
  173. throw new Check.DeveloperError('Rotated rectangle is invalid. It crosses over either the north or south pole.');
  174. }
  175. //>>includeEnd('debug')
  176. computedOptions.granYCos = rotationOptions.granYCos;
  177. computedOptions.granYSin = rotationOptions.granYSin;
  178. computedOptions.granXCos = rotationOptions.granXCos;
  179. computedOptions.granXSin = rotationOptions.granXSin;
  180. boundingRectangle.north = north;
  181. boundingRectangle.south = south;
  182. boundingRectangle.east = east;
  183. boundingRectangle.west = west;
  184. }
  185. if (stRotation !== 0) {
  186. rotation = rotation - stRotation;
  187. var stNwCorner = Cartesian2.Rectangle.northwest(boundingRectangle, stNwCornerResult);
  188. var stRotationOptions = getRotationOptions(stNwCorner, rotation, granularityX, granularityY, center, width, height);
  189. computedOptions.stGranYCos = stRotationOptions.granYCos;
  190. computedOptions.stGranXCos = stRotationOptions.granXCos;
  191. computedOptions.stGranYSin = stRotationOptions.granYSin;
  192. computedOptions.stGranXSin = stRotationOptions.granXSin;
  193. computedOptions.stNwCorner = stNwCorner;
  194. computedOptions.stWest = stRotationOptions.west;
  195. computedOptions.stSouth = stRotationOptions.south;
  196. }
  197. return computedOptions;
  198. };
  199. exports.RectangleGeometryLibrary = RectangleGeometryLibrary;
  200. });