Apollo 10.0
自动驾驶开放平台
apollo::common::util 命名空间参考

apollo::common::util 更多...

命名空间

namespace  test
 

struct  DebugStringFormatter
 
class  Factory
 Implements a Factory design pattern with Register and Create methods 更多...
 
class  JsonUtil
 
class  LRUCache
 
struct  Node
 
struct  PairHash
 
class  PointFactory
 
class  Timer
 
class  TimerWrapper
 
class  TimeUtil
 

函数

template<typename T , typename std::enable_if< std::is_base_of< google::protobuf::Message, T >::value, int >::type = 0>
bool DumpMessage (const std::shared_ptr< T > &msg, const std::string &dump_dir="/tmp")
 
size_t MessageFingerprint (const google::protobuf::Message &message)
 
std::string function_signature (const std::string &func_name, const std::string &indicator)
 
template<typename Points >
double GetPathAngle (const Points &points, const size_t start, const size_t end)
 Calculate the angle between the directions of two points on the path.
 
template<typename Points >
std::vector< size_t > DownsampleByAngle (const Points &points, const double angle_threshold)
 Downsample the points on the path according to the angle.
 
template<typename Points >
std::vector< size_t > DownsampleByDistance (const Points &points, int downsampleDistance, int steepTurnDownsampleDistance)
 Downsample the points on the path based on distance.
 
std::string EncodeBase64 (std::string_view in)
 
PointENU operator+ (const PointENU enu, const math::Vec2d &xy)
 
PathPoint GetWeightedAverageOfTwoPathPoints (const PathPoint &p1, const PathPoint &p2, const double w1, const double w2)
 
template<typename ProtoA , typename ProtoB >
bool IsProtoEqual (const ProtoA &a, const ProtoB &b)
 
template<typename T >
bool WithinBound (T start, T end, T value)
 
template<typename T >
void uniform_slice (const T start, const T end, uint32_t num, std::vector< T > *sliced)
 uniformly slice a segment [start, end] to num + 1 pieces the result sliced will contain the n + 1 points that slices the provided segment.
 
template<typename U , typename V >
double DistanceXY (const U &u, const V &v)
 calculate the distance beteween Point u and Point v, which are all have member function x() and y() in XY dimension.
 
template<typename U , typename V >
bool SamePointXY (const U &u, const V &v)
 Check if two points u and v are the same point on XY dimension.
 
template<typename T >
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type IsFloatEqual (T x, T y, int ulp=2)
 

详细描述

函数说明

◆ DistanceXY()

template<typename U , typename V >
double apollo::common::util::DistanceXY ( const U &  u,
const V &  v 
)

calculate the distance beteween Point u and Point v, which are all have member function x() and y() in XY dimension.

参数
uone point that has member function x() and y().
bone point that has member function x() and y().
返回
sqrt((u.x-v.x)^2 + (u.y-v.y)^2), i.e., the Euclid distance on XY dimension.

在文件 util.h97 行定义.

97 {
98 return std::hypot(u.x() - v.x(), u.y() - v.y());
99}

◆ DownsampleByAngle()

template<typename Points >
std::vector< size_t > apollo::common::util::DownsampleByAngle ( const Points &  points,
const double  angle_threshold 
)

Downsample the points on the path according to the angle.

参数
pointsPoints on the path.
angle_thresholdPoints are sampled when the accumulated direction change exceeds the threshold.
返回
sampled_indices Indices of all sampled points, or empty when fail.

在文件 points_downsampler.h80 行定义.

81 {
82 std::vector<size_t> sampled_indices;
83 if (points.empty()) {
84 return sampled_indices;
85 }
86
87 if (angle_threshold < 0.0) {
88 AERROR << "Input angle threshold is negative.";
89 return sampled_indices;
90 }
91 sampled_indices.push_back(0);
92 if (points.size() > 1) {
93 size_t start = 0;
94 size_t end = 1;
95 double accum_degree = 0.0;
96 while (end + 1 < static_cast<size_t>(points.size())) {
97 const double angle = GetPathAngle(points, start, end);
98 accum_degree += std::fabs(angle);
99
100 if (accum_degree > angle_threshold) {
101 sampled_indices.push_back(end);
102 start = end;
103 accum_degree = 0.0;
104 }
105 ++end;
106 }
107 sampled_indices.push_back(end);
108 }
109
110 ADEBUG << "Point Vector is downsampled from " << points.size() << " to "
111 << sampled_indices.size();
112
113 return sampled_indices;
114}
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44

◆ DownsampleByDistance()

template<typename Points >
std::vector< size_t > apollo::common::util::DownsampleByDistance ( const Points &  points,
int  downsampleDistance,
int  steepTurnDownsampleDistance 
)

Downsample the points on the path based on distance.

参数
pointsPoints on the path.
downsampleDistancedownsample rate for a normal path
steepTurnDownsampleDistancedownsample rate for a steep turn path
返回
sampled_indices Indices of all sampled points, or empty when fail.

