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

Example of boolean operation

  • C++
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshBoolean.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRMesh/MRUVSphere.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::BooleanResult result = MR::boolean( sphere1, sphere2, MR::BooleanOperation::Intersection );
    if ( !result.valid() )
    std::cerr << result.errorString << std::endl;
    MR::Mesh resultMesh = *result;
    // save result to STL file
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( resultMesh, "out_boolean.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Source meshes
    Boolean intersection
  • 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)
    if not result.valid():
    print(result.errorString)
    else:
    # save result to STL file
    mrmeshpy.saveMesh(result.mesh, "out_boolean.stl")
    Source meshes
    Boolean intersection
  • C
    #include <MRCMesh/MRAffineXf.h>
    #include <MRCMesh/MRMakeSphereMesh.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshBoolean.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRVector3.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // First, create a unit sphere.
    float radius = 1.f; // Set radius for the test
    int horizontalResolution = 64; // Increase horizontal resolution
    int verticalResolution = 64; // Increase vertical resolution
    MR_Mesh* sphere1 = MR_makeUVSphere( &radius, &horizontalResolution, &verticalResolution );
    // Create a copy of this sphere and offset it.
    MR_Mesh* sphere2 = MR_Mesh_ConstructFromAnother( MR_PassBy_Copy, sphere1 );
    MR_Vector3f xfTranslation = {.x = 0.7f};
    MR_AffineXf3f xf = MR_AffineXf3f_translation( &xfTranslation );
    MR_Mesh_transform( sphere2, &xf, NULL );
    // Perform the boolean operation.
    MR_BooleanResult* result = MR_boolean_4_const_MR_Mesh_ref( sphere1, sphere2, MR_BooleanOperation_Intersection, NULL );
    if ( !MR_BooleanResult_valid( result ) )
    {
    fprintf( stderr, "Failed to perform boolean: %s\n", MR_std_string_data( MR_BooleanResult_Get_errorString( result ) ) );
    goto fail;
    }
    // Save result to an STL file.
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat_3( MR_BooleanResult_Get_mesh( result ), "out_boolean.stl", NULL, NULL);
    if ( MR_expected_void_std_string_error( saveEx ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx ) ) );
    goto fail;
    }
    rc = EXIT_SUCCESS;
    fail:
    MR_BooleanResult_Destroy( result );
    MR_Mesh_Destroy( sphere2 );
    MR_Mesh_Destroy( sphere1 );
    return rc;
    }
    Source meshes
    Boolean intersection
  • C#
    using System.Reflection;
    public class MeshBooleanExample
    {
    public static void Run(string[] args)
    {
    if (args.Length != 3)
    {
    Console.WriteLine("Usage: {0} MeshBooleanExample INPUT1 INPUT2", Assembly.GetExecutingAssembly().GetName().Name);
    return;
    }
    try
    {
    // load mesh
    var mesh_a = MR.MeshLoad.fromAnySupportedFormat(args[1]);
    var mesh_b = MR.MeshLoad.fromAnySupportedFormat(args[2]);
    // perform boolean operation
    MR.BooleanResult res = MR.boolean(mesh_a, mesh_b, MR.BooleanOperation.Intersection);
    // save result to STL file
    MR.MeshSave.toAnySupportedFormat(res.mesh, "out_boolean.stl");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }
    Source meshes
    Boolean intersection