@itwin/core-common
Version:
iTwin.js components common to frontend and backend
62 lines • 2.53 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Views
*/
import { JsonUtils } from "@itwin/core-bentley";
import { Angle, Point3d } from "@itwin/core-geometry";
/** The current position (eyepoint), lens angle, and focus distance of a camera.
* @see [Views]($docs/learning/frontend/Views.md)
* @public
*/
export class Camera {
lens;
focusDist;
eye;
static isValidLensAngle(val) {
return val.radians > (Math.PI / 8.0) && val.radians < Math.PI;
}
static validateLensAngle(val) {
if (!this.isValidLensAngle(val))
val.setRadians(Math.PI / 2.0);
}
invalidateFocus() { this.focusDist = 0.0; }
get isFocusValid() { return this.focusDist > 0.0 && this.focusDist < 1.0e14; }
getFocusDistance() { return this.focusDist; }
setFocusDistance(dist) { this.focusDist = dist; }
get isLensValid() { return Camera.isValidLensAngle(this.lens); }
validateLens() { Camera.validateLensAngle(this.lens); }
getLensAngle() { return this.lens; }
setLensAngle(angle) { this.lens.setFrom(angle); }
getEyePoint() { return this.eye; }
setEyePoint(pt) { this.eye.setFrom(pt); }
get isValid() { return this.isLensValid && this.isFocusValid; }
equals(other) {
return Math.abs(this.lens.radians - other.lens.radians) < .01 &&
Math.abs(this.focusDist - other.focusDist) < .1 &&
this.eye.isAlmostEqual(other.eye);
}
clone() { return new Camera(this); }
setFrom(rhs) {
this.lens.setFrom(rhs.lens);
this.focusDist = rhs.focusDist;
this.eye.setFrom(rhs.eye);
}
/** Construct a Camera
* @param props The properties of the new camera. If undefined, create a camera with eye at {0,0,0}, 90 degree lens, 1 meter focus distance.
*/
constructor(props) {
if (props !== undefined) {
this.lens = Angle.fromJSON(props.lens);
this.focusDist = JsonUtils.asDouble(props.focusDist);
this.eye = Point3d.fromJSON(props.eye);
return;
}
this.lens = Angle.createRadians(Math.PI / 2.0);
this.focusDist = 1;
this.eye = new Point3d();
}
}
//# sourceMappingURL=Camera.js.map