30 #include "cartographer/mapping/proto/internal/legacy_serialized_data.pb.h" 42 using mapping::proto::SerializedData;
44 std::vector<std::string> SelectRangeSensorIds(
45 const std::set<MapBuilder::SensorId>& expected_sensor_ids) {
46 std::vector<std::string> range_sensor_ids;
49 range_sensor_ids.push_back(sensor_id.id);
52 return range_sensor_ids;
59 proto::MapBuilderOptions options;
60 options.set_use_trajectory_builder_2d(
61 parameter_dictionary->
GetBool(
"use_trajectory_builder_2d"));
62 options.set_use_trajectory_builder_3d(
63 parameter_dictionary->
GetBool(
"use_trajectory_builder_3d"));
64 options.set_num_background_threads(
68 CHECK_NE(options.use_trajectory_builder_2d(),
69 options.use_trajectory_builder_3d());
75 CHECK(options.use_trajectory_builder_2d() ^
76 options.use_trajectory_builder_3d());
77 if (options.use_trajectory_builder_2d()) {
80 common::make_unique<optimization::OptimizationProblem2D>(
81 options_.pose_graph_options().optimization_problem_options()),
84 if (options.use_trajectory_builder_3d()) {
87 common::make_unique<optimization::OptimizationProblem3D>(
88 options_.pose_graph_options().optimization_problem_options()),
91 if (options.collate_by_trajectory()) {
99 const std::set<SensorId>& expected_sensor_ids,
100 const proto::TrajectoryBuilderOptions& trajectory_options,
103 if (
options_.use_trajectory_builder_3d()) {
104 std::unique_ptr<LocalTrajectoryBuilder3D> local_trajectory_builder;
105 if (trajectory_options.has_trajectory_builder_3d_options()) {
106 local_trajectory_builder = common::make_unique<LocalTrajectoryBuilder3D>(
107 trajectory_options.trajectory_builder_3d_options(),
108 SelectRangeSensorIds(expected_sensor_ids));
110 DCHECK(dynamic_cast<PoseGraph3D*>(
pose_graph_.get()));
112 common::make_unique<CollatedTrajectoryBuilder>(
115 std::move(local_trajectory_builder), trajectory_id,
117 local_slam_result_callback)));
119 std::unique_ptr<LocalTrajectoryBuilder2D> local_trajectory_builder;
120 if (trajectory_options.has_trajectory_builder_2d_options()) {
121 local_trajectory_builder = common::make_unique<LocalTrajectoryBuilder2D>(
122 trajectory_options.trajectory_builder_2d_options(),
123 SelectRangeSensorIds(expected_sensor_ids));
125 DCHECK(dynamic_cast<PoseGraph2D*>(
pose_graph_.get()));
127 common::make_unique<CollatedTrajectoryBuilder>(
130 std::move(local_trajectory_builder), trajectory_id,
132 local_slam_result_callback)));
134 if (trajectory_options.has_overlapping_submaps_trimmer_2d()) {
135 const auto& trimmer_options =
136 trajectory_options.overlapping_submaps_trimmer_2d();
137 pose_graph_->AddTrimmer(common::make_unique<OverlappingSubmapsTrimmer2D>(
138 trimmer_options.fresh_submaps_count(),
139 trimmer_options.min_covered_area() /
140 common::Pow2(trajectory_options.trajectory_builder_2d_options()
144 trimmer_options.min_added_submaps_count()));
147 if (trajectory_options.pure_localization()) {
148 constexpr
int kSubmapsToKeep = 3;
149 pose_graph_->AddTrimmer(common::make_unique<PureLocalizationTrimmer>(
150 trajectory_id, kSubmapsToKeep));
152 if (trajectory_options.has_initial_trajectory_pose()) {
153 const auto& initial_trajectory_pose =
154 trajectory_options.initial_trajectory_pose();
156 trajectory_id, initial_trajectory_pose.to_trajectory_id(),
160 proto::TrajectoryBuilderOptionsWithSensorIds options_with_sensor_ids_proto;
161 for (
const auto& sensor_id : expected_sensor_ids) {
162 *options_with_sensor_ids_proto.add_sensor_id() =
ToProto(sensor_id);
164 *options_with_sensor_ids_proto.mutable_trajectory_builder_options() =
168 return trajectory_id;
172 const proto::TrajectoryBuilderOptionsWithSensorIds&
173 options_with_sensor_ids_proto) {
178 return trajectory_id;
187 const SubmapId& submap_id, proto::SubmapQuery::Response*
const response) {
190 return "Requested submap from trajectory " +
191 std::to_string(submap_id.
trajectory_id) +
" but there are only " +
195 const auto submap_data =
pose_graph_->GetSubmapData(submap_id);
196 if (submap_data.submap ==
nullptr) {
197 return "Requested submap " + std::to_string(submap_id.
submap_index) +
198 " from trajectory " + std::to_string(submap_id.
trajectory_id) +
199 " but it does not exist: maybe it has been trimmed.";
201 submap_data.submap->ToResponseProto(submap_data.pose, response);
210 bool load_frozen_state) {
215 proto::PoseGraph pose_graph_proto = deserializer.
pose_graph();
216 const auto& all_builder_options_proto =
219 std::map<int, int> trajectory_remapping;
220 for (
auto& trajectory_proto : *pose_graph_proto.mutable_trajectory()) {
221 const auto& options_with_sensor_ids_proto =
222 all_builder_options_proto.options_with_sensor_ids(
223 trajectory_proto.trajectory_id());
224 const int new_trajectory_id =
226 CHECK(trajectory_remapping
227 .emplace(trajectory_proto.trajectory_id(), new_trajectory_id)
229 <<
"Duplicate trajectory ID: " << trajectory_proto.trajectory_id();
230 trajectory_proto.set_trajectory_id(new_trajectory_id);
231 if (load_frozen_state) {
237 for (
auto& constraint_proto : *pose_graph_proto.mutable_constraint()) {
238 constraint_proto.mutable_submap_id()->set_trajectory_id(
239 trajectory_remapping.at(constraint_proto.submap_id().trajectory_id()));
240 constraint_proto.mutable_node_id()->set_trajectory_id(
241 trajectory_remapping.at(constraint_proto.node_id().trajectory_id()));
245 for (
const proto::Trajectory& trajectory_proto :
246 pose_graph_proto.trajectory()) {
247 for (
const proto::Trajectory::Submap& submap_proto :
248 trajectory_proto.submap()) {
250 submap_proto.submap_index()},
256 for (
const proto::Trajectory& trajectory_proto :
257 pose_graph_proto.trajectory()) {
258 for (
const proto::Trajectory::Node& node_proto : trajectory_proto.node()) {
266 for (
const auto& landmark : pose_graph_proto.landmark_poses()) {
267 pose_graph_->SetLandmarkPose(landmark.landmark_id(),
271 SerializedData proto;
273 switch (proto.data_case()) {
274 case SerializedData::kPoseGraph:
275 LOG(ERROR) <<
"Found multiple serialized `PoseGraph`. Serialized " 276 "stream likely corrupt!.";
278 case SerializedData::kAllTrajectoryBuilderOptions:
279 LOG(ERROR) <<
"Found multiple serialized " 280 "`AllTrajectoryBuilderOptions`. Serialized stream likely " 283 case SerializedData::kSubmap: {
284 proto.mutable_submap()->mutable_submap_id()->set_trajectory_id(
285 trajectory_remapping.at(
286 proto.submap().submap_id().trajectory_id()));
289 proto.submap().submap_id().submap_index()});
290 pose_graph_->AddSubmapFromProto(submap_pose, proto.submap());
293 case SerializedData::kNode: {
294 proto.mutable_node()->mutable_node_id()->set_trajectory_id(
295 trajectory_remapping.at(proto.node().node_id().trajectory_id()));
298 proto.node().node_id().node_index()});
299 pose_graph_->AddNodeFromProto(node_pose, proto.node());
302 case SerializedData::kTrajectoryData: {
303 proto.mutable_trajectory_data()->set_trajectory_id(
304 trajectory_remapping.at(proto.trajectory_data().trajectory_id()));
305 pose_graph_->SetTrajectoryDataFromProto(proto.trajectory_data());
308 case SerializedData::kImuData: {
309 if (load_frozen_state)
break;
311 trajectory_remapping.at(proto.imu_data().trajectory_id()),
315 case SerializedData::kOdometryData: {
316 if (load_frozen_state)
break;
318 trajectory_remapping.at(proto.odometry_data().trajectory_id()),
322 case SerializedData::kFixedFramePoseData: {
323 if (load_frozen_state)
break;
325 trajectory_remapping.at(
326 proto.fixed_frame_pose_data().trajectory_id()),
328 proto.fixed_frame_pose_data().fixed_frame_pose_data()));
331 case SerializedData::kLandmarkData: {
332 if (load_frozen_state)
break;
334 trajectory_remapping.at(proto.landmark_data().trajectory_id()),
339 LOG(WARNING) <<
"Skipping unknown message type in stream: " 340 << proto.GetTypeName();
344 if (load_frozen_state) {
347 for (
const proto::PoseGraph::Constraint& constraint_proto :
348 pose_graph_proto.constraint()) {
349 if (constraint_proto.tag() !=
350 proto::PoseGraph::Constraint::INTRA_SUBMAP) {
355 constraint_proto.node_id().node_index()},
357 constraint_proto.submap_id().submap_index()});
364 FromProto(pose_graph_proto.constraint()));
366 CHECK(reader->
eof());
PoseGraph::Constraint::Tag FromProto(const proto::PoseGraph::Constraint::Tag &proto)
std::unique_ptr< TrajectoryBuilderInterface > CreateGlobalTrajectoryBuilder2D(std::unique_ptr< LocalTrajectoryBuilder2D > local_trajectory_builder, const int trajectory_id, mapping::PoseGraph2D *const pose_graph, const TrajectoryBuilderInterface::LocalSlamResultCallback &local_slam_result_callback)
void SerializeState(io::ProtoStreamWriterInterface *writer) override
TrajectoryBuilderInterface::SensorId SensorId
void FinishTrajectory(int trajectory_id) override
std::vector< proto::TrajectoryBuilderOptionsWithSensorIds > all_trajectory_builder_options_
TrajectoryBuilderInterface::LocalSlamResultCallback LocalSlamResultCallback
void WritePbStream(const mapping::PoseGraph &pose_graph, const std::vector< mapping::proto::TrajectoryBuilderOptionsWithSensorIds > &trajectory_builder_options, ProtoStreamWriterInterface *const writer)
const DataType & at(const IdType &id) const
int num_trajectory_builders() const override
std::string SubmapToProto(const SubmapId &submap_id, proto::SubmapQuery::Response *response) override
void Insert(const IdType &id, const DataType &data)
int GetNonNegativeInt(const std::string &key)
FixedFramePoseData FromProto(const proto::FixedFramePoseData &proto)
bool ReadNextSerializedData(mapping::proto::SerializedData *data)
proto::MapBuilderOptions CreateMapBuilderOptions(common::LuaParameterDictionary *const parameter_dictionary)
FakeThreadPool thread_pool_
Time FromUniversal(const int64 ticks)
virtual bool eof() const =0
proto::ProbabilityGridRangeDataInserterOptions2D options_
const proto::MapBuilderOptions options_
proto::PoseGraphOptions CreatePoseGraphOptions(common::LuaParameterDictionary *const parameter_dictionary)
void LoadState(io::ProtoStreamReaderInterface *reader, bool load_frozen_state) override
mapping::proto::PoseGraph & pose_graph()
std::unique_ptr< PoseGraph > pose_graph_
std::vector< std::unique_ptr< mapping::TrajectoryBuilderInterface > > trajectory_builders_
std::unique_ptr< TrajectoryBuilderInterface > CreateGlobalTrajectoryBuilder3D(std::unique_ptr< LocalTrajectoryBuilder3D > local_trajectory_builder, const int trajectory_id, mapping::PoseGraph3D *const pose_graph, const TrajectoryBuilderInterface::LocalSlamResultCallback &local_slam_result_callback)
std::unique_ptr< sensor::CollatorInterface > sensor_collator_
common::ThreadPool thread_pool_
const mapping::proto::AllTrajectoryBuilderOptions & all_trajectory_builder_options()
int AddTrajectoryBuilder(const std::set< SensorId > &expected_sensor_ids, const proto::TrajectoryBuilderOptions &trajectory_options, LocalSlamResultCallback local_slam_result_callback) override
int AddTrajectoryForDeserialization(const proto::TrajectoryBuilderOptionsWithSensorIds &options_with_sensor_ids_proto) override
std::unique_ptr< LuaParameterDictionary > GetDictionary(const std::string &key)
proto::MapLimits ToProto(const MapLimits &map_limits)
MapBuilder(const proto::MapBuilderOptions &options)
bool GetBool(const std::string &key)