Added mirroring of solids

This commit is contained in:
Joost Nieuwenhuijse 2012-01-21 18:56:46 +01:00
parent 720136cd8b
commit 1eac51e260
2 changed files with 52 additions and 3 deletions

37
csg.js
View file

@ -203,7 +203,7 @@ CSG.prototype = {
// Return a new CSG solid with solid and empty space switched. This solid is
// not modified.
inverse: function() {
var flippedpolygons = this.polygons.map(function(p) { p.flipped(); });
var flippedpolygons = this.polygons.map(function(p) { return p.flipped(); });
return CSG.fromPolygons(flippedpolygons);
},
@ -212,6 +212,26 @@ CSG.prototype = {
var newpolygons = this.polygons.map(function(p) { return p.transform(matrix4x4); } );
return CSG.fromPolygons(newpolygons);
},
mirrored: function(plane) {
var newpolygons = this.polygons.map(function(p) { return p.mirrored(plane); } );
return CSG.fromPolygons(newpolygons);
},
mirroredX: function() {
var plane = new CSG.Plane(new CSG.Vector3D(1,0,0), 0);
return this.mirrored(plane);
},
mirroredY: function() {
var plane = new CSG.Plane(new CSG.Vector3D(0,1,0), 0);
return this.mirrored(plane);
},
mirroredZ: function() {
var plane = new CSG.Plane(new CSG.Vector3D(0,0,1), 0);
return this.mirrored(plane);
},
translate: function(v) {
return this.transform(CSG.Matrix4x4.translation(v));
@ -1130,6 +1150,12 @@ CSG.Plane.prototype = {
toString: function() {
return "[normal: "+this.normal.toString()+", w: "+this.w+"]";
},
mirrorPoint: function(point3d) {
var distance = this.signedDistanceToPoint(point3d);
var mirrored = point3d.minus(this.normal.times(distance * 2.0));
return mirrored;
},
};
@ -1290,6 +1316,15 @@ CSG.Polygon.prototype = {
return new CSG.Polygon(newvertices, this.shared, newplane);
},
mirrored: function(plane) {
var newvertices = this.vertices.map(function(v) {
var newpos = plane.mirrorPoint(v.pos);
return new CSG.Vertex(newpos);
});
newvertices.reverse();
return new CSG.Polygon(newvertices, this.shared);
},
// Affine transformation of polygon. Returns a new CSG.Polygon
transform: function(matrix4x4) {
var newvertices = this.vertices.map(function(v) { return v.transform(matrix4x4); } );