'use strict'; import * as Vector from './vectorlib.js'; /** A 3D vector * @typedef {Object} Vector * @property {number} x * @property {number} y * @property {number} z */ /** Estimate the maximum grade of the bezier. * @param {Vector} p1 - Start * @param {Vector} h1 - Handle 1 * @param {Vector} h2 - Handle 2 * @param {Vector} p2 - End * @param {number} iterationCount - How many steps along the bezier to check * @returns {number} Grade as a decimal */ export function estimateMaxGrade(p1, h1, h2, p2, iterationCount = 50){ let grade = 0; let totalGrade = 0; for(let i=0; i<=iterationCount; i++){ let tan = evaluateVelocity(p1, h1, h2, p2, i/iterationCount); let newGrade = Math.abs(tan.y)/Math.sqrt(tan.x*tan.x + tan.z*tan.z); grade = Math.max(grade, newGrade); totalGrade += newGrade; } return grade; } /** Estimate the length of the bezier. * @param {Vector} p1 - Start * @param {Vector} h1 - Handle 1 * @param {Vector} h2 - Handle 2 * @param {Vector} p2 - End * @param {number} iterationCount - How many steps along the bezier to check * @returns {number} Length */ export function estimateLength(p1, h1, h2, p2, iterationCount = 50){ let length = 0; let prevPos = p1; let curPos = {}; for(let i=1; i<=iterationCount; i++){ curPos = evaluatePoint(p1, h1, h2, p2, i/iterationCount); length += Vector.distance(prevPos, curPos); prevPos = curPos; } return length; } /** Estimate the maximum 3D curvature of the bezier. * @param {Vector} p1 - Start * @param {Vector} h1 - Handle 1 * @param {Vector} h2 - Handle 2 * @param {Vector} p2 - End * @param {number} iterationCount - How many steps along the bezier to check * @returns {number} Curvature, take `1/curvature` to get radius. */ export function estimateMaxCurvature(p1, h1, h2, p2, iterationCount = 50){ let curvature = 0; let t = 0; let vel = {}; for(let i=0; i