Apollo  6.0
Open source self driving car software
geo_util.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "Eigen/Core"
22 
24 
25 namespace apollo {
26 namespace perception {
27 namespace benchmark {
28 
29 template <typename T>
30 void quaternion_to_rotation_matrix(const T* quat, T* R) {
31  T x2 = quat[0] * quat[0];
32  T xy = quat[0] * quat[1];
33  T rx = quat[3] * quat[0];
34  T y2 = quat[1] * quat[1];
35  T yz = quat[1] * quat[2];
36  T ry = quat[3] * quat[1];
37  T z2 = quat[2] * quat[2];
38  T zx = quat[2] * quat[0];
39  T rz = quat[3] * quat[2];
40  T r2 = quat[3] * quat[3];
41  R[0] = r2 + x2 - y2 - z2; // fill diagonal terms
42  R[4] = r2 - x2 + y2 - z2;
43  R[8] = r2 - x2 - y2 + z2;
44  R[3] = 2 * (xy + rz); // fill off diagonal terms
45  R[6] = 2 * (zx - ry);
46  R[7] = 2 * (yz + rx);
47  R[1] = 2 * (xy - rz);
48  R[2] = 2 * (zx + ry);
49  R[5] = 2 * (yz - rx);
50 }
51 
52 bool is_point_xy_in_polygon2d_xy(const Point& point, const PointCloud& polygon,
53  float distance_to_boundary);
54 
55 inline bool approx_equal(float a, float b) {
56  return std::abs(a - b) <= std::max(std::abs(a), std::abs(b)) *
57  std::numeric_limits<float>::epsilon();
58 }
59 
60 inline bool strictly_less(float a, float b) {
61  return (b - a) > std::max(std::abs(a), std::abs(b)) *
62  std::numeric_limits<float>::epsilon();
63 }
64 
65 enum class Orientation {
66  left = 1,
67  right = -1,
68  collinear = 0,
69 };
70 
71 struct VisPoint {
72  VisPoint() = default;
73 
74  VisPoint(float x, float y) : point(Eigen::Vector2f(x, y)) {}
75 
76  explicit VisPoint(const Eigen::Vector2f& point_) : point(point_) {}
77 
78  bool operator<(const VisPoint& other) const;
79 
80  bool operator==(const VisPoint& other) const;
81 
82  bool operator!=(const VisPoint& other) const;
83 
84  VisPoint operator+(const VisPoint& other) const {
85  return VisPoint(x() + other.x(), y() + other.y());
86  }
87 
88  VisPoint operator-(const VisPoint& other) const {
89  return VisPoint(x() - other.x(), y() - other.y());
90  }
91 
92  VisPoint operator-() const { return VisPoint(-x(), -y()); }
93 
94  VisPoint operator*(float scale) const {
95  return VisPoint(x() * scale, y() * scale);
96  }
97 
98  VisPoint operator/(float scale) const {
99  return VisPoint(x() / scale, y() / scale);
100  }
101 
102  friend std::ostream& operator<<(std::ostream& output, const VisPoint& p) {
103  output << "(" << p.x() << ", " << p.y() << ")";
104  return output;
105  }
106 
107  inline const float& x() const { return point.x(); }
108 
109  inline const float& y() const { return point.y(); }
110 
111  inline float& x() { return point.x(); }
112 
113  inline float& y() { return point.y(); }
114 
115  inline float dot(const VisPoint& other) const {
116  return x() * other.x() + y() * other.y();
117  }
118 
119  inline float cross(const VisPoint& other) const {
120  return x() * other.y() - y() * other.x();
121  }
122 
123  inline float length_squared() const { return x() * x() + y() * y(); }
124 
126 };
127 
128 struct Segment {
129  Segment() = default;
130 
131  Segment(const VisPoint& start, const VisPoint& end, int idx = -2)
132  : start(start), end(end), idx(idx) {}
133 
134  bool operator<(const Segment& other) const;
135 
136  bool operator==(const Segment& other) const;
137 
138  bool operator!=(const Segment& other) const;
139 
140  friend std::ostream& operator<<(std::ostream& output, const Segment& s) {
141  output << "Segment: start: " << s.start << ", end: " << s.end
142  << ", idx: " << s.idx;
143  return output;
144  }
145 
147  int idx = -2;
148 };
149 
150 // position relation between Point B and line OA
152  const VisPoint& b);
153 
154 // calculate intersection point between ray and segment
155 bool intersects(const VisPoint& ray, const Segment& segment,
156  VisPoint* intersection);
157 
158 } // namespace benchmark
159 } // namespace perception
160 } // namespace apollo
VisPoint operator-(const VisPoint &other) const
Definition: geo_util.h:88
friend std::ostream & operator<<(std::ostream &output, const Segment &s)
Definition: geo_util.h:140
float cross(const VisPoint &other) const
Definition: geo_util.h:119
VisPoint operator/(float scale) const
Definition: geo_util.h:98
Segment(const VisPoint &start, const VisPoint &end, int idx=-2)
Definition: geo_util.h:131
const float & y() const
Definition: geo_util.h:109
Orientation compute_orientation(const VisPoint &o, const VisPoint &a, const VisPoint &b)
float dot(const VisPoint &other) const
Definition: geo_util.h:115
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
VisPoint(float x, float y)
Definition: geo_util.h:74
Definition: pointcloud.h:26
bool operator<(const edge &a, const edge &b)
Definition: segment_graph.h:53
void quaternion_to_rotation_matrix(const T *quat, T *R)
Definition: geo_util.h:30
T abs(const T &x)
Definition: misc.h:48
const float & x() const
Definition: geo_util.h:107
float length_squared() const
Definition: geo_util.h:123
friend std::ostream & operator<<(std::ostream &output, const VisPoint &p)
Definition: geo_util.h:102
int idx
Definition: geo_util.h:147
bool operator!=(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for inequality.
Definition: angle.h:266
VisPoint start
Definition: geo_util.h:146
bool approx_equal(float a, float b)
Definition: geo_util.h:55
VisPoint operator+(const VisPoint &other) const
Definition: geo_util.h:84
float & x()
Definition: geo_util.h:111
bool strictly_less(float a, float b)
Definition: geo_util.h:60
bool intersects(const VisPoint &ray, const Segment &segment, VisPoint *intersection)
Orientation
Definition: geo_util.h:65
bool is_point_xy_in_polygon2d_xy(const Point &point, const PointCloud &polygon, float distance_to_boundary)
bool operator==(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for equality.
Definition: angle.h:255
Eigen::Vector2f Vector2f
Definition: base_map_fwd.h:30
Eigen::Vector2f point
Definition: geo_util.h:125
float & y()
Definition: geo_util.h:113
VisPoint operator*(float scale) const
Definition: geo_util.h:94
Definition: geo_util.h:128
VisPoint operator-() const
Definition: geo_util.h:92
VisPoint(const Eigen::Vector2f &point_)
Definition: geo_util.h:76
VisPoint end
Definition: geo_util.h:146