ARGoS 3
A parallel, multi-engine simulator for swarm robotics
general.h
Go to the documentation of this file.
1
9#ifndef GENERAL_H
10#define GENERAL_H
11
12#include <argos3/core/utility/datatypes/datatypes.h>
13#include <vector>
14#include <utility>
15#include <cmath>
16
17namespace argos {
18 /****************************************/
19 /****************************************/
20
25 template<typename T> T Abs(const T& t_v) {
26 if (t_v > T(0)) return t_v;
27 if (t_v < T(0)) return -t_v;
28 return T(0);
29 }
30
37 inline SInt32 Abs(SInt32 t_v) {
38 if (t_v > 0) return t_v;
39 if (t_v < 0) return -t_v;
40 return 0;
41 }
42
48 inline Real Abs(Real t_v) {
49 if (t_v > 0.0f) return t_v;
50 if (t_v < 0.0f) return -t_v;
51 return 0.0f;
52 }
53
54 /****************************************/
55 /****************************************/
56
57#ifdef ARGOS_USE_DOUBLE
58#define Log ::log
59#define Sqrt ::sqrt
60#define Exp ::exp
61#define Mod ::fmod
62#else
63#define Log ::logf
64#define Sqrt ::sqrtf
65#define Exp ::expf
66#define Mod ::fmodf
67#endif
68
69 /****************************************/
70 /****************************************/
71
77 template<typename T> T Min(const T& t_v1, const T& t_v2) {
78 return t_v1 < t_v2 ? t_v1 : t_v2;
79 }
80
86 template<typename T> T& Min(T& t_v1, T& t_v2) {
87 return t_v1 < t_v2 ? t_v1 : t_v2;
88 }
89
95 template<typename T> T Max(const T& t_v1, const T& t_v2) {
96 return t_v1 > t_v2 ? t_v1 : t_v2;
97 }
98
104 template<typename T> T& Max(T& t_v1, T& t_v2) {
105 return t_v1 > t_v2 ? t_v1 : t_v2;
106 }
107
108 /****************************************/
109 /****************************************/
110
115 template<typename T> SInt32 Sign(const T& t_v) {
116 if (t_v > T(0)) return 1;
117 if (t_v < T(0)) return -1;
118 return 0;
119 }
120
121 /****************************************/
122 /****************************************/
123
128 template<typename T> T Square(const T& t_v) {
129 return t_v * t_v;
130 }
131
132 /****************************************/
133 /****************************************/
134
140 inline SInt32 Floor(Real f_value) {
141 SInt32 nI = static_cast<SInt32> (f_value);
142 if (f_value >= 0.0f) return nI;
143 return nI - 1;
144 }
145
151 inline SInt32 Ceil(Real f_value) {
152 SInt32 nI = static_cast<SInt32> (f_value);
153 if (nI < f_value) return nI + 1;
154 return nI;
155 }
156
162 inline SInt32 Round(Real f_value) {
163 if (f_value > 0.0f) return Floor(f_value + 0.5f);
164 return Ceil(f_value - 0.5f);
165 }
166
173 if (f_value > 0.0f) return Floor(f_value);
174 return Ceil(f_value);
175 }
176
177 /****************************************/
178 /****************************************/
179
194 inline bool DoubleEqAbsolute(Real f_value1, Real f_value2, Real f_epsilon) {
195 return Abs<Real > (f_value1 - f_value2) <= f_epsilon * Max<Real > (1.0f, Max<Real > (Abs<Real > (f_value1), Abs<Real > (f_value2)));
196 }
197
206 inline bool DoubleEq(Real f_value1, Real f_value2) {
207 return Abs<Real > (f_value1 - f_value2) <= 0.0001f * Max<Real > (1.0f, Max<Real > (Abs<Real > (f_value1), Abs<Real > (f_value2)));
208 }
209
210 /****************************************/
211 /****************************************/
212
219 inline Real Interpolate(Real f_x, const std::vector<std::pair<Real, Real> >& c_points) {
220 std::pair<Real, Real> cP0 = c_points.at(0);
221 std::pair<Real, Real> cP1;
222 for (UInt32 i = 1; i < c_points.size(); ++i) {
223 cP1 = c_points.at(i);
224 if (cP1.first >= f_x) {
225 break;
226 } else if (i < c_points.size() - 1) {
227 cP0 = cP1;
228 }
229 }
230 return (f_x * (cP0.second - cP1.second)) / (cP0.first - cP1.first)+(-cP1.first * cP0.second + cP0.first * cP1.second) / (cP0.first - cP1.first);;
231 }
232}
233
234#endif
signed int SInt32
32-bit signed integer.
Definition datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
float Real
Collects all ARGoS code.
Definition datatypes.h:39
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
bool DoubleEqAbsolute(Real f_value1, Real f_value2, Real f_epsilon)
Tests whether a floating-point value is lower than another.
Definition general.h:194
T Square(const T &t_v)
Returns the square of the value of the passed argument.
Definition general.h:128
bool DoubleEq(Real f_value1, Real f_value2)
Tests whether a floating-point value is lower than another.
Definition general.h:206
SInt32 Ceil(Real f_value)
Rounds the passed floating-point value to the closest higher integer.
Definition general.h:151
SInt32 Round(Real f_value)
Rounds the passed floating-point value to the closest integer.
Definition general.h:162
Real Interpolate(Real f_x, const std::vector< std::pair< Real, Real > > &c_points)
Return the value of the linear interpolation.
Definition general.h:219
SInt32 Sign(const T &t_v)
Returns the sign of the value of the passed argument.
Definition general.h:115
SInt32 Floor(Real f_value)
Rounds the passed floating-point value to the closest lower integer.
Definition general.h:140
SInt32 RoundClosestToZero(Real f_value)
Rounds the passed floating-point value to the integer closest to zero.
Definition general.h:172
T Max(const T &t_v1, const T &t_v2)
Returns the bigger of the two passed arguments.
Definition general.h:95
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition general.h:25
T Min(const T &t_v1, const T &t_v2)
Returns the smaller of the two passed arguments.
Definition general.h:77