46 #define EIGENTOOLS_DYNAMIC_MATRIX(Scalar) Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> 49 #define EIGENTOOLS_DYNAMIC_VECTOR(Scalar) Eigen::Matrix<Scalar, Eigen::Dynamic, 1> 52 #ifdef EIGENTOOLS_DISABLE_ALIGNMENT_OF_STATIC_MATRICES 53 #define EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE Eigen::DontAlign 55 #define EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE Eigen::AutoAlign 58 #define EIGENTOOLS_CONSTANT_SIZE_VECTOR(rows) Eigen::Matrix<etools::DefaultScalar, rows, 1, EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE> 59 #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR 61 #define EIGENTOOLS_CONSTANT_SIZE_MATRIX(rows, cols) Eigen::Matrix<etools::DefaultScalar, rows, cols, EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE> 63 #define EIGENTOOLS_CONSTANT_SIZE_MATRIX(rows, cols) Eigen::Matrix<etools::DefaultScalar, rows, cols, EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE | ((rows==1) ? (Eigen::RowMajor) : (Eigen::ColMajor))> 77 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 3)
Matrix3;
78 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 4)
Matrix4;
79 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 5)
Matrix5;
80 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 6)
Matrix6;
81 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 7)
Matrix7;
82 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 8)
Matrix8;
83 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 9)
Matrix9;
84 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(10, 10)
Matrix10;
86 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 1)
Matrix1x2;
87 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 3)
Matrix1x3;
88 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 4)
Matrix1x4;
89 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 5)
Matrix1x5;
90 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 6)
Matrix1x6;
91 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 7)
Matrix1x7;
92 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 8)
Matrix1x8;
93 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 9)
Matrix1x9;
94 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(1, 10)
Matrix1x10;
95 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 3)
Matrix2x3;
96 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 4)
Matrix2x4;
97 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 5)
Matrix2x5;
98 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 6)
Matrix2x6;
99 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 7)
Matrix2x7;
100 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 8)
Matrix2x8;
101 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(2, 9)
Matrix2x9;
103 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 2)
Matrix3x2;
104 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 4)
Matrix3x4;
105 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 5)
Matrix3x5;
106 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 6)
Matrix3x6;
107 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 7)
Matrix3x7;
108 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 8)
Matrix3x8;
109 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(3, 9)
Matrix3x9;
111 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 2)
Matrix4x2;
112 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 3)
Matrix4x3;
113 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 5)
Matrix4x5;
114 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 6)
Matrix4x6;
115 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 7)
Matrix4x7;
116 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 8)
Matrix4x8;
117 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(4, 9)
Matrix4x9;
119 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 2)
Matrix5x2;
120 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 3)
Matrix5x3;
121 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 4)
Matrix5x4;
122 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 6)
Matrix5x6;
123 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 7)
Matrix5x7;
124 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 8)
Matrix5x8;
125 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(5, 9)
Matrix5x9;
127 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 2)
Matrix6x2;
128 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 3)
Matrix6x3;
129 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 4)
Matrix6x4;
130 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 5)
Matrix6x5;
131 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 7)
Matrix6x7;
132 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 8)
Matrix6x8;
133 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(6, 9)
Matrix6x9;
135 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 2)
Matrix7x2;
136 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 3)
Matrix7x3;
137 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 4)
Matrix7x4;
138 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 5)
Matrix7x5;
139 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 6)
Matrix7x6;
140 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 8)
Matrix7x8;
141 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(7, 9)
Matrix7x9;
143 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 2)
Matrix8x2;
144 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 3)
Matrix8x3;
145 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 4)
Matrix8x4;
146 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 5)
Matrix8x5;
147 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 6)
Matrix8x6;
148 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 7)
Matrix8x7;
149 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(8, 9)
Matrix8x9;
151 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 2)
Matrix9x2;
152 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 3)
Matrix9x3;
153 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 4)
Matrix9x4;
154 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 5)
Matrix9x5;
155 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 6)
Matrix9x6;
156 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 7)
Matrix9x7;
157 typedef EIGENTOOLS_CONSTANT_SIZE_MATRIX(9, 8)
Matrix9x8;
177 template <typename t_Derived>
180 if (std::numeric_limits<etools::DefaultScalar>::has_quiet_NaN)
182 matrix.setConstant(std::numeric_limits<etools::DefaultScalar>::quiet_NaN());
202 template<
class t_DerivedOutput,
class t_DerivedInput>
204 getATA( Eigen::PlainObjectBase<t_DerivedOutput> &result,
205 const Eigen::DenseBase<t_DerivedInput> &A)
207 std::ptrdiff_t num_el = A.cols();
208 result.resize(num_el,num_el);
209 for (std::ptrdiff_t i = 0; i < num_el; ++i)
211 result.block(i, i, num_el-i, 1).noalias() = A.transpose().bottomRows(num_el-i) * A.col(i);
229 template<
class t_DerivedOutput,
class t_DerivedInput>
231 getATA( Eigen::PlainObjectBase<t_DerivedOutput> &result,
232 const Eigen::DenseBase<t_DerivedInput> &A,
233 const std::ptrdiff_t offset,
234 const std::ptrdiff_t num_el)
236 std::ptrdiff_t A_num_col = A.cols();
237 result.resize(num_el, num_el);
238 result.template triangularView<Eigen::Lower>().setZero();
239 for (std::ptrdiff_t i = 0; i < A_num_col; ++i)
241 result.block( offset,
244 A_num_col).block(i, i, A_num_col-i, 1).noalias() = A.transpose().bottomRows(A_num_col-i) * A.col(i);
260 template<
class t_DerivedOutput,
class t_DerivedInput>
262 addATA( Eigen::DenseBase<t_DerivedOutput> &result,
263 const Eigen::DenseBase<t_DerivedInput> &A)
265 std::ptrdiff_t num_el = A.cols();
266 for (std::ptrdiff_t i = 0; i < num_el; ++i)
268 result.block(i, i, num_el-i, 1).noalias() += A.transpose().bottomRows(num_el-i) * A.col(i);
285 template<
class t_DerivedOutput,
class t_DerivedInput>
287 addATA( Eigen::DenseBase<t_DerivedOutput> &result,
288 const Eigen::DenseBase<t_DerivedInput> &A,
289 const std::ptrdiff_t offset)
291 std::ptrdiff_t A_num_col = A.cols();
292 for (std::ptrdiff_t i = 0; i < A_num_col; ++i)
294 result.block( offset,
297 A_num_col).block(i, i, A_num_col-i, 1).noalias() += A.transpose().bottomRows(A_num_col-i) * A.col(i);
310 template<
class t_Derived>
314 std::ptrdiff_t num_el = matrix.cols();
315 for (std::ptrdiff_t i = 0; i < num_el-1; ++i)
317 matrix.block(i, i+1, 1, num_el-i-1) = matrix.transpose().block(i, i+1, 1, num_el-i-1);
336 template <
class t_DerivedMatrix,
class t_DerivedRotation,
class t_DerivedTranslation>
337 inline Eigen::Matrix< typename Eigen::MatrixBase<t_DerivedMatrix>::Scalar,
338 Eigen::MatrixBase<t_DerivedMatrix>::RowsAtCompileTime,
339 Eigen::MatrixBase<t_DerivedMatrix>::ColsAtCompileTime>
341 transform(
const Eigen::MatrixBase<t_DerivedMatrix> &matrix,
342 const Eigen::MatrixBase<t_DerivedRotation> &rotation,
343 const Eigen::MatrixBase<t_DerivedTranslation> &translation)
345 return ((rotation*matrix).colwise() + translation);
362 template<
typename t_Scalar,
368 makeBlockDiagonal(
const std::vector<Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> > &input_matrices)
370 switch (input_matrices.size())
376 return (input_matrices[0]);
380 std::ptrdiff_t row_size = 0;
381 std::ptrdiff_t col_size = 0;
383 for(std::size_t i = 0; i < input_matrices.size(); ++i)
385 row_size += input_matrices[i].rows();
386 col_size += input_matrices[i].cols();
389 output.setZero(row_size, col_size);
392 std::ptrdiff_t cumulative_row = 0;
393 std::ptrdiff_t cumulative_col = 0;
395 for(std::size_t i = 0; i < input_matrices.size(); ++i)
397 int current_num_rows = input_matrices[i].rows();
398 int current_num_cols = input_matrices[i].cols();
400 output.block(cumulative_row, cumulative_col, current_num_rows, current_num_cols) = input_matrices[i];
402 cumulative_row += current_num_rows;
403 cumulative_col += current_num_cols;
421 template<
class t_DerivedInput>
425 const std::ptrdiff_t num_copies)
433 return (input_matrix);
439 output.setZero(num_copies * input_matrix.rows(),
440 num_copies * input_matrix.cols());
443 for(std::ptrdiff_t i = 0; i < num_copies; ++i)
445 output.block(i*input_matrix.rows(), i*input_matrix.cols(),
446 input_matrix.rows(), input_matrix.cols()) = input_matrix;
466 template<
class t_DerivedOutput,
473 const std::vector<Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> > &matrices)
475 std::vector< Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> > nonempty_matrices = matrices;
476 std::size_t number_of_matrices = nonempty_matrices.size();
478 if (number_of_matrices > 0)
480 std::ptrdiff_t total_number_of_rows = 0;
481 std::ptrdiff_t number_of_cols = 0;
483 for(std::size_t i = 0; i < number_of_matrices; ++i)
485 if(!nonempty_matrices[i].size())
487 nonempty_matrices.erase(nonempty_matrices.begin() + i);
488 --number_of_matrices;
492 if(!number_of_matrices)
498 number_of_cols = nonempty_matrices[0].cols();
499 for (std::size_t i = 0; i < number_of_matrices; ++i)
501 EIGENTOOLS_ASSERT(number_of_cols == nonempty_matrices[i].cols(),
"Inconsistent size of input matrices.");
502 total_number_of_rows += nonempty_matrices[i].rows();
505 result.resize(total_number_of_rows, number_of_cols);
507 std::ptrdiff_t row_index = 0;
508 for (std::size_t i = 0; i < number_of_matrices; ++i)
510 result.block(row_index, 0, nonempty_matrices[i].rows(), number_of_cols) = nonempty_matrices[i];
511 row_index += nonempty_matrices[i].rows();
532 template<
class t_DerivedOutput,
533 class t_DerivedInput1,
534 class t_DerivedInput2>
537 Eigen::PlainObjectBase<t_DerivedOutput> &result,
538 const Eigen::DenseBase<t_DerivedInput1> &matrix1,
539 const Eigen::DenseBase<t_DerivedInput2> &matrix2)
541 if (matrix1.rows() == 0)
547 if (matrix2.rows() == 0)
553 std::ptrdiff_t number_of_rows = matrix1.rows();
554 std::ptrdiff_t number_of_cols = matrix1.cols() + matrix2.cols();
556 EIGENTOOLS_ASSERT(number_of_rows == matrix2.rows(),
"Inconsistent size of input matrices.");
558 result.resize(number_of_rows, number_of_cols);
559 result << matrix1, matrix2;
579 template<
class t_DerivedOutput,
580 class t_DerivedInput1,
581 class t_DerivedInput2,
582 class t_DerivedInput3>
585 Eigen::PlainObjectBase<t_DerivedOutput> &result,
586 const Eigen::DenseBase<t_DerivedInput1> &matrix1,
587 const Eigen::DenseBase<t_DerivedInput2> &matrix2,
588 const Eigen::DenseBase<t_DerivedInput3> &matrix3)
590 if (matrix1.rows() == 0)
596 if (matrix2.rows() == 0)
602 if (matrix3.rows() == 0)
608 std::ptrdiff_t number_of_rows = matrix1.rows();
609 std::ptrdiff_t number_of_cols = matrix1.cols() + matrix2.cols() + matrix3.cols();
612 && (number_of_rows == matrix3.rows()),
613 "Inconsistent size of input matrices.");
615 result.resize(number_of_rows, number_of_cols);
616 result << matrix1, matrix2, matrix3;
635 template<
class t_Derived>
638 const std::ptrdiff_t row_to_remove)
640 EIGENTOOLS_ASSERT(row_to_remove <= matrix.rows(),
"The index of a removed row is greater than the size of the matrix.");
642 std::ptrdiff_t number_of_rows = matrix.rows()-1;
643 std::ptrdiff_t number_of_cols = matrix.cols();
645 if( row_to_remove < number_of_rows )
647 matrix.block(row_to_remove, 0, number_of_rows - row_to_remove, number_of_cols) =
648 matrix.block(row_to_remove + 1, 0 , number_of_rows - row_to_remove, number_of_cols);
651 matrix.conservativeResize(number_of_rows, number_of_cols);
667 template<
class t_Derived>
670 const std::ptrdiff_t column_to_remove)
672 EIGENTOOLS_ASSERT(column_to_remove <= matrix.rows(),
"The index of a removed column is greater than the size of the matrix.");
674 std::ptrdiff_t number_of_rows = matrix.rows();
675 std::ptrdiff_t number_of_cols = matrix.cols()-1;
677 if( column_to_remove < number_of_cols )
679 matrix.block(0, column_to_remove, number_of_rows, number_of_cols - column_to_remove) =
680 matrix.block(0, column_to_remove + 1 ,number_of_rows, number_of_cols - column_to_remove);
683 matrix.conservativeResize(number_of_rows, number_of_cols);
700 template<
class t_Derived>
704 const std::size_t input_block_rows,
705 const std::size_t input_block_cols,
706 const std::size_t identity_size)
713 std::size_t num_blocks_vert = input.rows() / input_block_rows;
714 std::size_t num_blocks_hor = input.cols() / input_block_cols;
716 std::size_t output_block_rows = identity_size*input_block_rows;
717 std::size_t output_block_cols = identity_size*input_block_cols;
719 output.setZero(identity_size * input.rows(),
720 identity_size * input.cols());
722 for (std::size_t i = 0; i < num_blocks_vert; ++i)
724 for (std::size_t j = 0; j < num_blocks_hor; ++j)
726 for(std::size_t k = 0; k < identity_size; ++k)
728 output.block( i*output_block_rows + k*input_block_rows,
729 j*output_block_cols + k*input_block_cols,
732 = input.block(i*input_block_rows, j*input_block_cols, input_block_rows, input_block_cols);
758 template<
class t_Derived>
759 inline Eigen::Map< const EIGENTOOLS_DYNAMIC_MATRIX(typename Eigen::PlainObjectBase<t_Derived>::Scalar),
761 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> >
764 const std::size_t row_step,
765 const std::size_t first_row = 0)
768 matrix.data() + first_row,
769 ceil( static_cast<double> (matrix.rows() - first_row)/row_step),
771 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(matrix.rows(), row_step)));
780 template<
class t_Derived>
781 friend Eigen::Map< const EIGENTOOLS_DYNAMIC_MATRIX( typename Eigen::PlainObjectBase<t_Derived>::Scalar ),
783 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> >
785 const Eigen::PlainObjectBase<t_Derived> &);
800 const std::size_t first_index)
802 step_size_ = step_size;
803 first_index_ = first_index;
818 template<
class t_Derived>
819 inline Eigen::Map< const EIGENTOOLS_DYNAMIC_MATRIX( typename Eigen::PlainObjectBase<t_Derived>::Scalar ),
821 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> >
824 const Eigen::PlainObjectBase<t_Derived> & matrix)
830 #ifdef EIGENTOOLS_ENABLE_EIGENTYPE_DETECTION 831 #include <boost/utility/enable_if.hpp> 832 #include <boost/mpl/has_xxx.hpp> 833 #include <boost/mpl/and.hpp> 837 BOOST_MPL_HAS_XXX_TRAIT_DEF(Scalar)
838 BOOST_MPL_HAS_XXX_TRAIT_DEF(Index)
839 BOOST_MPL_HAS_XXX_TRAIT_DEF(StorageKind)
856 struct is_eigen_type : boost::mpl::and_<detail::has_Scalar<T>,
857 detail::has_Index<T>,
858 detail::has_StorageKind<T> >
862 #define EIGENTOOLS_EIGENTYPE_ENABLER_TYPE(DataType) \ 863 const typename boost::enable_if_c< etools::is_eigen_type<DataType>::value >::type 864 #define EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(DataType) \ 865 const typename boost::enable_if_c< !(etools::is_eigen_type<DataType>::value) >::type