MeshLib C++ Docs
Loading...
Searching...
No Matches
Surface Boolean overview

Chapter about Constructive Solid Geometry operations. More...

Classes

struct  MR::BooleanResultMapper
 Structure to map old mesh BitSets to new. More...
struct  MR::BooleanInternalParameters
 Parameters will be useful if specified. More...
struct  MR::NewEdgesMap
struct  MR::OneMeshContours
 Special data type for MR::cutMesh: the intersections of all contours in one array, one contour after another. More...
struct  MR::CutMeshParameters
 Parameters of MR::cutMesh. More...
struct  MR::CutMeshResult
struct  MR::BooleanResult
 Structure contain boolean result. More...

Enumerations

enum class  MR::BooleanOperation {
  MR::BooleanOperation::InsideA , MR::BooleanOperation::InsideB , MR::BooleanOperation::OutsideA , MR::BooleanOperation::OutsideB ,
  MR::BooleanOperation::Union , MR::BooleanOperation::Intersection , MR::BooleanOperation::DifferenceBA , MR::BooleanOperation::DifferenceAB ,
  MR::BooleanOperation::Count
}

Functions

Expected< Mesh > MR::doBooleanOperation (Mesh &&meshACut, Mesh &&meshBCut, std::vector< EdgePath > &&cutEdgesA, std::vector< EdgePath > &&cutEdgesB, BooleanOperation operation, const AffineXf3f *rigidB2A=nullptr, BooleanResultMapper *mapper=nullptr, bool mergeAllNonIntersectingComponents=false, const BooleanInternalParameters &intParams={})
CutMeshResult MR::cutMesh (Mesh &mesh, const OneMeshContours &contours, const CutMeshParameters &params={})
 Cuts mesh by given contours.
void MR::reserveForCut (Mesh &mesh, const Mesh &sizeOf, const OneMeshContours &contours)
Expected< OneMeshContours > MR::offsetSurfaceLineToMeshContours (const Mesh &mesh, const std::vector< MeshTriPoint > &surfaceLine, float offset, SearchPathSettings searchSettings={})
 Makes continuous contour by iso-line from mesh tri points, if first and last meshTriPoint is the same, makes closed contour.
Expected< OneMeshContours > MR::offsetSurfaceLineToMeshContours (const Mesh &mesh, const std::vector< MeshTriPoint > &surfaceLine, const std::function< float(int)> &offsetAtPoint, SearchPathSettings searchSettings={})
 Makes continuous contour by iso-line from mesh tri points, if first and last meshTriPoint is the same, makes closed contour.
Expected< OneMeshContour > MR::surfaceLineToMeshContour (const Mesh &mesh, const std::vector< MeshTriPoint > &surfaceLine, MeshTriPointsConnector connectorFn, std::vector< int > *pivotIndices=nullptr)
 Makes continuous contour by mesh tri points, if first and last meshTriPoint is the same, makes closed contour.
Expected< OneMeshContour > MR::surfaceLineToMeshContour (const Mesh &mesh, const std::vector< MeshTriPoint > &surfaceLine, SearchPathSettings searchSettings={}, std::vector< int > *pivotIndices=nullptr)
 Makes continuous contour by mesh tri points, if first and last meshTriPoint is the same, makes closed contour.
Expected< OneMeshContour > MR::surfaceLineToClosedMeshContour (const Mesh &mesh, const std::vector< MeshTriPoint > &surfaceLine, SearchPathSettings searchSettings={}, std::vector< int > *pivotIndices=nullptr)
 Makes closed continuous contour by mesh tri points, note that first and last meshTriPoint should not be same.
OneMeshContour MR::surfacePathWithEndsToMeshContour (const Mesh &mesh, const MeshTriPoint &start, const SurfacePath &surfacePath, const MeshTriPoint &end)
 Converts SurfacePath to OneMeshContours.
OneMeshContours MR::surfacePathsToMeshContours (const Mesh &mesh, const std::vector< SurfacePath > &surfacePaths)
 Converts SurfacePaths to OneMeshContours.
