-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(core) Add getWorldBounds() method #9920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ibgreen
wants to merge
1
commit into
master
Choose a base branch
from
codex/add-getworldbounds-method-to-layer.ts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,13 @@ import memoize from '../utils/memoize'; | |
| import {mergeShaders} from '../utils/shader'; | ||
| import {projectPosition, getWorldPosition} from '../shaderlib/project/project-functions'; | ||
| import typedArrayManager from '../utils/typed-array-manager'; | ||
| import {deepEqual} from '../utils/deep-equal'; | ||
|
|
||
| import Component from '../lifecycle/component'; | ||
| import LayerState, {ChangeFlags} from './layer-state'; | ||
|
|
||
| import {worldToPixels} from '@math.gl/web-mercator'; | ||
| import {OrientedBoundingBox} from '@math.gl/culling'; | ||
|
|
||
| import {load} from '@loaders.gl/core'; | ||
|
|
||
|
|
@@ -39,6 +41,7 @@ import type {LayerContext} from './layer-manager'; | |
| import type {BinaryAttribute} from './attribute/attribute'; | ||
| import {RenderPass} from '@luma.gl/core'; | ||
| import {PickingProps} from '@luma.gl/shadertools'; | ||
| import {transformBoundsToWorld, WorldBoundsTransformOptions} from './world-bounds'; | ||
|
|
||
| const TRACE_CHANGE_FLAG = 'layer.changeFlag'; | ||
| const TRACE_INITIALIZE = 'layer.initialize'; | ||
|
|
@@ -201,6 +204,13 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component< | |
| state!: SharedLayerState; // Will be set to the shared layer state object during layer matching | ||
|
|
||
| parent: Layer | null = null; | ||
| private _cachedLocalBounds: [number[], number[]] | null = null; | ||
| private _cachedWorldBounds: OrientedBoundingBox | null = null; | ||
| private _cachedWorldBoundsCacheKey: unknown; | ||
| private _cachedWorldBoundsProps: Omit<WorldBoundsTransformOptions, 'bounds' | 'viewport'> | null = | ||
| null; | ||
| private _worldBoundsViewportId?: string; | ||
| private _worldBoundsDirty: boolean = true; | ||
|
|
||
| get root(): Layer { | ||
| // eslint-disable-next-line | ||
|
|
@@ -446,6 +456,67 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component< | |
| return this.getAttributeManager()?.getBounds(['positions', 'instancePositions']); | ||
| } | ||
|
|
||
| // Default implementation | ||
| getWorldBounds(): OrientedBoundingBox | null { | ||
| const options = this.getWorldBoundsOptions(); | ||
| if (!options) { | ||
| return null; | ||
| } | ||
|
|
||
| const {bounds, viewport, worldBoundsCacheKey, ...transformOptions} = options; | ||
| const cacheKey = worldBoundsCacheKey ?? null; | ||
| const viewportId = viewport?.id; | ||
| const useCache = | ||
| !this._worldBoundsDirty && | ||
| this._cachedWorldBounds && | ||
| deepEqual(bounds, this._cachedLocalBounds, 3) && | ||
| deepEqual(transformOptions, this._cachedWorldBoundsProps, 3) && | ||
| deepEqual(cacheKey, this._cachedWorldBoundsCacheKey, 3) && | ||
| viewportId === this._worldBoundsViewportId; | ||
|
|
||
| if (useCache) { | ||
| return this._cachedWorldBounds; | ||
| } | ||
|
|
||
| const worldBounds = transformBoundsToWorld({ | ||
| bounds, | ||
| viewport, | ||
| ...transformOptions | ||
| }); | ||
|
|
||
| this._cachedWorldBounds = worldBounds; | ||
| this._cachedLocalBounds = bounds; | ||
| this._cachedWorldBoundsProps = transformOptions; | ||
| this._cachedWorldBoundsCacheKey = cacheKey; | ||
| this._worldBoundsViewportId = viewportId; | ||
| this._worldBoundsDirty = false; | ||
|
|
||
| return worldBounds; | ||
| } | ||
|
|
||
| /** | ||
| * Override to customize how world bounds are generated and cached. | ||
| * Add `worldBoundsCacheKey` to the returned object if additional props affect bounds, | ||
| * e.g. radius or scale. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| protected getWorldBoundsOptions(): WorldBoundsTransformOptions | null { | ||
| const bounds = this.getBounds(); | ||
| const viewport = this.internalState?.viewport || this.context.viewport; | ||
| if (!bounds || !viewport) { | ||
| return null; | ||
| } | ||
|
|
||
| const {coordinateOrigin, coordinateSystem, modelMatrix} = this.props; | ||
| return { | ||
| bounds, | ||
| viewport, | ||
| coordinateOrigin, | ||
| coordinateSystem, | ||
| modelMatrix | ||
| }; | ||
| } | ||
|
|
||
| // / LIFECYCLE METHODS - overridden by the layer subclasses | ||
|
|
||
| /** Called once to set up the initial state. Layers can create WebGL resources here. */ | ||
|
|
@@ -472,6 +543,9 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component< | |
| updateState(params: UpdateParameters<Layer<PropsT>>): void { | ||
| const attributeManager = this.getAttributeManager(); | ||
| const {dataChanged} = params.changeFlags; | ||
| if (params.changeFlags.propsOrDataChanged || params.changeFlags.viewportChanged) { | ||
| this._worldBoundsDirty = true; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is technically incorrect. There are multiple existing caching mechanism (e.g. in |
||
| } | ||
| if (dataChanged && attributeManager) { | ||
| if (Array.isArray(dataChanged)) { | ||
| // is partial update | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // deck.gl | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) vis.gl contributors | ||
|
|
||
| import {projectPosition} from '../shaderlib/project/project-functions'; | ||
| import {makeOrientedBoundingBoxFromPoints, OrientedBoundingBox} from '@math.gl/culling'; | ||
|
|
||
| import type Viewport from '../viewports/viewport'; | ||
| import type {CoordinateSystem} from './constants'; | ||
| import type {NumericArray} from '../types/types'; | ||
|
|
||
| export type WorldBoundsTransformOptions = { | ||
| bounds: [number[], number[]]; | ||
| viewport: Viewport; | ||
| coordinateSystem: CoordinateSystem; | ||
| coordinateOrigin: [number, number, number]; | ||
| modelMatrix?: NumericArray | null; | ||
| /** Additional props that affect bounds transformation and caching */ | ||
| worldBoundsCacheKey?: unknown; | ||
| }; | ||
|
|
||
| export function transformBoundsToWorld(options: WorldBoundsTransformOptions): OrientedBoundingBox { | ||
| const {bounds, viewport, coordinateOrigin, coordinateSystem, modelMatrix} = options; | ||
| const corners = getBoundingBoxCorners(bounds); | ||
|
|
||
| const worldPositions = corners.map(position => | ||
| projectPosition(position, { | ||
| viewport, | ||
| coordinateSystem, | ||
| coordinateOrigin, | ||
| modelMatrix | ||
| }) | ||
| ); | ||
|
|
||
| return makeOrientedBoundingBoxFromPoints(worldPositions); | ||
| } | ||
|
|
||
| function getBoundingBoxCorners(bounds: [number[], number[]]): number[][] { | ||
| const [min, max] = bounds; | ||
| const dimension = Math.max(min.length, max.length, 3); | ||
| const corners: number[][] = []; | ||
|
|
||
| for (let i = 0; i < 8; i++) { | ||
| const corner: number[] = []; | ||
| for (let axis = 0; axis < 3; axis++) { | ||
| const minValue = axis < dimension ? (min[axis] ?? 0) : 0; | ||
| const maxValue = axis < dimension ? (max[axis] ?? minValue) : minValue; | ||
| corner[axis] = i & (1 << axis) ? maxValue : minValue; | ||
| } | ||
| corners.push(corner); | ||
| } | ||
|
|
||
| return corners; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // deck.gl | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) vis.gl contributors | ||
|
|
||
| import test from 'tape-catch'; | ||
| import {Layer, COORDINATE_SYSTEM, transformBoundsToWorld, Viewport} from '@deck.gl/core'; | ||
| import {Matrix4} from '@math.gl/core'; | ||
| import LayerState from '../../../../modules/core/src/lib/layer-state'; | ||
|
|
||
| class WorldBoundsLayer extends Layer<{customKey?: number}> { | ||
| initializeState() {} | ||
|
|
||
| override getBounds() { | ||
| return [ | ||
| [-1, 0, 0], | ||
| [1, 0, 0] | ||
| ]; | ||
| } | ||
|
|
||
| protected override getWorldBoundsOptions() { | ||
| const options = super.getWorldBoundsOptions(); | ||
| if (!options) { | ||
| return null; | ||
| } | ||
| return {...options, worldBoundsCacheKey: this.props.customKey}; | ||
| } | ||
| } | ||
|
|
||
| WorldBoundsLayer.defaultProps = { | ||
| customKey: 0 | ||
| }; | ||
|
|
||
| const VIEWPORT = new Viewport({ | ||
| id: 'identity', | ||
| width: 1, | ||
| height: 1, | ||
| position: [0, 0, 0], | ||
| viewMatrix: new Matrix4().identity(), | ||
| projectionMatrix: new Matrix4().identity() | ||
| }); | ||
|
|
||
| test('transformBoundsToWorld#modelMatrix applied to corners', t => { | ||
| const modelMatrix = new Matrix4().rotateZ(Math.PI / 2); | ||
| const bounds = [ | ||
| [-1, 0, 0], | ||
| [1, 0, 0] | ||
| ] as [number[], number[]]; | ||
|
|
||
| const obb = transformBoundsToWorld({ | ||
| bounds, | ||
| coordinateOrigin: [0, 0, 0], | ||
| coordinateSystem: COORDINATE_SYSTEM.CARTESIAN, | ||
| modelMatrix, | ||
| viewport: VIEWPORT | ||
| }); | ||
|
|
||
| const halfAxes = obb.halfAxes; | ||
| t.ok(Math.abs(halfAxes[0]) < 1e-6, 'x component is rotated to zero'); | ||
| t.ok(Math.abs(halfAxes[1] - 1) < 1e-6, 'y component reflects rotation'); | ||
| t.deepEqual([...obb.center], [0, 0, 0], 'center is preserved'); | ||
| t.end(); | ||
| }); | ||
|
|
||
| test('Layer#getWorldBounds#cache invalidation and key', t => { | ||
| const layer = new WorldBoundsLayer({ | ||
| id: 'world-bounds-layer', | ||
| data: [], | ||
| coordinateSystem: COORDINATE_SYSTEM.CARTESIAN, | ||
| customKey: 0 | ||
| }); | ||
|
|
||
| layer.context = {viewport: VIEWPORT} as any; | ||
| layer.internalState = new LayerState({attributeManager: null, layer}); | ||
| layer.internalState.viewport = VIEWPORT; | ||
|
|
||
| const first = layer.getWorldBounds(); | ||
| const second = layer.getWorldBounds(); | ||
| t.equal(first, second, 'bounds are cached when inputs are unchanged'); | ||
|
|
||
| layer.updateState({ | ||
| props: layer.props, | ||
| oldProps: layer.props, | ||
| context: layer.context, | ||
| changeFlags: { | ||
| dataChanged: false, | ||
| propsChanged: false, | ||
| updateTriggersChanged: false, | ||
| extensionsChanged: false, | ||
| viewportChanged: true, | ||
| stateChanged: false, | ||
| propsOrDataChanged: false, | ||
| somethingChanged: true | ||
| } | ||
| }); | ||
|
|
||
| const afterViewportChange = layer.getWorldBounds(); | ||
| t.notEqual(afterViewportChange, first, 'cache invalidates when viewport changes'); | ||
|
|
||
| const cachedAgain = layer.getWorldBounds(); | ||
| t.equal(afterViewportChange, cachedAgain, 'bounds cache is reused after recompute'); | ||
|
|
||
| layer.props = {...layer.props, customKey: 1}; | ||
| const afterKeyChange = layer.getWorldBounds(); | ||
| t.notEqual(afterKeyChange, cachedAgain, 'cache key forces recomputation'); | ||
| t.end(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core has a memoize utility function.