在文件 points_downsampler.h124 行定义.

126 {
127 std::vector<size_t> sampled_indices;
128 if (points.size() <= 4) {
129 // No need to downsample if there are not too many points.
130 for (size_t i = 0; i < points.size(); ++i) {
131 sampled_indices.push_back(i);
132 }
133 return sampled_indices;
134 }
135
137 Vec2d v_start =
138 Vec2d(points[1].x() - points[0].x(), points[1].y() - points[0].y());
139 Vec2d v_end =
140 Vec2d(points[points.size() - 1].x() - points[points.size() - 2].x(),
141 points[points.size() - 1].y() - points[points.size() - 2].y());
142 v_start.Normalize();
143 v_end.Normalize();
144 // If the angle exceeds 80 degree, it's a steep turn
145 bool is_steep_turn = v_start.InnerProd(v_end) <= cos(80.0 * M_PI / 180.0);
146 int downsampleRate =
147 is_steep_turn ? steepTurnDownsampleDistance : downsampleDistance;
148
149 // Make sure the first point is included
150 sampled_indices.push_back(0);
151
152 double accum_distance = 0.0;
153 for (size_t pos = 1; pos < points.size() - 1; ++pos) {
154 Vec2d point_start = Vec2d(points[pos - 1].x(), points[pos - 1].y());
155 Vec2d point_end = Vec2d(points[pos].x(), points[pos].y());
156 accum_distance += point_start.DistanceTo(point_end);
157
158 if (accum_distance > downsampleRate) {
159 sampled_indices.push_back(pos);
160 accum_distance = 0.0;
161 }
162 }
163
164 // Make sure the last point is included
165 sampled_indices.push_back(points.size() - 1);
166 return sampled_indices;
167}
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42

◆ DumpMessage()

template<typename T , typename std::enable_if< std::is_base_of< google::protobuf::Message, T >::value, int >::type = 0>
bool apollo::common::util::DumpMessage ( const std::shared_ptr< T > &  msg,
const std::string &  dump_dir = "/tmp" 
)

在文件 message_util.h57 行定义.

58 {
59 if (!msg) {
60 AWARN << "Message to be dumped is nullptr!";
61 }
62
63 auto type_name = T::descriptor()->full_name();
64 std::string dump_path = dump_dir + "/" + type_name;
65 if (!cyber::common::DirectoryExists(dump_path)) {
66 if (!cyber::common::EnsureDirectory(dump_path)) {
67 AERROR << "Cannot enable dumping for '" << type_name
68 << "' because the path " << dump_path
69 << " cannot be created or is not a directory.";
70 return false;
71 }
72 }
73
74 auto sequence_num = msg->header().sequence_num();
75 return cyber::common::SetProtoToASCIIFile(
76 *msg, absl::StrCat(dump_path, "/", sequence_num, ".pb.txt"));
77}
#define AWARN
Definition log.h:43

◆ EncodeBase64()

std::string apollo::common::util::EncodeBase64 ( std::string_view  in)

在文件 string_util.cc48 行定义.

48 {
49 std::string out;
50 if (in.empty()) {
51 return out;
52 }
53
54 const size_t in_size = in.length();
55 out.reserve(((in_size - 1) / 3 + 1) * 4);
56 for (size_t i = 0; i + 2 < in_size; i += 3) {
57 absl::StrAppend(&out, Base64Piece(in[i], in[i + 1], in[i + 2]));
58 }
59 if (in_size % 3 == 1) {
60 absl::StrAppend(&out, Base64Piece(in[in_size - 1], 0, 0));
61 }
62 if (in_size % 3 == 2) {
63 absl::StrAppend(&out, Base64Piece(in[in_size - 2], in[in_size - 1], 0));
64 }
65 return out;
66}

◆ function_signature()

std::string apollo::common::util::function_signature ( const std::string &  func_name,
const std::string &  indicator 
)

在文件 perf_util.cc44 行定义.

45 {
46 auto simplified_name = func_name_simplified(func_name);
47 if (indicator.empty()) {
48 return simplified_name;
49 }
50 return absl::StrCat(indicator, "_", simplified_name);
51}

◆ GetPathAngle()

template<typename Points >
double apollo::common::util::GetPathAngle ( const Points &  points,
const size_t  start,
const size_t  end 
)

Calculate the angle between the directions of two points on the path.

参数
pointsPoints on the path.
startThe index of the first point on the path.
endThe index of the second point on the path.
返回
The angle between the directions of the start point and the end point.

在文件 points_downsampler.h46 行定义.

