Name

CG_3DDifference — Perform 3D difference

Synopsis

geometry CG_3DDifference(geometry geom1, geometry geom2);

Description

Returns that part of geom1 that is not part of geom2.

Availability: 3.5.0

This method needs SFCGAL backend.

This method implements the SQL/MM specification. SQL-MM IEC 13249-3: 5.1

This function supports 3d and will not drop the z-index.

This function supports Polyhedral surfaces.

This function supports Triangles and Triangulated Irregular Network Surfaces (TIN).

Examples

Subtract a smaller tetrahedral solid and render the two inputs and the actual remaining solid separately.

Code
WITH data AS (
  SELECT CG_MakeSolid('POLYHEDRALSURFACE Z (
    ((0 0 0,0 4 0,4 0 0,0 0 0)),
    ((0 0 0,4 0 0,0 0 4,0 0 0)),
    ((0 0 0,0 0 4,0 4 0,0 0 0)),
    ((4 0 0,0 4 0,0 0 4,4 0 0))
  )'::geometry) AS outer_solid
), solids AS (
  SELECT outer_solid,
         ST_Scale(outer_solid, 0.5, 0.5, 0.5) AS inner_solid
  FROM data
)
SELECT outer_solid AS input_outer,
       inner_solid AS input_inner,
       CG_3DDifference(outer_solid, inner_solid) AS difference
FROM solids;
Output
POLYHEDRALSURFACE Z (((0 0 0,0 4 0,4 0 0,0 0 0)),((0 0 0,4 0 0,0 0 4,0 0 0)),((0 0 0,0 0 4,0 4 0,0 0 0)),((4 0 0,0 4 0,0 0 4,4 0 0))) | POLYHEDRALSURFACE Z (((0 0 0,0 2 0,2 0 0,0 0 0)),((0 0 0,2 0 0,0 0 2,0 0 0)),((0 0 0,0 0 2,0 2 0,0 0 0)),((2 0 0,0 2 0,0 0 2,2 0 0))) | POLYHEDRALSURFACE Z (((0 2 0,4 0 0,2 0 0,0 2 0)),((0 0 2,2 0 0,0 0 4,0 0 2)),((0 0 2,0 4 0,0 2 0,0 0 2)),((4 0 0,0 4 0,0 0 4,4 0 0)),((4 0 0,0 2 0,0 4 0,4 0 0)),((2 0 0,4 0 0,0 0 4,2 0 0)),((0 4 0,0 0 2,0 0 4,0 4 0)),((0 0 2,0 2 0,2 0 0,0 0 2)))
Figure
Geometry figure for visual-cg-3ddifference-01