Apollo 11.0
自动驾驶开放平台
pointcloud_preprocessor.cc
浏览该文件的文档.
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
18
19#include <limits>
20
21#include "modules/perception/pointcloud_preprocess/preprocessor/proto/pointcloud_preprocessor_config.pb.h"
22
23#include "cyber/common/file.h"
27
28namespace apollo {
29namespace perception {
30namespace lidar {
31
32const float PointCloudPreprocessor::kPointInfThreshold = 1e3;
33
35 const PointCloudPreprocessorInitOptions& options) {
36 std::string config_file =
37 GetConfigFile(options.config_path, options.config_file);
40 filter_naninf_points_ = config.filter_naninf_points();
41 filter_nearby_box_points_ = config.filter_nearby_box_points();
42 box_forward_x_ = config.box_forward_x();
43 box_backward_x_ = config.box_backward_x();
44 box_forward_y_ = config.box_forward_y();
45 box_backward_y_ = config.box_backward_y();
46 filter_high_z_points_ = config.filter_high_z_points();
47 z_threshold_ = config.z_threshold();
48 return true;
49}
50
52 const PointCloudPreprocessorOptions& options,
53 const std::shared_ptr<apollo::drivers::PointCloud const>& message,
54 LidarFrame* frame) const {
55 if (frame == nullptr) {
56 return false;
57 }
58 if (frame->cloud == nullptr) {
59 frame->cloud = base::PointFCloudPool::Instance().Get();
60 }
61 if (frame->world_cloud == nullptr) {
62 frame->world_cloud = base::PointDCloudPool::Instance().Get();
63 }
64
65 frame->cloud->set_timestamp(message->measurement_time());
66 if (message->point_size() > 0) {
67 frame->cloud->reserve(message->point_size());
68 base::PointF point;
69 for (int i = 0; i < message->point_size(); ++i) {
70 const apollo::drivers::PointXYZIT& pt = message->point(i);
71 if (filter_naninf_points_) {
72 if (std::isnan(pt.x()) || std::isnan(pt.y()) || std::isnan(pt.z())) {
73 continue;
74 }
75 if (fabs(pt.x()) > kPointInfThreshold ||
76 fabs(pt.y()) > kPointInfThreshold ||
77 fabs(pt.z()) > kPointInfThreshold) {
78 continue;
79 }
80 }
81 Eigen::Vector3d vec3d_lidar(pt.x(), pt.y(), pt.z());
82 // Eigen::Vector3d vec3d_novatel =
83 // options.sensor2novatel_extrinsics * vec3d_lidar;
84 Eigen::Vector3d vec3d_novatel = vec3d_lidar;
85 if (filter_nearby_box_points_ && vec3d_novatel[0] < box_forward_x_ &&
86 vec3d_novatel[0] > box_backward_x_ &&
87 vec3d_novatel[1] < box_forward_y_ &&
88 vec3d_novatel[1] > box_backward_y_) {
89 continue;
90 }
91 if (filter_high_z_points_ && pt.z() > z_threshold_) {
92 continue;
93 }
94 point.x = pt.x();
95 point.y = pt.y();
96 point.z = pt.z();
97 point.intensity = static_cast<float>(pt.intensity());
98 frame->cloud->push_back(point, static_cast<double>(pt.timestamp()) * 1e-9,
99 std::numeric_limits<float>::max(), i, 0);
100 }
101 TransformCloud(frame->cloud, frame->lidar2world_pose, frame->world_cloud);
102 }
103
104 return true;
105}
106
108 const PointCloudPreprocessorOptions& options, LidarFrame* frame) const {
109 if (frame == nullptr || frame->cloud == nullptr) {
110 return false;
111 }
112 if (frame->world_cloud == nullptr) {
113 frame->world_cloud = base::PointDCloudPool::Instance().Get();
114 }
115 if (frame->cloud->size() > 0) {
116 size_t size = frame->cloud->size();
117 size_t i = 0;
118 while (i < size) {
119 auto& pt = frame->cloud->at(i);
120 if (filter_naninf_points_) {
121 if (std::isnan(pt.x) || std::isnan(pt.y) || std::isnan(pt.z)) {
122 frame->cloud->SwapPoint(i, size--);
123 continue;
124 }
125 if (fabs(pt.x) > kPointInfThreshold ||
126 fabs(pt.y) > kPointInfThreshold ||
127 fabs(pt.z) > kPointInfThreshold) {
128 frame->cloud->SwapPoint(i, size--);
129 continue;
130 }
131 }
132 Eigen::Vector3d vec3d_lidar(pt.x, pt.y, pt.z);
133 // Eigen::Vector3d vec3d_novatel =
134 // options.sensor2novatel_extrinsics * vec3d_lidar;
135 Eigen::Vector3d vec3d_novatel = vec3d_lidar;
136 if (filter_nearby_box_points_ && vec3d_novatel[0] < box_forward_x_ &&
137 vec3d_novatel[0] > box_backward_x_ &&
138 vec3d_novatel[1] < box_forward_y_ &&
139 vec3d_novatel[1] > box_backward_y_) {
140 frame->cloud->SwapPoint(i, size--);
141 continue;
142 }
143 if (filter_high_z_points_ && pt.z > z_threshold_) {
144 frame->cloud->SwapPoint(i, size--);
145 continue;
146 }
147 ++i;
148 }
149 frame->cloud->resize(i);
150 TransformCloud(frame->cloud, frame->lidar2world_pose, frame->world_cloud);
151 AINFO << "Preprocessor filter points: " << size << " to " << i;
152 }
153 return true;
154}
155
156bool PointCloudPreprocessor::TransformCloud(
157 const base::PointFCloudPtr& local_cloud, const Eigen::Affine3d& pose,
158 base::PointDCloudPtr world_cloud) const {
159 if (local_cloud == nullptr) {
160 return false;
161 }
162 world_cloud->clear();
163 world_cloud->reserve(local_cloud->size());
164 for (size_t i = 0; i < local_cloud->size(); ++i) {
165 auto& pt = local_cloud->at(i);
166 Eigen::Vector3d trans_point(pt.x, pt.y, pt.z);
167 trans_point = pose * trans_point;
168 base::PointD world_point;
169 world_point.x = trans_point(0);
170 world_point.y = trans_point(1);
171 world_point.z = trans_point(2);
172 world_point.intensity = pt.intensity;
173 world_cloud->push_back(world_point, local_cloud->points_timestamp(i),
174 std::numeric_limits<float>::max(),
175 local_cloud->points_beam_id()[i], 0);
176 }
177 return true;
178}
179
181
182} // namespace lidar
183} // namespace perception
184} // namespace apollo
#define PERCEPTION_REGISTER_POINTCLOUDPREPROCESSOR(name)
bool Preprocess(const PointCloudPreprocessorOptions &options, const std::shared_ptr< apollo::drivers::PointCloud const > &message, LidarFrame *frame) const
Ireprocess point cloud
bool Init(const PointCloudPreprocessorInitOptions &options=PointCloudPreprocessorInitOptions())
Init pointcloud preprocessor
#define ACHECK(cond)
Definition log.h:80
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::shared_ptr< PointDCloud > PointDCloudPtr
std::shared_ptr< PointFCloud > PointFCloudPtr
std::string GetConfigFile(const std::string &config_path, const std::string &config_file)
Definition util.cc:80
class register implement
Definition arena_queue.h:37
std::shared_ptr< base::AttributePointCloud< base::PointD > > world_cloud
Definition lidar_frame.h:37
std::shared_ptr< base::AttributePointCloud< base::PointF > > cloud
Definition lidar_frame.h:35