MeshLib Documentation
Loading...
Searching...
No Matches
MRMesh/MRICP.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRICPEnums.h"
4#include "MRMeshOrPoints.h"
5#include "MRMatrix3.h"
6#include "MRId.h"
7#include "MRConstants.h"
8#include "MRAffineXf.h"
9#include "MRBitSet.h"
10#include <cfloat>
11
12namespace MR
13{
14
16{
18 Vector3f srcPoint;
19
21 Vector3f srcNorm;
22
24 Vector3f tgtPoint;
25
27 Vector3f tgtNorm;
28
30 float distSq = 0.f;
31
33 float weight = 1.f;
34
35 friend bool operator == ( const ICPPairData&, const ICPPairData& ) = default;
36};
37
39struct PointPair : public ICPPairData
40{
42 VertId srcVertId;
43
47
49 float normalsAngleCos = 1.f;
50
52 bool tgtOnBd = false;
53
54 friend bool operator == ( const PointPair&, const PointPair& ) = default;
55};
56
59{
60 // required to declare explicitly to avoid warnings
61 IPointPairs() = default;
62 IPointPairs( const IPointPairs& ) = default;
63 IPointPairs& operator=( const IPointPairs& ) = default;
64 IPointPairs( IPointPairs&& ) noexcept = default;
65 IPointPairs& operator=( IPointPairs&& ) noexcept = default;
66 virtual ~IPointPairs() = default;
67
68 virtual const ICPPairData& operator[]( size_t ) const = 0;
69 virtual ICPPairData& operator[]( size_t ) = 0;
70 virtual size_t size() const = 0;
72};
73
74struct PointPairs : public IPointPairs
75{
76 virtual const ICPPairData& operator[]( size_t idx ) const override { return vec[idx]; }
77 virtual ICPPairData& operator[]( size_t idx ) override { return vec[idx]; }
78 virtual size_t size() const override { return vec.size(); }
79 std::vector<PointPair> vec;
80};
81
83[[nodiscard]] inline size_t getNumSamples( const IPointPairs& pairs ) { return pairs.active.size(); }
84
86[[nodiscard]] MRMESH_API size_t getNumActivePairs( const IPointPairs& pairs );
87
88struct NumSum
89{
90 int num = 0;
91 double sum = 0;
92
93 friend NumSum operator + ( const NumSum & a, const NumSum & b ) { return { a.num + b.num, a.sum + b.sum }; }
94
95 [[nodiscard]] float rootMeanSqF() const { return ( num <= 0 ) ? FLT_MAX : (float)std::sqrt( sum / num ); }
96};
97
100[[nodiscard]] MRMESH_API NumSum getSumSqDistToPoint( const IPointPairs& pairs, double* inaccuracy = nullptr );
101
104[[nodiscard]] MRMESH_API NumSum getSumSqDistToPlane( const IPointPairs& pairs, double* inaccuracy = nullptr);
105
107[[nodiscard]] inline float getMeanSqDistToPoint( const IPointPairs& pairs ) { return getSumSqDistToPoint( pairs ).rootMeanSqF(); }
108
110[[nodiscard]] inline float getMeanSqDistToPlane( const IPointPairs& pairs ) { return getSumSqDistToPlane( pairs ).rootMeanSqF(); }
111
113[[nodiscard]] MRMESH_API std::string getICPStatusInfo( int iterations, ICPExitType exitType );
114
116[[nodiscard]] MRMESH_API AffineXf3f getAligningXf( const PointToPlaneAligningTransform & p2pl,
117 ICPMode mode, float angleLimit, float scaleLimit, const Vector3f & fixedRotationAxis );
118
119
121{
123 ICPMethod method = ICPMethod::PointToPlane;
124
126 float p2plAngleLimit = PI_F / 6.0f; // [radians]
127
129 float p2plScaleLimit = 2;
130
132 float cosThreshold = 0.7f; // in [-1,1]
133
135 float distThresholdSq = 1.f; // [distance^2]
136
139 float farDistFactor = 3.f; // dimensionless
140
142 ICPMode icpMode = ICPMode::AnyRigidXf;
143
146
148 int iterLimit = 10;
149
151 int badIterStopCount = 3;
152
154 float exitVal = 0; // [distance]
155
157 bool mutualClosest = false;
158};
159
161MRMESH_API size_t deactivateFarPairs( IPointPairs& pairs, float maxDistSq );
162
165 const MeshOrPointsXf& src, const MeshOrPointsXf& tgt,
166 float cosThreshold, float distThresholdSq, bool mutualClosest );
167
170class [[nodiscard]] ICP
171{
172public:
180 ICP( const MeshOrPoints& flt, const MeshOrPoints& ref, const AffineXf3f& fltXf, const AffineXf3f& refXf,
181 const VertBitSet& fltSamples = {}, const VertBitSet& refSamples = {} ) : ICP( { flt, fltXf }, { ref, refXf }, fltSamples, refSamples ) {}
182 MRMESH_API ICP( const MeshOrPointsXf& flt, const MeshOrPointsXf& ref, const VertBitSet& fltSamples = {}, const VertBitSet& refSamples = {} );
183
190 MRMESH_API ICP( const MeshOrPoints& flt, const MeshOrPoints& ref, const AffineXf3f& fltXf, const AffineXf3f& refXf,
191 float samplingVoxelSize ) : ICP( { flt, fltXf }, { ref, refXf }, samplingVoxelSize ) {}
192 MRMESH_API ICP( const MeshOrPointsXf& flt, const MeshOrPointsXf& ref, float samplingVoxelSize );
193
195 void setParams(const ICPProperties& prop) { prop_ = prop; }
196 MRMESH_API void setCosineLimit(const float cos);
197 MRMESH_API void setDistanceLimit( const float dist );
198 MRMESH_API void setBadIterCount( const int iter );
199 MRMESH_API void setFarDistFactor(const float factor);
200
202 MRMESH_API void setFltSamples( const VertBitSet& fltSamples );
203 MRMESH_API void sampleFltPoints( float samplingVoxelSize );
204
206 MRMESH_API void setRefSamples( const VertBitSet& refSamples );
207 MRMESH_API void sampleRefPoints( float samplingVoxelSize );
208
210 void samplePoints( float samplingVoxelSize ) { sampleFltPoints( samplingVoxelSize ); sampleRefPoints( samplingVoxelSize ); }
211
212 [[deprecated]] void recomputeBitSet( float fltSamplingVoxelSize ) { sampleFltPoints( fltSamplingVoxelSize ); }
213
215 MRMESH_API void setXfs( const AffineXf3f& fltXf, const AffineXf3f& refXf );
216
218 MRMESH_API void setFloatXf( const AffineXf3f& fltXf );
219
224
227
228 [[nodiscard]] const ICPProperties& getParams() const { return prop_; }
229
230 [[nodiscard]] MRMESH_API std::string getStatusInfo() const; // returns status info string
231
233 [[nodiscard]] size_t getNumSamples() const { return MR::getNumSamples( flt2refPairs_ ) + MR::getNumSamples( ref2fltPairs_ ); }
234
236 [[nodiscard]] size_t getNumActivePairs() const { return MR::getNumActivePairs( flt2refPairs_ ) + MR::getNumActivePairs( ref2fltPairs_ ); }
237
239 [[nodiscard]] MRMESH_API float getMeanSqDistToPoint() const;
240
242 [[nodiscard]] MRMESH_API float getMeanSqDistToPlane() const;
243
245 [[nodiscard]] const PointPairs & getFlt2RefPairs() const { return flt2refPairs_; }
246
248 [[nodiscard]] const PointPairs & getRef2FltPairs() const { return ref2fltPairs_; }
249
252 [[nodiscard]] MRMESH_API AffineXf3f calculateTransformation();
253
254private:
255 MeshOrPointsXf flt_;
256 MeshOrPointsXf ref_;
257
258 ICPProperties prop_;
259
260 PointPairs flt2refPairs_;
261 PointPairs ref2fltPairs_;
262
263 ICPExitType resultType_{ ICPExitType::NotStarted };
264
266 void deactivatefarDistPairs_();
267
268 int iter_ = 0;
269 bool p2ptIter_();
270 bool p2plIter_();
271};
272
273} //namespace MR
constexpr A operator+(A a)
Definition MRImGuiVectorOperators.h:97
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
container of bits
Definition MRMesh/MRBitSet.h:27
Definition MRMesh/MRICP.h:171
size_t getNumSamples() const
computes the number of samples able to form pairs
Definition MRMesh/MRICP.h:233
MRMESH_API void setCosineLimit(const float cos)
const ICPProperties & getParams() const
Definition MRMesh/MRICP.h:228
MRMESH_API AffineXf3f calculateTransformation()
void setParams(const ICPProperties &prop)
tune algorithm params before run calculateTransformation()
Definition MRMesh/MRICP.h:195
MRMESH_API void sampleRefPoints(float samplingVoxelSize)
MRMESH_API void setFltSamples(const VertBitSet &fltSamples)
select pairs with origin samples on floating object
void samplePoints(float samplingVoxelSize)
select pairs with origin samples on both objects
Definition MRMesh/MRICP.h:210
const PointPairs & getRef2FltPairs() const
returns current pairs formed from samples on reference object and projections on floating object
Definition MRMesh/MRICP.h:248
const PointPairs & getFlt2RefPairs() const
returns current pairs formed from samples on floating object and projections on reference object
Definition MRMesh/MRICP.h:245
MRMESH_API void setFloatXf(const AffineXf3f &fltXf)
sets to-world transformation for the floating object
size_t getNumActivePairs() const
computes the number of active point pairs
Definition MRMesh/MRICP.h:236
MRMESH_API void setRefSamples(const VertBitSet &refSamples)
select pairs with origin samples on reference object
MRMESH_API void sampleFltPoints(float samplingVoxelSize)
MRMESH_API void setBadIterCount(const int iter)
MRMESH_API void updatePointPairs()
recompute point pairs after manual change of transformations or parameters
MRMESH_API void setXfs(const AffineXf3f &fltXf, const AffineXf3f &refXf)
sets to-world transformations both for floating and reference objects
ICP(const MeshOrPoints &flt, const MeshOrPoints &ref, const AffineXf3f &fltXf, const AffineXf3f &refXf, const VertBitSet &fltSamples={}, const VertBitSet &refSamples={})
Definition MRMesh/MRICP.h:180
MRMESH_API ICP(const MeshOrPointsXf &flt, const MeshOrPointsXf &ref, const VertBitSet &fltSamples={}, const VertBitSet &refSamples={})
MRMESH_API float getMeanSqDistToPlane() const
computes root-mean-square deviation from points to target planes
MRMESH_API float getMeanSqDistToPoint() const
computes root-mean-square deviation between points
MRMESH_API ICP(const MeshOrPointsXf &flt, const MeshOrPointsXf &ref, float samplingVoxelSize)
MRMESH_API std::string getStatusInfo() const
MRMESH_API ICP(const MeshOrPoints &flt, const MeshOrPoints &ref, const AffineXf3f &fltXf, const AffineXf3f &refXf, float samplingVoxelSize)
Definition MRMesh/MRICP.h:190
MRMESH_API AffineXf3f autoSelectFloatXf()
MRMESH_API void setDistanceLimit(const float dist)
MRMESH_API void setFarDistFactor(const float factor)
void recomputeBitSet(float fltSamplingVoxelSize)
Definition MRMesh/MRICP.h:212
Definition MRMesh/MRMeshOrPoints.h:17
Definition MRPointToPlaneAligningTransform.h:20
Definition MRCameraOrientationPlugin.h:8
float getMeanSqDistToPlane(const IPointPairs &pairs)
computes root-mean-square deviation from points to target planes
Definition MRMesh/MRICP.h:110
ICPMode
The group of transformations, each with its own degrees of freedom.
Definition MRICPEnums.h:16
MRMESH_API size_t getNumActivePairs(const IPointPairs &pairs)
computes the number of active pairs
size_t getNumSamples(const IPointPairs &pairs)
returns the number of samples able to form pairs
Definition MRMesh/MRICP.h:83
MRMESH_API std::string getICPStatusInfo(int iterations, ICPExitType exitType)
returns status info string
float getMeanSqDistToPoint(const IPointPairs &pairs)
computes root-mean-square deviation between points
Definition MRMesh/MRICP.h:107
ICPExitType
Definition MRICPEnums.h:25
MRMESH_API AffineXf3f getAligningXf(const PointToPlaneAligningTransform &p2pl, ICPMode mode, float angleLimit, float scaleLimit, const Vector3f &fixedRotationAxis)
given prepared (p2pl) object, finds the best transformation from it of given type with given limitati...
MRMESH_API NumSum getSumSqDistToPlane(const IPointPairs &pairs, double *inaccuracy=nullptr)
ICPMethod
The method how to update transformation from point pairs.
Definition MRICPEnums.h:6
MRMESH_API void updatePointPairs(PointPairs &pairs, const MeshOrPointsXf &src, const MeshOrPointsXf &tgt, float cosThreshold, float distThresholdSq, bool mutualClosest)
in each pair updates the target data and performs basic filtering (activation)
MRMESH_API size_t deactivateFarPairs(IPointPairs &pairs, float maxDistSq)
reset active bit if pair distance is further than maxDistSq
MRMESH_API NumSum getSumSqDistToPoint(const IPointPairs &pairs, double *inaccuracy=nullptr)
Definition MRMesh/MRICP.h:16
friend bool operator==(const ICPPairData &, const ICPPairData &)=default
Vector3f srcNorm
normal in source point after transforming in world space
Definition MRMesh/MRICP.h:21
Vector3f tgtPoint
coordinates of the closest point on target after transforming in world space
Definition MRMesh/MRICP.h:24
Vector3f tgtNorm
normal in the target point after transforming in world space
Definition MRMesh/MRICP.h:27
float weight
weight of the pair (to prioritize over other pairs)
Definition MRMesh/MRICP.h:33
Vector3f srcPoint
coordinates of the source point after transforming in world space
Definition MRMesh/MRICP.h:18
float distSq
squared distance between source and target points
Definition MRMesh/MRICP.h:30
Definition MRMesh/MRICP.h:121
Vector3f fixedRotationAxis
If this vector is not zero then rotation is allowed relative to this axis only.
Definition MRMesh/MRICP.h:145
Simple interface for pairs holder.
Definition MRMesh/MRICP.h:59
virtual size_t size() const =0
IPointPairs(const IPointPairs &)=default
IPointPairs(IPointPairs &&) noexcept=default
BitSet active
whether corresponding pair from vec must be considered during minimization
Definition MRMesh/MRICP.h:71
IPointPairs & operator=(const IPointPairs &)=default
IPointPairs()=default
an object and its transformation to global space with other objects
Definition MRMesh/MRMeshOrPoints.h:97
Definition MRMesh/MRICP.h:89
float rootMeanSqF() const
Definition MRMesh/MRICP.h:95
double sum
Definition MRMesh/MRICP.h:91
int num
Definition MRMesh/MRICP.h:90
Stores a pair of points: one samples on the source and the closest to it on the target.
Definition MRMesh/MRICP.h:40
float normalsAngleCos
cosine between normals in source and target points
Definition MRMesh/MRICP.h:49
VertId srcVertId
id of the source point
Definition MRMesh/MRICP.h:42
friend bool operator==(const PointPair &, const PointPair &)=default
VertId tgtCloseVert
Definition MRMesh/MRICP.h:46
bool tgtOnBd
true if if the closest point on target is located on the boundary (only for meshes)
Definition MRMesh/MRICP.h:52
Definition MRMesh/MRICP.h:75
virtual size_t size() const override
Definition MRMesh/MRICP.h:78
std::vector< PointPair > vec
vector of all point pairs both active and not
Definition MRMesh/MRICP.h:79
virtual const ICPPairData & operator[](size_t idx) const override
Definition MRMesh/MRICP.h:76
virtual ICPPairData & operator[](size_t idx) override
Definition MRMesh/MRICP.h:77