@bitbybit-dev/jscad
Version:
Bit By Bit Developers JSCAD based CAD Library to Program Geometry
183 lines (182 loc) • 6.36 kB
JavaScript
/**
* Contains various functions for solid 3D shapes from JSCAD library https://github.com/jscad/OpenJSCAD.org
* Thanks JSCAD community for developing this kernel
*/
export class JSCADShapes {
constructor(jscad, math) {
this.math = math;
this.jscad = jscad;
}
cube(inputs) {
return this.jscad.primitives.cube({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
size: inputs.size
});
}
cubesOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.cube({ center, size: inputs.size });
});
}
cuboid(inputs) {
return this.jscad.primitives.cuboid({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
size: [inputs.width, inputs.height, inputs.length]
});
}
cuboidsOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.cuboid({
center,
width: inputs.width,
length: inputs.length,
height: inputs.height
});
});
}
cylinderElliptic(inputs) {
return this.jscad.primitives.cylinderElliptic({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
height: inputs.height,
startRadius: [inputs.startRadius[0], inputs.startRadius[1]],
endRadius: [inputs.endRadius[0], inputs.endRadius[1]],
segments: inputs.segments,
});
}
cylinderEllipticOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.cylinderElliptic({
center,
height: inputs.height,
startRadius: inputs.startRadius,
endRadius: inputs.endRadius,
segments: inputs.segments
});
});
}
cylinder(inputs) {
return this.jscad.primitives.cylinder({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
height: inputs.height,
radius: inputs.radius,
segments: inputs.segments,
});
}
cylindersOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.cylinder({
center,
height: inputs.height,
radius: inputs.radius,
segments: inputs.segments
});
});
}
ellipsoid(inputs) {
return this.jscad.primitives.ellipsoid({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
radius: [inputs.radius[0], inputs.radius[1], inputs.radius[2]],
segments: inputs.segments,
axes: [
[-1, 0, 0],
[0, -1, 0],
[0, 0, -1],
],
});
}
ellipsoidsOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.ellipsoid({
center,
radius: inputs.radius,
segments: inputs.segments
});
});
}
geodesicSphere(inputs) {
let sphere = this.jscad.primitives.geodesicSphere({ radius: inputs.radius, frequency: inputs.frequency });
sphere = this.jscad.transforms.translate([inputs.center[0], inputs.center[1], inputs.center[2]], sphere);
return sphere;
}
geodesicSpheresOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.geodesicSphere({
center,
radius: inputs.radius,
frequency: inputs.frequency
});
});
}
roundedCuboid(inputs) {
return this.jscad.primitives.roundedCuboid({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
size: [inputs.width, inputs.height, inputs.length],
roundRadius: inputs.roundRadius,
segments: inputs.segments,
});
}
roundedCuboidsOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.roundedCuboid({
center,
width: inputs.width,
height: inputs.height,
length: inputs.length,
roundRadius: inputs.roundRadius,
segments: inputs.segments
});
});
}
roundedCylinder(inputs) {
return this.jscad.primitives.roundedCylinder({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
height: inputs.height,
radius: inputs.radius,
roundRadius: inputs.roundRadius,
segments: inputs.segments,
});
}
roundedCylindersOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.roundedCylinder({
center,
radius: inputs.radius,
roundRadius: inputs.roundRadius,
segments: inputs.segments,
height: inputs.height,
});
});
}
sphere(inputs) {
return this.jscad.primitives.sphere({
center: [inputs.center[0], inputs.center[1], inputs.center[2]],
radius: inputs.radius,
segments: inputs.segments
});
}
spheresOnCenterPoints(inputs) {
return inputs.centers.map(center => {
return this.sphere({
center,
radius: inputs.radius,
segments: inputs.segments,
});
});
}
torus(inputs) {
return this.jscad.primitives.torus({
// center: [inputs.center[0], inputs.center[1], inputs.center[2]],
innerRadius: inputs.innerRadius,
outerRadius: inputs.outerRadius,
innerSegments: inputs.innerSegments,
outerSegments: inputs.outerSegments,
innerRotation: this.math.degToRad({ number: inputs.innerRotation }),
outerRotation: this.math.degToRad({ number: inputs.outerRotation }),
startAngle: this.math.degToRad({ number: inputs.startAngle }),
});
}
fromPolygonPoints(inputs) {
const pts = inputs.polygonPoints.map(vertices => vertices.reverse());
return this.jscad.geometries.geom3.fromPoints(pts);
}
}