2004-05-28 15:09:05 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2008-10-24 14:20:44 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2004-05-28 15:09:05 +02:00
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
#ifndef VCG_USE_EIGEN
|
2008-10-27 15:48:14 +01:00
|
|
|
#include "deprecated_matrix33.h"
|
2008-10-27 20:35:17 +01:00
|
|
|
#else
|
2004-05-28 15:09:05 +02:00
|
|
|
|
|
|
|
#ifndef __VCGLIB_MATRIX33_H
|
|
|
|
#define __VCGLIB_MATRIX33_H
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
#include "eigen.h"
|
2008-10-27 20:35:17 +01:00
|
|
|
#include "matrix44.h"
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
namespace vcg{
|
|
|
|
template<class Scalar> class Matrix33;
|
|
|
|
}
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
namespace Eigen{
|
|
|
|
template<typename Scalar>
|
|
|
|
struct ei_traits<vcg::Matrix33<Scalar> > : ei_traits<Eigen::Matrix<Scalar,3,3,RowMajor> > {};
|
2008-10-29 01:05:44 +01:00
|
|
|
template<typename XprType> struct ei_to_vcgtype<XprType,3,3,RowMajor,3,3>
|
|
|
|
{ typedef vcg::Matrix33<typename XprType::Scalar> type; };
|
2008-10-27 15:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace vcg {
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2008-10-29 01:05:44 +01:00
|
|
|
/** \deprecated use Matrix<Scalar,3,3>
|
|
|
|
@name Matrix33
|
2004-05-28 15:09:05 +02:00
|
|
|
Class Matrix33.
|
2008-10-24 14:20:44 +02:00
|
|
|
This is the class for definition of a matrix 3x3.
|
2004-05-28 15:09:05 +02:00
|
|
|
@param S (Templete Parameter) Specifies the ScalarType field.
|
|
|
|
*/
|
2008-10-27 15:48:14 +01:00
|
|
|
template<class _Scalar>
|
2008-10-27 20:35:17 +01:00
|
|
|
class Matrix33 : public Eigen::Matrix<_Scalar,3,3,Eigen::RowMajor> // FIXME col or row major ?
|
2004-05-28 15:09:05 +02:00
|
|
|
{
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
typedef Eigen::Matrix<_Scalar,3,3,Eigen::RowMajor> _Base;
|
2008-10-29 12:29:57 +01:00
|
|
|
public:
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
using _Base::coeff;
|
|
|
|
using _Base::coeffRef;
|
|
|
|
using _Base::setZero;
|
2005-09-29 11:53:58 +02:00
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
_EIGEN_GENERIC_PUBLIC_INTERFACE(Matrix33,_Base);
|
2008-10-27 15:48:14 +01:00
|
|
|
typedef _Scalar ScalarType;
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
VCG_EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Matrix33)
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/// Default constructor
|
|
|
|
inline Matrix33() : Base() {}
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/// Copy constructor
|
|
|
|
Matrix33(const Matrix33& m ) : Base(m) {}
|
2006-01-20 17:41:44 +01:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/// create from a \b row-major array
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33(const Scalar * v ) : Base(Eigen::Map<Eigen::Matrix<Scalar,3,3,Eigen::RowMajor> >(v)) {}
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/// create from Matrix44 excluding row and column k
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33(const Matrix44<Scalar> & m, const int & k) : Base(m.minor(k,k)) {}
|
|
|
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
Matrix33(const Eigen::MatrixBase<OtherDerived>& other) : Base(other) {}
|
2005-05-23 17:05:26 +02:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
/*! \deprecated use *this.row(i) */
|
2008-10-27 15:48:14 +01:00
|
|
|
inline typename Base::RowXpr operator[](const unsigned int i)
|
|
|
|
{ return Base::row(i); }
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
/*! \deprecated use *this.row(i) */
|
2008-10-27 15:48:14 +01:00
|
|
|
inline const typename Base::RowXpr operator[](const unsigned int i) const
|
|
|
|
{ return Base::row(i); }
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/** \deprecated */
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33 & SetRotateRad(Scalar angle, const Point3<Scalar> & axis )
|
2004-05-28 15:09:05 +02:00
|
|
|
{
|
2008-10-27 15:48:14 +01:00
|
|
|
*this = Eigen::AngleAxis<Scalar>(angle,axis).toRotationMatrix();
|
2008-08-12 19:31:11 +02:00
|
|
|
return (*this);
|
2004-05-28 15:09:05 +02:00
|
|
|
}
|
2008-10-27 15:48:14 +01:00
|
|
|
/** \deprecated */
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33 & SetRotateDeg(Scalar angle, const Point3<Scalar> & axis ){
|
2008-08-14 12:02:07 +02:00
|
|
|
return SetRotateRad(math::ToRad(angle),axis);
|
2008-08-12 19:31:11 +02:00
|
|
|
}
|
|
|
|
|
2005-11-14 11:28:25 +01:00
|
|
|
// Warning, this Inversion code can be HIGHLY NUMERICALLY UNSTABLE!
|
|
|
|
// In most case you are advised to use the Invert() method based on SVD decomposition.
|
2008-10-27 15:48:14 +01:00
|
|
|
/** \deprecated */
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33 & FastInvert() { return *this = Base::inverse(); }
|
2005-11-14 11:28:25 +01:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
void show(FILE * fp)
|
2004-05-28 15:09:05 +02:00
|
|
|
{
|
2008-10-27 15:48:14 +01:00
|
|
|
for(int i=0;i<3;++i)
|
|
|
|
printf("| %g \t%g \t%g |\n",coeff(i,0),coeff(i,1),coeff(i,2));
|
2004-05-28 15:09:05 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 01:05:44 +01:00
|
|
|
/** \deprecated use a * b.transpose()
|
2008-10-27 15:48:14 +01:00
|
|
|
compute the matrix generated by the product of a * b^T
|
|
|
|
*/
|
2008-10-29 01:05:44 +01:00
|
|
|
// hm.... this is the outer product
|
|
|
|
void ExternalProduct(const Point3<Scalar> &a, const Point3<Scalar> &b) { *this = a * b.transpose(); }
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-29 01:05:44 +01:00
|
|
|
/** Compute the Frobenius Norm of the Matrix */
|
2008-10-27 20:35:17 +01:00
|
|
|
Scalar Norm() { return Base::cwise().abs2().sum(); }
|
2008-08-07 20:33:23 +02:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
/** Computes the covariance matrix of a set of 3d points. Returns the barycenter.
|
2008-10-27 15:48:14 +01:00
|
|
|
*/
|
|
|
|
// FIXME should be outside Matrix
|
2012-10-22 20:34:08 +02:00
|
|
|
|
2008-08-07 20:33:23 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
/**
|
|
|
|
It computes the cross covariance matrix of two set of 3d points P and X;
|
|
|
|
it returns also the barycenters of P and X.
|
|
|
|
fonte:
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
Besl, McKay
|
|
|
|
A method for registration o f 3d Shapes
|
|
|
|
IEEE TPAMI Vol 14, No 2 1992
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
*/
|
|
|
|
// FIXME should be outside Matrix
|
|
|
|
template <class STLPOINTCONTAINER >
|
|
|
|
void CrossCovariance(const STLPOINTCONTAINER &P, const STLPOINTCONTAINER &X,
|
2008-10-27 20:35:17 +01:00
|
|
|
Point3<Scalar> &bp, Point3<Scalar> &bx)
|
2008-10-27 15:48:14 +01:00
|
|
|
{
|
|
|
|
setZero();
|
|
|
|
assert(P.size()==X.size());
|
|
|
|
bx.setZero();
|
|
|
|
bp.setZero();
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33<Scalar> tmp;
|
|
|
|
typename std::vector <Point3<Scalar> >::const_iterator pi,xi;
|
2008-10-27 15:48:14 +01:00
|
|
|
for(pi=P.begin(),xi=X.begin();pi!=P.end();++pi,++xi){
|
|
|
|
bp+=*pi;
|
|
|
|
bx+=*xi;
|
|
|
|
tmp.ExternalProduct(*pi,*xi);
|
|
|
|
(*this)+=tmp;
|
|
|
|
}
|
|
|
|
bp/=P.size();
|
|
|
|
bx/=X.size();
|
|
|
|
(*this)/=P.size();
|
|
|
|
tmp.ExternalProduct(bp,bx);
|
|
|
|
(*this)-=tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class STLPOINTCONTAINER, class STLREALCONTAINER>
|
|
|
|
void WeightedCrossCovariance(const STLREALCONTAINER & weights,
|
|
|
|
const STLPOINTCONTAINER &P,
|
|
|
|
const STLPOINTCONTAINER &X,
|
2008-10-27 20:35:17 +01:00
|
|
|
Point3<Scalar> &bp,
|
|
|
|
Point3<Scalar> &bx)
|
2008-10-27 15:48:14 +01:00
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
setZero();
|
2008-10-27 15:48:14 +01:00
|
|
|
assert(P.size()==X.size());
|
|
|
|
bx.SetZero();
|
|
|
|
bp.SetZero();
|
2008-10-27 20:35:17 +01:00
|
|
|
Matrix33<Scalar> tmp;
|
|
|
|
typename std::vector <Point3<Scalar> >::const_iterator pi,xi;
|
2008-10-27 15:48:14 +01:00
|
|
|
typename STLREALCONTAINER::const_iterator pw;
|
|
|
|
|
|
|
|
for(pi=P.begin(),xi=X.begin();pi!=P.end();++pi,++xi){
|
|
|
|
bp+=(*pi);
|
|
|
|
bx+=(*xi);
|
|
|
|
}
|
|
|
|
bp/=P.size();
|
|
|
|
bx/=X.size();
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
for(pi=P.begin(),xi=X.begin(),pw = weights.begin();pi!=P.end();++pi,++xi,++pw){
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
tmp.ExternalProduct(((*pi)-(bp)),((*xi)-(bp)));
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
(*this)+=tmp*(*pw);
|
|
|
|
}
|
2004-05-28 15:09:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-09-29 11:53:58 +02:00
|
|
|
template <class S>
|
2008-10-27 15:48:14 +01:00
|
|
|
void Invert(Matrix33<S> &m) { m = m.lu().inverse(); }
|
2005-09-29 11:53:58 +02:00
|
|
|
|
|
|
|
template <class S>
|
2008-10-27 15:48:14 +01:00
|
|
|
Matrix33<S> Inverse(const Matrix33<S>&m) { return m.lu().inverse(); }
|
2004-05-28 15:09:05 +02:00
|
|
|
|
2007-04-08 01:06:47 +02:00
|
|
|
///given 2 vector centered into origin calculate the rotation matrix from first to the second
|
|
|
|
template <class S>
|
|
|
|
Matrix33<S> RotationMatrix(vcg::Point3<S> v0,vcg::Point3<S> v1,bool normalized=true)
|
|
|
|
{
|
|
|
|
typedef typename vcg::Point3<S> CoordType;
|
|
|
|
Matrix33<S> rotM;
|
|
|
|
const S epsilon=0.00001;
|
|
|
|
if (!normalized)
|
|
|
|
{
|
|
|
|
v0.Normalize();
|
|
|
|
v1.Normalize();
|
|
|
|
}
|
make point2 derived Eigen's Matrix, and a set of minimal fixes to make meshlab compile
with both old and new version. The fixes include:
- dot product: vec0 * vec1 => vec0.dot(vec1) (I added .dot() to the old Point classes too)
- Transpose: Transpose is an Eigen type, so we cannot keep it if Eigen is used. Therefore
I added a .tranpose() to old matrix classes, and modified most of the Transpose() to transpose()
both in vcg and meshlab. In fact, transpose() are free with Eigen, it simply returns a transpose
expression without copies. On the other be carefull: m = m.transpose() won't work as expected,
here me must evaluate to a temporary: m = m.transpose().eval(); However, this operation in very
rarely needed: you transpose at the same sime you set m, or you use m.transpose() directly.
- the last issue is Normalize which both modifies *this and return a ref to it. This behavior
don't make sense anymore when using expression template, e.g., in (a+b).Normalize(), the type
of a+b if not a Point (or whatever Vector types), it an expression of the addition of 2 points,
so we cannot modify the value of *this, since there is no value. Therefore I've already changed
all those .Normalize() of expressions to the Eigen's version .normalized().
- Finally I've changed the Zero to SetZero in the old Point classes too.
2008-10-28 01:59:46 +01:00
|
|
|
S dot=v0.dot(v1);
|
2007-04-08 01:06:47 +02:00
|
|
|
///control if there is no rotation
|
|
|
|
if (dot>((S)1-epsilon))
|
|
|
|
{
|
|
|
|
rotM.SetIdentity();
|
|
|
|
return rotM;
|
|
|
|
}
|
|
|
|
|
2008-10-24 14:20:44 +02:00
|
|
|
///find the axis of rotation
|
2007-04-08 01:06:47 +02:00
|
|
|
CoordType axis;
|
|
|
|
axis=v0^v1;
|
|
|
|
axis.Normalize();
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2007-04-08 01:06:47 +02:00
|
|
|
///construct rotation matrix
|
|
|
|
S u=axis.X();
|
|
|
|
S v=axis.Y();
|
|
|
|
S w=axis.Z();
|
|
|
|
S phi=acos(dot);
|
|
|
|
S rcos = cos(phi);
|
|
|
|
S rsin = sin(phi);
|
2008-10-24 14:20:44 +02:00
|
|
|
|
2007-04-08 01:06:47 +02:00
|
|
|
rotM[0][0] = rcos + u*u*(1-rcos);
|
|
|
|
rotM[1][0] = w * rsin + v*u*(1-rcos);
|
|
|
|
rotM[2][0] = -v * rsin + w*u*(1-rcos);
|
|
|
|
rotM[0][1] = -w * rsin + u*v*(1-rcos);
|
|
|
|
rotM[1][1] = rcos + v*v*(1-rcos);
|
|
|
|
rotM[2][1] = u * rsin + w*v*(1-rcos);
|
|
|
|
rotM[0][2] = v * rsin + u*w*(1-rcos);
|
|
|
|
rotM[1][2] = -u * rsin + v*w*(1-rcos);
|
|
|
|
rotM[2][2] = rcos + w*w*(1-rcos);
|
|
|
|
|
|
|
|
return rotM;
|
|
|
|
}
|
|
|
|
|
2007-04-19 16:30:26 +02:00
|
|
|
///return the rotation matrix along axis
|
|
|
|
template <class S>
|
|
|
|
Matrix33<S> RotationMatrix(const vcg::Point3<S> &axis,
|
|
|
|
const float &angleRad)
|
|
|
|
{
|
|
|
|
vcg::Matrix44<S> matr44;
|
|
|
|
vcg::Matrix33<S> matr33;
|
|
|
|
matr44.SetRotate(angleRad,axis);
|
|
|
|
for (int i=0;i<3;i++)
|
|
|
|
for (int j=0;j<3;j++)
|
|
|
|
matr33[i][j]=matr44[i][j];
|
|
|
|
return matr33;
|
|
|
|
}
|
2007-04-08 01:06:47 +02:00
|
|
|
|
2008-01-03 18:40:17 +01:00
|
|
|
/// return a random rotation matrix, from the paper:
|
|
|
|
/// Fast Random Rotation Matrices, James Arvo
|
|
|
|
/// Graphics Gems III pp. 117-120
|
|
|
|
template <class S>
|
|
|
|
Matrix33<S> RandomRotation(){
|
|
|
|
S x1,x2,x3;
|
|
|
|
Matrix33<S> R,H,M,vv;
|
|
|
|
Point3<S> v;
|
|
|
|
R.SetIdentity();
|
|
|
|
H.SetIdentity();
|
|
|
|
x1 = rand()/S(RAND_MAX);
|
|
|
|
x2 = rand()/S(RAND_MAX);
|
|
|
|
x3 = rand()/S(RAND_MAX);
|
|
|
|
|
|
|
|
R[0][0] = cos(S(2)*M_PI*x1);
|
|
|
|
R[0][1] = sin(S(2)*M_PI*x1);
|
|
|
|
R[1][0] = - R[0][1];
|
|
|
|
R[1][1] = R[0][0];
|
|
|
|
|
|
|
|
v[0] = cos(2.0 * M_PI * x2)*sqrt(x3);
|
|
|
|
v[1] = sin(2.0 * M_PI * x2)*sqrt(x3);
|
|
|
|
v[2] = sqrt(1-x3);
|
|
|
|
|
|
|
|
vv.OuterProduct(v,v);
|
|
|
|
H -= vv*S(2);
|
|
|
|
M = H*R*S(-1);
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2008-10-24 14:20:44 +02:00
|
|
|
///
|
2004-05-28 15:09:05 +02:00
|
|
|
typedef Matrix33<short> Matrix33s;
|
2008-10-27 20:35:17 +01:00
|
|
|
typedef Matrix33<int> Matrix33i;
|
2004-05-28 15:09:05 +02:00
|
|
|
typedef Matrix33<float> Matrix33f;
|
|
|
|
typedef Matrix33<double> Matrix33d;
|
|
|
|
|
|
|
|
} // end of namespace
|
|
|
|
|
|
|
|
#endif
|
2008-10-27 20:35:17 +01:00
|
|
|
|
|
|
|
#endif
|