Class CSGSolid
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()Creates a deep clone of this CSG solid.static CSGSolidCreates a CSG solid from a composite shape.static CSGSolidfromPolygons(List<CSGPolygon> polygonList) Creates a CSG solid from a list of CSG polygons.static CSGSolidfromSolidPolygons(List<SolidPolygon> solidPolygons) Creates a CSG solid from a list of SolidPolygon triangles.intReturns the number of polygons in this solid.Returns an unmodifiable view of the polygons in this solid.Performs an intersection operation with another CSG solid.intersect(AbstractCompositeShape shape) Performs an intersection operation with a composite shape.static CSGSolidPerforms an intersection operation on two composite shapes.inverse()Returns the inverse of this solid.Performs a subtraction operation with another CSG solid.subtract(AbstractCompositeShape shape) Performs a subtraction operation with a composite shape.static CSGSolidPerforms a subtraction operation on two composite shapes.Converts this CSG solid to a renderable mesh.Performs a union operation with another CSG solid.union(AbstractCompositeShape shape) Performs a union operation with a composite shape.static CSGSolidPerforms a union operation on two composite shapes.
-
Constructor Details
-
CSGSolid
public CSGSolid()Creates an empty CSG solid.
-
-
Method Details
-
fromPolygons
Creates a CSG solid from a list of CSG polygons.- Parameters:
polygonList- the polygons to include- Returns:
- a new CSG solid
-
fromSolidPolygons
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
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 shapeb- the second shape- Returns:
- a new CSG solid representing the union
-
subtract
Performs a subtraction operation on two composite shapes.Convenience method that converts both shapes to CSGSolids internally.
- Parameters:
a- the shape to subtract fromb- the shape to subtract- Returns:
- a new CSG solid representing the difference
-
intersect
Performs an intersection operation on two composite shapes.Convenience method that converts both shapes to CSGSolids internally.
- Parameters:
a- the first shapeb- 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
Returns an unmodifiable view of the polygons in this solid.- Returns:
- unmodifiable list of polygons
-
clone
Creates a deep clone of this CSG solid. -
union
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)
- Clip A's polygons to keep only parts outside B
- Clip B's polygons to keep only parts outside A
- Invert B, clip to A, invert again (keeps B's surface inside A)
- Build final tree from all remaining polygons
- Parameters:
other- the other solid to union with- Returns:
- a new CSG solid representing the union
-
subtract
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
- Invert A (turning solid into cavity, cavity into solid)
- Clip inverted A to keep only parts inside B
- Clip B to keep only parts inside inverted A
- Invert B twice to get B's cavity surface
- 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
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
- Invert A (swap inside/outside)
- Clip inverted-A to B, keeping parts outside B
- Invert B, clip to A (captures B's interior surface)
- Clip B again to ensure proper boundaries
- 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
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
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
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
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
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
-