#include "MRCMesh/MRMeshSave.h"
#include "MRCMisc/expected_MR_Mesh_std_string.h"
#include "MRCMisc/expected_void_std_string.h"
#include "MRCMisc/std_string.h"
#include <MRCMesh/MRBitSet.h>
#include <MRCMesh/MRMesh.h>
#include <MRCMesh/MRMeshPart.h>
#include <MRCMesh/MRPointCloud.h>
#include <MRCMesh/MRPointCloudTriangulation.h>
#include <MRCMesh/MRUniformSampling.h>
#include <MRCMesh/MRVector.h>
#include <MRCMisc/std_optional_MR_Mesh.h>
#include <MRCMisc/std_optional_MR_VertBitSet.h>
#include <MRCVoxels/MROffset.h>
#include <math.h>
#include <stdio.h>
#define PI_F 3.14159265358979323846f
#define PI2_F ( 2 * PI_F )
{
MR_PointCloud* pc = MR_PointCloud_DefaultConstruct();
MR_VertCoords_reserve( MR_PointCloud_GetMutable_points( pc ), 10000 );
for ( int i = 0; i < 100; ++i )
{
const float u = PI2_F * (float)i / ( 100.f - 1.f );
for ( int j = 0; j < 100; ++j )
{
const float v = PI_F * (float)j / ( 100.f - 1.f );
MR_PointCloud_addPoint( pc, &(MR_Vector3f){
cosf( u ) * sinf( v ),
sinf( u ) * sinf( v ),
cosf( v )
} );
}
}
MR_UniformSamplingSettings* samplingSettings = MR_UniformSamplingSettings_DefaultConstruct();
MR_UniformSamplingSettings_Set_distance( samplingSettings, 1e-3f );
MR_std_optional_MR_VertBitSet* vs = MR_pointUniformSampling( pc, samplingSettings );
MR_UniformSamplingSettings_Destroy( samplingSettings );
if ( !vs )
{
fprintf( stderr, "Removing duplicate points failed\n" );
MR_std_optional_MR_VertBitSet_Destroy( vs );
return 1;
}
MR_PointCloud_Set_validPoints( pc, MR_PassBy_Move, MR_std_optional_MR_VertBitSet_value_mut( vs ) );
MR_std_optional_MR_VertBitSet_Destroy( vs );
MR_PointCloud_invalidateCaches( pc );
MR_std_optional_MR_Mesh* triangulatedOpt = MR_triangulatePointCloud( pc, NULL, NULL );
MR_PointCloud_Destroy( pc );
MR_Mesh* triangulated = MR_std_optional_MR_Mesh_value_mut( triangulatedOpt );
if ( !triangulated )
{
fprintf( stderr, "Triangulation failed\n" );
MR_std_optional_MR_Mesh_Destroy( triangulatedOpt );
return 1;
}
MR_OffsetParameters* offsetParams = MR_OffsetParameters_DefaultConstruct();
MR_MeshPart* mp = MR_MeshPart_Construct( triangulated, NULL );
MR_OffsetParameters_Set_voxelSize( offsetParams, MR_suggestVoxelSize( mp, 5e+6f ) );
MR_expected_MR_Mesh_std_string *meshEx = MR_offsetMesh( mp, 0.f, offsetParams );
MR_MeshPart_Destroy( mp );
MR_std_optional_MR_Mesh_Destroy( triangulatedOpt );
MR_Mesh*
mesh = MR_expected_MR_Mesh_std_string_value_mut( meshEx );
{
fprintf( stderr, "Offset failed: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
MR_expected_MR_Mesh_std_string_Destroy( meshEx );
return 1;
}
MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat(
mesh,
"result.stl", NULL, NULL);
MR_expected_MR_Mesh_std_string_Destroy( meshEx );
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 ) ) );
MR_expected_void_std_string_Destroy( saveEx );
return 1;
}
MR_expected_void_std_string_Destroy( saveEx );
return 0;
}