UNPKG

@bitbybit-dev/core

Version:

Bit By Bit Developers Core CAD API to Program Geometry

303 lines (302 loc) 9.6 kB
import { VerbCurveCircle } from "./curve-circle"; import { VerbCurveEllipse } from "./curve-ellipse"; /** * Contains various methods for nurbs curves. * These methods wrap around Verbnurbs library that you can find here http://verbnurbs.com/. * Thanks Peter Boyer for his work. */ export class VerbCurve { constructor(context, geometryHelper, math) { this.context = context; this.geometryHelper = geometryHelper; this.math = math; this.circle = new VerbCurveCircle(context, this.math); this.ellipse = new VerbCurveEllipse(context, this.math); } /** * Creates a Nurbs curve by providing knots, control points & weights * @param inputs Contains knots, control points and weights * @returns Nurbs curve */ createCurveByKnotsControlPointsWeights(inputs) { return this.context.verb.geom.NurbsCurve.byKnotsControlPointsWeights(inputs.degree, inputs.knots, inputs.points, inputs.weights); } /** * Creates a Nurbs curve by providing control points * @param inputs Control points * @returns Nurbs curve */ createCurveByPoints(inputs) { return this.context.verb.geom.NurbsCurve.byPoints(inputs.points, inputs.degree); } /** * Creates a Bezier Nurbs curve by providing control points and weights * @param inputs Control points * @returns Bezier Nurbs curve */ createBezierCurve(inputs) { return new this.context.verb.geom.BezierCurve(inputs.points, inputs.weights); } /** * Clone the Nurbs curve * @param inputs Nurbs curve * @returns Nurbs curve */ clone(inputs) { return inputs.curve.clone(); } /** * Finds the closest param on the Nurbs curve from the point * @param inputs Nurbs curve with point * @returns Param number */ closestParam(inputs) { return inputs.curve.closestParam(inputs.point); } /** * Finds the closest params on the Nurbs curve from the points * @param inputs Nurbs curve with points * @returns Param numbers */ closestParams(inputs) { return inputs.points.map(pt => inputs.curve.closestParam(pt)); } /** * Finds the closest point on the Nurbs curve from the point * @param inputs Nurbs curve with point * @returns Point */ closestPoint(inputs) { return inputs.curve.closestPoint(inputs.point); } /** * Finds the closest points on the Nurbs curve from the list of points * @param inputs Nurbs curve with points * @returns Points */ closestPoints(inputs) { return inputs.points.map(pt => inputs.curve.closestPoint(pt)); } /** * Finds the control points of the Nurbs curve * @param inputs Nurbs curve * @returns Points */ controlPoints(inputs) { return inputs.curve.controlPoints(); } /** * Finds the degree of the Nurbs curve * @param inputs Nurbs curve * @returns Degree number */ degree(inputs) { return inputs.curve.degree(); } /** * Finds the derivatives of the Nurbs curve at parameter * @param inputs Nurbs curve with specified derivative number and parameter * @returns Derivatives */ derivatives(inputs) { return inputs.curve.derivatives(inputs.parameter, inputs.numDerivatives); } /** * Divides the curve by equal arc length to parameters * @param inputs Nurbs curve * @returns Parameters */ divideByEqualArcLengthToParams(inputs) { const segments = inputs.curve.divideByEqualArcLength(inputs.subdivision); return segments.map(s => s.u); } /** * Divides the curve by equal arc length to points * @param inputs Nurbs curve * @returns Points */ divideByEqualArcLengthToPoints(inputs) { const segments = inputs.curve.divideByEqualArcLength(inputs.subdivision); return segments.map(s => inputs.curve.point(s.u)); } /** * Divides the curve by arc length to parameters * @param inputs Nurbs curve * @returns Parameters */ divideByArcLengthToParams(inputs) { const segments = inputs.curve.divideByArcLength(inputs.length); return segments.map(s => s.u); } /** * Divides the curve by arc length to points * @param inputs Nurbs curve * @returns Points */ divideByArcLengthToPoints(inputs) { const segments = inputs.curve.divideByArcLength(inputs.length); return segments.map(s => inputs.curve.point(s.u)); } /** * Divides multiple curves by equal arc length to points * @param inputs Nurbs curves * @returns Points placed for each curve in separate arrays */ divideCurvesByEqualArcLengthToPoints(inputs) { return inputs.curves.map(curve => this.divideByEqualArcLengthToPoints({ curve, subdivision: inputs.subdivision })); } /** * Divides multiple curves by arc length to points * @param inputs Nurbs curves * @returns Points placed for each curve in separate arrays */ divideCurvesByArcLengthToPoints(inputs) { return inputs.curves.map(curve => this.divideByArcLengthToPoints({ curve, length: inputs.length })); } /** * Finds the domain interval of the curve parameters * @param inputs Nurbs curve * @returns Interval domain */ domain(inputs) { return inputs.curve.domain(); } /** * Start point of the curve * @param inputs Nurbs curve * @returns Start point */ startPoint(inputs) { return inputs.curve.point(inputs.curve.domain().min); } /** * End point of the curve * @param inputs Nurbs curve * @returns End point */ endPoint(inputs) { return inputs.curve.point(inputs.curve.domain().max); } /** * Start points of the curves * @param inputs Nurbs curves * @returns Start points */ startPoints(inputs) { return inputs.curves.map(curve => this.startPoint({ curve })); } /** * End points of the curves * @param inputs Nurbs curves * @returns End points */ endPoints(inputs) { return inputs.curves.map(curve => this.endPoint({ curve })); } /** * Finds the knots of the Nurbs curve * @param inputs Nurbs curve * @returns Knots */ knots(inputs) { return inputs.curve.knots(); } /** * Gets the length of the Nurbs curve at specific parameter * @param inputs Nurbs curve and parameter * @returns Length */ lengthAtParam(inputs) { return inputs.curve.lengthAtParam(inputs.parameter); } /** * Gets the length of the Nurbs curve * @param inputs Nurbs curve * @returns Length */ length(inputs) { return inputs.curve.length(); } /** * Gets the param at specified length on the Nurbs curve * @param inputs Nurbs curve, length and tolerance * @returns Parameter */ paramAtLength(inputs) { return inputs.curve.paramAtLength(inputs.length, inputs.tolerance); } /** * Gets the point at specified parameter on the Nurbs curve * @param inputs Nurbs curve and a parameter * @returns Point */ pointAtParam(inputs) { return inputs.curve.point(inputs.parameter); } /** * Gets the points at specified parameter on the Nurbs curves * @param inputs Nurbs curves and a parameter * @returns Points in arrays for each curve */ pointsAtParam(inputs) { return inputs.curves.map(curve => this.pointAtParam({ curve, parameter: inputs.parameter })); } /** * Reverses the Nurbs curve * @param inputs Nurbs curve * @returns Reversed Nurbs curve */ reverse(inputs) { return inputs.curve.reverse(); } /** * Splits the Nurbs curve in two at a given parameter * @param inputs Nurbs curve with parameter * @returns Nurbs curves */ split(inputs) { return inputs.curve.split(inputs.parameter); } /** * Tangent of the Nurbs curve at a given parameter * @param inputs Nurbs curve with parameter * @returns Tangent vector */ tangent(inputs) { return inputs.curve.tangent(inputs.parameter); } /** * Tessellates the Nurbs curve into a list of points * @param inputs Nurbs curve with tolerance * @returns Points */ tessellate(inputs) { return inputs.curve.tessellate(inputs.tolerance); } /** * Transforms the Nurbs curve * @param inputs Nurbs curve with transformation matrixes * @returns Transformed curve */ transform(inputs) { const points = inputs.curve.controlPoints(); const transformedControlPoints = this.geometryHelper.transformControlPoints(inputs.transformation, points); return this.context.verb.geom.NurbsCurve.byKnotsControlPointsWeights(inputs.curve.degree(), inputs.curve.knots(), transformedControlPoints, inputs.curve.weights()); } /** * Transforms the Nurbs curves * @param inputs Nurbs curves with transformation matrixes * @returns Transformed curves */ transformCurves(inputs) { return inputs.curves.map(curve => this.transform({ curve, transformation: inputs.transformation })); } /** * Weights of the Nurbs curve * @param inputs Nurbs curve * @returns Weights */ weights(inputs) { return inputs.curve.weights(); } }