Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

6.2 KB
Raw Blame
import {
  Matrix4,
  Vector3,
  Quaternion,
  WebGPUCoordinateSystem,
  PerspectiveCamera,
  OrthographicCamera,
} from "three";
import type { NumericArray, Pixel } from "./contracts.js";

/** Column-major matrices; right-handed world; WebGPU clip depth [0,1]. Pixels are CSS pixels. */
export class TelosCamera {
  position: Vector3;
  target: Vector3;
  up: Vector3;
  mode: "perspective" | "orthographic";
  fov: number;
  span: number;
  near: number;
  far: number;
  width: number;
  height: number;
  view: Matrix4;
  inverseView: Matrix4;
  projection: Matrix4;
  inverseProjection: Matrix4;
  viewProjection: Matrix4;
  inverseViewProjection: Matrix4;
  constructor() {
    this.position = new Vector3(4, 3, 5);
    this.target = new Vector3();
    this.up = new Vector3(0, 1, 0);
    this.mode = "perspective";
    this.fov = 35;
    this.span = 5;
    this.near = 0.01;
    this.far = 10000;
    this.width = 1;
    this.height = 1;
    this.view = new Matrix4();
    this.inverseView = new Matrix4();
    this.projection = new Matrix4();
    this.inverseProjection = new Matrix4();
    this.viewProjection = new Matrix4();
    this.inverseViewProjection = new Matrix4();
    this.update();
  }
  resize(width: number, height: number) {
    this.width = Math.max(1, width);
    this.height = Math.max(1, height);
    this.update();
  }
  /** Apply compiler-owned Scene presentation metadata through the existing camera authority. */
  applyDisplayCamera(value: {
    transform: readonly number[];
    lookAtMm?: readonly number[];
    fovDegrees: number;
  }) {
    const t = value.transform;
    if (
      t.length !== 16 ||
      t.some((v) => !Number.isFinite(v)) ||
      !(value.fovDegrees > 0 && value.fovDegrees < 180)
    )
      throw new Error("telos-scene-camera-invalid");
    this.position.set(t[12], t[13], t[14]);
    this.up.set(t[4], t[5], t[6]);
    if (value.lookAtMm)
      this.target.set(value.lookAtMm[0], value.lookAtMm[1], value.lookAtMm[2]);
    else this.target.copy(this.position).add(new Vector3(-t[8], -t[9], -t[10]));
    this.mode = "perspective";
    this.fov = value.fovDegrees;
    this.update();
  }
  /** Tumble the viewing frame together with the eye; poles are ordinary orientations. */
  orbit(yaw: number, pitch: number) {
    const offset = this.position.clone().sub(this.target);
    offset.applyAxisAngle(this.up.clone().normalize(), yaw);
    const right = new Vector3().crossVectors(offset, this.up).normalize();
    offset.applyAxisAngle(right, pitch);
    this.up.applyAxisAngle(right, pitch).normalize();
    this.position.copy(this.target).add(offset);
    this.update();
  }
  orient(direction: NumericArray, up: NumericArray) {
    const distance = this.position.distanceTo(this.target);
    this.position.copy(this.target).add(new Vector3(direction[0], direction[1], direction[2]).normalize().multiplyScalar(distance));
    this.up.set(up[0], up[1], up[2]).normalize();
    this.update();
  }
  update() {
    if (!(this.near > 0 && this.far > this.near && this.span > 0))
      throw new Error("telos-camera-clipping-invalid");
    const rotation = new Matrix4().lookAt(this.position, this.target, this.up);
    this.inverseView.compose(
      this.position,
      new Quaternion().setFromRotationMatrix(rotation),
      new Vector3(1, 1, 1),
    );
    this.view.copy(this.inverseView).invert();
    const aspect = this.width / this.height;
    if (this.mode === "orthographic") {
      const h = this.span / 2;
      this.projection.makeOrthographic(
        -h * aspect,
        h * aspect,
        h,
        -h,
        this.near,
        this.far,
        WebGPUCoordinateSystem,
      );
    } else {
      const h = this.near * Math.tan((this.fov * Math.PI) / 360);
      this.projection.makePerspective(
        -h * aspect,
        h * aspect,
        h,
        -h,
        this.near,
        this.far,
        WebGPUCoordinateSystem,
      );
    }
    this.inverseProjection.copy(this.projection).invert();
    this.viewProjection.multiplyMatrices(this.projection, this.view);
    this.inverseViewProjection.copy(this.viewProjection).invert();
  }
  project(point: NumericArray) {
    const p = new Vector3(point[0], point[1], point[2]).applyMatrix4(
      this.viewProjection,
    );
    return [((p.x + 1) * this.width) / 2, ((1 - p.y) * this.height) / 2, p.z];
  }
  unproject(pixel: Pixel, depth: number) {
    return new Vector3(
      (pixel[0] / this.width) * 2 - 1,
      1 - (pixel[1] / this.height) * 2,
      depth,
    ).applyMatrix4(this.inverseViewProjection);
  }
  worldRay(pixel: Pixel) {
    const origin = this.unproject(pixel, 0),
      end = this.unproject(pixel, 1);
    return { origin, direction: end.clone().sub(origin).normalize() };
  }
  fit(minimum: NumericArray, maximum: NumericArray) {
    const direction = this.position.clone().sub(this.target).normalize();
    const min = new Vector3(minimum[0], minimum[1], minimum[2]),
      max = new Vector3(maximum[0], maximum[1], maximum[2]);
    if (
      ![...Array.from(minimum), ...Array.from(maximum)].every(Number.isFinite)
    )
      return;
    this.target.copy(min).add(max).multiplyScalar(0.5);
    const radius = Math.max(max.distanceTo(min) / 2, 1e-5);
    const aspect = this.width / this.height;
    const halfAngle = Math.atan(
      Math.tan((this.fov * Math.PI) / 360) * Math.min(1, aspect),
    );
    const distance = (radius / Math.sin(halfAngle)) * 1.15;
    // Fit changes framing, not the user's orientation or the viewing-frame up.
    this.position
      .copy(this.target)
      .add(direction.normalize().multiplyScalar(distance));
    this.span = (radius * 2.3) / Math.min(1, aspect);
    this.near = Math.max(radius * 0.01, 1e-8);
    this.far = distance + radius * 20;
    this.update();
  }
  /** Output-only compatibility state. Changes to the adapter never change Telos. */
  toThree() {
    const camera =
      this.mode === "orthographic"
        ? new OrthographicCamera()
        : new PerspectiveCamera();
    camera.matrixAutoUpdate = false;
    camera.matrixWorld.copy(this.inverseView);
    camera.matrixWorldInverse.copy(this.view);
    camera.projectionMatrix.copy(this.projection);
    camera.projectionMatrixInverse.copy(this.inverseProjection);
    camera.position.copy(this.position);
    return camera;
  }
}