All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
utils.h
1 /* Copyright (C) 2004-2009 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: martin.o.foerg@googlemail.com
18  */
19 
20 #ifndef UTILS_H_
21 #define UTILS_H_
22 
23 #include <string>
24 #include "fmatvec/fmatvec.h"
25 #include <mbsim/numerics/csparse.h>
26 #include <mbxmlutilshelper/dom.h>
27 #include <xercesc/dom/DOMDocument.hpp>
28 #include <limits>
29 #include <vector>
30 #include <set>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string/trim.hpp>
33 
34 namespace MBSim {
35 
36  std::string numtostr(int i);
37  std::string numtostr(double d);
38  template<class Type, class Row, class Col>
39  std::string numtostr(fmatvec::Matrix<Type,Row,Col,double> m) {
40  std::ostringstream oss;
41  oss << "[ ";
42  for(int i=0;i<m.rows()-1;i++) {
43  for(int j=0;i<m.cols()-1;j++) oss << m.e(i,j) << ", ";
44  oss << "\n";
45  }
46  oss << "]";
47  return oss.str();
48  }
49 
50  double degtorad(double alpha);
51  double radtodeg(double phi);
52  fmatvec::Vec degtorad(fmatvec::Vec alpha);
53  fmatvec::Vec radtodeg(fmatvec::Vec phi);
54  fmatvec::Vec tildetovec(const fmatvec::SqrMat &A);
55 
56  double sign(double x);
57 
64  double ArcTan(double x,double y);
65 
69  fmatvec::Mat cs2Mat(cs* sparseMat);
70 
71  template <class T>
72  inline std::string toStr(const T &val) {
73  std::stringstream s;
74  s << std::setprecision(std::numeric_limits<double>::digits10+1) << val;
75  return s.str();
76  }
77 
78  inline xercesc::DOMNode* toXML(const std::string &str, xercesc::DOMNode* parent) {
79  return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%str);
80  }
81 
82  inline xercesc::DOMNode* toXML(int i, xercesc::DOMNode* parent) {
83  return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
84  }
85 
86  inline xercesc::DOMNode* toXML(unsigned int i, xercesc::DOMNode* parent) {
87  return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(i));
88  }
89 
90  inline xercesc::DOMNode* toXML(double d, xercesc::DOMNode* parent) {
91  return parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%toStr(d));
92  }
93 
94  template <class T>
95  inline xercesc::DOMNode* toXML(const std::vector<T> &x, xercesc::DOMNode* parent) {
96  xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlVector");
97  for(unsigned int i=0; i<x.size(); i++) {
98  xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"ele");
99  xercesc::DOMText *text = new xercesc::DOMText(toStr(x[i]));
100  elei->insertBefore(text, NULL);
101  ele->insertBefore(elei, NULL);
102  }
103  return ele;
104  }
105 
106  template <class Row>
107  inline xercesc::DOMNode* toXML(const fmatvec::Vector<Row,double> &x, xercesc::DOMNode* parent) {
108  xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlVector");
109  for(int i=0; i<x.size(); i++) {
110  xercesc::DOMDocument *doc=parent->getOwnerDocument();
111  xercesc::DOMElement *elei = MBXMLUtils::D(doc)->createElement(MBXMLUtils::PV%"ele");
112  xercesc::DOMText *text = doc->createTextNode(MBXMLUtils::X()%toStr(x.e(i)));
113  elei->insertBefore(text, NULL);
114  ele->insertBefore(elei, NULL);
115  }
116  return ele;
117  }
118 
119  template <class Type, class Row, class Col>
120  inline xercesc::DOMNode* toXML(const fmatvec::Matrix<Type,Row,Col,double> &A, xercesc::DOMNode* parent) {
121  xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"xmlMatrix");
122  for(int i=0; i<A.rows(); i++) {
123  xercesc::DOMElement *elei = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"row");
124  for(int j=0; j<A.cols(); j++) {
125  xercesc::DOMElement *elej = MBXMLUtils::D(parent->getOwnerDocument())->createElement(MBXMLUtils::PV%"ele");
126  xercesc::DOMText *text = new xercesc::DOMText(toStr(A.e(i,j)));
127  elej->insertBefore(text, NULL);
128  elei->insertBefore(elej, NULL);
129  }
130  ele->insertBefore(elei, NULL);
131  }
132  return ele;
133  }
134 
135  template<class T>
136  inline std::string funcExt() {
137  return "V";
138  }
139 
140  template < >
141  inline std::string funcExt<double>() {
142  return "S";
143  }
144 
145  template <class T>
146  void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const T &value) {
147  xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(name);
148  ele->insertBefore(toXML(value,parent), NULL);
149  parent->insertBefore(ele, NULL);
150  }
151 
152  template <class Arg>
153  class ToDouble {
154  };
155 
156  template <>
157  class ToDouble<double> {
158  public:
159  static double cast(const double &x) {
160  return x;
161  }
162  };
163 
164  template <class Col>
165  class ToDouble<fmatvec::Vector<Col,double> > {
166  public:
167  static double cast(const fmatvec::Vector<Col,double> &x) {
168  return x.e(0);
169  }
170  };
171 
172  template <class Row>
173  class ToDouble<fmatvec::RowVector<Row,double> > {
174  public:
175  static double cast(const fmatvec::RowVector<Row,double> &x) {
176  return x.e(0);
177  }
178  };
179 
180  template <class Ret>
181  class FromMatStr {
182  public:
183  static Ret cast(const char *x) {
184  return Ret(x);
185  }
186  };
187 
188  template <>
189  class FromMatStr<double> {
190  public:
191  static double cast(const std::string &x) {
192  return boost::lexical_cast<double>(boost::algorithm::trim_copy(x));
193  }
194  };
195 
196  template <class Ret>
197  class FromDouble {
198  public:
199  static Ret cast(double x) {
200  return Ret(1,fmatvec::INIT,x);
201  }
202  };
203 
204  template <>
205  class FromDouble<double> {
206  public:
207  static double cast(double x) {
208  return x;
209  }
210  };
211 
212  template <class Ret>
213  class FromVecV {
214  public:
215  static Ret cast(const fmatvec::VecV &x) {
216  return x;
217  }
218  };
219 
220  template <>
221  class FromVecV<double> {
222  public:
223  static double cast(const fmatvec::VecV &x) {
224  return x(0);
225  }
226  };
227 
232  fmatvec::Vec3 computeTangential(const fmatvec::Vec3 &n);
233 
234 }
235 
236 #endif
Definition: utils.h:213
int rows() const
int cols() const
Definition: utils.h:153
double ArcTan(double x, double y)
calculates planar angle in [0,2] with respect to Cartesian coordinates of: Arc Tangent (y/x) ...
Definition: utils.cc:72
Definition: csparse.h:20
Definition: utils.h:197
Mat cs2Mat(cs *sparseMat)
calculate a fmatvec::Mat out of a sparse matrix
Definition: utils.cc:81
Definition: utils.h:181

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML