18 #include "cartographer/mapping/proto/serialization.pb.h" 24 using mapping::MapById;
25 using mapping::NodeId;
26 using mapping::PoseGraphInterface;
27 using mapping::SubmapId;
28 using mapping::TrajectoryNode;
29 using mapping::proto::SerializedData;
31 mapping::proto::AllTrajectoryBuilderOptions
32 CreateAllTrajectoryBuilderOptionsProto(
33 const std::vector<mapping::proto::TrajectoryBuilderOptionsWithSensorIds>&
34 all_options_with_sensor_ids) {
35 mapping::proto::AllTrajectoryBuilderOptions all_options_proto;
36 for (
const auto& options_with_sensor_ids : all_options_with_sensor_ids) {
37 *all_options_proto.add_options_with_sensor_ids() = options_with_sensor_ids;
39 CHECK_EQ(all_options_with_sensor_ids.size(),
40 all_options_proto.options_with_sensor_ids_size());
41 return all_options_proto;
44 mapping::proto::SerializationHeader CreateHeader() {
45 mapping::proto::SerializationHeader header;
50 SerializedData SerializePoseGraph(
const mapping::PoseGraph& pose_graph) {
52 *proto.mutable_pose_graph() = pose_graph.ToProto();
56 SerializedData SerializeAllTrajectoryBuilderOptions(
57 const std::vector<mapping::proto::TrajectoryBuilderOptionsWithSensorIds>&
58 trajectory_builder_options) {
60 *proto.mutable_all_trajectory_builder_options() =
61 CreateAllTrajectoryBuilderOptionsProto(trajectory_builder_options);
65 void SerializeSubmaps(
66 const MapById<SubmapId, PoseGraphInterface::SubmapData>& submap_data,
67 ProtoStreamWriterInterface*
const writer) {
70 for (
const auto& submap_id_data : submap_data) {
71 auto*
const submap_proto = proto.mutable_submap();
72 submap_proto->mutable_submap_id()->set_trajectory_id(
73 submap_id_data.id.trajectory_id);
74 submap_proto->mutable_submap_id()->set_submap_index(
75 submap_id_data.id.submap_index);
76 submap_id_data.data.submap->ToProto(submap_proto,
78 writer->WriteProto(proto);
82 void SerializeTrajectoryNodes(
83 const MapById<NodeId, TrajectoryNode>& trajectory_nodes,
84 ProtoStreamWriterInterface*
const writer) {
86 for (
const auto& node_id_data : trajectory_nodes) {
87 auto*
const node_proto = proto.mutable_node();
88 node_proto->mutable_node_id()->set_trajectory_id(
89 node_id_data.id.trajectory_id);
90 node_proto->mutable_node_id()->set_node_index(node_id_data.id.node_index);
91 *node_proto->mutable_node_data() =
92 ToProto(*node_id_data.data.constant_data);
93 writer->WriteProto(proto);
97 void SerializeTrajectoryData(
98 const std::map<int, PoseGraphInterface::TrajectoryData>&
100 ProtoStreamWriterInterface*
const writer) {
101 SerializedData proto;
102 for (
const auto& trajectory_data : all_trajectory_data) {
103 auto*
const trajectory_data_proto = proto.mutable_trajectory_data();
104 trajectory_data_proto->set_trajectory_id(trajectory_data.first);
105 trajectory_data_proto->set_gravity_constant(
106 trajectory_data.second.gravity_constant);
108 Eigen::Quaterniond(trajectory_data.second.imu_calibration[0],
109 trajectory_data.second.imu_calibration[1],
110 trajectory_data.second.imu_calibration[2],
111 trajectory_data.second.imu_calibration[3]));
112 if (trajectory_data.second.fixed_frame_origin_in_map.has_value()) {
113 *trajectory_data_proto->mutable_fixed_frame_origin_in_map() =
115 trajectory_data.second.fixed_frame_origin_in_map.value());
117 writer->WriteProto(proto);
121 void SerializeImuData(
const sensor::MapByTime<sensor::ImuData>& all_imu_data,
122 ProtoStreamWriterInterface*
const writer) {
123 SerializedData proto;
124 for (
const int trajectory_id : all_imu_data.trajectory_ids()) {
125 for (
const auto& imu_data : all_imu_data.trajectory(trajectory_id)) {
126 auto*
const imu_data_proto = proto.mutable_imu_data();
127 imu_data_proto->set_trajectory_id(trajectory_id);
129 writer->WriteProto(proto);
134 void SerializeOdometryData(
135 const sensor::MapByTime<sensor::OdometryData>& all_odometry_data,
136 ProtoStreamWriterInterface*
const writer) {
137 SerializedData proto;
138 for (
const int trajectory_id : all_odometry_data.trajectory_ids()) {
139 for (
const auto& odometry_data :
140 all_odometry_data.trajectory(trajectory_id)) {
141 auto*
const odometry_data_proto = proto.mutable_odometry_data();
142 odometry_data_proto->set_trajectory_id(trajectory_id);
143 *odometry_data_proto->mutable_odometry_data() =
145 writer->WriteProto(proto);
150 void SerializeFixedFramePoseData(
151 const sensor::MapByTime<sensor::FixedFramePoseData>&
152 all_fixed_frame_pose_data,
153 ProtoStreamWriterInterface*
const writer) {
154 SerializedData proto;
155 for (
const int trajectory_id : all_fixed_frame_pose_data.trajectory_ids()) {
156 for (
const auto& fixed_frame_pose_data :
157 all_fixed_frame_pose_data.trajectory(trajectory_id)) {
158 auto*
const fixed_frame_pose_data_proto =
159 proto.mutable_fixed_frame_pose_data();
160 fixed_frame_pose_data_proto->set_trajectory_id(trajectory_id);
161 *fixed_frame_pose_data_proto->mutable_fixed_frame_pose_data() =
163 writer->WriteProto(proto);
168 void SerializeLandmarkNodes(
169 const std::map<std::string, PoseGraphInterface::LandmarkNode>&
171 ProtoStreamWriterInterface*
const writer) {
172 SerializedData proto;
173 for (
const auto& node : all_landmark_nodes) {
174 for (
const auto& observation : node.second.landmark_observations) {
175 auto* landmark_data_proto = proto.mutable_landmark_data();
176 landmark_data_proto->set_trajectory_id(observation.trajectory_id);
177 landmark_data_proto->mutable_landmark_data()->set_timestamp(
179 auto* observation_proto = landmark_data_proto->mutable_landmark_data()
180 ->add_landmark_observations();
181 observation_proto->set_id(node.first);
182 *observation_proto->mutable_landmark_to_tracking_transform() =
184 observation_proto->set_translation_weight(observation.translation_weight);
185 observation_proto->set_rotation_weight(observation.rotation_weight);
186 writer->WriteProto(proto);
195 const std::vector<mapping::proto::TrajectoryBuilderOptionsWithSensorIds>&
196 trajectory_builder_options,
199 writer->
WriteProto(SerializePoseGraph(pose_graph));
201 SerializeAllTrajectoryBuilderOptions(trajectory_builder_options));
206 SerializeImuData(pose_graph.
GetImuData(), writer);
virtual sensor::MapByTime< sensor::ImuData > GetImuData() const =0
void WritePbStream(const mapping::PoseGraph &pose_graph, const std::vector< mapping::proto::TrajectoryBuilderOptionsWithSensorIds > &trajectory_builder_options, ProtoStreamWriterInterface *const writer)
virtual sensor::MapByTime< sensor::FixedFramePoseData > GetFixedFramePoseData() const =0
proto::FixedFramePoseData ToProto(const FixedFramePoseData &pose_data)
static constexpr int kMappingStateSerializationFormatVersion
virtual std::map< int, TrajectoryData > GetTrajectoryData() const =0
int64 ToUniversal(const Time time)
virtual std::map< std::string, PoseGraph::LandmarkNode > GetLandmarkNodes() const =0
virtual MapById< SubmapId, SubmapData > GetAllSubmapData() const =0
virtual void WriteProto(const google::protobuf::Message &proto)=0
proto::SensorId ToProto(const mapping::TrajectoryBuilderInterface::SensorId &sensor_id)
virtual MapById< NodeId, TrajectoryNode > GetTrajectoryNodes() const =0
virtual sensor::MapByTime< sensor::OdometryData > GetOdometryData() const =0