00001 /* 00002 * Copyright 2018 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "cartographer/io/proto_stream_deserializer.h" 00018 00019 #include "cartographer/io/internal/mapping_state_serialization.h" 00020 #include "cartographer/io/proto_stream.h" 00021 #include "glog/logging.h" 00022 00023 namespace cartographer { 00024 namespace io { 00025 namespace { 00026 mapping::proto::SerializationHeader ReadHeaderOrDie( 00027 ProtoStreamReaderInterface* const reader) { 00028 mapping::proto::SerializationHeader header; 00029 CHECK(reader->ReadProto(&header)) << "Failed to read SerializationHeader."; 00030 return header; 00031 } 00032 00033 bool IsVersionSupported(const mapping::proto::SerializationHeader& header) { 00034 return header.format_version() == kMappingStateSerializationFormatVersion || 00035 header.format_version() == kFormatVersionWithoutSubmapHistograms; 00036 } 00037 00038 } // namespace 00039 00040 mapping::proto::PoseGraph DeserializePoseGraphFromFile( 00041 const std::string& file_name) { 00042 ProtoStreamReader reader(file_name); 00043 ProtoStreamDeserializer deserializer(&reader); 00044 return deserializer.pose_graph(); 00045 } 00046 00047 ProtoStreamDeserializer::ProtoStreamDeserializer( 00048 ProtoStreamReaderInterface* const reader) 00049 : reader_(reader), header_(ReadHeaderOrDie(reader)) { 00050 CHECK(IsVersionSupported(header_)) << "Unsupported serialization format \"" 00051 << header_.format_version() << "\""; 00052 00053 CHECK(ReadNextSerializedData(&pose_graph_)) 00054 << "Serialized stream misses PoseGraph."; 00055 CHECK(pose_graph_.has_pose_graph()) 00056 << "Serialized stream order corrupt. Expecting `PoseGraph` after " 00057 "`SerializationHeader`, but got field tag " 00058 << pose_graph_.data_case(); 00059 00060 CHECK(ReadNextSerializedData(&all_trajectory_builder_options_)) 00061 << "Serialized stream misses `AllTrajectoryBuilderOptions`."; 00062 CHECK(all_trajectory_builder_options_.has_all_trajectory_builder_options()) 00063 << "Serialized stream order corrupt. Expecting " 00064 "`AllTrajectoryBuilderOptions` after " 00065 "PoseGraph, got field tag " 00066 << all_trajectory_builder_options_.data_case(); 00067 00068 CHECK_EQ(pose_graph_.pose_graph().trajectory_size(), 00069 all_trajectory_builder_options_.all_trajectory_builder_options() 00070 .options_with_sensor_ids_size()); 00071 } 00072 00073 bool ProtoStreamDeserializer::ReadNextSerializedData( 00074 mapping::proto::SerializedData* data) { 00075 return reader_->ReadProto(data); 00076 } 00077 00078 } // namespace io 00079 } // namespace cartographer