BooleanResult MR::boolean (const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const AffineXf3f *rigidB2A, BooleanResultMapper *mapper=nullptr, ProgressCallback cb={})
 Performs CSG operation on two meshes.
Expected< BooleanResultPoints, std::string > MR::getBooleanPoints (const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const AffineXf3f *rigidB2A=nullptr)
 Returns the points of mesh boolean's result mesh.
BooleanResult MR::boolean (const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const BooleanParameters &params={})
BooleanResult MR::boolean (Mesh &&meshA, Mesh &&meshB, BooleanOperation operation, const BooleanParameters &params={})
BooleanResult MR::boolean (const Mesh &meshA, Mesh &&meshB, BooleanOperation operation, const BooleanParameters &params={})
 keeps meshA as the first overload does (its whole tree is cached in it) and consumes meshB as the second one does
BooleanResult MR::boolean (Mesh &&meshA, const Mesh &meshB, BooleanOperation operation, const BooleanParameters &params={})
BooleanResult MR::forceBoolean (const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const BooleanParameters &params={})
Expected< Mesh > MR::selfBoolean (const Mesh &mesh, FaceMap *new2Old=nullptr)
Contours3f MR::findIntersectionContours (const Mesh &meshA, const Mesh &meshB, const AffineXf3f *rigidB2A=nullptr)
 returns intersection contours of given meshes
void MR::convertIntFloatAllVerts (Mesh &mesh, const CoordinateConverters &conv)

Detailed Description

Chapter about Constructive Solid Geometry operations.

This chapter represents documentation of MeshLib CSG

Enumeration Type Documentation

◆ BooleanOperation

enum class MR::BooleanOperation
strong

#include <MRMesh/MRBooleanOperation.h>

Enum class of available CSG operations

Two separate meshes
See also
MR::boolean
Enumerator
InsideA 

Part of mesh A that is inside of mesh B

Inside A
InsideB 

Part of mesh B that is inside of mesh A

Inside B
OutsideA 

Part of mesh A that is outside of mesh B

Outside A
OutsideB 

Part of mesh B that is outside of mesh A

Outside B
Union 

Union surface of two meshes (outside parts)

Union
Intersection 

Intersection surface of two meshes (inside parts)

Intersection
DifferenceBA 

Surface of mesh B - surface of mesh A (outside B - inside A)

Difference B-A
DifferenceAB 

Surface of mesh A - surface of mesh B (outside A - inside B)

Difference A-B
Count 

not a valid operation

Function Documentation

◆ boolean() [1/5]

BooleanResult MR::boolean ( const Mesh & meshA,
const Mesh & meshB,
BooleanOperation operation,
const AffineXf3f * rigidB2A,
BooleanResultMapper * mapper = nullptr,
ProgressCallback cb = {} )

#include <MRMesh/MRMeshBoolean.h>

Performs CSG operation on two meshes.

Makes new mesh - result of boolean operation on mesh A and mesh B

// create first sphere with radius of 1 unit
MR::Mesh sphere1 = MR::makeUVSphere( 1.0f, 64, 64 );
// create second sphere by cloning the first sphere and moving it in X direction
MR::Mesh sphere2 = sphere1;
MR::AffineXf3f xf = MR::AffineXf3f::translation( MR::Vector3f( 0.7f, 0.0f, 0.0f ) );
sphere2.transform( xf );
// optional mapper relating the primitives of the input meshes to the primitives of the result
// perform boolean operation
MR::BooleanResult result = MR::boolean( sphere1, sphere2, MR::BooleanOperation::Intersection, { .mapper = &mapper } );
if ( !result.valid() )
{
std::cerr << result.errorString << std::endl;
return 1;
}
MR::Mesh resultMesh = *result;
Parameters
meshAInput mesh A
meshBInput mesh B
operationCSG operation to perform
rigidB2ATransform from mesh B space to mesh A space
mapperOptional output structure to map mesh A and mesh B topology to result mesh topology
Note
Input meshes should have no self-intersections in intersecting zone
If meshes are not closed in intersecting zone some boolean operations are not allowed (as far as input meshes interior and exterior cannot be determined)

