UNPKG

toosoon-utils

Version:
123 lines (122 loc) 4.06 kB
import { catmullRom } from '../../maths'; import { Vector3 } from '../geometry'; import Curve from './Curve'; /** * Utility class for manipulating Catmull-Rom 3D curves * * @exports * @class CatmullRomCurve3 * @extends Curve */ export default class CatmullRomCurve3 extends Curve { type = 'CatmullRomCurve3'; /** * 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 first control point */ cp1x; /** * Y-axis coordinate of the first control point */ cp1y; /** * Z-axis coordinate of the first control point */ cp1z; /** * X-axis coordinate of the second control point */ cp2x; /** * Y-axis coordinate of the second control point */ cp2y; /** * Z-axis coordinate of the second control point */ cp2z; /** * 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} cp1x X-axis coordinate of the first control point * @param {number} cp1y Y-axis coordinate of the first control point * @param {number} cp1z Z-axis coordinate of the first control point * @param {number} cp2x X-axis coordinate of the second control point * @param {number} cp2y Y-axis coordinate of the second control point * @param {number} cp2z Z-axis coordinate of the second control 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, cp1x, cp1y, cp1z, cp2x, cp2y, cp2z, x2, y2, z2) { super(); this.x1 = x1; this.y1 = y1; this.z1 = z1; this.cp1x = cp1x; this.cp1y = cp1y; this.cp1z = cp1z; this.cp2x = cp2x; this.cp2y = cp2y; this.cp2z = cp2z; this.x2 = x2; this.y2 = y2; this.z2 = z2; } /** * Interpolate a point on this curve * * @param {number} t Normalized time value to interpolate * @returns {Vector2} Interpolated coordinates on this curve */ getPoint(t) { return new Vector3(...CatmullRomCurve3.interpolate(t, this.x1, this.y1, this.z1, this.cp1x, this.cp1y, this.cp1z, this.cp2x, this.cp2y, this.cp2z, this.x2, this.y2, this.z2)); } /** * Interpolate a point on a Catmull-Rom curve * * @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} cp1x X-axis coordinate of the first control point * @param {number} cp1y Y-axis coordinate of the first control point * @param {number} cp1z Z-axis coordinate of the first control point * @param {number} cp2x X-axis coordinate of the second control point * @param {number} cp2y Y-axis coordinate of the second control point * @param {number} cp2z Z-axis coordinate of the second control 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 curve */ static interpolate(t, x1, y1, z1, cp1x, cp1y, cp1z, cp2x, cp2y, cp2z, x2, y2, z2) { const x = catmullRom(t, x1, cp1x, cp2x, x2); const y = catmullRom(t, y1, cp1y, cp2y, y2); const z = catmullRom(t, z1, cp1z, cp2z, z2); return [x, y, z]; } }