Constructive Solid Geometry - Sixth 3D
Table of Contents
1. What is CSG?
Constructive Solid Geometry (CSG) is a modeling technique that builds complex 3D shapes by combining simpler primitives using boolean operations. Instead of manually creating every vertex and face, you define shapes as the result of operations like "merge these two cubes" or "carve a hole using this sphere."
CSG is particularly powerful for:
- Procedural modeling — generate complex geometry algorithmically
- CAD/CAM applications — define parts as combinations of primitives
- Game development — create architectural elements, holes, cavities
- Rapid prototyping — iterate on designs by adjusting operations
The three fundamental CSG operations are:
| Operation | Symbol | Result |
|---|---|---|
| Subtract | A - B | A with B carved out (holes, cavities) |
| Union | A + B | Combined volume (both shapes merged) |
| Intersect | A ∩ B | Volume where both overlap |
See the CSG demo for an interactive visualization.
2. The Three Operations
The screenshot above shows all three operations displayed left to right: subtract (green cube with spherical cavity), union (merged green and orange shapes), and intersect (only the overlapping region in blue).
The diagrams below use the same green cube (A) and orange sphere (B) as the screenshot above. Each operation transforms these inputs differently, producing the results shown from left to right in the image.
2.1. Subtract (A - B)
Subtract removes the orange sphere (B) from the green cube (A), carving out a cavity. The diagram shows B acting as a "cutter" — where it overlaps A, a hole is created. Interior faces are preserved and become visible, allowing you to see inside the carved-out space (shown as the orange dashed curve in the result).
This matches the leftmost shape in the screenshot: a green cube with a visible spherical hollow inside, showing the interior surfaces created by the subtraction.
This operation is ideal for creating:
- Holes and tunnels
- Carved-out spaces
- Hollow objects
SolidPolygonCube cube = new SolidPolygonCube(origin(), 80, Color.GREEN); SolidPolygonSphere sphere = new SolidPolygonSphere(origin(), 60, 8, Color.ORANGE); cube.subtract(sphere); // cube now has a spherical cavity
2.2. Union (A + B)
Union merges the green cube (A) and orange sphere (B) into one continuous volume. The diagram shows both shapes combining — the interior seam (where they overlap) is removed, creating a single solid surface with no internal boundaries (indicated by the dashed blue line labeled "removed").
This corresponds to the center shape in the screenshot: both green and orange colors present but seamlessly joined, forming one unified object.
SolidPolygonCube cube = new SolidPolygonCube(origin(), 80, Color.GREEN); SolidPolygonSphere sphere = new SolidPolygonSphere(origin(), 60, 8, Color.ORANGE); cube.union(sphere); // cube now contains the merged result
2.3. Intersect (A ∩ B)
Intersect keeps only the volume where the green cube (A) and orange sphere (B) overlap — the region that is inside both shapes simultaneously. The diagram shows this as the blue-shaded area: the portion of the sphere that fits within the cube boundaries. Everything else is discarded.
This is the rightmost shape in the screenshot: only the overlapping portion remains, showing which parts of space were occupied by both the cube and sphere at the same time.
This operation is useful for:
- Creating shapes constrained by multiple boundaries
- Finding collision regions
- Trimming geometry to fit within bounds
SolidPolygonCube cube = new SolidPolygonCube(origin(), 80, Color.GREEN); SolidPolygonSphere sphere = new SolidPolygonSphere(origin(), 60, 8, Color.ORANGE); cube.intersect(sphere); // only the overlapping region remains
3. BSP Tree Algorithm
CSG boolean operations are implemented using Binary Space Partitioning (BSP) trees. A BSP tree recursively divides 3D space using planes, creating a hierarchical structure that enables efficient polygon clipping and spatial queries.
3.1. BSP Tree Structure
Each BSP node contains:
- A partitioning plane that divides space into two half-spaces
- Polygons that lie exactly on this plane (coplanar)
- Front subtree — polygons on the same side as the plane's normal
- Back subtree — polygons on the opposite side
3.2. Key BSP Operations
The BSP tree provides three core operations that enable CSG:
| Operation | Description |
|---|---|
invert() |
Flip all normals, swap front/back children |
clipTo(tree) |
Remove polygons inside the other tree's solid |
addPolygons() |
Insert new polygons, splitting at planes |
Invert is fundamental to CSG. By flipping inside/outside, we can transform subtraction and intersection into variations of clipping:
- Subtract = invert A, clip against B, add B's clipped parts, invert back
- Intersect = invert A, clip B against A, invert both, combine, invert back
3.3. Polygon Clipping
When a polygon crosses a partitioning plane, it's split into two fragments:
This recursive splitting ensures that all polygons are cleanly classified as entirely in front, entirely behind, or exactly on a plane — never "spanning" across.
4. Using CSG in Sixth 3D
CSG operations are methods on AbstractCompositeShape. They modify the shape in-place — the result replaces the original geometry.
4.1. Basic Usage
import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.solid.*; // Create two shapes SolidPolygonCube cube = new SolidPolygonCube(origin(), 100, Color.GREEN); SolidPolygonSphere sphere = new SolidPolygonSphere(origin(), 70, 12, Color.ORANGE); // Perform CSG operations (in-place modification) cube.subtract(sphere); // Cube with spherical cavity // or cube.union(sphere); // Merged shape // or cube.intersect(sphere); // Only overlapping region // Add to scene shapes.addShape(cube.setBackfaceCulling(true));
4.2. Child Handling Behavior
CSG operations only affect SolidPolygon children. Other children are preserved:
| Child Type | Union | Subtract | Intersect |
|---|---|---|---|
| SolidPolygon (this) | Replaced with result | Replaced with result | Replaced with result |
| SolidPolygon (other) | Merged into result | Discarded (cutter) | Discarded |
| Line, TextCanvas | Preserved | Preserved | Preserved |
| Nested composite | Preserved unchanged | Preserved unchanged | Preserved unchanged |
This allows you to attach labels, decorations, or wireframe overlays to shapes without them being affected by CSG operations.
4.3. Important Notes
- Shapes are modified in-place. The original geometry is replaced. Clone shapes beforehand if you need to preserve the originals.
- CSG works on SolidPolygon children only. TexturedTriangle and other shape types are not processed.
- Result quality depends on mesh density. Low-polygon inputs may produce visible artifacts at intersection boundaries. Use higher subdivision counts for smoother results.
- Backface culling is recommended. CSG results often have internal
faces from the cutting operation. Enable culling to hide backfaces:
shape.setBackfaceCulling(true)
