Skip to content
Snippets Groups Projects
Commit cdf8cccb authored by Marco Biasini's avatar Marco Biasini
Browse files

added converter to initialize std::vectors from iterable

In Python, it's now possible to do:

a=geom.Vec3(1,0,0)
b=geom.Vec3(0,1,0)
c=geom.Vec3(0,0,1)
print geom.Vec3List((a,b,c,))
print geom.Vec3List([a,b,c,])

def f(*args):
  for a in args:
    yield a

print geom.Vec3List(f(a,b,c))
parent 0418e3e3
No related branches found
No related tags found
No related merge requests found
#ifndef OST_EXPORT_HELPER_VECTOR_HH #ifndef OST_EXPORT_HELPER_VECTOR_HH
#define OST_EXPORT_HELPER_VECTOR_HH #define OST_EXPORT_HELPER_VECTOR_HH
# include <boost/python/def_visitor.hpp> #include <boost/python/def_visitor.hpp>
#include <boost/python/suite/indexing/container_utils.hpp>
/* /*
Author: Marco Biasini Author: Marco Biasini
*/ */
namespace ost { namespace ost {
namespace bp=boost::python;
template <typename Container> template <typename Container>
class VectorAdditions : class VectorAdditions :
public boost::python::def_visitor<VectorAdditions<Container> > { public bp::def_visitor<VectorAdditions<Container> > {
public: public:
typedef typename Container::value_type value_type;
typedef Container container_type;
template <class Class> template <class Class>
void visit(Class& cl) const void visit(Class& cl) const
{ {
cl cl
.def("__str__", &to_string) .def("__str__", &to_string)
.def("__init__", make_constructor(&from_iter))
; ;
} }
private: private:
static boost::shared_ptr<Container> from_iter(const bp::object& obj)
{
boost::shared_ptr<Container> c(new Container);
bp::container_utils::extend_container(*c.get(), obj);
return c;
}
static std::string to_string(Container& cl) static std::string to_string(Container& cl)
{ {
std::stringstream ss; std::stringstream ss;
......
...@@ -31,6 +31,13 @@ geom::Vec3 NormalizeV3(const geom::Vec3& v) { ...@@ -31,6 +31,13 @@ geom::Vec3 NormalizeV3(const geom::Vec3& v) {
return geom::Normalize(v); return geom::Normalize(v);
} }
void vec_test(const geom::Vec3List& v)
{
for (size_t i=0; i<v.size(); ++i) {
std::cout << i << v[i] << std::endl;
}
}
void export_Vec3() void export_Vec3()
{ {
using namespace geom; using namespace geom;
...@@ -75,4 +82,5 @@ void export_Vec3() ...@@ -75,4 +82,5 @@ void export_Vec3()
.add_property("inertia", &Vec3List::GetInertia) .add_property("inertia", &Vec3List::GetInertia)
.add_property("principal_axes", &Vec3List::GetPrincipalAxes) .add_property("principal_axes", &Vec3List::GetPrincipalAxes)
; ;
def("vec_test", &vec_test);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment