Eigen对QDebug 的输出接口

Eigen对QDebug 的输出接口


QEigenExt.h

#ifndef QEIGENEXT_H
#define QEIGENEXT_H

#pragma once
#include <QDebug>
#include <Eigen/Eigen>

namespace Eigen {

    namespace internal {
        template<typename Derived>
            QDebug & print_matrix(QDebug & s, const Derived& _m, const IOFormat& fmt);
    }// end namespace internal

} // end namespace Eigen

#define DECLARE_OSTREAM_OPERATOR(T)   \
    QDebug & operator << (QDebug & s, const T & m);

DECLARE_OSTREAM_OPERATOR( Eigen::Vector2f )
DECLARE_OSTREAM_OPERATOR( Eigen::Vector3f )
DECLARE_OSTREAM_OPERATOR( Eigen::Vector4f )
DECLARE_OSTREAM_OPERATOR( Eigen::VectorXf )
DECLARE_OSTREAM_OPERATOR( Eigen::Matrix2f )
DECLARE_OSTREAM_OPERATOR( Eigen::Matrix3f )
DECLARE_OSTREAM_OPERATOR( Eigen::Matrix4f )
DECLARE_OSTREAM_OPERATOR( Eigen::MatrixXf )


#endif // QEIGENEXT_H

QEigenExt.cpp

#include "QEigenExt.h"
#include <QString>
#include <Eigen/Eigen>

namespace Eigen {

namespace internal {

    /** \internal
      * print the matrix \a _m to the output stream \a s using the output format \a fmt */
    template<typename Derived>
    QDebug & print_matrix(QDebug & qDbg, const Derived& _m, const IOFormat& fmt)
    {
      std::stringstream s;
      using internal::is_same;
      using internal::conditional;

      if(_m.size() == 0)
      {
        qDbg << QString::fromStdString(fmt.matPrefix) << QString::fromStdString(fmt.matSuffix);
        return qDbg;
      }

      typename Derived::Nested m = _m;
      typedef typename Derived::Scalar Scalar;
      typedef typename
          conditional<
              is_same<Scalar, char>::value ||
                is_same<Scalar, unsigned char>::value ||
                is_same<Scalar, numext::int8_t>::value ||
                is_same<Scalar, numext::uint8_t>::value,
              int,
              typename conditional<
                  is_same<Scalar, std::complex<char> >::value ||
                    is_same<Scalar, std::complex<unsigned char> >::value ||
                    is_same<Scalar, std::complex<numext::int8_t> >::value ||
                    is_same<Scalar, std::complex<numext::uint8_t> >::value,
                  std::complex<int>,
                  const Scalar&
                >::type
            >::type PrintType;

      Index width = 0;

      std::streamsize explicit_precision;
      if(fmt.precision == StreamPrecision)
      {
        explicit_precision = 0;
      }
      else if(fmt.precision == FullPrecision)
      {
        if (NumTraits<Scalar>::IsInteger)
        {
          explicit_precision = 0;
        }
        else
        {
          explicit_precision = significant_decimals_impl<Scalar>::run();
        }
      }
      else
      {
        explicit_precision = fmt.precision;
      }

      std::streamsize old_precision = 0;
      if(explicit_precision) old_precision = s.precision(explicit_precision);

      bool align_cols = !(fmt.flags & DontAlignCols);
      if(align_cols)
      {
        // compute the largest width
        for(Index j = 0; j < m.cols(); ++j)
          for(Index i = 0; i < m.rows(); ++i)
          {
            std::stringstream sstr;
            sstr.copyfmt(s);
            sstr << static_cast<PrintType>(m.coeff(i,j));
            width = std::max<Index>(width, Index(sstr.str().length()));
          }
      }
      std::streamsize old_width = s.width();
      char old_fill_character = s.fill();
      s << fmt.matPrefix;
      for(Index i = 0; i < m.rows(); ++i)
      {
        if (i)
          s << fmt.rowSpacer;
        s <<fmt.rowPrefix;
        if(width) {
          s.fill(fmt.fill);
          s.width(width);
        }
        s << static_cast<PrintType>(m.coeff(i, 0));
        for(Index j = 1; j < m.cols(); ++j)
        {
          s << fmt.coeffSeparator;
          if(width) {
            s.fill(fmt.fill);
            s.width(width);
          }
          s << static_cast<PrintType>(m.coeff(i, j));
        }
        s << fmt.rowSuffix;
        if( i < m.rows() - 1)
          s << fmt.rowSeparator;
      }
      s << fmt.matSuffix;
      if(explicit_precision) s.precision(old_precision);
      if(width) {
        s.fill(old_fill_character);
        s.width(old_width);
      }
      qDbg <<  QString::fromStdString( s.str() );
      return qDbg;
    }

} // end namespace internal


} // end namespace Eigen


#define DEFINE_OSTREAM_OPERATOR(T)   \
    QDebug & operator << (QDebug & s, const T & m) { \
        return Eigen::internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);  \
    }

DEFINE_OSTREAM_OPERATOR( Eigen::Vector2f )
DEFINE_OSTREAM_OPERATOR( Eigen::Vector3f )
DEFINE_OSTREAM_OPERATOR( Eigen::Vector4f )
DEFINE_OSTREAM_OPERATOR( Eigen::VectorXf )
DEFINE_OSTREAM_OPERATOR( Eigen::Matrix2f )
DEFINE_OSTREAM_OPERATOR( Eigen::Matrix3f )
DEFINE_OSTREAM_OPERATOR( Eigen::Matrix4f )
DEFINE_OSTREAM_OPERATOR( Eigen::MatrixXf )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值