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>
    int main()
    {
    // Load mesh
    auto mesh = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    assert( mesh );
    // Compute mesh bounding box
    const auto box = mesh->computeBoundingBox();
    // Construct deformer on mesh vertices
    MR::FreeFormDeformer ffDeformer( mesh->points, mesh->topology.getValidVerts() );
    // 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
    MR::MeshSave::toAnySupportedFormat( *mesh, "deformed_mesh.stl" );
    }
    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 <MRMeshC/MRFreeFormDeformer.h>
    #include <MRMeshC/MRMesh.h>
    #include <MRMeshC/MRMeshLoad.h>
    #include <MRMeshC/MRMeshSave.h>
    #include <stdlib.h>
    int main( int argc, char* argv[] )
    {
    // Load mesh
    MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( "mesh.stl", NULL );
    // Compute mesh bounding box
    MRBox3f box = mrMeshComputeBoundingBox( mesh, NULL );
    // Construct deformer on mesh vertices
    // Init deformer with 3x3 grid on mesh box
    MRVector3i resolution = mrVector3iDiagonal( 3 );
    mrFreeFormDeformerInit( ffDeformer, &resolution, &box );
    // Move some control points of the grid to the center
    MRVector3i controlPoints[] = {
    { 1, 1, 0 },
    { 1, 1, 2 },
    { 0, 1, 1 },
    { 2, 1, 1 },
    { 1, 0, 1 },
    { 1, 2, 1 },
    };
    MRVector3f center = mrBox3fCenter( &box );
    for ( int i = 0; i < 6; ++i )
    mrFreeFormDeformerSetRefGridPointPosition( ffDeformer, &controlPoints[i], &center );
    // Apply the deformation to the mesh vertices
    mrFreeFormDeformerApply( ffDeformer );
    // Invalidate the mesh because of external vertex changes
    mrMeshInvalidateCaches( mesh, true );
    // Save deformed mesh
    mrMeshSaveToAnySupportedFormat( mesh, "deformed_mesh.stl", NULL, NULL );
    mrFreeFormDeformerFree( ffDeformer );
    mrMeshFree( mesh );
    return EXIT_SUCCESS;
    }
    MRMESHC_API MRVector3f mrBox3fCenter(const MRBox3f *box)
    typedefMR_EXTERN_C_BEGIN struct MRFreeFormDeformer MRFreeFormDeformer
    MRMESHC_API void mrFreeFormDeformerApply(const MRFreeFormDeformer *deformer)
    MRMESHC_API void mrFreeFormDeformerInit(MRFreeFormDeformer *deformer, const MRVector3i *resolution, const MRBox3f *initialBox)
    MRMESHC_API void mrFreeFormDeformerFree(MRFreeFormDeformer *deformer)
    MRMESHC_API MRFreeFormDeformer * mrFreeFormDeformerNewFromMesh(MRMesh *mesh, const MRVertBitSet *region)
    MRMESHC_API void mrFreeFormDeformerSetRefGridPointPosition(MRFreeFormDeformer *deformer, const MRVector3i *coordOfPointInGrid, const MRVector3f *newPos)
    struct MRMesh MRMesh
    MR_EXTERN_C_BEGIN MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MR_EXTERN_C_BEGIN MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, const MRSaveSettings *settings, MRString **errorStr)
    MRMESHC_API MRBox3f mrMeshComputeBoundingBox(const MRMesh *mesh, const MRAffineXf3f *toWorld)
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    MRMESHC_API void mrMeshInvalidateCaches(MRMesh *mesh, bool pointsChanged)
    MRMESHC_API MRVector3i mrVector3iDiagonal(int a)