UNPKG

@rwk/physics-math

Version:
263 lines 12.2 kB
/** * @packageDocumentation * @module Functionals */ import { quat, vec4 } from "gl-matrix"; import { ViewOf } from "./utils"; import { Value, ValueInFrame, IPCompiled, IPFunction, Relative, IPFunctionCalculus, IPCompileResult, IndefiniteIntegral, IPFunctionBase, Variable } from "./base"; import { Units } from "./unit-defs"; import { InertialFrame } from "./frame"; import { Divide, Multiply, Unit } from "./units"; import { Style, StyleContext } from "./latex"; export declare enum TYPE { SCALAR = 0, VECTOR = 1, ROTATION = 2, POINT = 9, ORIENTATION = 10 } interface DataType<T extends TYPE> { readonly type: T; } export declare type DataTypeOf<D> = D extends number ? TYPE.SCALAR : D extends DataType<infer T> ? T : never; export declare const datatype: (d: any) => TYPE; /** * Marker for all non-scalar values */ /** * Marker for all non-scalar values */ export interface NonScalarValue extends DataType<TYPE.VECTOR | TYPE.POINT | TYPE.ROTATION | TYPE.ORIENTATION> { 0: number; 1: number; 2: number; 3: number; type: TYPE.POINT | TYPE.VECTOR | TYPE.ORIENTATION | TYPE.ROTATION; } export declare type RelativeOf<T> = T extends Point ? Vector : T extends Orientation ? Rotation : T; export declare type InFrameOf<T> = T extends Vector ? Point : T extends Rotation ? Orientation : T; /** * Our primitive datatypes */ export declare type ScalarValue<U extends Unit> = number; export declare type BaseValueRelative<U extends Unit = Unit> = ScalarValue<U> | Vector<U> | Rotation<U>; export declare type BaseValueInFrame = Point | Orientation; export declare type BaseValueNonScalar = Point | Vector | Orientation | Rotation | NonScalarValue; export declare type BaseValueRelativeNonScalar<U extends Unit = Unit> = Vector<U> | Rotation<U>; export declare type BaseValue = number | BaseValueNonScalar; declare abstract class ArrayBase<C extends Unit<any>> extends Float64Array implements NonScalarValue, Value<NonScalarValue, C> { 0: number; 1: number; 2: number; 3: number; readonly unit: C; protected constructor(unit: C); get value(): this; abstract get type(): TYPE.POINT | TYPE.VECTOR | TYPE.ORIENTATION | TYPE.ROTATION; assign(): this; assign(other: ArrayBase<C>): this; assign(a: number, b: number, c: number): this; assign(a: number, b: number, c: number, d: number): this; magnitude(): number; magnitudeSqr(): number; equiv<T>(f: T): this | null; protected compileFn(): IPCompileResult<NonScalarValue, 0>; f_?: IPCompiled<BaseValue, C, 0>; /** * The implementing function */ get f(): IPCompiled<BaseValue, C, 0>; compile(): IPCompiled<NonScalarValue, C, 0>; /** * Compute the LaTeX representation of this function. * @param varName? The parameter name (or expression) * @param ctx? */ abstract toTex(varName?: Variable, ctx?: StyleContext): string; toTexWithUnits(varName?: Variable, ctx?: StyleContext): string; /** * Cached LaTeX string */ tex_?: string; style_?: Style; /** * Get the LaTeX representation of this function. The value is cached. */ get tex(): string; /** * Produce HTML from the LaTeX representation. Produces a new HTML element on each call * @param varName?? The variable name to be used; ordinarily t (time). * @param block?? * @param ctx?? */ toHtml(varName?: Variable, block?: boolean, ctx?: StyleContext): ViewOf<this> & Element; /** * Produce HTML from the LaTeX representation. Produces a new HTML element on each reference, * equivalent to: * ``` * pFun.toHtml(); * ``` */ get html(): ViewOf<this> & Element; } declare abstract class Vectorish<C extends Unit, W extends 0 | 1 = 0 | 1> extends ArrayBase<C> implements DataType<TYPE.VECTOR | TYPE.POINT> { 0: number; 1: number; 2: number; 3: W; protected constructor(unit: C, x: number | undefined, y: number | undefined, z: number | undefined, w: W); abstract get type(): TYPE.POINT | TYPE.VECTOR; get vec4(): vec4; get x(): number; set x(v: number); get y(): number; set y(v: number); get z(): number; set z(v: number); get w(): W; add(v: Vector<C>): this; addf(v: Vector<C>): this; sub(v: Vector<C>): this; subf(v: Vector<C>): this; mult(n: number): this; multf(n: number): this; clone(): this; } export declare class Point extends Vectorish<Units.length, 1> implements DataType<TYPE.POINT>, ValueInFrame<Point, Units.length> { get type(): TYPE.POINT; readonly frame: InertialFrame; constructor(frame: InertialFrame, x?: number, y?: number, z?: number); clone(): this; toTex(varName: string | undefined, ctx: StyleContext | undefined): string; } export declare class Vector<U extends Unit = Units.length, D extends Divide<U, Units.time> = Divide<U, Units.time>, I extends Multiply<U, Units.time> = Multiply<U, Units.time>> extends Vectorish<U, 0> implements Relative, Value<Vector<U, D, I>, U>, DataType<TYPE.VECTOR>, IPFunctionCalculus<Vector<U, D, I>, U, 0, D, I> { get type(): TYPE.VECTOR; constructor(unit: U, x?: number, y?: number, z?: number, w?: 0); get returnType(): TYPE.VECTOR; derivative(): IPFunctionCalculus<Vector<D, Divide<D, Units.time>, U>, D, 1, Divide<D, Units.time>, U>; integral(): IndefiniteIntegral<Vector<U>, Multiply<Unit, Units.time>, Unit>; simplify(options?: any): IPFunctionBase<Vector<U, D, I>, U, 0>; toTex(varName?: Variable, ctx?: StyleContext): string; toTexWithUnits(varName?: Variable, ctx?: StyleContext): string; get name(): string; get nargs(): 0; } /** * Rotation or Orientation represented as a versor, aka a unit quaternion. */ declare abstract class Rotationish<C extends Unit> extends ArrayBase<C> implements DataType<TYPE.ROTATION | TYPE.ORIENTATION> { protected constructor(unit: C, i?: number, j?: number, k?: number, w?: number); abstract get type(): TYPE.ROTATION | TYPE.ORIENTATION; get i(): number; set i(v: number); get j(): number; set j(v: number); get k(): number; set k(v: number); get w(): number; set w(v: number); protected get quat(): vec4; /** * Addition here is the group operator on the rotation group, i.e. addition of spherical (great-circle) * on the versor surface. This corresponds to multiplication of the underlying quaternions. * @param a */ add(a: Rotationish<C>): this; abstract create(): Rotationish<C>; abstract clone(): Rotationish<C>; addf(a: Rotationish<C>): this; sub(a: Rotationish<C>): this; subf(a: Rotationish<C>): this; /** * Because multiplication of quaternions is equal to addition of spherical vectors on the * 3D unit sphere, multiplication of those vectors by a real is the same as exponentiation of * their quaternions by a real. * @param n */ mult(n: number): this; /** * Because multiplication of quaternions is equal to addition of spherical vectors on the * 3D unit sphere, multiplication of those vectors by a real is the same as exponentiation of * their quaternions by a real. * @param n */ multf(n: number): this; /** * Normalizes in-place. */ normalize(): this; conjugate(): Rotationish<C>; rotate<T extends Rotationish<C>>(b: T): T; static fromEulerX<U extends Unit, T extends Rotationish<U>>(f: (i: number, j: number, k: number, w: number) => T, x: number, y: number, z: number): T; } export declare type Positional = Point | Vector; export declare type Rotational = Orientation | Rotation; export declare class Orientation extends Rotationish<Units.angle> implements DataType<TYPE.ORIENTATION>, ValueInFrame<Orientation, Units.angle> { readonly frame: InertialFrame; constructor(frame: InertialFrame, i?: number, j?: number, k?: number, w?: number); get type(): TYPE.ORIENTATION; clone(): this; create(frame?: InertialFrame): this; static coerce<U extends Unit>(frame: InertialFrame, q: Rotationish<U> | quat): Orientation; static fromEuler(frame: InertialFrame, x: number, y: number, z: number): Orientation; toTex(varName: string | undefined, ctx: StyleContext | undefined): string; } export declare class Rotation<C extends Unit = Units.angle> extends Rotationish<C> implements Relative { toTex(varName?: string | undefined, ctx?: StyleContext | undefined): string; constructor(unit: C, i?: number, j?: number, k?: number, w?: number); get type(): TYPE.ROTATION; clone(): Rotation<C>; create(): Rotation<C>; /** * Addition here is the group operator on the rotation group, i.e. addition of spherical (great-circle) * on the versor surface. This corresponds to multiplication of the underlying quaternions. * @param a */ add(a: Rotationish<C>): this; static fromEuler<U extends Unit>(unit: U, x: number, y: number, z: number): Rotation<U>; } export declare const isBaseValue: (v: any) => v is BaseValue; export declare const valueType: (v: any) => TYPE; export declare const isPositional: (v: any) => v is Positional; export declare const isPoint: (v: any) => v is Point; export declare const isVector: (v: any) => v is Vector<Unit<import("./units").CompleteTerms<{ length: 1; }>>, Unit<import("./units").DivideTerms<import("./units").CompleteTerms<{ length: 1; }>, import("./units").CompleteTerms<{ time: 1; }>>>, Unit<import("./units").MultiplyTerms<import("./units").CompleteTerms<{ length: 1; }>, import("./units").CompleteTerms<{ time: 1; }>>>>; export declare const isRotational: (v: any) => v is Rotational; export declare const isOrientation: (v: any) => v is Orientation; export declare const isRotation: (v: any) => v is Rotation<Unit<import("./units").CompleteTerms<{ angle: 1; }>>>; export declare const isIntrinsicValue: (v: any) => v is BaseValueInFrame; export declare const isScalarValue: (v: any) => v is number; export declare const isNonScalarValue: (v: any) => v is NonScalarValue; export declare function vector<U extends Unit>(unit: U): (x?: number, y?: number, z?: number) => Vector<U>; export declare function vector<U extends Unit>(unit: U, x?: number, y?: number, z?: number): Vector<U>; export declare function point<F extends InertialFrame>(frame: F): (x?: number, y?: number, z?: number) => Point; export declare function point<F extends InertialFrame>(frame: F, x?: number, y?: number, z?: number): Point; export declare function rotation<U extends Unit>(unit: U): (x?: number, y?: number, z?: number, w?: number) => Rotation<U>; export declare function rotation<U extends Unit>(unit: U, x?: number, y?: number, z?: number, w?: number): Rotation<U>; export declare function orientation<F extends InertialFrame>(frame: F): (x?: number, y?: number, z?: number, w?: number) => Orientation; export declare function orientation<F extends InertialFrame>(frame: F, x?: number, y?: number, z?: number, w?: number): Orientation; export declare function isRelative(v: IPFunction): v is IPFunction<BaseValueRelative>; export declare function isRelative(v: IPCompiled): v is IPCompiled<BaseValueRelative>; export declare function isRelative(v: number): v is number; export declare function isRelative(v: Positional): v is Vector; export declare function isRelative(v: Rotational): v is Orientation; export declare function isRelative(v: BaseValue): v is BaseValueRelative; export declare function isScalar(v: IPFunction): v is IPFunction<number>; export declare function isScalar(v: IPCompiled): v is IPCompiled<number>; export declare function isScalar(v: number): v is number; export declare function isScalar(v: Positional): false; export declare function isScalar(v: Rotational): false; export declare function isScalar(v: BaseValue): v is number; export {}; //# sourceMappingURL=math-types.d.ts.map