Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython C++ wrapper operator() overloading error

Connected with my previous question. Using Cython to wrap a C++ class that uses OpenCV types as parameters

Now I'm stuck in another error. My cython wrapping code of the OpenCV type Matx33d looks like:

cdef extern from "opencv2/core/core.hpp" namespace "cv":
    cdef cppclass Matx33d "cv::Matx<double, 3, 3>":
        Matx33d()
        Matx33d(double v0, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8)
        double& operator()(int i, int j)

Then I define a function to copy the Matx33d to a numpy array.

cdef Matx33d2numpy(Matx33d &m):
    cdef np.ndarray[np.double_t, ndim=2] np_m = np.empty((3,3), dtype=np.float64)  
    np_m[0,0]= m(0,0); np_m[0,1]= m(0,1); np_m[0,2]= m(0,2)
    np_m[1,0]= m(1,0); np_m[1,1]= m(1,1); np_m[1,2]= m(1,2)
    np_m[2,0]= m(2,0); np_m[2,1]= m(2,1); np_m[2,2]= m(2,2)    
    return np_m

When I compile the cython wrapper I get these error

geom_gateway.cpp(2528) error C3861: '()': identifier not found

This corresponds to the first use of Matx33d::operator(), that's when accessing m(0,0) in the code above. If I look at the generated geom_gateway.cpp line 2528 I get:

  *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_np_m.rcbuffer->pybuffer.buf, __pyx_t_6, __pyx_pybuffernd_np_m.diminfo[0].strides, __pyx_t_7, __pyx_pybuffernd_np_m.diminfo[1].strides) = operator()(0, 0);

I don't understand this operator()(0, 0) there alone at the end of the line without any object!! How is this possible? Is this a Cython bug? or is the syntax I'm using for the operator() wrong? Any help is appreciated!

like image 616
martinako Avatar asked Feb 15 '23 08:02

martinako


1 Answers

Ok, I don't know why that error happened, to me it looks like the syntax

double& operator()(int i, int j)

...should work, but it doesn't. This syntax does work for other operators like +,-,/,*

An alternative syntax that does work is the following:

double& get "operator()"(int i, int j)

Then in cython code when we want to use the operator()(i,j) instead we call get(i, j)

like image 193
martinako Avatar answered Feb 24 '23 16:02

martinako