Skip to content

Commit

Permalink
Introduce *Like interfaces (#727)
Browse files Browse the repository at this point in the history
* Introduce *Like interfaces

* Remove unused

* Cleanup

* Remove usage of Vector

* Update

* Fix imports
  • Loading branch information
Methuselah96 authored Dec 23, 2023
1 parent 8265252 commit 9c7d0d4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 279 deletions.
18 changes: 9 additions & 9 deletions types/three/src/extras/core/Curve.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vector } from '../../math/Vector2.js';
import { Vector2 } from '../../math/Vector2.js';
import { Vector3 } from '../../math/Vector3.js';

/**
Expand All @@ -25,7 +25,7 @@ import { Vector3 } from '../../math/Vector3.js';
* @see {@link https://threejs.org/docs/index.html#api/en/extras/core/Curve | Official Documentation}
* @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/Curve.js | Source}
*/
export abstract class Curve<T extends Vector> {
export abstract class Curve<TVector extends Vector2 | Vector3> {
protected constructor();

/**
Expand All @@ -49,26 +49,26 @@ export abstract class Curve<T extends Vector> {
* @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float`
* @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`.
*/
getPoint(t: number, optionalTarget?: T): T;
getPoint(t: number, optionalTarget?: TVector): TVector;

/**
* Returns a vector for a given position on the {@link Curve} according to the arc length.
* @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float`
* @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created. Default `new T`.
*/
getPointAt(u: number, optionalTarget?: T): T;
getPointAt(u: number, optionalTarget?: TVector): TVector;

/**
* Returns a set of divisions `+1` points using {@link .getPoint | getPoint(t)}.
* @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5`
*/
getPoints(divisions?: number): T[];
getPoints(divisions?: number): TVector[];

/**
* Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}.
* @param divisions Number of pieces to divide the {@link Curve} into. Expects a `Integer`. Default `5`
*/
getSpacedPoints(divisions?: number): T[];
getSpacedPoints(divisions?: number): TVector[];

/**
* Get total {@link Curve} arc length.
Expand Down Expand Up @@ -107,14 +107,14 @@ export abstract class Curve<T extends Vector> {
* @param t A position on the curve. Must be in the range `[ 0, 1 ]`. Expects a `Float`
* @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created.
*/
getTangent(t: number, optionalTarget?: T): T;
getTangent(t: number, optionalTarget?: TVector): TVector;

/**
* Returns tangent at a point which is equidistant to the ends of the {@link Curve} from the point given in {@link .getTangent}.
* @param u A position on the {@link Curve} according to the arc length. Must be in the range `[ 0, 1 ]`. Expects a `Float`
* @param optionalTarget If specified, the result will be copied into this Vector, otherwise a new Vector will be created.
*/
getTangentAt(u: number, optionalTarget?: T): T;
getTangentAt(u: number, optionalTarget?: TVector): TVector;

/**
* Generates the Frenet Frames
Expand All @@ -141,7 +141,7 @@ export abstract class Curve<T extends Vector> {
* Copies another {@link Curve} object to this instance.
* @param source
*/
copy(source: Curve<T>): this;
copy(source: Curve<TVector>): this;

/**
* Returns a JSON object representation of this instance.
Expand Down
15 changes: 8 additions & 7 deletions types/three/src/extras/core/CurvePath.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Curve } from './Curve.js';
import { Vector } from '../../math/Vector2.js';
import { Vector2 } from '../../math/Vector2.js';
import { Vector3 } from '../../math/Vector3.js';

/**
* Curved Path - a curve path is simply a array of connected curves, but retains the api of a curve.
Expand All @@ -8,7 +9,7 @@ import { Vector } from '../../math/Vector2.js';
* @see {@link https://threejs.org/docs/index.html#api/en/extras/core/CurvePath | Official Documentation}
* @see {@link https://github.com/mrdoob/three.js/blob/master/src/extras/core/CurvePath.js | Source}
*/
export class CurvePath<T extends Vector> extends Curve<T> {
export class CurvePath<TVector extends Vector2 | Vector3> extends Curve<TVector> {
/**
* The constructor take no parameters.
*/
Expand All @@ -25,7 +26,7 @@ export class CurvePath<T extends Vector> extends Curve<T> {
* The array of {@link Curve | Curves}.
* @defaultValue `[]`
*/
curves: Array<Curve<T>>;
curves: Array<Curve<TVector>>;

/**
* Whether or not to automatically close the path.
Expand All @@ -37,13 +38,13 @@ export class CurvePath<T extends Vector> extends Curve<T> {
* Add a curve to the {@link .curves} array.
* @param curve
*/
add(curve: Curve<T>): void;
add(curve: Curve<TVector>): void;
/**
* Adds a {@link LineCurve | lineCurve} to close the path.
*/
closePath(): this;

getPoint(t: number, optionalTarget?: T): T;
getPoint(t: number, optionalTarget?: TVector): TVector;

/**
* Get list of cumulative curve lengths of the curves in the {@link .curves} array.
Expand All @@ -58,11 +59,11 @@ export class CurvePath<T extends Vector> extends Curve<T> {
* For example, for a {@link THREE.LineCurve | LineCurve}, the returned number of points is always just 2.
* @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `12`
*/
override getPoints(divisions?: number): T[];
override getPoints(divisions?: number): TVector[];

/**
* Returns a set of divisions `+1` equi-spaced points using {@link .getPointAt | getPointAt(u)}.
* @param divisions Number of pieces to divide the curve into. Expects a `Integer`. Default `40`
*/
override getSpacedPoints(divisions?: number): T[];
override getSpacedPoints(divisions?: number): TVector[];
}
57 changes: 27 additions & 30 deletions types/three/src/math/Quaternion.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Euler } from './Euler.js';
import { Vector3 } from './Vector3.js';
import { Vector3, Vector3Like } from './Vector3.js';
import { Matrix4 } from './Matrix4.js';
import { BufferAttribute } from '../core/BufferAttribute.js';
import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';

export interface QuaternionLike {
readonly x: number;
readonly y: number;
readonly z: number;
readonly w: number;
}

/**
* Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages.
*
Expand Down Expand Up @@ -46,7 +53,7 @@ export class Quaternion {
/**
* Sets values of this quaternion.
*/
set(x: number, y: number, z: number, w: number): Quaternion;
set(x: number, y: number, z: number, w: number): this;

/**
* Clones this quaternion.
Expand All @@ -56,36 +63,36 @@ export class Quaternion {
/**
* Copies values of q to this quaternion.
*/
copy(q: Quaternion): this;
copy(q: QuaternionLike): this;

/**
* Sets this quaternion from rotation specified by Euler angles.
*/
setFromEuler(euler: Euler, update?: boolean): Quaternion;
setFromEuler(euler: Euler, update?: boolean): this;

/**
* Sets this quaternion from rotation specified by axis and angle.
* Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm.
* Axis have to be normalized, angle is in radians.
*/
setFromAxisAngle(axis: Vector3, angle: number): Quaternion;
setFromAxisAngle(axis: Vector3Like, angle: number): this;

/**
* Sets this quaternion from rotation component of m. Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm.
*/
setFromRotationMatrix(m: Matrix4): Quaternion;
setFromUnitVectors(vFrom: Vector3, vTo: Vector3): Quaternion;
setFromRotationMatrix(m: Matrix4): this;
setFromUnitVectors(vFrom: Vector3, vTo: Vector3Like): this;
angleTo(q: Quaternion): number;
rotateTowards(q: Quaternion, step: number): Quaternion;
rotateTowards(q: Quaternion, step: number): this;

identity(): Quaternion;
identity(): this;

/**
* Inverts this quaternion.
*/
invert(): Quaternion;
invert(): this;

conjugate(): Quaternion;
conjugate(): this;
dot(v: Quaternion): number;
lengthSq(): number;

Expand All @@ -97,22 +104,22 @@ export class Quaternion {
/**
* Normalizes this quaternion.
*/
normalize(): Quaternion;
normalize(): this;

/**
* Multiplies this quaternion by b.
*/
multiply(q: Quaternion): Quaternion;
premultiply(q: Quaternion): Quaternion;
multiply(q: Quaternion): this;
premultiply(q: Quaternion): this;

/**
* Sets this quaternion to a x b
* Adapted from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm.
*/
multiplyQuaternions(a: Quaternion, b: Quaternion): Quaternion;
multiplyQuaternions(a: Quaternion, b: Quaternion): this;

slerp(qb: Quaternion, t: number): Quaternion;
slerpQuaternions(qa: Quaternion, qb: Quaternion, t: number): Quaternion;
slerp(qb: Quaternion, t: number): this;
slerpQuaternions(qa: QuaternionLike, qb: Quaternion, t: number): this;
equals(v: Quaternion): boolean;

/**
Expand Down Expand Up @@ -149,9 +156,9 @@ export class Quaternion {
* @param attribute the source attribute.
* @param index index in the attribute.
*/
fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): Quaternion;
fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this;

_onChange(callback: () => void): Quaternion;
_onChange(callback: () => void): this;
_onChangeCallback: () => void;

static slerpFlat(
Expand All @@ -173,17 +180,7 @@ export class Quaternion {
stcOffset1: number,
): number[];

/**
* @deprecated Use qm.slerpQuaternions( qa, qb, t ) instead..
*/
static slerp(qa: Quaternion, qb: Quaternion, qm: Quaternion, t: number): number;

/**
* @deprecated Use {@link Vector#applyQuaternion vector.applyQuaternion( quaternion )} instead.
*/
multiplyVector3(v: any): any;

random(): Quaternion;
random(): this;

[Symbol.iterator](): Generator<number, void>;
}
Loading

0 comments on commit 9c7d0d4

Please sign in to comment.