MBSim

Project Page
utils.h
00001 /* Copyright (C) 2004-2009 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef UTILS_H_
00021 #define UTILS_H_
00022 
00023 #include <string>
00024 #include "fmatvec/fmatvec.h"
00025 #include <mbsim/numerics/csparse.h>
00026 #include <mbxmlutilshelper/dom.h>
00027 #include <xercesc/dom/DOMDocument.hpp>
00028 #include <limits>
00029 #include <vector>
00030 #include <set>
00031 
00032 namespace MBSim {
00033 
00034   std::string numtostr(int i);   
00035   std::string numtostr(double d);   
00036   template<class Type, class Row, class Col>
00037     std::string numtostr(fmatvec::Matrix<Type,Row,Col,double> m) {
00038       std::ostringstream oss;
00039       oss << "[ ";
00040       for(int i=0;i<m.rows()-1;i++) {
00041         for(int j=0;i<m.cols()-1;j++) oss << m.e(i,j) << ", ";
00042         oss << "\n";
00043       }
00044       oss << "]";
00045       return oss.str(); 
00046     }
00047 
00048   double degtorad(double alpha);
00049   double radtodeg(double phi);
00050   fmatvec::Vec degtorad(fmatvec::Vec alpha);
00051   fmatvec::Vec radtodeg(fmatvec::Vec phi);
00052   fmatvec::Vec tildetovec(const fmatvec::SqrMat &A);
00053 
00054   double sign(double x);
00055 
00062   double ArcTan(double x,double y);
00063 
00067   fmatvec::Mat cs2Mat(cs* sparseMat);
00068 
00069   template <class T>
00070     inline std::string toStr(const T &val) {
00071       std::stringstream s;
00072       s << std::setprecision(std::numeric_limits<double>::digits10+1) << val;
00073       return s.str();
00074     }
00075 
00076   inline xercesc::DOMNode* toXML(const std::string &str, xercesc::DOMNode* parent) {
00077     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%str);
00078   }
00079 
00080   inline xercesc::DOMNode* toXML(int i, xercesc::DOMNode* parent) {
00081     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
00082   }
00083 
00084   inline xercesc::DOMNode* toXML(unsigned int i, xercesc::DOMNode* parent) {
00085     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
00086   }
00087 
00088   inline xercesc::DOMNode* toXML(double d, xercesc::DOMNode* parent) {
00089     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(d));
00090   }
00091 
00092   template <class T>
00093     inline xercesc::DOMNode* toXML(const std::vector<T> &x, xercesc::DOMNode* parent) {
00094       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlVector");
00095       for(unsigned int i=0; i<x.size(); i++) {
00096         xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"ele");
00097         xercesc::DOMText *text = new xercesc::DOMText(toStr(x[i]));
00098         elei->insertBefore(text, NULL);
00099         ele->insertBefore(elei, NULL);
00100       }
00101       return ele;
00102     }
00103 
00104   template <class Row>
00105     inline xercesc::DOMNode* toXML(const fmatvec::Vector<Row,double> &x, xercesc::DOMNode* parent) {
00106       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlVector");
00107       for(int i=0; i<x.size(); i++) {
00108         xercesc::DOMDocument *doc=parent->getOwnerDocument();
00109         xercesc::DOMElement *elei = MBXMLUtils::D(doc)->createElement(MBXMLUtils::PV%"ele");
00110         xercesc::DOMText *text = doc->createTextNode(MBXMLUtils::X()%toStr(x.e(i)));
00111         elei->insertBefore(text, NULL);
00112         ele->insertBefore(elei, NULL);
00113       }
00114       return ele;
00115     }
00116 
00117   template <class Type, class Row, class Col>
00118     inline xercesc::DOMNode* toXML(const fmatvec::Matrix<Type,Row,Col,double> &A, xercesc::DOMNode* parent) {
00119       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlMatrix");
00120       for(int i=0; i<A.rows(); i++) {
00121         xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"row");
00122         for(int j=0; j<A.cols(); j++) {
00123           xercesc::DOMElement *elej = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"ele");
00124           xercesc::DOMText *text = new xercesc::DOMText(toStr(A.e(i,j)));
00125           elej->insertBefore(text, NULL);
00126           elei->insertBefore(elej, NULL);
00127         }
00128         ele->insertBefore(elei, NULL);
00129       }
00130       return ele;
00131     }
00132 
00133   template<class T>
00134     inline std::string funcExt() {
00135       return "V";
00136     }
00137 
00138   template < >
00139     inline std::string funcExt<double>() {
00140       return "S";
00141     }
00142 
00143   template <class T>
00144     void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const T &value) {
00145       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(name);
00146       ele->insertBefore(toXML(value,parent), NULL);
00147       parent->insertBefore(ele, NULL);
00148     }
00149 
00150   template <class Arg>
00151     class ToDouble {
00152     };
00153 
00154   template <>
00155     class ToDouble<double> {
00156       public:
00157         static double cast(const double &x) {
00158           return x;
00159         }
00160     };
00161 
00162   template <class Col>
00163     class ToDouble<fmatvec::Vector<Col,double> > {
00164       public:
00165         static double cast(const fmatvec::Vector<Col,double> &x) {
00166           return x.e(0); 
00167         }
00168     };
00169 
00170   template <class Row>
00171     class ToDouble<fmatvec::RowVector<Row,double> > {
00172       public:
00173         static double cast(const fmatvec::RowVector<Row,double> &x) {
00174           return x.e(0); 
00175         }
00176     };
00177 
00178   template <class Ret>
00179   class FromMatStr {
00180     public:
00181       static Ret cast(const char *x) {
00182         return Ret(x);
00183       }
00184   };
00185 
00186   template <>
00187   class FromMatStr<double> {
00188     public:
00189       static double cast(const char *x) {
00190         return atof(x);
00191       }
00192   };
00193 
00194   template <class Ret>
00195   class FromDouble {
00196     public:
00197       static Ret cast(double x) {
00198         return Ret(1,fmatvec::INIT,x);
00199       }
00200   };
00201 
00202   template <>
00203   class FromDouble<double> {
00204     public:
00205       static double cast(double x) {
00206         return x;
00207       }
00208   };
00209 
00210   template <class Ret>
00211   class FromVecV {
00212     public:
00213       static Ret cast(const fmatvec::VecV &x) {
00214         return x;
00215       }
00216   };
00217 
00218   template <>
00219   class FromVecV<double> {
00220     public:
00221       static double cast(const fmatvec::VecV &x) {
00222         return x(0);
00223       }
00224   };
00225 
00226 }
00227 
00228 #endif