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

Example of mesh ICP (finding transformation to match objects)

#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 } },
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:169
void setParams(const ICPProperties &prop)
tune algorithm params before run calculateTransformation()
Definition MRMesh/MRICP.h:193
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 VoidOrErrStr 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:613
AffineXf< Vector3f > AffineXf3f
Definition MRDotNet/MRMeshFwd.h:22
T diagonal() const
computes length from min to max
Definition MRMesh/MRBox.h:59
Definition MRMesh/MRICP.h:119
float distThresholdSq
Points pair will be counted only if squared distance between points is lower than.
Definition MRMesh/MRICP.h:133
float exitVal
Algorithm target root-mean-square distance. As soon as it is reached, the algorithm stops.
Definition MRMesh/MRICP.h:152
Definition MRMesh/MRMeshPart.h:11
Definition MRMesh/MRMesh.h:23
MRMESH_API Box3f getBoundingBox() const
MRMESH_API void transform(const AffineXf3f &xf, const VertBitSet *region=nullptr)