molstar
Version:
A comprehensive macromolecular library.
38 lines (37 loc) • 2.06 kB
JavaScript
/**
* Copyright (c) 2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Adam Midlik <midlik@gmail.com>
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.fovAdjustedPosition = fovAdjustedPosition;
exports.fovNormalizedCameraPosition = fovNormalizedCameraPosition;
const linear_algebra_1 = require("../mol-math/linear-algebra");
/** Return the distance adjustment ratio for conversion from the "reference camera"
* to a camera with an arbitrary field of view `fov`. */
function distanceAdjustment(mode, fov) {
if (mode === 'orthographic')
return 1 / (2 * Math.tan(fov / 2));
else
return 1 / (2 * Math.sin(fov / 2));
}
/** Return the position for a camera with an arbitrary field of view `fov`
* necessary to just fit into view the same sphere (with center at `target`)
* as the "reference camera" placed at `refPosition` would fit, while keeping the camera orientation.
* The "reference camera" is a camera which can just fit into view a sphere of radius R with center at distance 2R
* (this corresponds to FOV = 2 * asin(1/2) in perspective mode or FOV = 2 * atan(1/2) in orthographic mode). */
function fovAdjustedPosition(target, refPosition, mode, fov) {
const delta = linear_algebra_1.Vec3.sub((0, linear_algebra_1.Vec3)(), refPosition, target);
const adjustment = distanceAdjustment(mode, fov);
return linear_algebra_1.Vec3.scaleAndAdd(delta, target, delta, adjustment); // return target + delta * adjustment
}
/** Return the inverse of fovAdjustedPosition to be able to store invariant camera position,
* e.g., in MolViewSpec snapshots.
*/
function fovNormalizedCameraPosition(target, refPosition, mode, fov) {
const delta = linear_algebra_1.Vec3.sub((0, linear_algebra_1.Vec3)(), refPosition, target);
const adjustment = distanceAdjustment(mode, fov) || 1;
return linear_algebra_1.Vec3.scaleAndAdd(delta, target, delta, 1 / adjustment); // return target + delta / adjustment
}
;