Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CommaInitializer.h Source File

CommaInitializer.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_COMMAINITIALIZER_H
00012 #define EIGEN_COMMAINITIALIZER_H
00013 
00014 namespace Eigen { 
00015 
00016 /** \class CommaInitializer
00017   * \ingroup Core_Module
00018   *
00019   * \brief Helper class used by the comma initializer operator
00020   *
00021   * This class is internally used to implement the comma initializer feature. It is
00022   * the return type of MatrixBase::operator<<, and most of the time this is the only
00023   * way it is used.
00024   *
00025   * \sa \ref MatrixBaseCommaInitRef "MatrixBase::operator<<", CommaInitializer::finished()
00026   */
00027 template<typename XprType>
00028 struct CommaInitializer
00029 {
00030   typedef typename XprType::Scalar Scalar;
00031   typedef typename XprType::Index Index;
00032 
00033   inline CommaInitializer(XprType& xpr, const Scalar& s)
00034     : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
00035   {
00036     m_xpr.coeffRef(0,0) = s;
00037   }
00038 
00039   template<typename OtherDerived>
00040   inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
00041     : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
00042   {
00043     m_xpr.block(0, 0, other.rows(), other.cols()) = other;
00044   }
00045 
00046   /* Copy/Move constructor which transfers ownership. This is crucial in 
00047    * absence of return value optimization to avoid assertions during destruction. */
00048   // FIXME in C++11 mode this could be replaced by a proper RValue constructor
00049   inline CommaInitializer(const CommaInitializer& o)
00050   : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
00051     // Mark original object as finished. In absence of R-value references we need to const_cast:
00052     const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
00053     const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
00054     const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
00055   }
00056 
00057   /* inserts a scalar value in the target matrix */
00058   CommaInitializer& operator,(const Scalar& s)
00059   {
00060     if (m_col==m_xpr.cols())
00061     {
00062       m_row+=m_currentBlockRows;
00063       m_col = 0;
00064       m_currentBlockRows = 1;
00065       eigen_assert(m_row<m_xpr.rows()
00066         && "Too many rows passed to comma initializer (operator<<)");
00067     }
00068     eigen_assert(m_col<m_xpr.cols()
00069       && "Too many coefficients passed to comma initializer (operator<<)");
00070     eigen_assert(m_currentBlockRows==1);
00071     m_xpr.coeffRef(m_row, m_col++) = s;
00072     return *this;
00073   }
00074 
00075   /* inserts a matrix expression in the target matrix */
00076   template<typename OtherDerived>
00077   CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
00078   {
00079     if(other.cols()==0 || other.rows()==0)
00080       return *this;
00081     if (m_col==m_xpr.cols())
00082     {
00083       m_row+=m_currentBlockRows;
00084       m_col = 0;
00085       m_currentBlockRows = other.rows();
00086       eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
00087         && "Too many rows passed to comma initializer (operator<<)");
00088     }
00089     eigen_assert(m_col<m_xpr.cols()
00090       && "Too many coefficients passed to comma initializer (operator<<)");
00091     eigen_assert(m_currentBlockRows==other.rows());
00092     if (OtherDerived::SizeAtCompileTime != Dynamic)
00093       m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
00094                               OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
00095                     (m_row, m_col) = other;
00096     else
00097       m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
00098     m_col += other.cols();
00099     return *this;
00100   }
00101 
00102   inline ~CommaInitializer()
00103   {
00104     eigen_assert((m_row+m_currentBlockRows) == m_xpr.rows()
00105          && m_col == m_xpr.cols()
00106          && "Too few coefficients passed to comma initializer (operator<<)");
00107   }
00108 
00109   /** \returns the built matrix once all its coefficients have been set.
00110     * Calling finished is 100% optional. Its purpose is to write expressions
00111     * like this:
00112     * \code
00113     * quaternion.fromRotationMatrix((Matrix3f() << axis0, axis1, axis2).finished());
00114     * \endcode
00115     */
00116   inline XprType& finished () { return m_xpr; }
00117 
00118   XprType& m_xpr;   // target expression
00119   Index m_row;              // current row id
00120   Index m_col;              // current col id
00121   Index m_currentBlockRows; // current block height
00122 };
00123 
00124 /** \anchor MatrixBaseCommaInitRef
00125   * Convenient operator to set the coefficients of a matrix.
00126   *
00127   * The coefficients must be provided in a row major order and exactly match
00128   * the size of the matrix. Otherwise an assertion is raised.
00129   *
00130   * Example: \include MatrixBase_set.cpp
00131   * Output: \verbinclude MatrixBase_set.out
00132   * 
00133   * \note According the c++ standard, the argument expressions of this comma initializer are evaluated in arbitrary order.
00134   *
00135   * \sa CommaInitializer::finished(), class CommaInitializer
00136   */
00137 template<typename Derived>
00138 inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
00139 {
00140   return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
00141 }
00142 
00143 /** \sa operator<<(const Scalar&) */
00144 template<typename Derived>
00145 template<typename OtherDerived>
00146 inline CommaInitializer<Derived>
00147 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
00148 {
00149   return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
00150 }
00151 
00152 } // end namespace Eigen
00153 
00154 #endif // EIGEN_COMMAINITIALIZER_H