Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | 1x 1x 1x 1x 1x 1x | import defined from "../../Core/defined.js";
import Rectangle from "../../Core/Rectangle.js";
import CartesianRectangle from "./CartesianRectangle.js";
const imageryBoundsScratch = new Rectangle();
const overlappedRectangleScratch = new Rectangle();
const clippedRectangleScratch = new Rectangle();
const nativeInputRectangleScratch = new Rectangle();
const nativeImageryBoundsScratch = new Rectangle();
const nativeClippedImageryBoundsScratch = new Rectangle();
/**
* A class containing information about a piece of imagery.
*
* This represents the result of computing the imagery tiles that
* are covered by a given <code>Rectangle</code> (and which part
* of that imagery is covered, in terms of texture coordinates).
*
* This class represents a plain structure, without member functions.
* Instances are created with the <code>createImageryCoverages</code>
* function.
*
* The instances are used by the <code>ModelPrimitiveImagery</code>, to
* represent the imagery tiles that are covered by the cartographic
* bounding rectangle of the primitive positions.
*
* Implementation note for ImageryCoverage:
*
* Some of the static functions in this class have been extracted from
* <code>ImageryLayer.prototype._createTileImagerySkeletons</code>
* See https://github.com/CesiumGS/cesium/blob/5eaa2280f495d8f300d9e1f0497118c97aec54c8/packages/engine/Source/Scene/ImageryLayer.js#L700
* An instance of this class roughly corresponds to the <code>TileImagery</code>
* that is created there.
*
* @private
*/
class ImageryCoverage {
/**
* Creates a new instance.
*
* @param {number} x x-coordinate of the imagery tile
* @param {number} y y-coordinate of the imagery tile
* @param {number} level level of the imagery tile
* @param {CartesianRectangle} textureCoordinateRectangle The texture coordinate
* rectangle from the imagery tile that is covered
* @param {Imagery} imagery The imagery
*/
constructor(x, y, level, textureCoordinateRectangle, imagery) {
this._x = x;
this._y = y;
this._level = level;
this._textureCoordinateRectangle = textureCoordinateRectangle;
this._imagery = imagery;
}
/**
* The x-coordinate of the imagery tile, typically correlated with longitude
*
* @type {number}
* @readonly
*/
get x() {
return this._x;
}
/**
* The y-coordinate of the imagery tile, typically correlated with latitude
*
* @type {number}
* @readonly
*/
get y() {
return this._y;
}
/**
* The level of the imagery tile
*
* @type {number}
* @readonly
*/
get level() {
return this._level;
}
/**
* The texture coordinate range that is covered from the
* imagery tile.
*
* This is a <code>CartesianRectangle</code> that contains the
* (minU, minV, maxU, maxV) coordinate range.
*
* Clients may not modify the returned instance.
*
* @type {CartesianRectangle}
* @readonly
*/
get textureCoordinateRectangle() {
return this._textureCoordinateRectangle;
}
/**
* Returns the imagery
*
* @type {Imagery}
* @readonly
*/
get imagery() {
return this._imagery;
}
/**
* Computes the <code>ImageryCoverage</code> objects that describe the imagery
* tiles and the respective texture coordinates that are covered by the given
* input rectangle in the given imagery data.
*
* The given imagery level will be clamped if necessary, to be in the valid
* range for the imagery provider of the given imagery layer.
*
* @param {Rectangle} inputRectangle The input rectangle (e.g. tile bounds)
* @param {ImageryLayer} imageryLayer The imagery layer
* @param {number} inputImageryLevel The level for which the imagery coverage
* should be computed.
* @returns {ImageryCoverage[]} The objects describing the covered imagery
* and the respective texture coordinates
*/
static createImageryCoverages(
inputRectangle,
imageryLayer,
inputImageryLevel,
) {
if (!imageryLayer.show) {
return [];
}
const imageryProvider = imageryLayer.imageryProvider;
const imageryLevel = ImageryCoverage._clampImageryLevel(
imageryProvider,
inputImageryLevel,
);
// Compute the range, in integer coordinates, of imagery
// tiles that are covered by the input rectangle
const imageryBounds = Rectangle.intersection(
imageryProvider.rectangle,
imageryLayer.rectangle,
imageryBoundsScratch,
);
const imageryTilingScheme = imageryProvider.tilingScheme;
const imageryRange = ImageryCoverage._computeImageryRange(
inputRectangle,
imageryBounds,
imageryTilingScheme,
imageryLevel,
);
// Convert the input rectangle and the imagery bounds into
// the native coordinate system of the tiling scheme
const nativeInputRectangle = nativeInputRectangleScratch;
imageryTilingScheme.rectangleToNativeRectangle(
inputRectangle,
nativeInputRectangle,
);
const nativeImageryBounds = nativeImageryBoundsScratch;
imageryTilingScheme.rectangleToNativeRectangle(
imageryBounds,
nativeImageryBounds,
);
// A function that returns an imagery rectangle, based on (x, y, level),
// clipped to the imagery bounds (or undefined if there is no intersection
// between the imagery rectangle and the bounds)
const computeClippedImageryRectangle = (x, y, level) => {
const localImageryRectangle = imageryTilingScheme.tileXYToRectangle(
x,
y,
level,
);
const localClippedImageryRectangle = Rectangle.intersection(
localImageryRectangle,
imageryBounds,
clippedRectangleScratch,
);
if (!defined(localClippedImageryRectangle)) {
return undefined;
}
const nativeClippedImageryBounds = nativeClippedImageryBoundsScratch;
imageryTilingScheme.rectangleToNativeRectangle(
localClippedImageryRectangle,
nativeClippedImageryBounds,
);
return nativeClippedImageryBounds;
};
const imageryCoverages = ImageryCoverage._computeImageryCoverages(
imageryLayer,
imageryRange,
imageryLevel,
nativeInputRectangle,
computeClippedImageryRectangle,
);
return imageryCoverages;
}
/**
* Validate the given imagery level against the constraints of the
* given imagery provider.
*
* This will clamp the given level to be in the range
* <code>[minimumLevel, maximumLevel)</code> that is
* defined by the given imagery provider (and cut off
* any fractional part that the input may have)
*
* @param {ImageryProvider} imageryProvider The imagery provider
* @param {number} imageryLevel The imagery level
* @returns {number} The validated level
*/
static _clampImageryLevel(imageryProvider, imageryLevel) {
const minimumLevel = imageryProvider.minimumLevel ?? 0;
const maximumLevel =
imageryProvider.maximumLevel ?? Number.POSITIVE_INFINITY;
const clampedImageryLevel = Math.min(
maximumLevel - 1,
Math.max(minimumLevel, imageryLevel),
);
const validImageryLevel = Math.floor(clampedImageryLevel);
return validImageryLevel;
}
/**
* Compute the rectangle describing the range of imagery that is covered
* with the given rectangle.
*
* This will compute a rectangle with integer coordinates that describe
* the X/Y coordinates of the imagery that is overlapped by the given
* input rectangle, based on the given imagery rectangle.
*
* Extracted from _createTileImagerySkeletons.
*
* @param {Rectangle} inputRectangle The input rectangle
* @param {Rectangle} imageryBounds The imagery bounds
* @param {TilingScheme} imageryTilingScheme The tiling scheme
* @param {number} imageryLevel The imagery level
* @returns {CartesianRectangle} The rectangle
*/
static _computeImageryRange(
inputRectangle,
imageryBounds,
imageryTilingScheme,
imageryLevel,
) {
const overlappedRectangle = ImageryCoverage._computeOverlappedRectangle(
inputRectangle,
imageryBounds,
);
const northwestTileCoordinates = imageryTilingScheme.positionToTileXY(
Rectangle.northwest(overlappedRectangle),
imageryLevel,
);
const southeastTileCoordinates = imageryTilingScheme.positionToTileXY(
Rectangle.southeast(overlappedRectangle),
imageryLevel,
);
const result = new CartesianRectangle();
result.minX = northwestTileCoordinates.x;
result.minY = northwestTileCoordinates.y;
result.maxX = southeastTileCoordinates.x;
result.maxY = southeastTileCoordinates.y;
// As extracted from _createTileImagerySkeletons:
// If the southeast corner of the rectangle lies very close to the north or west side
// of the southeast tile, we don't actually need the southernmost or easternmost
// tiles.
// Similarly, if the northwest corner of the rectangle lies very close to the south or east side
// of the northwest tile, we don't actually need the northernmost or westernmost tiles.
// We define "very close" as being within 1/512 of the width of the tile.
const veryCloseX = inputRectangle.width / 512.0;
const veryCloseY = inputRectangle.height / 512.0;
const northwestTileRectangle = imageryTilingScheme.tileXYToRectangle(
result.minX,
result.minY,
imageryLevel,
);
const deltaNorth = Math.abs(
northwestTileRectangle.south - inputRectangle.north,
);
if (deltaNorth < veryCloseY && result.minY < result.maxY) {
++result.minY;
}
const deltaWest = Math.abs(
northwestTileRectangle.east - inputRectangle.west,
);
if (deltaWest < veryCloseX && result.minX < result.maxX) {
++result.minX;
}
const southeastTileRectangle = imageryTilingScheme.tileXYToRectangle(
result.maxX,
result.maxY,
imageryLevel,
);
const deltaSouth = Math.abs(
southeastTileRectangle.north - inputRectangle.south,
);
if (deltaSouth < veryCloseY && result.maxY > result.minY) {
--result.maxY;
}
const deltaEast = Math.abs(
southeastTileRectangle.west - inputRectangle.east,
);
if (deltaEast < veryCloseX && result.maxX > result.minX) {
--result.maxX;
}
return result;
}
/**
* Clamp the given input rectangle to the given clamp rectangle.
*
* If the input rectangle is completely above/below or left/right
* of the clamp rectangle, then the north/south or east/east
* if the clamp rectangle will be used in the result.
*
* @param {Rectangle} input The input rectangle
* @param {Rectangle} clamp The clamping rectangle
* @param {Rectangle} [result] The result
* @returns {Rectangle} The result
*/
static _clampRectangle(input, clamp, result) {
if (!defined(result)) {
result = new Rectangle();
}
if (input.south >= clamp.north) {
result.north = result.south = clamp.north;
} else if (input.north <= clamp.south) {
result.north = result.south = clamp.south;
} else {
result.south = Math.max(input.south, clamp.south);
result.north = Math.min(input.north, clamp.north);
}
if (input.west >= clamp.east) {
result.west = result.east = clamp.east;
} else if (input.east <= clamp.west) {
result.west = result.east = clamp.west;
} else {
result.west = Math.max(input.west, clamp.west);
result.east = Math.min(input.east, clamp.east);
}
return result;
}
/**
* Compute overlap between the given input rectangle, and the given
* bounds that have been obtained from the imagery provider.
*
* @param {Rectangle} inputRectangle The input
* @param {Rectangle} imageryBounds The imagery bounds
* @returns {Rectangle} The rectangle
*/
static _computeOverlappedRectangle(inputRectangle, imageryBounds) {
const overlappedRectangle = Rectangle.intersection(
inputRectangle,
imageryBounds,
overlappedRectangleScratch,
);
if (defined(overlappedRectangle)) {
return overlappedRectangle;
}
return ImageryCoverage._clampRectangle(
inputRectangle,
imageryBounds,
overlappedRectangleScratch,
);
}
/**
* Computes the <code>ImageryCoverage</code> objects that describe the imagery and
* the texture coordinates that are contained in the given range of
* imagery tile coordinates, referring to the given input rectangle.
*
* @param {ImageryLayer} imageryLayer The imagery layer
* @param {CartesianRectangle} imageryRange The range of imagery tile coordinates
* @param {number} imageryLevel The imagery level
* @param {Rectangle} nativeInputRectangle The input rectangle, in coordinates
* that are native for the tiling scheme
* @param {Function} computeClippedImageryRectangle A function that returns
* an imagery rectangle, based on (x, y, level), clipped to the imagery bounds
* (or undefined if there is no intersection between the imagery rectangle
* and the bounds)
* @returns {ImageryCoverage[]} The objects describing the covered imagery
* and the respective texture coordinates
*/
static _computeImageryCoverages(
imageryLayer,
imageryRange,
imageryLevel,
nativeInputRectangle,
computeClippedImageryRectangle,
) {
const imageryCoverages = [];
for (let i = imageryRange.minX; i <= imageryRange.maxX; i++) {
const clippedImageryRectangleU = computeClippedImageryRectangle(
i,
imageryRange.maxY,
imageryLevel,
);
if (!defined(clippedImageryRectangleU)) {
continue;
}
for (let j = imageryRange.minY; j <= imageryRange.maxY; j++) {
const clippedImageryRectangleV = computeClippedImageryRectangle(
i,
j,
imageryLevel,
);
if (!defined(clippedImageryRectangleV)) {
continue;
}
const textureCoordinateRectangle =
ImageryCoverage._localizeToCartesianRectangle(
clippedImageryRectangleV,
nativeInputRectangle,
undefined,
);
// Note: The getImageryFromCache function will create the whole "chain"
// of ancestor imageries, up to the root, and increases the reference
// counter for each of them, even though it is not called
// getImageryFromCacheAndCreateAllAncestorsAndAddReferences.
// There is currently no way to have a single imagery, because
// somewhere in TileImagery, the parent is assumed to be present.
const imagery = imageryLayer.getImageryFromCache(i, j, imageryLevel);
const imageryCoverage = new ImageryCoverage(
i,
j,
imageryLevel,
textureCoordinateRectangle,
imagery,
);
imageryCoverages.push(imageryCoverage);
}
}
return imageryCoverages;
}
/**
* Compute the coordinates of the first rectangle relative to the
* second rectangle.
*
* The result will describe the bounds of the first rectangle
* in coordinates that are relative to the (south,west) and
* (width, height) of the second rectangle. This is suitable
* for describing the texture coordinates of the first
* rectangle within the second one.
*
* The result will be stored in the given result parameter, or
* in a new rectangle if the result was undefined.
*
* @param {Rectangle} rectangleA The first rectangle
* @param {Rectangle} rectangleB The second rectangle
* @param {CartesianRectangle} [result] The result
* @returns {CartesianRectangle} The result
*/
static _localizeToCartesianRectangle(rectangleA, rectangleB, result) {
if (!defined(result)) {
result = new CartesianRectangle();
}
const invX = 1.0 / rectangleB.width;
const invY = 1.0 / rectangleB.height;
result.minX = (rectangleA.west - rectangleB.west) * invX;
result.minY = (rectangleA.south - rectangleB.south) * invY;
result.maxX = (rectangleA.east - rectangleB.west) * invX;
result.maxY = (rectangleA.north - rectangleB.south) * invY;
return result;
}
}
export default ImageryCoverage;
|