Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyBind - Overloaded functions

Firstly, my thanks to all of you for trying to resolve this doubt of mine. I am working on converting a minimal C++ project to be used in Python. The real reason behind this effort is for speed.

I came across PyBind and was quite surprised at its capabilities and also the amount of documentation they have provided. Right now there is something stopping the work because I have no idea on how to do it. Consider the below code in the file "MySource.hpp" and can you kindly tell me how a binding can be done?

    struct Point3D  
    {
    public:
        double x, y, z;
        CPoint3D();
        CPoint3D(double x, double y, double z); 
        inline double Len() const;
        inline void Normalize();
    };

    Point3D VectorCross(const Point3D& pt1, const Point3D& pt2, const Point3D& pt3);
    void VectorCross(const float* u, const float* v, float * n);

I was able to define a binding for Point3D as a class and certain of its member functions. But I don't have a clue about how to do the binding for the overloaded method "VectorCross". It has two methods with one accepting instances of Point3D, and another one accepting pointers to float arrays.

The binding I wrote so far is shown below

PYBIND11_MODULE(mymodule, m)
{
    py::class_<Point3D> point3d(m, "Point3D");
    point3d.def_readwrite("x", &CPoint3D::x);
    point3d.def_readwrite("y", &CPoint3D::y);
    point3d.def_readwrite("z", &CPoint3D::z);
    point3d.def(py::init<>());
    point3d.def(py::init<double , double , double >());
    point3d.def("Len", &CPoint3D::Len);
    point3d.def("Normalize", &CPoint3D::Normalize);
}

Can someone please guide me on how to do this?

like image 809
Srinivasan Ramachandran Avatar asked Jan 04 '23 10:01

Srinivasan Ramachandran


1 Answers

It seems that you need to do overload cast as described here.

m.def("VectorCross", py::overload_cast<const Point3D&, const Point3D&, const Point3D&>(&VectorCross));
m.def("VectorCross", py::overload_cast<const float*, const float*, float*>(&VectorCross));
like image 71
Roman Miroshnychenko Avatar answered Apr 01 '23 11:04

Roman Miroshnychenko