6 #include <opencv2/core.hpp>
8 #include <boost/python.hpp>
9 #include <boost/python/stl_iterator.hpp>
10 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
15 struct iterable_converter
19 template <
typename Container>
23 boost::python::converter::registry::push_back(
24 &iterable_converter::convertible,
25 &iterable_converter::construct_from_python<Container>,
26 boost::python::type_id<Container>());
33 boost::python::list to_python(std::vector<T> v)
35 typename std::vector<T>::iterator iter;
36 boost::python::list list;
37 for(iter = v.begin();iter != v.end(); ++iter)
45 static void* convertible(PyObject*
object)
47 return PyObject_GetIter(
object) ?
object : NULL;
57 template <
typename Container>
58 static void construct_from_python(
60 boost::python::converter::rvalue_from_python_stage1_data* data)
62 namespace python = boost::python;
65 python::handle<> handle(python::borrowed(
object));
69 typedef python::converter::rvalue_from_python_storage<Container>
71 void* storage =
reinterpret_cast<storage_type*
>(data)->storage.bytes;
73 typedef python::stl_input_iterator<typename Container::value_type>
80 new (storage) Container(
81 iterator(python::object(handle)),
83 data->convertible = storage;
89 template <
typename T1,
typename T2>
90 struct std_pair_to_tuple
92 static PyObject* convert(std::pair<T1, T2>
const& p)
94 return boost::python::incref(
95 boost::python::make_tuple(p.first, p.second).ptr());
97 static PyTypeObject
const *get_pytype () {
return &PyTuple_Type; }
101 template <
typename T1,
typename T2>
102 struct std_pair_to_python_converter
104 std_pair_to_python_converter()
106 boost::python::to_python_converter<
108 std_pair_to_tuple<T1, T2>,
114 struct cv_vec3b_to_python_converter
116 static PyObject* convert(cv::Vec3b
const& vec)
118 boost::python::list l;
119 l.append(boost::python::object(vec[0]));
120 l.append(boost::python::object(vec[1]));
121 l.append(boost::python::object(vec[2]));
122 return boost::python::incref(l.ptr());
128 #define MAKE_VECTOR_WRAPPER( CPP_NAME , PYTHON_NAME ) \
129 boost::python::class_< CPP_NAME >( #PYTHON_NAME ) \
130 .def(boost::python::vector_indexing_suite< CPP_NAME >()) \
137 typedef typename T::value_type V;
139 static V&
get( T& x,
int i )
141 if( i < 0 ) { i += x.size(); }
142 if( i >= 0 && (
unsigned)i < x.size() ) {
return x[i]; }
143 else { ;
return x[0]; }
145 static void set( T& x,
int i, V& v )
147 if( i < 0 ) { i += x.size(); }
148 if( i >= 0 && (
unsigned)i < x.size() ) { x[i] = v; }
151 static void del( T& x,
int i )
153 if( i < 0 ) { i += x.size(); }
154 if( i >= 0 && (
unsigned)i < x.size() ) { x.erase( x.begin() + i ); }
157 static void add( T& x, V& v )
162 static bool in( T& x, V& v ) {
163 return find_eq( x.begin, x.end, v ) != x.end();
165 static int index( T& x, V& v ) {
167 for(
typename T::const_iterator it = x.begin; it != x.end(); ++it, ++i )
168 if( *it == v ) {
return i; }
174 #define MAKE_VECTOR_WRAPPER_LIMITED( CPP_NAME , PYTHON_NAME ) \
175 boost::python::class_< CPP_NAME >( #PYTHON_NAME ) \
176 .def("__len__" , & CPP_NAME::size) \
177 .def("clear" , & CPP_NAME::clear) \
178 .def("append" , & vector_item< CPP_NAME >::add \
179 , boost::python::with_custodian_and_ward<1, 2>()) \
180 .def("__setitem__" , & vector_item< CPP_NAME >::set \
181 , boost::python::with_custodian_and_ward<1, 2>()) \
182 .def("__getitem__" , & vector_item< CPP_NAME >::get \
183 , boost::python::return_value_policy<boost::python::copy_non_const_reference>()) \
184 .def("__delitem__" , & vector_item< CPP_NAME >::del) \
185 .def("__iter__" , boost::python::iterator< CPP_NAME >() ) \
188 #endif // PYTHON_UTILS_H