/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */ /** */
import { Vector3, Axis, Plane, Ray, Matrix } from '../loader/babylonjs-import';
/**
 * Extension of [Plane](https://doc.babylonjs.com/typedoc/classes/babylon.plane) with methods to keep the plane facing
 * a [Camera](https://doc.babylonjs.com/typedoc/classes/babylon.camera) and putting it in front of a Camera.
 */
export class PivotPlane extends Plane {
    static _tmpCameraDirection = Vector3.Zero();
    static _tmpRay = Ray.Zero();
    static _tmpIdentityMatrix = Matrix.Identity();
    _planeOrigin = Vector3.Zero();
    /**
     * Default distance to a pivot plane.
     */
    static DefaultDistanceToPivotPlane = 2;
    /**
     * Constructor to create empty pivot plane.
     */
    constructor() {
        super(0.0, 0.0, 0.0, 0.0);
    }
    /**
     * Create empty pivot plane.
     */
    static Empty() {
        return new PivotPlane();
    }
    /**
     * Origin of the plane. Will be (0,0,0) if neither {@link  update} or {@link putInFrontOfCamera}
     * has been called.
     */
    get Origin() {
        return this._planeOrigin;
    }
    /**
     * Updates a existing pivot plane so it faces the camera.
     * @param camera Camera to reorient the pivot plane for.
     * @param planeOrigin Optional origin (point on) for plane. Will set {@link Origin}.
     * @returns Signed distance to pivot plane. Positive if the camera is in front. Otherwise false.
     */
    update(camera, planeOrigin) {
        // If pos is not specified then we simply recalculate the pivotplane
        // based on the old position.
        if (planeOrigin !== undefined) {
            this._planeOrigin.copyFromFloats(planeOrigin.x, planeOrigin.y, planeOrigin.z);
        }
        camera.getDirectionToRef(Axis.Z, PivotPlane._tmpCameraDirection);
        const cameraDirection = PivotPlane._tmpCameraDirection;
        cameraDirection.negateToRef(this.normal);
        this.d = -(this.normal.x * this._planeOrigin.x +
            this.normal.y * this._planeOrigin.y +
            this.normal.z * this._planeOrigin.z);
        return this.signedDistanceTo(camera.globalPosition);
    }
    /**
     * Puts the plane so it faces the camera at a distance of `distance` from the camera
     * @param camera {@link Camera} to position plane in front of.
     * @param distance Distance in front of `camera` to place the plane at
     */
    putInFrontOfCamera(camera, distance = PivotPlane.DefaultDistanceToPivotPlane) {
        camera.getDirectionToRef(Axis.Z, PivotPlane._tmpCameraDirection);
        const cameraDirection = PivotPlane._tmpCameraDirection;
        const pivotPointAheadOfCamera = cameraDirection.scaleInPlace(distance).addInPlace(camera.position);
        this._planeOrigin.copyFrom(pivotPointAheadOfCamera);
        this.d = -(this.normal.x * this._planeOrigin.x +
            this.normal.y * this._planeOrigin.y +
            this.normal.z * this._planeOrigin.z);
    }
    /**
     * Checks if plane is in front of the {@link camera} (`true`) or not (`false`).
     * @param camera {@link Camera}
     * @returns `true` if the plane is in front of the {@link camera}. Otherwise `false`.
     */
    isInFrontOfCamera(camera) {
        return this.signedDistanceTo(camera.globalPosition) > 0;
    }
}
//# sourceMappingURL=PivotPlane.js.map