◆ boolean() [2/5]

BooleanResult MR::boolean ( const Mesh & meshA,
const Mesh & meshB,
BooleanOperation operation,
const BooleanParameters & params = {} )

#include <MRMesh/MRMeshBoolean.h>

constructs the AABB trees of the whole meshes if they are not cached yet and caches them in the meshes, so that the next calls of Boolean with the same meshes are faster

◆ boolean() [3/5]

BooleanResult MR::boolean ( const Mesh & meshA,
Mesh && meshB,
BooleanOperation operation,
const BooleanParameters & params = {} )

#include <MRMesh/MRMeshBoolean.h>

keeps meshA as the first overload does (its whole tree is cached in it) and consumes meshB as the second one does

◆ boolean() [4/5]

BooleanResult MR::boolean ( Mesh && meshA,
const Mesh & meshB,
BooleanOperation operation,
const BooleanParameters & params = {} )

#include <MRMesh/MRMeshBoolean.h>

consumes meshA as the second overload does and keeps meshB as the first one does (its whole tree is cached in it), e.g. for many subtractions of the same small mesh in different positions from a large mesh

◆ boolean() [5/5]

BooleanResult MR::boolean ( Mesh && meshA,
Mesh && meshB,
BooleanOperation operation,
const BooleanParameters & params = {} )

#include <MRMesh/MRMeshBoolean.h>

consumes the meshes; if a mesh has no cached AABB tree and only a small part of it is near the other mesh, then only a temporary tree of that part is constructed, which is much faster

◆ convertIntFloatAllVerts()

void MR::convertIntFloatAllVerts ( Mesh & mesh,
const CoordinateConverters & conv )

#include <MRMesh/MRMeshBoolean.h>

converts all vertices of the mesh first in integer-coordinates, and then back in float-coordinates; this is necessary to avoid small self-intersections near newly introduced vertices on cut-contours; this actually sligntly moves only small perentage of vertices near x=0 or y=0 or z=0 planes, where float-precision is higher than int-precision; newly introduced vertices on cut-contours are also converted, but we expected that they remain unchanged due to idempotent property of the conversion

◆ cutMesh()

CutMeshResult MR::cutMesh ( Mesh & mesh,
const OneMeshContours & contours,
const CutMeshParameters & params = {} )

#include <MRMesh/MRContoursCut.h>

Cuts mesh by given contours.

This function cuts mesh making new edges paths on place of input contours; a contour inside one face is connected with the rest of the face by new edges

Parameters
meshInput mesh that will be cut
contoursInput contours to cut mesh with, find more MR::OneMeshContours
paramsParameters describing some cut options, find more MR::CutMeshParameters
Returns
New edges that correspond to given contours, find more MR::CutMeshResult
Warning
Input contours should have no intersections, faces where contours intersects (bad faces) will not be allowed for fill, unless a force fill mode is set (see CutMeshParameters::ForceFill)
Warning
Every two consecutive intersections of a contour should lie in one face, and a contour should not fold back at a point inside an edge: a mesh vertex next to such a point is the third vertex of the face the contour goes in there
Warning
Input mesh will be changed in any case, if bad faces are in mesh, mesh will be spoiled,
so if you cannot guarantee contours without intersections better make copy of mesh, before using this function

◆ doBooleanOperation()

Expected< Mesh > MR::doBooleanOperation ( Mesh && meshACut,
Mesh && meshBCut,
std::vector< EdgePath > && cutEdgesA,
std::vector< EdgePath > && cutEdgesB,
BooleanOperation operation,
const AffineXf3f * rigidB2A = nullptr,
BooleanResultMapper * mapper = nullptr,
bool mergeAllNonIntersectingComponents = false,
const BooleanInternalParameters & intParams = {} )

#include <MRMesh/MRBooleanOperation.h>

