2004-10-15 15:44:09 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* VCGLib o o *
|
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-10-15 15:44:09 +02:00
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
|
* \ *
|
|
|
|
|
* All rights reserved. *
|
|
|
|
|
* *
|
2008-10-27 15:48:14 +01:00
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2004-10-15 15:44:09 +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. *
|
|
|
|
|
* *
|
|
|
|
|
****************************************************************************/
|
2006-09-21 20:09:34 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
#ifndef VCG_USE_EIGEN
|
|
|
|
|
#include "deprecated_matrix.h"
|
2006-04-11 10:09:35 +02:00
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
|
#else
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
|
|
|
|
#ifndef MATRIX_VCGLIB
|
|
|
|
|
#define MATRIX_VCGLIB
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
#include "eigen.h"
|
2006-04-11 10:09:35 +02:00
|
|
|
|
#include <vcg/space/point.h>
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2005-12-12 12:25:00 +01:00
|
|
|
|
namespace vcg{
|
2008-10-27 15:48:14 +01:00
|
|
|
|
namespace ndim{
|
|
|
|
|
template<class Scalar> class Matrix;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
namespace Eigen{
|
|
|
|
|
template<typename Scalar>
|
|
|
|
|
struct ei_traits<vcg::ndim::Matrix<Scalar> > : ei_traits<Eigen::Matrix<Scalar,Dynamic,Dynamic> > {};
|
2008-10-29 01:05:44 +01:00
|
|
|
|
template<typename XprType> struct ei_to_vcgtype<XprType,Dynamic,Dynamic,RowMajor,Dynamic,Dynamic>
|
|
|
|
|
{ typedef vcg::ndim::Matrix<typename XprType::Scalar> type; };
|
2008-10-27 15:48:14 +01:00
|
|
|
|
}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
namespace vcg{
|
|
|
|
|
namespace ndim{
|
|
|
|
|
|
|
|
|
|
/** \addtogroup math */
|
|
|
|
|
/* @{ */
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
2008-10-29 01:05:44 +01:00
|
|
|
|
* \deprecated use Matrix<Scalar,Rows,Cols> or Matrix<Scalar,Dynamic,Dynamic> or any typedef
|
2008-10-27 15:48:14 +01:00
|
|
|
|
* This class represent a generic <I>m</I><EFBFBD><I>n</I> matrix. The class is templated over the scalar type field.
|
|
|
|
|
* @param Scalar (Templete Parameter) Specifies the ScalarType field.
|
|
|
|
|
*/
|
|
|
|
|
template<class _Scalar>
|
2008-10-27 20:35:17 +01:00
|
|
|
|
class Matrix : public Eigen::Matrix<_Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> // FIXME col or row major ?
|
2008-10-27 15:48:14 +01:00
|
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
|
typedef Eigen::Matrix<_Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> _Base;
|
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
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
public:
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
_EIGEN_GENERIC_PUBLIC_INTERFACE(Matrix,_Base);
|
|
|
|
|
typedef _Scalar ScalarType;
|
|
|
|
|
VCG_EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Matrix)
|
2006-09-21 20:09:34 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Default constructor
|
|
|
|
|
* All the elements are initialized to zero.
|
|
|
|
|
* \param m the number of matrix rows
|
|
|
|
|
* \param n the number of matrix columns
|
|
|
|
|
*/
|
2008-10-27 20:35:17 +01:00
|
|
|
|
Matrix(int m, int n)
|
2008-10-27 15:48:14 +01:00
|
|
|
|
: Base(m,n)
|
|
|
|
|
{
|
|
|
|
|
memset(Base::data(), 0, m*n*sizeof(Scalar));
|
|
|
|
|
}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Constructor
|
|
|
|
|
* The matrix elements are initialized with the values of the elements in \i values.
|
|
|
|
|
* \param m the number of matrix rows
|
|
|
|
|
* \param n the number of matrix columns
|
|
|
|
|
* \param values the values of the matrix elements
|
|
|
|
|
*/
|
2008-10-27 20:35:17 +01:00
|
|
|
|
Matrix(int m, int n, Scalar *values)
|
|
|
|
|
: Base(m,n)
|
2008-10-27 15:48:14 +01:00
|
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
|
*this = Eigen::Map<Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> >(values, m , n);
|
2008-10-27 15:48:14 +01:00
|
|
|
|
}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Empty constructor
|
|
|
|
|
* Just create the object
|
|
|
|
|
*/
|
|
|
|
|
Matrix() : Base() {}
|
2004-10-18 14:16:57 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Copy constructor
|
|
|
|
|
* The matrix elements are initialized with the value of the corresponding element in \i m
|
|
|
|
|
* \param m the matrix to be copied
|
|
|
|
|
*/
|
|
|
|
|
Matrix(const Matrix<Scalar> &m) : Base(m) {}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Matrix(const Eigen::MatrixBase<OtherDerived> &m) : Base(m) {}
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Default destructor
|
|
|
|
|
*/
|
|
|
|
|
~Matrix() {}
|
2005-02-16 12:11:12 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* \deprecated use *this.row(i)
|
|
|
|
|
* Subscript operator:
|
|
|
|
|
* \param i the index of the row
|
|
|
|
|
* \return a reference to the <I>i</I>-th matrix row
|
|
|
|
|
*/
|
|
|
|
|
inline typename Base::RowXpr operator[](const unsigned int i)
|
|
|
|
|
{ return Base::row(i); }
|
2005-02-16 12:11:12 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* \deprecated use *this.row(i)
|
|
|
|
|
* Const subscript operator
|
|
|
|
|
* \param i the index of the row
|
|
|
|
|
* \return a reference to the <I>i</I>-th matrix row
|
|
|
|
|
*/
|
|
|
|
|
inline const typename Base::RowXpr operator[](const unsigned int i) const
|
|
|
|
|
{ return Base::row(i); }
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
|
|
|
|
* Matrix multiplication: calculates the cross product.
|
|
|
|
|
* \param reference to the matrix to multiply by
|
|
|
|
|
* \return the matrix product
|
|
|
|
|
*/
|
2008-10-28 12:47:37 +01:00
|
|
|
|
// FIXME what the hell is that !
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*template <int N,int M>
|
|
|
|
|
void DotProduct(Point<N,Scalar> &m,Point<M,Scalar> &result)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i, j, p, r;
|
|
|
|
|
for (i=0, p=0, r=0; i<M; i++)
|
|
|
|
|
{ result[i]=0;
|
|
|
|
|
for (j=0; j<N; j++)
|
|
|
|
|
result[i]+=(*this)[i][j]*m[j];
|
|
|
|
|
}
|
|
|
|
|
};*/
|
2004-10-15 15:44:09 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*!
|
2008-10-28 12:47:37 +01:00
|
|
|
|
* \deprecated use *this.resize(); *this.setZero();
|
2008-10-27 15:48:14 +01:00
|
|
|
|
* Resize the current matrix.
|
|
|
|
|
* \param m the number of matrix rows.
|
|
|
|
|
* \param n the number of matrix columns.
|
|
|
|
|
*/
|
|
|
|
|
void Resize(const unsigned int m, const unsigned int n)
|
|
|
|
|
{
|
|
|
|
|
assert(m>=2);
|
|
|
|
|
assert(n>=2);
|
|
|
|
|
Base::resize(m,n);
|
|
|
|
|
memset(Base::data(), 0, m*n*sizeof(Scalar));
|
|
|
|
|
};
|
|
|
|
|
};
|
2006-04-29 12:26:04 +02:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
typedef vcg::ndim::Matrix<double> MatrixMNd;
|
|
|
|
|
typedef vcg::ndim::Matrix<float> MatrixMNf;
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
/*! @} */
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
template <class MatrixType>
|
|
|
|
|
void Invert(MatrixType & m)
|
|
|
|
|
{
|
|
|
|
|
m = m.inverse();
|
|
|
|
|
}
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
2008-10-27 15:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
} // end of namespace
|
2005-12-12 12:25:00 +01:00
|
|
|
|
|
2006-04-11 10:09:35 +02:00
|
|
|
|
#endif
|
2008-10-27 20:35:17 +01:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|