Class CSGSolid

java.lang.Object
eu.svjatoslav.sixth.e3d.csg.CSGSolid

public class CSGSolid extends Object
A mutable solid for Constructive Solid Geometry (CSG) operations.

CSG allows combining 3D shapes using boolean operations:

  • Union: Combine two shapes into one
  • Subtract: Carve one shape out of another
  • Intersect: Keep only the overlapping volume

Usage examples:


 // Direct operation on composite shapes (most concise)
 SolidPolygonCube cube = new SolidPolygonCube(new Point3D(0, 0, 0), 80, Color.RED);
 SolidPolygonSphere sphere = new SolidPolygonSphere(new Point3D(0, 0, 0), 96, 12, Color.BLUE);
 CSGSolid result = CSGSolid.subtract(cube, sphere);

 // Fluent chaining for multiple operations
 CSGSolid result = CSGSolid.fromCompositeShape(cube)
     .subtract(sphere)
     .union(cylinder);

 // Convert to renderable mesh (preserves original polygon colors)
 SolidPolygonMesh mesh = result.toMesh(new Point3D(0, 0, 0));
 
See Also:
  • Constructor Details

    • CSGSolid

      public CSGSolid()
      Creates an empty CSG solid.
  • Method Details

    • fromPolygons

      public static CSGSolid fromPolygons(List<CSGPolygon> polygonList)
      Creates a CSG solid from a list of CSG polygons.
      Parameters:
      polygonList - the polygons to include
      Returns:
      a new CSG solid
    • fromSolidPolygons

      public static CSGSolid fromSolidPolygons(List<SolidPolygon> solidPolygons)
      Creates a CSG solid from a list of SolidPolygon triangles.

      Each SolidPolygon is converted to a CSGPolygon (3-vertex N-gon). The color from each SolidPolygon is preserved.

      Parameters:
      solidPolygons - the triangles to convert
      Returns:
      a new CSG solid
    • fromCompositeShape

      public static CSGSolid fromCompositeShape(AbstractCompositeShape shape)
      Creates a CSG solid from a composite shape.

      Extracts all SolidPolygon triangles from the composite shape and converts them to CSGPolygons. This allows using shapes like SolidPolygonCube, SolidPolygonSphere, etc. with CSG operations.

      Parameters:
      shape - the composite shape to convert
      Returns:
      a new CSG solid containing all triangles from the shape
    • union

      Performs a union operation on two composite shapes.

      Convenience method that converts both shapes to CSGSolids internally.

      Parameters:
      a - the first shape
      b - the second shape
      Returns:
      a new CSG solid representing the union
    • subtract

      public static CSGSolid subtract(AbstractCompositeShape a, AbstractCompositeShape b)
      Performs a subtraction operation on two composite shapes.

      Convenience method that converts both shapes to CSGSolids internally.

      Parameters:
      a - the shape to subtract from
      b - the shape to subtract
      Returns:
      a new CSG solid representing the difference
    • intersect

      public static CSGSolid intersect(AbstractCompositeShape a, AbstractCompositeShape b)
      Performs an intersection operation on two composite shapes.

      Convenience method that converts both shapes to CSGSolids internally.

      Parameters:
      a - the first shape
      b - the second shape
      Returns:
      a new CSG solid representing the intersection
    • getPolygonCount

      public int getPolygonCount()
      Returns the number of polygons in this solid.
      Returns:
      the polygon count
    • getPolygons

      public List<CSGPolygon> getPolygons()
      Returns an unmodifiable view of the polygons in this solid.
      Returns:
      unmodifiable list of polygons
    • clone

      public CSGSolid clone()
      Creates a deep clone of this CSG solid.
      Overrides:
      clone in class Object
      Returns:
      a new CSG solid with cloned polygons
    • union

      public CSGSolid union(CSGSolid other)
      Performs a union operation with another CSG solid.

      The result contains all points that are in either solid.

      Algorithm:

       Union(A, B) = clip(A to outside B) + clip(B to outside A)
       
      1. Clip A's polygons to keep only parts outside B
      2. Clip B's polygons to keep only parts outside A
      3. Invert B, clip to A, invert again (keeps B's surface inside A)
      4. Build final tree from all remaining polygons
      Parameters:
      other - the other solid to union with
      Returns:
      a new CSG solid representing the union
    • subtract

      public CSGSolid subtract(CSGSolid other)
      Performs a subtraction operation with another CSG solid.

      The result contains all points that are in this solid but not in the other. This effectively carves the other solid out of this one.

      Algorithm:

       Subtract(A, B) = A - B = clip(inverted A to B) inverted
       
      1. Invert A (turning solid into cavity, cavity into solid)
      2. Clip inverted A to keep only parts inside B
      3. Clip B to keep only parts inside inverted A
      4. Invert B twice to get B's cavity surface
      5. Combine and invert final result

      The inversion trick converts "subtract B from A" into "intersect A with the inverse of B", which the BSP algorithm handles naturally.

      Parameters:
      other - the solid to subtract
      Returns:
      a new CSG solid representing the difference
    • intersect

      public CSGSolid intersect(CSGSolid other)
      Performs an intersection operation with another CSG solid.

      The result contains only the points that are in both solids.

      Algorithm:

       Intersect(A, B) = clip(inverted A to outside B) inverted
       
      1. Invert A (swap inside/outside)
      2. Clip inverted-A to B, keeping parts outside B
      3. Invert B, clip to A (captures B's interior surface)
      4. Clip B again to ensure proper boundaries
      5. Combine and invert final result

      This uses the principle: A ∩ B = ¬(¬A ∪ ¬B)

      Parameters:
      other - the other solid to intersect with
      Returns:
      a new CSG solid representing the intersection
    • union

      public CSGSolid union(AbstractCompositeShape shape)
      Performs a union operation with a composite shape.

      Convenience method that converts the shape to a CSGSolid internally.

      Parameters:
      shape - the composite shape to union with
      Returns:
      a new CSG solid representing the union
    • subtract

      public CSGSolid subtract(AbstractCompositeShape shape)
      Performs a subtraction operation with a composite shape.

      Convenience method that converts the shape to a CSGSolid internally.

      Parameters:
      shape - the composite shape to subtract
      Returns:
      a new CSG solid representing the difference
    • intersect

      public CSGSolid intersect(AbstractCompositeShape shape)
      Performs an intersection operation with a composite shape.

      Convenience method that converts the shape to a CSGSolid internally.

      Parameters:
      shape - the composite shape to intersect with
      Returns:
      a new CSG solid representing the intersection
    • inverse

      public CSGSolid inverse()
      Returns the inverse of this solid.

      The inverse has all polygons flipped, effectively turning the solid inside-out.

      Returns:
      a new CSG solid representing the inverse
    • toMesh

      public SolidPolygonMesh toMesh(Point3D location)
      Converts this CSG solid to a renderable mesh.

      Each polygon's original color is preserved. Colors from source shapes are carried through CSG operations, allowing the result to display which parts came from which shape.

      Parameters:
      location - the position in 3D space for the mesh
      Returns:
      a renderable mesh containing triangles with preserved colors