Skip to content

Commit e8f0823

Browse files
committed
Improves method for getting child quad tile at position
1 parent 6febd2b commit e8f0823

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

packages/engine/Source/Scene/QuadtreePrimitive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ function selectTilesForRendering(primitive, frameState) {
564564
}
565565
});
566566

567-
levelZeroTiles.forEach((tile) => tile._updateCustomData());
567+
levelZeroTiles.forEach((tile) => tile.updateCustomData());
568568
customDataAdded.length = 0;
569569
customDataRemoved.length = 0;
570570

@@ -721,7 +721,7 @@ function visitTile(
721721
++debug.tilesVisited;
722722

723723
primitive._tileReplacementQueue.markTileRendered(tile);
724-
tile._updateCustomData();
724+
tile.updateCustomData();
725725

726726
if (tile.level > debug.maxDepthVisited) {
727727
debug.maxDepthVisited = tile.level;

packages/engine/Source/Scene/QuadtreeTile.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ QuadtreeTile.prototype.clearPositionCache = function () {
313313
}
314314
};
315315

316-
QuadtreeTile.prototype._updateCustomData = function () {
316+
QuadtreeTile.prototype.updateCustomData = function () {
317317
const added = this._addedCustomData;
318318
const removed = this._removedCustomData;
319319
if (added.length === 0 && removed.length === 0) {
@@ -342,7 +342,7 @@ QuadtreeTile.prototype._updateCustomData = function () {
342342
this._removedCustomData.length = 0;
343343
};
344344

345-
const centerScratch = new Cartographic();
345+
const splitPointScratch = new Cartographic();
346346

347347
/**
348348
* Determines which child tile that contains the specified position. Assumes the position is within
@@ -353,9 +353,16 @@ const centerScratch = new Cartographic();
353353
* @returns {QuadtreeTile} The child tile that contains the position.
354354
*/
355355
function childTileAtPosition(tile, positionCartographic) {
356-
const center = Rectangle.center(tile.rectangle, centerScratch);
357-
const x = positionCartographic.longitude >= center.longitude ? 1 : 0;
358-
const y = positionCartographic.latitude < center.latitude ? 1 : 0;
356+
// Can't assume that a given tiling scheme divides a parent into four tiles at its rectangle's center.
357+
// But we can safely take any child tile's rectangle and take its center-facing corner as the parent's split point.
358+
const nwChildRectangle = tile.northwestChild.rectangle;
359+
const tileSplitPoint = Rectangle.southeast(
360+
nwChildRectangle,
361+
splitPointScratch,
362+
);
363+
364+
const x = positionCartographic.longitude >= tileSplitPoint.longitude ? 1 : 0;
365+
const y = positionCartographic.latitude < tileSplitPoint.latitude ? 1 : 0;
359366

360367
switch (y * 2 + x) {
361368
case 0:

packages/engine/Specs/Scene/QuadtreeTileSpec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,53 @@ describe("Scene/QuadtreeTile", function () {
458458
expect(cachedData).toEqual(dummyPosition);
459459
});
460460
});
461+
462+
describe("updateCustomData", function () {
463+
function addAndRemoveCustomData(tilingScheme) {
464+
const tile = new QuadtreeTile({
465+
level: 0,
466+
x: 0,
467+
y: 0,
468+
tilingScheme: tilingScheme,
469+
});
470+
471+
const child = tile.northwestChild;
472+
const centerCartographic = Rectangle.center(child.rectangle);
473+
474+
const data = {
475+
positionCartographic: centerCartographic,
476+
};
477+
478+
tile._addedCustomData.push(data);
479+
tile.updateCustomData();
480+
481+
expect(tile.customData.has(data)).toBe(true);
482+
expect(tile._addedCustomData.length).toBe(0);
483+
expect(child._addedCustomData.length).toBe(1);
484+
expect(child._addedCustomData[0]).toBe(data);
485+
486+
child.updateCustomData();
487+
expect(child.customData.has(data)).toBe(true);
488+
489+
// Now remove the data from the parent tile.
490+
tile._removedCustomData.push(data);
491+
tile.updateCustomData();
492+
493+
expect(tile.customData.has(data)).toBe(false);
494+
expect(tile._removedCustomData.length).toBe(0);
495+
expect(child._removedCustomData.length).toBe(1);
496+
expect(child._removedCustomData[0]).toBe(data);
497+
498+
child.updateCustomData();
499+
expect(child.customData.has(data)).toBe(false);
500+
}
501+
502+
it("can add and remove custom data when tiling scheme is GeographicTilingScheme", function () {
503+
addAndRemoveCustomData(new GeographicTilingScheme());
504+
});
505+
506+
it("can add and remove custom data when tiling scheme is WebMercatorTilingScheme", function () {
507+
addAndRemoveCustomData(new WebMercatorTilingScheme());
508+
});
509+
});
461510
});

0 commit comments

Comments
 (0)