Apollo  6.0
Open source self driving car software
pcl_util.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 #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 
26 
27 namespace apollo {
28 namespace perception {
29 namespace lidar {
30 
31 typedef pcl::PointXYZRGB CPoint;
32 typedef pcl::PointCloud<CPoint> CPointCloud;
33 typedef pcl::PointCloud<CPoint>::Ptr CPointCloudPtr;
34 typedef pcl::PointCloud<CPoint>::ConstPtr CPointCloudConstPtr;
35 
36 struct PCLPointXYZIT {
37  float x;
38  float y;
39  float z;
40  std::uint8_t intensity;
41  double timestamp;
42  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
44 
45 struct PCLPointXYZL {
46  float x;
47  float y;
48  float z;
49  uint32_t label;
50  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
52 
53 inline bool LoadPCLPCD(const std::string& file_path,
54  base::PointFCloud* cloud_out) {
55  pcl::PointCloud<PCLPointXYZIT> org_cloud;
56  if (pcl::io::loadPCDFile(file_path, org_cloud) < 0) {
57  AERROR << "Failed to load pcd file " << file_path;
58  return false;
59  }
60  cloud_out->resize(org_cloud.size());
61  for (size_t i = 0; i < org_cloud.size(); ++i) {
62  auto& pt = org_cloud.points[i];
63  auto& new_pt = cloud_out->at(i);
64  new_pt.x = pt.x;
65  new_pt.y = pt.y;
66  new_pt.z = pt.z;
67  new_pt.intensity = pt.intensity;
68  cloud_out->mutable_points_timestamp()->at(i) = pt.timestamp;
69  }
70  return true;
71 }
72 
73 // static bool WritePcd(const std::string& file_path,
74 // const base::PointFCloud& cloud) {
75 // pcl::PointCloud<PCLPointXYZL> pcl_cloud;
76 // for (size_t i = 0; i < cloud.size(); ++i) {
77 // PCLPointXYZL point;
78 // point.x = cloud[i].x;
79 // point.y = cloud[i].y;
80 // point.z = cloud[i].z;
81 // point.label = cloud.points_label().at(i);
82 // pcl_cloud.push_back(point);
83 // }
84 // try {
85 // pcl::PCDWriter writer;
86 // writer.writeBinaryCompressed(file_path, pcl_cloud);
87 // } catch (const pcl::IOException& ex) {
88 // AERROR << ex.detailedMessage();
89 // return false;
90 // }
91 // return true;
92 // }
93 //
94 
95 template <typename PointT>
96 inline void TransformToPCLXYZI(
97  const base::AttributePointCloud<PointT>& org_cloud,
98  const pcl::PointCloud<pcl::PointXYZI>::Ptr& out_cloud_ptr) {
99  for (size_t i = 0; i < org_cloud.size(); ++i) {
100  PointT pt = org_cloud.at(i);
101  pcl::PointXYZI point;
102  point.x = static_cast<float>(pt.x);
103  point.y = static_cast<float>(pt.y);
104  point.z = static_cast<float>(pt.z);
105  point.intensity = static_cast<float>(pt.intensity);
106  out_cloud_ptr->push_back(point);
107  }
108 }
109 
111  const pcl::PointCloud<pcl::PointXYZI>::Ptr& org_cloud_ptr,
112  const base::PointFCloudPtr& out_cloud_ptr) {
113  for (size_t i = 0; i < org_cloud_ptr->size(); ++i) {
114  const auto& pt = org_cloud_ptr->at(i);
115  base::PointF point;
116  point.x = pt.x;
117  point.y = pt.y;
118  point.z = pt.z;
119  point.intensity = pt.intensity;
120  out_cloud_ptr->push_back(point);
121  }
122 }
123 
125  const pcl::PointCloud<pcl::PointXYZI>::Ptr& cloud_ptr,
126  const pcl::PointCloud<pcl::PointXYZI>::Ptr& filtered_cloud_ptr,
127  float lx = 0.01f, float ly = 0.01f, float lz = 0.01f) {
128  pcl::VoxelGrid<pcl::PointXYZI> voxel_grid;
129  voxel_grid.setInputCloud(cloud_ptr);
130  voxel_grid.setLeafSize(lx, ly, lz);
131  voxel_grid.filter(*filtered_cloud_ptr);
132 }
133 
134 } // namespace lidar
135 } // namespace perception
136 } // namespace apollo
137 
138 POINT_CLOUD_REGISTER_POINT_STRUCT(apollo::perception::lidar::PCLPointXYZIT,
139  (float, x, x)(float, y, y)(float, z, z)(
140  std::uint8_t, intensity,
141  intensity)(double, timestamp, timestamp))
142 
143 POINT_CLOUD_REGISTER_POINT_STRUCT(apollo::perception::lidar::PCLPointXYZL,
144  (float, x, x)(float, y, y)(float, z, z)(
145  std::uint32_t, label, label))
void resize(const size_t size) override
Definition: point_cloud.h:324
float z
Definition: pcl_util.h:39
void TransformFromPCLXYZI(const pcl::PointCloud< pcl::PointXYZI >::Ptr &org_cloud_ptr, const base::PointFCloudPtr &out_cloud_ptr)
Definition: pcl_util.h:110
float y
Definition: pcl_util.h:38
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:124
pcl::PointXYZRGB CPoint
Definition: pcl_util.h:31
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: point.h:28
pcl::PointCloud< CPoint >::Ptr CPointCloudPtr
Definition: pcl_util.h:33
pcl::PointCloud< CPoint > CPointCloud
Definition: pcl_util.h:32
pcl::PointCloud< CPoint >::ConstPtr CPointCloudConstPtr
Definition: pcl_util.h:34
T x
Definition: point.h:29
size_t size() const
Definition: point_cloud.h:83
double timestamp
Definition: pcl_util.h:41
float x
Definition: pcl_util.h:46
struct apollo::perception::lidar::PCLPointXYZIT EIGEN_ALIGN16
T z
Definition: point.h:31
uint32_t label
Definition: pcl_util.h:49
float z
Definition: pcl_util.h:48
float y
Definition: pcl_util.h:47
std::vector< double > * mutable_points_timestamp()
Definition: point_cloud.h:445
std::uint8_t intensity
Definition: pcl_util.h:40
bool LoadPCLPCD(const std::string &file_path, base::PointFCloud *cloud_out)
Definition: pcl_util.h:53
std::shared_ptr< PointFCloud > PointFCloudPtr
Definition: point_cloud.h:482
void TransformToPCLXYZI(const base::AttributePointCloud< PointT > &org_cloud, const pcl::PointCloud< pcl::PointXYZI >::Ptr &out_cloud_ptr)
Definition: pcl_util.h:96
T intensity
Definition: point.h:32
#define AERROR
Definition: log.h:44
float x
Definition: pcl_util.h:37
T y
Definition: point.h:30
const PointT * at(size_t col, size_t row) const
Definition: point_cloud.h:64