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

Measures the speed of boolean operations: subtracts from a sphere the same sphere rotated around Z-axis by an angle increasing by 0.1 degrees each time, and prints the elapsed time and the number of Booleans per second. The rotation is passed as rigidB2A, so neither mesh is modified and their AABB trees are built only once.

  • C++
    #include <MRMesh/MRMesh.h>
    #include <chrono>
    #include <cstdlib>
    #include <iomanip>
    #include <iostream>
    int main( int argc, char* argv[] )
    {
    // the number of Booleans can be given in the command line
    const int numBooleans = argc > 1 ? std::atoi( argv[1] ) : 2000;
    // two identical spheres of radius 1
    MR::Mesh sphereA = MR::makeSphere( { .radius = 1.0f, .numMeshVertices = 3366 } );
    MR::Mesh sphereB = sphereA;
    // subtracts sphereB rotated around Z-axis by i * 0.1 degrees from sphereA;
    // sphereB itself is not modified, so the AABB trees of both meshes are built once and reused
    auto subtractRotated = [&] ( int i )
    {
    const auto rigidB2A = MR::AffineXf3f::linear( MR::Matrix3f::rotation( MR::Vector3f::plusZ(), i * 0.1f * MR::PI_F / 180 ) );
    return MR::boolean( sphereA, sphereB, MR::BooleanOperation::DifferenceAB, { .rigidB2A = &rigidB2A } );
    };
    // untimed warm-up Boolean, it builds the AABB trees
    if ( auto res = subtractRotated( 1 ); !res.valid() )
    {
    std::cerr << res.errorString << std::endl;
    return 1;
    }
    const auto start = std::chrono::steady_clock::now();
    for ( int i = 1; i <= numBooleans; ++i )
    {
    if ( auto res = subtractRotated( i ); !res.valid() )
    {
    std::cerr << res.errorString << std::endl;
    return 1;
    }
    }
    const double seconds = std::chrono::duration<double>( std::chrono::steady_clock::now() - start ).count();
    std::cout << std::fixed << numBooleans << " Booleans in " << std::setprecision( 3 ) << seconds << " s, "
    << std::setprecision( 1 ) << numBooleans / seconds << " Booleans/s" << std::endl;
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    BooleanResult boolean(const Mesh &meshA, const Mesh &meshB, BooleanOperation operation, const AffineXf3f *rigidB2A, BooleanResultMapper *mapper=nullptr, ProgressCallback cb={})
    Mesh makeSphere(const SphereParams &params)
    EdgeId start(VertId v)
  • Python
    import math
    import sys
    import time
    import meshlib.mrmeshpy as mrmeshpy
    # the number of Booleans can be given in the command line
    num_booleans = int(sys.argv[1]) if len(sys.argv) > 1 else 2000
    # two identical spheres of radius 1
    sphere_params = mrmeshpy.SphereParams()
    sphere_params.radius = 1.0
    sphere_params.numMeshVertices = 3366
    sphere_a = mrmeshpy.makeSphere(sphere_params)
    sphere_b = mrmeshpy.copyMesh(sphere_a)
    # subtracts sphere_b rotated around Z-axis by i * 0.1 degrees from sphere_a;
    # sphere_b itself is not modified, so the AABB trees of both meshes are built once and reused
    def subtract_rotated(i):
    rigid_b2a = mrmeshpy.AffineXf3f.linear(mrmeshpy.Matrix3f.rotation(mrmeshpy.Vector3f.plusZ(), math.radians(i * 0.1)))
    result = mrmeshpy.boolean(sphere_a, sphere_b, mrmeshpy.BooleanOperation.DifferenceAB, rigid_b2a)
    if not result.valid():
    raise RuntimeError(result.errorString)
    # untimed warm-up Boolean, it builds the AABB trees
    subtract_rotated(1)
    start = time.perf_counter()
    for i in range(1, num_booleans + 1):
    subtract_rotated(i)
    seconds = time.perf_counter() - start
    print(f"{num_booleans} Booleans in {seconds:.3f} s, {num_booleans / seconds:.1f} Booleans/s")
  • C
    #include <MRCMesh/MRAffineXf.h>
    #include <MRCMesh/MRBooleanOperation.h>
    #include <MRCMesh/MRMakeSphereMesh.h>
    #include <MRCMesh/MRMatrix3.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshBoolean.h>
    #include <MRCMesh/MRVector3.h>
    #include <MRCMisc/std_string.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    // Subtracts sphereB rotated around Z-axis by i * 0.1 degrees from sphereA.
    // sphereB itself is not modified, so the AABB trees of both meshes are built once and reused.
    static bool subtractRotated( const MR_Mesh* sphereA, const MR_Mesh* sphereB, int i )
    {
    MR_Vector3f plusZ = MR_Vector3f_plusZ();
    MR_Matrix3f rotation = MR_Matrix3f_rotation_float( &plusZ, i * 0.1f * 3.14159265f / 180 );
    MR_AffineXf3f rigidB2A = MR_AffineXf3f_linear( &rotation );
    MR_BooleanResult* result = MR_boolean_withXf( sphereA, sphereB, MR_BooleanOperation_DifferenceAB, &rigidB2A, NULL, MR_PassBy_DefaultArgument, NULL );
    bool valid = MR_BooleanResult_valid( result );
    if ( !valid )
    fprintf( stderr, "Failed to perform boolean: %s\n", MR_std_string_data( MR_BooleanResult_Get_errorString( result ) ) );
    MR_BooleanResult_Destroy( result );
    return valid;
    }
    static double nowSeconds( void )
    {
    struct timespec ts;
    timespec_get( &ts, TIME_UTC );
    return ts.tv_sec + ts.tv_nsec * 1e-9;
    }
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    // The number of Booleans can be given in the command line.
    int numBooleans = argc > 1 ? atoi( argv[1] ) : 2000;
    // Two identical spheres of radius 1.
    MR_SphereParams* params = MR_SphereParams_ConstructFrom( 1.f, 3366 );
    MR_Mesh* sphereA = MR_makeSphere( params );
    MR_Mesh* sphereB = MR_Mesh_ConstructFromAnother( MR_PassBy_Copy, sphereA );
    MR_SphereParams_Destroy( params );
    // Untimed warm-up Boolean, it builds the AABB trees.
    if ( !subtractRotated( sphereA, sphereB, 1 ) )
    goto fail;
    double start = nowSeconds();
    for ( int i = 1; i <= numBooleans; ++i )
    if ( !subtractRotated( sphereA, sphereB, i ) )
    goto fail;
    double seconds = nowSeconds() - start;
    printf( "%d Booleans in %.3f s, %.1f Booleans/s\n", numBooleans, seconds, numBooleans / seconds );
    rc = EXIT_SUCCESS;
    fail:
    MR_Mesh_Destroy( sphereB );
    MR_Mesh_Destroy( sphereA );
    return rc;
    }
    bool valid() const
    static constexpr Matrix2 rotation(T angle) noexcept
    const UiRenderParams * params
    static constexpr Vector3 plusZ() noexcept
  • C#
    public static class BooleanBenchmarkExample
    {
    public static void Main(string[] args)
    {
    // the number of Booleans can be given in the command line
    int numBooleans = args.Length > 0 ? int.Parse(args[0]) : 2000;
    // two identical spheres of radius 1
    var sphereA = MR.makeSphere(new MR.SphereParams(1.0f, 3366));
    var sphereB = new MR.Mesh(sphereA);
    // subtracts sphereB rotated around Z-axis by i * 0.1 degrees from sphereA;
    // sphereB itself is not modified, so the AABB trees of both meshes are built once and reused
    void SubtractRotated(int i)
    {
    var rigidB2A = MR.AffineXf3f.linear(MR.Matrix3f.rotation(MR.Vector3f.plusZ(), i * 0.1f * MathF.PI / 180));
    var result = MR.boolean(sphereA, sphereB, MR.BooleanOperation.DifferenceAB, rigidB2A);
    if (!result.valid())
    throw new Exception(result.errorString);
    }
    // untimed warm-up Boolean, it builds the AABB trees
    SubtractRotated(1);
    var stopwatch = System.Diagnostics.Stopwatch.StartNew();
    for (int i = 1; i <= numBooleans; ++i)
    SubtractRotated(i);
    double seconds = stopwatch.Elapsed.TotalSeconds;
    Console.WriteLine("{0} Booleans in {1:F3} s, {2:F1} Booleans/s", numBooleans, seconds, numBooleans / seconds);
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // the number of Booleans can be given in the command line
    const numBooleans = process.argv.length > 2 ? Number(process.argv[2]) : 2000;
    // two identical spheres of radius 1
    using sphereA = ml.makeSphere({ radius: 1.0, numMeshVertices: 3366 });
    using sphereB = ml.makeSphere({ radius: 1.0, numMeshVertices: 3366 });
    // subtracts sphereB rotated around Z-axis by i * 0.1 degrees from sphereA;
    // sphereB itself is not modified, so the AABB trees of both meshes are built once and reused
    function subtractRotated(i) {
    using rotation = ml.Matrix3f.rotation(ml.Vector3.plusZ(), i * 0.1 * Math.PI / 180);
    using rigidB2A = ml.AffineXf3f.linear(rotation);
    using result = ml.booleanWithXf(sphereA, sphereB, ml.BooleanOperation.DifferenceAB, rigidB2A);
    if (!result.valid())
    throw new Error(result.errorString);
    }
    // untimed warm-up Boolean, it builds the AABB trees
    subtractRotated(1);
    const start = performance.now();
    for (let i = 1; i <= numBooleans; ++i)
    subtractRotated(i);
    const seconds = (performance.now() - start) / 1000;
    console.log(`${numBooleans} Booleans in ${seconds.toFixed(3)} s, ${(numBooleans / seconds).toFixed(1)} Booleans/s`);
    Note
    The using declaration requires Node.js 24+ or a modern browser. On older runtimes, call .delete() on each object instead (see Memory Management in the JavaScript Setup Guide).
  • Rust
    use meshlib::{AffineXf3f, BooleanOperation, Matrix3f, Mesh, SphereParams, Vector3f};
    // Subtracts sphere_b rotated around Z-axis by i * 0.1 degrees from sphere_a.
    // sphere_b itself is not modified, so the AABB trees of both meshes are built once and reused.
    fn subtract_rotated(sphere_a: &Mesh, sphere_b: &Mesh, i: i32) -> Result<(), String>
    {
    let rigid_b2a = AffineXf3f::linear(&Matrix3f::rotation_float(&Vector3f::plus_z(), (i as f32 * 0.1).to_radians()));
    let result = meshlib::boolean_with_xf(sphere_a, sphere_b, BooleanOperation::DifferenceAB, Some(&rigid_b2a), None, None);
    if !result.valid()
    {
    return Err(format!("Failed to perform boolean: {}", result.error_string()));
    }
    Ok(())
    }
    fn main() -> Result<(), String>
    {
    // The number of Booleans can be given in the command line.
    let num_booleans: i32 = std::env::args().nth(1).map_or(2000, |arg| arg.parse().expect("the number of Booleans"));
    // Two identical spheres of radius 1.
    let sphere_a = meshlib::make_sphere(&SphereParams::construct_from(1.0, 3366));
    let sphere_b = sphere_a.clone();
    // Untimed warm-up Boolean, it builds the AABB trees.
    subtract_rotated(&sphere_a, &sphere_b, 1)?;
    let start = std::time::Instant::now();
    for i in 1..=num_booleans
    {
    subtract_rotated(&sphere_a, &sphere_b, i)?;
    }
    let seconds = start.elapsed().as_secs_f64();
    println!("{num_booleans} Booleans in {seconds:.3} s, {:.1} Booleans/s", num_booleans as f64 / seconds);
    Ok(())
    }
  • Java
    import meshlib.AffineXf3f;
    import meshlib.BooleanOperation;
    import meshlib.BooleanResult;
    import meshlib.Functions;
    import meshlib.Matrix3f;
    import meshlib.Mesh;
    import meshlib.SphereParams;
    import meshlib.Vector3f;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class BooleanBenchmark
    {
    // Subtracts sphereB rotated around Z-axis by i * 0.1 degrees from sphereA.
    // sphereB itself is not modified, so the AABB trees of both meshes are built once and reused.
    static void subtractRotated(Mesh sphereA, Mesh sphereB, int i)
    {
    AffineXf3f rigidB2A = AffineXf3f.linear(Matrix3f.rotation(Vector3f.plusZ(), (float)Math.toRadians(i * 0.1)));
    BooleanResult result = Functions.boolean_(sphereA, sphereB, BooleanOperation.DifferenceAB, rigidB2A, null, null);
    if (!result.valid())
    throw new RuntimeException("Failed to perform boolean: " + result.errorString());
    }
    public static void main(String[] args)
    {
    // The number of Booleans can be given in the command line.
    int numBooleans = args.length > 0 ? Integer.parseInt(args[0]) : 2000;
    // Two identical spheres of radius 1.
    Mesh sphereA = Functions.makeSphere(new SphereParams(1.0f, 3366));
    Mesh sphereB = new Mesh(sphereA);
    // Untimed warm-up Boolean, it builds the AABB trees.
    subtractRotated(sphereA, sphereB, 1);
    long start = System.nanoTime();
    for (int i = 1; i <= numBooleans; ++i)
    subtractRotated(sphereA, sphereB, i);
    double seconds = (System.nanoTime() - start) * 1e-9;
    System.out.printf("%d Booleans in %.3f s, %.1f Booleans/s%n", numBooleans, seconds, numBooleans / seconds);
    }
    }