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 #define PVNS_ "http://openmbv.berlios.de/MBXMLUtils/physicalvariable"
00024 #define PVNS "{"PVNS_"}"
00025 
00026 #include <string>
00027 #include "fmatvec/fmatvec.h"
00028 #include <mbsim/numerics/csparse.h>
00029 #include <mbxmlutilshelper/dom.h>
00030 #include <xercesc/dom/DOMDocument.hpp>
00031 #include <limits>
00032 #include <vector>
00033 #include <set>
00034 
00035 namespace MBSim {
00036 
00037   std::string numtostr(int i);   
00038   std::string numtostr(double d);   
00039   template<class Type, class Row, class Col>
00040     std::string numtostr(fmatvec::Matrix<Type,Row,Col,double> m) {
00041       std::ostringstream oss;
00042       oss << "[ ";
00043       for(int i=0;i<m.rows()-1;i++) {
00044         for(int j=0;i<m.cols()-1;j++) oss << m.e(i,j) << ", ";
00045         oss << "\n";
00046       }
00047       oss << "]";
00048       return oss.str(); 
00049     }
00050 
00051   double degtorad(double alpha);
00052   double radtodeg(double phi);
00053   fmatvec::Vec degtorad(fmatvec::Vec alpha);
00054   fmatvec::Vec radtodeg(fmatvec::Vec phi);
00055   fmatvec::Vec tildetovec(const fmatvec::SqrMat &A);
00056 
00057   double sign(double x);
00058 
00065   double ArcTan(double x,double y);
00066 
00070   fmatvec::Mat cs2Mat(cs* sparseMat);
00071 
00072   template <class T>
00073     inline std::string toStr(const T &val) {
00074       std::stringstream s;
00075       s << std::setprecision(std::numeric_limits<double>::digits10+1) << val;
00076       return s.str();
00077     }
00078 
00079   inline xercesc::DOMNode* toXML(const std::string &str, xercesc::DOMNode* parent) {
00080     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%str);
00081   }
00082 
00083   inline xercesc::DOMNode* toXML(int i, xercesc::DOMNode* parent) {
00084     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
00085   }
00086 
00087   inline xercesc::DOMNode* toXML(unsigned int i, xercesc::DOMNode* parent) {
00088     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
00089   }
00090 
00091   inline xercesc::DOMNode* toXML(double d, xercesc::DOMNode* parent) {
00092     return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(d));
00093   }
00094 
00095   template <class T>
00096     inline xercesc::DOMNode* toXML(const std::vector<T> &x, xercesc::DOMNode* parent) {
00097       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"xmlVector");
00098       for(unsigned int i=0; i<x.size(); i++) {
00099         xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"ele");
00100         xercesc::DOMText *text = new xercesc::DOMText(toStr(x[i]));
00101         elei->insertBefore(text, NULL);
00102         ele->insertBefore(elei, NULL);
00103       }
00104       return ele;
00105     }
00106 
00107   template <class Row>
00108     inline xercesc::DOMNode* toXML(const fmatvec::Vector<Row,double> &x, xercesc::DOMNode* parent) {
00109       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"xmlVector");
00110       for(int i=0; i<x.size(); i++) {
00111         xercesc::DOMDocument *doc=parent->getOwnerDocument();
00112         xercesc::DOMElement *elei = MBXMLUtils::D(doc)->createElement(PVNS"ele");
00113         xercesc::DOMText *text = doc->createTextNode(MBXMLUtils::X()%toStr(x.e(i)));
00114         elei->insertBefore(text, NULL);
00115         ele->insertBefore(elei, NULL);
00116       }
00117       return ele;
00118     }
00119 
00120   template <class Type, class Row, class Col>
00121     inline xercesc::DOMNode* toXML(const fmatvec::Matrix<Type,Row,Col,double> &A, xercesc::DOMNode* parent) {
00122       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"xmlMatrix");
00123       for(int i=0; i<A.rows(); i++) {
00124         xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"row");
00125         for(int j=0; j<A.cols(); j++) {
00126           xercesc::DOMElement *elej = MBXMLUtils::D(parent->getOwnerDocument())->createElement(PVNS"ele");
00127           xercesc::DOMText *text = new xercesc::DOMText(toStr(A.e(i,j)));
00128           elej->insertBefore(text, NULL);
00129           elei->insertBefore(elej, NULL);
00130         }
00131         ele->insertBefore(elei, NULL);
00132       }
00133       return ele;
00134     }
00135 
00136   template<class T>
00137     inline std::string funcExt() {
00138       return "V";
00139     }
00140 
00141   template < >
00142     inline std::string funcExt<double>() {
00143       return "S";
00144     }
00145 
00146   template <class T>
00147     void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const T &value) {
00148       xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(name);
00149       ele->insertBefore(toXML(value,parent), NULL);
00150       parent->insertBefore(ele, NULL);
00151     }
00152 
00153   template <class Arg>
00154     class ToDouble {
00155     };
00156 
00157   template <>
00158     class ToDouble<double> {
00159       public:
00160         static double cast(const double &x) {
00161           return x;
00162         }
00163     };
00164 
00165   template <class Col>
00166     class ToDouble<fmatvec::Vector<Col,double> > {
00167       public:
00168         static double cast(const fmatvec::Vector<Col,double> &x) {
00169           return x.e(0); 
00170         }
00171     };
00172 
00173   template <class Row>
00174     class ToDouble<fmatvec::RowVector<Row,double> > {
00175       public:
00176         static double cast(const fmatvec::RowVector<Row,double> &x) {
00177           return x.e(0); 
00178         }
00179     };
00180 
00181   template <class Ret>
00182   class FromMatStr {
00183     public:
00184       static Ret cast(const char *x) {
00185         return Ret(x);
00186       }
00187   };
00188 
00189   template <>
00190   class FromMatStr<double> {
00191     public:
00192       static double cast(const char *x) {
00193         return atof(x);
00194       }
00195   };
00196 
00197   template <class Ret>
00198   class FromDouble {
00199     public:
00200       static Ret cast(double x) {
00201         return Ret(1,fmatvec::INIT,x);
00202       }
00203   };
00204 
00205   template <>
00206   class FromDouble<double> {
00207     public:
00208       static double cast(double x) {
00209         return x;
00210       }
00211   };
00212 
00213   template <class Ret>
00214   class FromVecV {
00215     public:
00216       static Ret cast(const fmatvec::VecV &x) {
00217         return x;
00218       }
00219   };
00220 
00221   template <>
00222   class FromVecV<double> {
00223     public:
00224       static double cast(const fmatvec::VecV &x) {
00225         return x(0);
00226       }
00227   };
00228 
00229 }
00230 
00231 #endif