Skip to content

SDF boolean operations

By evaluating the sign of the SDF, it is possible to classify the set of points as being within or outside the boundary of the domain. This enables set operations such as union, difference, and intersection to be performed.

In the signed distance package, these operations are implemented using Matlab's arithmetic operators between two or more instances of the Sdf class, including + (union), - (difference), / (intersection), * (scaling), and .* (repeating). By utilizing these set operations and a library of basic SDF primitives, it is possible to construct a wide range of complex geometries with relative ease. Subsequently, the SDFs can be transformed into a .stl file using the Marching Cube algorithm by Lorensen1, enabling 3D printing. This functionality is implemented in the command sdf.export.

Example: 2D Bellow shape Sdf

Image title

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
R1 = sRectangle([5,5],[20,15]);
R2 = sRectangle([0,0],[5,20]);
C1 = sCircle(5,[20,10]);

% adding shapes via union
s = R1 + C1;

% smoothly adding straight part
S2 = s.smoothunion(R2,10);

% show
S2.showContour('Quality',200);

Example: Revolve and repeat Sdf shapes

Image title

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
% repeat 2D Sdf twice with spacing [0,20];
S2 = S2.repeat([0,20], 2);

% revolve 2D Sdf into 3D Sdf
S2 = S2.revolve();

% generate a shell of thickness T=1.5 mm
S3 = S3.shell(1.5);

% render and export Sdf shape
S3 = S3.render('Quality',50);
S3.export('Double_Bellow.stl');


  1. William E. Lorensen and Harvey E. Cline. Marching cubes: A high resolution 3D surface construction algorithm. In ACM SIGGRAPH Computer Graphics, volume 21, pages 163–169. Association for Computing Machinery, New York, NY, USA, 1987. doi:10.1145/37401.37422