Perform boolean operation on cut meshes

Returns
mesh in space of meshA or error.
Note
: actually this function is meant to be internal, use "boolean" instead

◆ findIntersectionContours()

Contours3f MR::findIntersectionContours ( const Mesh & meshA,
const Mesh & meshB,
const AffineXf3f * rigidB2A = nullptr )

#include <MRMesh/MRMeshBoolean.h>

returns intersection contours of given meshes

◆ forceBoolean()

BooleanResult MR::forceBoolean ( const Mesh & meshA,
const Mesh & meshB,
BooleanOperation operation,
const BooleanParameters & params = {} )

#include <MRMesh/MRMeshBoolean.h>

calls boolean in force mode, also unites close vertices and fills holes afterwards

Note
expects closed mesh on input, and fills all holes
Warning
params.mapper might be not correctly updated when new holes are filled

◆ getBooleanPoints()

Expected< BooleanResultPoints, std::string > MR::getBooleanPoints ( const Mesh & meshA,
const Mesh & meshB,
BooleanOperation operation,
const AffineXf3f * rigidB2A = nullptr )

#include <MRMesh/MRMeshBoolean.h>

Returns the points of mesh boolean's result mesh.

Returns vertices and intersection points of mesh that is result of boolean operation of mesh A and mesh B. Can be used as fast alternative for cases where the mesh topology can be ignored (bounding box, convex hull, etc.)

Parameters
meshAInput mesh A
meshBInput mesh B
operationBoolean operation to perform
rigidB2ATransform from mesh B space to mesh A space

◆ offsetSurfaceLineToMeshContours() [1/2]

Expected< OneMeshContours > MR::offsetSurfaceLineToMeshContours ( const Mesh & mesh,
const std::vector< MeshTriPoint > & surfaceLine,
const std::function< float(int)> & offsetAtPoint,
SearchPathSettings searchSettings = {} )
nodiscard

#include <MRMesh/MRContoursCut.h>

Makes continuous contour by iso-line from mesh tri points, if first and last meshTriPoint is the same, makes closed contour.

Finds shortest paths between neighbor surfaceLine and build offset contour on surface for MR::cutMesh input. An isoline that reaches the mesh boundary is an open contour (closed = false) from a boundary edge to a boundary edge; the other isolines are closed, with the last intersection equal to the first one

Parameters
offsetAtPointfunctor that returns amount of offset form arg point, note that absolute value is used and isoline in both direction returned
searchSettingssettings for search geodesic path
See also
offsetSurfaceLine to get the same contours as plain points

◆ offsetSurfaceLineToMeshContours() [2/2]

Expected< OneMeshContours > MR::offsetSurfaceLineToMeshContours ( const Mesh & mesh,
const std::vector< MeshTriPoint > & surfaceLine,
float offset,
SearchPathSettings searchSettings = {} )
nodiscard

#include <MRMesh/MRContoursCut.h>

Makes continuous contour by iso-line from mesh tri points, if first and last meshTriPoint is the same, makes closed contour.

Finds shortest paths between neighbor surfaceLine and build offset contour on surface for MR::cutMesh input. An isoline that reaches the mesh boundary is an open contour (closed = false) from a boundary edge to a boundary edge; the other isolines are closed, with the last intersection equal to the first one

Parameters
offsetamount of offset form given point, note that absolute value is used and isoline in both direction returned
searchSettingssettings for search geodesic path
See also
offsetSurfaceLine to get the same contours as plain points

◆ reserveForCut()

void MR::reserveForCut ( Mesh & mesh,
const Mesh & sizeOf,
const OneMeshContours & contours )

#include <MRMesh/MRContoursCut.h>

reserves the capacity in mesh for a mesh with the sizes of sizeOf (e.g. mesh itself, or the mesh to be copied in it) together with the elements that cutMesh by contours adds, so the cut does not reallocate the mesh

◆ selfBoolean()

Expected< Mesh > MR::selfBoolean ( const Mesh & mesh,
FaceMap * new2Old = nullptr )

