Apollo  v5.5.0
Open source self driving car software
basic.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 
17 #pragma once
18 
19 #include <limits>
20 
21 #include "Eigen/Core"
22 
24 
25 namespace apollo {
26 namespace perception {
27 namespace common {
28 
29 // @brief cross production on two vectors
30 // one is from pt1 to pt2, another is from pt1 to pt3
31 // the type of points could be double or float
32 // old name: cross_prod
33 template <typename Type>
34 inline Type CrossProduct(const Eigen::Matrix<Type, 2, 1> &point1,
35  const Eigen::Matrix<Type, 2, 1> &point2,
36  const Eigen::Matrix<Type, 2, 1> &point3) {
37  return (point2.x() - point1.x()) * (point3.y() - point1.y()) -
38  (point3.x() - point1.x()) * (point2.y() - point1.y());
39 }
40 
41 // @brief cross production on two vectors
42 // one is from pt1 to pt2, another is from pt1 to pt3
43 // the type of points could be double or float
44 // old name: cross_prod
45 template <typename PointT>
46 inline typename PointT::Type CrossProduct(const PointT &point1,
47  const PointT &point2,
48  const PointT &point3) {
49  return (point2.x - point1.x) * (point3.y - point1.y) -
50  (point3.x - point1.x) * (point2.y - point1.y);
51 }
52 
53 // @brief calculate the Eucliden distance between two points
54 // old name: euclidean_dist
55 template <typename PointT>
56 inline typename PointT::Type CalculateEuclidenDist(const PointT &pt1,
57  const PointT &pt2) {
58  typename PointT::Type dist = (pt1.x - pt2.x) * (pt1.x - pt2.x);
59  dist += (pt1.y - pt2.y) * (pt1.y - pt2.y);
60  dist += (pt1.z - pt2.z) * (pt1.z - pt2.z);
61  return static_cast<typename PointT::Type>(sqrt(dist));
62 }
63 
64 // @brief calculate the Euclidean distance between two points in X-Y plane
65 // old name: euclidean_dist_2d_xy
66 template <typename PointT>
67 inline typename PointT::Type CalculateEuclidenDist2DXY(const PointT &pt1,
68  const PointT &pt2) {
69  typename PointT::Type dist = (pt1.x - pt2.x) * (pt1.x - pt2.x);
70  dist += (pt1.y - pt2.y) * (pt1.y - pt2.y);
71  return static_cast<typename PointT::Type>(sqrt(dist));
72 }
73 
74 // @brief calculate cos value of the rotation angle
75 // between two vectors in X-Y plane
76 // old name: vector_cos_theta_2d_xy
77 template <typename T>
78 T CalculateCosTheta2DXY(const Eigen::Matrix<T, 3, 1> &v1,
79  const Eigen::Matrix<T, 3, 1> &v2) {
80  T v1_len = static_cast<T>(sqrt((v1.head(2).cwiseProduct(v1.head(2))).sum()));
81  T v2_len = static_cast<T>(sqrt((v2.head(2).cwiseProduct(v2.head(2))).sum()));
82  if (v1_len < std::numeric_limits<T>::epsilon() ||
83  v2_len < std::numeric_limits<T>::epsilon()) {
84  return 0.0;
85  }
86  T cos_theta = (v1.head(2).cwiseProduct(v2.head(2))).sum() / (v1_len * v2_len);
87  return cos_theta;
88 }
89 
90 // @brief calculate the rotation angle between two vectors in X-Y plane
91 // old name: vector_theta_2d_xy
92 template <typename T>
93 T CalculateTheta2DXY(const Eigen::Matrix<T, 3, 1> &v1,
94  const Eigen::Matrix<T, 3, 1> &v2) {
95  T v1_len = static_cast<T>(sqrt((v1.head(2).cwiseProduct(v1.head(2))).sum()));
96  T v2_len = static_cast<T>(sqrt((v2.head(2).cwiseProduct(v2.head(2))).sum()));
97  if (v1_len < std::numeric_limits<T>::epsilon() ||
98  v2_len < std::numeric_limits<T>::epsilon()) {
99  return 0.0;
100  }
101  const T cos_theta =
102  (v1.head(2).cwiseProduct(v2.head(2))).sum() / (v1_len * v2_len);
103  const T sin_theta = (v1(0) * v2(1) - v1(1) * v2(0)) / (v1_len * v2_len);
104  T theta = std::acos(cos_theta);
105  if (sin_theta < 0.0) {
106  theta = -theta;
107  }
108  return theta;
109 }
110 
111 // @brief calculate the rotation matrix
112 // transform from v1 axis coordinate to v2 axis coordinate
113 // old name: vector_rot_mat_2d_xy
114 template <typename T>
115 Eigen::Matrix<T, 3, 3> CalculateRotationMat2DXY(
116  const Eigen::Matrix<T, 3, 1> &v1, const Eigen::Matrix<T, 3, 1> &v2) {
117  T v1_len = static_cast<T>(sqrt((v1.head(2).cwiseProduct(v1.head(2))).sum()));
118  T v2_len = static_cast<T>(sqrt((v2.head(2).cwiseProduct(v2.head(2))).sum()));
119  if (v1_len < std::numeric_limits<T>::epsilon() ||
120  v2_len < std::numeric_limits<T>::epsilon()) {
121  return Eigen::Matrix<T, 3, 3>::Zero(3, 3);
122  }
123 
124  const T cos_theta =
125  (v1.head(2).cwiseProduct(v2.head(2))).sum() / (v1_len * v2_len);
126  const T sin_theta = (v1(0) * v2(1) - v1(1) * v2(0)) / (v1_len * v2_len);
127 
128  Eigen::Matrix<T, 3, 3> rot_mat;
129  rot_mat << cos_theta, sin_theta, 0, -sin_theta, cos_theta, 0, 0, 0, 1;
130  return rot_mat;
131 }
132 
133 // @brief calculate the project vector from one vector to another
134 // old name: compute_2d_xy_project_vector
135 template <typename T>
136 Eigen::Matrix<T, 3, 1> Calculate2DXYProjectVector(
137  const Eigen::Matrix<T, 3, 1> &projected_vector,
138  const Eigen::Matrix<T, 3, 1> &project_vector) {
139  if (projected_vector.head(2).norm() < std::numeric_limits<T>::epsilon() ||
140  project_vector.head(2).norm() < std::numeric_limits<T>::epsilon()) {
141  return Eigen::Matrix<T, 3, 1>::Zero(3, 1);
142  }
143  Eigen::Matrix<T, 3, 1> project_dir = project_vector;
144  project_dir(2) = 0.0;
145  project_dir.normalize();
146 
147  const T projected_vector_project_dir_inner_product =
148  projected_vector(0) * project_dir(0) +
149  projected_vector(1) * project_dir(1);
150  const T projected_vector_project_dir_angle_cos =
151  projected_vector_project_dir_inner_product /
152  (projected_vector.head(2).norm() * project_dir.head(2).norm());
153  const T projected_vector_norm_on_project_dir =
154  projected_vector.head(2).norm() * projected_vector_project_dir_angle_cos;
155  return project_dir * projected_vector_norm_on_project_dir;
156 }
157 
158 // @brief convert point xyz in Cartesian coordinate to polar coordinate
159 // old name: xyz_to_polar_coordinate
160 template <typename PointT>
161 void ConvertCartesiantoPolarCoordinate(const PointT &xyz,
162  typename PointT::Type *h_angle_in_degree,
163  typename PointT::Type *v_angle_in_degree,
164  typename PointT::Type *dist) {
165  using T = typename PointT::Type;
166  const T radian_to_degree = 180.0 / M_PI;
167  const T x = xyz.x;
168  const T y = xyz.y;
169  const T z = xyz.z;
170 
171  (*dist) = static_cast<T>(sqrt(x * x + y * y + z * z));
172  T dist_xy = static_cast<T>(sqrt(x * x + y * y));
173 
174  (*h_angle_in_degree) = std::acos(x / dist_xy) * radian_to_degree;
175  if (y < 0.0) {
176  (*h_angle_in_degree) = static_cast<T>(360.0) - (*h_angle_in_degree);
177  }
178 
179  (*v_angle_in_degree) = std::acos(dist_xy / (*dist)) * radian_to_degree;
180  if (z < 0.0) {
181  (*v_angle_in_degree) = -(*v_angle_in_degree);
182  }
183 }
184 
185 } // namespace common
186 } // namespace perception
187 } // namespace apollo
Eigen::Matrix< T, 3, 3 > CalculateRotationMat2DXY(const Eigen::Matrix< T, 3, 1 > &v1, const Eigen::Matrix< T, 3, 1 > &v2)
Definition: basic.h:115
T CalculateTheta2DXY(const Eigen::Matrix< T, 3, 1 > &v1, const Eigen::Matrix< T, 3, 1 > &v2)
Definition: basic.h:93
Eigen::Matrix< T, 3, 1 > Calculate2DXYProjectVector(const Eigen::Matrix< T, 3, 1 > &projected_vector, const Eigen::Matrix< T, 3, 1 > &project_vector)
Definition: basic.h:136
Definition: blob.h:72
Type CrossProduct(const Eigen::Matrix< Type, 2, 1 > &point1, const Eigen::Matrix< Type, 2, 1 > &point2, const Eigen::Matrix< Type, 2, 1 > &point3)
Definition: basic.h:34
void ConvertCartesiantoPolarCoordinate(const PointT &xyz, typename PointT::Type *h_angle_in_degree, typename PointT::Type *v_angle_in_degree, typename PointT::Type *dist)
Definition: basic.h:161
T CalculateCosTheta2DXY(const Eigen::Matrix< T, 3, 1 > &v1, const Eigen::Matrix< T, 3, 1 > &v2)
Definition: basic.h:78
PointT::Type CalculateEuclidenDist2DXY(const PointT &pt1, const PointT &pt2)
Definition: basic.h:67
PointT::Type CalculateEuclidenDist(const PointT &pt1, const PointT &pt2)
Definition: basic.h:56