Apollo 11.0
自动驾驶开放平台
pcd_exporter.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 "cyber/cyber.h"
21#include "pcl/io/pcd_io.h"
22#include "pcl/point_types.h"
23
24namespace apollo {
25namespace localization {
26namespace msf {
27
28PCDExporter::PCDExporter(const std::string &pcd_folder) {
29 pcd_folder_ = pcd_folder;
30 std::string stamp_file = pcd_folder_ + "/pcd_timestamp.txt";
31
32 if ((stamp_file_handle_ = fopen(stamp_file.c_str(), "a")) == nullptr) {
33 AERROR << "Cannot open stamp file!";
34 }
35}
36
38 if (stamp_file_handle_ != nullptr) {
39 fclose(stamp_file_handle_);
40 }
41}
42
43void PCDExporter::CompensatedPcdCallback(const std::string &msg_string) {
44 AINFO << "Compensated pcd callback.";
46 msg.ParseFromString(msg_string);
47
48 static unsigned int index = 1;
49
50 std::stringstream ss_pcd;
51 ss_pcd << pcd_folder_ << "/" << index << ".pcd";
52 std::string pcd_filename = ss_pcd.str();
53
54 WritePcdFile(pcd_filename, msg);
55 double timestamp = cyber::Time(msg.measurement_time()).ToSecond();
56 fprintf(stamp_file_handle_, "%u %lf\n", index, timestamp);
57
58 ++index;
59}
60
61void PCDExporter::WritePcdFile(const std::string &filename,
62 const drivers::PointCloud &msg) {
63 pcl::PointCloud<velodyne::PointXYZIT> cloud;
64 cloud.width = msg.width();
65 cloud.height = msg.height();
66 cloud.is_dense = false;
67 cloud.points.resize(cloud.width * cloud.height);
68
69 if (cloud.width == 0 || cloud.height == 0) {
70 cloud.width = 1;
71 cloud.height = msg.point_size();
72 cloud.points.resize(msg.point_size());
73 }
74
75 for (unsigned int i = 0; i < static_cast<unsigned int>(cloud.points.size());
76 ++i) {
77 cloud.points[i].x = msg.point(i).x();
78 cloud.points[i].y = msg.point(i).y();
79 cloud.points[i].z = msg.point(i).z();
80 cloud.points[i].intensity =
81 static_cast<unsigned char>(msg.point(i).intensity());
82 }
83
84 pcl::io::savePCDFileBinaryCompressed(filename, cloud);
85}
86
87} // namespace msf
88} // namespace localization
89} // namespace apollo
Cyber has builtin time type Time.
Definition time.h:31
double ToSecond() const
convert time to second.
Definition time.cc:77
void CompensatedPcdCallback(const std::string &msg)
PCDExporter(const std::string &pcd_folder)
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
The pcl types.
optional double measurement_time
repeated PointXYZIT point