Blender  V2.59
KX_PyMath.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_PyMath.cpp 35171 2011-02-25 13:35:59Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  * Initialize Python thingies.
00029  */
00030 
00036 #if defined(WIN32) && !defined(FREE_WINDOWS)
00037 #pragma warning (disable : 4786)
00038 #endif //WIN32
00039 
00040 #ifdef WITH_PYTHON
00041 
00042 #include "MT_Vector3.h"
00043 #include "MT_Vector4.h"
00044 #include "MT_Matrix4x4.h"
00045 #include "MT_Point2.h"
00046 
00047 #include "ListValue.h"
00048 
00049 #include "KX_Python.h"
00050 #include "KX_PyMath.h"
00051 
00052 bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &rot, const char *error_prefix)
00053 {
00054         int size= PySequence_Size(pyval);
00055         
00056         if (size == 4)
00057         {
00058                 MT_Quaternion qrot;
00059                 if (PyQuatTo(pyval, qrot))
00060                 {
00061                         rot.setRotation(qrot);
00062                         return true;
00063                 }
00064         }
00065         else if (size == 3) {
00066                 /* 3x3 matrix or euler */
00067                 MT_Vector3 erot;
00068                 if (PyVecTo(pyval, erot))
00069                 {
00070                         rot.setEuler(erot);
00071                         return true;
00072                 }
00073                 PyErr_Clear();
00074                 
00075                 if (PyMatTo(pyval, rot))
00076                 {
00077                         return true;
00078                 }
00079         }
00080         
00081         PyErr_Format(PyExc_TypeError, "%s, could not set the orientation from a 3x3 matrix, quaternion or euler sequence", error_prefix);
00082         return false;
00083 }
00084 
00085 bool PyQuatTo(PyObject* pyval, MT_Quaternion &qrot)
00086 {
00087         if(!PyVecTo(pyval, qrot))
00088                 return false;
00089 
00090         /* annoying!, Blender/Mathutils have the W axis first! */
00091         MT_Scalar w= qrot[0]; /* from python, this is actually the W */
00092         qrot[0]= qrot[1];
00093         qrot[1]= qrot[2];
00094         qrot[2]= qrot[3];
00095         qrot[3]= w;
00096 
00097         return true;
00098 }
00099 
00100 PyObject* PyObjectFrom(const MT_Matrix4x4 &mat)
00101 {
00102 #ifdef USE_MATHUTILS
00103         float fmat[16];
00104         mat.getValue(fmat);
00105         return newMatrixObject(fmat, 4, 4, Py_NEW, NULL);
00106 #else
00107         PyObject *collist = PyList_New(4);
00108         PyObject *col;
00109         int i;
00110         
00111         for(i=0; i < 4; i++) {
00112                 col = PyList_New(4);
00113                 PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i]));
00114                 PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i]));
00115                 PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i]));
00116                 PyList_SET_ITEM(col, 3, PyFloat_FromDouble(mat[3][i]));
00117                 PyList_SET_ITEM(collist, i, col);
00118         }
00119         
00120         return collist;
00121 #endif
00122 }
00123 
00124 PyObject* PyObjectFrom(const MT_Matrix3x3 &mat)
00125 {
00126 #ifdef USE_MATHUTILS
00127         float fmat[9];
00128         mat.getValue3x3(fmat);
00129         return newMatrixObject(fmat, 3, 3, Py_NEW, NULL);
00130 #else
00131         PyObject *collist = PyList_New(3);
00132         PyObject *col;
00133         int i;
00134         
00135         for(i=0; i < 3; i++) {
00136                 col = PyList_New(3);
00137                 PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i]));
00138                 PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i]));
00139                 PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i]));
00140                 PyList_SET_ITEM(collist, i, col);
00141         }
00142         
00143         return collist;
00144 #endif
00145 }
00146 
00147 #ifdef USE_MATHUTILS
00148 PyObject* PyObjectFrom(const MT_Quaternion &qrot)
00149 {
00150         /* NOTE, were re-ordering here for Mathutils compat */
00151         float fvec[4]= {qrot[3], qrot[0], qrot[1], qrot[2]};
00152         return newQuaternionObject(fvec, Py_NEW, NULL);
00153 }
00154 #endif
00155 
00156 PyObject* PyObjectFrom(const MT_Tuple4 &vec)
00157 {
00158 #ifdef USE_MATHUTILS
00159         float fvec[4]= {vec[0], vec[1], vec[2], vec[3]};
00160         return newVectorObject(fvec, 4, Py_NEW, NULL);
00161 #else
00162         PyObject *list = PyList_New(4);
00163         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
00164         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
00165         PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2]));
00166         PyList_SET_ITEM(list, 3, PyFloat_FromDouble(vec[3]));
00167         return list;
00168 #endif
00169 }
00170 
00171 PyObject* PyObjectFrom(const MT_Tuple3 &vec)
00172 {
00173 #ifdef USE_MATHUTILS
00174         float fvec[3]= {vec[0], vec[1], vec[2]};
00175         return newVectorObject(fvec, 3, Py_NEW, NULL);
00176 #else
00177         PyObject *list = PyList_New(3);
00178         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
00179         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
00180         PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2]));
00181         return list;
00182 #endif  
00183 }
00184 
00185 PyObject* PyObjectFrom(const MT_Tuple2 &vec)
00186 {
00187 #ifdef USE_MATHUTILS
00188         float fvec[2]= {vec[0], vec[1]};
00189         return newVectorObject(fvec, 2, Py_NEW, NULL);
00190 #else
00191         PyObject *list = PyList_New(2);
00192         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0]));
00193         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1]));
00194         return list;
00195 #endif
00196 }
00197 
00198 #endif // WITH_PYTHON