| 1 | // This file is part of Eigen, a lightweight C++ template library | 
|---|
| 2 | // for linear algebra. | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> | 
|---|
| 5 | // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> | 
|---|
| 6 | // | 
|---|
| 7 | // This Source Code Form is subject to the terms of the Mozilla | 
|---|
| 8 | // Public License v. 2.0. If a copy of the MPL was not distributed | 
|---|
| 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | 
|---|
| 10 |  | 
|---|
| 11 | #ifndef EIGEN_COMMAINITIALIZER_H | 
|---|
| 12 | #define EIGEN_COMMAINITIALIZER_H | 
|---|
| 13 |  | 
|---|
| 14 | namespace Eigen { | 
|---|
| 15 |  | 
|---|
| 16 | /** \class CommaInitializer | 
|---|
| 17 | * \ingroup Core_Module | 
|---|
| 18 | * | 
|---|
| 19 | * \brief Helper class used by the comma initializer operator | 
|---|
| 20 | * | 
|---|
| 21 | * This class is internally used to implement the comma initializer feature. It is | 
|---|
| 22 | * the return type of MatrixBase::operator<<, and most of the time this is the only | 
|---|
| 23 | * way it is used. | 
|---|
| 24 | * | 
|---|
| 25 | * \sa \blank \ref MatrixBaseCommaInitRef "MatrixBase::operator<<", CommaInitializer::finished() | 
|---|
| 26 | */ | 
|---|
| 27 | template<typename XprType> | 
|---|
| 28 | struct CommaInitializer | 
|---|
| 29 | { | 
|---|
| 30 | typedef typename XprType::Scalar Scalar; | 
|---|
| 31 |  | 
|---|
| 32 | EIGEN_DEVICE_FUNC | 
|---|
| 33 | inline CommaInitializer(XprType& xpr, const Scalar& s) | 
|---|
| 34 | : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1) | 
|---|
| 35 | { | 
|---|
| 36 | m_xpr.coeffRef(0,0) = s; | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | template<typename OtherDerived> | 
|---|
| 40 | EIGEN_DEVICE_FUNC | 
|---|
| 41 | inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other) | 
|---|
| 42 | : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows()) | 
|---|
| 43 | { | 
|---|
| 44 | m_xpr.block(0, 0, other.rows(), other.cols()) = other; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | /* Copy/Move constructor which transfers ownership. This is crucial in | 
|---|
| 48 | * absence of return value optimization to avoid assertions during destruction. */ | 
|---|
| 49 | // FIXME in C++11 mode this could be replaced by a proper RValue constructor | 
|---|
| 50 | EIGEN_DEVICE_FUNC | 
|---|
| 51 | inline CommaInitializer(const CommaInitializer& o) | 
|---|
| 52 | : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) { | 
|---|
| 53 | // Mark original object as finished. In absence of R-value references we need to const_cast: | 
|---|
| 54 | const_cast<CommaInitializer&>(o).m_row = m_xpr.rows(); | 
|---|
| 55 | const_cast<CommaInitializer&>(o).m_col = m_xpr.cols(); | 
|---|
| 56 | const_cast<CommaInitializer&>(o).m_currentBlockRows = 0; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | /* inserts a scalar value in the target matrix */ | 
|---|
| 60 | EIGEN_DEVICE_FUNC | 
|---|
| 61 | CommaInitializer& operator,(const Scalar& s) | 
|---|
| 62 | { | 
|---|
| 63 | if (m_col==m_xpr.cols()) | 
|---|
| 64 | { | 
|---|
| 65 | m_row+=m_currentBlockRows; | 
|---|
| 66 | m_col = 0; | 
|---|
| 67 | m_currentBlockRows = 1; | 
|---|
| 68 | eigen_assert(m_row<m_xpr.rows() | 
|---|
| 69 | && "Too many rows passed to comma initializer (operator<<)"); | 
|---|
| 70 | } | 
|---|
| 71 | eigen_assert(m_col<m_xpr.cols() | 
|---|
| 72 | && "Too many coefficients passed to comma initializer (operator<<)"); | 
|---|
| 73 | eigen_assert(m_currentBlockRows==1); | 
|---|
| 74 | m_xpr.coeffRef(m_row, m_col++) = s; | 
|---|
| 75 | return *this; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /* inserts a matrix expression in the target matrix */ | 
|---|
| 79 | template<typename OtherDerived> | 
|---|
| 80 | EIGEN_DEVICE_FUNC | 
|---|
| 81 | CommaInitializer& operator,(const DenseBase<OtherDerived>& other) | 
|---|
| 82 | { | 
|---|
| 83 | if (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows)) | 
|---|
| 84 | { | 
|---|
| 85 | m_row+=m_currentBlockRows; | 
|---|
| 86 | m_col = 0; | 
|---|
| 87 | m_currentBlockRows = other.rows(); | 
|---|
| 88 | eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows() | 
|---|
| 89 | && "Too many rows passed to comma initializer (operator<<)"); | 
|---|
| 90 | } | 
|---|
| 91 | eigen_assert((m_col + other.cols() <= m_xpr.cols()) | 
|---|
| 92 | && "Too many coefficients passed to comma initializer (operator<<)"); | 
|---|
| 93 | eigen_assert(m_currentBlockRows==other.rows()); | 
|---|
| 94 | m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime> | 
|---|
| 95 | (m_row, m_col, other.rows(), other.cols()) = other; | 
|---|
| 96 | m_col += other.cols(); | 
|---|
| 97 | return *this; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | EIGEN_DEVICE_FUNC | 
|---|
| 101 | inline ~CommaInitializer() | 
|---|
| 102 | #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS | 
|---|
| 103 | EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception) | 
|---|
| 104 | #endif | 
|---|
| 105 | { | 
|---|
| 106 | finished(); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /** \returns the built matrix once all its coefficients have been set. | 
|---|
| 110 | * Calling finished is 100% optional. Its purpose is to write expressions | 
|---|
| 111 | * like this: | 
|---|
| 112 | * \code | 
|---|
| 113 | * quaternion.fromRotationMatrix((Matrix3f() << axis0, axis1, axis2).finished()); | 
|---|
| 114 | * \endcode | 
|---|
| 115 | */ | 
|---|
| 116 | EIGEN_DEVICE_FUNC | 
|---|
| 117 | inline XprType& finished() { | 
|---|
| 118 | eigen_assert(((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0) | 
|---|
| 119 | && m_col == m_xpr.cols() | 
|---|
| 120 | && "Too few coefficients passed to comma initializer (operator<<)"); | 
|---|
| 121 | return m_xpr; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | XprType& m_xpr;           // target expression | 
|---|
| 125 | Index m_row;              // current row id | 
|---|
| 126 | Index m_col;              // current col id | 
|---|
| 127 | Index m_currentBlockRows; // current block height | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | /** \anchor MatrixBaseCommaInitRef | 
|---|
| 131 | * Convenient operator to set the coefficients of a matrix. | 
|---|
| 132 | * | 
|---|
| 133 | * The coefficients must be provided in a row major order and exactly match | 
|---|
| 134 | * the size of the matrix. Otherwise an assertion is raised. | 
|---|
| 135 | * | 
|---|
| 136 | * Example: \include MatrixBase_set.cpp | 
|---|
| 137 | * Output: \verbinclude MatrixBase_set.out | 
|---|
| 138 | * | 
|---|
| 139 | * \note According the c++ standard, the argument expressions of this comma initializer are evaluated in arbitrary order. | 
|---|
| 140 | * | 
|---|
| 141 | * \sa CommaInitializer::finished(), class CommaInitializer | 
|---|
| 142 | */ | 
|---|
| 143 | template<typename Derived> | 
|---|
| 144 | inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s) | 
|---|
| 145 | { | 
|---|
| 146 | return CommaInitializer<Derived>(*static_cast<Derived*>(this), s); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /** \sa operator<<(const Scalar&) */ | 
|---|
| 150 | template<typename Derived> | 
|---|
| 151 | template<typename OtherDerived> | 
|---|
| 152 | inline CommaInitializer<Derived> | 
|---|
| 153 | DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other) | 
|---|
| 154 | { | 
|---|
| 155 | return CommaInitializer<Derived>(*static_cast<Derived *>(this), other); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | } // end namespace Eigen | 
|---|
| 159 |  | 
|---|
| 160 | #endif // EIGEN_COMMAINITIALIZER_H | 
|---|
| 161 |  | 
|---|