state-observation 1.7.0
Loading...
Searching...
No Matches
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
19namespace stateObservation
20{
21namespace tools
22{
23
25template<class T>
26inline T square(const T & x)
27{
28 return T(x * x);
29}
30
32template<class T>
33inline T derivate(const T & o1, const T & o2, double dt)
34{
35 T o(o2 - o1);
36 return o * (1 / dt);
37}
38
40template<typename T>
41inline int signum(T x)
42{
43 return (T(0) < x) - (x < T(0));
44}
45
46template<typename T>
47inline std::string toString(T val)
48{
49 std::stringstream ss("");
50 ss << val;
51 return ss.str();
52}
53
59inline 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
77inline bool checkIfNormalized(const Vector3 & v, double & outputSquaredNorm)
78{
79 outputSquaredNorm = v.squaredNorm();
81 {
82 return false;
83 }
84 else
85 {
86 return true;
87 }
88}
89
97template<typename T>
98inline 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
123template<typename T>
124inline T clampScalar(const T & x, const T & limit)
125{
126 return clampScalar(x, limit, -limit);
127}
128
134{
135 double squaredNorm;
137 {
138 return v;
139 }
140 else
141 {
142 return v / sqrt(squaredNorm);
143 }
144}
145
149inline 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
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
172namespace Detail
173{
174double 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
186double constexpr sqrt(double x)
187{
189 : std::numeric_limits<double>::quiet_NaN();
190}
191
192} // namespace tools
193
194} // namespace stateObservation
195
196#endif // STATEOBSERVATIONTOOLSMISCELANEOUSALGORITHMS
Definitions of types and some structures.
constexpr double epsilon1
number considered zero when compared to 1
Definition definitions.hpp:672
double constexpr sqrtNewtonRaphson(double x, double curr, double prev)
Definition miscellaneous-algorithms.hpp:174
double constexpr sqrt(double x)
Constexpr version of the square root.
Definition miscellaneous-algorithms.hpp:186
T clampScalar(const T &x, const T &max, const T &min)
returns the value clamped between min and max
Definition miscellaneous-algorithms.hpp:98
T derivate(const T &o1, const T &o2, double dt)
derivates any type with finite differences
Definition miscellaneous-algorithms.hpp:33
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
int signum(T x)
gives the sign of a variable (1, 0 or -1)
Definition miscellaneous-algorithms.hpp:41
double STATE_OBSERVATION_DLLAPI finiteTimeAccControl(double x, double xd, double kp=-1, double kv=-1)
Definition miscellaneous-algorithms.hpp:149
std::string toString(T val)
Definition miscellaneous-algorithms.hpp:47
bool checkIfNormalized(const Vector3 &v)
checks if the vector is already normalized or not
Definition miscellaneous-algorithms.hpp:59
T square(const T &x)
computes the square of a value of any type
Definition miscellaneous-algorithms.hpp:26
Definition bidim-elastic-inv-pendulum-dyn-sys.hpp:21
Eigen::Vector3d Vector3
3D vector
Definition definitions.hpp:85
Eigen::MatrixXd Matrix
Dynamic sized Matrix.
Definition definitions.hpp:100
Eigen::Index Index
Definition definitions.hpp:138
Eigen::VectorXd Vector
Dynamic sized scalar vector.
Definition definitions.hpp:76