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 "mbxmlutilstinyxml/tinyxml.h"
00029 #include <limits>
00030 #include <vector>
00031 #include <set>
00032 
00033 namespace MBSim {
00034 
00035 std::string numtostr(int i);   
00036 std::string numtostr(double d);   
00037 template<class Type, class Row, class Col>
00038 std::string numtostr(fmatvec::Matrix<Type,Row,Col,double> m) {
00039   std::ostringstream oss;
00040   oss << "[ ";
00041   for(int i=0;i<m.rows()-1;i++) {
00042     for(int j=0;i<m.cols()-1;j++) oss << m.e(i,j) << ", ";
00043     oss << "\n";
00044   }
00045   oss << "]";
00046   return oss.str(); 
00047 }
00048 
00049 double degtorad(double alpha);
00050 double radtodeg(double phi);
00051 fmatvec::Vec degtorad(fmatvec::Vec alpha);
00052 fmatvec::Vec radtodeg(fmatvec::Vec phi);
00053 fmatvec::Vec tildetovec(const fmatvec::SqrMat &A);
00054 
00055 double sign(double x);
00056 
00063 double ArcTan(double x,double y);
00064 
00065 template <class T>
00066 inline std::string toStr(const T &val) {
00067   std::stringstream s;
00068   s << std::setprecision(std::numeric_limits<double>::digits10+1) << val;
00069   return s.str();
00070 }
00071 
00072 inline MBXMLUtils::TiXmlNode* toXML(const std::string &str) {
00073   return new MBXMLUtils::TiXmlText(str);
00074 }
00075 
00076 inline MBXMLUtils::TiXmlNode* toXML(int i) {
00077   return new MBXMLUtils::TiXmlText(toStr(i));
00078 }
00079 
00080 inline MBXMLUtils::TiXmlNode* toXML(unsigned int i) {
00081   return new MBXMLUtils::TiXmlText(toStr(i));
00082 }
00083 
00084 inline MBXMLUtils::TiXmlNode* toXML(double d) {
00085   return new MBXMLUtils::TiXmlText(toStr(d));
00086 }
00087 
00088 template <class T>
00089 inline MBXMLUtils::TiXmlNode* toXML(const std::vector<T> &x) {
00090   MBXMLUtils::TiXmlElement *ele = new MBXMLUtils::TiXmlElement(PVNS"xmlVector");
00091   for(unsigned int i=0; i<x.size(); i++) {
00092     MBXMLUtils::TiXmlElement *elei = new MBXMLUtils::TiXmlElement(PVNS"ele");
00093     MBXMLUtils::TiXmlText *text = new MBXMLUtils::TiXmlText(toStr(x[i]));
00094     elei->LinkEndChild(text);
00095     ele->LinkEndChild(elei);
00096   }
00097   return ele;
00098 }
00099 
00100 template <class Row>
00101 inline MBXMLUtils::TiXmlNode* toXML(const fmatvec::Vector<Row,double> &x) {
00102   MBXMLUtils::TiXmlElement *ele = new MBXMLUtils::TiXmlElement(PVNS"xmlVector");
00103   for(int i=0; i<x.size(); i++) {
00104     MBXMLUtils::TiXmlElement *elei = new MBXMLUtils::TiXmlElement(PVNS"ele");
00105     MBXMLUtils::TiXmlText *text = new MBXMLUtils::TiXmlText(toStr(x.e(i)));
00106     elei->LinkEndChild(text);
00107     ele->LinkEndChild(elei);
00108   }
00109   return ele;
00110 }
00111 
00112 template <class Type, class Row, class Col>
00113 inline MBXMLUtils::TiXmlNode* toXML(const fmatvec::Matrix<Type,Row,Col,double> &A) {
00114   MBXMLUtils::TiXmlElement *ele = new MBXMLUtils::TiXmlElement(PVNS"xmlMatrix");
00115   for(int i=0; i<A.rows(); i++) {
00116     MBXMLUtils::TiXmlElement *elei = new MBXMLUtils::TiXmlElement(PVNS"row");
00117     for(int j=0; j<A.cols(); j++) {
00118       MBXMLUtils::TiXmlElement *elej = new MBXMLUtils::TiXmlElement(PVNS"ele");
00119       MBXMLUtils::TiXmlText *text = new MBXMLUtils::TiXmlText(toStr(A.e(i,j)));
00120       elej->LinkEndChild(text);
00121       elei->LinkEndChild(elej);
00122     }
00123     ele->LinkEndChild(elei);
00124   }
00125   return ele;
00126 }
00127 
00128 template<class T>
00129 inline std::string funcExt() {
00130   return "V";
00131 }
00132 
00133 template < >
00134 inline std::string funcExt<double>() {
00135   return "S";
00136 }
00137 
00138 template <class T>
00139 void addElementText(MBXMLUtils::TiXmlElement *parent, std::string name, const T &value) {
00140   parent->LinkEndChild(new MBXMLUtils::TiXmlElement(name))->LinkEndChild(toXML(value));
00141 }
00142 
00143 }
00144 
00145 #endif