mbsim  4.0.0
MBSim Kernel
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/mbsim_event.h"
26#include <mbsim/numerics/csparse.h>
27#include <mbxmlutilshelper/dom.h>
28#include <xercesc/dom/DOMDocument.hpp>
29#include <limits>
30#include <vector>
31#include <set>
32#include <boost/lexical_cast.hpp>
33#include <boost/algorithm/string/trim.hpp>
34
35namespace MBSim {
36
40 double sign(double x);
41
45 inline double mod(double x, double y) { return x - y * floor (x / y); }
46
53 double ArcTan(double x,double y);
54
58 fmatvec::Mat cs2Mat(cs* sparseMat);
59
60 template <class Arg>
61 class ToDouble {
62 };
63
64 template <>
65 class ToDouble<double> {
66 public:
67 static double cast(const double &x) {
68 return x;
69 }
70 };
71
72 template <class Col>
73 class ToDouble<fmatvec::Vector<Col,double>> {
74 public:
75 static double cast(const fmatvec::Vector<Col,double> &x) {
76 return x.e(0);
77 }
78 };
79
80 template <class Row>
81 class ToDouble<fmatvec::RowVector<Row,double>> {
82 public:
83 static double cast(const fmatvec::RowVector<Row,double> &x) {
84 return x.e(0);
85 }
86 };
87
88 template <class Ret>
89 class FromDouble {
90 public:
91 static Ret cast(double x) {
92 throw std::runtime_error("FromDouble::cast not implemented for current type.");
93 }
94 };
95
96 template <class Col>
97 class FromDouble<fmatvec::Vector<Col,double>> {
98 public:
99 static fmatvec::Vector<Col,double> cast(double x) {
100 return fmatvec::Vector<Col,double>(1,fmatvec::INIT,x);
101 }
102 };
103
104 template <class Col>
105 class FromDouble<fmatvec::RowVector<Col,double>> {
106 public:
107 static fmatvec::RowVector<Col,double> cast(double x) {
108 return fmatvec::RowVector<Col,double>(1,fmatvec::INIT,x);
109 }
110 };
111
112 template <class Row, class Col>
113 class FromDouble<fmatvec::Matrix<fmatvec::General,Row,Col,double>> {
114 public:
115 static fmatvec::Matrix<fmatvec::General,Row,Col,double> cast(double x) {
116 return fmatvec::Matrix<fmatvec::General,Row,Col,double>(1,1,fmatvec::INIT,x);
117 }
118 };
119
120 template <>
121 class FromDouble<double> {
122 public:
123 static double cast(double x) {
124 return x;
125 }
126 };
127
128 template <class Ret>
129 class FromVecV {
130 public:
131 static Ret cast(const fmatvec::VecV &x) {
132 return x;
133 }
134 };
135
136 template <>
137 class FromVecV<double> {
138 public:
139 static double cast(const fmatvec::VecV &x) {
140 return x(0);
141 }
142 };
143
144 template <typename T>
146 public:
147 static fmatvec::Mat cast(const std::vector<T> &x) {
148 fmatvec::Mat y(x.size(),x[0].cols(),fmatvec::NONINIT);
149 for (unsigned int i=0; i<x.size(); i++)
150 y.set(i,x[i]);
151 return y;
152 }
153 };
154
155 template <>
156 class FromStdvec<double> {
157 public:
158 static fmatvec::Vec cast(const std::vector<double> &x) {
159 fmatvec::Vec y(x.size(),fmatvec::NONINIT);
160 for (unsigned int i=0; i<x.size(); i++)
161 y(i)=x[i];
162 return y;
163 }
164 };
165
170 fmatvec::Vec3 computeTangential(const fmatvec::Vec3 &n);
171
172}
173
174#endif
Definition: utils.h:89
Definition: utils.h:145
Definition: utils.h:129
Definition: utils.h:61
namespace MBSim
Definition: bilateral_constraint.cc:30
double sign(double x)
Compute the sign of x.
Definition: utils.cc:30
double mod(double x, double y)
Compute the modulo of x and y.
Definition: utils.h:45
Mat cs2Mat(cs *sparseMat)
calculate a fmatvec::Mat out of a sparse matrix
Definition: utils.cc:48
double ArcTan(double x, double y)
calculates planar angle in [0,2\pi] with respect to Cartesian coordinates of: Arc Tangent (y/x)
Definition: utils.cc:39
Definition: csparse.h:21