Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions @here/harp-mapview/lib/CameraUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ function setCameraParams(
): void {
const viewportWidth = viewportHeight * camera.aspect;
let hFov = computeFov(focalLength, ppalPoint.x, viewportWidth);
let adjustedFocalLength = focalLength;

if (hFov < MIN_FOV_RAD || hFov > MAX_FOV_RAD) {
// Invalid horizontal fov, clamp and compute again focal length and vertical fov.
hFov = THREE.MathUtils.clamp(hFov, MIN_FOV_RAD, MAX_FOV_RAD);
const focalLength = computeFocalLengthFromFov(hFov, viewportWidth, ppalPoint.x);
verticalFov = computeFov(focalLength, ppalPoint.y, viewportHeight);
adjustedFocalLength = computeFocalLengthFromFov(hFov, viewportWidth, ppalPoint.x);
verticalFov = computeFov(adjustedFocalLength, ppalPoint.y, viewportHeight);
}

camera.fov = THREE.MathUtils.radToDeg(verticalFov);
Expand All @@ -87,12 +88,12 @@ function setCameraParams(
} else {
const width = viewportHeight * camera.aspect;
camera.userData.fovs = {
top: computePosSideFov(focalLength, ppalPoint.y, viewportHeight),
right: computePosSideFov(focalLength, ppalPoint.x, width),
top: computePosSideFov(adjustedFocalLength, ppalPoint.y, viewportHeight),
right: computePosSideFov(adjustedFocalLength, ppalPoint.x, width),
horizontal: hFov
} as Fovs;
}
camera.userData.focalLength = focalLength;
camera.userData.focalLength = adjustedFocalLength;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions @here/harp-mapview/test/CameraUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,31 @@ describe("CameraUtils", function () {
expect(actualPpOffset.y).lt(ppOffset.y).and.closeTo(ppOffset.y, 1e-3);
});
});

describe("horizontal fov clamping with focal length adjustment", function () {
it("correctly adjusts focal length when horizontal fov exceeds limits", function () {
// Set up a wide aspect ratio camera that would cause horizontal FOV issues
camera.aspect = 3.0; // Very wide aspect ratio
const height = 100;
const ppalPoint = { x: 0.8, y: 0 }; // Off-center principal point

CameraUtils.setPrincipalPoint(camera, ppalPoint);

// Set a vertical FOV that would result in horizontal FOV exceeding MAX_FOV_RAD
const verticalFov = Math.PI / 2; // 90 degrees
CameraUtils.setVerticalFov(camera, verticalFov, height);

// Verify that the focal length was adjusted correctly
const adjustedFocalLength = CameraUtils.getFocalLength(camera);
expect(adjustedFocalLength).to.be.greaterThan(0);

// Verify that horizontal FOV is within limits
const horizontalFov = CameraUtils.getHorizontalFov(camera);
expect(horizontalFov).to.be.at.most(MAX_FOV_RAD);

// Verify that the camera's FOV calculations are consistent
const expectedVerticalFov = CameraUtils.getVerticalFov(camera);
expect(expectedVerticalFov).to.be.greaterThan(0).and.at.most(MAX_FOV_RAD);
});
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,11 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/offscreencanvas@^2019.6.4":
version "2019.7.3"
resolved "https://registry.yarnpkg.com/@types/offscreencanvas/-/offscreencanvas-2019.7.3.tgz#90267db13f64d6e9ccb5ae3eac92786a7c77a516"
integrity sha512-ieXiYmgSRXUDeOntE1InxjWyvEelZGP63M+cGuquuRLuIKKT1osnkXjxev9B7d1nXSug5vpunx+gNlbVxMlC9A==

"@types/parse-json@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
Expand Down