toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] [PATCH] Honour field width in output operators, remove fl


From: Pekka Paalanen
Subject: [Toon-members] [PATCH] Honour field width in output operators, remove flush from se2, se3.
Date: Mon, 14 Jun 2010 13:11:04 +0300

In C++ standard output streams, the field width property is reset after
the first item has been formatted. This makes printing vectors and
matrices with a given field width impossible with the current TooN
output operators.

Fix the Vector, Matrix, SE3 and SE2 output operators to honour the field
width for all elements.

An example of printing an SE3 object:

        TooN::SE3<> s(...);
        cout.precision(6);
        cout.width(15);
        cout << s;

Output before the fix:
        0.99962 0.0275654 -0.000100134 8.45411e-05
-0.0275654 0.99962 0.00046239 -0.000228848
0.000112842 -0.000459454 1 0.00880446

Output after the fix:
        0.99962       0.0275654    -0.000100134     8.45411e-05
     -0.0275654         0.99962      0.00046239    -0.000228848
    0.000112842    -0.000459454               1      0.00880446

In SE2 and SE3 output operators, remove the forced stream flush. This is
a theoretical speed improvement.

Signed-off-by: Pekka Paalanen <address@hidden>
---
 internal/operators.hh |    4 ++++
 se2.h                 |    9 +++++++--
 se3.h                 |    6 +++++-
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/internal/operators.hh b/internal/operators.hh
index ca3217d..2daf5f3 100644
--- a/internal/operators.hh
+++ b/internal/operators.hh
@@ -644,7 +644,9 @@ Matrix<Rows, Cols, typename 
Internal::Subtract::Return<typename Operator<Op>::Pr
 // output operator <<
 template <int Size, typename Precision, typename Base>
 inline std::ostream& operator<< (std::ostream& os, const 
Vector<Size,Precision,Base>& v){
+  std::streamsize fw = os.width();
   for(int i=0; i<v.size(); i++){
+    os.width(fw);
     os << v[i] << " ";
   }
   return os;
@@ -661,12 +663,14 @@ std::istream& operator >> (std::istream& is, Vector<Size, 
Precision, Base>& v){
 
 template<int Rows, int Cols, typename Precision, class Base>
 inline std::ostream& operator<< (std::ostream& os, const Matrix<Rows, Cols, 
Precision, Base>& m){
+       std::streamsize fw = os.width();
        for(int i=0; i < m.num_rows(); i++)
        {
                for(int j=0; j < m.num_cols(); j++)
                {
                        if(j != 0)
                                os << " ";
+                       os.width(fw);
                        os << m(i,j);
                }
                os << std::endl;
diff --git a/se2.h b/se2.h
index 1420446..bbc840d 100644
--- a/se2.h
+++ b/se2.h
@@ -144,8 +144,13 @@ private:
 /// @relates SE2
 template <class Precision>
 inline std::ostream& operator<<(std::ostream& os, const SE2<Precision> & rhs){
-       for(int i=0; i<2; i++)
-               os << rhs.get_rotation().get_matrix()[i] << 
rhs.get_translation()[i] << std::endl;
+       std::streamsize fw = os.width();
+       for(int i=0; i<2; i++){
+               os.width(fw);
+               os << rhs.get_rotation().get_matrix()[i];
+               os.width(fw);
+               os << rhs.get_translation()[i] << '\n';
+       }
        return os;
 }
 
diff --git a/se3.h b/se3.h
index 7fb5474..e3921d8 100644
--- a/se3.h
+++ b/se3.h
@@ -224,8 +224,12 @@ inline Matrix<6,6,Precision> 
SE3<Precision>::trinvadjoint(const Matrix<R,C,P2,Ac
 /// @relates SE3
 template <typename Precision>
 inline std::ostream& operator <<(std::ostream& os, const SE3<Precision>& rhs){
+       std::streamsize fw = os.width();
        for(int i=0; i<3; i++){
-               os << rhs.get_rotation().get_matrix()[i] << 
rhs.get_translation()[i] << std::endl;
+               os.width(fw);
+               os << rhs.get_rotation().get_matrix()[i];
+               os.width(fw);
+               os << rhs.get_translation()[i] << '\n';
        }
        return os;
 }
-- 
1.6.4.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]