MeshLib Documentation
Loading...
Searching...
No Matches
Free Form Deformation Example

Example of using Free Form deformation of the mesh

  • C++
    #include <MRMesh/MRFreeFormDeformer.h>
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshLoad.h>
    #include <MRMesh/MRMeshSave.h>
    #include <iostream>
    int main()
    {
    // Load mesh
    auto mesh = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    if ( !mesh.has_value() )
    {
    std::cerr << mesh.error() << std::endl;
    return 1;
    }
    // Construct deformer on mesh vertices
    MR::FreeFormDeformer ffDeformer( mesh->points, mesh->topology.getValidVerts() );
    // Compute mesh bounding box
    const auto box = mesh->computeBoundingBox();
    // Init deformer with 3x3 grid on mesh box
    ffDeformer.init( MR::Vector3i::diagonal( 3 ), box );
    // Move some control points of the grid to the center
    ffDeformer.setRefGridPointPosition( { 1, 1, 0 }, box.center() );
    ffDeformer.setRefGridPointPosition( { 1, 1, 2 }, box.center() );
    ffDeformer.setRefGridPointPosition( { 0, 1, 1 }, box.center() );
    ffDeformer.setRefGridPointPosition( { 2, 1, 1 }, box.center() );
    ffDeformer.setRefGridPointPosition( { 1, 0, 1 }, box.center() );
    ffDeformer.setRefGridPointPosition( { 1, 2, 1 }, box.center() );
    // Apply the deformation to the mesh vertices
    ffDeformer.apply();
    // Invalidate the mesh because of external vertex changes
    mesh->invalidateCaches();
    // Save deformed mesh
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( *mesh, "deformed_mesh.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    }
    int main()
    Definition LaplacianDeformation.cpp:4
  • Python
    Note
    Python API version 3 and later
    from meshlib import mrmeshpy as mm
    # Load mesh
    mesh = mm.loadMesh("mesh.stl")
    # Compute mesh bounding box
    box = mesh.computeBoundingBox()
    # Construct deformer on mesh vertices
    ffDeformer = mm.FreeFormDeformer(mesh.points,mesh.topology.getValidVerts())
    # Init deformer with 3x3 grid on mesh box
    ffDeformer.init(mm.Vector3i.diagonal(3),box)
    # Move some control points of grid to the center
    ffDeformer.setRefGridPointPosition(mm.Vector3i(1,1,0),box.center())
    ffDeformer.setRefGridPointPosition(mm.Vector3i(1,1,2),box.center())
    ffDeformer.setRefGridPointPosition(mm.Vector3i(0,1,1),box.center())
    ffDeformer.setRefGridPointPosition(mm.Vector3i(2,1,1),box.center())
    ffDeformer.setRefGridPointPosition(mm.Vector3i(1,0,1),box.center())
    ffDeformer.setRefGridPointPosition(mm.Vector3i(1,2,1),box.center())
    # Apply deformation to mesh vertices
    ffDeformer.apply()
    # Invalidate mesh because of external vertices changes
    mesh.invalidateCaches()
    # Save deformed mesh
    mm.saveMesh(mesh,"deformed_mesh.stl")
  • C
    #include <MRCMesh/MRFreeFormDeformer.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    // Load mesh
    MR_expected_MR_Mesh_std_string* meshEx = MR_MeshLoad_fromAnySupportedFormat_2( "mesh.stl", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( meshEx );
    // Handle failure to load mesh
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return 1;
    }
    // Compute mesh bounding box
    MR_Box3f box = MR_Mesh_computeBoundingBox_1( mesh, NULL );
    // Construct deformer on mesh vertices
    MR_FreeFormDeformer* ffDeformer = MR_FreeFormDeformer_Construct_MR_Mesh( mesh, NULL );
    // Init deformer with 3x3 grid on mesh box
    MR_Vector3i resolution = MR_Vector3i_diagonal( 3 );
    MR_FreeFormDeformer_init( ffDeformer, &resolution, &box );
    // Move some control points of the grid to the center
    MR_Vector3i controlPoints[] = {
    { 1, 1, 0 },
    { 1, 1, 2 },
    { 0, 1, 1 },
    { 2, 1, 1 },
    { 1, 0, 1 },
    { 1, 2, 1 },
    };
    MR_Vector3f center = MR_Box3f_center( &box );
    for ( int i = 0; i < 6; ++i )
    MR_FreeFormDeformer_setRefGridPointPosition( ffDeformer, &controlPoints[i], &center );
    // Apply the deformation to the mesh vertices
    MR_FreeFormDeformer_apply( ffDeformer );
    // Invalidate the mesh because of external vertex changes
    MR_Mesh_invalidateCaches( mesh, NULL );
    // Save deformed mesh
    MR_MeshSave_toAnySupportedFormat_3( mesh, "deformed_mesh.stl", NULL, NULL );
    MR_FreeFormDeformer_Destroy( ffDeformer );
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return EXIT_SUCCESS;
    }
  • C#
    public class FreeFormDeformationExample
    {
    public static void Run(string[] args)
    {
    try
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Compute mesh bounding box
    MR.Box3f box = mesh.getBoundingBox();
    // Construct deformer on mesh vertices
    MR.FreeFormDeformer ffDeformer = new(mesh);
    // Init deformer with 3x3 grid on mesh box
    ffDeformer.init(MR.Vector3i.diagonal(3), box);
    // Move some control points of the grid to the center
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(1, 1, 0), box.center());
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(1, 1, 2), box.center());
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(0, 1, 1), box.center());
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(2, 1, 1), box.center());
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(1, 0, 1), box.center());
    ffDeformer.setRefGridPointPosition(new MR.Vector3i(1, 2, 1), box.center());
    // Apply the deformation to the mesh vertices
    ffDeformer.apply();
    // Invalidate the mesh because of external vertex changes
    mesh.invalidateCaches();
    // Save deformed mesh
    MR.MeshSave.toAnySupportedFormat(mesh, "deformed_mesh.stl");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }