Skip to content

Commit

Permalink
Use CBOR as binary format
Browse files Browse the repository at this point in the history
Dropping cereal library which seems not well maintained.
  • Loading branch information
RainerKuemmerle committed Aug 18, 2024
1 parent e5f73fc commit a99d53c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 214 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${g2o_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${g2o_C_FLAGS}")

find_package(Eigen3 3.3 REQUIRED)
find_package(cereal)
find_package(nlohmann_json 3.2.0)

# Generate config.h
set(G2O_OPENGL_FOUND ${OPENGL_FOUND})
set(G2O_HAVE_CHOLMOD ${CHOLMOD_FOUND})
set(G2O_HAVE_CSPARSE ${G2O_USE_CSPARSE})
if (TARGET cereal::cereal)
set(G2O_HAVE_CEREAL 1)
else()
set(G2O_HAVE_CEREAL 0)
endif()
if (TARGET nlohmann_json::nlohmann_json)
set(G2O_HAVE_JSON 1)
else()
Expand Down
1 change: 0 additions & 1 deletion config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#cmakedefine G2O_SHARED_LIBS 1
#cmakedefine G2O_LGPL_SHARED_LIBS 1

#cmakedefine G2O_HAVE_CEREAL 1
#cmakedefine G2O_HAVE_JSON 1

// available sparse matrix libraries
Expand Down
31 changes: 20 additions & 11 deletions g2o/core/io/io_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,35 @@

#include "g2o/config.h"
#include "g2o/core/abstract_graph.h"

#ifdef G2O_HAVE_CEREAL
#include <cereal/archives/portable_binary.hpp>
#include <cereal/cereal.hpp>

#include "io_wrapper_cereal.h" // IWYU pragma: keep
#else
#include "g2o/stuff/logger.h"
#endif // HAVE CEREAL

#ifdef G2O_HAVE_JSON
#include "io_wrapper_json.h"
#endif // HAVE_JSON

namespace g2o {

#ifdef G2O_HAVE_CEREAL
#ifdef G2O_HAVE_JSON

std::optional<AbstractGraph> IoBinary::load(std::istream& input) {
return io::load<cereal::PortableBinaryInputArchive>(input, "BINARY");
try {
return json::fromJson(nlohmann::json::from_cbor(input));
} catch (const std::exception& e) {
G2O_ERROR("Exception while loading: {}", e.what());
}
return std::nullopt;
}

bool IoBinary::save(std::ostream& output, const AbstractGraph& graph) {
return io::save<cereal::PortableBinaryOutputArchive>(output, graph, "BINARY");
try {
std::vector<std::uint8_t> binary =
nlohmann::json::to_cbor(json::toJson(graph));
output.write(reinterpret_cast<char*>(binary.data()), binary.size());
return output.good();
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
}
return false;
}

#else
Expand Down
34 changes: 6 additions & 28 deletions g2o/core/io/io_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,28 @@

#ifdef G2O_HAVE_JSON
#include <nlohmann/json.hpp>
#endif // HAVE CEREAL

#include "io_wrapper_json.h"
#endif // HAVE_JSON

namespace g2o {

#ifdef G2O_HAVE_JSON

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractParameter, tag, id,
value);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractData, tag, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractVertex, tag, id,
estimate, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractEdge, tag, ids,
param_ids, measurement, information, data);

std::optional<AbstractGraph> IoJson::load(std::istream& input) {
nlohmann::json json;
try {
nlohmann::json json;
input >> json;
const nlohmann::json& json_graph = json["graph"];
AbstractGraph graph;
graph.fixed() = json_graph["fixed"].get<std::vector<int>>();
graph.parameters() =
json_graph["params"]
.get<std::vector<AbstractGraph::AbstractParameter>>();
graph.vertices() = json_graph["vertices"]
.get<std::vector<AbstractGraph::AbstractVertex>>();
graph.edges() =
json_graph["edges"].get<std::vector<AbstractGraph::AbstractEdge>>();
return graph;
return json::fromJson(json);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
}
return std::nullopt;
}

bool IoJson::save(std::ostream& output, const AbstractGraph& graph) {
nlohmann::json json;
try {
nlohmann::json& json_graph = json["graph"];
json_graph["fixed"] = graph.fixed();
json_graph["params"] = graph.parameters();
json_graph["vertices"] = graph.vertices();
json_graph["edges"] = graph.edges();
output << json;
output << json::toJson(graph);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
return false;
Expand Down
165 changes: 0 additions & 165 deletions g2o/core/io/io_wrapper_cereal.h

This file was deleted.

90 changes: 90 additions & 0 deletions g2o/core/io/io_wrapper_json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// g2o - General Graph Optimization
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef G2O_CORE_IO_WRAPPER_JSON_H
#define G2O_CORE_IO_WRAPPER_JSON_H

#include "g2o/config.h"

#ifdef G2O_HAVE_JSON
#include <nlohmann/json.hpp>

#include "g2o/core/abstract_graph.h"

namespace g2o {

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractParameter, tag, id,
value);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractData, tag, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractVertex, tag, id,
estimate, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractEdge, tag, ids,
param_ids, measurement, information, data);

namespace json {

/**
* @brief Versions of the IO API for the graph using cereal.
*/
enum class IoVersions {
kGraph = 0,
kData = 0,
kParameter = 0,
kGraphElement = 0,
kVertex = 0,
kEdge = 0,
};

inline AbstractGraph fromJson(const nlohmann::json& json) {
const nlohmann::json& json_graph = json["graph"];
AbstractGraph graph;
graph.fixed() = json_graph["fixed"].get<std::vector<int>>();
graph.parameters() =
json_graph["params"].get<std::vector<AbstractGraph::AbstractParameter>>();
graph.vertices() =
json_graph["vertices"].get<std::vector<AbstractGraph::AbstractVertex>>();
graph.edges() =
json_graph["edges"].get<std::vector<AbstractGraph::AbstractEdge>>();
return graph;
}

inline nlohmann::json toJson(const AbstractGraph& graph) {
nlohmann::json json;
nlohmann::json& json_graph = json["graph"];
json_graph["fixed"] = graph.fixed();
json_graph["params"] = graph.parameters();
json_graph["vertices"] = graph.vertices();
json_graph["edges"] = graph.edges();
return json;
}

} // namespace json

} // namespace g2o

#endif // HAVE_JSON

#endif
3 changes: 2 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[ ] drop cereal (replace with json directly, drop xml)
[x] drop cereal (replace with json directly, drop xml)
[ ] install json on Windows CI
[ ] wrap io_format.h to python
[x] wrap simulator into library
[x] add config types for simulation
[ ] add tests for simulator
Expand Down
Loading

0 comments on commit a99d53c

Please sign in to comment.