From 818db173929f04a047121a394eab1f513378d5de Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Mon, 23 Nov 2015 15:41:17 +0100 Subject: [PATCH] Wrap symbol to python --- python/handwritten/exportgtsam.cpp | 5 ++ python/handwritten/inference/Symbol.cpp | 66 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 python/handwritten/inference/Symbol.cpp diff --git a/python/handwritten/exportgtsam.cpp b/python/handwritten/exportgtsam.cpp index 6afc1f85e..a0ba634e2 100644 --- a/python/handwritten/exportgtsam.cpp +++ b/python/handwritten/exportgtsam.cpp @@ -36,6 +36,9 @@ void exportPose2(); void exportPose3(); void exportPinholeCamera(); +// Inference +void exportSymbol(); + // Linear void exportNoiseModels(); @@ -70,6 +73,8 @@ BOOST_PYTHON_MODULE(libgtsam_python){ exportPose3(); exportPinholeCamera(); + exportSymbol(); + exportNoiseModels(); exportValues(); diff --git a/python/handwritten/inference/Symbol.cpp b/python/handwritten/inference/Symbol.cpp new file mode 100644 index 000000000..ace26c67d --- /dev/null +++ b/python/handwritten/inference/Symbol.cpp @@ -0,0 +1,66 @@ + /* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @brief wraps Symbol class to python + * @author Ellon Paiva Mendes (LAAS-CNRS) + **/ + +#include +#include + +#define NO_IMPORT_ARRAY +#include + +#include "gtsam/inference/Symbol.h" + +using namespace boost::python; +using namespace gtsam; + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(print_overloads, Symbol::print, 0, 1) +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(equals_overloads, Symbol::equals, 1, 2) + +// Helper function to allow building a symbol from a python string and a index. +static boost::shared_ptr makeSymbol(const std::string &str, size_t j) +{ + if(str.size() > 1) + throw std::runtime_error("string argument must have one character only"); + + return boost::make_shared(str.at(0),j); +} + +// Helper function to print the symbol as "char-and-index" in python +std::string selfToString(const Symbol & self) +{ + return (std::string)self; +} + +void exportSymbol(){ + +class_ >("Symbol") + .def(init<>()) + .def(init()) + .def("__init__", make_constructor(makeSymbol)) + .def(init()) + .def("print", &Symbol::print, print_overloads(args("s"))) + .def("equals", &Symbol::equals, equals_overloads(args("q","tol"))) + .def("key", &Symbol::key) + .def("chr", &Symbol::chr) + .def("index", &Symbol::index) + .def(self < self) + .def(self == self) + .def(self == other()) + .def(self != self) + .def(self != other()) + .def("__repr__", &selfToString) +; + +} \ No newline at end of file