Wrapping types with boost.python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping types with boost.python

Robert Parcus
Hello all, I'm new here :)

I'm a CS student doing my internship and I'm working on wrapping the glm library (http://glm.g-truc.net/) to export it to python. (eg. I have a lot of time to do it, but I'm learning while I'm doing it)
It's an open source library and the results (if any) of my work will go to the same field.

I've spent the last few weeks familiarising with boost.python and I've spent a large amount of time looking for and reading tutorials and examples but now I feel like I have to ask you for some help, if that's ok.

glm is a header only library and it has many types (mostly vectors(n), matrices(n,m)...) and many many functions to play with them. It also makes heavy use of templates. 

While exposing the functions has been pretty easy, I was not able yet to expose a single type. (I have been exposing the functions and specializing them only with int, double, etc types not the vectors and matrices, which are the most important types for me actually... I did it as a proof of concept, and because I'm stuck in this "exposing types" problem since a long time...)

As an example, on line 38 of this code http://glm.g-truc.net/api-0.9.1/a00127_source.html is the definition of vec2. All glm types are pretty similar to this and if I could have some insights on how to wrap something like this the rest would be almost "not very hard" for me to do.

Thanks in advance,

Robert

ps: I also checked py++ in the beginning of my researches. I couldn't make it work at the time, and I'm not even sure a library like glm could be easily wrapped using it. Any advices about that as well?

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrapping types with boost.python

Stefan Seefeld-2
Hi Robert,

On 2011-07-05 09:01, Robert Parcus wrote:
>
> While exposing the functions has been pretty easy, I was not able yet
> to expose a single type. (I have been exposing the functions and
> specializing them only with int, double, etc types not the vectors and
> matrices, which are the most important types for me actually... I did
> it as a proof of concept, and because I'm stuck in this "exposing
> types" problem since a long time...)

Could you elaborate on what problems you have with exposing types ? What
exactly did you try that didn't work ?

>
> As an example, on line 38 of this code
> http://glm.g-truc.net/api-0.9.1/a00127_source.html is the definition
> of vec2. All glm types are pretty similar to this and if I could have
> some insights on how to wrap something like this the rest would be
> almost "not very hard" for me to do.

tvec2 itself isn't a type. You need to instantiate it to get a type that
you can expose to Python. Then the normal rules should work just fine:

   class_<tvec2<float> > f_tvec2("fvec2");
   float (tvec2<float>::*swizzle_1)(comp) = &tvec2<float>::swizzle;
   f_tvec2.def("swizzle", swizzle_1);

I assume you'd like to do this for many different value-types, so it
might be best to wrap this in a function template:

template <typename T>
void define_vec2(char const *name)
{
   typedef tvec2<T> vec2_type;
   class_<vec2_type> vec2(name);
   float (vec2_type::*swizzle_1)(comp) = &vec2_type::swizzle;
   vec2.def("swizzle", swizzle_1);
   //...
}

and then call that function for all the value-types you want:

   define_vec2<float>("fvec2");
   define_vec2<int>("ivec2");
   //...

HTH,
         Stefan

--

       ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig