g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
stuff
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
44
45
namespace
g2o
{
46
48
template
<
class
T1,
class
T2,
class
Pred = std::less<T1> >
49
struct
CmpPairFirst
{
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
60
inline
constexpr
double
cst
(
long
double
v) {
return
(
double
)v; }
61
62
constexpr
double
const_pi
() {
return
cst
(3.14159265358979323846); }
63
67
template
<
typename
T>
68
inline
T
square
(T x) {
69
return
x * x;
70
}
71
75
template
<
typename
T>
76
inline
T
hypot
(T x, T y) {
77
return
(T)(std::sqrt(x * x + y * y));
78
}
79
83
template
<
typename
T>
84
inline
T
hypot_sqr
(T x, T y) {
85
return
x * x + y * y;
86
}
87
91
inline
double
deg2rad
(
double
degree) {
92
return
degree *
cst
(0.01745329251994329576);
93
}
94
98
inline
double
rad2deg
(
double
rad) {
return
rad *
cst
(57.29577951308232087721); }
99
103
inline
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
112
inline
double
inverse_theta
(
double
th) {
113
return
normalize_theta
(th +
const_pi
());
114
}
115
119
inline
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
134
template
<
typename
T>
135
inline
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
147
template
<
typename
T>
148
inline
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
157
template
<
typename
T>
158
inline
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
168
inline
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
180
extern
"C"
{
181
typedef
void (*
ForceLinkFunction
)(void);
182
}
183
184
struct
ForceLinker
{
185
ForceLinker
(
ForceLinkFunction
function) { (function)(); }
186
};
187
188
}
// namespace g2o
189
190
// @}
191
192
#endif
macros.h
g2o_isnan
#define g2o_isnan(x)
Definition
macros.h:100
g2o
Definition
dl_wrapper.cpp:55
g2o::ForceLinkFunction
void(* ForceLinkFunction)(void)
Definition
misc.h:181
g2o::arrayHasNaN
bool arrayHasNaN(const double *array, int size, int *nanIndex=0)
Definition
misc.h:168
g2o::wrap
T wrap(T l, T x, T u)
Definition
misc.h:158
g2o::average_angle
double average_angle(double theta1, double theta2)
Definition
misc.h:119
g2o::rad2deg
double rad2deg(double rad)
Definition
misc.h:98
g2o::clamp
T clamp(T l, T x, T u)
Definition
misc.h:148
g2o::hypot
T hypot(T x, T y)
Definition
misc.h:76
g2o::sign
int sign(T x)
Definition
misc.h:135
g2o::cst
constexpr double cst(long double v)
Definition
misc.h:60
g2o::deg2rad
double deg2rad(double degree)
Definition
misc.h:91
g2o::inverse_theta
double inverse_theta(double th)
Definition
misc.h:112
g2o::normalize_theta
double normalize_theta(double theta)
Definition
misc.h:103
g2o::hypot_sqr
T hypot_sqr(T x, T y)
Definition
misc.h:84
g2o::square
T square(T x)
Definition
misc.h:68
g2o::const_pi
constexpr double const_pi()
Definition
misc.h:62
g2o::CmpPairFirst
Definition
misc.h:49
g2o::CmpPairFirst::operator()
bool operator()(const std::pair< T1, T2 > &left, const std::pair< T1, T2 > &right)
Definition
misc.h:50
g2o::ForceLinker::ForceLinker
ForceLinker(ForceLinkFunction function)
Definition
misc.h:185
Generated on
for g2o by
1.17.0