# Booleans

Boolean operations are an advanced modeling operation only available in SMLib (not TSNLib). Booleans are accomplished with the SmMerge class which takes two objects as Input. Input objects can be manifold or non manifold. In other words, it is possible to boolean solids and surfaces. Some examples are listed below:

- Solid Union - Solid yields the solid plus the portion of the second solid outside of the first solid
- Solid Union - Surface yields the solid plus the portion of the surface outside of the solid.
- Solid Intersect - Surface yields the portion of the surface which is inside of the volume of the solid.
- Solid Difference - Surface yields the solid plus the portion of the surface inside of the solid. In some cases this may cut the solid into multiple regions.
- Surface Difference - Solid yields the portion of the surface not inside the solid.
- Surface Union - Surface yields the two surfaces plus attached by an intersection curve.
- Surface Intersect - Surface yields just the intersection curves.
- Surface Difference - Surface yields the first surface with the curves of intersection.

The primary operators are SmMerge::ManifoldBoolean or SmMerge::NonManifoldBoolean. A flag is used to specify the operation type union, difference, intersection, etc. The possible operation types are listed below.

SM_BO_UNION, // A Union B

SM_BO_INTERSECTION // A Intersection B

SM_BO_DIFFERENCE // A minus B

SM_BO_EXCLUSIVE_OR // ExclusiveOr = (A Union B) - (A intersect B)

SM_BO_MERGE // Merge B into A

SM_BO_PARTIAL_MERGE // Merge subset of B parts into A.

SM_BO_IMPRINT // A plus (A Intersect B)

SM_BO_EXTRACT_SEPARATE // Merge B into A placing the separate regions in separate breps.

SM_BO_SLICE // Slice a solid with a sheet and

The following code demonstrates how to set up and difference two Breps:

// base brep in difference

SmBrep *pBrep1 = ...;

// brep to subtract

SmBrep *pBrep2 = ...;

// Initialize merge object

SmMerge sMerge(crContext, pBrep1, pBrep2);

// Perform Boolean operation

SmBrep *pResult = NULL;

SER (sMerge.ManifoldBoolean(SM_BO_DIFFERENCE, pResult));

One great thing about having an underlying NMT representation is that the Boolean operation is now a closed operation. In other words you can not do a Boolean that would produce something you can not represent topologically. It is possible to union two solids which just touch along an edge or at a vertex whereas this is not a valid representation with traditional manifold operators.

### Merge

The Merge operation is like the Boolean in that it combines the topologies from two separate Breps. The major difference being that the Merge only adds topology it does not remove any topology. For example the Merge of a box and a cylinder will contain all of the original edges and faces of both the cylinder and box plus any intersection edges. The Merge of two solids will typically create edges which have four or more faces and may have several regions.

The Non-Manifold Topology enables us to represent the results of the Merge completely and explicitly with a single topological structure. We do not need to break things up into separate volumes. You may merge any combination of wireframe, surfaces, open shells, solids, and non-manifold topology models. To invoke the merge operation you do the same thing as show above in the Boolean except that the operation you send in is "SM_BO_MERGE " instead of "SM_BO_DIFFERENCE ".

SmMerge::StitchFace

This operations allows the user to start with a brep and add a new surface one by one merging, intersecting, and sewing after every addition.

SmMerge::merge_breps

This is a convenience function to perform repeated boolean operations on a list of breps.

SmBrep::MakeManifold

The MakeManifold operation produces a manifold solid from a non-manifold topology. It deletes excess faces, edges and vertices to get to either a manifold topology. The following shows how to utilize the operation.

// Make manifold by default uses all regions

// except the infinite region.

// Do not keep interior faces.

SER(pResult->MakeManifold(NULL, FALSE));

// Somehow we select which regions to keep in

// a cellular topology.

SmTArray<SmRegion*> sRegionsToKeep = ....;

// Remove all faces not bounding one of

// sRegionsToKeep. Note that any faces which

// between regions will also be kept.

SER (pResult->MakeManifold(sRegionsToKeep, TRUE));