Apollo 11.0
自动驾驶开放平台
velodyne_utility.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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 "pcl/io/pcd_io.h"
20#include "yaml-cpp/yaml.h"
21
22#include "cyber/common/log.h"
24
25namespace apollo {
26namespace localization {
27namespace msf {
28namespace velodyne {
29
30void LoadPcds(const std::string& file_path, const unsigned int frame_index,
31 const Eigen::Affine3d& pose, VelodyneFrame* velodyne_frame,
32 bool is_global) {
33 velodyne_frame->frame_index = frame_index;
34 velodyne_frame->pose = pose;
35 LoadPcds(file_path, frame_index, pose, &velodyne_frame->pt3ds,
36 &velodyne_frame->intensities, is_global);
37}
38
39void LoadPcds(const std::string& file_path, const unsigned int frame_index,
40 const Eigen::Affine3d& pose,
42 std::vector<unsigned char>* intensities, bool is_global) {
43 Eigen::Affine3d pose_inv = pose.inverse();
44 pcl::PointCloud<PointXYZIT>::Ptr cloud(new pcl::PointCloud<PointXYZIT>);
45 if (pcl::io::loadPCDFile(file_path, *cloud) >= 0) {
46 if (cloud->height == 1 || cloud->width == 1) {
47 AINFO << "Un-organized-point-cloud";
48 for (unsigned int i = 0; i < cloud->size(); ++i) {
49 Eigen::Vector3d pt3d;
50 pt3d[0] = (*cloud)[i].x;
51 pt3d[1] = (*cloud)[i].y;
52 pt3d[2] = (*cloud)[i].z;
53
54 if (pt3d[0] == pt3d[0] && pt3d[1] == pt3d[1] && pt3d[2] == pt3d[2]) {
55 Eigen::Vector3d pt3d_local;
56 if (is_global) {
57 pt3d_local = pose_inv * pt3d;
58 } else {
59 pt3d_local = pt3d;
60 }
61 unsigned char intensity =
62 static_cast<unsigned char>((*cloud)[i].intensity);
63 pt3ds->push_back(pt3d_local);
64 intensities->push_back(intensity);
65 }
66 }
67 } else {
68 for (unsigned int h = 0; h < cloud->height; ++h) {
69 for (unsigned int w = 0; w < cloud->width; ++w) {
70 double x = cloud->at(w, h).x;
71 double y = cloud->at(w, h).y;
72 double z = cloud->at(w, h).z;
73 Eigen::Vector3d pt3d(x, y, z);
74 if (pt3d[0] == pt3d[0] && pt3d[1] == pt3d[1] && pt3d[2] == pt3d[2]) {
75 Eigen::Vector3d pt3d_local;
76 if (is_global) {
77 pt3d_local = pose_inv * pt3d;
78 } else {
79 pt3d_local = pt3d;
80 }
81 unsigned char intensity =
82 static_cast<unsigned char>(cloud->at(w, h).intensity);
83 pt3ds->push_back(pt3d_local);
84 intensities->push_back(intensity);
85 }
86 }
87 }
88 }
89 } else {
90 AERROR << "Failed to load PCD file: " << file_path;
91 }
92}
93
94void LoadPcdPoses(const std::string& file_path,
96 std::vector<double>* timestamps) {
97 std::vector<unsigned int> pcd_indices;
98 LoadPcdPoses(file_path, poses, timestamps, &pcd_indices);
99}
100
101void LoadPcdPoses(const std::string& file_path,
103 std::vector<double>* timestamps,
104 std::vector<unsigned int>* pcd_indices) {
105 poses->clear();
106 timestamps->clear();
107 pcd_indices->clear();
108
109 FILE* file = fopen(file_path.c_str(), "r");
110 if (file) {
111 unsigned int index;
112 double timestamp;
113 double x, y, z;
114 double qx, qy, qz, qr;
115 static constexpr int kSize = 9;
116 while (fscanf(file, "%u %lf %lf %lf %lf %lf %lf %lf %lf\n", &index,
117 &timestamp, &x, &y, &z, &qx, &qy, &qz, &qr) == kSize) {
118 Eigen::Translation3d trans(Eigen::Vector3d(x, y, z));
119 Eigen::Quaterniond quat(qr, qx, qy, qz);
120 poses->push_back(trans * quat);
121 timestamps->push_back(timestamp);
122 pcd_indices->push_back(index);
123 }
124 fclose(file);
125 } else {
126 AERROR << "Can't open file to read: " << file_path;
127 }
128}
129
130void LoadPosesAndStds(const std::string& file_path,
133 std::vector<double>* timestamps) {
134 poses->clear();
135 stds->clear();
136 timestamps->clear();
137
138 FILE* file = fopen(file_path.c_str(), "r");
139 if (file) {
140 unsigned int index;
141 double timestamp;
142 double x, y, z;
143 double qx, qy, qz, qr;
144 double std_x, std_y, std_z;
145 static constexpr int kSize = 12;
146 while (fscanf(file, "%u %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",
147 &index, &timestamp, &x, &y, &z, &qx, &qy, &qz, &qr, &std_x,
148 &std_y, &std_z) == kSize) {
149 Eigen::Translation3d trans(Eigen::Vector3d(x, y, z));
150 Eigen::Quaterniond quat(qr, qx, qy, qz);
151 poses->push_back(trans * quat);
152 timestamps->push_back(timestamp);
153
154 Eigen::Vector3d std;
155 std << std_x, std_y, std_z;
156 stds->push_back(std);
157 }
158 fclose(file);
159 } else {
160 AERROR << "Can't open file to read: " << file_path;
161 }
162}
163
164bool LoadExtrinsic(const std::string& file_path, Eigen::Affine3d* extrinsic) {
165 YAML::Node config = YAML::LoadFile(file_path);
166 if (config["transform"]) {
167 if (config["transform"]["translation"]) {
168 extrinsic->translation()(0) =
169 config["transform"]["translation"]["x"].as<double>();
170 extrinsic->translation()(1) =
171 config["transform"]["translation"]["y"].as<double>();
172 extrinsic->translation()(2) =
173 config["transform"]["translation"]["z"].as<double>();
174 if (config["transform"]["rotation"]) {
175 double qx = config["transform"]["rotation"]["x"].as<double>();
176 double qy = config["transform"]["rotation"]["y"].as<double>();
177 double qz = config["transform"]["rotation"]["z"].as<double>();
178 double qw = config["transform"]["rotation"]["w"].as<double>();
179 extrinsic->linear() =
180 Eigen::Quaterniond(qw, qx, qy, qz).toRotationMatrix();
181 return true;
182 }
183 }
184 }
185 return false;
186}
187
188} // namespace velodyne
189} // namespace msf
190} // namespace localization
191} // namespace apollo
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
EigenVector< Eigen::Affine3d > EigenAffine3dVec
Definition eigen_defs.h:47
EigenVector< Eigen::Vector3d > EigenVector3dVec
Definition eigen_defs.h:46
void LoadPcds(const std::string &file_path, const unsigned int frame_index, const Eigen::Affine3d &pose, VelodyneFrame *velodyne_frame, bool is_global)
bool LoadExtrinsic(const std::string &file_path, Eigen::Affine3d *extrinsic)
Load the velodyne extrinsic from a YAML file.
void LoadPosesAndStds(const std::string &file_path, ::apollo::common::EigenAffine3dVec *poses, ::apollo::common::EigenVector3dVec *stds, std::vector< double > *timestamps)
Load poses and stds their timestamps.
void LoadPcdPoses(const std::string &file_path, ::apollo::common::EigenAffine3dVec *poses, std::vector< double > *timestamps)
Load the PCD poses with their timestamps.
class register implement
Definition arena_queue.h:37
Definition future.h:29
The pcl types.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW unsigned int frame_index
The frame index.
::apollo::common::EigenVector3dVec pt3ds
The 3D point cloud in this frame.
std::vector< unsigned char > intensities
The laser reflection values in this frames.
Eigen::Affine3d pose
The pose of the frame.
The utilities of velodyne.