Skip to content

Commit

Permalink
Wrap IO helpers to python
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Aug 25, 2024
1 parent 52f0844 commit 208b181
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
14 changes: 14 additions & 0 deletions python/core/py_io_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

namespace g2o {

namespace {
struct IoWrapper {};
} // namespace

void declareIOFormat(py::module& m) {
py::enum_<io::Format>(m, "IoFormat")
.value("G2O", io::Format::kG2O, "G2O's custom ASCII IO format")
.value("JSON", io::Format::kJson, "JSON IO format")
.value("BINARY", io::Format::kBinary, "G2O's custom binary format");

py::class_<io::FileFilter>(m, "IoFileFilter")
.def(py::init<std::string, io::Format>(), "filter"_a, "format"_a)
.def_readwrite("filter", &io::FileFilter::filter)
.def_readwrite("format", &io::FileFilter::format);

py::class_<IoWrapper>(m, "IoWrapper")
.def_static("to_string", &io::to_string)
.def_static("file_filter", &io::getFileFilter)
.def_static("format_for_file_extension", &io::formatForFileExtension);
}

} // end namespace g2o
4 changes: 0 additions & 4 deletions python/core/py_robust_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ namespace g2o {

void declareRobustKernel(py::module& m) {
py::class_<RobustKernel, std::shared_ptr<RobustKernel>>(m, "BaseRobustKernel")
//.def(py::init<>())
//.def(py::init<double>(),
// "delta"_a)
.def("robustify", &RobustKernel::robustify, "squared_error"_a,
"rho"_a) // (double, Vector3&) -> void
.def("set_delta", &RobustKernel::setDelta,
Expand All @@ -24,7 +21,6 @@ void declareRobustKernel(py::module& m) {
.def(py::init<double>(), "delta"_a = 1.)
.def(py::init<const RobustKernelPtr&, double>(), "kernel"_a,
"delta"_a = 1., py::keep_alive<1, 2>())
.def(py::init<double>(), "delta"_a)
.def("kernel", &RobustKernelScaleDelta::kernel) // -> RobustKernelPtr&
.def("set_kernel", &RobustKernelScaleDelta::setKernel, "ptr"_a,
py::keep_alive<1, 2>()) // const RobustKernelPtr& ->
Expand Down
12 changes: 8 additions & 4 deletions python/examples/simple_optimize.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python3

import argparse

import g2opy as g2o


def to_io_format(value: str) -> g2o.IoFormat:
return g2o.IoFormat.__members__[value.upper()]
format = g2o.IoWrapper.format_for_file_extension(value)
return format if format is not None else g2o.IoFormat.G2O


def main():
Expand All @@ -19,7 +22,7 @@ def main():
parser.add_argument(
"--output-format",
type=str,
choices=["g2o", "json", "xml", "binary"],
choices=["g2o", "json", "bin"],
default=g2o.IoFormat.G2O.name,
help="define the output format",
)
Expand Down Expand Up @@ -51,8 +54,9 @@ def main():
print(f"Final chi2: {optimizer.chi2()}")

if len(args.output) > 0:
print(f"Saving to {args.output}")
optimizer.save(args.output, to_io_format(args.output_format))
io_output_format = to_io_format(args.output_format)
print(f"Saving to {args.output} as {g2o.IoWrapper.to_string(io_output_format)}")
optimizer.save(args.output, io_output_format)


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

0 comments on commit 208b181

Please sign in to comment.