#include <MRMesh/MRMeshBoolean.h>

performs boolean operation on mesh with itself, cutting its self-intersection contours and flipping their connectivity; the contours may cross one another where three sheets of the mesh meet at a point (nested intersections); two triangles with the same vertices glued along two edges or all three (a part without volume) that cross other parts are eliminated, see eliminateDoubleTris this function is experimental and likely to change signature and/or behavior in future

Parameters
new2Oldoptional output, set on success: for every valid face of the result, the face of the input mesh that it is or is a part of; the faces with new ids (not less than the face size of the input mesh) are the parts of the cut input faces

◆ surfaceLineToClosedMeshContour()

Expected< OneMeshContour > MR::surfaceLineToClosedMeshContour ( const Mesh & mesh,
const std::vector< MeshTriPoint > & surfaceLine,
SearchPathSettings searchSettings = {},
std::vector< int > * pivotIndices = nullptr )
nodiscard

#include <MRMesh/MROneMeshContours.h>

Makes closed continuous contour by mesh tri points, note that first and last meshTriPoint should not be same.

Finds shortest paths between neighbor surfaceLine and build closed contour MR::cutMesh input

Parameters
pivotIndicesoptional output indices of given surfaceLine in result OneMeshContour
Note
better use surfaceLineToMeshContour(...) instead, note that it requires same front and back MeshTriPoints for closed contour

◆ surfaceLineToMeshContour() [1/2]

Expected< OneMeshContour > MR::surfaceLineToMeshContour ( const Mesh & mesh,
const std::vector< MeshTriPoint > & surfaceLine,
MeshTriPointsConnector connectorFn,
std::vector< int > * pivotIndices = nullptr )
nodiscard

#include <MRMesh/MROneMeshContours.h>

Makes continuous contour by mesh tri points, if first and last meshTriPoint is the same, makes closed contour.

Finds paths between neighbor surfaceLine with MeshTriPointsConnector function and build contour MR::cutMesh input

Parameters
connectorFnfunction to build path between neighbor surfaceLine, if not present simple geodesic path function is used
pivotIndicesoptional output indices of given surfaceLine in result OneMeshContour

◆ surfaceLineToMeshContour() [2/2]

Expected< OneMeshContour > MR::surfaceLineToMeshContour ( const Mesh & mesh,
const std::vector< MeshTriPoint > & surfaceLine,
SearchPathSettings searchSettings = {},
std::vector< int > * pivotIndices = nullptr )
nodiscard

#include <MRMesh/MROneMeshContours.h>

Makes continuous contour by mesh tri points, if first and last meshTriPoint is the same, makes closed contour.

Finds shortest paths between neighbor surfaceLine and build contour MR::cutMesh input

Parameters
searchSettingssettings for search geo path
pivotIndicesoptional output indices of given surfaceLine in result OneMeshContour

◆ surfacePathsToMeshContours()

OneMeshContours MR::surfacePathsToMeshContours ( const Mesh & mesh,
const std::vector< SurfacePath > & surfacePaths )
nodiscard

#include <MRMesh/MROneMeshContours.h>

Converts SurfacePaths to OneMeshContours.

Creates MR::OneMeshContours object from given surface paths for MR::cutMesh input

◆ surfacePathWithEndsToMeshContour()

OneMeshContour MR::surfacePathWithEndsToMeshContour ( const Mesh & mesh,
const MeshTriPoint & start,
const SurfacePath & surfacePath,
const MeshTriPoint & end )
nodiscard

#include <MRMesh/MROneMeshContours.h>

Converts SurfacePath to OneMeshContours.

Creates MR::OneMeshContour object from given surface path with ends for MR::cutMesh input start and surfacePath.front() should be from same face surfacePath.back() and end should be from same face

note that whole path (including start and end) should not have self-intersections also following case is not supported (vertex -> edge (incident with vertex)):

vert path edge point path edge end o-------—o- – – – – O \ \ / \ \ / \ \ / \/ o path