Skip to content

Commit

Permalink
Merge pull request #16428 from MoonE/remove-loop-over-single-item-array
Browse files Browse the repository at this point in the history
Add some types, some code cleanup
  • Loading branch information
MoonE authored Dec 4, 2024
2 parents 7703381 + 83219d4 commit 7c9c2da
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 109 deletions.
20 changes: 7 additions & 13 deletions src/ol/render/canvas/ExecutorGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import Executor from './Executor.js';
import {ascending} from '../../array.js';
import {ascending, descending} from '../../array.js';
import {buffer, createEmpty, extendCoordinate} from '../../extent.js';
import {
compose as composeTransform,
Expand Down Expand Up @@ -235,9 +235,7 @@ class ExecutorGroup {
context.clearRect(0, 0, contextSize, contextSize);
}

/**
* @type {import("../../extent.js").Extent}
*/
/** @type {import("../../extent.js").Extent|undefined} */
let hitExtent;
if (this.renderBuffer_ !== undefined) {
hitExtent = createEmpty();
Expand All @@ -251,6 +249,7 @@ class ExecutorGroup {

const indexes = getPixelIndexArray(hitTolerance);

/** @type {import("../canvas.js").BuilderType} */
let builderType;

/**
Expand Down Expand Up @@ -362,20 +361,15 @@ class ExecutorGroup {
builderTypes,
declutterTree,
) {
/** @type {Array<number>} */
const zs = Object.keys(this.executorsByZIndex_).map(Number);
zs.sort(ascending);
zs.sort(declutterTree ? descending : ascending);

builderTypes = builderTypes ? builderTypes : ALL;
const maxBuilderTypes = ALL.length;
let i, ii, j, jj, replays;
if (declutterTree) {
zs.reverse();
}
for (i = 0, ii = zs.length; i < ii; ++i) {
for (let i = 0, ii = zs.length; i < ii; ++i) {
const zIndexKey = zs[i].toString();
replays = this.executorsByZIndex_[zIndexKey];
for (j = 0, jj = builderTypes.length; j < jj; ++j) {
const replays = this.executorsByZIndex_[zIndexKey];
for (let j = 0, jj = builderTypes.length; j < jj; ++j) {
const builderType = builderTypes[j];
const replay = replays[builderType];
if (replay !== undefined) {
Expand Down
15 changes: 9 additions & 6 deletions src/ol/render/webgl/MixedGeometryBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ class MixedGeometryBatch {
* @private
*/
clearFeatureEntryInPointBatch_(feature) {
const entry = this.pointBatch.entries[getUid(feature)];
const featureUid = getUid(feature);
const entry = this.pointBatch.entries[featureUid];
if (!entry) {
return;
}
this.pointBatch.geometriesCount -= entry.flatCoordss.length;
delete this.pointBatch.entries[getUid(feature)];
delete this.pointBatch.entries[featureUid];
return entry;
}

Expand All @@ -173,13 +174,14 @@ class MixedGeometryBatch {
* @private
*/
clearFeatureEntryInLineStringBatch_(feature) {
const entry = this.lineStringBatch.entries[getUid(feature)];
const featureUid = getUid(feature);
const entry = this.lineStringBatch.entries[featureUid];
if (!entry) {
return;
}
this.lineStringBatch.verticesCount -= entry.verticesCount;
this.lineStringBatch.geometriesCount -= entry.flatCoordss.length;
delete this.lineStringBatch.entries[getUid(feature)];
delete this.lineStringBatch.entries[featureUid];
return entry;
}

Expand All @@ -189,14 +191,15 @@ class MixedGeometryBatch {
* @private
*/
clearFeatureEntryInPolygonBatch_(feature) {
const entry = this.polygonBatch.entries[getUid(feature)];
const featureUid = getUid(feature);
const entry = this.polygonBatch.entries[featureUid];
if (!entry) {
return;
}
this.polygonBatch.verticesCount -= entry.verticesCount;
this.polygonBatch.ringsCount -= entry.ringsCount;
this.polygonBatch.geometriesCount -= entry.flatCoordss.length;
delete this.polygonBatch.entries[getUid(feature)];
delete this.polygonBatch.entries[featureUid];
return entry;
}

Expand Down
26 changes: 10 additions & 16 deletions src/ol/renderer/canvas/VectorLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,23 +522,17 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
return undefined;
};

let result;
const executorGroups = [this.replayGroup_];
const declutter = this.getLayer().getDeclutter();
executorGroups.some((executorGroup) => {
return (result = executorGroup.forEachFeatureAtCoordinate(
coordinate,
resolution,
rotation,
hitTolerance,
featureCallback,
declutter && frameState.declutter[declutter]
? frameState.declutter[declutter].all().map((item) => item.value)
: null,
));
});

return result;
return this.replayGroup_.forEachFeatureAtCoordinate(
coordinate,
resolution,
rotation,
hitTolerance,
featureCallback,
declutter
? frameState.declutter[declutter].all().map((item) => item.value)
: null,
);
}

/**
Expand Down
150 changes: 79 additions & 71 deletions src/ol/renderer/canvas/VectorTileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,37 +377,33 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
/** @type {Array<import("../../VectorRenderTile.js").default>} */ (
this.renderedTiles
);

const layerUid = getUid(layer);
const declutter = layer.getDeclutter();
const declutteredFeatures = declutter
? frameState.declutter[declutter].all().map((item) => item.value)
: null;
let found;
for (let i = 0, ii = renderedTiles.length; !found && i < ii; ++i) {
foundFeature: for (let i = 0, ii = renderedTiles.length; i < ii; ++i) {
const tile = renderedTiles[i];
const tileExtent = tileGrid.getTileCoordExtent(tile.wrappedTileCoord);
if (!intersects(tileExtent, hitExtent)) {
continue;
}

const layerUid = getUid(layer);
const executorGroups = [tile.executorGroups[layerUid]];
const declutter = layer.getDeclutter();
executorGroups.some((executorGroups) => {
const declutteredFeatures = declutter
? frameState.declutter[declutter].all().map((item) => item.value)
: null;
for (let t = 0, tt = executorGroups.length; t < tt; ++t) {
const executorGroup = executorGroups[t];
found = executorGroup.forEachFeatureAtCoordinate(
coordinate,
resolution,
rotation,
hitTolerance,
featureCallback,
declutteredFeatures,
);
if (found) {
return true;
}
const executorGroups = tile.executorGroups[layerUid];
for (let t = 0, tt = executorGroups.length; t < tt; ++t) {
found = executorGroups[t].forEachFeatureAtCoordinate(
coordinate,
resolution,
rotation,
hitTolerance,
featureCallback,
declutteredFeatures,
);
if (found) {
break foundFeature;
}
});
}
}
return found;
}
Expand All @@ -424,7 +420,6 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
}
return new Promise((resolve, reject) => {
const layer = this.getLayer();
const layerUid = getUid(layer);
const source = layer.getSource();
const projection = this.renderedProjection;
const projectionExtent = projection.getExtent();
Expand All @@ -434,51 +429,45 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
this.renderedPixelToCoordinateTransform_,
pixel.slice(),
);
const tileCoord = tileGrid.getTileCoordForCoordAndResolution(
coordinate,
resolution,
);
/** @type {import("../../VectorRenderTile.js").default|undefined} */
let tile;
for (let i = 0, ii = this.renderedTiles.length; i < ii; ++i) {
if (
tileCoord.toString() === this.renderedTiles[i].tileCoord.toString()
) {
tile = /** @type {import("../../VectorRenderTile.js").default} */ (
this.renderedTiles[i]
);
if (tile.getState() === TileState.LOADED) {
const extent = tileGrid.getTileCoordExtent(tile.tileCoord);
if (
source.getWrapX() &&
projection.canWrapX() &&
!containsExtent(projectionExtent, extent)
) {
wrapX(coordinate, projection);
}
break;
}
tile = undefined;
}
}
const tileCoordString = tileGrid
.getTileCoordForCoordAndResolution(coordinate, resolution)
.toString();
const tile =
/** @type {Array<import("../../VectorRenderTile.js").default>} */ (
this.renderedTiles
).find(
(tile) =>
tile.tileCoord.toString() === tileCoordString &&
tile.getState() === TileState.LOADED,
);
if (!tile || tile.loadingSourceTiles > 0) {
resolve([]);
return;
}
if (
source.getWrapX() &&
projection.canWrapX() &&
!containsExtent(
projectionExtent,
tileGrid.getTileCoordExtent(tile.tileCoord),
)
) {
wrapX(coordinate, projection);
}
const layerUid = getUid(layer);
const extent = tileGrid.getTileCoordExtent(tile.wrappedTileCoord);
const corner = getTopLeft(extent);
const tilePixel = [
(coordinate[0] - corner[0]) / resolution,
(corner[1] - coordinate[1]) / resolution,
];
/** @type {Array<import("../../Feature.js").FeatureLike>} */
const features = tile.getSourceTiles().reduce(function (
accumulator,
sourceTile,
) {
return accumulator.concat(sourceTile.getFeatures());
}, []);
/** @type {ImageData|undefined} */
const features = tile
.getSourceTiles()
.reduce(
(accumulator, sourceTile) =>
accumulator.concat(sourceTile.getFeatures()),
/** @type {Array<import("../../Feature.js").FeatureLike>} */ ([]),
);
let hitDetectionImageData = tile.hitDetectionImageData[layerUid];
if (!hitDetectionImageData) {
const tileSize = toSize(
Expand Down Expand Up @@ -518,6 +507,7 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
* @return {Array<import('../../Feature.js').FeatureLike>} Features.
*/
getFeaturesInExtent(extent) {
/** @type {Array<import('../../Feature.js').FeatureLike>} */
const features = [];
const tileCache = this.getTileCache();
if (tileCache.getCount() === 0) {
Expand All @@ -528,6 +518,7 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
this.frameState.viewState.projection,
);
const z = tileGrid.getZForResolution(this.renderedResolution);
/** @type {Object<string, true>} */
const visitedSourceTiles = {};
tileCache.forEach((tile) => {
if (tile.tileCoord[0] !== z || tile.getState() !== TileState.LOADED) {
Expand Down Expand Up @@ -592,24 +583,32 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
const hifi = !(
viewHints[ViewHint.ANIMATING] || viewHints[ViewHint.INTERACTING]
);
const scaledCanvasSize = [
this.context.canvas.width,
this.context.canvas.height,
];
const declutter = this.getLayer().getDeclutter();
const declutterTree = declutter
? frameState.declutter[declutter]
: undefined;
const layerUid = getUid(this.getLayer());
const tiles =
/** @type {Array<import("../../VectorRenderTile.js").default>} */ (
this.renderedTiles
);
for (let i = 0, ii = tiles.length; i < ii; ++i) {
const tile = tiles[i];
const executorGroups = tile.executorGroups[getUid(this.getLayer())];
const declutter = this.getLayer().getDeclutter();
const executorGroups = tile.executorGroups[layerUid];
if (executorGroups) {
for (let j = executorGroups.length - 1; j >= 0; --j) {
executorGroups[j].execute(
this.context,
[this.context.canvas.width, this.context.canvas.height],
scaledCanvasSize,
this.getTileRenderTransform(tile, frameState),
frameState.viewState.rotation,
hifi,
DECLUTTER,
declutter ? frameState.declutter[declutter] : undefined,
declutterTree,
);
}
}
Expand All @@ -626,19 +625,21 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
/** @type {Array<import("../../VectorRenderTile.js").default>} */ (
this.renderedTiles
);
const layerUid = getUid(this.getLayer());
const executorGroups = tiles.reduce((acc, tile, index) => {
tile.executorGroups[getUid(this.getLayer())].forEach((executorGroup) =>
tile.executorGroups[layerUid].forEach((executorGroup) =>
acc.push({
executorGroup,
index,
}),
);
return acc;
}, []);
}, /** @type {Array<{executorGroup: CanvasExecutorGroup, index: number}>} */ ([]));

const executorGroupZIndexContexts = executorGroups.map(({executorGroup}) =>
executorGroup.getDeferredZIndexContexts(),
);
/** @type {Object<number, true>} */
const usedZIndices = {};
for (let i = 0, ii = executorGroups.length; i < ii; ++i) {
const executorGroupZindexContext =
Expand Down Expand Up @@ -674,6 +675,11 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
});
}

/**
* @param {import("../../VectorRenderTile.js").default} tile The tile
* @param {import('../../Map.js').FrameState} frameState Current frame state
* @return {import('../../transform.js').Transform} Transform to use to render this tile
*/
getTileRenderTransform(tile, frameState) {
const pixelRatio = frameState.pixelRatio;
const viewState = frameState.viewState;
Expand Down Expand Up @@ -745,18 +751,20 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
tileSource.zDirection,
);

const tiles = this.renderedTiles;
const tiles =
/** @type {Array<import("../../VectorRenderTile.js").default>} */ (
this.renderedTiles
);
const clips = [];
const clipZs = [];
const tileClipContexts = [];
const layerUid = getUid(layer);
let ready = true;
for (let i = tiles.length - 1; i >= 0; --i) {
const tile = /** @type {import("../../VectorRenderTile.js").default} */ (
tiles[i]
);
const tile = tiles[i];
ready = ready && !tile.getReplayState(layer).dirty;
const executorGroups = tile.executorGroups[getUid(layer)].filter(
(group) => group.hasExecutors(replayTypes),
const executorGroups = tile.executorGroups[layerUid].filter((group) =>
group.hasExecutors(replayTypes),
);
if (executorGroups.length === 0) {
continue;
Expand Down
Loading

0 comments on commit 7c9c2da

Please sign in to comment.