|
| 1 | +/*global define*/ |
| 2 | +define([ |
| 3 | + '../Core/BoundingSphere', |
| 4 | + '../Core/Cartesian3', |
| 5 | + '../Core/Cartographic2', |
| 6 | + '../Core/Cartographic3', |
| 7 | + '../Core/DeveloperError', |
| 8 | + '../Core/Ellipsoid', |
| 9 | + '../Core/Math', |
| 10 | + '../Core/Occluder', |
| 11 | + '../Core/Rectangle' |
| 12 | + ], function( |
| 13 | + BoundingSphere, |
| 14 | + Cartesian3, |
| 15 | + Cartographic2, |
| 16 | + Cartographic3, |
| 17 | + DeveloperError, |
| 18 | + Ellipsoid, |
| 19 | + CesiumMath, |
| 20 | + Occluder, |
| 21 | + Rectangle) { |
| 22 | + "use strict"; |
| 23 | + |
| 24 | + /** |
| 25 | + * Two-dimensional coordinates given in latitude and longitude. |
| 26 | + * |
| 27 | + * @name Extent |
| 28 | + * @constructor |
| 29 | + * |
| 30 | + * @param {Number} north The northernmost latitude in the range [-Pi/2, Pi/2]. |
| 31 | + * @param {Number} east The easternmost longitude in the range [-Pi, Pi]. |
| 32 | + * @param {Number} south The southernmost latitude in the range [-Pi/2, Pi/2]. |
| 33 | + * @param {Number} west The westernmost longitude in the range [-Pi, Pi]. |
| 34 | + * |
| 35 | + * @exception {DeveloperError} One of the parameters is out of range. |
| 36 | + */ |
| 37 | + function Extent(west, south, east, north) { |
| 38 | + /** |
| 39 | + * The northernmost latitude. |
| 40 | + * |
| 41 | + * @type Number |
| 42 | + */ |
| 43 | + this.north = north; |
| 44 | + |
| 45 | + /** |
| 46 | + * The southernmost latitude. |
| 47 | + * |
| 48 | + * @type Number |
| 49 | + */ |
| 50 | + this.south = south; |
| 51 | + |
| 52 | + /** |
| 53 | + * The westernmost longitude. |
| 54 | + * |
| 55 | + * @type Number |
| 56 | + */ |
| 57 | + this.west = west; |
| 58 | + |
| 59 | + /** |
| 60 | + * The easternmost longitude. |
| 61 | + * |
| 62 | + * @type Number |
| 63 | + */ |
| 64 | + this.east = east; |
| 65 | + |
| 66 | + Extent.validate(this); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a duplicate of this Extent. |
| 71 | + * |
| 72 | + * @return {Extent} A new Extent instance. |
| 73 | + */ |
| 74 | + Extent.prototype.clone = function() { |
| 75 | + return new Extent(this.west, this.south, this.east, this.north); |
| 76 | + }; |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks that an {@link Extent}'s members are in the proper ranges, north is greater than south and east is greater than west. |
| 80 | + * |
| 81 | + * @param {Extent} extent The extent to be checked for validity. |
| 82 | + * |
| 83 | + * @exception {DeveloperError} <code>extent</code> is required and must have north, south, east and west attributes. |
| 84 | + * @exception {DeveloperError} <code>extent.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>]. |
| 85 | + * @exception {DeveloperError} <code>extent.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>]. |
| 86 | + * @exception {DeveloperError} <code>extent.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>]. |
| 87 | + * @exception {DeveloperError} <code>extent.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>]. |
| 88 | + */ |
| 89 | + Extent.validate = function(extent) { |
| 90 | + if (!extent || |
| 91 | + typeof extent.north === "undefined" || |
| 92 | + typeof extent.south === "undefined" || |
| 93 | + typeof extent.west === "undefined" || |
| 94 | + typeof extent.east === "undefined") { |
| 95 | + throw new DeveloperError("extent is required and must have north, south, east and west attributes.", "extent"); |
| 96 | + } |
| 97 | + |
| 98 | + if (extent.north < -CesiumMath.PI_OVER_TWO || extent.north > CesiumMath.PI_OVER_TWO) { |
| 99 | + throw new DeveloperError("extent.north must be in the interval [-Pi/2, Pi/2].", "extent.north"); |
| 100 | + } |
| 101 | + |
| 102 | + if (extent.south < -CesiumMath.PI_OVER_TWO || extent.south > CesiumMath.PI_OVER_TWO) { |
| 103 | + throw new DeveloperError("extent.south must be in the interval [-Pi/2, Pi/2].", "extent.south"); |
| 104 | + } |
| 105 | + |
| 106 | + if (extent.west < -CesiumMath.PI || extent.west > CesiumMath.PI) { |
| 107 | + throw new DeveloperError("extent.west must be in the interval [-Pi, Pi].", "extent.west"); |
| 108 | + } |
| 109 | + |
| 110 | + if (extent.east < -CesiumMath.PI || extent.east > CesiumMath.PI) { |
| 111 | + throw new DeveloperError("extent.east must be in the interval [-Pi, Pi].", "extent.east"); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + function getPosition(lla, ellipsoid, time, projection) { |
| 116 | + if (typeof time === 'undefined' || time === 0.0 || typeof projection === 'undefined') { |
| 117 | + return ellipsoid.toCartesian(lla); |
| 118 | + } |
| 119 | + |
| 120 | + var twod = projection.project(lla); |
| 121 | + twod = new Cartesian3(0.0, twod.x, twod.y); |
| 122 | + return twod.lerp(ellipsoid.toCartesian(lla), time); |
| 123 | + } |
| 124 | + |
| 125 | + Extent._computePositions = function(extent, ellipsoid, time, projection) { |
| 126 | + if (typeof extent === 'undefined') { |
| 127 | + throw new DeveloperError("extent is required.", "extent"); |
| 128 | + } |
| 129 | + |
| 130 | + Extent.validate(extent); |
| 131 | + |
| 132 | + ellipsoid = ellipsoid || Ellipsoid.WGS84; |
| 133 | + var positions = []; |
| 134 | + |
| 135 | + var lla = new Cartographic3(extent.west, extent.north, 0.0); |
| 136 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 137 | + lla.longitude = extent.east; |
| 138 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 139 | + lla.latitude = extent.south; |
| 140 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 141 | + lla.longitude = extent.west; |
| 142 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 143 | + |
| 144 | + if (extent.north < 0.0) { |
| 145 | + lla.latitude = extent.north; |
| 146 | + } else if (extent.south > 0.0) { |
| 147 | + lla.latitude = extent.south; |
| 148 | + } else { |
| 149 | + lla.latitude = 0.0; |
| 150 | + } |
| 151 | + |
| 152 | + for ( var i = 1; i < 8; ++i) { |
| 153 | + var temp = -Math.PI + i * CesiumMath.PI_OVER_TWO; |
| 154 | + if (extent.west < temp && temp < extent.east) { |
| 155 | + lla.longitude = temp; |
| 156 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (lla.latitude === 0.0) { |
| 161 | + lla.longitude = extent.west; |
| 162 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 163 | + lla.longitude = extent.east; |
| 164 | + positions.push(getPosition(lla, ellipsoid, time, projection)); |
| 165 | + } |
| 166 | + |
| 167 | + return positions; |
| 168 | + }; |
| 169 | + |
| 170 | + /** |
| 171 | + * DOC_TBA |
| 172 | + * |
| 173 | + * @param {Extent} extent DOC_TBA |
| 174 | + * @param {Ellipsoid} ellipsoid DOC_TBA |
| 175 | + * @param {Number} time DOC_TBA |
| 176 | + * @param {Object} projection DOC_TBA |
| 177 | + * |
| 178 | + * @returns {BoundingSphere} DOC_TBA |
| 179 | + */ |
| 180 | + Extent.computeMorphBoundingSphere = function(extent, ellipsoid, time, projection) { |
| 181 | + return new BoundingSphere(Extent._computePositions(extent, ellipsoid, time, projection)); |
| 182 | + }; |
| 183 | + |
| 184 | + /** |
| 185 | + * DOC_TBA |
| 186 | + * |
| 187 | + * @param {Extent} extent DOC_TBA |
| 188 | + * @param {Ellipsoid} ellipsoid DOC_TBA |
| 189 | + * @returns {BoundingSphere} DOC_TBA |
| 190 | + */ |
| 191 | + Extent.compute3DBoundingSphere = function(extent, ellipsoid) { |
| 192 | + return new BoundingSphere(Extent._computePositions(extent, ellipsoid)); |
| 193 | + }; |
| 194 | + |
| 195 | + /** |
| 196 | + * DOC_TBA |
| 197 | + * |
| 198 | + * @param {Extent} extent DOC_TBA |
| 199 | + * @param {Ellipsoid} ellipsoid DOC_TBA |
| 200 | + * |
| 201 | + * @returns {Object} DOC_TBA |
| 202 | + */ |
| 203 | + Extent.computeOccludeePoint = function(extent, ellipsoid) { |
| 204 | + ellipsoid = ellipsoid || Ellipsoid.WGS84; |
| 205 | + var positions = Extent._computePositions(extent, ellipsoid); |
| 206 | + var bs = new BoundingSphere(positions); |
| 207 | + |
| 208 | + // TODO: get correct ellipsoid center |
| 209 | + var ellipsoidCenter = Cartesian3.ZERO; |
| 210 | + if (!ellipsoidCenter.equals(bs.center)) { |
| 211 | + return Occluder.getOccludeePoint(new BoundingSphere(ellipsoidCenter, ellipsoid.getMinimumRadius()), bs.center, positions); |
| 212 | + } |
| 213 | + return { |
| 214 | + valid : false |
| 215 | + }; |
| 216 | + }; |
| 217 | + |
| 218 | + /** |
| 219 | + * DOC_TBA |
| 220 | + * |
| 221 | + * @param {Extent} extent DOC_TBA |
| 222 | + * @param {Object} projection DOC_TBA |
| 223 | + * @returns {Rectangle} DOC_TBA |
| 224 | + */ |
| 225 | + Extent.computeBoundingRectangle = function(extent, projection) { |
| 226 | + if (typeof extent === 'undefined') { |
| 227 | + throw new DeveloperError("extent is required.", "extent"); |
| 228 | + } |
| 229 | + |
| 230 | + Extent.validate(extent); |
| 231 | + |
| 232 | + if (typeof projection === 'undefined') { |
| 233 | + throw new DeveloperError("projection is required.", "projection"); |
| 234 | + } |
| 235 | + |
| 236 | + var lla = new Cartographic2(extent.west, extent.south); |
| 237 | + var lowerLeft = projection.project(lla); |
| 238 | + lla.longitude = extent.east; |
| 239 | + lla.latitude = extent.north; |
| 240 | + var upperRight = projection.project(lla); |
| 241 | + |
| 242 | + var diagonal = upperRight.subtract(lowerLeft); |
| 243 | + return new Rectangle(lowerLeft.x, lowerLeft.y, diagonal.x, diagonal.y); |
| 244 | + }; |
| 245 | + |
| 246 | + /** |
| 247 | + * DOC_TBA |
| 248 | + * |
| 249 | + * @param {Extent} extent DOC_TBA |
| 250 | + * @param {Object} projection DOC_TBA |
| 251 | + * @returns {BoundingSphere} DOC_TBA |
| 252 | + */ |
| 253 | + Extent.compute2DBoundingSphere = function(extent, projection) { |
| 254 | + var rect = Extent.computeBoundingRectangle(extent, projection); |
| 255 | + var center = new Cartesian3((2.0 * rect.x + rect.width) * 0.5, (2.0 * rect.y + rect.height) * 0.5, 0.0); |
| 256 | + var radius = Math.sqrt(rect.width * rect.width + rect.height * rect.height) * 0.5; |
| 257 | + return new BoundingSphere(center, radius); |
| 258 | + }; |
| 259 | + |
| 260 | + return Extent; |
| 261 | +}); |
0 commit comments