From 1eac51e2609309292283f0c0395df35b925b4fd4 Mon Sep 17 00:00:00 2001 From: Joost Nieuwenhuijse Date: Sat, 21 Jan 2012 18:56:46 +0100 Subject: [PATCH] Added mirroring of solids --- csg.js | 37 ++++++++++++++++++++++++++++++++++++- index.html | 18 ++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/csg.js b/csg.js index 01fc9fa..bf44224 100644 --- a/csg.js +++ b/csg.js @@ -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); } ); diff --git a/index.html b/index.html index 91f969a..f0cca01 100644 --- a/index.html +++ b/index.html @@ -226,7 +226,6 @@ var cube = CSG.roundedCube({

CSG operations

The 3 standard CSG operations are supported. All CSG operations return a new solid; the source solids are not modified: -

 var csg1 = cube.union(sphere);
 var csg2 = cube.intersect(sphere);
@@ -235,7 +234,6 @@ var csg3 = cube.subtract(sphere);
 
 

Transformations

Solids can be translated, scaled and rotated. Multiple transforms can be combined into a single matrix transform: -

 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);
 
+

Mirroring

+Solids can be mirrored in any plane in 3D space: +
+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);
+
+

Expansion and contraction

Expansion can be seen as the 3D convolution of an object with a sphere. Contraction is the reverse: the area outside the solid