MeshLib Documentation
Loading...
Searching...
No Matches
Mesh Boolean

Example of boolean operation

  • C++
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    // 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 );
    // perform boolean operation
    MR::Mesh resultMesh = *result;
    if ( !result.valid() )
    std::cerr << result.errorString << std::endl;
    // save result to STL file
    MR::MeshSave::toAnySupportedFormat( resultMesh, "out_boolean.stl" );
    return 0;
    }
    MRMESH_API BooleanResult boolean(const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const AffineXf3f *rigidB2A, BooleanResultMapper *mapper=nullptr, ProgressCallback cb={})
    Performs CSG operation on two meshes.
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    detects the format from file extension and save mesh to it
    MRMESH_API Mesh makeUVSphere(float radius=1.0, int horisontalResolution=16, int verticalResolution=16)
    creates a mesh of sphere with regular triangulation (parallels and meridians)
    Structure contain boolean result.
    bool valid() const
    Returns true if boolean succeed, false otherwise.
    Definition MRMesh/MRMeshBoolean.h:37
    std::string errorString
    Holds error message, empty if boolean succeed.
    Definition MRMesh/MRMeshBoolean.h:35
    Definition MRMesh/MRMesh.h:23
    MRMESH_API void transform(const AffineXf3f &xf, const VertBitSet *region=nullptr)
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    # create first sphere with radius of 1 unit
    sphere1 = mrmeshpy.makeUVSphere(1.0, 64, 64)
    # create second sphere by cloning the first sphere and moving it in X direction
    sphere2 = mrmeshpy.copyMesh(sphere1)
    xf = mrmeshpy.AffineXf3f.translation(mrmeshpy.Vector3f(0.7, 0.0, 0.0))
    sphere2.transform(xf)
    # perform boolean operation
    result = mrmeshpy.boolean(sphere1, sphere2, mrmeshpy.BooleanOperation.Intersection)
    result_mesh = result.mesh
    if not result.valid():
    print(result.errorString)
    # save result to STL file
    mrmeshpy.saveMesh(result_mesh, "out_boolean.stl")
  • C
    #include <MRMeshC/MRMesh.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    // print progress every 10%
    int gProgress = -1;
    bool onProgress( float v )
    {
    int progress = (int)( 10.f * v );
    if ( progress != gProgress )
    {
    gProgress = progress;
    printf( "%d%%...\n", progress * 10 );
    }
    return true;
    }
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    if ( argc != 5 )
    {
    fprintf( stderr, "Usage: %s { unite | intersect } INPUT1 INPUT2 OUTPUT", argv[0] );
    goto out;
    }
    if ( strcmp( argv[1], "unite" ) == 0 )
    else if ( strcmp( argv[1], "intersect" ) == 0 )
    else
    {
    fprintf( stderr, "Incorrect operation: %s", argv[1] );
    goto out;
    }
    const char* input1 = argv[2];
    const char* input2 = argv[3];
    const char* output = argv[4];
    // error messages will be stored here
    MRString* errorString = NULL;
    MRMesh* mesh1 = mrMeshLoadFromAnySupportedFormat( input1, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh 1: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    MRMesh* mesh2 = mrMeshLoadFromAnySupportedFormat( input2, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh 2: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out_mesh1;
    }
    // you can set some parameters for boolean, e.g. progress callback
    params.cb = onProgress;
    // perform the boolean operation
    MRBooleanResult result = mrBoolean( mesh1, mesh2, op, &params );
    if ( result.errorString )
    {
    fprintf( stderr, "Failed to perform boolean: %s", mrStringData( result.errorString ) );
    mrStringFree( errorString );
    goto out_mesh2;
    }
    mrMeshSaveToAnySupportedFormat( result.mesh, output, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to save result: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out_result;
    }
    rc = EXIT_SUCCESS;
    out_result:
    mrMeshFree( result.mesh );
    out_mesh2:
    mrMeshFree( mesh2 );
    out_mesh1:
    mrMeshFree( mesh1 );
    out:
    return rc;
    }
    MRBooleanOperation
    Available CSG operations.
    Definition MRMeshC/MRBooleanOperation.h:10
    @ MRBooleanOperationUnion
    Union surface of two meshes (outside parts)
    Definition MRMeshC/MRBooleanOperation.h:20
    @ MRBooleanOperationIntersection
    Intersection surface of two meshes (inside parts)
    Definition MRMeshC/MRBooleanOperation.h:22
    MRMESHC_API MRBooleanParameters mrBooleanParametersNew(void)
    initializes a default instance
    MRMESHC_API MRBooleanResult mrBoolean(const MRMesh *meshA, const MRMesh *meshB, MRBooleanOperation operation, const MRBooleanParameters *params)
    struct MRMesh MRMesh
    Definition MRMeshC/MRMeshFwd.h:43
    typedefMR_EXTERN_C_BEGIN struct MRString MRString
    Definition MRMeshC/MRMeshFwd.h:32
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    deallocates a Mesh object
    MRMESHC_API void mrStringFree(MRString *str)
    deallocates the string object
    MR_EXTERN_C_BEGIN MRMESHC_API const char * mrStringData(const MRString *str)
    gets read-only access to the string data
    MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, MRString **errorStr)
    optional parameters for mrBoolean
    Definition MRMeshC/MRMeshBoolean.h:11
    MRProgressCallback cb
    Progress callback.
    Definition MRMeshC/MRMeshBoolean.h:23
    This structure store result mesh of mrBoolean or some error info.
    Definition MRMeshC/MRMeshBoolean.h:31
    MRString * errorString
    Holds error message, empty if boolean succeed.
    Definition MRMeshC/MRMeshBoolean.h:37
    MRMesh * mesh
    Result mesh of boolean operation, if error occurred it would be empty.
    Definition MRMeshC/MRMeshBoolean.h:33
  • C#
    using MR.DotNet;
    using System;
    using System.Reflection;
    class Program
    {
    static void Main(string[] args)
    {
    if (args.Length != 4)
    Console.WriteLine( "Usage: {0} {{unite | intersect}} INPUT1 INPUT2 OUTPUT", Assembly.GetExecutingAssembly().GetName().Name );
    BooleanOperation op;
    switch (args[0])
    {
    case "unite":
    op = BooleanOperation.Union;
    break;
    case "intersect":
    op = BooleanOperation.Intersection;
    break;
    default:
    Console.WriteLine( "Unknown operation: {0}", args[0] );
    return;
    }
    try
    {
    Mesh meshA = Mesh.FromAnySupportedFormat(args[1]);
    Mesh meshB = Mesh.FromAnySupportedFormat(args[2]);
    var res = MeshBoolean.Boolean(meshA, meshB, op);
    Mesh.ToAnySupportedFormat(res.mesh, args[3]);
    }
    catch (Exception e)
    {
    Console.WriteLine( "Error: {0}", e.Message );
    }
    }
    }
    Definition MeshBoolean.dox.py:1