Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 2c260c6de39f3d5c171b586deac010760c380886 @evanw committed Nov 30, 2011
Showing with 634 additions and 0 deletions.
  1. +7 −0 LICENSE
  2. +38 −0 README.md
  3. +589 −0 csg.js
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Evan Wallace (http://madebyevan.com/)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
+# csg.js
+
+![](http://evanw.github.com/csg.js/csg.png)
+
+Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean operations like union and intersection to combine 3D solids. This library implements CSG operations elegantly and concisely using BSP trees, and is meant to serve as an easily understandable implementation of the algorithm. All edge cases involving overlapping coplanar polygons in both solids are correctly handled.
+
+Example usage:
+
+ var cube = CSG.cube();
+ var sphere = CSG.sphere({ radius: 1.3 });
+ var polygons = cube.subtract(sphere).toPolygons();
+
+## Documentation
+
+[Detailed documentation](http://evanw.github.com/csg.js/docs/) can be automatically generated using [Docco](http://jashkenas.github.com/docco/).
+
+## Implementation Details
+
+All CSG operations are implemented in terms of two functions, `clipTo()` and `invert()`, which remove parts of a BSP tree inside another BSP tree and swap solid and empty space, respectively. To find the union of `a` and `b`, we want to remove everything in `a` inside `b` and everything in `b` inside `a`, then combine polygons from `a` and `b` into one solid:
+
+ a.root.clipTo(b.root);
+ b.root.clipTo(a.root);
+ a.root.build(b.root.allPolygons());
+
+The only tricky part is handling overlapping coplanar polygons in both trees. The code above keeps both copies, but we need to keep them in one tree and remove them in the other tree. To remove them from `b` we can clip the inverse of `b` against `a`. The code for union now looks like this:
+
+ a.root.clipTo(b.root);
+ b.root.clipTo(a.root);
+ b.root.invert();
+ b.root.clipTo(a.root);
+ b.root.invert();
+ a.root.build(b.root.allPolygons());
+
+Subtraction and intersection naturally follow from set operations. If union is `A | B`, subtraction is `A - B = ~(~A | B)` and intersection is `A & B = ~(~A | ~B)` where `~` is the complement operator.
+
+## License
+
+Copyright (c) 2011 Evan Wallace (http://madebyevan.com/), under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
Oops, something went wrong.

0 comments on commit 2c260c6

Please sign in to comment.