add matrix constructor taking an array. update unit-tests.

This commit is contained in:
Benoit Jacob 2007-12-18 08:56:18 +00:00
parent 53040f53d9
commit 2c656c51e6
4 changed files with 21 additions and 10 deletions

View File

@ -36,7 +36,7 @@ template<typename MatrixType> class Map
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime, static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ColsAtCompileTime = MatrixType::ColsAtCompileTime; ColsAtCompileTime = MatrixType::ColsAtCompileTime;
Map(int rows, int cols, Scalar* array) : m_rows(rows), m_cols(cols), m_data(array) Map(Scalar* data, int rows, int cols) : m_data(data), m_rows(rows), m_cols(cols)
{ {
assert(rows > 0 && cols > 0); assert(rows > 0 && cols > 0);
} }
@ -59,14 +59,22 @@ template<typename MatrixType> class Map
} }
protected: protected:
int m_rows, m_cols;
Scalar* m_data; Scalar* m_data;
int m_rows, m_cols;
}; };
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
Map<Derived> MatrixBase<Scalar, Derived>::map(const Scalar* array, int rows, int cols) Map<Derived> MatrixBase<Scalar, Derived>::map(const Scalar* data, int rows, int cols)
{ {
return Map<Derived>(rows, cols, const_cast<Scalar*>(array)); return Map<Derived>(const_cast<Scalar*>(data),rows, cols);
}
template<typename _Scalar, int _Rows, int _Cols>
Matrix<_Scalar, _Rows, _Cols>
::Matrix(const Scalar *data, int rows, int cols)
: Storage(rows, cols)
{
*this = Map<Matrix>(const_cast<Scalar*>(data), rows, cols);
} }
#endif // EIGEN_FROMARRAY_H #endif // EIGEN_FROMARRAY_H

View File

@ -144,6 +144,8 @@ class Matrix : public MatrixBase<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
(Storage::m_data)[2] = z; (Storage::m_data)[2] = z;
(Storage::m_data)[3] = w; (Storage::m_data)[3] = w;
} }
explicit Matrix(const Scalar *data, int rows = RowsAtCompileTime,
int cols = ColsAtCompileTime);
template<typename OtherDerived> template<typename OtherDerived>
Matrix(const MatrixBase<Scalar, OtherDerived>& other) Matrix(const MatrixBase<Scalar, OtherDerived>& other)

View File

@ -195,7 +195,6 @@ template<typename Scalar, typename Derived> class MatrixBase
Scalar& z() { return coeffRef(2, UserDebugging); } Scalar& z() { return coeffRef(2, UserDebugging); }
Scalar& w() { return coeffRef(3, UserDebugging); } Scalar& w() { return coeffRef(3, UserDebugging); }
Eval<Derived> eval() const EIGEN_ALWAYS_INLINE; Eval<Derived> eval() const EIGEN_ALWAYS_INLINE;
}; };

View File

@ -150,12 +150,14 @@ template<typename MatrixType> void basicStuff(const MatrixType& m)
// test Map.h // test Map.h
Scalar* array1 = new Scalar[rows]; Scalar* array1 = new Scalar[rows];
Scalar* array2 = new Scalar[rows]; Scalar* array2 = new Scalar[rows];
Matrix<Scalar, Dynamic, 1>::map(array1, rows) = Matrix<Scalar, Dynamic, 1>::random(rows); typedef Matrix<Scalar, Dynamic, 1> VectorX;
Matrix<Scalar, Dynamic, 1>::map(array2, rows) VectorX::map(array1, rows) = VectorX::random(rows);
= Matrix<Scalar, Dynamic, 1>::map(array1, rows); VectorX::map(array2, rows) = VectorX::map(array1, rows);
Matrix<Scalar, Dynamic, 1> ma1 = Matrix<Scalar, Dynamic, 1>::map(array1, rows); VectorX ma1 = VectorX::map(array1, rows);
Matrix<Scalar, Dynamic, 1> ma2 = Matrix<Scalar, Dynamic, 1>::map(array2, rows); VectorX ma2 = VectorX::map(array2, rows);
VERIFY_IS_APPROX(ma1, ma2); VERIFY_IS_APPROX(ma1, ma2);
VERIFY_IS_APPROX(ma1, VectorX(array2, rows));
delete[] array1; delete[] array1;
delete[] array2; delete[] array2;
} }