Class CSG

java.lang.Object
eu.mihosoft.vrl.v3d.CSG

public class CSG extends Object
Constructive Solid Geometry (CSG). This implementation is a Java port of https://github.com/evanw/csg.js/ with some additional features like polygon extrude, transformations etc. Thanks to the author for creating the CSG.js library.

Implementation Details All CSG operations are implemented in terms of two functions, Node.clipTo(eu.mihosoft.vrl.v3d.Node) and Node.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.clipTo(b);
     b.clipTo(a);
     a.build(b.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.clipTo(b);
     b.clipTo(a);
     b.invert();
     b.clipTo(a);
     b.invert();
     a.build(b.allPolygons());
 
Subtraction and intersection naturally follow from set operations. If union is A | B, differenceion is A - B = ~(~A | B) and intersection is A & B = ~(~A | ~B) where ~ is the complement operator.
  • Method Details Link icon

    • fromPolygons Link icon

      public static CSG fromPolygons(List<Polygon> polygons)
      Constructs a CSG from a list of Polygon instances.
      Parameters:
      polygons - polygons
      Returns:
      a CSG instance
    • fromPolygons Link icon

      public static CSG fromPolygons(Polygon... polygons)
      Constructs a CSG from the specified Polygon instances.
      Parameters:
      polygons - polygons
      Returns:
      a CSG instance
    • fromPolygons Link icon

      public static CSG fromPolygons(PropertyStorage storage, List<Polygon> polygons)
      Constructs a CSG from a list of Polygon instances.
      Parameters:
      storage - shared storage
      polygons - polygons
      Returns:
      a CSG instance
    • fromPolygons Link icon

      public static CSG fromPolygons(PropertyStorage storage, Polygon... polygons)
      Constructs a CSG from the specified Polygon instances.
      Parameters:
      storage - shared storage
      polygons - polygons
      Returns:
      a CSG instance
    • clone Link icon

      public CSG clone()
      Overrides:
      clone in class Object
    • getPolygons Link icon

      public List<Polygon> getPolygons()
      Returns:
      the polygons of this CSG
    • getIntersections Link icon

      public List<Vector3d> getIntersections(Straight line)
      Parameters:
      line -
      Returns:
      the positions of intersections of CSG with line
    • getIntersectionsAndNormals Link icon

      public List<Intersection> getIntersectionsAndNormals(Straight line)
      Parameters:
      line -
      Returns:
      the positions of intersections of CSG with line
    • getCrossSection Link icon

      public List<Line3d> getCrossSection(Vector3d planePoint, Vector3d planeNormal)
    • optimization Link icon

      public CSG optimization(CSG.OptType type)
      Defines the CSg optimization type.
      Parameters:
      type - optimization type
      Returns:
      this CSG
    • union Link icon

      public CSG union(CSG csg)
      Return a new CSG solid representing the union of this csg and the specified csg. Note: Neither this csg nor the specified csg are weighted.
          A.union(B)
      
          +-------+            +-------+
          |       |            |       |
          |   A   |            |       |
          |    +--+----+   =   |       +----+
          +----+--+    |       +----+       |
               |   B   |            |       |
               |       |            |       |
               +-------+            +-------+
       
      Parameters:
      csg - other csg
      Returns:
      union of this csg and the specified csg
    • dumbUnion Link icon

      public CSG dumbUnion(CSG csg)
      Returns a csg consisting of the polygons of this csg and the specified csg. The purpose of this method is to allow fast union operations for objects that do not intersect.

      WARNING: this method does not apply the csg algorithms. Therefore, please ensure that this csg and the specified csg do not intersect.

      Parameters:
      csg - csg
      Returns:
      a csg consisting of the polygons of this csg and the specified csg
    • union Link icon

      public CSG union(List<CSG> csgs)
      Return a new CSG solid representing the union of this csg and the specified csgs. Note: Neither this csg nor the specified csg are weighted.
          A.union(B)
      
          +-------+            +-------+
          |       |            |       |
          |   A   |            |       |
          |    +--+----+   =   |       +----+
          +----+--+    |       +----+       |
               |   B   |            |       |
               |       |            |       |
               +-------+            +-------+
       
      Parameters:
      csgs - other csgs
      Returns:
      union of this csg and the specified csgs
    • union Link icon

      public CSG union(CSG... csgs)
      Return a new CSG solid representing the union of this csg and the specified csgs. Note: Neither this csg nor the specified csg are weighted.
          A.union(B)
      
          +-------+            +-------+
          |       |            |       |
          |   A   |            |       |
          |    +--+----+   =   |       +----+
          +----+--+    |       +----+       |
               |   B   |            |       |
               |       |            |       |
               +-------+            +-------+
       
      Parameters:
      csgs - other csgs
      Returns:
      union of this csg and the specified csgs
    • hull Link icon

      public CSG hull(List<CSG> csgs)
      Returns the convex hull of this csg and the union of the specified csgs.
      Parameters:
      csgs - csgs
      Returns:
      the convex hull of this csg and the specified csgs
    • hull Link icon

      public CSG hull(CSG... csgs)
      Returns the convex hull of this csg and the union of the specified csgs.
      Parameters:
      csgs - csgs
      Returns:
      the convex hull of this csg and the specified csgs
    • difference Link icon

      public CSG difference(List<CSG> csgs)
      Return a new CSG solid representing the difference of this csg and the specified csgs. Note: Neither this csg nor the specified csgs are weighted.
       A.difference(B)
      
       +-------+            +-------+
       |       |            |       |
       |   A   |            |       |
       |    +--+----+   =   |    +--+
       +----+--+    |       +----+
            |   B   |
            |       |
            +-------+
       
      Parameters:
      csgs - other csgs
      Returns:
      difference of this csg and the specified csgs
    • difference Link icon

      public CSG difference(CSG... csgs)
      Return a new CSG solid representing the difference of this csg and the specified csgs. Note: Neither this csg nor the specified csgs are weighted.
       A.difference(B)
      
       +-------+            +-------+
       |       |            |       |
       |   A   |            |       |
       |    +--+----+   =   |    +--+
       +----+--+    |       +----+
            |   B   |
            |       |
            +-------+
       
      Parameters:
      csgs - other csgs
      Returns:
      difference of this csg and the specified csgs
    • difference Link icon

      public CSG difference(CSG csg)
      Return a new CSG solid representing the difference of this csg and the specified csg. Note: Neither this csg nor the specified csg are weighted.
       A.difference(B)
      
       +-------+            +-------+
       |       |            |       |
       |   A   |            |       |
       |    +--+----+   =   |    +--+
       +----+--+    |       +----+
            |   B   |
            |       |
            +-------+
       
      Parameters:
      csg - other csg
      Returns:
      difference of this csg and the specified csg
    • intersect Link icon

      public CSG intersect(CSG csg)
      Return a new CSG solid representing the intersection of this csg and the specified csg. Note: Neither this csg nor the specified csg are weighted.
           A.intersect(B)
      
           +-------+
           |       |
           |   A   |
           |    +--+----+   =   +--+
           +----+--+    |       +--+
                |   B   |
                |       |
                +-------+
       }
       
      Parameters:
      csg - other csg
      Returns:
      intersection of this csg and the specified csg
    • intersect Link icon

      public CSG intersect(List<CSG> csgs)
      Return a new CSG solid representing the intersection of this csg and the specified csgs. Note: Neither this csg nor the specified csgs are weighted.
           A.intersect(B)
      
           +-------+
           |       |
           |   A   |
           |    +--+----+   =   +--+
           +----+--+    |       +--+
                |   B   |
                |       |
                +-------+
       }
       
      Parameters:
      csgs - other csgs
      Returns:
      intersection of this csg and the specified csgs
    • intersect Link icon

      public CSG intersect(CSG... csgs)
      Return a new CSG solid representing the intersection of this csg and the specified csgs. Note: Neither this csg nor the specified csgs are weighted.
           A.intersect(B)
      
           +-------+
           |       |
           |   A   |
           |    +--+----+   =   +--+
           +----+--+    |       +--+
                |   B   |
                |       |
                +-------+
       }
       
      Parameters:
      csgs - other csgs
      Returns:
      intersection of this csg and the specified csgs
    • toStlFile Link icon

      public void toStlFile(String filename) throws IOException
      Throws:
      IOException
    • toStlString Link icon

      public String toStlString()
      Returns this csg in STL string format.
      Returns:
      this csg in STL string format
    • toStlString Link icon

      public StringBuilder toStlString(StringBuilder sb)
      Returns this csg in STL string format.
      Parameters:
      sb - string builder
      Returns:
      the specified string builder
    • color Link icon

      public CSG color(javafx.scene.paint.Color c)
    • toObj Link icon

      public ObjFile toObj()
    • toObjString Link icon

      public StringBuilder toObjString(StringBuilder sb)
      Returns this csg in OBJ string format.
      Parameters:
      sb - string builder
      Returns:
      the specified string builder
    • toObjString Link icon

      public String toObjString()
      Returns this csg in OBJ string format.
      Returns:
      this csg in OBJ string format
    • weighted Link icon

      public CSG weighted(WeightFunction f)
    • transformed Link icon

      public CSG transformed(Transform transform)
      Returns a transformed copy of this CSG.
      Parameters:
      transform - the transform to apply
      Returns:
      a transformed copy of this CSG
    • transform Link icon

      public void transform(Transform transform)
      Parameters:
      transform - the current CSG
    • toJavaFXMesh Link icon

      public MeshContainer toJavaFXMesh()
    • toJavaFXMeshSimple Link icon

      public MeshContainer toJavaFXMeshSimple()
      Returns the CSG as JavaFX triangle mesh.
      Returns:
      the CSG as JavaFX triangle mesh
    • getBounds Link icon

      public Bounds getBounds()
      Returns the bounds of this csg.
      Returns:
      bouds of this csg
    • setDefaultOptType Link icon

      public static void setDefaultOptType(CSG.OptType optType)
      Parameters:
      optType - the optType to set
    • setOptType Link icon

      public void setOptType(CSG.OptType optType)
      Parameters:
      optType - the optType to set