Apollo 11.0
自动驾驶开放平台
common.h
浏览该文件的文档.
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
28
29namespace apollo {
30namespace perception {
31namespace algorithm {
32
33// @brief transform a point
34// old name: transform_point_cloud
35template <typename PointT>
36void TransformPoint(const PointT &point_in, const Eigen::Affine3d &pose,
37 PointT *point_out) {
38 Eigen::Vector3d point3d(point_in.x, point_in.y, point_in.z);
39 point3d = pose * point3d;
40 point_out->x = static_cast<typename PointT::Type>(point3d.x());
41 point_out->y = static_cast<typename PointT::Type>(point3d.y());
42 point_out->z = static_cast<typename PointT::Type>(point3d.z());
43}
44
45// @brief transform a point cloud
46// old name: transform_point_cloud
47template <typename PointT>
49 const Eigen::Affine3d &pose,
50 base::PointCloud<PointT> *cloud_out) {
51 if (cloud_out->size() < cloud_in.size()) {
52 cloud_out->resize(cloud_in.size());
53 }
54 for (size_t i = 0; i < cloud_in.size(); ++i) {
55 TransformPoint<PointT>(cloud_in.at(i), pose, &(cloud_out->at(i)));
56 }
57}
58
59// @brief transform a point cloud
60// old name:transform_point_cloud
61template <typename PointT>
62void TransformPointCloud(const Eigen::Affine3d &pose,
63 base::PointCloud<PointT> *cloud_in_out) {
64 TransformPointCloud<PointT>(*cloud_in_out, pose, cloud_in_out);
65}
66
67// @brief extract the indexed points from a point cloud
68// old name: transform_cloud
69template <typename PointCloudT>
70void ExtractIndicedCloud(const std::shared_ptr<const PointCloudT> cloud,
71 const std::vector<int> &indices,
72 std::shared_ptr<PointCloudT> trans_cloud) {
73 if (trans_cloud->size() != indices.size()) {
74 trans_cloud->resize(indices.size());
75 }
76 for (size_t i = 0; i < indices.size(); ++i) {
77 const auto &p = cloud->at(indices[i]);
78 auto &tp = trans_cloud->at(i);
79 tp.x = p.x;
80 tp.y = p.y;
81 tp.z = p.z;
82 tp.intensity = p.intensity;
83 }
84}
85
86// @brief get the maximum and minimum in each axis of a point cloud
87// old name: du_get_min_max_3d
88template <typename PointT>
90 const size_t range,
91 Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
92 Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
93 using T = typename PointT::Type;
94 (*min_p)[0] = (*min_p)[1] = (*min_p)[2] = std::numeric_limits<T>::max();
95 (*max_p)[0] = (*max_p)[1] = (*max_p)[2] = -std::numeric_limits<T>::max();
96 (*min_p)[3] = 0.0;
97 (*max_p)[3] = 0.0;
98 for (size_t i = 0; i < range; ++i) {
99 const auto &pt = cloud.at(i);
100 if (std::isnan(pt.x) || std::isnan(pt.y) || std::isnan(pt.z)) {
101 continue;
102 }
103 (*min_p)[0] = std::min((*min_p)[0], static_cast<T>(pt.x));
104 (*max_p)[0] = std::max((*max_p)[0], static_cast<T>(pt.x));
105 (*min_p)[1] = std::min((*min_p)[1], static_cast<T>(pt.y));
106 (*max_p)[1] = std::max((*max_p)[1], static_cast<T>(pt.y));
107 (*min_p)[2] = std::min((*min_p)[2], static_cast<T>(pt.z));
108 (*max_p)[2] = std::max((*max_p)[2], static_cast<T>(pt.z));
109 }
110}
111
112// @brief get the maximum and minimum in each axis of an indexed point cloud
113// old name: du_get_min_max_3d
114template <typename PointT>
116 const base::PointIndices &indices,
117 Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
118 Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
119 GetMinMaxIn3DWithRange<PointT>(cloud, indices.indices.size(), min_p, max_p);
120}
121
122// @brief get the maximum and minimum in each axis of a point cloud
123// old name: du_get_min_max_3d
124template <typename PointT>
126 Eigen::Matrix<typename PointT::Type, 4, 1> *min_p,
127 Eigen::Matrix<typename PointT::Type, 4, 1> *max_p) {
128 GetMinMaxIn3DWithRange<PointT>(cloud, cloud.size(), min_p, max_p);
129}
130
131// @brief calculate the centroid of a point cloud
132// old name: get_barycenter
133template <typename T>
134Eigen::Matrix<T, 3, 1> CalculateCentroid(
136 size_t point_num = cloud.size();
137 Eigen::Matrix<T, 3, 1> centroid(0.0, 0.0, 0.0);
138 for (const auto &pt : cloud.points()) {
139 centroid[0] += pt.x;
140 centroid[1] += pt.y;
141 centroid[2] += pt.z;
142 }
143 if (point_num > 0) {
144 centroid[0] /= static_cast<T>(point_num);
145 centroid[1] /= static_cast<T>(point_num);
146 centroid[2] /= static_cast<T>(point_num);
147 }
148 return centroid;
149}
150
151template <typename T>
152Eigen::Matrix<T, 3, 1> CalculateRadarCentroid(
154 size_t point_num = cloud.size();
155 Eigen::Matrix<T, 3, 1> centroid(0.0, 0.0, 0.0);
156 for (const auto &pt : cloud.points()) {
157 centroid[0] += pt.x;
158 centroid[1] += pt.y;
159 centroid[2] += pt.z;
160 }
161 if (point_num > 0) {
162 centroid[0] /= static_cast<T>(point_num);
163 centroid[1] /= static_cast<T>(point_num);
164 centroid[2] /= static_cast<T>(point_num);
165 }
166 return centroid;
167}
168
169} // namespace algorithm
170} // namespace perception
171} // namespace apollo
const PointT * at(size_t col, size_t row) const
Definition point_cloud.h:63
virtual void resize(size_t size)
Definition point_cloud.h:88
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:89
void TransformPoint(const PointT &point_in, const Eigen::Affine3d &pose, PointT *point_out)
Definition common.h:36
void TransformPointCloud(const base::PointCloud< PointT > &cloud_in, const Eigen::Affine3d &pose, base::PointCloud< PointT > *cloud_out)
Definition common.h:48
Eigen::Matrix< T, 3, 1 > CalculateCentroid(const base::AttributePointCloud< base::Point< T > > &cloud)
Definition common.h:134
void ExtractIndicedCloud(const std::shared_ptr< const PointCloudT > cloud, const std::vector< int > &indices, std::shared_ptr< PointCloudT > trans_cloud)
Definition common.h:70
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:115
Eigen::Matrix< T, 3, 1 > CalculateRadarCentroid(const base::AttributeRadarPointCloud< base::RadarPoint< T > > &cloud)
Definition common.h:152
class register implement
Definition arena_queue.h:37