UNPKG

@bitbybit-dev/jscad

Version:

Bit By Bit Developers JSCAD based CAD Library to Program Geometry

70 lines (69 loc) 2.9 kB
/** * Contains various functions for Polygon from JSCAD library https://github.com/jscad/OpenJSCAD.org * Thanks JSCAD community for developing this kernel */ export class JSCADPolygon { constructor(jscad, geometryHelper, math) { this.jscad = jscad; this.geometryHelper = geometryHelper; this.math = math; } createFromPoints(inputs) { const twoDimensionalPoints = inputs.points.map(pt => [pt[0], pt[1]]); return this.removeDuplicatesAndCreateFromPoints(twoDimensionalPoints); } createFromPolyline(inputs) { const twoDimensionalPoints = inputs.polyline.points.map(pt => [pt[0], pt[1]]); return this.removeDuplicatesAndCreateFromPoints(twoDimensionalPoints); } // TODO conversions between various CAD kernels should happen on higher levels of abstraction (this was meant to create jscad polygons from VERB-NURBS curves) createFromCurve(inputs) { const twoDimensionalPoints = inputs.curve.tessellate().map(pt => [pt[0], pt[1]]); return this.removeDuplicatesAndCreateFromPoints(twoDimensionalPoints); } createFromPath(inputs) { return this.removeDuplicatesAndCreateFromPoints(inputs.path.points); } circle(inputs) { return this.jscad.primitives.circle({ center: inputs.center, radius: inputs.radius, segments: inputs.segments }); } ellipse(inputs) { return this.jscad.primitives.ellipse({ center: inputs.center, radius: [inputs.radius[0], inputs.radius[1]], segments: inputs.segments }); } rectangle(inputs) { return this.jscad.primitives.rectangle({ center: [inputs.center[0], inputs.center[1]], size: [inputs.width, inputs.length] }); } roundedRectangle(inputs) { return this.jscad.primitives.roundedRectangle({ center: [inputs.center[0], inputs.center[1]], size: [inputs.width, inputs.length], roundRadius: inputs.roundRadius, segments: inputs.segments, }); } square(inputs) { return this.jscad.primitives.square({ center: [inputs.center[0], inputs.center[1]], size: inputs.size }); } star(inputs) { return this.jscad.primitives.star({ center: [inputs.center[0], inputs.center[1]], vertices: inputs.vertices, density: inputs.density, outerRadius: inputs.outerRadius, innerRadius: inputs.innerRadius, startAngle: this.math.degToRad({ number: inputs.startAngle }), }); } removeDuplicatesAndCreateFromPoints(twoDimensionalPoints) { const duplicatePointsRemoved = this.geometryHelper.removeConsecutiveVectorDuplicates(twoDimensionalPoints); const polygon = this.jscad.primitives.polygon({ points: duplicatePointsRemoved }); return polygon; } }