miscellaneous-algorithms.hpp
Go to the documentation of this file.
1 
11 #ifndef STATEOBSERVATIONTOOLSMISCELANEOUSALGORITHMS
12 #define STATEOBSERVATIONTOOLSMISCELANEOUSALGORITHMS
13 #include <boost/utility.hpp>
14 #include <cmath>
15 
16 #include <state-observation/api.h>
18 
19 namespace stateObservation
20 {
21 namespace tools
22 {
23 
25 template<class T>
26 inline T square(const T & x)
27 {
28  return T(x * x);
29 }
30 
32 template<class T>
33 inline T derivate(const T & o1, const T & o2, double dt)
34 {
35  T o(o2 - o1);
36  return o * (1 / dt);
37 }
38 
40 template<typename T>
41 inline int signum(T x)
42 {
43  return (T(0) < x) - (x < T(0));
44 }
45 
46 template<typename T>
47 inline std::string toString(T val)
48 {
49  std::stringstream ss("");
50  ss << val;
51  return ss.str();
52 }
53 
59 inline bool checkIfNormalized(const Vector3 & v)
60 {
61  if(fabs(v.squaredNorm() - 1) > cst::epsilon1)
62  {
63  return false;
64  }
65  else
66  {
67  return true;
68  }
69 }
70 
77 inline bool checkIfNormalized(const Vector3 & v, double & outputSquaredNorm)
78 {
79  outputSquaredNorm = v.squaredNorm();
80  if(fabs(outputSquaredNorm - 1) > cst::epsilon1)
81  {
82  return false;
83  }
84  else
85  {
86  return true;
87  }
88 }
89 
97 template<typename T>
98 inline T clampScalar(const T & x, const T & max, const T & min)
99 {
100  if(x > max)
101  {
102  return max;
103  }
104  else
105  {
106  if(x < min)
107  {
108  return min;
109  }
110  else
111  {
112  return x;
113  }
114  }
115 }
116 
123 template<typename T>
124 inline T clampScalar(const T & x, const T & limit)
125 {
126  return clampScalar(x, limit, -limit);
127 }
128 
133 inline Vector3 normalizedLazy(const Vector3 & v)
134 {
135  double squaredNorm;
136  if(checkIfNormalized(v, squaredNorm))
137  {
138  return v;
139  }
140  else
141  {
142  return v / sqrt(squaredNorm);
143  }
144 }
145 
149 inline double STATE_OBSERVATION_DLLAPI finiteTimeAccControl(double x, double xd, double kp = -1, double kv = -1)
150 {
151  double sax = sqrt(fabs(x));
152  double xdr = kp * signum(x) * sax;
153  double y = xd - xdr;
154  double ydr = -kv * signum(y) * sqrt(fabs(y));
155  return ydr - kp * xd / (2 * sax);
156 }
157 
159 inline Vector STATE_OBSERVATION_DLLAPI finiteTimeAccControl(const Vector & x,
160  const Vector & xd,
161  double kp = -1,
162  double kv = -1)
163 {
164  Vector xdd(x.size());
165  for(Index i = 1; i < x.size(); ++i)
166  {
167  xdd(i) = finiteTimeAccControl(x(i), xd(i), kp, kv);
168  }
169  return xdd;
170 }
171 
172 namespace Detail
173 {
174 double constexpr sqrtNewtonRaphson(double x, double curr, double prev)
175 {
176  return curr == prev ? curr : sqrtNewtonRaphson(x, 0.5 * (curr + x / curr), curr);
177 }
178 } // namespace Detail
179 
186 double constexpr sqrt(double x)
187 {
188  return x >= 0 && x < std::numeric_limits<double>::infinity() ? Detail::sqrtNewtonRaphson(x, x, 0)
189  : std::numeric_limits<double>::quiet_NaN();
190 }
191 
192 } // namespace tools
193 
194 } // namespace stateObservation
195 
196 #endif // STATEOBSERVATIONTOOLSMISCELANEOUSALGORITHMS
stateObservation::Vector
Eigen::VectorXd Vector
Dynamic sized scalar vector.
Definition: definitions.hpp:76
stateObservation::tools::sqrt
constexpr double sqrt(double x)
Constexpr version of the square root.
Definition: miscellaneous-algorithms.hpp:186
stateObservation::tools::square
T square(const T &x)
computes the square of a value of any type
Definition: miscellaneous-algorithms.hpp:26
stateObservation::tools::clampScalar
T clampScalar(const T &x, const T &max, const T &min)
returns the value clamped between min and max
Definition: miscellaneous-algorithms.hpp:98
stateObservation::tools::derivate
T derivate(const T &o1, const T &o2, double dt)
derivates any type with finite differences
Definition: miscellaneous-algorithms.hpp:33
stateObservation::tools::checkIfNormalized
bool checkIfNormalized(const Vector3 &v)
checks if the vector is already normalized or not
Definition: miscellaneous-algorithms.hpp:59
stateObservation::tools::finiteTimeAccControl
double STATE_OBSERVATION_DLLAPI finiteTimeAccControl(double x, double xd, double kp=-1, double kv=-1)
Definition: miscellaneous-algorithms.hpp:149
stateObservation::tools::normalizedLazy
Vector3 normalizedLazy(const Vector3 &v)
normalize the vector only if it is not normalized already. Useful if the vector is likely to be norma...
Definition: miscellaneous-algorithms.hpp:133
stateObservation::tools::signum
int signum(T x)
gives the sign of a variable (1, 0 or -1)
Definition: miscellaneous-algorithms.hpp:41
stateObservation::Index
Eigen::Index Index
Definition: definitions.hpp:138
definitions.hpp
Definitions of types and some structures.
stateObservation::tools::Detail::sqrtNewtonRaphson
constexpr double sqrtNewtonRaphson(double x, double curr, double prev)
Definition: miscellaneous-algorithms.hpp:174
stateObservation::cst::epsilon1
constexpr double epsilon1
number considered zero when compared to 1
Definition: definitions.hpp:672
stateObservation::Vector3
Eigen::Vector3d Vector3
3D vector
Definition: definitions.hpp:85
stateObservation
Definition: bidim-elastic-inv-pendulum-dyn-sys.hpp:20
stateObservation::tools::toString
std::string toString(T val)
Definition: miscellaneous-algorithms.hpp:47