humoto
utility.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @author Jan Michalczyk
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 namespace humoto
14 {
15  //========================================================================
16  // Shorthand types
17  //========================================================================
18 
19  typedef Eigen::Matrix<unsigned int, Eigen::Dynamic, 1> IndexVector;
20 
21  /**
22  * Index type used by Eigen. We should not use Eigen::DenseIndex since it
23  * is going to be deprecated in new versions of Eigen. May be EigenIndex
24  * should be defined depending on the version of Eigen.
25  */
26  typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex;
27 
28 
29  //========================================================================
30  // Generic types
31  //========================================================================
32 
33 
34  /**
35  * @brief Index of an axis
36  */
37  class AxisIndex
38  {
39  public:
40  enum Index
41  {
42  X = 0,
43  Y = 1,
44  Z = 2
45  };
46 
47  enum Flag
48  {
50  // Z Y X
51  FLAG_X = 1,
52  FLAG_Y = 2,
53  FLAG_Z = 4
54  };
55  };
56 
57 
58  /**
59  * @brief Indices of RPY angles
60  */
61  class AngleIndex
62  {
63  public:
64  enum Index
65  {
66  ROLL = 0,
67  PITCH = 1,
68  YAW = 2
69  };
70  };
71 
72 
73  //========================================================================
74  // Math
75  //========================================================================
76 
77  /**
78  * @brief Returns true if the difference between two given variables is
79  * below the given tolerance.
80  *
81  * @param[in] var1
82  * @param[in] var2
83  * @param[in] tol
84  *
85  * @return true / false
86  */
87  inline bool isApproximatelyEqual( const double var1,
88  const double var2,
89  const double tol = humoto::g_generic_tolerance)
90  {
91  return (std::abs(var1 - var2) < tol);
92  }
93 
94 
95  //========================================================================
96  // Geometry
97  //========================================================================
98 
99 
100  /**
101  * @brief Convert degrees to radians
102  *
103  * @param[in] degrees degrees
104  *
105  * @return radians
106  */
107  inline double HUMOTO_LOCAL convertDegreesToRadians(const double degrees)
108  {
109  return (humoto::g_pi * degrees / 180);
110  }
111 
112 
113  /**
114  * @brief Convert radians to degrees
115  *
116  * @param[in] radians
117  *
118  * @return degrees
119  */
120  inline double HUMOTO_LOCAL convertRadiansToDegrees(const double radians)
121  {
122  return (radians * 180 / humoto::g_pi);
123  }
124 
125 
126  /**
127  * @brief Function computing side length of a square
128  * inscribed inside of a circle of given radius.
129  *
130  * @param radius circle radius
131  * @return length of a side of a square inscribed in the circle
132  */
133  inline double HUMOTO_LOCAL getEncircledSquareSide(const double radius)
134  {
135  return((2. / std::sqrt(2.)) * radius);
136  }
137 
138 
139  //========================================================================
140  // Simple generic classes
141  //========================================================================
142 
143  /**
144  * @brief Location of a data chunk (offset + length).
145  */
147  {
148  public:
149  std::size_t offset_;
150  std::size_t length_;
151 
152 
153  /**
154  * @brief Constructor
155  */
157  {
158  set(0, 0);
159  }
160 
161 
162  /**
163  * @brief Construct location with given parameters
164  *
165  * @param[in] offset offset
166  * @param[in] length length
167  */
168  Location (const std::size_t offset, const std::size_t length)
169  {
170  set(offset, length);
171  }
172 
173 
174  /**
175  * @brief Compare two locations
176  *
177  * @param[in] another_location some other location
178  *
179  * @return true/false
180  */
181  bool operator== (const Location &another_location) const
182  {
183  return ( (offset_ == another_location.offset_) &&
184  (length_ == another_location.length_) );
185  }
186 
187 
188  /**
189  * @brief Compare two locations
190  *
191  * @param[in] another_location some other location
192  *
193  * @return true/false
194  */
195  bool operator!= (const Location &another_location) const
196  {
197  return ( !(*this == another_location) );
198  }
199 
200 
201  /**
202  * @brief Check length of location
203  *
204  * @param[in] max_length maximal length
205  *
206  * @return true if location is valid
207  */
208  bool checkLength(const std::size_t max_length) const
209  {
210  return ( (offset_ <= end()) &&
211  (end() <= max_length) );
212  }
213 
214 
215  /**
216  * @brief Offset of the first element
217  *
218  * @return offset of the first element (offset_)
219  */
220  std::size_t front() const
221  {
222  return(offset_);
223  }
224 
225 
226  /**
227  * @brief Offset of the element following the last element
228  *
229  * @return offset of the element following the last element (offset_ + length_)
230  */
231  std::size_t end() const
232  {
233  return(offset_ + length_);
234  }
235 
236 
237  /**
238  * @brief Set location.
239  *
240  * @param[in] offset offset
241  * @param[in] length length
242  */
243  void set(const std::size_t offset, const std::size_t length)
244  {
245  offset_ = offset;
246  length_ = length;
247  }
248 
249 
250  /**
251  * @brief Log hierarchy as a set of tasks
252  *
253  * @param[in,out] logger logger
254  * @param[in] parent parent
255  * @param[in] name name
256  */
258  const LogEntryName & parent = LogEntryName(),
259  const std::string & name = "location") const
260  {
261  LogEntryName subname = parent;
262  subname.add(name);
263 
264  logger.log(LogEntryName(subname).add("offset"), offset_);
265  logger.log(LogEntryName(subname).add("length"), length_);
266  }
267  };
268 }
bool checkLength(const std::size_t max_length) const
Check length of location.
Definition: utility.h:208
std::size_t front() const
Offset of the first element.
Definition: utility.h:220
#define HUMOTO_LOCAL
Definition: export_import.h:26
Index of an axis.
Definition: utility.h:37
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
Location()
Constructor.
Definition: utility.h:156
Represents log entry name.
Definition: logger.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
Definition: utility.h:26
std::size_t length_
Definition: utility.h:150
Eigen::Matrix< unsigned int, Eigen::Dynamic, 1 > IndexVector
Definition: utility.h:19
double HUMOTO_LOCAL convertRadiansToDegrees(const double radians)
Convert radians to degrees.
Definition: utility.h:120
double HUMOTO_LOCAL convertDegreesToRadians(const double degrees)
Convert degrees to radians.
Definition: utility.h:107
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="location") const
Log hierarchy as a set of tasks.
Definition: utility.h:257
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
Indices of RPY angles.
Definition: utility.h:61
The root namespace of HuMoTo.
Definition: config.h:12
std::size_t end() const
Offset of the element following the last element.
Definition: utility.h:231
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
Location(const std::size_t offset, const std::size_t length)
Construct location with given parameters.
Definition: utility.h:168
bool isApproximatelyEqual(const double var1, const double var2, const double tol=humoto::g_generic_tolerance)
Returns true if the difference between two given variables is below the given tolerance.
Definition: utility.h:87
Location of a data chunk (offset + length).
Definition: utility.h:146
const double g_pi
PI constant.
Definition: constants.h:18
std::size_t offset_
Definition: utility.h:149
double HUMOTO_LOCAL getEncircledSquareSide(const double radius)
Function computing side length of a square inscribed inside of a circle of given radius.
Definition: utility.h:133
const double g_generic_tolerance
Generic tolerance, should be used by default.
Definition: constants.h:28