19 #include <unordered_map> 22 #include "cartographer/mapping/proto/internal/legacy_serialized_data.pb.h" 23 #include "cartographer/mapping/proto/trajectory_builder_options.pb.h" 24 #include "glog/logging.h" 30 using mapping::proto::SerializedData;
31 using ProtoMap = std::unordered_map<int, std::vector<SerializedData>>;
34 ProtoMap* proto_map) {
35 auto& pose_graph_vec = (*proto_map)[SerializedData::kPoseGraph];
36 pose_graph_vec.emplace_back();
37 return input->
ReadProto(pose_graph_vec.back().mutable_pose_graph());
40 bool ReadBuilderOptions(
42 ProtoMap* proto_map) {
44 (*proto_map)[SerializedData::kAllTrajectoryBuilderOptions];
45 options_vec.emplace_back();
47 options_vec.back().mutable_all_trajectory_builder_options());
51 ProtoMap* proto_map) {
52 mapping::proto::LegacySerializedData legacy_data;
53 if (!input->
ReadProto(&legacy_data))
return false;
55 if (legacy_data.has_submap()) {
56 auto& output_vector = (*proto_map)[SerializedData::kSubmapFieldNumber];
57 output_vector.emplace_back();
58 *output_vector.back().mutable_submap() = legacy_data.submap();
60 if (legacy_data.has_node()) {
61 auto& output_vector = (*proto_map)[SerializedData::kNodeFieldNumber];
62 output_vector.emplace_back();
63 *output_vector.back().mutable_node() = legacy_data.node();
65 if (legacy_data.has_trajectory_data()) {
67 (*proto_map)[SerializedData::kTrajectoryDataFieldNumber];
68 output_vector.emplace_back();
69 *output_vector.back().mutable_trajectory_data() =
70 legacy_data.trajectory_data();
72 if (legacy_data.has_imu_data()) {
73 auto& output_vector = (*proto_map)[SerializedData::kImuDataFieldNumber];
74 output_vector.emplace_back();
75 *output_vector.back().mutable_imu_data() = legacy_data.imu_data();
77 if (legacy_data.has_odometry_data()) {
78 auto& output_vector = (*proto_map)[SerializedData::kOdometryData];
79 output_vector.emplace_back();
80 *output_vector.back().mutable_odometry_data() = legacy_data.odometry_data();
82 if (legacy_data.has_fixed_frame_pose_data()) {
84 (*proto_map)[SerializedData::kFixedFramePoseDataFieldNumber];
85 output_vector.emplace_back();
86 *output_vector.back().mutable_fixed_frame_pose_data() =
87 legacy_data.fixed_frame_pose_data();
89 if (legacy_data.has_landmark_data()) {
91 (*proto_map)[SerializedData::kLandmarkDataFieldNumber];
92 output_vector.emplace_back();
93 *output_vector.back().mutable_landmark_data() = legacy_data.landmark_data();
98 ProtoMap ParseLegacyData(
101 CHECK(ReadPoseGraph(input, &proto_map))
102 <<
"Input stream seems to differ from original stream format. Could " 104 "read PoseGraph as first message.";
105 CHECK(ReadBuilderOptions(input, &proto_map))
106 <<
"Input stream seems to differ from original stream format. Could " 108 "read AllTrajectoryBuilderOptions as second message.";
110 }
while (DeserializeNext(input, &proto_map));
114 mapping::proto::SerializationHeader CreateSerializationHeader() {
115 constexpr uint32_t kVersion1 = 1;
116 mapping::proto::SerializationHeader header;
117 header.set_format_version(kVersion1);
121 void SerializeToVersion1Format(
122 const ProtoMap& deserialized_data,
124 const std::vector<int> kFieldSerializationOrder = {
125 SerializedData::kPoseGraphFieldNumber,
126 SerializedData::kAllTrajectoryBuilderOptionsFieldNumber,
127 SerializedData::kSubmapFieldNumber,
128 SerializedData::kNodeFieldNumber,
129 SerializedData::kTrajectoryDataFieldNumber,
130 SerializedData::kImuDataFieldNumber,
131 SerializedData::kOdometryDataFieldNumber,
132 SerializedData::kFixedFramePoseDataFieldNumber,
133 SerializedData::kLandmarkDataFieldNumber};
135 output->
WriteProto(CreateSerializationHeader());
136 for (
auto field_index : kFieldSerializationOrder) {
137 const auto proto_vector_it = deserialized_data.find(field_index);
138 if (proto_vector_it == deserialized_data.end())
continue;
139 for (
const auto& proto : proto_vector_it->second) {
149 SerializeToVersion1Format(ParseLegacyData(input), output);
void MigrateStreamFormatToVersion1(cartographer::io::ProtoStreamReaderInterface *const input, cartographer::io::ProtoStreamWriterInterface *const output)
virtual bool ReadProto(google::protobuf::Message *proto)=0
virtual void WriteProto(const google::protobuf::Message &proto)=0