Tutorial: Concatenation (Merging) and Boolean Operations (Fusion) for Solids
Prof. Dr. Tim C. Lueth, Professor at Technical University of Munich, MIMED - Department of Mechanical Engineering, Munich July/August 2025
Basic information about expectations for Boolean operations
Before we talk about Boolean operations, a few basic words about the design methodology of SGLib. SG-Lib works with surface models with a resolution of up to approx. 50 µm. SG-Lib is designed for generating and writing STL data for 3D printing. SG-Lib also allows STL data to be imported, processed, and rewritten. SG-Lib does not claim to have the same properties as a volume modeling program. These work differently and were originally designed for creating technical drawings and using standard parts. SG-Lib, on the other hand, can generate threads, gear teeth, and mechanisms that are printed already assembled. The aim of SG-Lib is to be able to transform the thoughts and ideas of users into physical objects as quickly as possible. If a file can be converted to gcode for a printer using a slicer, it is sufficient to create the necessary interfaces. To perform an FEM analysis with SG-Lib, the models must be "watertight," but this is not necessary for 3D printing.
Difference between concatenation (merging) and Boolean operations (fusion)
We use the two bodies from the spatial relations again.
A=SGofCPLz(PLsquare([30,20]),10); A=SGcolorfaces(A,'g'); B=SGofCPLz(PLstar(13),9); B=SGcolorfaces(B,'r');
SGfigure(-30,30); SGplotalpha(A,'',0.5);
SGplotalpha(B,'',0.5,'',A,'right',10); % Wir sehen, dass auch die Plotroutinen relative räumliche Relationen zu anderen Solids unterstützen, ohne die Position der Solids zu ändern
Merging of two bodies with "SGconcat"
First, we show how to use the concatenation (merging) function "SGconcat" "SGconcat" does nothing more than append the vertex list of the second body to that of the first body and add the number of vertices of the first body to the facet list so that the indices of the facet list of the second body are correctly indexed. VL=[VL1;VL2]; FL=[FL1;FL2+size(VL1,1)]; "SGconcat" does much more than that, of course, such as the relations and transformations of the frames, colors, etc., but essentially it is just a merging of vertex lists as in "SGcat2."C=SGconcat(A,B,'ontop',-1) % could also be C=SGtransrelSG(B,A,'ontop',-1,'cat');
SGfigure(-30,30); SGplotalpha(C,'w',0.5);
In the transparent view, we can clearly see that both bodies are present, but are now referred to collectively as "C." An analysis of independent individual surfaces is also possible with "SGsurfaces" and shows that "C" can easily be split back into two bodies.SGsurfaces(C)
It is also worth keeping an eye on the number of vertices (82) and facets (156).%%
[size(C.VL,1) size(C.FL,1)]
Fusion of two bodies with "SGunion"
Unlike "SGconcat," "SGunion" attempts a Boolean merge of the two geometries into a single body that can no longer be decomposed. "Attempts" means that SGLib does not always succeed in performing Boolean operations and occasionally simply performs a merging/concat instead of the Boolean operation, accompanied by a warning.
When bodies are merged, more points and more facets are almost inevitably created than when merging. Problems arise numerically when two bodies have surfaces in the same plane, points and edges that lie on the same straight line, and this affects more situations than is apparent at first glance The problems also affect SGsubtract, SGintersect, and other Boolean operations. Each Boolean operation creates new additional facets because existing facets are cut and broken down into smaller facets. This makes the facets smaller and smaller. Numerically, problems arise in the triangulation calculations when there are more than 6 orders of magnitude between the edge lengths of the facet triangles. So far, there is no correction for the geometries
D=SGunion(A,B,'ontop',-1) % could also be D=SGtransrelSG(B,A,'ontop',-1,'add');
D =
VL: [110×3 double]
FL: [216×3 double]
FC: [216×3 double]
Tname: {}
T: {}
TFiL: {}
SGfigure(-30,30); SGplotalpha(D,'w',0.5);
An analysis of the outer contours shows that a slicer will not notice the difference.
SGfigure(0,90);CPLplotcwccw(cpl);
There are now 110 vertices and 216 facets.
[size(D.VL,1) size(D.FL,1)]
It is now a new body that can no longer be dismantled.
SGsurfaces(D)
ans =
VL: [110×3 double]
FL: [216×3 double]
SGfigure(0,90);CPLplotcwccw(cpl);
This body is waterproof, meaning that there are no overlapping or missing facets anywhere. You can check this using "SGcheckmeshlab."
SGcheckmeshlab(D)
SGcheckmeshlab
OPEN BOUNDARY:
0 open boundary groups
0 open boundary edges
NON MANIFOLD EGDES:
0 non manifold edges
0 faces over non manifold edges
NON MANIFOLD VERTICES:
1 independent surfaces
0 non manifold vertices
0 faces over non manifold vertices
SELF INTERSECTING FACETS:
0 self intersecting facets
0 additional intersecting facets
Finally, it should be mentioned again that there are no clear advantages of fusion over merging. They are two different processes with advantages and disadvantages for the "addition."
Subtraction of two bodies with "SGsubtract" A without B
D=SGsubtract(A,B,'ontop',-1); % B is placed on top of A before subtraction.
FLrepair: (SGremsurfedgepoints) No open boundaries finally exist!
FLrepair: (SGsubtract) No open boundaries finally exist!
SGfigure(-30,30); SGplotalpha(D,'w',0.9);
Subtraction of two bodies with "SGsubtract" B without A
It is noticeable here that the elegant application of spatial relations no longer works as well as with SGunion and SGsubtract, where swapping the operands would also change the relation. The subtraction notation is therefore unfortunate. A parameter "2nd\1st" or "1st\2nd" (default) would be better.
D=SGsubtract(B,A,'ontop',-1); % ATTENTION - This time, A is placed on B, even though we only wanted the remaining geometry as in A without B.
SGfigure(-30,30); SGplotalpha(D,'w',0.9);
Subtraction of two bodies with "SGsubtractremain" B without A
"SGsubtractremain" allows the exchange of A and B without having to change the spatial relations. This function is needed just as often as SGsubtract. (See also "2nd\1st" or "1st\2nd" under "SGtranrelSG"
D=SGsubtract(A,B,'2nd\1st','ontop',-1);
SGfigure(-30,30); SGplotalpha(D,'w',0.9); view(-30,11)
Intersection of two bodies with "SGintersect"
D=SGintersect(A,B,'ontop',-1);
SGfigure(-30,30); SGplotalpha(D,'w',0.9);
XOR of two bodies with "SGintersect"
The SGxor function generally creates bodies that cannot be easily processed further, as bodies with parallel edges, faces, and points are created by design. "SGxor" does not occur in practice.
D=SGxor(A,B,'ontop',-1);
Warning: SGxor should not be used since it creates solids that cannot be sliced!
FLrepair: (SGremsurfedgepoints) No open boundaries finally exist!
SGfigure(-30,30); SGplotalpha(D,'w',0.9);
stamp
stamp: Mac OSX 15.6 | R2024b Update 6 | SG-Lib 5.4 | Java 1.8.0_202-b08 | i7-Intelx64 6-Core 64GByte RAM | 05-Aug-2025 15:28:22