Main Content

subtract

Boolean subtraction of 3-D geometries

Since R2025a

    Description

    g3 = subtract(g1,g2) subtracts the 3-D geometry g2 from the 3-D geometry g1 and returns the combined geometry g3. If g2 is a vector of geometries, the function subtracts all 3-D geometries specified by g2 from the geometry g1.

    Boolean subtraction of a sphere from a cube resulting in a cube with a cutout piece

    example

    Examples

    collapse all

    Cut out a quarter of a sphere by subtracting a cube from the sphere.

    Create and plot a geometry of a sphere.

    gsphere = multisphere(5);
    pdegplot(gsphere)

    Figure contains an axes object. The axes object contains 5 objects of type quiver, text, patch.

    Create and plot a geometry of a cuboid.

    gcube = multicuboid(5,5,10);
    gcube = translate(gcube,[2.5 -2.5 -5]);
    figure
    pdegplot(gcube)

    Figure contains an axes object. The axes object contains 6 objects of type quiver, text, patch, line.

    Convert both geometries to fegeometry objects.

    gsphere = fegeometry(gsphere);
    gcube = fegeometry(gcube);

    Subtract the cuboid from the sphere by using the Boolean subtraction operation.

    g = subtract(gsphere,gcube);

    Plot the resulting geometry with cell labels.

    figure
    pdegplot(g,CellLabels="on",FaceAlpha=0.3)

    Figure contains an axes object. The axes object contains 6 objects of type quiver, text, patch, line.

    Subtract a cube from a larger sphere by using the Boolean subtraction operation.

    Create and plot a geometry of a sphere.

    gsphere = multisphere(5);
    pdegplot(gsphere)

    Figure contains an axes object. The axes object contains 5 objects of type quiver, text, patch.

    Create and plot a geometry of a cube.

    gcube = multicuboid(6,6,6,Zoffset=-3);
    figure
    pdegplot(gcube)

    Figure contains an axes object. The axes object contains 6 objects of type quiver, text, patch, line.

    Convert both geometries to fegeometry objects.

    gsphere = fegeometry(gsphere);
    gcube = fegeometry(gcube);

    Subtract the cube from the sphere by using the Boolean subtraction operation.

    g = subtract(gsphere,gcube);

    Plot the resulting geometry with cell labels.

    figure
    pdegplot(g,CellLabels="on",FaceAlpha=0.3)

    Figure contains an axes object. The axes object contains 6 objects of type quiver, text, patch, line.

    Since R2026a

    Cut out multiple cubes from a cylinder.

    Create geometries representing a cylinder and a unit cube.

    gmcyl = fegeometry(multicylinder(1,1));
    gmcube = fegeometry(multicuboid(1,1,1));

    Create a vector of geometries by rotating the cube by 30 and 60 degrees.

    angle = [0 30 60];
    for k = 1:numel(angle)
        gv(k) = rotate(gmcube,angle(k));
    end

    Subtract the resulting vector of geometries from the cylinder.

    gm = subtract(gmcyl,gv);

    Plot the result.

    pdegplot(gm)

    Figure contains an axes object. The axes object contains 6 objects of type quiver, text, patch, line.

    Input Arguments

    collapse all

    3-D geometry, specified as an fegeometry object.

    3-D geometries to subtract, specified as an fegeometry object or a vector of fegeometry objects.

    Output Arguments

    collapse all

    Combined geometry, returned as an fegeometry object.

    Limitations

    • subtract works only on 3-D geometries. Use decsg to subtract 2-D geometries.

    Tips

    • When creating a geometry, subtract uses the default threshold of 44 degrees for the dihedral angle between adjacent triangles. If the angle between the triangles exceeds the threshold, the edge becomes a topological (feature) edge separating two faces. If the angle does not exceed the threshold, subtract does not create a topological edge with two separate faces, unless the function can create the edge based on other criteria. Instead, subtract creates one face.

      To use a different feature angle value, create a triangulation object from the resulting geometry g3, and then convert the object back to an fegeometry object. Set the value of the FeatureAngle argument as a number between 10 and 90. The specified FeatureAngle value represents degrees.

      tr = triangulation(g3);
      g3 = fegeometry(tr,FeatureAngle=15)

      The triangulation function accepts only single-domain geometries, so g3 must have one cell.

    Version History

    Introduced in R2025a

    expand all

    See Also

    Functions

    Objects