Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:11

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_COMMAINITIALIZER_H
0012 #define EIGEN_COMMAINITIALIZER_H
0013 
0014 namespace Eigen { 
0015 
0016 /** \class CommaInitializer
0017   * \ingroup Core_Module
0018   *
0019   * \brief Helper class used by the comma initializer operator
0020   *
0021   * This class is internally used to implement the comma initializer feature. It is
0022   * the return type of MatrixBase::operator<<, and most of the time this is the only
0023   * way it is used.
0024   *
0025   * \sa \blank \ref MatrixBaseCommaInitRef "MatrixBase::operator<<", CommaInitializer::finished()
0026   */
0027 template<typename XprType>
0028 struct CommaInitializer
0029 {
0030   typedef typename XprType::Scalar Scalar;
0031 
0032   EIGEN_DEVICE_FUNC
0033   inline CommaInitializer(XprType& xpr, const Scalar& s)
0034     : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
0035   {
0036     eigen_assert(m_xpr.rows() > 0 && m_xpr.cols() > 0
0037       && "Cannot comma-initialize a 0x0 matrix (operator<<)");
0038     m_xpr.coeffRef(0,0) = s;
0039   }
0040 
0041   template<typename OtherDerived>
0042   EIGEN_DEVICE_FUNC
0043   inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
0044     : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
0045   {
0046     eigen_assert(m_xpr.rows() >= other.rows() && m_xpr.cols() >= other.cols()
0047       && "Cannot comma-initialize a 0x0 matrix (operator<<)");
0048     m_xpr.block(0, 0, other.rows(), other.cols()) = other;
0049   }
0050 
0051   /* Copy/Move constructor which transfers ownership. This is crucial in 
0052    * absence of return value optimization to avoid assertions during destruction. */
0053   // FIXME in C++11 mode this could be replaced by a proper RValue constructor
0054   EIGEN_DEVICE_FUNC
0055   inline CommaInitializer(const CommaInitializer& o)
0056   : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
0057     // Mark original object as finished. In absence of R-value references we need to const_cast:
0058     const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
0059     const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
0060     const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
0061   }
0062 
0063   /* inserts a scalar value in the target matrix */
0064   EIGEN_DEVICE_FUNC
0065   CommaInitializer& operator,(const Scalar& s)
0066   {
0067     if (m_col==m_xpr.cols())
0068     {
0069       m_row+=m_currentBlockRows;
0070       m_col = 0;
0071       m_currentBlockRows = 1;
0072       eigen_assert(m_row<m_xpr.rows()
0073         && "Too many rows passed to comma initializer (operator<<)");
0074     }
0075     eigen_assert(m_col<m_xpr.cols()
0076       && "Too many coefficients passed to comma initializer (operator<<)");
0077     eigen_assert(m_currentBlockRows==1);
0078     m_xpr.coeffRef(m_row, m_col++) = s;
0079     return *this;
0080   }
0081 
0082   /* inserts a matrix expression in the target matrix */
0083   template<typename OtherDerived>
0084   EIGEN_DEVICE_FUNC
0085   CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
0086   {
0087     if (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows))
0088     {
0089       m_row+=m_currentBlockRows;
0090       m_col = 0;
0091       m_currentBlockRows = other.rows();
0092       eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
0093         && "Too many rows passed to comma initializer (operator<<)");
0094     }
0095     eigen_assert((m_col + other.cols() <= m_xpr.cols())
0096       && "Too many coefficients passed to comma initializer (operator<<)");
0097     eigen_assert(m_currentBlockRows==other.rows());
0098     m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>
0099                     (m_row, m_col, other.rows(), other.cols()) = other;
0100     m_col += other.cols();
0101     return *this;
0102   }
0103 
0104   EIGEN_DEVICE_FUNC
0105   inline ~CommaInitializer()
0106 #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
0107   EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception)
0108 #endif
0109   {
0110     finished();
0111   }
0112 
0113   /** \returns the built matrix once all its coefficients have been set.
0114     * Calling finished is 100% optional. Its purpose is to write expressions
0115     * like this:
0116     * \code
0117     * quaternion.fromRotationMatrix((Matrix3f() << axis0, axis1, axis2).finished());
0118     * \endcode
0119     */
0120   EIGEN_DEVICE_FUNC
0121   inline XprType& finished() {
0122       eigen_assert(((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0)
0123            && m_col == m_xpr.cols()
0124            && "Too few coefficients passed to comma initializer (operator<<)");
0125       return m_xpr;
0126   }
0127 
0128   XprType& m_xpr;           // target expression
0129   Index m_row;              // current row id
0130   Index m_col;              // current col id
0131   Index m_currentBlockRows; // current block height
0132 };
0133 
0134 /** \anchor MatrixBaseCommaInitRef
0135   * Convenient operator to set the coefficients of a matrix.
0136   *
0137   * The coefficients must be provided in a row major order and exactly match
0138   * the size of the matrix. Otherwise an assertion is raised.
0139   *
0140   * Example: \include MatrixBase_set.cpp
0141   * Output: \verbinclude MatrixBase_set.out
0142   * 
0143   * \note According the c++ standard, the argument expressions of this comma initializer are evaluated in arbitrary order.
0144   *
0145   * \sa CommaInitializer::finished(), class CommaInitializer
0146   */
0147 template<typename Derived>
0148 EIGEN_DEVICE_FUNC inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
0149 {
0150   return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
0151 }
0152 
0153 /** \sa operator<<(const Scalar&) */
0154 template<typename Derived>
0155 template<typename OtherDerived>
0156 EIGEN_DEVICE_FUNC inline CommaInitializer<Derived>
0157 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
0158 {
0159   return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
0160 }
0161 
0162 } // end namespace Eigen
0163 
0164 #endif // EIGEN_COMMAINITIALIZER_H