2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2004-05-16 12:53:54 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2004-05-16 12:53:54 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2004-05-16 12:53:54 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-05-16 12:53:54 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2004-05-16 12:53:54 +00:00
|
|
|
* Initialize Python thingies.
|
|
|
|
*/
|
|
|
|
|
2011-02-25 13:35:59 +00:00
|
|
|
/** \file gameengine/Ketsji/KX_PyMath.cpp
|
|
|
|
* \ingroup ketsji
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-09-15 16:13:32 +00:00
|
|
|
#if defined(WIN32) && !defined(FREE_WINDOWS)
|
2004-05-16 12:53:54 +00:00
|
|
|
#pragma warning (disable : 4786)
|
|
|
|
#endif //WIN32
|
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2009-09-29 21:42:40 +00:00
|
|
|
|
2004-05-16 12:53:54 +00:00
|
|
|
#include "MT_Vector3.h"
|
|
|
|
#include "MT_Vector4.h"
|
|
|
|
#include "MT_Matrix4x4.h"
|
2004-05-30 11:09:46 +00:00
|
|
|
#include "MT_Point2.h"
|
2004-05-16 12:53:54 +00:00
|
|
|
|
|
|
|
#include "ListValue.h"
|
|
|
|
|
|
|
|
#include "KX_Python.h"
|
2009-04-20 15:06:46 +00:00
|
|
|
#include "KX_PyMath.h"
|
2004-05-16 12:53:54 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &rot, const char *error_prefix)
|
2009-04-20 15:06:46 +00:00
|
|
|
{
|
|
|
|
int size= PySequence_Size(pyval);
|
|
|
|
|
|
|
|
if (size == 4)
|
|
|
|
{
|
|
|
|
MT_Quaternion qrot;
|
2009-06-25 20:47:41 +00:00
|
|
|
if (PyQuatTo(pyval, qrot))
|
2009-04-20 15:06:46 +00:00
|
|
|
{
|
|
|
|
rot.setRotation(qrot);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (size == 3) {
|
|
|
|
/* 3x3 matrix or euler */
|
|
|
|
MT_Vector3 erot;
|
|
|
|
if (PyVecTo(pyval, erot))
|
|
|
|
{
|
|
|
|
rot.setEuler(erot);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
if (PyMatTo(pyval, rot))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_TypeError, "%s, could not set the orientation from a 3x3 matrix, quaternion or euler sequence", error_prefix);
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-26 12:09:17 +00:00
|
|
|
|
2009-06-25 20:47:41 +00:00
|
|
|
bool PyQuatTo(PyObject* pyval, MT_Quaternion &qrot)
|
|
|
|
{
|
|
|
|
if(!PyVecTo(pyval, qrot))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* annoying!, Blender/Mathutils have the W axis first! */
|
|
|
|
MT_Scalar w= qrot[0]; /* from python, this is actually the W */
|
|
|
|
qrot[0]= qrot[1];
|
|
|
|
qrot[1]= qrot[2];
|
|
|
|
qrot[2]= qrot[3];
|
|
|
|
qrot[3]= w;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-07-17 05:28:23 +00:00
|
|
|
PyObject* PyObjectFrom(const MT_Matrix4x4 &mat)
|
2004-05-16 12:53:54 +00:00
|
|
|
{
|
2009-06-25 10:11:37 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
float fmat[16];
|
|
|
|
mat.getValue(fmat);
|
2009-06-30 00:42:17 +00:00
|
|
|
return newMatrixObject(fmat, 4, 4, Py_NEW, NULL);
|
2009-02-23 06:41:10 +00:00
|
|
|
#else
|
2009-12-23 09:55:34 +00:00
|
|
|
PyObject *collist = PyList_New(4);
|
|
|
|
PyObject *col;
|
2009-02-23 06:41:10 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < 4; i++) {
|
2009-12-23 09:55:34 +00:00
|
|
|
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);
|
2009-02-23 06:41:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-23 09:55:34 +00:00
|
|
|
return collist;
|
2009-02-23 06:41:10 +00:00
|
|
|
#endif
|
2004-05-26 12:09:17 +00:00
|
|
|
}
|
|
|
|
|
2004-07-17 05:28:23 +00:00
|
|
|
PyObject* PyObjectFrom(const MT_Matrix3x3 &mat)
|
2004-05-26 12:09:17 +00:00
|
|
|
{
|
2009-06-25 10:11:37 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
float fmat[9];
|
|
|
|
mat.getValue3x3(fmat);
|
2009-06-30 00:42:17 +00:00
|
|
|
return newMatrixObject(fmat, 3, 3, Py_NEW, NULL);
|
2009-02-23 06:41:10 +00:00
|
|
|
#else
|
2009-12-23 09:55:34 +00:00
|
|
|
PyObject *collist = PyList_New(3);
|
|
|
|
PyObject *col;
|
2009-02-23 06:41:10 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < 3; i++) {
|
2009-12-23 09:55:34 +00:00
|
|
|
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);
|
2009-02-23 06:41:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-23 09:55:34 +00:00
|
|
|
return collist;
|
2009-02-23 06:41:10 +00:00
|
|
|
#endif
|
2004-05-16 12:53:54 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 20:47:41 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
PyObject* PyObjectFrom(const MT_Quaternion &qrot)
|
|
|
|
{
|
|
|
|
/* NOTE, were re-ordering here for Mathutils compat */
|
|
|
|
float fvec[4]= {qrot[3], qrot[0], qrot[1], qrot[2]};
|
2009-08-25 13:54:56 +00:00
|
|
|
return newQuaternionObject(fvec, Py_NEW, NULL);
|
2009-06-25 20:47:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-17 05:28:23 +00:00
|
|
|
PyObject* PyObjectFrom(const MT_Tuple4 &vec)
|
2004-05-30 11:09:46 +00:00
|
|
|
{
|
2009-06-25 10:11:37 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
float fvec[4]= {vec[0], vec[1], vec[2], vec[3]};
|
2009-08-25 13:54:56 +00:00
|
|
|
return newVectorObject(fvec, 4, Py_NEW, NULL);
|
2009-02-23 06:41:10 +00:00
|
|
|
#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
|
2004-05-30 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
2004-07-17 05:28:23 +00:00
|
|
|
PyObject* PyObjectFrom(const MT_Tuple3 &vec)
|
2004-05-26 12:09:17 +00:00
|
|
|
{
|
2009-06-25 10:11:37 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
float fvec[3]= {vec[0], vec[1], vec[2]};
|
2009-08-25 13:54:56 +00:00
|
|
|
return newVectorObject(fvec, 3, Py_NEW, NULL);
|
2009-02-23 06:41:10 +00:00
|
|
|
#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
|
2004-05-26 12:09:17 +00:00
|
|
|
}
|
|
|
|
|
2004-07-17 05:28:23 +00:00
|
|
|
PyObject* PyObjectFrom(const MT_Tuple2 &vec)
|
2004-05-30 11:09:46 +00:00
|
|
|
{
|
2009-06-25 10:11:37 +00:00
|
|
|
#ifdef USE_MATHUTILS
|
|
|
|
float fvec[2]= {vec[0], vec[1]};
|
2009-08-25 13:54:56 +00:00
|
|
|
return newVectorObject(fvec, 2, Py_NEW, NULL);
|
2009-02-23 06:41:10 +00:00
|
|
|
#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
|
2004-05-30 11:09:46 +00:00
|
|
|
}
|
2009-09-29 21:42:40 +00:00
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#endif // WITH_PYTHON
|