UNPKG

@bitbybit-dev/core

Version:

Bit By Bit Developers Core CAD API to Program Geometry

79 lines (78 loc) 2.51 kB
/** * Contains various methods for polyline. Polyline in bitbybit is a simple object that has points property containing an array of points. * { points: number[][] } */ export class Polyline { constructor(context, geometryHelper) { this.context = context; this.geometryHelper = geometryHelper; } /** * Converts a polyline to a NURBS curve * Returns the verbnurbs NurbsCurve object * @param inputs Polyline to be transformed to curve * @returns Verb nurbs curve */ convertToNurbsCurve(inputs) { return this.context.verb.geom.NurbsCurve.byPoints(inputs.polyline.points, 1); } /** * Gets the length of the polyline * @param inputs Polyline to be queried * @returns Length of the polyline */ length(inputs) { let distanceOfPolyline = 0; for (let i = 1; i < inputs.polyline.points.length; i++) { const previousPoint = inputs.polyline.points[i - 1]; const currentPoint = inputs.polyline.points[i]; distanceOfPolyline += this.context.verb.core.Vec.dist(previousPoint, currentPoint); } return distanceOfPolyline; } /** * Gets the number of points in the polyline * @param inputs Polyline to be queried * @returns Number of points in polyline */ countPoints(inputs) { return inputs.polyline.points.length; } /** * Gets the points of the polyline * @param inputs Polyline to be queried * @returns Points of the polyline */ getPoints(inputs) { return inputs.polyline.points; } /** * Reverse the points of the polyline * @param inputs Polyline to be reversed * @returns Reversed polyline */ reverse(inputs) { return { points: inputs.polyline.points.reverse() }; } /** * Transform the polyline * @param inputs Polyline to be transformed * @returns Transformed polyline */ transformPolyline(inputs) { const transformation = inputs.transformation; let transformedControlPoints = inputs.polyline.points; transformedControlPoints = this.geometryHelper.transformControlPoints(transformation, transformedControlPoints); return { points: transformedControlPoints }; } /** * Create the polyline * @param inputs Points of the polyline * @returns Polyline */ create(inputs) { return { points: inputs.points, }; } }