Skip to content

Commit db1d133

Browse files
committed
feat(csg.js): updated csg.js based on recent changes in OpenjSCAD.org
- node module export - various updates (elipse basics etc)
1 parent 541ced7 commit db1d133

File tree

1 file changed

+79
-26
lines changed

1 file changed

+79
-26
lines changed

csg.js

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ for solid CAD anyway.
9292
9393
*/
9494

95-
(function(module) {
96-
9795
var _CSGDEBUG = false;
9896

9997
function fnNumberSort(a, b) {
@@ -1314,6 +1312,32 @@ for solid CAD anyway.
13141312
var newvertices = polygon.vertices.slice(0);
13151313
newvertices.splice(insertionvertextagindex, 0, endvertex);
13161314
var newpolygon = new CSG.Polygon(newvertices, polygon.shared /*polygon.plane*/ );
1315+
1316+
// FIX
1317+
//calculate plane with differents point
1318+
if(isNaN(newpolygon.plane.w)){
1319+
1320+
var found = false,
1321+
loop = function(callback){
1322+
newpolygon.vertices.forEach(function(item){
1323+
if(found) return;
1324+
callback(item);
1325+
})
1326+
};
1327+
1328+
loop(function(a){
1329+
loop(function(b) {
1330+
loop(function (c) {
1331+
newpolygon.plane = CSG.Plane.fromPoints(a.pos, b.pos, c.pos)
1332+
if(!isNaN(newpolygon.plane.w)) {
1333+
found = true;
1334+
}
1335+
})
1336+
})
1337+
})
1338+
}
1339+
// FIX
1340+
13171341
polygons[polygonindex] = newpolygon;
13181342

13191343
// remove the original sides from our maps:
@@ -1959,11 +1983,11 @@ for solid CAD anyway.
19591983
this._z = 0;
19601984
}
19611985
}
1962-
} else if (('x' in x) && ('y' in x)) {
1963-
this._x = parseFloat(x.x);
1964-
this._y = parseFloat(x.y);
1965-
if ('z' in x) {
1966-
this._z = parseFloat(x.z);
1986+
} else if (('_x' in x) && ('_y' in x)) {
1987+
this._x = parseFloat(x._x);
1988+
this._y = parseFloat(x._y);
1989+
if ('_z' in x) {
1990+
this._z = parseFloat(x._z);
19671991
} else {
19681992
this._z = 0;
19691993
}
@@ -5439,7 +5463,7 @@ for solid CAD anyway.
54395463
CSG.addCenteringToPrototype = function(prot, axes) {
54405464
prot.center = function(cAxes) {
54415465
cAxes = Array.prototype.map.call(arguments, function(a) {
5442-
return a.toLowerCase();
5466+
return a; //.toLowerCase();
54435467
});
54445468
// no args: center on all axes
54455469
if (!cAxes.length) {
@@ -5462,15 +5486,14 @@ for solid CAD anyway.
54625486
this.isCanonicalized = false;
54635487
};
54645488

5465-
// create from an untyped object with identical property names.
5489+
// create from an untyped object with identical property names:
54665490
CAG.fromObject = function(obj) {
54675491
var sides = obj.sides.map(function(s) {
54685492
return CAG.Side.fromObject(s);
54695493
});
54705494
var cag = CAG.fromSides(sides);
5471-
cag.isCanonicalized = obj.isCanonicalized;
54725495
return cag;
5473-
};
5496+
}
54745497

54755498
// Construct a CAG from a list of `CAG.Side` instances.
54765499
CAG.fromSides = function(sides) {
@@ -5583,6 +5606,41 @@ for solid CAD anyway.
55835606
return CAG.fromSides(sides);
55845607
};
55855608

5609+
/* Construct an ellispe
5610+
options:
5611+
center: a 2D center point
5612+
radius: a 2D vector with width and height
5613+
resolution: number of sides per 360 degree rotation
5614+
returns a CAG object
5615+
*/
5616+
CAG.ellipse = function(options) {
5617+
options = options || {};
5618+
var c = CSG.parseOptionAs2DVector(options, "center", [0, 0]);
5619+
var r = CSG.parseOptionAs2DVector(options, "radius", [1, 1]);
5620+
r = r.abs(); // negative radii make no sense
5621+
var res = CSG.parseOptionAsInt(options, "resolution", CSG.defaultResolution2D);
5622+
5623+
var e2 = new CSG.Path2D([[c.x,c.y + r.y]]);
5624+
e2 = e2.appendArc([c.x,c.y - r.y], {
5625+
xradius: r.x,
5626+
yradius: r.y,
5627+
xaxisrotation: 0,
5628+
resolution: res,
5629+
clockwise: true,
5630+
large: false,
5631+
});
5632+
e2 = e2.appendArc([c.x,c.y + r.y], {
5633+
xradius: r.x,
5634+
yradius: r.y,
5635+
xaxisrotation: 0,
5636+
resolution: res,
5637+
clockwise: true,
5638+
large: false,
5639+
});
5640+
e2 = e2.close();
5641+
return e2.innerToCAG();
5642+
};
5643+
55865644
/* Construct a rectangle
55875645
options:
55885646
center: a 2D center point
@@ -6022,23 +6080,15 @@ for solid CAD anyway.
60226080
// by rotating around the plane's origin. An additional right-hand vector should be specified as well,
60236081
// and this is exactly a CSG.OrthoNormalBasis.
60246082
// orthonormalbasis: characterizes the plane in which to extrude
6025-
// depth: thickness of the extruded shape. Extrusion is done from the plane towards above (unless
6026-
// symmetrical option is set, see below)
6027-
//
6028-
// options:
6029-
// {symmetrical: true} // extrude symmetrically in two directions about the plane
6030-
extrudeInOrthonormalBasis: function(orthonormalbasis, depth, options) {
6083+
// depth: thickness of the extruded shape. Extrusion is done symmetrically above and below the plane.
6084+
extrudeInOrthonormalBasis: function(orthonormalbasis, depth) {
60316085
// first extrude in the regular Z plane:
60326086
if (!(orthonormalbasis instanceof CSG.OrthoNormalBasis)) {
60336087
throw new Error("extrudeInPlane: the first parameter should be a CSG.OrthoNormalBasis");
60346088
}
60356089
var extruded = this.extrude({
60366090
offset: [0, 0, depth]
60376091
});
6038-
if(CSG.parseOptionAsBool(options, "symmetrical", false))
6039-
{
6040-
extruded = extruded.translate([0,0,-depth/2]);
6041-
}
60426092
var matrix = orthonormalbasis.getInverseProjectionMatrix();
60436093
extruded = extruded.transform(matrix);
60446094
return extruded;
@@ -6048,8 +6098,8 @@ for solid CAD anyway.
60486098
// one of ["X","Y","Z","-X","-Y","-Z"]
60496099
// The 2d x axis will map to the first given 3D axis, the 2d y axis will map to the second.
60506100
// See CSG.OrthoNormalBasis.GetCartesian for details.
6051-
extrudeInPlane: function(axis1, axis2, depth, options) {
6052-
return this.extrudeInOrthonormalBasis(CSG.OrthoNormalBasis.GetCartesian(axis1, axis2), depth, options);
6101+
extrudeInPlane: function(axis1, axis2, depth) {
6102+
return this.extrudeInOrthonormalBasis(CSG.OrthoNormalBasis.GetCartesian(axis1, axis2), depth);
60536103
},
60546104

60556105
// extruded=cag.extrude({offset: [0,0,10], twistangle: 360, twiststeps: 100});
@@ -6544,14 +6594,17 @@ for solid CAD anyway.
65446594
65456595
But we'll keep CSG.Polygon2D as a stub for backwards compatibility
65466596
*/
6597+
65476598
CSG.Polygon2D = function(points) {
65486599
var cag = CAG.fromPoints(points);
65496600
this.sides = cag.sides;
65506601
};
65516602
CSG.Polygon2D.prototype = CAG.prototype;
65526603

65536604

6605+
//console.log('module', module)
6606+
//module.CSG = CSG;
6607+
//module.CAG = CAG;
6608+
//})(this); //module to export to
65546609

6555-
module.CSG = CSG;
6556-
module.CAG = CAG;
6557-
})(this); //module to export to
6610+
module.exports = {CSG,CAG}//({})(module)

0 commit comments

Comments
 (0)