Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pymem fileformat #753

Merged
merged 52 commits into from
Mar 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
3c04860
Add draft document for file format
RainerKuemmerle Dec 10, 2023
b23ce9d
WIP Graph IO Abstraction
RainerKuemmerle Dec 16, 2023
72f8357
Refine draft of the file format
RainerKuemmerle Dec 17, 2023
56233d3
WIP reading in from g2o format
RainerKuemmerle Dec 17, 2023
ef1f219
Add save function for g2o format
RainerKuemmerle Dec 20, 2023
2c49cd8
Start to add test for save/load in g2o format
RainerKuemmerle Dec 21, 2023
efbc918
Implement save/load of OptimizableGraph with AbstractGraph
RainerKuemmerle Dec 23, 2023
85ce55e
Add JSON by using cereal library
RainerKuemmerle Dec 24, 2023
7aae11a
Add file format to save/load API
RainerKuemmerle Dec 24, 2023
1eabf2a
Extend unit tests
RainerKuemmerle Dec 24, 2023
257a4f1
Update python wrapper API
RainerKuemmerle Dec 25, 2023
9d32a9e
Handle cereal's exception
RainerKuemmerle Dec 25, 2023
72102c1
Add XML and binary IO formats
RainerKuemmerle Dec 25, 2023
6611f5f
Update README and CI to include cereal dependency
RainerKuemmerle Dec 25, 2023
8cde6f1
Try workaround for broken cereal cmake
RainerKuemmerle Dec 25, 2023
90e2d15
Try to fix MSVC
RainerKuemmerle Dec 25, 2023
950bd10
Support renamed types when loading graph
RainerKuemmerle Dec 27, 2023
0f2d579
Simplify IO code with cereal by templated functions
RainerKuemmerle Dec 27, 2023
8847654
Drop read/write API for Vertex and Edges
RainerKuemmerle Dec 27, 2023
99d37de
Fix CMakeLists
RainerKuemmerle Dec 27, 2023
850a371
Fix tests
RainerKuemmerle Dec 27, 2023
c75397b
Simplify AbstractGraph's API
RainerKuemmerle Dec 27, 2023
b8871ef
Add test load/save with data in the graph
RainerKuemmerle Dec 27, 2023
7b65d27
Cleaning up deprecated IO helpers
RainerKuemmerle Dec 28, 2023
c356477
Fix saving of dynamically sized edge
RainerKuemmerle Dec 28, 2023
2cc8139
Cleaning API
RainerKuemmerle Dec 28, 2023
d20506f
Support dynamically sized vertex in G2O format
RainerKuemmerle Dec 29, 2023
eb5a848
Add tests for BaseVertex's methods
RainerKuemmerle Dec 29, 2023
4ed408b
Re-add the IO tests for 2D SLAM
RainerKuemmerle Dec 31, 2023
a3ea2d7
Add IO tests for SBA types
RainerKuemmerle Jan 7, 2024
56a3118
Fix SBA test names
RainerKuemmerle Jan 7, 2024
c0df08c
Rework ParameterCamera in SLAM3D
RainerKuemmerle Jan 20, 2024
5e33a32
Drop aux include files
RainerKuemmerle Jan 21, 2024
ff91c61
Fix writing param IDs in g2o format
RainerKuemmerle Jan 21, 2024
3ababa2
Extend typed IO tests to support optional parameters
RainerKuemmerle Jan 21, 2024
08f3789
Implement type based tests for Jacobian
RainerKuemmerle Jan 28, 2024
6343821
Fix compile error in tests
RainerKuemmerle Jan 28, 2024
4fc6b3d
Use more flexible setInformation
RainerKuemmerle Jan 28, 2024
def728b
Fix linkage of ExtractTupleHead
RainerKuemmerle Jan 28, 2024
70f0b1c
Fix compilation of Python wrapper
RainerKuemmerle Jan 28, 2024
9788518
Use numeric Jacobian for EdgeSE3Expmap as workaround
RainerKuemmerle Jan 28, 2024
8305772
Relax basic Jacobian Epsilon function
RainerKuemmerle Jan 28, 2024
275b54f
Compile own test_main
RainerKuemmerle Jan 28, 2024
c9eaa02
Update python README
RainerKuemmerle Jan 28, 2024
9669511
Disable one getPureFilename test on Windows
RainerKuemmerle Jan 28, 2024
3039bc9
Disable Jacobian tests on MSVC
RainerKuemmerle Jan 28, 2024
4d32225
Fix typed_basic_tests.h on Windows
RainerKuemmerle Jan 29, 2024
9d78f06
Turn edges into fixed size
RainerKuemmerle Jan 30, 2024
c3c61f9
Avoid void* in pointer arithmetic
RainerKuemmerle Feb 1, 2024
4ecec8b
Removed obsolete Makefile
RainerKuemmerle Feb 3, 2024
9d5bd81
Install g2opy to g2o
RainerKuemmerle Feb 3, 2024
06c57fe
Fix registration of types in g2opy
RainerKuemmerle Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Start to add test for save/load in g2o format
RainerKuemmerle committed Mar 13, 2024
commit 2c49cd8a96b364e3f8a2207f98a5358b1d0671bc
1 change: 1 addition & 0 deletions g2o/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ robust_kernel_factory.cpp robust_kernel_factory.h
io_helper.h
g2o_core_api.h
# IO
abstract_graph.cpp abstract_graph.h
io/io_g2o.cpp io/io_g2o.h
)

