MeshLib
 
Loading...
Searching...
No Matches
Mesh boolean

Example of Boolean operation

#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:9
@ MRBooleanOperationUnion
Union surface of two meshes (outside parts)
Definition MRMeshC/MRBooleanOperation.h:19
@ MRBooleanOperationIntersection
Intersection surface of two meshes (inside parts)
Definition MRMeshC/MRBooleanOperation.h:21
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:42
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:22
This structure store result mesh of mrBoolean or some error info.
Definition MRMeshC/MRMeshBoolean.h:30
MRString * errorString
Holds error message, empty if boolean succeed.
Definition MRMeshC/MRMeshBoolean.h:36
MRMesh * mesh
Result mesh of boolean operation, if error occurred it would be empty.
Definition MRMeshC/MRMeshBoolean.h:32