MeshLib
 
Loading...
Searching...
No Matches
Loading and saving mesh files

Following code presents example of loading and saving mesh file

#include "MRMeshC/MRMesh.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char* argv[] )
{
int rc = EXIT_FAILURE;
if ( argc != 3 )
{
fprintf( stderr, "Usage: %s INPUTFILE OUTPUTFILE", argv[0] );
goto out;
}
const char* inputFile = argv[1];
const char* outputFile = argv[2];
// error messages will be stored here
MRString* errorString = NULL;
MRMesh* mesh1 = mrMeshLoadFromAnySupportedFormat( inputFile, &errorString );
if ( errorString )
{
fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
mrStringFree( errorString );
goto out;
}
// extract vertices
const MRVector3f* vertices = mrMeshPoints( mesh1 );
size_t verticesNum = mrMeshPointsNum( mesh1 );
// you can access coordinates via struct fields...
if ( verticesNum > 0 )
printf( "The first vertex coordinates: %f; %f; %f", vertices[0].x, vertices[0].y, vertices[0].z );
// ..or cast them to a row-major float array
float* vertexData = malloc( sizeof( float ) * 3 * verticesNum );
memcpy( vertexData, vertices, sizeof( MRVector3f ) * verticesNum );
// extract faces
const MRThreeVertIds* faces = mrTriangulationData( t );
size_t facesNum = mrTriangulationSize( t );
// faces are stored as vertex id triples...
if ( facesNum > 0 )
printf( "The first face's vertex ids: %d, %d, %d", faces[0][0].id, faces[0][1].id, faces[0][2].id );
// ...and can also be casted to an integer array
int* faceData = malloc( sizeof( int ) * 3 * facesNum );
memcpy( faceData, faces, sizeof( MRThreeVertIds ) * facesNum );
// meshes can be constructed from these data arrays
MRMesh* mesh2 = mrMeshFromTriangles( (const MRVector3f*)vertexData, verticesNum, (const MRThreeVertIds*)faceData, facesNum );
mrMeshSaveToAnySupportedFormat( mesh2, outputFile, &errorString );
if ( errorString )
{
fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
mrStringFree( errorString );
goto out_mesh2;
}
rc = EXIT_SUCCESS;
out_mesh2:
mrMeshFree( mesh2 );
out_faceData:
free( faceData );
out_t:
out_vertexData:
free( vertexData );
out_mesh1:
mrMeshFree( mesh1 );
out:
return rc;
}
MRVertId MRThreeVertIds[3]
a set of 3 vertices; useful for representing a face via its vertex indices
Definition MRMeshC/MRId.h:17
struct MRMesh MRMesh
Definition MRMeshC/MRMeshFwd.h:42
typedefMR_EXTERN_C_BEGIN struct MRString MRString
Definition MRMeshC/MRMeshFwd.h:32
struct MRTriangulation MRTriangulation
Definition MRMeshC/MRMeshFwd.h:44
MRMESHC_API size_t mrTriangulationSize(const MRTriangulation *tris)
gets total count of the vertex triples of the triangulation
MRMESHC_API void mrTriangulationFree(MRTriangulation *tris)
deallocates the Triangulation object
MRMESHC_API const MRThreeVertIds * mrTriangulationData(const MRTriangulation *tris)
gets read-only access to the vertex triples of the triangulation
MRMESHC_API const MRVector3f * mrMeshPoints(const MRMesh *mesh)
gets read-only access to the mesh vertex coordinates
MRMESHC_API MRTriangulation * mrMeshGetTriangulation(const MRMesh *mesh)
MRMESHC_API size_t mrMeshPointsNum(const MRMesh *mesh)
gets count of the mesh vertex coordinates
MRMESHC_API MRMesh * mrMeshFromTriangles(const MRVector3f *vertexCoordinates, size_t vertexCoordinatesNum, const MRThreeVertIds *t, size_t tNum)
constructs a mesh from vertex coordinates and a set of triangles with given ids
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)
three-dimensional vector
Definition MRMeshC/MRVector3.h:9