1

I'm currently trying to cythonize my c++ classes to be usabe in python and I startet with a simple class of mine, but I'm stuck with returning a pointer. My apttempt to solve this problem was to add a copy-constructor and to return the Python version of the C++ class but without any success.

I get the following error:

Error compiling Cython file:
------------------------------------------------------------
...

    def bearing(self, Coordinate *coordinate):
        return self.thisptr.bearing(coordinate)

    def destination(self, double bearing, double distance):
        return PyCCoordinate(self.thisptr.destination(bearing, distance))
                                                    ^
------------------------------------------------------------

./coordinate.pyx:32:53: Cannot convert 'Coordinate *' to Python object

here are my files

coordinate.h

class Coordinate {
private:
    double m_x = 0;
    double m_y = 0;
public:
    Coordinate(const double x, const double y);

    Coordinate(const Coordinate *coord);

    void setX(const double value);

    void setY(const double value);

    double getX();

    double getY();

    double distance(Coordinate *coord);

    double bearing(Coordinate *coord);

    Coordinate *destination(const double bearing, const double distance);
};

coordinate.cpp

#include <cmath>
#include "coordinate.h"

Coordinate::Coordinate(const double x, const double y) {
    m_x = x;
    m_y = y;
}

Coordinate::Coordinate(const Coordinate *coord) {
    m_x = coord->x;
    m_y = coord->y;
}


void Coordinate::setX(const double value) {
    m_x = value;
}

void Coordinate::setY(const double value) {
    m_y = value;
}

double Coordinate::getX() {
    return m_x;
}

double Coordinate::getY() {
    return m_y;
}

double Coordinate::distance(Coordinate *coord) {
    return sqrt(pow(m_x - coord->getX(), 2.0) + pow(m_y - coord->getY(), 2.0));
}

double Coordinate::bearing(Coordinate *coord) {
    const double len_x = coord->getX() - m_x;
    const double len_y = coord->getY() - m_y;
    double res = atan2(len_y, len_x);
    if (res < 0.0) {
        res += 2.0 * M_PI;
    }
    return res;
}

Coordinate *Coordinate::destination(const double bearing, const double distance) {
    double new_x = m_x + cos(bearing) * distance;
    double new_y = m_y + sin(bearing) * distance;
    return new Coordinate(new_x, new_y);
}

coordinate.pxy

cdef extern from "coordinate.h":
    cdef cppclass Coordinate:
        Coordinate(const double x, const double y) except +
        Coordinate(const Coordinate *coord) except +

        void setX(const double value)
        void setY(const double value)
        double getX()
        double getY()
        double distance(Coordinate *coord)
        double bearing(Coordinate *coord)
        Coordinate *destination(const double bearing, const double distance)

cdef class PyCCoordinate:
    cdef Coordinate *thisptr
    def __cinit__(self, double x, double y):
        self.thisptr = new Coordinate(x,y)
    def __cinit__(self, Coordinate* coord):
        self.thisptr = new Coordinate(coord)
    def __dealloc__(self):
        del self.thisptr        

    def distance(self, Coordinate *coordinate):
        return self.thisptr.distance(coordinate)

    def bearing(self, Coordinate *coordinate):
        return self.thisptr.bearing(coordinate)

    def destination(self, double bearing, double distance):
        return PyCCoordinate(self.thisptr.destination(bearing, distance))
4

1 回答 1

1

一个问题是 cython 语法有些误导:如果将 -functiondef定义为例如(因为您的示例不是最小的,我组成了一个不相关的示例):

def twice(double d):
   return 2.0*d

然后参数不是作为 C-double 而是作为通常的 Python 对象传递给这个(python)函数。但是,早期绑定将导致 cython__pyx_PyFloat_AsDouble在运行时尝试将此 Python 对象转换为 C-double,这是调用该函数时的第一件事 - 这一切都发生在幕后,因此作为编码人员,您有这种欺骗性的填充,您将真正传递一个 double 给函数。

然而,这种自动转换仅对某些类型是可能的——最突出double的是 , int, cdef-classes 等等。对于其他类型,这是不可能的,例如对于原始指针(这也意味着指向自定义 cpp 类的指针)——没有什么像__pyx_PyFloat_AsDouble.

例如

def twice(double *d):
   pass

不能被cythonized,因为原始指针不能自动从/转换为python-object,这是python-function所需要的。

可以cdef用原始指针定义一个函数,因为它们不过是简单的 C 函数,因此

cdef twice(double *d):
   pass

会编译。然而,这对你没有帮助__cinit__,因为它必须是一个def-function。

不幸的是,Cython 不会显示代码中的所有错误,只会显示它找到的第一个错误 - 否则它会告诉你你所有的defsCoordinate *coordinate都无效。

如何解决它?基本上你应该在签名中使用你的cdef-wrapper-classPyCCoordinate然后-forthisptr计算,例如:

def distance(self, PyCCoordinate coordinate):
    return self.thisptr.distance(coordinate.thisptr)

这个解决方案显然不适用于对象的构造,就像在方法中一样destination- 你必须先构造一个PyCCoordinate-object 才能使用它!一个可能的解决方案是有一个构造函数,它将构造一个带有thisptrbeing的包装对象NULL,调用它并手动设置这个指针,比如

cdef class PyCCoordinate:
    cdef Coordinate *thisptr
    def __cinit__(self):
        pass

    def __dealloc__(self):
        del self.thisptr        

    def destination(self, double bearing, double distance):
        cdef PyCCoordinate res=PyCCoordinate()
        res.thisptr=self.thisptr.destination(bearing, distance)
        return res

另一个问题:cython(也是python),不像c++,不知道重载,所以不能定义两个不同的构造函数(也不能定义一个私有的),所以调度必须手动完成,例如:

cdef class PyCCoordinate:
    cdef Coordinate *thisptr

    def __cinit__(self, x=None, y=None):
        if x is None or y is None:#default, "private" constructor
           pass     #thisptr is initialized to NULL
        else:
           self.thisptr = new Coordinate(x,y) #

    def __dealloc__(self):
        del self.thisptr        

    def destination(self, double bearing, double distance):
        cdef PyCCoordinate res=PyCCoordinate()
        res.thisptr=self.thisptr.destination(bearing, distance)
        return res
于 2018-04-13T06:31:00.907 回答