1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef _VCG_MATH_POINTMATCHING_H
#define _VCG_MATH_POINTMATCHING_H
#include <vcg/math/quaternion.h>
#include <vcg/math/matrix44.h>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include <iostream>
namespace vcg
{
/*! \brief Compute cross covariance
It computes the cross covariance matrix of two set of 3D points P and X;
it returns also the barycenters of P and X.
Ref:
Besl, McKay
A method for registration of 3d Shapes
IEEE TPAMI Vol 14, No 2 1992
*/
template <class S >
void ComputeCrossCovarianceMatrix(const std::vector<Point3<S> > &spVec, Point3<S> &spBarycenter,
const std::vector<Point3<S> > &tpVec, Point3<S> &tpBarycenter,
Eigen::Matrix3d &m)
{
assert(spVec.size() == tpVec.size());
m.setZero();
spBarycenter.SetZero();
tpBarycenter.SetZero();
Eigen::Vector3d spe;
Eigen::Vector3d tpe;
typename std::vector <Point3<S> >::const_iterator si, ti;
for (si = spVec.begin(), ti = tpVec.begin(); si != spVec.end(); ++si, ++ti){
spBarycenter += *si;
tpBarycenter += *ti;
si->ToEigenVector(spe);
ti->ToEigenVector(tpe);
m += spe*tpe.transpose();
}
spBarycenter /= double(spVec.size());
tpBarycenter /= double(tpVec.size());
spBarycenter.ToEigenVector(spe);
tpBarycenter.ToEigenVector(tpe);
m /= double(spVec.size());
m -= spe*tpe.transpose();
}
/*! \brief Compute the roto-translation that applied to PMov bring them onto Pfix
* Rotation is computed as a quaternion.
*
* E.g. it find a matrix such that:
*
* Pfix[i] = res * Pmov[i]
*
* Ref:
* Besl, McKay
* A method for registration of 3d Shapes
* IEEE TPAMI Vol 14, No 2 1992
*/
template < class S >
void ComputeRigidMatchMatrix(std::vector<Point3<S> > &Pfix,
std::vector<Point3<S> > &Pmov,
Quaternion<S> &q,
Point3<S> &tr)
{
Eigen::Matrix3d ccm;
Point3<S> bfix,bmov; // baricenter of src e trg
ComputeCrossCovarianceMatrix(Pmov,bmov,Pfix,bfix,ccm);
Eigen::Matrix3d cyc; // the cyclic components of the cross covariance matrix.
cyc=ccm-ccm.transpose();
Eigen::Matrix4d QQ;
QQ.setZero();
Eigen::Vector3d D(cyc(1,2),cyc(2,0),cyc(0,1));
Eigen::Matrix3d RM;
RM.setZero();
RM(0,0)=-ccm.trace();
RM(1,1)=-ccm.trace();
RM(2,2)=-ccm.trace();
RM += ccm + ccm.transpose();
QQ(0,0) = ccm.trace();
QQ.block<1,3> (0,1) = D.transpose();
QQ.block<3,1> (1,0) = D;
QQ.block<3,3> (1,1) = RM;
Eigen::SelfAdjointEigenSolver<Eigen::Matrix4d> eig(QQ);
Eigen::Vector4d eval = eig.eigenvalues();
Eigen::Matrix4d evec = eig.eigenvectors();
// std::cout << "EigenVectors:" << std::endl << evec << std::endl;
// std::cout << "Eigenvalues:" << std::endl << eval << std::endl;
int ind;
eval.cwiseAbs().maxCoeff(&ind);
q=Quaternion<S>(evec.col(ind)[0],evec.col(ind)[1],evec.col(ind)[2],evec.col(ind)[3]);
Matrix44<S> Rot;
q.ToMatrix(Rot);
tr= (bfix - Rot*bmov);
}
/*! \brief Compute the roto-translation that applied to PMov bring them onto Pfix
* Rotation is computed as a quaternion.
*
* E.g. it find a matrix such that:
*
* Pfix[i] = res * Pmov[i]
*
* Ref:
* Besl, McKay
* A method for registration of 3d Shapes
* IEEE TPAMI Vol 14, No 2 1992
*/
template < class S >
void ComputeRigidMatchMatrix(std::vector<Point3<S> > &Pfix,
std::vector<Point3<S> > &Pmov,
Matrix44<S> &res)
{
Quaternion<S> q;
Point3<S> tr;
ComputeRigidMatchMatrix(Pfix,Pmov,q,tr);
Matrix44<S> Rot;
q.ToMatrix(Rot);
Matrix44<S> Trn;
Trn.SetTranslate(tr);
res=Trn*Rot;
}
/*! \brief Computes the best fitting rigid transformations to align two sets of corresponding points
*
* Ref:
* Olga Sorkine-Hornung and Michael Rabinovich
* Least-Squares Rigid Motion Using SVD
*/
template <class S>
Matrix44<S> ComputeLeastSquaresRigidMotion(std::vector<Point3<S> > &pFix,
std::vector<Point3<S> > &pMov)
{
if (pFix.size() != pMov.size() || pFix.size() < 3)
return Matrix44<S>::Identity();
Eigen::Matrix3Xd p(3, pMov.size()); // moving
Eigen::MatrixX3d q(pFix.size(), 3); // fixed
for (size_t i=0; i<pMov.size(); i++)
{
Eigen::Vector3d v;
pMov[i].ToEigenVector(v);
p.col(i) = v;
}
Eigen::Vector3d avgP = p.rowwise().mean();
p.colwise() -= avgP;
for (size_t i=0; i<pFix.size(); i++)
{
Eigen::Vector3d v;
pFix[i].ToEigenVector(v);
q.row(i) = v;
}
Eigen::Vector3d avgQ = q.colwise().mean();
q.rowwise() -= avgQ.transpose();
Eigen::Matrix3d cov = p * q;
Eigen::JacobiSVD<Eigen::Matrix3d> svd;
svd.compute(cov, Eigen::ComputeFullU | Eigen::ComputeFullV);
Eigen::Matrix3d d = Eigen::Matrix3d::Identity();
d(2,2) = (svd.matrixV() * svd.matrixU().transpose()).determinant() > 0 ? 1 : -1;
Eigen::Matrix3d R = (svd.matrixV() * d * svd.matrixU().transpose());
Eigen::Vector3d t = avgQ - R * avgP;
Eigen::Matrix4d res = Eigen::Matrix4d::Identity();
res.block<3,3>(0,0) = R;
res.block<3,1>(0,3) = t;
Matrix44<S> ret;
ret.FromEigenMatrix(res);
return ret;
}
/*
Compute a similarity matching (rigid + uniform scaling)
simply create a temporary point set with the correct scaling factor
*/
template <class S>
void ComputeSimilarityMatchMatrix(std::vector<Point3<S> > &Pfix,
std::vector<Point3<S> > &Pmov,
Matrix44<S> &res)
{
S scalingFactor=0;
for(size_t i=0;i<( Pmov.size()-1);++i)
{
scalingFactor += Distance(Pmov[i],Pmov[i+1])/ Distance(Pfix[i],Pfix[i+1]);
}
scalingFactor/=(Pmov.size()-1);
std::vector<Point3<S> > Pnew(Pmov.size());
for(size_t i=0;i<Pmov.size();++i)
Pnew[i]=Pmov[i]/scalingFactor;
ComputeRigidMatchMatrix(Pfix,Pnew,res);
Matrix44<S> scaleM; scaleM.SetDiagonal(1.0/scalingFactor);
res = res * scaleM;
}
} // end namespace
#endif
|