Apollo  6.0
Open source self driving car software
common.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 <algorithm>
20 #include <limits>
21 #include <memory>
22 #include <vector>
23 
24 #include "Eigen/Core"
25 
27 
28 namespace apollo {
29 namespace perception {
30 namespace common {
31 
32 // @brief transform a point
33 // old name: transform_point_cloud
34 template <typename PointT>
35 void TransformPoint(const PointT &point_in, const Eigen::Affine3d &pose,
36  PointT *point_out) {
37  Eigen::Vector3d point3d(point_in.x, point_in.y, point_in.z);
38  point3d = pose * point3d;
39  point_out->x = static_cast<typename PointT::Type>(point3d.x());
40  point_out->y = static_cast<typename PointT::Type>(point3d.y());
41  point_out->z = static_cast<typename PointT::Type>(point3d.z());
42 }
43 
44 // @brief transform a point cloud
45 // old name: transform_point_cloud
46 template <typename PointT>
48  const Eigen::Affine3d &pose,
49  base::PointCloud<PointT> *cloud_out) {
50  if (cloud_out->size() < cloud_in.size()) {
51  cloud_out->resize(cloud_in.size());
52  }
53  for (size_t i = 0; i < cloud_in.size(); ++i) {
54  TransformPoint<PointT>(cloud_in.at(i), pose, &(cloud_out->at(i)));
55  }
56 }
57 
58 // @brief transform a point cloud
59 // old name:transform_point_cloud
60 template <typename PointT>
62  base::PointCloud<PointT> *cloud_in_out) {
63  TransformPointCloud<PointT>(*cloud_in_out, pose, cloud_in_out);
64 }
65 
66 // @brief extract the indexed points from a point cloud
67 // old name: transform_cloud
68 template <typename PointCloudT>
69 void ExtractIndicedCloud(const std::shared_ptr<const PointCloudT> cloud,
70  const std::vector<int> &indices,
71  std::shared_ptr<PointCloudT> trans_cloud) {
72  if (trans_cloud->size() != indices.size()) {
73  trans_cloud->resize(indices.size());
74  }
75  for (size_t i = 0; i < indices.size(); ++i) {
76  const auto &p = cloud->at(indices[i]);
77  auto &tp = trans_cloud->at(i);
78  tp.x = p.x;
79  tp.y = p.y;
80  tp.z = p.z;
81  tp.intensity = p.intensity;
82  }
83 }
84 
85 // @brief get the maximum and minimum in each axis of a point cloud
86 // old name: du_get_min_max_3d
87 template <typename PointT>
89  const size_t range,
90  Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
91  Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
92  using T = typename PointT::Type;
93  (*min_p)[0] = (*min_p)[1] = (*min_p)[2] = std::numeric_limits<T>::max();
94  (*max_p)[0] = (*max_p)[1] = (*max_p)[2] = -std::numeric_limits<T>::max();
95  (*min_p)[3] = 0.0;
96  (*max_p)[3] = 0.0;
97  for (size_t i = 0; i < range; ++i) {
98  const auto &pt = cloud.at(i);
99  if (std::isnan(pt.x) || std::isnan(pt.y) || std::isnan(pt.z)) {
100  continue;
101  }
102  (*min_p)[0] = std::min((*min_p)[0], static_cast<T>(pt.x));
103  (*max_p)[0] = std::max((*max_p)[0], static_cast<T>(pt.x));
104  (*min_p)[1] = std::min((*min_p)[1], static_cast<T>(pt.y));
105  (*max_p)[1] = std::max((*max_p)[1], static_cast<T>(pt.y));
106  (*min_p)[2] = std::min((*min_p)[2], static_cast<T>(pt.z));
107  (*max_p)[2] = std::max((*max_p)[2], static_cast<T>(pt.z));
108  }
109 }
110 
111 // @brief get the maximum and minimum in each axis of an indexed point cloud
112 // old name: du_get_min_max_3d
113 template <typename PointT>
115  const base::PointIndices &indices,
116  Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
117  Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
118  GetMinMaxIn3DWithRange<PointT>(cloud, indices.indices.size(), min_p, max_p);
119 }
120 
121 // @brief get the maximum and minimum in each axis of a point cloud
122 // old name: du_get_min_max_3d
123 template <typename PointT>
125  Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
126  Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
127  GetMinMaxIn3DWithRange<PointT>(cloud, cloud.size(), min_p, max_p);
128 }
129 
130 // @brief calculate the centroid of a point cloud
131 // old name: get_barycenter
132 template <typename T>
133 Eigen::Matrix<T, 3, 1> CalculateCentroid(
135  size_t point_num = cloud.size();
136  Eigen::Matrix<T, 3, 1> centroid(0.0, 0.0, 0.0);
137  for (const auto &pt : cloud.points()) {
138  centroid[0] += pt.x;
139  centroid[1] += pt.y;
140  centroid[2] += pt.z;
141  }
142  if (point_num > 0) {
143  centroid[0] /= static_cast<T>(point_num);
144  centroid[1] /= static_cast<T>(point_num);
145  centroid[2] /= static_cast<T>(point_num);
146  }
147  return centroid;
148 }
149 
150 } // namespace common
151 } // namespace perception
152 } // namespace apollo
void TransformPoint(const PointT &point_in, const Eigen::Affine3d &pose, PointT *point_out)
Definition: common.h:35
std::vector< int > indices
Definition: point.h:75
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: point.h:28
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
size_t size() const
Definition: point_cloud.h:83
void TransformPointCloud(const base::PointCloud< PointT > &cloud_in, const Eigen::Affine3d &pose, base::PointCloud< PointT > *cloud_out)
Definition: common.h:47
Definition: point_cloud.h:37
void GetMinMaxIn3DWithRange(const base::AttributePointCloud< PointT > &cloud, const size_t range, Eigen::Matrix< typename PointT::Type, 4, 1 > *min_p, Eigen::Matrix< typename PointT::Type, 4, 1 > *max_p)
Definition: common.h:88
virtual void resize(size_t size)
Definition: point_cloud.h:89
Eigen::Matrix< T, 3, 1 > CalculateCentroid(const base::AttributePointCloud< base::Point< T >> &cloud)
Definition: common.h:133
void ExtractIndicedCloud(const std::shared_ptr< const PointCloudT > cloud, const std::vector< int > &indices, std::shared_ptr< PointCloudT > trans_cloud)
Definition: common.h:69
void GetMinMaxIn3D(const base::AttributePointCloud< PointT > &cloud, const base::PointIndices &indices, Eigen::Matrix< typename PointT::Type, 4, 1 > *min_p, Eigen::Matrix< typename PointT::Type, 4, 1 > *max_p)
Definition: common.h:114
Eigen::Affine3d Affine3d
Definition: base_map_fwd.h:34
const PointT * at(size_t col, size_t row) const
Definition: point_cloud.h:64