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))