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
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
  • 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/MRMesh.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    // Load mesh
    // 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_GetError( meshEx ) ) );
    return 1;
    }
    // Compute mesh bounding box
    // Construct deformer on mesh vertices
    // 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
    // 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 );
    return EXIT_SUCCESS;
    }
    MRC_API MR_Vector3f MR_Box3f_center(const MR_Box3f *_this)
    struct MR_expected_MR_Mesh_std_string MR_expected_MR_Mesh_std_string
    struct MR_Mesh MR_Mesh
    struct MR_FreeFormDeformer MR_FreeFormDeformer
    MRC_API void MR_FreeFormDeformer_init(MR_FreeFormDeformer *_this, const MR_Vector3i *resolution, const MR_Box3f *initialBox)
    MRC_API void MR_FreeFormDeformer_setRefGridPointPosition(MR_FreeFormDeformer *_this, const MR_Vector3i *coordOfPointInGrid, const MR_Vector3f *newPos)
    MRC_API void MR_FreeFormDeformer_apply(const MR_FreeFormDeformer *_this)
    MRC_API void MR_FreeFormDeformer_Destroy(const MR_FreeFormDeformer *_this)
    MRC_API MR_FreeFormDeformer * MR_FreeFormDeformer_Construct_MR_Mesh(MR_Mesh *mesh, const MR_VertBitSet *region)
    MRC_API MR_expected_MR_Mesh_std_string * MR_MeshLoad_fromAnySupportedFormat_2(const char *file, const char *file_end, const MR_MeshLoadSettings *settings)
    MRC_API MR_expected_void_std_string * MR_MeshSave_toAnySupportedFormat_3(const MR_Mesh *mesh, const char *file, const char *file_end, const MR_SaveSettings *settings)
    MRC_API MR_Box3f MR_Mesh_computeBoundingBox_1(const MR_Mesh *_this, const MR_AffineXf3f *toWorld)
    MRC_API void MR_Mesh_invalidateCaches(MR_Mesh *_this, const bool *pointsChanged)
    MRC_API MR_Vector3i MR_Vector3i_diagonal(int32_t a)
    MRC_API MR_Mesh * MR_expected_MR_Mesh_std_string_GetMutableValue(MR_expected_MR_Mesh_std_string *_this)
    MRC_API const MR_std_string * MR_expected_MR_Mesh_std_string_GetError(const MR_expected_MR_Mesh_std_string *_this)
    MRC_API void MR_expected_MR_Mesh_std_string_Destroy(const MR_expected_MR_Mesh_std_string *_this)
    MRC_API const char * MR_std_string_Data(const MR_std_string *_this)
  • C#
    using static MR.DotNet;
    public class FreeFormDeformationExample
    {
    public static void Run(string[] args)
    {
    try
    {
    // Load mesh
    var mesh = MeshLoad.FromAnySupportedFormat("mesh.stl");
    // Compute mesh bounding box
    var box = mesh.BoundingBox;
    // Construct deformer on mesh vertices
    var ffDeformer = new FreeFormDeformer(mesh);
    // Init deformer with 3x3 grid on mesh box
    ffDeformer.Init(Vector3i.Diagonal(3), box);
    // Move some control points of the grid to the center
    ffDeformer.SetRefGridPointPosition(new Vector3i(1, 1, 0), box.Center());
    ffDeformer.SetRefGridPointPosition(new Vector3i(1, 1, 2), box.Center());
    ffDeformer.SetRefGridPointPosition(new Vector3i(0, 1, 1), box.Center());
    ffDeformer.SetRefGridPointPosition(new Vector3i(2, 1, 1), box.Center());
    ffDeformer.SetRefGridPointPosition(new Vector3i(1, 0, 1), box.Center());
    ffDeformer.SetRefGridPointPosition(new 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
    MeshSave.ToAnySupportedFormat(mesh, "deformed_mesh.stl");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }