animouse
Version:
lightweight animation state machine for three js
142 lines (141 loc) • 5.99 kB
TypeScript
export interface Vector2Like {
readonly x: number;
readonly y: number;
}
/** The number of vertices in a triangle. */
export declare const TRIANGLE_VERTEX_COUNT = 3;
/**
* Precomputed triangle data for efficient operations.
*/
export interface TriangleCache {
/** A vertex of the triangle. */
readonly origin: Vector2Like;
/** The circumcenter of the triangle. */
readonly circumcenter: Vector2Like;
/** The squared circumradius of the triangle. */
readonly circumradiusSquared: number;
/** Vector from vertex A to vertex B. */
readonly u: Vector2Like;
/** Vector from vertex A to vertex C. */
readonly v: Vector2Like;
/** Pseudo dot product of u with itself (u·u). */
readonly d00: number;
/** Pseudo dot product of u with v (u·v). */
readonly d01: number;
/** Pseudo dot product of v with itself (v·v). */
readonly d11: number;
/** Inverse of the determinant used in barycentric calculations. */
readonly invDenom: number;
/** Minimum bounds of the triangle's axis-aligned bounding box. */
readonly min: Vector2Like;
/** Maximum bounds of the triangle's axis-aligned bounding box. */
readonly max: Vector2Like;
}
/**
* Precomputes triangle data for efficient operations.
* Calculates circumcenter, bounding box, and barycentric coordinate helpers.
*
* @param a - First vertex of the triangle
* @param b - Second vertex of the triangle
* @param c - Third vertex of the triangle
* @returns Precomputed triangle data cache
* @throws {Error} When triangle is degenerate or coordinates are invalid
*/
export declare function precomputeTriangle(a: Vector2Like, b: Vector2Like, c: Vector2Like): TriangleCache;
/**
* Calculates barycentric coordinates for a point relative to a triangle.
* Returns undefined if the point lies outside the triangle.
*
* @param point - The point to calculate barycentric coordinates for
* @param cache - Precomputed triangle data
* @returns Barycentric weights {aW, bW, cW} if point is inside triangle, undefined otherwise
* @throws {Error} When point coordinates or cache data are invalid
*/
export declare function calculateBarycentricWeights(point: Vector2Like, cache: {
origin: Vector2Like;
u: Vector2Like;
v: Vector2Like;
d00: number;
d01: number;
d11: number;
invDenom: number;
min: Vector2Like;
max: Vector2Like;
}): {
aW: number;
bW: number;
cW: number;
} | undefined;
/**
* Calculates the centroid (geometric center) of a triangle.
*
* @param a - First vertex of the triangle
* @param b - Second vertex of the triangle
* @param c - Third vertex of the triangle
* @returns The centroid point of the triangle
* @throws {Error} When any coordinate value is invalid
*/
export declare function calculateTriangleCentroid(a: Vector2Like, b: Vector2Like, c: Vector2Like): Vector2Like;
/**
* Determines if a point is strictly inside a circle (excluding the boundary).
* Uses squared distance comparison to avoid square root operations.
*
* @param origin - The center point of the circle
* @param radiusSquared - The squared radius of the circle
* @param point - The point to test
* @returns True if the point is strictly inside the circle, false otherwise
* @throws {Error} When coordinates are invalid or radius is not positive
*/
export declare function isPointInsideCircle(origin: Vector2Like, radiusSquared: number, point: Vector2Like): boolean;
/**
* Normalizes an azimuth angle to the range [0, 2π).
*
* @param azimuth - The azimuth angle in radians
* @returns The normalized azimuth in the range [0, 2π) radians
* @throws {Error} When the azimuth value is not a valid number
*/
export declare function calculateNormalizedAzimuth(azimuth: number): number;
/**
* Calculates the forward angular distance between two azimuth angles.
* Always measures distance in the positive direction.
*
* @param from - The starting azimuth angle in radians (normalized to [0, 2π))
* @param to - The target azimuth angle in radians (normalized to [0, 2π))
* @returns The forward angular distance in radians [0, 2π)
* @throws {Error} When either azimuth value is invalid
*/
export declare function calculateAngularDistanceForward(from: number, to: number): number;
/**
* Determines if an azimuth angle falls within a specified angular range.
* Handles ranges that wrap around the 0/2π boundary.
*
* @param value - The azimuth angle to test in radians (normalized to [0, 2π))
* @param from - The start of the angular range in radians (normalized to [0, 2π))
* @param to - The end of the angular range in radians (normalized to [0, 2π))
* @returns True if the azimuth is within the range (inclusive), false otherwise
* @throws {Error} When any azimuth value is invalid
*/
export declare function isAzimuthBetween(value: number, from: number, to: number): boolean;
/**
* Calculates the squared Euclidean distance between two points.
* Uses squared distance to avoid square root operation.
*
* @param x1 - X coordinate of the first point
* @param y1 - Y coordinate of the first point
* @param x2 - X coordinate of the second point
* @param y2 - Y coordinate of the second point
* @returns The squared distance between the two points
* @throws {Error} When any coordinate value is invalid
*/
export declare function calculateDistanceSquared(x1: number, y1: number, x2: number, y2: number): number;
/**
* Calculates the squared distance from a point to a line segment.
* Projects the point onto the line segment and clamps to segment boundaries.
*
* @param edge - A tuple containing the two endpoints of the edge
* @param x - X coordinate of the point
* @param y - Y coordinate of the point
* @returns The squared distance from the point to the closest point on the edge
* @throws {Error} When any coordinate value is invalid
*/
export declare function calculateDistanceToEdgeSquared([p1, p2]: [Vector2Like, Vector2Like], x: number, y: number): number;