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 // Return a new CSG solid with solid and empty space switched. This solid is
// not modified. // not modified.
inverse: function() { 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); return CSG.fromPolygons(flippedpolygons);
}, },
@ -213,6 +213,26 @@ CSG.prototype = {
return CSG.fromPolygons(newpolygons); 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) { translate: function(v) {
return this.transform(CSG.Matrix4x4.translation(v)); return this.transform(CSG.Matrix4x4.translation(v));
}, },
@ -1130,6 +1150,12 @@ CSG.Plane.prototype = {
toString: function() { toString: function() {
return "[normal: "+this.normal.toString()+", w: "+this.w+"]"; 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); 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 // Affine transformation of polygon. Returns a new CSG.Polygon
transform: function(matrix4x4) { transform: function(matrix4x4) {
var newvertices = this.vertices.map(function(v) { return v.transform(matrix4x4); } ); var newvertices = this.vertices.map(function(v) { return v.transform(matrix4x4); } );

View file

@ -226,7 +226,6 @@ var cube = CSG.roundedCube({
<h2>CSG operations</h2> <h2>CSG operations</h2>
The 3 standard CSG operations are supported. All CSG operations return a new solid; the source solids The 3 standard CSG operations are supported. All CSG operations return a new solid; the source solids
are not modified: are not modified:
<br><br>
<pre> <pre>
var csg1 = cube.union(sphere); var csg1 = cube.union(sphere);
var csg2 = cube.intersect(sphere); var csg2 = cube.intersect(sphere);
@ -235,7 +234,6 @@ var csg3 = cube.subtract(sphere);
<h2>Transformations</h2> <h2>Transformations</h2>
Solids can be translated, scaled and rotated. Multiple transforms can be combined into a single matrix transform: Solids can be translated, scaled and rotated. Multiple transforms can be combined into a single matrix transform:
<br><br>
<pre> <pre>
var cube = CSG.cube(); var cube = CSG.cube();
@ -262,6 +260,22 @@ m = m.multiply(CSG.Matrix4x4.scaling([1.1, 1.2, 1.3]));
var cube3 = cube.transform(m); var cube3 = cube.transform(m);
</pre> </pre>
<h2>Mirroring</h2>
Solids can be mirrored in any plane in 3D space:
<pre>
var cube = CSG.cube().translate([1,0,0]);
var cube2 = cube.mirroredX(); // mirrored in the x=0 plane
var cube3 = cube.mirroredY(); // mirrored in the y=0 plane
var cube4 = cube.mirroredZ(); // mirrored in the z=0 plane
// create a plane by specifying 3 points:
var plane = CSG.Plane.fromPoints([5,0,0], [5, 1, 0], [3, 1, 7]);
// and mirror in that plane:
var cube5 = cube.mirrored(plane);
</pre>
<h2>Expansion and contraction</h2> <h2>Expansion and contraction</h2>
Expansion can be seen Expansion can be seen
as the 3D convolution of an object with a sphere. Contraction is the reverse: the area outside the solid as the 3D convolution of an object with a sphere. Contraction is the reverse: the area outside the solid