pbstream_to_ros_map_main.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 The Cartographer Authors
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 
17 #include <map>
18 #include <string>
19 
26 #include "cartographer/mapping/proto/pose_graph.pb.h"
27 #include "cartographer/mapping/proto/serialization.pb.h"
28 #include "cartographer/mapping/proto/submap.pb.h"
29 #include "cartographer/mapping/proto/trajectory_builder_options.pb.h"
32 #include "gflags/gflags.h"
33 #include "glog/logging.h"
34 
35 DEFINE_string(pbstream_filename, "",
36  "Filename of a pbstream to draw a map from.");
37 DEFINE_string(map_filestem, "map", "Stem of the output files.");
38 DEFINE_double(resolution, 0.05, "Resolution of a grid cell in the drawn map.");
39 
40 namespace cartographer_ros {
41 namespace {
42 
43 void Run(const std::string& pbstream_filename, const std::string& map_filestem,
44  const double resolution) {
45  ::cartographer::io::ProtoStreamReader reader(pbstream_filename);
46  ::cartographer::io::ProtoStreamDeserializer deserializer(&reader);
47 
48  const auto& pose_graph = deserializer.pose_graph();
49 
50  LOG(INFO) << "Loading submap slices from serialized data.";
51  std::map<::cartographer::mapping::SubmapId, ::cartographer::io::SubmapSlice>
52  submap_slices;
53  ::cartographer::mapping::proto::SerializedData proto;
54  while (deserializer.ReadNextSerializedData(&proto)) {
55  if (proto.has_submap()) {
56  const auto& submap = proto.submap();
57  const ::cartographer::mapping::SubmapId id{
58  submap.submap_id().trajectory_id(),
59  submap.submap_id().submap_index()};
60  const ::cartographer::transform::Rigid3d global_submap_pose =
61  ::cartographer::transform::ToRigid3(
62  pose_graph.trajectory(id.trajectory_id)
63  .submap(id.submap_index)
64  .pose());
65  FillSubmapSlice(global_submap_pose, submap, &submap_slices[id]);
66  }
67  }
68  CHECK(reader.eof());
69 
70  LOG(INFO) << "Generating combined map image from submap slices.";
71  auto result =
72  ::cartographer::io::PaintSubmapSlices(submap_slices, resolution);
73 
74  ::cartographer::io::StreamFileWriter pgm_writer(map_filestem + ".pgm");
75 
76  ::cartographer::io::Image image(std::move(result.surface));
77  WritePgm(image, resolution, &pgm_writer);
78 
79  const Eigen::Vector2d origin(
80  -result.origin.x() * resolution,
81  (result.origin.y() - image.height()) * resolution);
82 
83  ::cartographer::io::StreamFileWriter yaml_writer(map_filestem + ".yaml");
84  WriteYaml(resolution, origin, pgm_writer.GetFilename(), &yaml_writer);
85 }
86 
87 } // namespace
88 } // namespace cartographer_ros
89 
90 int main(int argc, char** argv) {
91  FLAGS_alsologtostderr = true;
92  google::InitGoogleLogging(argv[0]);
93  google::ParseCommandLineFlags(&argc, &argv, true);
94 
95  CHECK(!FLAGS_pbstream_filename.empty()) << "-pbstream_filename is missing.";
96  CHECK(!FLAGS_map_filestem.empty()) << "-map_filestem is missing.";
97 
98  ::cartographer_ros::Run(FLAGS_pbstream_filename, FLAGS_map_filestem,
99  FLAGS_resolution);
100 }
void WriteYaml(const double resolution, const Eigen::Vector2d &origin, const std::string &pgm_filename, ::cartographer::io::FileWriter *file_writer)
Definition: ros_map.cc:36
int main(int argc, char **argv)
void WritePgm(const ::cartographer::io::Image &image, const double resolution, ::cartographer::io::FileWriter *file_writer)
Definition: ros_map.cc:21
DEFINE_double(resolution, 0.05, "Resolution of a grid cell in the drawn map.")
void FillSubmapSlice(const ::cartographer::transform::Rigid3d &global_submap_pose, const ::cartographer::mapping::proto::Submap &proto, SubmapSlice *const submap_slice)
DEFINE_string(pbstream_filename, "", "Filename of a pbstream to draw a map from.")
void Run(const std::string &configuration_directory, const std::string &configuration_basename)


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:06:05