jspace/jspace/wrap_eigen.cpp

Go to the documentation of this file.
00001 /*
00002  * Stanford Whole-Body Control Framework http://stanford-wbc.sourceforge.net/
00003  *
00004  * Copyright (C) 2010 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00005  *
00006  * This program is free software: you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation, either version 3 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this program.  If not, see
00018  * <http://www.gnu.org/licenses/>
00019  */
00020 
00026 #include "wrap_eigen.hpp"
00027 #include <stdio.h>
00028 
00029 using namespace std;
00030 
00031 namespace jspace {
00032   
00033   bool compare(jspace::Matrix const & lhs, jspace::Matrix const & rhs, double precision)
00034   {
00035     if ( &lhs == &rhs ) {
00036       return true;
00037     }
00038     if ( lhs.rows() != rhs.rows() ) {
00039       return false;
00040     }
00041     if ( lhs.cols() != rhs.cols() ) {
00042       return false;
00043     }
00044     for (int ii(0); ii < lhs.rows(); ++ii) {
00045       for (int jj(0); jj < lhs.cols(); ++jj) {
00046   if (fabs(lhs.coeff(ii, jj) - rhs.coeff(ii, jj)) > precision) {
00047     return false;
00048   }
00049       }
00050     }
00051     return true;
00052   }
00053     
00054     
00055   bool compare(jspace::Quaternion const & lhs, jspace::Quaternion const & rhs, double precision)
00056   {
00057     return compare(lhs.coeffs(), rhs.coeffs(), precision);
00058   }
00059   
00060   
00061   std::string pretty_string(jspace::Vector const & vv)
00062   {
00063     ostringstream os;
00064     pretty_print(vv, os, "", "", true);
00065     return os.str();
00066   }
00067   
00068     
00069   std::string pretty_string(jspace::Quaternion const & qq)
00070   {
00071     ostringstream os;
00072     pretty_print(qq, os, "", "", true);
00073     return os.str();
00074   }
00075     
00076     
00077   std::string pretty_string(jspace::Matrix const & mm, std::string const & prefix)
00078   {
00079     ostringstream os;
00080     pretty_print(mm, os, "", prefix);
00081     return os.str();
00082   }
00083     
00084     
00085   void pretty_print(jspace::Vector const & vv, std::ostream & os,
00086         std::string const & title, std::string const & prefix,
00087         bool nonl)
00088   {
00089     pretty_print((jspace::Matrix const &) vv, os, title, prefix, true, nonl);
00090   }
00091     
00092     
00093   void pretty_print(jspace::Quaternion const & qq, std::ostream & os,
00094         std::string const & title, std::string const & prefix,
00095         bool nonl)
00096   {
00097     pretty_print(qq.coeffs(), os, title, prefix, true, nonl);
00098   }
00099     
00100     
00101   std::string pretty_string(double vv)
00102   {
00103     static int const buflen(32);
00104     static char buf[buflen];
00105     memset(buf, 0, sizeof(buf));
00106 #ifndef WIN32
00107     if (isinf(vv)) {
00108       snprintf(buf, buflen-1, " inf    ");
00109     }
00110     else if (isnan(vv)) {
00111       snprintf(buf, buflen-1, " nan    ");
00112     }
00113     else if (fabs(fmod(vv, 1)) < 1e-6) {
00114       snprintf(buf, buflen-1, "%- 7d  ", static_cast<int>(rint(vv)));
00115     }
00116     else {
00117       snprintf(buf, buflen-1, "% 6.4f  ", vv);
00118     }
00119 #else // WIN32
00120     sprintf_s(buf, buflen-1, "% 6.4f  ", vv);
00121 #endif // WIN32
00122     string str(buf);
00123     return str;
00124   }
00125   
00126   
00127   void pretty_print(jspace::Matrix const & mm, std::ostream & os,
00128         std::string const & title, std::string const & prefix,
00129         bool vecmode, bool nonl)
00130   {
00131     char const * nlornot("\n");
00132     if (nonl) {
00133       nlornot = "";
00134     }
00135     if ( ! title.empty()) {
00136       os << title << nlornot;
00137     }
00138     if ((mm.rows() <= 0) || (mm.cols() <= 0)) {
00139       os << prefix << " (empty)" << nlornot;
00140     }
00141     else {
00142       // if (mm.cols() == 1) {
00143       //   vecmode = true;
00144       // }
00145   
00146       if (vecmode) {
00147   if ( ! prefix.empty())
00148     os << prefix;
00149   for (int ir(0); ir < mm.rows(); ++ir) {
00150     os << pretty_string(mm.coeff(ir, 0));
00151   }
00152   os << nlornot;
00153   
00154       }
00155       else {
00156 
00157   for (int ir(0); ir < mm.rows(); ++ir) {
00158     if ( ! prefix.empty())
00159       os << prefix;
00160     for (int ic(0); ic < mm.cols(); ++ic) {
00161       os << pretty_string(mm.coeff(ir, ic));
00162     }
00163     os << nlornot;
00164   }
00165     
00166       }
00167     }
00168   }
00169   
00170   
00171   void convert(jspace::Vector const & from, std::vector<double> & to)
00172   {
00173     to.resize(from.size());
00174     Vector::Map(&to[0], to.size()) = from;
00175   }
00176   
00177   
00178   void convert(std::vector<double> const & from, jspace::Vector & to)
00179   {
00180     to = Vector::Map(&from[0], from.size());
00181   }
00182   
00183   
00184   void convert(double const * from, size_t length, jspace::Vector & to)
00185   {
00186     to = Vector::Map(from, length);
00187   }
00188   
00189 }

Generated on Fri Aug 26 01:31:16 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4