Main MRPT website > C++ reference
MRPT logo

IO.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_IO_H
00027 #define EIGEN_IO_H
00028 
00029 enum { DontAlignCols = 1 };
00030 enum { StreamPrecision = -1,
00031        FullPrecision = -2 };
00032 
00033 namespace internal {
00034 template<typename Derived>
00035 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
00036 }
00037 
00038 /** \class IOFormat
00039   * \ingroup Core_Module
00040   *
00041   * \brief Stores a set of parameters controlling the way matrices are printed
00042   *
00043   * List of available parameters:
00044   *  - \b precision number of digits for floating point values, or one of the special constants \c StreamPrecision and \c FullPrecision.
00045   *                 The default is the special value \c StreamPrecision which means to use the
00046   *                 stream's own precision setting, as set for instance using \c cout.precision(3). The other special value
00047   *                 \c FullPrecision means that the number of digits will be computed to match the full precision of each floating-point
00048   *                 type.
00049   *  - \b flags an OR-ed combination of flags, the default value is 0, the only currently available flag is \c DontAlignCols which
00050   *             allows to disable the alignment of columns, resulting in faster code.
00051   *  - \b coeffSeparator string printed between two coefficients of the same row
00052   *  - \b rowSeparator string printed between two rows
00053   *  - \b rowPrefix string printed at the beginning of each row
00054   *  - \b rowSuffix string printed at the end of each row
00055   *  - \b matPrefix string printed at the beginning of the matrix
00056   *  - \b matSuffix string printed at the end of the matrix
00057   *
00058   * Example: \include IOFormat.cpp
00059   * Output: \verbinclude IOFormat.out
00060   *
00061   * \sa DenseBase::format(), class WithFormat
00062   */
00063 struct IOFormat
00064 {
00065   /** Default contructor, see class IOFormat for the meaning of the parameters */
00066   IOFormat(int _precision = StreamPrecision, int _flags = 0,
00067     const std::string& _coeffSeparator = " ",
00068     const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
00069     const std::string& _matPrefix="", const std::string& _matSuffix="")
00070   : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
00071     coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
00072   {
00073     rowSpacer = "";
00074     int i = int(matSuffix.length())-1;
00075     while (i>=0 && matSuffix[i]!='\n')
00076     {
00077       rowSpacer += ' ';
00078       i--;
00079     }
00080   }
00081   std::string matPrefix, matSuffix;
00082   std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
00083   std::string coeffSeparator;
00084   int precision;
00085   int flags;
00086 };
00087 
00088 /** \class WithFormat
00089   * \ingroup Core_Module
00090   *
00091   * \brief Pseudo expression providing matrix output with given format
00092   *
00093   * \param ExpressionType the type of the object on which IO stream operations are performed
00094   *
00095   * This class represents an expression with stream operators controlled by a given IOFormat.
00096   * It is the return type of DenseBase::format()
00097   * and most of the time this is the only way it is used.
00098   *
00099   * See class IOFormat for some examples.
00100   *
00101   * \sa DenseBase::format(), class IOFormat
00102   */
00103 template<typename ExpressionType>
00104 class WithFormat
00105 {
00106   public:
00107 
00108     WithFormat(const ExpressionType& matrix, const IOFormat& format)
00109       : m_matrix(matrix), m_format(format)
00110     {}
00111 
00112     friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
00113     {
00114       return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
00115     }
00116 
00117   protected:
00118     const typename ExpressionType::Nested m_matrix;
00119     IOFormat m_format;
00120 };
00121 
00122 /** \returns a WithFormat proxy object allowing to print a matrix the with given
00123   * format \a fmt.
00124   *
00125   * See class IOFormat for some examples.
00126   *
00127   * \sa class IOFormat, class WithFormat
00128   */
00129 template<typename Derived>
00130 inline const WithFormat<Derived>
00131 DenseBase<Derived>::format(const IOFormat& fmt) const
00132 {
00133   return WithFormat<Derived>(derived(), fmt);
00134 }
00135 
00136 namespace internal {
00137 
00138 template<typename Scalar, bool IsInteger>
00139 struct significant_decimals_default_impl
00140 {
00141   typedef typename NumTraits<Scalar>::Real RealScalar;
00142   static inline int run()
00143   {
00144     return cast<RealScalar,int>(std::ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
00145   }
00146 };
00147 
00148 template<typename Scalar>
00149 struct significant_decimals_default_impl<Scalar, true>
00150 {
00151   static inline int run()
00152   {
00153     return 0;
00154   }
00155 };
00156 
00157 template<typename Scalar>
00158 struct significant_decimals_impl
00159   : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
00160 {};
00161 
00162 /** \internal
00163   * print the matrix \a _m to the output stream \a s using the output format \a fmt */
00164 template<typename Derived>
00165 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
00166 {
00167   if(_m.size() == 0)
00168   {
00169     s << fmt.matPrefix << fmt.matSuffix;
00170     return s;
00171   }
00172   
00173   const typename Derived::Nested m = _m;
00174   typedef typename Derived::Scalar Scalar;
00175   typedef typename Derived::Index Index;
00176 
00177   Index width = 0;
00178 
00179   std::streamsize explicit_precision;
00180   if(fmt.precision == StreamPrecision)
00181   {
00182     explicit_precision = 0;
00183   }
00184   else if(fmt.precision == FullPrecision)
00185   {
00186     if (NumTraits<Scalar>::IsInteger)
00187     {
00188       explicit_precision = 0;
00189     }
00190     else
00191     {
00192       explicit_precision = significant_decimals_impl<Scalar>::run();
00193     }
00194   }
00195   else
00196   {
00197     explicit_precision = fmt.precision;
00198   }
00199 
00200   bool align_cols = !(fmt.flags & DontAlignCols);
00201   if(align_cols)
00202   {
00203     // compute the largest width
00204     for(Index j = 1; j < m.cols(); ++j)
00205       for(Index i = 0; i < m.rows(); ++i)
00206       {
00207         std::stringstream sstr;
00208         if(explicit_precision) sstr.precision(explicit_precision);
00209         sstr << m.coeff(i,j);
00210         width = std::max<Index>(width, Index(sstr.str().length()));
00211       }
00212   }
00213   std::streamsize old_precision = 0;
00214   if(explicit_precision) old_precision = s.precision(explicit_precision);
00215   s << fmt.matPrefix;
00216   for(Index i = 0; i < m.rows(); ++i)
00217   {
00218     if (i)
00219       s << fmt.rowSpacer;
00220     s << fmt.rowPrefix;
00221     if(width) s.width(width);
00222     s << m.coeff(i, 0);
00223     for(Index j = 1; j < m.cols(); ++j)
00224     {
00225       s << fmt.coeffSeparator;
00226       if (width) s.width(width);
00227       s << m.coeff(i, j);
00228     }
00229     s << fmt.rowSuffix;
00230     if( i < m.rows() - 1)
00231       s << fmt.rowSeparator;
00232   }
00233   s << fmt.matSuffix;
00234   if(explicit_precision) s.precision(old_precision);
00235   return s;
00236 }
00237 
00238 } // end namespace internal
00239 
00240 /** \relates DenseBase
00241   *
00242   * Outputs the matrix, to the given stream.
00243   *
00244   * If you wish to print the matrix with a format different than the default, use DenseBase::format().
00245   *
00246   * It is also possible to change the default format by defining EIGEN_DEFAULT_IO_FORMAT before including Eigen headers.
00247   * If not defined, this will automatically be defined to Eigen::IOFormat(), that is the Eigen::IOFormat with default parameters.
00248   *
00249   * \sa DenseBase::format()
00250   */
00251 template<typename Derived>
00252 std::ostream & operator <<
00253 (std::ostream & s,
00254  const DenseBase<Derived> & m)
00255 {
00256   return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
00257 }
00258 
00259 #endif // EIGEN_IO_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011