g2o
Loading...
Searching...
No Matches
misc.h
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef G2O_STUFF_MISC_H
28#define G2O_STUFF_MISC_H
29
30#include <cmath>
31#include <memory>
32
33#include "g2o/config.h"
34#include "macros.h"
35
37// @{
38
45namespace g2o {
46
48template <class T1, class T2, class Pred = std::less<T1> >
50 bool operator()(const std::pair<T1, T2>& left,
51 const std::pair<T1, T2>& right) {
52 return Pred()(left.first, right.first);
53 }
54};
55
60inline constexpr double cst(long double v) { return (double)v; }
61
62constexpr double const_pi() { return cst(3.14159265358979323846); }
63
67template <typename T>
68inline T square(T x) {
69 return x * x;
70}
71
75template <typename T>
76inline T hypot(T x, T y) {
77 return (T)(std::sqrt(x * x + y * y));
78}
79
83template <typename T>
84inline T hypot_sqr(T x, T y) {
85 return x * x + y * y;
86}
87
91inline double deg2rad(double degree) {
92 return degree * cst(0.01745329251994329576);
93}
94
98inline double rad2deg(double rad) { return rad * cst(57.29577951308232087721); }
99
103inline double normalize_theta(double theta) {
104 const double result = fmod(theta + const_pi(), 2.0 * const_pi());
105 if (result <= 0.0) return result + const_pi();
106 return result - const_pi();
107}
108
112inline double inverse_theta(double th) {
113 return normalize_theta(th + const_pi());
114}
115
119inline double average_angle(double theta1, double theta2) {
120 double x, y;
121
122 x = std::cos(theta1) + std::cos(theta2);
123 y = std::sin(theta1) + std::sin(theta2);
124 if (x == 0 && y == 0)
125 return 0;
126 else
127 return std::atan2(y, x);
128}
129
134template <typename T>
135inline int sign(T x) {
136 if (x > 0)
137 return 1;
138 else if (x < 0)
139 return -1;
140 else
141 return 0;
142}
143
147template <typename T>
148inline T clamp(T l, T x, T u) {
149 if (x < l) return l;
150 if (x > u) return u;
151 return x;
152}
153
157template <typename T>
158inline T wrap(T l, T x, T u) {
159 T intervalWidth = u - l;
160 while (x < l) x += intervalWidth;
161 while (x > u) x -= intervalWidth;
162 return x;
163}
164
168inline bool arrayHasNaN(const double* array, int size, int* nanIndex = 0) {
169 for (int i = 0; i < size; ++i)
170 if (g2o_isnan(array[i])) {
171 if (nanIndex) *nanIndex = i;
172 return true;
173 }
174 return false;
175}
176
180extern "C" {
181typedef void (*ForceLinkFunction)(void);
182}
183
185 ForceLinker(ForceLinkFunction function) { (function)(); }
186};
187
188} // namespace g2o
189
190// @}
191
192#endif
#define g2o_isnan(x)
Definition macros.h:100
void(* ForceLinkFunction)(void)
Definition misc.h:181
bool arrayHasNaN(const double *array, int size, int *nanIndex=0)
Definition misc.h:168
T wrap(T l, T x, T u)
Definition misc.h:158
double average_angle(double theta1, double theta2)
Definition misc.h:119
double rad2deg(double rad)
Definition misc.h:98
T clamp(T l, T x, T u)
Definition misc.h:148
int sign(T x)
Definition misc.h:135
constexpr double cst(long double v)
Definition misc.h:60
double deg2rad(double degree)
Definition misc.h:91
double inverse_theta(double th)
Definition misc.h:112
double normalize_theta(double theta)
Definition misc.h:103
T hypot_sqr(T x, T y)
Definition misc.h:84
T square(T x)
Definition misc.h:68
constexpr double const_pi()
Definition misc.h:62
bool operator()(const std::pair< T1, T2 > &left, const std::pair< T1, T2 > &right)
Definition misc.h:50
ForceLinker(ForceLinkFunction function)
Definition misc.h:185