forked from ApolloAuto/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
274 lines (238 loc) · 8.38 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
/**
* @file
* @brief Some util functions.
*/
#pragma once
#include <algorithm>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "google/protobuf/util/message_differencer.h"
#include "modules/common/proto/geometry.pb.h"
#include "modules/common/proto/pnc_point.pb.h"
#include "modules/common/math/vec2d.h"
// The helper function "std::make_unique()" is defined since C++14.
// The definition of "std::make_unique()" borrowed from C++14 is given here
// so that it can be used in C++11.
#if __cplusplus == 201103L
namespace std {
template <typename _Tp>
struct _MakeUniq {
typedef unique_ptr<_Tp> __single_object;
};
template <typename _Tp>
struct _MakeUniq<_Tp[]> {
typedef unique_ptr<_Tp[]> __array;
};
template <typename _Tp, size_t _Bound>
struct _MakeUniq<_Tp[_Bound]> {
struct __invalid_type {};
};
// std::make_unique for single objects
template <typename _Tp, typename... _Args>
inline typename _MakeUniq<_Tp>::__single_object make_unique(_Args&&... __args) {
return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
}
// Alias template for remove_extent
template <typename _Tp>
using remove_extent_t = typename remove_extent<_Tp>::type;
// std::make_unique for arrays of unknown bound
template <typename _Tp>
inline typename _MakeUniq<_Tp>::__array make_unique(size_t __num) {
return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]());
}
// Disable std::make_unique for arrays of known bound
template <typename _Tp, typename... _Args>
inline typename _MakeUniq<_Tp>::__invalid_type make_unique(_Args&&...) = delete;
} // namespace std
#endif
/**
* @namespace apollo::common::util
* @brief apollo::common::util
*/
namespace apollo {
namespace common {
namespace util {
template <typename ProtoA, typename ProtoB>
bool IsProtoEqual(const ProtoA& a, const ProtoB& b) {
return a.GetTypeName() == b.GetTypeName() &&
a.SerializeAsString() == b.SerializeAsString();
// Test shows that the above method is 5 times faster than the
// API: google::protobuf::util::MessageDifferencer::Equals(a, b);
}
struct PairHash {
template <typename T, typename U>
size_t operator()(const std::pair<T, U>& pair) const {
return std::hash<T>()(pair.first) ^ std::hash<U>()(pair.second);
}
};
template <typename T>
bool WithinBound(T start, T end, T value) {
return value >= start && value <= end;
}
/**
* @brief create a SL point
* @param s the s value
* @param l the l value
* @return a SLPoint instance
*/
SLPoint MakeSLPoint(const double s, const double l);
template <typename T>
common::math::Vec2d MakeVec2d(const T& t) {
return common::math::Vec2d(t.x(), t.y());
}
PointENU MakePointENU(const double x, const double y, const double z);
PointENU operator+(const PointENU enu, const math::Vec2d& xy);
PointENU MakePointENU(const math::Vec2d& xy);
SpeedPoint MakeSpeedPoint(const double s, const double t, const double v,
const double a, const double da);
PathPoint MakePathPoint(const double x, const double y, const double z,
const double theta, const double kappa,
const double dkappa, const double ddkappa);
/**
* uniformly slice a segment [start, end] to num + 1 pieces
* the result sliced will contain the n + 1 points that slices the provided
* segment. `start` and `end` will be the first and last element in `sliced`.
*/
template <typename T>
void uniform_slice(const T start, const T end, uint32_t num,
std::vector<T>* sliced) {
if (!sliced || num == 0) {
return;
}
const T delta = (end - start) / num;
sliced->resize(num + 1);
T s = start;
for (uint32_t i = 0; i < num; ++i, s += delta) {
sliced->at(i) = s;
}
sliced->at(num) = end;
}
template <typename Container>
typename Container::value_type MaxElement(const Container& elements) {
return *std::max_element(elements.begin(), elements.end());
}
template <typename Container>
typename Container::value_type MinElement(const Container& elements) {
return *std::min_element(elements.begin(), elements.end());
}
template <typename T>
std::unordered_set<T> Intersection(const std::unordered_set<T>& s1,
const std::unordered_set<T>& s2) {
if (s1.size() < s2.size()) {
std::unordered_set<T> result;
for (const auto& v : s1) {
if (s2.count(v) > 0) {
result.insert(v);
}
}
return result;
} else {
return intersection(s2, s1);
}
}
/**
* calculate the distance beteween Point u and Point v, which are all have
* member function x() and y() in XY dimension.
* @param u one point that has member function x() and y().
* @param b one point that has member function x() and y().
* @return sqrt((u.x-v.x)^2 + (u.y-v.y)^2), i.e., the Euclid distance on XY
* dimension.
*/
template <typename U, typename V>
double DistanceXY(const U& u, const V& v) {
return std::hypot(u.x() - v.x(), u.y() - v.y());
}
/**
* Check if two points u and v are the same point on XY dimension.
* @param u one point that has member function x() and y().
* @param v one point that has member function x() and y().
* @return sqrt((u.x-v.x)^2 + (u.y-v.y)^2) < epsilon, i.e., the Euclid distance
* on XY dimension.
*/
template <typename U, typename V>
bool SamePointXY(const U& u, const V& v) {
constexpr double kMathEpsilonSqr = 1e-8 * 1e-8;
return (u.x() - v.x()) * (u.x() - v.x()) < kMathEpsilonSqr &&
(u.y() - v.y()) * (u.y() - v.y()) < kMathEpsilonSqr;
}
PathPoint GetWeightedAverageOfTwoPathPoints(const PathPoint& p1,
const PathPoint& p2,
const double w1, const double w2);
// a wrapper template function for remove_if (notice that remove_if cannot
// change the Container size)
template <class Container, class F>
void erase_where(Container& c, F&& f) { // NOLINT
c.erase(std::remove_if(c.begin(), c.end(), std::forward<F>(f)), c.end());
}
// a wrapper template function for remove_if on associative containers
template <class Container, class F>
void erase_map_where(Container& c, F&& f) { // NOLINT
for (auto it = c.begin(); it != c.end();) {
if (f(*it)) {
it = c.erase(it);
} else {
++it;
}
}
}
template <typename T>
void QuaternionToRotationMatrix(const T* quat, T* R) {
T x2 = quat[0] * quat[0];
T xy = quat[0] * quat[1];
T rx = quat[3] * quat[0];
T y2 = quat[1] * quat[1];
T yz = quat[1] * quat[2];
T ry = quat[3] * quat[1];
T z2 = quat[2] * quat[2];
T zx = quat[2] * quat[0];
T rz = quat[3] * quat[2];
T r2 = quat[3] * quat[3];
R[0] = r2 + x2 - y2 - z2; // fill diagonal terms
R[4] = r2 - x2 + y2 - z2;
R[8] = r2 - x2 - y2 + z2;
R[3] = 2 * (xy + rz); // fill off diagonal terms
R[6] = 2 * (zx - ry);
R[7] = 2 * (yz + rx);
R[1] = 2 * (xy - rz);
R[2] = 2 * (zx + ry);
R[5] = 2 * (yz - rx);
}
// Test whether two float or double numbers are equal.
// ulp: units in the last place.
template <typename T>
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
IsFloatEqual(T x, T y, int ulp = 2) {
// the machine epsilon has to be scaled to the magnitude of the values used
// and multiplied by the desired precision in ULPs (units in the last place)
return std::fabs(x - y) <
std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
// unless the result is subnormal
|| std::fabs(x - y) < std::numeric_limits<T>::min();
}
} // namespace util
} // namespace common
} // namespace apollo
template <typename A, typename B>
std::ostream& operator<<(std::ostream& os, std::pair<A, B>& p) {
return os << "first: " << p.first << ", second: " << p.second;
}