MeshLib
 
Loading...
Searching...
No Matches
Mesh decimation

Example of mesh decimate

#include "MRMeshC/MRMesh.h"
#include <stdio.h>
#include <stdlib.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 != 2 && argc != 3 )
{
fprintf( stderr, "Usage: %s INPUT [OUTPUT]", argv[0] );
goto out;
}
const char* input = argv[1];
const char* output = ( argc == 2 ) ? argv[1] : argv[2];
// error messages will be stored here
MRString* errorString = NULL;
MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( input, &errorString );
if ( errorString )
{
fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
mrStringFree( errorString );
goto out;
}
// you can set various parameters for the mesh decimation; see the documentation for more info
// maximum permitted deviation
const MRBox3f bbox = mrMeshComputeBoundingBox( mesh, NULL );
params.maxError = 1e-5f * mrBox3fDiagonal( &bbox );
// maximum length of edges to be collapsed
params.tinyEdgeLength = 1e-3f;
// pack mesh after decimation
params.packMesh = true;
// set progress callback
params.progressCallback = onProgress;
MRDecimateResult result = mrDecimateMesh( mesh, &params );
if ( !result.cancelled )
printf( "Removed %d vertices, %d faces", result.vertsDeleted, result.facesDeleted );
else
{
fprintf( stderr, "Cancelled" );
goto out_mesh;
}
mrMeshSaveToAnySupportedFormat( mesh, output, &errorString );
if ( errorString )
{
fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
mrStringFree( errorString );
goto out_mesh;
}
rc = EXIT_SUCCESS;
out_mesh:
mrMeshFree( mesh );
out:
return rc;
}
MRMESHC_API float mrBox3fDiagonal(const MRBox3f *box)
computes length from min to max
@ MRDecimateStrategyMinimizeError
the next edge to collapse will be the one that introduced minimal error to the surface
Definition MRMeshC/MRMeshDecimate.h:11
MRMESHC_API MRDecimateSettings mrDecimateSettingsNew(void)
initializes a default instance
struct MRMesh MRMesh
Definition MRMeshC/MRMeshFwd.h:42
typedefMR_EXTERN_C_BEGIN struct MRString MRString
Definition MRMeshC/MRMeshFwd.h:32
MRMESHC_API MRBox3f mrMeshComputeBoundingBox(const MRMesh *mesh, const MRAffineXf3f *toWorld)
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)
MRMESHC_API MRDecimateResult mrDecimateMesh(MRMesh *mesh, const MRDecimateSettings *settings)
Collapse edges in mesh region according to the settings.
Definition MRMeshC/MRBox.h:9
results of mrDecimateMesh
Definition MRMeshC/MRMeshDecimate.h:89
int vertsDeleted
Number deleted verts. Same as the number of performed collapses.
Definition MRMeshC/MRMeshDecimate.h:91
int facesDeleted
Number deleted faces.
Definition MRMeshC/MRMeshDecimate.h:93
bool cancelled
whether the algorithm was cancelled by the callback
Definition MRMeshC/MRMeshDecimate.h:100
parameters for mrDecimateMesh
Definition MRMeshC/MRMeshDecimate.h:18
float maxError
Definition MRMeshC/MRMeshDecimate.h:24
bool packMesh
whether to pack mesh at the end
Definition MRMeshC/MRMeshDecimate.h:69
MRDecimateStrategy strategy
Definition MRMeshC/MRMeshDecimate.h:19
float tinyEdgeLength
edges not longer than this value will be collapsed even if it results in appearance of a triangle wit...
Definition MRMeshC/MRMeshDecimate.h:35
MRProgressCallback progressCallback
callback to report algorithm progress and cancel it by user request
Definition MRMeshC/MRMeshDecimate.h:71