Skip to content

Commit

Permalink
fix: camera settings not respected when trigger home (#2265)
Browse files Browse the repository at this point in the history
Fix behavior when triggering home.
Add export of Point2D and Point3D at top level
  • Loading branch information
w1nklr authored Sep 25, 2024
1 parent e0679d2 commit 6045633
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
10 changes: 5 additions & 5 deletions typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 32 additions & 28 deletions typescript/packages/subsurface-viewer/src/components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ function createLayer(
///////////////////////////////////////////////////////////////////////////////////////////
// View Controller
// Implements the algorithms to compute the views and the view state
type ScaledCamera = ViewStateType & {
scale: number;
};
type ViewControllerState = {
// Explicit state
triggerHome: number | undefined;
Expand All @@ -983,7 +986,7 @@ type ViewControllerState = {
};
type ViewControllerDerivedState = {
// Derived state
cameraClone: ViewStateType | undefined; // clone of the camera
scaledCameraClone: ScaledCamera | undefined; // clone of the camera
eventTarget: Point3D | undefined; // target set by events
readyForInteraction: boolean;
viewStateChanged: boolean;
Expand All @@ -994,7 +997,7 @@ class ViewController {
private rerender_: React.DispatchWithoutAction;

private derivedState_: ViewControllerDerivedState = {
cameraClone: undefined,
scaledCameraClone: undefined,
eventTarget: undefined,
readyForInteraction: false,
viewStateChanged: false,
Expand Down Expand Up @@ -1063,9 +1066,17 @@ class ViewController {
state: ViewControllerState
): ViewControllerFullState => {
const fullState = { ...state, ...this.derivedState_ };
if (fullState.camera != this.state_.camera) {
if (
fullState.camera != this.state_.camera ||
!fullState.scaledCameraClone
) {
// create a clone of the camera property to avoid editing it
fullState.cameraClone = cloneDeep(fullState.camera);
fullState.scaledCameraClone = fullState.camera
? {
...(cloneDeep(fullState.camera) as ViewStateType),
scale: 1,
}
: undefined;
}
return fullState;
};
Expand Down Expand Up @@ -1122,16 +1133,15 @@ class ViewController {
viewState = buildDeckGlViewStates(
views,
state.viewPortMargins,
state.cameraClone,
state.scaledCameraClone,
state.boundingBox3d,
state.zScale,
this.state_.zScale,
state.bounds,
state.deckSize
);
// reset state
this.derivedState_.readyForInteraction = canCameraBeDefined(
state.cameraClone,
state.scaledCameraClone,
state.boundingBox3d,
state.bounds,
state.deckSize
Expand Down Expand Up @@ -1665,21 +1675,19 @@ function updateViewState(
boundingBox[5] = 0;
}

// clone the camera in case of triggerHome
const camera_ = cloneDeep(camera);
if (!cameraHasZoom(camera_)) {
camera_.zoom = computeCameraZoom(camera, boundingBox, size);
if (!cameraHasZoom(camera)) {
camera.zoom = computeCameraZoom(camera, boundingBox, size);
}
if (!cameraHasTarget(camera_)) {
camera_.target = boxCenter(boundingBox);
if (!cameraHasTarget(camera)) {
camera.target = boxCenter(boundingBox);
if (is3D) {
// apply zScaling to target (target is in real coordinates while zScaling is applied to matrix transform)
applyZScale(camera.target, zScale);
}
}
camera_.minZoom = camera_.minZoom ?? minZoom3D;
camera_.maxZoom = camera_.maxZoom ?? maxZoom3D;
return camera_;
camera.minZoom = camera.minZoom ?? minZoom3D;
camera.maxZoom = camera.maxZoom ?? maxZoom3D;
return camera;
}

/**
Expand All @@ -1688,17 +1696,16 @@ function updateViewState(
*/
function computeViewState(
viewPort: ViewportType,
cameraPosition: ViewStateType | undefined,
scaledCamera: ScaledCamera | undefined,
boundingBox: BoundingBox3D | undefined,
zScale: number,
oldZScale: number,
bounds: BoundingBox2D | BoundsAccessor | undefined,
viewportMargins: MarginsType,
views: ViewsType | undefined,
size: Size
): ViewStateType {
// If the camera is defined, use it
const isCameraPositionDefined = cameraPosition != undefined;
const isCameraPositionDefined = scaledCamera != undefined;
const isBoundsDefined =
bounds &&
!isEmptyBox2D(typeof bounds == "function" ? bounds() : bounds);
Expand All @@ -1712,9 +1719,9 @@ function computeViewState(
// If the camera is defined, use it
if (isCameraPositionDefined) {
return updateViewState(
cameraPosition,
scaledCamera,
boundingBox,
zScale / (oldZScale || 1),
zScale / (scaledCamera.scale || 1),
size
);
}
Expand Down Expand Up @@ -1744,7 +1751,7 @@ function computeViewState(
// If the camera is defined, use it
if (isCameraPositionDefined) {
return updateViewState(
cameraPosition,
scaledCamera,
boundingBox,
zScale,
size,
Expand Down Expand Up @@ -1784,10 +1791,9 @@ function computeViewState(
function buildDeckGlViewStates(
views: ViewsType | undefined,
viewPortMargins: MarginsType,
cameraPosition: ViewStateType | undefined,
scaledCamera: ScaledCamera | undefined,
boundingBox: BoundingBox3D | undefined,
zScale: number,
oldZScale: number,
bounds: BoundingBox2D | BoundsAccessor | undefined,
size: Size
): Record<string, ViewStateType> {
Expand All @@ -1804,10 +1810,9 @@ function buildDeckGlViewStates(
if (singleView) {
const viewState = computeViewState(
views.viewports[0],
cameraPosition,
scaledCamera,
boundingBox,
zScale,
oldZScale,
bounds,
viewPortMargins,
views,
Expand All @@ -1831,10 +1836,9 @@ function buildDeckGlViewStates(
const currentViewport: ViewportType = views.viewports[resultLength];
const currentViewState = computeViewState(
currentViewport,
cameraPosition,
scaledCamera,
boundingBox,
zScale,
oldZScale,
bounds,
viewPortMargins,
views,
Expand Down
7 changes: 6 additions & 1 deletion typescript/packages/subsurface-viewer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export type {

export { TGrid3DColoringMode } from "./SubsurfaceViewer";

export type { BoundingBox2D, BoundingBox3D } from "./components/Map";
export type {
BoundingBox2D,
BoundingBox3D,
Point2D,
Point3D,
} from "./components/Map";

export type {
ExtendedLayerProps,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6045633

Please sign in to comment.