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

Example of mesh ICP (finding transformation to match objects)

  • C++
    #include <MRMesh/MRBox.h>
    #include <MRMesh/MRICP.h>
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    // Load meshes
    MR::Mesh meshFloating = *MR::MeshLoad::fromAnySupportedFormat( "meshA.stl" );
    MR::Mesh meshFixed = *MR::MeshLoad::fromAnySupportedFormat( "meshB.stl" );
    // Prepare ICP parameters
    float diagonal = meshFixed.getBoundingBox().diagonal();
    float icpSamplingVoxelSize = diagonal * 0.01f; // To sample points from object
    MR::ICPProperties icpParams;
    icpParams.distThresholdSq = MR::sqr( diagonal * 0.1f ); // Use points pairs with maximum distance specified
    icpParams.exitVal = diagonal * 0.003f; // Stop when distance reached
    // Calculate transformation
    MR::ICP icp(
    MR::MeshOrPoints{ MR::MeshPart{ meshFloating } },
    MR::MeshOrPoints{ MR::MeshPart{ meshFixed } },
    MR::AffineXf3f(), MR::AffineXf3f(),
    icpSamplingVoxelSize );
    icp.setParams( icpParams );
    MR::AffineXf3f xf = icp.calculateTransformation();
    // Transform floating mesh
    meshFloating.transform( xf );
    // Output information string
    std::string info = icp.getStatusInfo();
    std::cerr << info << std::endl;
    // Save result
    MR::MeshSave::toAnySupportedFormat( meshFloating, "meshA_icp.stl" );
    }
    Definition MRMesh/MRICP.h:171
    void setParams(const ICPProperties &prop)
    tune algorithm params before run calculateTransformation()
    Definition MRMesh/MRICP.h:195
    Definition MRMesh/MRMeshOrPoints.h:17
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    detects the format from file extension and loads mesh from it
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    detects the format from file extension and save mesh to it
    constexpr T sqr(T x) noexcept
    Definition MRMesh/MRMeshFwd.h:600
    Definition MRMesh/MRICP.h:121
    float distThresholdSq
    Points pair will be counted only if squared distance between points is lower than.
    Definition MRMesh/MRICP.h:135
    float exitVal
    Algorithm target root-mean-square distance. As soon as it is reached, the algorithm stops.
    Definition MRMesh/MRICP.h:154
    Definition MRMesh/MRMesh.h:23
    MRMESH_API Box3f getBoundingBox() const
    MRMESH_API void transform(const AffineXf3f &xf, const VertBitSet *region=nullptr)
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    # Load meshes
    meshFloating = mrmeshpy.loadMesh("meshA.stl")
    meshFixed = mrmeshpy.loadMesh("meshB.stl")
    # Prepare ICP parameters
    diagonal = meshFixed.getBoundingBox().diagonal()
    icp_sampling_voxel_size = diagonal * 0.01 # To sample points from object
    icp_params = mrmeshpy.ICPProperties()
    icp_params.distThresholdSq = (diagonal * 0.1) ** 2 # Use points pairs with maximum distance specified
    icp_params.exitVal = diagonal * 0.003 # Stop when this distance reached
    # Calculate transformation
    icp = mrmeshpy.ICP(meshFloating, meshFixed,
    mrmeshpy.AffineXf3f(), mrmeshpy.AffineXf3f(),
    icp_sampling_voxel_size)
    icp.setParams(icp_params)
    xf = icp.calculateTransformation()
    # Transform floating mesh
    meshFloating.transform(xf)
    # Output information string
    print(icp.getLastICPInfo())
    # Save result
    mrmeshpy.saveMesh(meshFloating, "meshA_icp.stl")