Skip to content

Commit

Permalink
UPBGE: Use template function for PyObjectFrom.
Browse files Browse the repository at this point in the history
PyObjectFrom is now a template function to adapt on different vector
size and matrix sizes.
Support of vecX_mapped is addded.
  • Loading branch information
panzergame committed May 30, 2018
1 parent 7188624 commit 8b68754
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 117 deletions.
90 changes: 1 addition & 89 deletions source/gameengine/Ketsji/KX_PyMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,51 +86,6 @@ bool PyQuatTo(PyObject *pyval, mt::quat &qrot)
return true;
}

PyObject *PyObjectFrom(const mt::mat4 &mat)
{
#ifdef USE_MATHUTILS
return Matrix_CreatePyObject((float *)mat.Data(), 4, 4, nullptr);
#else
PyObject *collist = PyList_New(4);
PyObject *col;
int i;

for (i = 0; i < 4; i++) {
col = PyList_New(4);
PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i]));
PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i]));
PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i]));
PyList_SET_ITEM(col, 3, PyFloat_FromDouble(mat[3][i]));
PyList_SET_ITEM(collist, i, col);
}

return collist;
#endif
}

PyObject *PyObjectFrom(const mt::mat3 &mat)
{
#ifdef USE_MATHUTILS
float fmat[9];
mat.Pack(fmat);
return Matrix_CreatePyObject(fmat, 3, 3, nullptr);
#else
PyObject *collist = PyList_New(3);
PyObject *col;
int i;

for (i = 0; i < 3; i++) {
col = PyList_New(3);
PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i]));
PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i]));
PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i]));
PyList_SET_ITEM(collist, i, col);
}

return collist;
#endif
}

#ifdef USE_MATHUTILS
PyObject *PyObjectFrom(const mt::quat &qrot)
{
Expand All @@ -140,55 +95,12 @@ PyObject *PyObjectFrom(const mt::quat &qrot)
}
#endif

PyObject *PyObjectFrom(const mt::vec4 &vec)
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec.Data(), 4, nullptr);
#else
PyObject *list = PyList_New(4);
PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2]));
PyList_SET_ITEM(list, 3, PyFloat_FromDouble(vec[3]));
return list;
#endif
}

PyObject *PyObjectFrom(const mt::vec3 &vec)
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec.Data(), 3, nullptr);
#else
PyObject *list = PyList_New(3);
PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2]));
return list;
#endif
}

PyObject *PyObjectFrom(const mt::vec2 &vec)
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec.Data(), 2, nullptr);
#else
PyObject *list = PyList_New(2);
PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
return list;
#endif
}

PyObject *PyColorFromVector(const mt::vec3 &vec)
{
#ifdef USE_MATHUTILS
return Color_CreatePyObject(vec.Data(), nullptr);
#else
PyObject *list = PyList_New(3);
PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2]));
return list;
return PyObjectFrom(vec);
#endif
}

Expand Down
81 changes: 53 additions & 28 deletions source/gameengine/Ketsji/KX_PyMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,44 +232,69 @@ bool PyQuatTo(PyObject *pyval, mt::quat &qrot);

bool PyOrientationTo(PyObject *pyval, mt::mat3 &mat, const char *error_prefix);

/**
* Converts an mt::mat4 to a python object.
*/
PyObject *PyObjectFrom(const mt::mat4 &mat);

/**
* Converts an mt::mat3 to a python object.
*/
PyObject *PyObjectFrom(const mt::mat3 &mat);
/// Converts an mt::matX to a python object.
template <int Rows, int Cols>
PyObject *PyObjectFrom(const mt::Matrix<float, Rows, Cols> &mat)
{
#ifdef USE_MATHUTILS
float fmat[Rows * Cols];
mat.Pack(fmat);
return Matrix_CreatePyObject(fmat, Rows, Cols, nullptr);
#else
PyObject *list = PyList_New(Rows);

/**
* Converts an mt::vec2 to a python object.
*/
PyObject *PyObjectFrom(const mt::vec2 &vec);
for (unsigned short i = 0; i < Rows; ++i) {
PyObject *row = PyList_New(Cols);
for (unsigned short j = 0; j < Cols; ++j) {
PyList_SET_ITEM(row, j, PyFloat_FromDouble(mat[i][j]));
}
PyList_SET_ITEM(list, i, row);
}

/**
* Converts an mt::vec3 to a python object
*/
PyObject *PyObjectFrom(const mt::vec3 &vec);
return list;
#endif
}

#ifdef USE_MATHUTILS
/**
* Converts an mt::quat to a python object.
*/
/// Converts an mt::quat to a python object.
PyObject *PyObjectFrom(const mt::quat &qrot);
#endif

/**
* Converts an mt::vec4 to a python object.
*/
PyObject *PyObjectFrom(const mt::vec4 &pos);
/// Converts an mt::vecX to a python object.
template <int Size>
PyObject *PyObjectFrom(const mt::Vector<float, Size> &vec)
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec.Data(), Size, nullptr);
#else
PyObject *list = PyList_New(Size);
for (unsigned short i = 0; i < Size; ++i) {
PyList_SET_ITEM(list, i, PyFloat_FromDouble(vec[i]));
}
return list;
#endif
}

/**
* Converts an mt::vec3 to a python color object.
*/
/// Converts an mt::vecX_packed to a python object.
template <int Size>
PyObject *PyObjectFrom(const mt::VectorPacked<float, Size> &vec)
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec.data, Size, nullptr);
#else
PyObject *list = PyList_New(Size);
for (unsigned short i = 0; i < Size; ++i) {
PyList_SET_ITEM(list, i, PyFloat_FromDouble(vec.data[i]));
}
return list;
#endif
}

/// Converts an mt::vec3 to a python color object.
PyObject *PyColorFromVector(const mt::vec3 &vec);

template <unsigned short Size>
/// Convert a float array to a python object.
template <int Size>
PyObject *PyObjectFrom(const float (&vec)[Size])
{
#ifdef USE_MATHUTILS
Expand Down

0 comments on commit 8b68754

Please sign in to comment.