47 {
48 if (start >= static_cast<size_t>(points.size() - 1) ||
49 end >= static_cast<size_t>(points.size() - 1)) {
50 AERROR << "Input indices are out of the range of the points vector: "
51 << "should be less than vector size - 1.";
52 return 0.0;
53 }
54 if (start >= end) {
55 AERROR << "Second index must be greater than the first index.";
56 return 0.0;
57 }
58 double vec_start_x = points[start + 1].x() - points[start].x();
59 double vec_start_y = points[start + 1].y() - points[start].y();
60 double vec_start_norm = std::hypot(vec_start_x, vec_start_y);
61
62 double vec_end_x = points[end + 1].x() - points[end].x();
63 double vec_end_y = points[end + 1].y() - points[end].y();
64 double vec_end_norm = std::hypot(vec_end_x, vec_end_y);
65
66 double dot_product = vec_start_x * vec_end_x + vec_start_y * vec_end_y;
67 double angle = std::acos(dot_product / (vec_start_norm * vec_end_norm));
68
69 return std::isnan(angle) ? 0.0 : angle;
70}

◆ GetWeightedAverageOfTwoPathPoints()

PathPoint apollo::common::util::GetWeightedAverageOfTwoPathPoints ( const PathPoint p1,
const PathPoint p2,
const double  w1,
const double  w2 
)

在文件 util.cc34 行定义.

36 {
37 PathPoint p;
38 p.set_x(p1.x() * w1 + p2.x() * w2);
39 p.set_y(p1.y() * w1 + p2.y() * w2);
40 p.set_z(p1.z() * w1 + p2.z() * w2);
41 p.set_theta(p1.theta() * w1 + p2.theta() * w2);
42 p.set_kappa(p1.kappa() * w1 + p2.kappa() * w2);
43 p.set_dkappa(p1.dkappa() * w1 + p2.dkappa() * w2);
44 p.set_ddkappa(p1.ddkappa() * w1 + p2.ddkappa() * w2);
45 p.set_s(p1.s() * w1 + p2.s() * w2);
46 return p;
47}

◆ IsFloatEqual()

template<typename T >
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type apollo::common::util::IsFloatEqual ( x,
y,
int  ulp = 2 
)

在文件 util.h123 行定义.

123 {
124 // the machine epsilon has to be scaled to the magnitude of the values used
125 // and multiplied by the desired precision in ULPs (units in the last place)
126 return std::fabs(x - y) <
127 std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
128 // unless the result is subnormal
129 || std::fabs(x - y) < std::numeric_limits<T>::min();
130}

◆ IsProtoEqual()

template<typename ProtoA , typename ProtoB >
bool apollo::common::util::IsProtoEqual ( const ProtoA &  a,
const ProtoB &  b 
)

在文件 util.h47 行定义.

47 {
48 return a.GetTypeName() == b.GetTypeName() &&
49 a.SerializeAsString() == b.SerializeAsString();
50 // Test shows that the above method is 5 times faster than the
51 // API: google::protobuf::util::MessageDifferencer::Equals(a, b);
52}

◆ MessageFingerprint()

size_t apollo::common::util::MessageFingerprint ( const google::protobuf::Message &  message)
inline

在文件 message_util.h79 行定义.

79 {
80 static std::hash<std::string> hash_fn;
81 std::string proto_bytes;
82 message.SerializeToString(&proto_bytes);
83 return hash_fn(proto_bytes);
84}

◆ operator+()

PointENU apollo::common::util::operator+ ( const PointENU  enu,
const math::Vec2d xy 
)

在文件 util.cc26 行定义.

26 {
27 PointENU point;
28 point.set_x(enu.x() + xy.x());
29 point.set_y(enu.y() + xy.y());
30 point.set_z(enu.z());
31 return point;
32}
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54

◆ SamePointXY()

template<typename U , typename V >
bool apollo::common::util::SamePointXY ( const U &  u,
const V &  v 
)

Check if two points u and v are the same point on XY dimension.

参数
uone point that has member function x() and y().
vone point that has member function x() and y().
返回
sqrt((u.x-v.x)^2 + (u.y-v.y)^2) < epsilon, i.e., the Euclid distance on XY dimension.

在文件 util.h109 行定义.

109 {
110 static constexpr double kMathEpsilonSqr = 1e-8 * 1e-8;
111 return (u.x() - v.x()) * (u.x() - v.x()) < kMathEpsilonSqr &&
112 (u.y() - v.y()) * (u.y() - v.y()) < kMathEpsilonSqr;
113}

◆ uniform_slice()

template<typename T >
void apollo::common::util::uniform_slice ( const T  start,
const T  end,
uint32_t  num,
std::vector< T > *  sliced 
)

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.

在文件 util.h74 行定义.

75 {
76 if (!sliced || num == 0) {
77 return;
78 }
79 const T delta = (end - start) / num;
80 sliced->resize(num + 1);
81 T s = start;
82 for (uint32_t i = 0; i < num; ++i, s += delta) {
83 sliced->at(i) = s;
84 }
85 sliced->at(num) = end;
86}

◆ WithinBound()

template<typename T >
bool apollo::common::util::WithinBound ( start,
end,
value 
)

在文件 util.h62 行定义.

62 {
63 return value >= start && value <= end;
64}