MeshLib Documentation
Loading...
Searching...
No Matches
Mesh export

Export example of points and triangles from mesh (e.g. for rendering)

  • C++
    #include <MRMesh/MRCube.h>
    #include <MRMesh/MRMesh.h>
    int main()
    {
    // create some mesh
    // all vertices of valid triangles
    const std::vector<std::array<MR::VertId, 3>> triangles = mesh.topology.getAllTriVerts();
    // all point coordinates
    const MR::VertCoords & points = mesh.points;
    // triangle vertices as triples of ints (pointing to elements in points vector)
    const int * vertexTriples = reinterpret_cast<const int*>( triangles.data() );
    // TODO: export in your format
    std::ignore = points;
    std::ignore = vertexTriples;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Mesh makeCube(const Vector3f &size=Vector3f::diagonal(1.0f), const Vector3f &base=Vector3f::diagonal(-0.5f))
    VertCoords & points() const
    const std::shared_ptr< const Mesh > & mesh() const
  • Python See a more relevant example: Numpy support
  • C
    #include <MRCMesh/MRCube.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshTopology.h>
    #include <MRCMesh/MRVector.h>
    #include <MRCMesh/MRVector3.h>
    #include <MRCMisc/std_vector_std_array_MR_VertId_3.h>
    #include <stdlib.h>
    int main( void )
    {
    // create some mesh
    const MR_Vector3f size = MR_Vector3f_diagonal( 1.f );
    const MR_Vector3f base = MR_Vector3f_diagonal( -0.5f );
    MR_Mesh* mesh = MR_makeCube( &size, &base );
    // all vertices of valid triangles
    MR_std_vector_std_array_MR_VertId_3* triangles = MR_MeshTopology_getAllTriVerts( MR_Mesh_Get_topology( mesh ) );
    // all point coordinates as x,y,z triples
    const float* points = (const float*)MR_VertCoords_data( MR_Mesh_Get_points( mesh ) );
    // triangle vertices as triples of ints (pointing to elements in points vector)
    const int* vertexTriples = (const int*)MR_std_vector_std_array_MR_VertId_3_data( triangles );
    // TODO: export in your format
    (void)points;
    (void)vertexTriples;
    MR_std_vector_std_array_MR_VertId_3_Destroy( triangles );
    MR_Mesh_Destroy( mesh );
    return EXIT_SUCCESS;
    }
    size_t size() const
  • C#
    public static class MeshExportExample
    {
    public static void Main(string[] args)
    {
    // create some mesh
    var mesh = MR.makeCube();
    // all vertices of valid triangles
    var triangles = mesh.topology.getAllTriVerts();
    // all point coordinates
    var points = mesh.points;
    // triangle vertices as triples of ints (pointing to elements in points vector)
    var vertexTriples = new int[(int)triangles.size() * 3];
    for (int t = 0; t < (int)triangles.size(); ++t)
    {
    var tri = triangles[(nuint)t].elems;
    vertexTriples[3 * t + 0] = tri._0.id;
    vertexTriples[3 * t + 1] = tri._1.id;
    vertexTriples[3 * t + 2] = tri._2.id;
    }
    // TODO: export in your format
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // create some mesh
    using mesh = ml.makeCube({ x: 1, y: 1, z: 1 }, { x: -0.5, y: -0.5, z: -0.5 });
    // all point coordinates, as a flat Float32Array of x, y, z triples
    using points = mesh.points;
    const vertexCoordinates = points.toArray();
    // triangle vertices, as a flat Uint32Array of triples of indices into the points array
    using topology = mesh.topology;
    using triangulation = topology.getTriangulation();
    const vertexTriples = triangulation.toArray();
    // TODO: export in your format
    console.log(`${vertexCoordinates.length / 3} vertices, ${vertexTriples.length / 3} triangles`);
    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
    fn main()
    {
    // create some mesh
    let mesh = meshlib::make_cube(None, None);
    // all vertices of valid triangles
    let triangles = mesh.topology().get_all_tri_verts();
    // all point coordinates
    let points = mesh.points();
    // triangle vertices as triples of ints (pointing to elements in points vector)
    let vertex_triples = triangles.data() as *const i32;
    // TODO: export in your format
    let _ = points;
    let _ = vertex_triples;
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.VertCoords;
    import meshlib.std_vector_std_array_MR_VertId_3;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshExport
    {
    public static void main(String[] args)
    {
    // create some mesh
    Mesh mesh = Functions.makeCube(null, null);
    // all vertices of valid triangles
    std_vector_std_array_MR_VertId_3 triangles = mesh.topology().getAllTriVerts();
    // all point coordinates
    VertCoords points = mesh.points();
    // triangle vertices as triples of ints (pointing to elements in points vector)
    int[] vertexTriples = new int[(int)triangles.size() * 3];
    for (int t = 0; t < triangles.size(); t++)
    for (int i = 0; i < 3; i++)
    vertexTriples[3 * t + i] = triangles.at(t).elems(i).id_();
    // TODO: export in your format
    }
    }