3 changes: 2 additions & 1 deletion g2o/core/abstract_graph.cpp
Original file line number Diff line number Diff line change
@@ -43,7 +43,8 @@ std::unique_ptr<g2o::IoInterface> allocate(g2o::AbstractGraph::Format format) {
case g2o::AbstractGraph::Format::kXML:
break;
}
G2O_CRITICAL("Failed to create graph loader interface for format {}", format);
G2O_CRITICAL("Failed to create graph loader interface for format {}",
static_cast<int>(format));
return nullptr;
}
} // namespace
33 changes: 29 additions & 4 deletions g2o/core/abstract_graph.h
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@

#include <istream>
#include <string>
#include <utility>
#include <vector>

namespace g2o {
@@ -49,29 +50,53 @@ class AbstractGraph {
struct AbstractData {
std::string tag; ///< the tag of this data
std::string data; ///< the serialized data as a string
AbstractData() = default;
AbstractData(std::string tag, std::string data)
: tag(std::move(tag)), data(std::move(data)) {}
};

struct AbstractParameter {
std::string tag; ///< the tag of this parameter
int id; ///< its ID
std::vector<double> value; ///< its value as a vector
AbstractParameter() = default;
AbstractParameter(std::string tag, int id, std::vector<double> value)
: tag(std::move(tag)), id(id), value(std::move(value)) {}
};

struct AbstractGraphElement {
std::string tag;
std::vector<AbstractData> data; ///< the associated data
AbstractGraphElement() = default;
AbstractGraphElement(std::string tag, std::vector<AbstractData> data)
: tag(std::move(tag)), data(std::move(data)) {}
};

struct AbstractVertex : public AbstractGraphElement {
int id; ///< its ID
std::vector<double> estimate; ///< the estimate as a vector
AbstractVertex() = default;
AbstractVertex(std::string tag, int id, std::vector<double> estimate,
std::vector<AbstractData> data = {})
: AbstractGraphElement(std::move(tag), std::move(data)),
id(id),
estimate(std::move(estimate)) {}
};

struct AbstractEdge : public AbstractGraphElement {
std::vector<int> ids; ///< the ids of the vertices connected by this edge
std::vector<double> measurement; ///< the measurement as a vector
std::vector<double>
information; ///< upper triangular part of the information matrix
AbstractEdge() = default;
AbstractEdge(std::string tag, std::vector<int> ids,
std::vector<double> measurement,
std::vector<double> information,
std::vector<AbstractData> data = {})
: AbstractGraphElement(std::move(tag), std::move(data)),
ids(std::move(ids)),
measurement(std::move(measurement)),
information(std::move(information)) {}
};

//! Possible formats for loading and saving
@@ -84,10 +109,10 @@ class AbstractGraph {
Format format = AbstractGraph::Format::kG2O);
bool load(std::istream& input, Format format = AbstractGraph::Format::kG2O);

bool save(const std::string& filename,
Format format = AbstractGraph::Format::kG2O) const;
bool save(std::ostream& output,
Format format = AbstractGraph::Format::kG2O) const;
[[nodiscard]] bool save(const std::string& filename,
Format format = AbstractGraph::Format::kG2O) const;
[[nodiscard]] bool save(std::ostream& output,
Format format = AbstractGraph::Format::kG2O) const;

std::vector<int>& fixed() { return fixed_; };
[[nodiscard]] const std::vector<int>& fixed() const { return fixed_; };
2 changes: 1 addition & 1 deletion g2o/core/io/io_g2o.cpp
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ AbstractGraph IoG2O::load(std::istream& input) {

bool IoG2O::save(std::ostream& output, const AbstractGraph& graph) {
if (!graph.fixed().empty()) {
output << "FIX" << graph.fixed() << '\n';
output << "FIX " << graph.fixed() << '\n';
}

for (const auto& param : graph.parameters()) {
4 changes: 3 additions & 1 deletion unit_test/general/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
add_executable(unittest_general
auto_diff.cpp
batch_statistics.cpp
graph_io.cpp
graph_operations.cpp
clear_and_redo.cpp
base_fixed_sized_edge.cpp
robust_kernel_tests.cpp
sparse_block_matrix.cpp
type_traits_tests.cpp
)
target_link_libraries(unittest_general unittest_helper types_slam3d types_slam2d)
target_link_libraries(unittest_general unittest_helper)
target_link_libraries(unittest_general types_slam3d types_slam2d types_data)
create_test(unittest_general)
11 changes: 0 additions & 11 deletions unit_test/general/auto_diff.cpp
Original file line number Diff line number Diff line change
@@ -165,12 +165,6 @@ class AutoDifferentiation : public ::testing::Test {
TEST_F(AutoDifferentiation, ComputesSomething) {
testEdge_.linearizeOplus(jacobianWorkspace_);

#if 0
std::cerr << PVAR(testEdge.jacobianOplusXn<0>()) << std::endl;
std::cerr << PVAR(testEdge.jacobianOplusXn<1>()) << std::endl;
std::cerr << PVAR(testEdge.jacobianOplusXn<2>()) << std::endl;
#endif

ASSERT_FALSE(testEdge_.jacobianOplusXn<0>().array().isNaN().any())
<< "Jacobian contains NaN";
ASSERT_FALSE(testEdge_.jacobianOplusXn<1>().array().isNaN().any())
@@ -242,11 +236,6 @@ class AutoDifferentiationEdgeSE2 : public ::testing::Test {
TEST_F(AutoDifferentiationEdgeSE2, AdComputesSomething) {
testEdgeAd_.linearizeOplus(jacobianWorkspaceAd_);

#if 0
std::cerr << PVAR(testEdgeAd.jacobianOplusXn<0>()) << std::endl;
std::cerr << PVAR(testEdgeAd.jacobianOplusXn<1>()) << std::endl;
#endif

ASSERT_FALSE(testEdgeAd_.jacobianOplusXn<0>().array().isNaN().any())
<< "Jacobian contains NaN";
ASSERT_FALSE(testEdgeAd_.jacobianOplusXn<1>().array().isNaN().any())
104 changes: 104 additions & 0 deletions unit_test/general/graph_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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.

#include <sstream>

#include "g2o/core/abstract_graph.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace testing; // NOLINT

MATCHER(ParamEqual, "") {
const auto& that = std::get<0>(arg);
const auto& expected = std::get<1>(arg);
return ExplainMatchResult(
AllOf(Field("tag", &g2o::AbstractGraph::AbstractParameter::tag,
Eq(expected.tag)),
Field("id", &g2o::AbstractGraph::AbstractParameter::id,
Eq(expected.id)),
Field("value", &g2o::AbstractGraph::AbstractParameter::value,
ElementsAreArray(expected.value))),
that, result_listener);
}

/**
* @brief Test fixture for IO with the abstract graph.
*/
class AbstractGraphIO : public TestWithParam<g2o::AbstractGraph::Format> {
protected:
void SetUp() override {
abstract_graph_.fixed() = {1};

abstract_graph_.parameters().emplace_back("PARAMS_SE2OFFSET", 42,
std::vector<double>{12., 13.});

abstract_graph_.vertices().emplace_back("VERTEX_SE2", 1,
std::vector<double>{1., 2., 3.});
abstract_graph_.vertices().emplace_back("VERTEX_SE2", 2,
std::vector<double>{2., 3., 4.});
abstract_graph_.vertices().emplace_back(
"VERTEX_SE2", 3, std::vector<double>{5., 6., 7.},
std::vector<g2o::AbstractGraph::AbstractData>{
{"VERTEX_TAG", "fancy data"}});

abstract_graph_.edges().emplace_back(
"EDGE_SE2", std::vector<int>{1, 2}, std::vector<double>{0.1, 0.2, 0.3},
std::vector<double>{1., 2., 3., 4., 5., 6.},
std::vector<g2o::AbstractGraph::AbstractData>{
{"VERTEX_TAG", "more fancy data"}});
abstract_graph_.edges().emplace_back(
"EDGE_SE2", std::vector<int>{2, 3}, std::vector<double>{0.4, 0.5, 0.6},
std::vector<double>{1.1, 2.1, 3.1, 4.1, 5.1, 6.1});
}
g2o::AbstractGraph abstract_graph_;
};

TEST_P(AbstractGraphIO, SaveAndLoad) {
g2o::AbstractGraph::Format format = GetParam();
g2o::AbstractGraph load_save_graph(abstract_graph_);

std::stringstream buffer;
bool save_result = load_save_graph.save(buffer, format);
ASSERT_THAT(save_result, IsTrue());

load_save_graph.clear();
EXPECT_THAT(load_save_graph.fixed(), IsEmpty());
EXPECT_THAT(load_save_graph.parameters(), IsEmpty());
EXPECT_THAT(load_save_graph.vertices(), IsEmpty());
EXPECT_THAT(load_save_graph.edges(), IsEmpty());

bool load_status = load_save_graph.load(buffer, format);
ASSERT_THAT(load_status, IsTrue());

EXPECT_THAT(load_save_graph.fixed(),
ElementsAreArray(abstract_graph_.fixed()));
EXPECT_THAT(load_save_graph.parameters(),
Pointwise(ParamEqual(), abstract_graph_.parameters()));
}

INSTANTIATE_TEST_SUITE_P(AbstractGraph, AbstractGraphIO,
Values(g2o::AbstractGraph::Format::kG2O));