diff --git a/src/external/viewer.ts b/src/external/viewer.ts index ee7bf4ac..db7cc61e 100644 --- a/src/external/viewer.ts +++ b/src/external/viewer.ts @@ -41,6 +41,14 @@ export { FallbackOptions } from "../viewer/options/FallbackOptions"; export { UrlOptions } from "../viewer/options/UrlOptions"; export { ViewerOptions } from "../viewer/options/ViewerOptions"; +// Geometry +export { + CameraConstructor, + CameraParameters, + CameraUniforms, + ICamera, +} from "../geometry/interfaces/ICamera"; + // Graph export { Image } from "../graph/Image"; export { NavigationDirection } from "../graph/edge/NavigationDirection"; diff --git a/src/geometry/Camera.ts b/src/geometry/Camera.ts index 0b5aa8b9..579ba633 100644 --- a/src/geometry/Camera.ts +++ b/src/geometry/Camera.ts @@ -8,9 +8,9 @@ export abstract class Camera implements ICamera { public readonly parameters: CameraParameters = {}; public readonly uniforms: CameraUniforms = {}; - public projectToSfmFunction: string; - - constructor(public readonly type: string) { } + constructor( + public readonly type: string, + public readonly projectToSfmFunction: string) { } public abstract bearingFromSfm(_point: number[]): number[]; public abstract projectToSfm(_point: number[]): number[]; diff --git a/src/geometry/camera/FisheyeCamera.ts b/src/geometry/camera/FisheyeCamera.ts index dd2040ee..da3edf33 100644 --- a/src/geometry/camera/FisheyeCamera.ts +++ b/src/geometry/camera/FisheyeCamera.ts @@ -97,10 +97,10 @@ vec2 projectToSfm(vec3 bearing, Parameters parameters, Uniforms uniforms) { `; export class FisheyeCamera extends Camera { - public readonly projectToSfmFunction: string = FISHEYE_PROJECT_FUNCTION; - constructor(parameters: number[]) { - super(FISHEYE_CAMERA_TYPE); + super( + FISHEYE_CAMERA_TYPE, + FISHEYE_PROJECT_FUNCTION); const [focal, k1, k2] = parameters; this.parameters.focal = focal; diff --git a/src/geometry/camera/PerspectiveCamera.ts b/src/geometry/camera/PerspectiveCamera.ts index d921b5a3..f225f035 100644 --- a/src/geometry/camera/PerspectiveCamera.ts +++ b/src/geometry/camera/PerspectiveCamera.ts @@ -90,10 +90,10 @@ vec2 projectToSfm(vec3 bearing, Parameters parameters, Uniforms uniforms) { `; export class PerspectiveCamera extends Camera { - public readonly projectToSfmFunction: string = PERSPECTIVE_PROJECT_FUNCTION; - constructor(parameters: number[]) { - super(PERSPECTIVE_CAMERA_TYPE); + super( + PERSPECTIVE_CAMERA_TYPE, + PERSPECTIVE_PROJECT_FUNCTION); const [focal, k1, k2] = parameters; this.parameters.focal = focal; diff --git a/src/geometry/camera/SphericalCamera.ts b/src/geometry/camera/SphericalCamera.ts index 457827f0..02224c35 100644 --- a/src/geometry/camera/SphericalCamera.ts +++ b/src/geometry/camera/SphericalCamera.ts @@ -43,9 +43,11 @@ vec2 projectToSfm(vec3 bearing) { `; export class SphericalCamera extends Camera { - public readonly projectToSfmFunction: string = SPHERICAL_PROJECT_FUNCTION; - - constructor() { super(SPHERICAL_CAMERA_TYPE); } + constructor() { + super( + SPHERICAL_CAMERA_TYPE, + SPHERICAL_PROJECT_FUNCTION); + } public bearingFromSfm(point: number[]): number[] { return bearing(point); diff --git a/src/geometry/interfaces/ICamera.ts b/src/geometry/interfaces/ICamera.ts index d319fa72..42734b34 100644 --- a/src/geometry/interfaces/ICamera.ts +++ b/src/geometry/interfaces/ICamera.ts @@ -1,6 +1,19 @@ export type CameraParameters = { [key: string]: number; }; + export type CameraUniforms = { [key: string]: boolean | number | number[]; }; +/** + * @interface + * + * @description Interface for cameras. This is a + * specification for implementers to model: it is not + * an exported method or class. + * + * Implmenting a custom camera allows the implementer to + * render textures and camera frustums with proper undistortion. + * + * Custom cameras must have a unique type. + */ export interface ICamera { readonly type: string; @@ -12,3 +25,7 @@ export interface ICamera { bearingFromSfm(point: number[]): number[]; projectToSfm(bearing: number[]): number[]; } + +export interface CameraConstructor { + new(parameters: number[]): ICamera; +}; diff --git a/src/viewer/ProjectionService.ts b/src/viewer/ProjectionService.ts index 6d48df19..614d3216 100644 --- a/src/viewer/ProjectionService.ts +++ b/src/viewer/ProjectionService.ts @@ -10,13 +10,14 @@ import { SphericalCamera, SPHERICAL_CAMERA_TYPE, } from "../geometry/camera/SphericalCamera"; -import { ICamera } from "../geometry/interfaces/ICamera"; +import { + CameraConstructor, + ICamera, +} from "../geometry/interfaces/ICamera"; import { ICameraFactory } from "../geometry/interfaces/ICameraFactory"; -export type CameraCtor = { new(parameters: number[]): ICamera; }; - export class ProjectionService implements ICameraFactory { - private readonly _cameraFactory: { [type: string]: CameraCtor; } = {}; + private readonly _cameraFactory: { [type: string]: CameraConstructor; } = {}; constructor() { this.registerCamera( @@ -30,7 +31,7 @@ export class ProjectionService implements ICameraFactory { SphericalCamera); } - public registerCamera(type: string, ctor: CameraCtor): void { + public registerCamera(type: string, ctor: CameraConstructor): void { this._cameraFactory[type] = ctor; } @@ -38,6 +39,7 @@ export class ProjectionService implements ICameraFactory { if (!(type in this._cameraFactory)) { return new PerspectiveCamera([0.85, 0, 0]); } + return new this._cameraFactory[type](parameters); } } diff --git a/src/viewer/Viewer.ts b/src/viewer/Viewer.ts index a1f79dbe..0ee6cd8b 100644 --- a/src/viewer/Viewer.ts +++ b/src/viewer/Viewer.ts @@ -49,7 +49,7 @@ import { cameraControlsToState } from "./Modes"; import { ViewerReferenceEvent } from "./events/ViewerReferenceEvent"; import { IDataProvider } from "../external/api"; import { ViewerResetEvent } from "./events/ViewerResetEvent"; -import { ICamera } from "../geometry/interfaces/ICamera"; +import { CameraConstructor } from "../geometry/interfaces/ICamera"; /** * @class Viewer @@ -1486,7 +1486,7 @@ export class Viewer extends EventEmitter implements IViewer { */ public registerCamera( type: string, - ctor: { new(parameters: number[]): ICamera; }): void { + ctor: CameraConstructor): void { this._navigator.projectionService.registerCamera(type, ctor); }