Apollo 11.0
自动驾驶开放平台
pcl_util.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#pragma once
17
18#include <string>
19
20#include "pcl/filters/voxel_grid.h"
21#include "pcl/io/pcd_io.h"
22#include "pcl/point_types.h"
23
27
28namespace apollo {
29namespace perception {
30namespace lidar {
31
32typedef pcl::PointXYZRGB CPoint;
33typedef pcl::PointCloud<CPoint> CPointCloud;
34typedef pcl::PointCloud<CPoint>::Ptr CPointCloudPtr;
35typedef pcl::PointCloud<CPoint>::ConstPtr CPointCloudConstPtr;
36
38 float x;
39 float y;
40 float z;
41 std::uint8_t intensity;
42 double timestamp;
43 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
45
47 float x;
48 float y;
49 float z;
50 uint32_t label;
51 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
53
54inline bool LoadPCLPCD(const std::string& file_path,
55 base::PointFCloud* cloud_out) {
56 pcl::PointCloud<PCLPointXYZIT> org_cloud;
57 if (pcl::io::loadPCDFile(file_path, org_cloud) < 0) {
58 AERROR << "Failed to load pcd file " << file_path;
59 return false;
60 }
61 cloud_out->resize(org_cloud.size());
62 for (size_t i = 0; i < org_cloud.size(); ++i) {
63 auto& pt = org_cloud.points[i];
64 auto& new_pt = cloud_out->at(i);
65 new_pt.x = pt.x;
66 new_pt.y = pt.y;
67 new_pt.z = pt.z;
68 new_pt.intensity = pt.intensity;
69 cloud_out->mutable_points_timestamp()->at(i) = pt.timestamp;
70 }
71 return true;
72}
73
74inline bool WritePcdByLabel(const std::string& file_path,
75 const base::PointFCloud& cloud,
76 const LidarPointLabel& label) {
77 pcl::PointCloud<PCLPointXYZIT> pcl_cloud;
78 for (size_t i = 0; i < cloud.size(); ++i) {
79 if (cloud.points_label().at(i) != static_cast<uint8_t>(label))
80 continue;
81 PCLPointXYZIT point;
82 point.x = cloud[i].x;
83 point.y = cloud[i].y;
84 point.z = cloud[i].z;
85 point.intensity = cloud[i].intensity;
86 point.timestamp = cloud.points_timestamp().at(i);
87 pcl_cloud.push_back(point);
88 }
89 try {
90 pcl::PCDWriter writer;
91 writer.writeBinaryCompressed(file_path, pcl_cloud);
92 } catch (const pcl::IOException& ex) {
93 AERROR << ex.detailedMessage();
94 return false;
95 }
96 return true;
97}
98
99template <typename PointT>
101 const base::AttributePointCloud<PointT>& org_cloud,
102 const pcl::PointCloud<pcl::PointXYZI>::Ptr& out_cloud_ptr) {
103 for (size_t i = 0; i < org_cloud.size(); ++i) {
104 PointT pt = org_cloud.at(i);
105 pcl::PointXYZI point;
106 point.x = static_cast<float>(pt.x);
107 point.y = static_cast<float>(pt.y);
108 point.z = static_cast<float>(pt.z);
109 point.intensity = static_cast<float>(pt.intensity);
110 out_cloud_ptr->push_back(point);
111 }
112}
113
115 const pcl::PointCloud<pcl::PointXYZI>::Ptr& org_cloud_ptr,
116 const base::PointFCloudPtr& out_cloud_ptr) {
117 for (size_t i = 0; i < org_cloud_ptr->size(); ++i) {
118 const auto& pt = org_cloud_ptr->at(i);
119 base::PointF point;
120 point.x = pt.x;
121 point.y = pt.y;
122 point.z = pt.z;
123 point.intensity = pt.intensity;
124 out_cloud_ptr->push_back(point);
125 }
126}
127
129 const pcl::PointCloud<pcl::PointXYZI>::Ptr& cloud_ptr,
130 const pcl::PointCloud<pcl::PointXYZI>::Ptr& filtered_cloud_ptr,
131 float lx = 0.01f, float ly = 0.01f, float lz = 0.01f) {
132 pcl::VoxelGrid<pcl::PointXYZI> voxel_grid;
133 voxel_grid.setInputCloud(cloud_ptr);
134 voxel_grid.setLeafSize(lx, ly, lz);
135 voxel_grid.filter(*filtered_cloud_ptr);
136}
137
138} // namespace lidar
139} // namespace perception
140} // namespace apollo
141
142POINT_CLOUD_REGISTER_POINT_STRUCT(apollo::perception::lidar::PCLPointXYZIT,
143 (float, x, x)(float, y, y)(float, z, z)(
144 std::uint8_t, intensity,
145 intensity)(double, timestamp, timestamp))
146
147POINT_CLOUD_REGISTER_POINT_STRUCT(apollo::perception::lidar::PCLPointXYZL,
148 (float, x, x)(float, y, y)(float, z,
149 z)(std::uint32_t,
150 label, label))
const std::vector< double > & points_timestamp() const
void resize(const size_t size) override
const std::vector< uint8_t > & points_label() const
std::vector< double > * mutable_points_timestamp()
const PointT * at(size_t col, size_t row) const
Definition point_cloud.h:63
#define AERROR
Definition log.h:44
std::shared_ptr< PointFCloud > PointFCloudPtr
pcl::PointXYZRGB CPoint
Definition pcl_util.h:32
struct apollo::perception::lidar::PCLPointXYZIT EIGEN_ALIGN16
pcl::PointCloud< CPoint >::ConstPtr CPointCloudConstPtr
Definition pcl_util.h:35
pcl::PointCloud< CPoint > CPointCloud
Definition pcl_util.h:33
void TransformToPCLXYZI(const base::AttributePointCloud< PointT > &org_cloud, const pcl::PointCloud< pcl::PointXYZI >::Ptr &out_cloud_ptr)
Definition pcl_util.h:100
bool WritePcdByLabel(const std::string &file_path, const base::PointFCloud &cloud, const LidarPointLabel &label)
Definition pcl_util.h:74
void DownSampleCloudByVoxelGrid(const pcl::PointCloud< pcl::PointXYZI >::Ptr &cloud_ptr, const pcl::PointCloud< pcl::PointXYZI >::Ptr &filtered_cloud_ptr, float lx=0.01f, float ly=0.01f, float lz=0.01f)
Definition pcl_util.h:128
bool LoadPCLPCD(const std::string &file_path, base::PointFCloud *cloud_out)
Definition pcl_util.h:54
void TransformFromPCLXYZI(const pcl::PointCloud< pcl::PointXYZI >::Ptr &org_cloud_ptr, const base::PointFCloudPtr &out_cloud_ptr)
Definition pcl_util.h:114
pcl::PointCloud< CPoint >::Ptr CPointCloudPtr
Definition pcl_util.h:34
class register implement
Definition arena_queue.h:37