UNPKG

toosoon-utils

Version:
109 lines (108 loc) 3.19 kB
import { lerp } from '../../math'; import { Vector3 } from '../geometry'; import Curve from './Curve'; /** * Utility class for manipulating 3D lines * * @exports * @class LineCurve3 * @extends Curve<Vector3> */ export default class LineCurve3 extends Curve { type = 'LineCurve3'; /** * X-axis coordinate of the start point */ x1; /** * Y-axis coordinate of the start point */ y1; /** * Z-axis coordinate of the start point */ z1; /** * X-axis coordinate of the end point */ x2; /** * Y-axis coordinate of the end point */ y2; /** * Z-axis coordinate of the end point */ z2; /** * @param {number} x1 X-axis coordinate of the start point * @param {number} y1 Y-axis coordinate of the start point * @param {number} z1 Z-axis coordinate of the start point * @param {number} x2 X-axis coordinate of the end point * @param {number} y2 Y-axis coordinate of the end point * @param {number} z2 Z-axis coordinate of the end point */ constructor(x1, y1, z1, x2, y2, z2) { super(); this.x1 = x1; this.y1 = y1; this.z1 = z1; this.x2 = x2; this.y2 = y2; this.z2 = z2; } /** * Interpolate a point on this curve * * @param {number} t Normalized time value to interpolate * @returns {Vector3} Interpolated coordinates on this curve */ getPoint(t) { return new Vector3(...LineCurve3.interpolate(t, this.x1, this.y1, this.z1, this.x2, this.y2, this.z2)); } /** * Interpolate a point on this curve * * @param {number} u Normalized position value to interpolate * @returns {Vector3} Interpolated coordinates on this curve */ getPointAt(u) { return this.getPoint(u); } /** * Compute an unit vector tangent for a given normalized time value * * @param {number} t Normalized time value * @returns {Vector3} Tangent vector */ getTangent(t) { return new Vector3(...Vector3.sub([this.x1, this.y1, this.z1], [this.x2, this.y2, this.z2])).normalize(); } /** * Compute an unit vector tangent for a given normalized position value * * @param {number} u Normalized position value * @returns {Vector3} Tangent vector */ getTangentAt(u) { return this.getTangent(u); } /** * Interpolate a point on a 3D line * * @param {number} t Normalized time value to interpolate * @param {number} x1 X-axis coordinate of the start point * @param {number} y1 Y-axis coordinate of the start point * @param {number} z1 Z-axis coordinate of the start point * @param {number} x2 X-axis coordinate of the end point * @param {number} y2 Y-axis coordinate of the end point * @param {number} z2 Z-axis coordinate of the end point * @returns {Point3} Interpolated coordinates on the line */ static interpolate(t, x1, y1, z1, x2, y2, z2) { const x = lerp(t, x1, x2); const y = lerp(t, y1, y2); const z = lerp(t, z1, z2); return [x, y, z]; } }