UNPKG

toosoon-utils

Version:
40 lines (39 loc) 1.08 kB
import { Vector3 } from '../geometry'; import LineCurve3 from './LineCurve3'; import Curve from './Curve'; /** * Utility class for manipulating 3D polylines * * @exports * @class PolylineCurve3 * @extends Curve */ export default class PolylineCurve3 extends Curve { type = 'PolylineCurve3'; /** * Array of points defining the curve */ points = []; /** * @param {Point3[]} [points] Array of points defining the curve */ constructor(points = []) { super(); this.points = points; } /** * Interpolate a point on this curve * * @param {number} t Normalized time value to interpolate * @returns {Vector3} Interpolated coordinates on this curve */ getPoint(t) { const points = this.points; const p = (points.length - 1) * t; const index = Math.floor(p); const weight = p - index; const p1 = points[index === 0 ? index : index - 1]; const p2 = points[index]; return new Vector3(...LineCurve3.interpolate(weight, ...p1, ...p2)); } }