Roll back changes from Big Mathutils Commit on 2005/05/20.

This commit is contained in:
Stephen Swaney
2005-05-22 17:40:00 +00:00
parent 910b0f2cda
commit ece00ff04a
19 changed files with 3697 additions and 3161 deletions

View File

@@ -1523,7 +1523,7 @@ PyObject *CreateGlobalDictionary( void )
PyDict_SetItemString( dict, "__name__",
PyString_FromString( "__main__" ) );
return EXPP_incr_ret(dict);
return dict;
}
/*****************************************************************************

View File

@@ -46,14 +46,12 @@
#include <BSE_editaction.h>
#include <BKE_constraint.h>
#include <MEM_guardedalloc.h>
#include <BKE_utildefines.h>
#include "constant.h"
#include "gen_utils.h"
#include "NLA.h"
#include "quat.h"
#include "matrix.h"
#include "vector.h"
#include "Types.h"
//--------------------Python API function prototypes for the Bone module----
static PyObject *M_Bone_New( PyObject * self, PyObject * args );
@@ -539,19 +537,45 @@ PyObject *Bone_CreatePyObject( struct Bone * bone )
//allocate space for python vars
blen_bone->name = PyMem_Malloc( 32 + 1 );
blen_bone->parent = PyMem_Malloc( 32 + 1 );
blen_bone->head = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->tail = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->loc = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->dloc = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->size = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->dsize = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
blen_bone->quat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
blen_bone->dquat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->parmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->defmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->irestmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->posemat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->head =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->tail =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->loc =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->dloc =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->size =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->dsize =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
blen_bone->quat =
( QuaternionObject * )
newQuaternionObject( PyMem_Malloc( 4 * sizeof( float ) ) );
blen_bone->dquat =
( QuaternionObject * )
newQuaternionObject( PyMem_Malloc( 4 * sizeof( float ) ) );
blen_bone->obmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
blen_bone->parmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
blen_bone->defmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
blen_bone->irestmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
blen_bone->posemat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
if( !updatePyBone( blen_bone ) )
return EXPP_ReturnPyObjError( PyExc_AttributeError,
@@ -600,19 +624,45 @@ static PyObject *M_Bone_New( PyObject * self, PyObject * args )
//allocate space for python vars
py_bone->name = PyMem_Malloc( 32 + 1 );
py_bone->parent = PyMem_Malloc( 32 + 1 );
py_bone->head = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->tail = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->loc = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->dloc = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->size = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->dsize = ( VectorObject *)newVectorObject( NULL, 3, Py_NEW );
py_bone->quat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
py_bone->dquat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
py_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
py_bone->parmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
py_bone->defmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
py_bone->irestmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
py_bone->posemat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
py_bone->head =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->tail =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->loc =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->dloc =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->size =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->dsize =
( VectorObject * )
newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ), 3 );
py_bone->quat =
( QuaternionObject * )
newQuaternionObject( PyMem_Malloc( 4 * sizeof( float ) ) );
py_bone->dquat =
( QuaternionObject * )
newQuaternionObject( PyMem_Malloc( 4 * sizeof( float ) ) );
py_bone->obmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
py_bone->parmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
py_bone->defmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
py_bone->irestmat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
py_bone->posemat =
( MatrixObject * )
newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
//default py values
BLI_strncpy( py_bone->name, name_str, strlen( name_str ) + 1 );
@@ -709,17 +759,19 @@ static PyObject *Bone_getWeight( BPy_Bone * self )
static PyObject *Bone_getHead( BPy_Bone * self )
{
PyObject *attr = NULL;
float vec[3];
float *vec;
int x;
if( !self->bone ) { //test to see if linked to armature
//use python vars
vec = PyMem_Malloc( 3 * sizeof( float ) );
for( x = 0; x < 3; x++ )
vec[x] = self->head->vec[x];
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
attr = ( PyObject * ) newVectorObject( vec, 3 );
} else {
//use bone datastruct
attr = newVectorObject( NULL, 3, Py_NEW );
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
( ( VectorObject * ) attr )->vec[0] = self->bone->head[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->head[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->head[2];
@@ -735,17 +787,19 @@ static PyObject *Bone_getHead( BPy_Bone * self )
static PyObject *Bone_getTail( BPy_Bone * self )
{
PyObject *attr = NULL;
float vec[3];
float *vec;
int x;
if( !self->bone ) { //test to see if linked to armature
//use python vars
vec = PyMem_Malloc( 3 * sizeof( float ) );
for( x = 0; x < 3; x++ )
vec[x] = self->tail->vec[x];
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
attr = ( PyObject * ) newVectorObject( vec, 3 );
} else {
//use bone datastruct
attr = newVectorObject( NULL, 3, Py_NEW );
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
( ( VectorObject * ) attr )->vec[0] = self->bone->tail[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->tail[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->tail[2];
@@ -761,17 +815,19 @@ static PyObject *Bone_getTail( BPy_Bone * self )
static PyObject *Bone_getLoc( BPy_Bone * self )
{
PyObject *attr = NULL;
float vec[3];
float *vec;
int x;
if( !self->bone ) { //test to see if linked to armature
//use python vars
vec = PyMem_Malloc( 3 * sizeof( float ) );
for( x = 0; x < 3; x++ )
vec[x] = self->loc->vec[x];
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
attr = ( PyObject * ) newVectorObject( vec, 3 );
} else {
//use bone datastruct
attr = newVectorObject( vec, 3, Py_NEW );
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
( ( VectorObject * ) attr )->vec[0] = self->bone->loc[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->loc[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->loc[2];
@@ -787,17 +843,19 @@ static PyObject *Bone_getLoc( BPy_Bone * self )
static PyObject *Bone_getSize( BPy_Bone * self )
{
PyObject *attr = NULL;
float vec[3];
float *vec;
int x;
if( !self->bone ) { //test to see if linked to armature
//use python vars
vec = PyMem_Malloc( 3 * sizeof( float ) );
for( x = 0; x < 3; x++ )
vec[x] = self->size->vec[x];
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
attr = ( PyObject * ) newVectorObject( vec, 3 );
} else {
//use bone datastruct
attr = newVectorObject( vec, 3, Py_NEW );
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
( ( VectorObject * ) attr )->vec[0] = self->bone->size[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->size[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->size[2];
@@ -813,18 +871,20 @@ static PyObject *Bone_getSize( BPy_Bone * self )
static PyObject *Bone_getQuat( BPy_Bone * self )
{
PyObject *attr = NULL;
float quat[4];
float *quat;
int x;
if( !self->bone ) { //test to see if linked to armature
//use python vars - p.s. - you must return a copy or else
//python will trash the internal var
quat = PyMem_Malloc( 4 * sizeof( float ) );
for( x = 0; x < 4; x++ )
quat[x] = self->quat->quat[x];
attr = ( PyObject * ) newQuaternionObject( quat, Py_NEW );
attr = ( PyObject * ) newQuaternionObject( quat );
} else {
//use bone datastruct
attr = newQuaternionObject( NULL, Py_NEW );
attr = newQuaternionObject( PyMem_Malloc
( 4 * sizeof( float ) ) );
( ( QuaternionObject * ) attr )->quat[0] = self->bone->quat[0];
( ( QuaternionObject * ) attr )->quat[1] = self->bone->quat[1];
( ( QuaternionObject * ) attr )->quat[2] = self->bone->quat[2];
@@ -1625,7 +1685,7 @@ static PyObject *Bone_getRestMatrix( BPy_Bone * self, PyObject * args )
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
"expected 'bonespace' or 'worldspace'" ) );
matrix = newMatrixObject( NULL, 4, 4 , Py_NEW);
matrix = newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
if( !self->bone ) { //test to see if linked to armature
//use python vars

File diff suppressed because it is too large Load Diff

View File

@@ -29,48 +29,14 @@
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
//Include this file for access to vector, quat, matrix, euler, etc...
#ifndef EXPP_Mathutils_H
#define EXPP_Mathutils_H
#include <Python.h>
#include "vector.h"
#include "matrix.h"
#include "quat.h"
#include "euler.h"
#include "Types.h"
PyObject *Mathutils_Init( void );
PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat);
PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec);
PyObject *M_Mathutils_Rand(PyObject * self, PyObject * args);
PyObject *M_Mathutils_Vector(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CrossVecs(PyObject * self, PyObject * args);
PyObject *M_Mathutils_DotVecs(PyObject * self, PyObject * args);
PyObject *M_Mathutils_AngleBetweenVecs(PyObject * self, PyObject * args);
PyObject *M_Mathutils_MidpointVecs(PyObject * self, PyObject * args);
PyObject *M_Mathutils_ProjectVecs(PyObject * self, PyObject * args);
PyObject *M_Mathutils_Matrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_TranslationMatrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_ScaleMatrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_OrthoProjectionMatrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_ShearMatrix(PyObject * self, PyObject * args);
PyObject *M_Mathutils_Quaternion(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CrossQuats(PyObject * self, PyObject * args);
PyObject *M_Mathutils_DotQuats(PyObject * self, PyObject * args);
PyObject *M_Mathutils_DifferenceQuats(PyObject * self, PyObject * args);
PyObject *M_Mathutils_Slerp(PyObject * self, PyObject * args);
PyObject *M_Mathutils_Euler(PyObject * self, PyObject * args);
//DEPRECATED
PyObject *M_Mathutils_CopyMat(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CopyVec(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CopyQuat(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CopyEuler(PyObject * self, PyObject * args);
PyObject *M_Mathutils_RotateEuler(PyObject * self, PyObject * args);
PyObject *M_Mathutils_MatMultVec(PyObject * self, PyObject * args);
PyObject *M_Mathutils_VecMultMat(PyObject * self, PyObject * args);
#endif /* EXPP_Mathutils_H */

View File

@@ -58,15 +58,14 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "blendef.h"
#include "mydevice.h"
#include "Object.h"
#include "vector.h"
#include "constant.h"
#include "gen_utils.h"
#include "Mathutils.h"
/* only used for ob.oopsloc at the moment */
#include "DNA_oops_types.h"
@@ -760,11 +759,12 @@ static PyObject *NMVert_getattr( PyObject * self, char *name )
BPy_NMVert *mv = ( BPy_NMVert * ) self;
if( !strcmp( name, "co" ) || !strcmp( name, "loc" ) )
return newVectorObject(mv->co,3,Py_WRAP);
return newVectorProxy( mv->co, 3 );
else if( strcmp( name, "no" ) == 0 )
return newVectorObject(mv->no,3,Py_WRAP);
return newVectorProxy( mv->no, 3 );
else if( strcmp( name, "uvco" ) == 0 )
return newVectorObject(mv->uvco,3,Py_WRAP);
return newVectorProxy( mv->uvco, 3 );
else if( strcmp( name, "index" ) == 0 )
return PyInt_FromLong( mv->index );
else if( strcmp( name, "sel" ) == 0 )

View File

@@ -59,7 +59,6 @@
#include "Ipo.h"
#include "Lattice.h"
#include "modules.h"
#include "Mathutils.h"
#include "constant.h"
/* only used for oops location get/set at the moment */
@@ -647,14 +646,14 @@ PyObject *M_Object_New( PyObject * self, PyObject * args )
object->dupend = 100;
/* Gameengine defaults */
object->mass = 1.0f;
object->inertia = 1.0f;
object->formfactor = 0.4f;
object->damping = 0.04f;
object->rdamping = 0.1f;
object->anisotropicFriction[0] = 1.0f;
object->anisotropicFriction[1] = 1.0f;
object->anisotropicFriction[2] = 1.0f;
object->mass = 1.0;
object->inertia = 1.0;
object->formfactor = 0.4;
object->damping = 0.04;
object->rdamping = 0.1;
object->anisotropicFriction[0] = 1.0;
object->anisotropicFriction[1] = 1.0;
object->anisotropicFriction[2] = 1.0;
object->gameflag = OB_PROP;
object->lay = 1; // Layer, by default visible
@@ -1115,20 +1114,21 @@ static PyObject *Object_getDrawType( BPy_Object * self )
static PyObject *Object_getEuler( BPy_Object * self )
{
float eul[3];
EulerObject *eul;
eul[0] = self->object->rot[0];
eul[1] = self->object->rot[1];
eul[2] = self->object->rot[2];
eul = ( EulerObject * ) newEulerObject( NULL );
eul->eul[0] = self->object->rot[0];
eul->eul[1] = self->object->rot[1];
eul->eul[2] = self->object->rot[2];
return ( PyObject * ) newEulerObject( eul, Py_WRAP );
return ( PyObject * ) eul;
}
static PyObject *Object_getInverseMatrix( BPy_Object * self )
{
MatrixObject *inverse =
( MatrixObject * ) newMatrixObject( NULL, 4, 4, Py_NEW);
( MatrixObject * ) newMatrixObject( NULL, 4, 4 );
Mat4Invert( (float ( * )[4])*inverse->matrix, self->object->obmat );
return ( ( PyObject * ) inverse );
@@ -1175,29 +1175,35 @@ static PyObject *Object_getMaterials( BPy_Object * self, PyObject * args )
static PyObject *Object_getMatrix( BPy_Object * self, PyObject * args )
{
float matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
PyObject *matrix;
char *space = "worldspace"; /* default to world */
if( !PyArg_ParseTuple( args, "|s", &space ) ) {
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
"expected a string or nothing" ) );
}
//new matrix
matrix = newMatrixObject( NULL, 4, 4 );
if( BLI_streq( space, "worldspace" ) ) { /* Worldspace matrix */
disable_where_script( 1 );
where_is_object( self->object );
disable_where_script( 0 );
Mat4CpyMat4((float ( * )[4]) *( ( MatrixObject * ) matrix )->matrix,
self->object->obmat );
} else if( BLI_streq( space, "localspace" ) ) { /* Localspace matrix */
object_to_mat4( self->object, (float (*)[4])matrix );
return newMatrixObject(matrix,4,4,Py_NEW);
} else if( BLI_streq( space, "old_worldspace" ) ) {
object_to_mat4( self->object,
( float ( * )[4] ) *( ( MatrixObject * ) matrix )->matrix );
/* old behavior, prior to 2.34, check this method's doc string: */
} else if( BLI_streq( space, "old_worldspace" ) ) {
Mat4CpyMat4( (float ( * )[4]) *( ( MatrixObject * ) matrix )->matrix,
self->object->obmat );
} else {
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
"wrong parameter, expected nothing or either 'worldspace' (default),\n\
'localspace' or 'old_worldspace'" ) );
}
return newMatrixObject((float*)self->object->obmat,4,4,Py_WRAP);
return matrix;
}
static PyObject *Object_getName( BPy_Object * self )
@@ -1379,7 +1385,7 @@ static PyObject *Object_getBoundBox( BPy_Object * self )
does not have its own memory,
we must create vectors that allocate space */
vector = newVectorObject( NULL, 3, Py_NEW);
vector = newVectorObject( NULL, 3 );
memcpy( ( ( VectorObject * ) vector )->vec,
tmpvec, 3 * sizeof( float ) );
PyList_SET_ITEM( bbox, i, vector );
@@ -1400,7 +1406,7 @@ static PyObject *Object_getBoundBox( BPy_Object * self )
/* create vectors referencing object bounding box coords */
for( i = 0; i < 8; i++ ) {
vector = newVectorObject( vec, 3, Py_WRAP );
vector = newVectorObject( vec, 3 );
PyList_SET_ITEM( bbox, i, vector );
vec += 3;
}
@@ -3917,18 +3923,17 @@ int setupSB(Object* ob){
}
if(ob->soft){
ob->soft->nodemass = 1.0f;
ob->soft->grav = 0.0f;
ob->soft->mediafrict = 0.5f;
ob->soft->rklimit = 0.1f;
ob->soft->goalspring = 0.5f;
ob->soft->goalfrict = 0.0f;
ob->soft->mingoal = 0.0f;
ob->soft->maxgoal = 1.0f;
ob->soft->inspring = 0.5f;
ob->soft->infrict = 0.5f;
ob->soft->defgoal = 0.7f;
ob->soft->nodemass = 1.0;
ob->soft->grav = 0.0;
ob->soft->mediafrict = 0.5;
ob->soft->rklimit = 0.1;
ob->soft->goalspring = 0.5;
ob->soft->goalfrict = 0.0;
ob->soft->mingoal = 0.0;
ob->soft->maxgoal = 1.0;
ob->soft->inspring = 0.5;
ob->soft->infrict = 0.5;
ob->soft->defgoal = 0.7;
return 1;
}
else {

View File

@@ -33,6 +33,7 @@
#ifndef EXPP_OBJECT_H
#define EXPP_OBJECT_H
#include <Python.h>
#include <stdio.h>
#include <BDR_editobject.h>
#include <BKE_armature.h>
@@ -59,7 +60,10 @@
#include <DNA_action_types.h>
#include "gen_utils.h"
#include "vector.h"
#include "matrix.h"
#include "euler.h"
#include "quat.h"
/* The Object PyType Object defined in Object.c */
extern PyTypeObject Object_Type;

View File

@@ -57,7 +57,6 @@ void types_InitAll( void )
CurNurb_Type.ob_type = &PyType_Type;
Curve_Type.ob_type = &PyType_Type;
Effect_Type.ob_type = &PyType_Type;
Font_Type.ob_type = &PyType_Type;
Image_Type.ob_type = &PyType_Type;
Ipo_Type.ob_type = &PyType_Type;
IpoCurve_Type.ob_type = &PyType_Type;

View File

@@ -830,7 +830,7 @@ static PyObject *M_Window_GetViewMatrix( PyObject * self )
viewmat =
( PyObject * ) newMatrixObject( ( float * ) G.vd->viewmat, 4,
4, Py_WRAP );
4 );
if( !viewmat )
return EXPP_ReturnPyObjError( PyExc_MemoryError,
@@ -854,7 +854,7 @@ static PyObject *M_Window_GetPerspMatrix( PyObject * self )
perspmat =
( PyObject * ) newMatrixObject( ( float * ) G.vd->persmat, 4,
4, Py_WRAP);
4 );
if( !perspmat )
return EXPP_ReturnPyObjError( PyExc_MemoryError,

View File

@@ -29,328 +29,277 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include <BLI_arithb.h>
#include <BKE_utildefines.h>
#include "Mathutils.h"
#include "gen_utils.h"
#include "euler.h"
//-------------------------DOC STRINGS ---------------------------
//doc strings
char Euler_Zero_doc[] = "() - set all values in the euler to 0";
char Euler_Unique_doc[] ="() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock";
char Euler_ToMatrix_doc[] = "() - returns a rotation matrix representing the euler rotation";
char Euler_ToQuat_doc[] = "() - returns a quaternion representing the euler rotation";
char Euler_Rotate_doc[] = "() - rotate a euler by certain amount around an axis of rotation";
//-----------------------METHOD DEFINITIONS ----------------------
char Euler_Unique_doc[] =
"() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock";
char Euler_ToMatrix_doc[] =
"() - returns a rotation matrix representing the euler rotation";
char Euler_ToQuat_doc[] =
"() - returns a quaternion representing the euler rotation";
//methods table
struct PyMethodDef Euler_methods[] = {
{"zero", (PyCFunction) Euler_Zero, METH_NOARGS, Euler_Zero_doc},
{"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
{"toMatrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
{"toQuat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
{"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, Euler_Rotate_doc},
{"zero", ( PyCFunction ) Euler_Zero, METH_NOARGS,
Euler_Zero_doc},
{"unique", ( PyCFunction ) Euler_Unique, METH_NOARGS,
Euler_Unique_doc},
{"toMatrix", ( PyCFunction ) Euler_ToMatrix, METH_NOARGS,
Euler_ToMatrix_doc},
{"toQuat", ( PyCFunction ) Euler_ToQuat, METH_NOARGS,
Euler_ToQuat_doc},
{NULL, NULL, 0, NULL}
};
//-----------------------------METHODS----------------------------
//----------------------------Euler.toQuat()----------------------
//return a quaternion representation of the euler
/*****************************/
// Euler Python Object
/*****************************/
//euler methods
PyObject *Euler_ToQuat( EulerObject * self )
{
float eul[3];
float quat[4];
float *quat;
int x;
for( x = 0; x < 3; x++ ) {
eul[x] = self->eul[x] * ((float)Py_PI / 180);
self->eul[x] *= ( float ) ( Py_PI / 180 );
}
EulToQuat(eul, quat);
if(self->data.blend_data)
return (PyObject *) newQuaternionObject(quat, Py_WRAP);
else
return (PyObject *) newQuaternionObject(quat, Py_NEW);
quat = PyMem_Malloc( 4 * sizeof( float ) );
EulToQuat( self->eul, quat );
for( x = 0; x < 3; x++ ) {
self->eul[x] *= ( float ) ( 180 / Py_PI );
}
//----------------------------Euler.toMatrix()---------------------
//return a matrix representation of the euler
return ( PyObject * ) newQuaternionObject( quat );
}
PyObject *Euler_ToMatrix( EulerObject * self )
{
float eul[3];
float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
float *mat;
int x;
for( x = 0; x < 3; x++ ) {
eul[x] = self->eul[x] * ((float)Py_PI / 180);
self->eul[x] *= ( float ) ( Py_PI / 180 );
}
EulToMat3(eul, (float (*)[3]) mat);
if(self->data.blend_data)
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_WRAP);
else
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_NEW);
mat = PyMem_Malloc( 3 * 3 * sizeof( float ) );
EulToMat3( self->eul, ( float ( * )[3] ) mat );
for( x = 0; x < 3; x++ ) {
self->eul[x] *= ( float ) ( 180 / Py_PI );
}
//----------------------------Euler.unique()-----------------------
//sets the x,y,z values to a unique euler rotation
return ( PyObject * ) newMatrixObject( mat, 3, 3 );
}
PyObject *Euler_Unique( EulerObject * self )
{
double heading, pitch, bank;
double pi2 = Py_PI * 2.0f;
double piO2 = Py_PI / 2.0f;
double Opi2 = 1.0f / pi2;
float heading, pitch, bank;
float pi2 = ( float ) Py_PI * 2.0f;
float piO2 = ( float ) Py_PI / 2.0f;
float Opi2 = 1.0f / pi2;
//radians
heading = self->eul[0] * (float)Py_PI / 180;
pitch = self->eul[1] * (float)Py_PI / 180;
bank = self->eul[2] * (float)Py_PI / 180;
heading = self->eul[0] * ( float ) ( Py_PI / 180 );
pitch = self->eul[1] * ( float ) ( Py_PI / 180 );
bank = self->eul[2] * ( float ) ( Py_PI / 180 );
//wrap heading in +180 / -180
pitch += Py_PI;
pitch -= floor(pitch * Opi2) * pi2;
pitch -= Py_PI;
pitch += ( float ) Py_PI;
pitch -= ( float ) floor( pitch * Opi2 ) * pi2;
pitch -= ( float ) Py_PI;
if( pitch < -piO2 ) {
pitch = -Py_PI - pitch;
heading += Py_PI;
bank += Py_PI;
pitch = ( float ) -Py_PI - pitch;
heading += ( float ) Py_PI;
bank += ( float ) Py_PI;
} else if( pitch > piO2 ) {
pitch = Py_PI - pitch;
heading += Py_PI;
bank += Py_PI;
pitch = ( float ) Py_PI - pitch;
heading += ( float ) Py_PI;
bank += ( float ) Py_PI;
}
//gimbal lock test
if( fabs( pitch ) > piO2 - 1e-4 ) {
heading += bank;
bank = 0.0f;
} else {
bank += Py_PI;
bank -= (floor(bank * Opi2)) * pi2;
bank -= Py_PI;
bank += ( float ) Py_PI;
bank -= ( float ) ( floor( bank * Opi2 ) ) * pi2;
bank -= ( float ) Py_PI;
}
heading += Py_PI;
heading -= (floor(heading * Opi2)) * pi2;
heading -= Py_PI;
heading += ( float ) Py_PI;
heading -= ( float ) ( floor( heading * Opi2 ) ) * pi2;
heading -= ( float ) Py_PI;
//back to degrees
self->eul[0] = heading * 180 / (float)Py_PI;
self->eul[1] = pitch * 180 / (float)Py_PI;
self->eul[2] = bank * 180 / (float)Py_PI;
self->eul[0] = heading * ( float ) ( 180 / Py_PI );
self->eul[1] = pitch * ( float ) ( 180 / Py_PI );
self->eul[2] = bank * ( float ) ( 180 / Py_PI );
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------Euler.zero()-------------------------
//sets the euler to 0,0,0
PyObject *Euler_Zero( EulerObject * self )
{
self->eul[0] = 0.0;
self->eul[1] = 0.0;
self->eul[2] = 0.0;
return (PyObject*)self;
}
//----------------------------Euler.rotate()-----------------------
//rotates a euler a certain amount and returns the result
//should return a unique euler rotation (i.e. no 720 degree pitches :)
PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
{
float angle = 0.0f;
char *axis;
int x;
if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"euler.rotate():expected angle (float) and axis (x,y,z)");
}
if(!STREQ3(axis,"x","y","z")){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"euler.rotate(): expected axis to be 'x', 'y' or 'z'");
return EXPP_incr_ret( Py_None );
}
//covert to radians
angle *= ((float)Py_PI / 180);
for(x = 0; x < 3; x++) {
self->eul[x] *= ((float)Py_PI / 180);
}
euler_rot(self->eul, angle, *axis);
//convert back from radians
for(x = 0; x < 3; x++) {
self->eul[x] *= (180 / (float)Py_PI);
}
return (PyObject*)self;
}
//----------------------------dealloc()(internal) ------------------
//free the py_object
static void Euler_dealloc( EulerObject * self )
{
//only free py_data
if(self->data.py_data){
PyMem_Free(self->data.py_data);
}
/* since we own this memory... */
PyMem_Free( self->eul );
PyObject_DEL( self );
}
//----------------------------getattr()(internal) ------------------
//object.attribute access (get)
static PyObject *Euler_getattr( EulerObject * self, char *name )
{
int x;
if(STREQ(name,"x")){
return PyFloat_FromDouble(self->eul[0]);
}else if(STREQ(name, "y")){
return PyFloat_FromDouble(self->eul[1]);
}else if(STREQ(name, "z")){
return PyFloat_FromDouble(self->eul[2]);
if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
return PyFloat_FromDouble( self->eul[name[0] - 'x'] );
}
return Py_FindMethod( Euler_methods, ( PyObject * ) self, name );
}
//----------------------------setattr()(internal) ------------------
//object.attribute access (set)
static int Euler_setattr( EulerObject * self, char *name, PyObject * e )
{
PyObject *f = NULL;
float val;
f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number
if( !PyArg_Parse( e, "f", &val ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"euler.attribute = x: argument not a number\n");
}
"unable to parse float argument\n" );
if(STREQ(name,"x")){
self->eul[0] = PyFloat_AS_DOUBLE(f);
}else if(STREQ(name, "y")){
self->eul[1] = PyFloat_AS_DOUBLE(f);
}else if(STREQ(name, "z")){
self->eul[2] = PyFloat_AS_DOUBLE(f);
}else{
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_AttributeError,
"euler.attribute = x: unknown attribute\n");
}
Py_DECREF(f);
if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
self->eul[name[0] - 'x'] = val;
return 0;
} else
return -1;
}
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Euler_repr(EulerObject * self)
{
int i;
char buffer[48], str[1024];
BLI_strncpy(str,"[",1024);
for(i = 0; i < 3; i++){
if(i < (2)){
sprintf(buffer, "%.6f, ", self->eul[i]);
strcat(str,buffer);
}else{
sprintf(buffer, "%.6f", self->eul[i]);
strcat(str,buffer);
}
}
strcat(str, "](euler)");
return EXPP_incr_ret(PyString_FromString(str));
}
//---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------
//sequence length
static int Euler_len(EulerObject * self)
{
return 3;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
/* Eulers Sequence methods */
static PyObject *Euler_item( EulerObject * self, int i )
{
if( i < 0 || i >= 3 )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"euler[attribute]: array index out of range\n");
"array index out of range\n" );
return Py_BuildValue( "f", self->eul[i] );
}
//----------------------------object[]-------------------------
//sequence accessor (set)
static int Euler_ass_item(EulerObject * self, int i, PyObject * ob)
{
PyObject *f = NULL;
f = PyNumber_Float(ob);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"euler[attribute] = x: argument not a number\n");
}
if(i < 0 || i >= 3){
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_IndexError,
"euler[attribute] = x: array assignment index out of range\n");
}
self->eul[i] = PyFloat_AS_DOUBLE(f);
Py_DECREF(f);
return 0;
}
//----------------------------object[z:y]------------------------
//sequence slice (get)
static PyObject *Euler_slice( EulerObject * self, int begin, int end )
{
PyObject *list = NULL;
PyObject *list;
int count;
CLAMP(begin, 0, 3);
CLAMP(end, 0, 3);
begin = MIN2(begin,end);
if( begin < 0 )
begin = 0;
if( end > 3 )
end = 3;
if( begin > end )
begin = end;
list = PyList_New( end - begin );
for( count = begin; count < end; count++ ) {
PyList_SetItem( list, count - begin,
PyFloat_FromDouble( self->eul[count] ) );
}
return list;
}
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Euler_ass_slice(EulerObject * self, int begin, int end,
PyObject * seq)
static int Euler_ass_item( EulerObject * self, int i, PyObject * ob )
{
int i, y, size = 0;
float eul[3];
if( i < 0 || i >= 3 )
return EXPP_ReturnIntError( PyExc_IndexError,
"array assignment index out of range\n" );
CLAMP(begin, 0, 3);
CLAMP(end, 0, 3);
begin = MIN2(begin,end);
if( !PyNumber_Check( ob ) )
return EXPP_ReturnIntError( PyExc_IndexError,
"Euler member must be a number\n" );
size = PySequence_Length(seq);
if(size != (end - begin)){
if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
return EXPP_ReturnIntError( PyExc_TypeError,
"euler[begin:end] = []: size mismatch in slice assignment\n");
}
for (i = 0; i < size; i++) {
PyObject *e, *f;
e = PySequence_GetItem(seq, i);
if (e == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError,
"euler[begin:end] = []: unable to read sequence\n");
}
f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number
Py_DECREF(e);
return EXPP_ReturnIntError(PyExc_TypeError,
"euler[begin:end] = []: sequence argument not a number\n");
}
eul[i] = PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,e);
}
//parsed well - now set in vector
for(y = 0; y < 3; y++){
self->eul[begin + y] = eul[y];
"int or float expected\n" );
} else {
self->eul[i] = ( float ) PyFloat_AsDouble( ob );
}
return 0;
}
//-----------------PROTCOL DECLARATIONS--------------------------
static int Euler_ass_slice( EulerObject * self, int begin, int end,
PyObject * seq )
{
int count, z;
if( begin < 0 )
begin = 0;
if( end > 3 )
end = 3;
if( begin > end )
begin = end;
if( !PySequence_Check( seq ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"illegal argument type for built-in operation\n" );
if( PySequence_Length( seq ) != ( end - begin ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"size mismatch in slice assignment\n" );
z = 0;
for( count = begin; count < end; count++ ) {
PyObject *ob = PySequence_GetItem( seq, z );
z++;
if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
Py_DECREF( ob );
return -1;
} else {
if( !PyArg_Parse( ob, "f", &self->eul[count] ) ) {
Py_DECREF( ob );
return -1;
}
}
}
return 0;
}
static PyObject *Euler_repr( EulerObject * self )
{
int i, maxindex = 3 - 1;
char ftoa[24];
PyObject *str1, *str2;
str1 = PyString_FromString( "[" );
for( i = 0; i < maxindex; i++ ) {
sprintf( ftoa, "%.4f, ", self->eul[i] );
str2 = PyString_FromString( ftoa );
if( !str1 || !str2 )
goto error;
PyString_ConcatAndDel( &str1, str2 );
}
sprintf( ftoa, "%.4f]\n", self->eul[maxindex] );
str2 = PyString_FromString( ftoa );
if( !str1 || !str2 )
goto error;
PyString_ConcatAndDel( &str1, str2 );
if( str1 )
return str1;
error:
Py_XDECREF( str1 );
Py_XDECREF( str2 );
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"couldn't create PyString!\n" );
}
static PySequenceMethods Euler_SeqMethods = {
(inquiry) Euler_len, /* sq_length */
( inquiry ) 0, /* sq_length */
( binaryfunc ) 0, /* sq_concat */
( intargfunc ) 0, /* sq_repeat */
( intargfunc ) Euler_item, /* sq_item */
@@ -358,7 +307,7 @@ static PySequenceMethods Euler_SeqMethods = {
( intobjargproc ) Euler_ass_item, /* sq_ass_item */
( intintobjargproc ) Euler_ass_slice, /* sq_ass_slice */
};
//------------------PY_OBECT DEFINITION--------------------------
PyTypeObject euler_Type = {
PyObject_HEAD_INIT( NULL )
0, /*ob_size */
@@ -374,29 +323,27 @@ PyTypeObject euler_Type = {
0, /*tp_as_number */
&Euler_SeqMethods, /*tp_as_sequence */
};
//------------------------newEulerObject (internal)-------------
//creates a new euler object
/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
(i.e. it was allocated elsewhere by MEM_mallocN())
pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
(i.e. it must be created here with PyMEM_malloc())*/
PyObject *newEulerObject(float *eul, int type)
PyObject *newEulerObject( float *eul )
{
EulerObject *self;
int x;
euler_Type.ob_type = &PyType_Type;
self = PyObject_NEW(EulerObject, &euler_Type);
self->data.blend_data = NULL;
self->data.py_data = NULL;
if(type == Py_WRAP){
self->data.blend_data = eul;
self->eul = self->data.blend_data;
}else if (type == Py_NEW){
self->data.py_data = PyMem_Malloc(3 * sizeof(float));
self->eul = self->data.py_data;
if(!eul) { //new empty
self = PyObject_NEW( EulerObject, &euler_Type );
/*
we own the self->eul memory and will free it later.
if we received an input arg, copy to our internal array
*/
self->eul = PyMem_Malloc( 3 * sizeof( float ) );
if( ! self->eul )
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"newEulerObject:PyMem_Malloc failed" );
if( !eul ) {
for( x = 0; x < 3; x++ ) {
self->eul[x] = 0.0f;
}
@@ -405,9 +352,6 @@ PyObject *newEulerObject(float *eul, int type)
self->eul[x] = eul[x];
}
}
}else{ //bad type
return NULL;
}
return (PyObject *) EXPP_incr_ret((PyObject *)self);
}
return ( PyObject * ) self;
}

View File

@@ -1,3 +1,4 @@
/*
* $Id$
*
@@ -34,28 +35,33 @@
#ifndef EXPP_euler_h
#define EXPP_euler_h
#include "Python.h"
#include "gen_utils.h"
#include "Types.h"
#include <BLI_arithb.h>
#include "quat.h"
#include "matrix.h"
#include "BKE_utildefines.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*****************************/
// Euler Python Object
/*****************************/
#define EulerObject_Check(v) ((v)->ob_type == &euler_Type)
typedef struct {
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
float *eul; //1D array of data (alias)
PyObject_VAR_HEAD float *eul;
} EulerObject;
/*struct data contains a pointer to the actual data that the
object uses. It can use either PyMem allocated data (which will
be stored in py_data) or be a wrapper for data allocated through
blender (stored in blend_data). This is an either/or struct not both*/
//prototypes
PyObject *newEulerObject( float *eul );
PyObject *Euler_Zero( EulerObject * self );
PyObject *Euler_Unique( EulerObject * self );
PyObject *Euler_ToMatrix( EulerObject * self );
PyObject *Euler_ToQuat( EulerObject * self );
PyObject *Euler_Rotate( EulerObject * self, PyObject *args );
PyObject *newEulerObject( float *eul, int type );
#endif /* EXPP_euler_h */

View File

@@ -120,31 +120,6 @@ int EXPP_ReturnIntError( PyObject * type, char *error_msg )
/* Description: This function increments the reference count of the given */
/* Python object (usually Py_None) and returns it. */
/*****************************************************************************/
void EXPP_incr2( PyObject * ob1, PyObject * ob2 )
{
Py_INCREF( ob1 );
Py_INCREF( ob2 );
}
void EXPP_incr3( PyObject * ob1, PyObject * ob2, PyObject * ob3 )
{
Py_INCREF( ob1 );
Py_INCREF( ob2 );
Py_INCREF( ob3 );
}
void EXPP_decr2( PyObject * ob1, PyObject * ob2 )
{
Py_DECREF( ob1 );
Py_DECREF( ob2 );
}
void EXPP_decr3( PyObject * ob1, PyObject * ob2, PyObject * ob3 )
{
Py_DECREF( ob1 );
Py_DECREF( ob2 );
Py_DECREF( ob3 );
}
PyObject *EXPP_incr_ret( PyObject * object )
{

View File

@@ -50,8 +50,6 @@
#include <DNA_listBase.h>
#define Py_PI 3.14159265358979323846
#define Py_WRAP 1024
#define Py_NEW 2048
/*
Py_RETURN_NONE
@@ -74,10 +72,6 @@ char *event_to_name( short event );
float EXPP_ClampFloat( float value, float min, float max );
int EXPP_ClampInt( int value, int min, int max );
void EXPP_incr2( PyObject * ob1, PyObject * ob2 );
void EXPP_incr3( PyObject * ob1, PyObject * ob2, PyObject * ob3 );
void EXPP_decr2( PyObject * ob1, PyObject * ob2 );
void EXPP_decr3( PyObject * ob1, PyObject * ob2, PyObject * ob3 );
PyObject *EXPP_incr_ret( PyObject * object );
PyObject *EXPP_incr_ret_True(void);
PyObject *EXPP_incr_ret_False(void);

File diff suppressed because it is too large Load Diff

View File

@@ -33,31 +33,37 @@
#ifndef EXPP_matrix_h
#define EXPP_matrix_h
#define MatrixObject_Check(v) ((v)->ob_type == &matrix_Type)
#include "Python.h"
#include "BLI_arithb.h"
#include "vector.h"
#include "gen_utils.h"
#include "Types.h"
#include "quat.h"
#include "euler.h"
#define Matrix_CheckPyObject(v) ((v)->ob_type == &matrix_Type)
/*****************************/
/* Matrix Python Object */
/*****************************/
typedef float **ptRow;
typedef struct _Matrix {
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
ptRow matrix; //ptr to the contigPtr (accessor)
float *contigPtr; //1D array of data (alias)
PyObject_VAR_HEAD /* standard python macro */
ptRow matrix;
float *contigPtr;
int rowSize;
int colSize;
PyObject *coerced_object;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
} MatrixObject;
/*coerced_object is a pointer to the object that it was
coerced from when a dummy vector needs to be created from
the coerce() function for numeric protocol operations*/
/*struct data contains a pointer to the actual data that the
object uses. It can use either PyMem allocated data (which will
be stored in py_data) or be a wrapper for data allocated through
blender (stored in blend_data). This is an either/or struct not both*/
//prototypes
/*****************************************************************************/
/* Python API function prototypes. */
/*****************************************************************************/
PyObject *newMatrixObject( float *mat, int rowSize, int colSize );
PyObject *Matrix_Zero( MatrixObject * self );
PyObject *Matrix_Identity( MatrixObject * self );
PyObject *Matrix_Transpose( MatrixObject * self );
@@ -68,6 +74,5 @@ PyObject *Matrix_RotationPart( MatrixObject * self );
PyObject *Matrix_Resize4x4( MatrixObject * self );
PyObject *Matrix_toEuler( MatrixObject * self );
PyObject *Matrix_toQuat( MatrixObject * self );
PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type);
#endif /* EXPP_matrix_H */

View File

@@ -29,71 +29,86 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include <BLI_arithb.h>
#include <BKE_utildefines.h>
#include "Mathutils.h"
#include "gen_utils.h"
#include "quat.h"
//-------------------------DOC STRINGS ---------------------------
char Quaternion_Identity_doc[] = "() - set the quaternion to it's identity (1, vector)";
char Quaternion_Negate_doc[] = "() - set all values in the quaternion to their negative";
//doc strings
char Quaternion_Identity_doc[] =
"() - set the quaternion to it's identity (1, vector)";
char Quaternion_Negate_doc[] =
"() - set all values in the quaternion to their negative";
char Quaternion_Conjugate_doc[] = "() - set the quaternion to it's conjugate";
char Quaternion_Inverse_doc[] = "() - set the quaternion to it's inverse";
char Quaternion_Normalize_doc[] = "() - normalize the vector portion of the quaternion";
char Quaternion_ToEuler_doc[] = "() - return a euler rotation representing the quaternion";
char Quaternion_ToMatrix_doc[] = "() - return a rotation matrix representing the quaternion";
//-----------------------METHOD DEFINITIONS ----------------------
char Quaternion_Normalize_doc[] =
"() - normalize the vector portion of the quaternion";
char Quaternion_ToEuler_doc[] =
"() - return a euler rotation representing the quaternion";
char Quaternion_ToMatrix_doc[] =
"() - return a rotation matrix representing the quaternion";
//methods table
struct PyMethodDef Quaternion_methods[] = {
{"identity", (PyCFunction) Quaternion_Identity, METH_NOARGS, Quaternion_Identity_doc},
{"negate", (PyCFunction) Quaternion_Negate, METH_NOARGS, Quaternion_Negate_doc},
{"conjugate", (PyCFunction) Quaternion_Conjugate, METH_NOARGS, Quaternion_Conjugate_doc},
{"inverse", (PyCFunction) Quaternion_Inverse, METH_NOARGS, Quaternion_Inverse_doc},
{"normalize", (PyCFunction) Quaternion_Normalize, METH_NOARGS, Quaternion_Normalize_doc},
{"toEuler", (PyCFunction) Quaternion_ToEuler, METH_NOARGS, Quaternion_ToEuler_doc},
{"toMatrix", (PyCFunction) Quaternion_ToMatrix, METH_NOARGS, Quaternion_ToMatrix_doc},
{"identity", ( PyCFunction ) Quaternion_Identity, METH_NOARGS,
Quaternion_Identity_doc},
{"negate", ( PyCFunction ) Quaternion_Negate, METH_NOARGS,
Quaternion_Negate_doc},
{"conjugate", ( PyCFunction ) Quaternion_Conjugate, METH_NOARGS,
Quaternion_Conjugate_doc},
{"inverse", ( PyCFunction ) Quaternion_Inverse, METH_NOARGS,
Quaternion_Inverse_doc},
{"normalize", ( PyCFunction ) Quaternion_Normalize, METH_NOARGS,
Quaternion_Normalize_doc},
{"toEuler", ( PyCFunction ) Quaternion_ToEuler, METH_NOARGS,
Quaternion_ToEuler_doc},
{"toMatrix", ( PyCFunction ) Quaternion_ToMatrix, METH_NOARGS,
Quaternion_ToMatrix_doc},
{NULL, NULL, 0, NULL}
};
//-----------------------------METHODS------------------------------
//----------------------------Quaternion.toEuler()------------------
//return the quat as a euler
/* ****** prototypes ********** */
PyObject *Quaternion_add( PyObject * q1, PyObject * q2 );
PyObject *Quaternion_sub( PyObject * q1, PyObject * q2 );
PyObject *Quaternion_mul( PyObject * q1, PyObject * q2 );
int Quaternion_coerce( PyObject ** q1, PyObject ** q2 );
/*****************************/
// Quaternion Python Object
/*****************************/
PyObject *Quaternion_ToEuler( QuaternionObject * self )
{
float eul[3];
float *eul;
int x;
eul = PyMem_Malloc( 3 * sizeof( float ) );
QuatToEul( self->quat, eul );
for( x = 0; x < 3; x++ ) {
eul[x] *= (180 / (float)Py_PI);
eul[x] *= ( float ) ( 180 / Py_PI );
}
if(self->data.blend_data)
return newEulerObject(eul, Py_WRAP);
else
return newEulerObject(eul, Py_NEW);
return ( PyObject * ) newEulerObject( eul );
}
//----------------------------Quaternion.toMatrix()------------------
//return the quat as a matrix
PyObject *Quaternion_ToMatrix( QuaternionObject * self )
{
float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
float *mat;
mat = PyMem_Malloc( 3 * 3 * sizeof( float ) );
QuatToMat3( self->quat, ( float ( * )[3] ) mat );
if(self->data.blend_data)
return newMatrixObject(mat, 3, 3, Py_WRAP);
else
return newMatrixObject(mat, 3, 3, Py_NEW);
return ( PyObject * ) newMatrixObject( mat, 3, 3 );
}
//----------------------------Quaternion.normalize()----------------
//normalize the axis of rotation of [theta,vector]
PyObject *Quaternion_Normalize( QuaternionObject * self )
{
NormalQuat( self->quat );
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------Quaternion.inverse()------------------
//invert the quat
PyObject *Quaternion_Inverse( QuaternionObject * self )
{
double mag = 0.0f;
float mag = 0.0f;
int x;
for( x = 1; x < 4; x++ ) {
@@ -102,15 +117,14 @@ PyObject *Quaternion_Inverse(QuaternionObject * self)
for( x = 0; x < 4; x++ ) {
mag += ( self->quat[x] * self->quat[x] );
}
mag = sqrt(mag);
mag = ( float ) sqrt( mag );
for( x = 0; x < 4; x++ ) {
self->quat[x] /= ( mag * mag );
}
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------Quaternion.identity()-----------------
//generate the identity quaternion
PyObject *Quaternion_Identity( QuaternionObject * self )
{
self->quat[0] = 1.0;
@@ -118,401 +132,361 @@ PyObject *Quaternion_Identity(QuaternionObject * self)
self->quat[2] = 0.0;
self->quat[3] = 0.0;
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------Quaternion.negate()-------------------
//negate the quat
PyObject *Quaternion_Negate( QuaternionObject * self )
{
int x;
for( x = 0; x < 4; x++ ) {
self->quat[x] = -self->quat[x];
}
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------Quaternion.conjugate()----------------
//negate the vector part
PyObject *Quaternion_Conjugate( QuaternionObject * self )
{
int x;
for( x = 1; x < 4; x++ ) {
self->quat[x] = -self->quat[x];
}
return (PyObject*)self;
return EXPP_incr_ret( Py_None );
}
//----------------------------dealloc()(internal) ------------------
//free the py_object
static void Quaternion_dealloc( QuaternionObject * self )
{
//only free py_data
if(self->data.py_data){
PyMem_Free(self->data.py_data);
}
PyMem_Free( self->quat );
PyObject_DEL( self );
}
//----------------------------getattr()(internal) ------------------
//object.attribute access (get)
static PyObject *Quaternion_getattr( QuaternionObject * self, char *name )
{
int x;
double mag = 0.0f;
float vec[3];
float *vec = NULL;
int x;
PyObject *retval;
if(STREQ(name,"w")){
return PyFloat_FromDouble(self->quat[0]);
}else if(STREQ(name, "x")){
return PyFloat_FromDouble(self->quat[1]);
}else if(STREQ(name, "y")){
return PyFloat_FromDouble(self->quat[2]);
}else if(STREQ(name, "z")){
return PyFloat_FromDouble(self->quat[3]);
if( ELEM4( name[0], 'w', 'x', 'y', 'z' ) && name[1] == 0 ) {
return PyFloat_FromDouble( self->quat[name[0] - 'w'] );
}
if(STREQ(name, "magnitude")) {
if( strcmp( name, "magnitude" ) == 0 ) {
for( x = 0; x < 4; x++ ) {
mag += self->quat[x] * self->quat[x];
}
mag = sqrt(mag);
mag = ( float ) sqrt( mag );
return PyFloat_FromDouble( mag );
}
if(STREQ(name, "angle")) {
if( strcmp( name, "angle" ) == 0 ) {
mag = self->quat[0];
mag = 2 * ( acos( mag ) );
mag *= ( 180 / Py_PI );
return PyFloat_FromDouble( mag );
}
if(STREQ(name, "axis")) {
mag = self->quat[0] * (Py_PI / 180);
if( strcmp( name, "axis" ) == 0 ) {
mag = ( double ) ( self->quat[0] * ( Py_PI / 180 ) );
mag = 2 * ( acos( mag ) );
mag = sin( mag / 2 );
vec = PyMem_Malloc( 3 * sizeof( float ) );
for( x = 0; x < 3; x++ ) {
vec[x] = (self->quat[x + 1] / mag);
vec[x] = ( self->quat[x + 1] / ( ( float ) ( mag ) ) );
}
Normalise( vec );
return (PyObject *) newVectorObject(vec, 3, Py_NEW);
retval = ( PyObject * ) newVectorObject( vec, 3 );
PyMem_Free( vec );
return retval;
}
return Py_FindMethod( Quaternion_methods, ( PyObject * ) self, name );
}
//----------------------------setattr()(internal) ------------------
//object.attribute access (set)
static int Quaternion_setattr(QuaternionObject * self, char *name, PyObject * q)
static int Quaternion_setattr( QuaternionObject * self, char *name,
PyObject * v )
{
PyObject *f = NULL;
float val;
f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number
if( !PyFloat_Check( v ) && !PyInt_Check( v ) ) {
return EXPP_ReturnIntError( PyExc_TypeError,
"quaternion.attribute = x: argument not a number\n");
}
if(STREQ(name,"w")){
self->quat[0] = PyFloat_AS_DOUBLE(f);
}else if(STREQ(name, "x")){
self->quat[1] = PyFloat_AS_DOUBLE(f);
}else if(STREQ(name, "y")){
self->quat[2] = PyFloat_AS_DOUBLE(f);
}else if(STREQ(name, "z")){
self->quat[3] = PyFloat_AS_DOUBLE(f);
"int or float expected\n" );
} else {
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_AttributeError,
"quaternion.attribute = x: unknown attribute\n");
if( !PyArg_Parse( v, "f", &val ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"unable to parse float argument\n" );
}
if( ELEM4( name[0], 'w', 'x', 'y', 'z' ) && name[1] == 0 ) {
self->quat[name[0] - 'w'] = val;
} else
return -1;
Py_DECREF(f);
return 0;
}
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Quaternion_repr(QuaternionObject * self)
{
int i;
char buffer[48], str[1024];
BLI_strncpy(str,"[",1024);
for(i = 0; i < 4; i++){
if(i < (3)){
sprintf(buffer, "%.6f, ", self->quat[i]);
strcat(str,buffer);
}else{
sprintf(buffer, "%.6f", self->quat[i]);
strcat(str,buffer);
}
}
strcat(str, "](quaternion)");
return EXPP_incr_ret(PyString_FromString(str));
}
//---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------
//sequence length
static int Quaternion_len(QuaternionObject * self)
{
return 4;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
/* Quaternions Sequence methods */
static PyObject *Quaternion_item( QuaternionObject * self, int i )
{
if( i < 0 || i >= 4 )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"quaternion[attribute]: array index out of range\n");
"array index out of range\n" );
return Py_BuildValue( "f", self->quat[i] );
}
//----------------------------object[]-------------------------
//sequence accessor (set)
static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
static PyObject *Quaternion_slice( QuaternionObject * self, int begin,
int end )
{
PyObject *f = NULL;
f = PyNumber_Float(ob);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[attribute] = x: argument not a number\n");
}
if(i < 0 || i >= 4){
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_IndexError,
"quaternion[attribute] = x: array assignment index out of range\n");
}
self->quat[i] = PyFloat_AS_DOUBLE(f);
Py_DECREF(f);
return 0;
}
//----------------------------object[z:y]------------------------
//sequence slice (get)
static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
{
PyObject *list = NULL;
PyObject *list;
int count;
CLAMP(begin, 0, 4);
CLAMP(end, 0, 4);
begin = MIN2(begin,end);
if( begin < 0 )
begin = 0;
if( end > 4 )
end = 4;
if( begin > end )
begin = end;
list = PyList_New( end - begin );
for( count = begin; count < end; count++ ) {
PyList_SetItem( list, count - begin,
PyFloat_FromDouble( self->quat[count] ) );
}
return list;
}
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Quaternion_ass_item( QuaternionObject * self, int i, PyObject * ob )
{
if( i < 0 || i >= 4 )
return EXPP_ReturnIntError( PyExc_IndexError,
"array assignment index out of range\n" );
if( !PyNumber_Check( ob ) )
return EXPP_ReturnIntError( PyExc_IndexError,
"Quaternion member must be a number\n" );
if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
return EXPP_ReturnIntError( PyExc_TypeError,
"int or float expected\n" );
} else {
self->quat[i] = ( float ) PyFloat_AsDouble( ob );
}
return 0;
}
static int Quaternion_ass_slice( QuaternionObject * self, int begin, int end,
PyObject * seq )
{
int i, y, size = 0;
float quat[4];
int count, z;
CLAMP(begin, 0, 4);
CLAMP(end, 0, 4);
begin = MIN2(begin,end);
if( begin < 0 )
begin = 0;
if( end > 4 )
end = 4;
if( begin > end )
begin = end;
size = PySequence_Length(seq);
if(size != (end - begin)){
if( !PySequence_Check( seq ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"quaternion[begin:end] = []: size mismatch in slice assignment\n");
}
for (i = 0; i < size; i++) {
PyObject *q, *f;
q = PySequence_GetItem(seq, i);
if (q == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError,
"quaternion[begin:end] = []: unable to read sequence\n");
}
f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number
Py_DECREF(q);
"illegal argument type for built-in operation\n" );
if( PySequence_Length( seq ) != ( end - begin ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"quaternion[begin:end] = []: sequence argument not a number\n");
"size mismatch in slice assignment\n" );
z = 0;
for( count = begin; count < end; count++ ) {
PyObject *ob = PySequence_GetItem( seq, z );
z++;
if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
Py_DECREF( ob );
return -1;
} else {
if( !PyArg_Parse( ob, "f", &self->quat[count] ) ) {
Py_DECREF( ob );
return -1;
}
quat[i] = PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,q);
}
//parsed well - now set in vector
for(y = 0; y < size; y++){
self->quat[begin + y] = quat[y];
}
return 0;
}
//------------------------NUMERIC PROTOCOLS----------------------
//------------------------obj + obj------------------------------
//addition
static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
static PyObject *Quaternion_repr( QuaternionObject * self )
{
int x;
float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
int i, maxindex = 4 - 1;
char ftoa[24];
PyObject *str1, *str2;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
str1 = PyString_FromString( "[" );
if(quat1->coerced_object || quat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Quaternion addition: arguments not valid for this operation....\n");
}
for(x = 0; x < 4; x++) {
quat[x] = quat1->quat[x] + quat2->quat[x];
for( i = 0; i < maxindex; i++ ) {
sprintf( ftoa, "%.4f, ", self->quat[i] );
str2 = PyString_FromString( ftoa );
if( !str1 || !str2 )
goto error;
PyString_ConcatAndDel( &str1, str2 );
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
sprintf( ftoa, "%.4f]", self->quat[maxindex] );
str2 = PyString_FromString( ftoa );
if( !str1 || !str2 )
goto error;
PyString_ConcatAndDel( &str1, str2 );
if( str1 )
return str1;
error:
Py_XDECREF( str1 );
Py_XDECREF( str2 );
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"couldn't create PyString!\n" );
}
//------------------------obj - obj------------------------------
//subtraction
static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
PyObject *Quaternion_add( PyObject * q1, PyObject * q2 )
{
float *quat = NULL;
PyObject *retval;
int x;
float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
if( ( !QuaternionObject_Check( q1 ) )
|| ( !QuaternionObject_Check( q2 ) ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"unsupported type for this operation\n" );
if( ( ( QuaternionObject * ) q1 )->flag > 0
|| ( ( QuaternionObject * ) q2 )->flag > 0 )
return EXPP_ReturnPyObjError( PyExc_ArithmeticError,
"cannot add a scalar and a quat\n" );
if(quat1->coerced_object || quat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Quaternion addition: arguments not valid for this operation....\n");
}
quat = PyMem_Malloc( 4 * sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] = quat1->quat[x] - quat2->quat[x];
quat[x] =
( ( ( QuaternionObject * ) q1 )->quat[x] ) +
( ( ( QuaternionObject * ) q2 )->quat[x] );
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
}
//------------------------obj * obj------------------------------
//mulplication
static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
PyObject *Quaternion_sub( PyObject * q1, PyObject * q2 )
{
float *quat = NULL;
PyObject *retval;
int x;
float quat[4], scalar, newVec[3];
double dot = 0.0f;
QuaternionObject *quat1 = NULL, *quat2 = NULL;
PyObject *f = NULL;
VectorObject *vec = NULL;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
if( ( !QuaternionObject_Check( q1 ) )
|| ( !QuaternionObject_Check( q2 ) ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"unsupported type for this operation\n" );
if( ( ( QuaternionObject * ) q1 )->flag > 0
|| ( ( QuaternionObject * ) q2 )->flag > 0 )
return EXPP_ReturnPyObjError( PyExc_ArithmeticError,
"cannot subtract a scalar and a quat\n" );
if(quat1->coerced_object){
if (PyFloat_Check(quat1->coerced_object) ||
PyInt_Check(quat1->coerced_object)){ // FLOAT/INT * QUAT
f = PyNumber_Float(quat1->coerced_object);
if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
}
scalar = PyFloat_AS_DOUBLE(f);
quat = PyMem_Malloc( 4 * sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] = quat2->quat[x] * scalar;
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}
}else{
if(quat2->coerced_object){
if (PyFloat_Check(quat2->coerced_object) ||
PyInt_Check(quat2->coerced_object)){ // QUAT * FLOAT/INT
f = PyNumber_Float(quat2->coerced_object);
if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
}
scalar = PyFloat_AS_DOUBLE(f);
for(x = 0; x < 4; x++) {
quat[x] = quat1->quat[x] * scalar;
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}else if(VectorObject_Check(quat2->coerced_object)){ //QUAT * VEC
vec = (VectorObject*)EXPP_incr_ret(quat2->coerced_object);
if(vec->size != 3){
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: only 3D vector rotations currently supported\n");
}
newVec[0] = quat1->quat[0]*quat1->quat[0]*vec->vec[0] +
2*quat1->quat[2]*quat1->quat[0]*vec->vec[2] -
2*quat1->quat[3]*quat1->quat[0]*vec->vec[1] +
quat1->quat[1]*quat1->quat[1]*vec->vec[0] +
2*quat1->quat[2]*quat1->quat[1]*vec->vec[1] +
2*quat1->quat[3]*quat1->quat[1]*vec->vec[2] -
quat1->quat[3]*quat1->quat[3]*vec->vec[0] -
quat1->quat[2]*quat1->quat[2]*vec->vec[0];
newVec[1] = 2*quat1->quat[1]*quat1->quat[2]*vec->vec[0] +
quat1->quat[2]*quat1->quat[2]*vec->vec[1] +
2*quat1->quat[3]*quat1->quat[2]*vec->vec[2] +
2*quat1->quat[0]*quat1->quat[3]*vec->vec[0] -
quat1->quat[3]*quat1->quat[3]*vec->vec[1] +
quat1->quat[0]*quat1->quat[0]*vec->vec[1] -
2*quat1->quat[1]*quat1->quat[0]*vec->vec[2] -
quat1->quat[1]*quat1->quat[1]*vec->vec[1];
newVec[2] = 2*quat1->quat[1]*quat1->quat[3]*vec->vec[0] +
2*quat1->quat[2]*quat1->quat[3]*vec->vec[1] +
quat1->quat[3]*quat1->quat[3]*vec->vec[2] -
2*quat1->quat[0]*quat1->quat[2]*vec->vec[0] -
quat1->quat[2]*quat1->quat[2]*vec->vec[2] +
2*quat1->quat[0]*quat1->quat[1]*vec->vec[1] -
quat1->quat[1]*quat1->quat[1]*vec->vec[2] +
quat1->quat[0]*quat1->quat[0]*vec->vec[2];
EXPP_decr3((PyObject*)quat1, (PyObject*)quat2, (PyObject*)vec);
return newVectorObject(newVec,3,Py_NEW);
}
}else{ //QUAT * QUAT (dot product)
for(x = 0; x < 4; x++) {
dot += quat1->quat[x] * quat1->quat[x];
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return PyFloat_FromDouble(dot);
quat[x] =
( ( ( QuaternionObject * ) q1 )->quat[x] ) -
( ( ( QuaternionObject * ) q2 )->quat[x] );
}
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
}
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
PyObject *Quaternion_mul( PyObject * q1, PyObject * q2 )
{
float *quat = NULL;
PyObject *retval;
int x;
if( ( !QuaternionObject_Check( q1 ) )
|| ( !QuaternionObject_Check( q2 ) ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
"unsupported type for this operation\n" );
if( ( ( QuaternionObject * ) q1 )->flag == 0
&& ( ( QuaternionObject * ) q2 )->flag == 0 )
return EXPP_ReturnPyObjError( PyExc_ArithmeticError,
"please use the dot or cross product to multiply quaternions\n" );
quat = PyMem_Malloc( 4 * sizeof( float ) );
//scalar mult by quat
for( x = 0; x < 4; x++ ) {
quat[x] =
( ( QuaternionObject * ) q1 )->quat[x] *
( ( QuaternionObject * ) q2 )->quat[x];
}
//------------------------coerce(obj, obj)-----------------------
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
}
//coercion of unknown types to type QuaternionObject for numeric protocols
/*Coercion() is called whenever a math operation has 2 operands that
it doesn't understand how to evaluate. 2+Matrix for example. We want to
evaluate some of these operations like: (vector * 2), however, for math
to proceed, the unknown operand must be cast to a type that python math will
understand. (e.g. in the case above case, 2 must be cast to a vector and
then call vector.multiply(vector, scalar_cast_as_vector)*/
static int Quaternion_coerce(PyObject ** q1, PyObject ** q2)
int Quaternion_coerce( PyObject ** q1, PyObject ** q2 )
{
long *tempI = NULL;
double *tempF = NULL;
float *quat = NULL;
int x;
float quat[4];
PyObject *coerced = NULL;
if(!QuaternionObject_Check(*q2)) {
if(VectorObject_Check(*q2) || PyFloat_Check(*q2) || PyInt_Check(*q2)) {
coerced = EXPP_incr_ret(*q2);
*q2 = newQuaternionObject(NULL,Py_NEW);
((QuaternionObject*)*q2)->coerced_object = coerced;
if( QuaternionObject_Check( *q1 ) ) {
if( QuaternionObject_Check( *q2 ) ) { //two Quaternions
Py_INCREF( *q1 );
Py_INCREF( *q2 );
return 0;
} else {
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion.coerce(): unknown operand - can't coerce for numeric protocols\n");
if( PyNumber_Check( *q2 ) ) {
if( PyInt_Check( *q2 ) ) { //cast scalar to Quaternion
tempI = PyMem_Malloc( 1 *
sizeof( long ) );
*tempI = PyInt_AsLong( *q2 );
quat = PyMem_Malloc( 4 *
sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] = ( float ) *tempI;
}
PyMem_Free( tempI );
*q2 = newQuaternionObject( quat );
PyMem_Free( quat );
( ( QuaternionObject * ) * q2 )->flag = 1; //int coercion
Py_INCREF( *q1 ); /* fixme: is this needed? */
return 0;
} else if( PyFloat_Check( *q2 ) ) { //cast scalar to Quaternion
tempF = PyMem_Malloc( 1 *
sizeof
( double ) );
*tempF = PyFloat_AsDouble( *q2 );
quat = PyMem_Malloc( 4 *
sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] = ( float ) *tempF;
}
EXPP_incr2(*q1, *q2);
PyMem_Free( tempF );
*q2 = newQuaternionObject( quat );
PyMem_Free( quat );
( ( QuaternionObject * ) * q2 )->flag = 2; //float coercion
Py_INCREF( *q1 ); /* fixme: is this needed? */
return 0;
}
//-----------------PROTCOL DECLARATIONS--------------------------
}
//unknown type or numeric cast failure
printf( "attempting quaternion operation with unsupported type...\n" );
Py_INCREF( *q1 ); /* fixme: is this needed? */
return 0; //operation will type check
}
} else {
printf( "numeric protocol failure...\n" );
return -1; //this should not occur - fail
}
return -1;
}
static PySequenceMethods Quaternion_SeqMethods = {
(inquiry) Quaternion_len, /* sq_length */
( inquiry ) 0, /* sq_length */
( binaryfunc ) 0, /* sq_concat */
( intargfunc ) 0, /* sq_repeat */
( intargfunc ) Quaternion_item, /* sq_item */
@@ -520,6 +494,7 @@ static PySequenceMethods Quaternion_SeqMethods = {
( intobjargproc ) Quaternion_ass_item, /* sq_ass_item */
( intintobjargproc ) Quaternion_ass_slice, /* sq_ass_slice */
};
static PyNumberMethods Quaternion_NumMethods = {
( binaryfunc ) Quaternion_add, /* __add__ */
( binaryfunc ) Quaternion_sub, /* __sub__ */
@@ -546,7 +521,7 @@ static PyNumberMethods Quaternion_NumMethods = {
( unaryfunc ) 0, /* __hex__ */
};
//------------------PY_OBECT DEFINITION--------------------------
PyTypeObject quaternion_Type = {
PyObject_HEAD_INIT( NULL )
0, /*ob_size */
@@ -562,38 +537,37 @@ PyTypeObject quaternion_Type = {
&Quaternion_NumMethods, /*tp_as_number */
&Quaternion_SeqMethods, /*tp_as_sequence */
};
//------------------------newQuaternionObject (internal)-------------
//creates a new quaternion object
/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
(i.e. it was allocated elsewhere by MEM_mallocN())
pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
(i.e. it must be created here with PyMEM_malloc())*/
PyObject *newQuaternionObject(float *quat, int type)
/** Creates a new quaternion object.
*
* Memory for a new quaternion is allocated. The quaternion copies the given
* list of parameters or initializes to the identity, if a <code>NULL</code>
* pointer is given as parameter. The memory will be freed in the dealloc
* routine.
*
* @param quat Pointer to a list of floats for the quanternion parameters w, x, y, z.
* @return Quaternion Python object.
* @see Quaternion_Identity
*/
PyObject *newQuaternionObject( float *quat )
{
QuaternionObject *self;
int x;
quaternion_Type.ob_type = &PyType_Type;
self = PyObject_NEW(QuaternionObject, &quaternion_Type);
self->data.blend_data = NULL;
self->data.py_data = NULL;
self->coerced_object = NULL;
if(type == Py_WRAP){
self->data.blend_data = quat;
self->quat = self->data.blend_data;
}else if (type == Py_NEW){
self->data.py_data = PyMem_Malloc(4 * sizeof(float));
self->quat = self->data.py_data;
if(!quat) { //new empty
self = PyObject_NEW( QuaternionObject, &quaternion_Type );
self->quat = PyMem_Malloc( 4 * sizeof( float ) );
if( !quat ) {
Quaternion_Identity(self);
} else {
for( x = 0; x < 4; x++ ) {
self->quat[x] = quat[x];
}
}
}else{ //bad type
return NULL;
}
return (PyObject *) EXPP_incr_ret((PyObject *)self);
self->flag = 0;
return ( PyObject * ) self;
}

View File

@@ -34,27 +34,34 @@
#ifndef EXPP_quat_h
#define EXPP_quat_h
#include "Python.h"
#include "gen_utils.h"
#include "Types.h"
#include <BLI_arithb.h>
#include "euler.h"
#include "matrix.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*****************************/
// Quaternion Python Object
/*****************************/
#define QuaternionObject_Check(v) ((v)->ob_type == &quaternion_Type)
typedef struct {
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
float *quat; //1D array of data (alias)
PyObject *coerced_object;
PyObject_VAR_HEAD float *quat;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
} QuaternionObject;
/*coerced_object is a pointer to the object that it was
coerced from when a dummy vector needs to be created from
the coerce() function for numeric protocol operations*/
/*struct data contains a pointer to the actual data that the
object uses. It can use either PyMem allocated data (which will
be stored in py_data) or be a wrapper for data allocated through
blender (stored in blend_data). This is an either/or struct not both*/
//prototypes
PyObject *newQuaternionObject( float *quat );
PyObject *Quaternion_Identity( QuaternionObject * self );
PyObject *Quaternion_Negate( QuaternionObject * self );
PyObject *Quaternion_Conjugate( QuaternionObject * self );
@@ -62,6 +69,5 @@ PyObject *Quaternion_Inverse( QuaternionObject * self );
PyObject *Quaternion_Normalize( QuaternionObject * self );
PyObject *Quaternion_ToEuler( QuaternionObject * self );
PyObject *Quaternion_ToMatrix( QuaternionObject * self );
PyObject *newQuaternionObject( float *quat, int type );
#endif /* EXPP_quat_h */

File diff suppressed because it is too large Load Diff

View File

@@ -33,34 +33,40 @@
#ifndef EXPP_vector_h
#define EXPP_vector_h
#include "Python.h"
#include "gen_utils.h"
#include "Types.h"
#include "matrix.h"
#include "BKE_utildefines.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*****************************/
// Vector Python Object
/*****************************/
#define VectorObject_Check(v) ((v)->ob_type == &vector_Type)
typedef struct {
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
float *vec; //1D array of data (alias)
PyObject_VAR_HEAD float *vec;
int size;
PyObject *coerced_object;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
int delete_pymem; /* flag to delete the memory vec points at */
} VectorObject;
/*coerced_object is a pointer to the object that it was
coerced from when a dummy vector needs to be created from
the coerce() function for numeric protocol operations*/
/*struct data contains a pointer to the actual data that the
object uses. It can use either PyMem allocated data (which will
be stored in py_data) or be a wrapper for data allocated through
blender (stored in blend_data). This is an either/or struct not both*/
//prototypes
PyObject *newVectorObject( float *vec, int size );
PyObject *newVectorProxy( float *vec, int size );
PyObject *Vector_Zero( VectorObject * self );
PyObject *Vector_Normalize( VectorObject * self );
PyObject *Vector_Negate( VectorObject * self );
PyObject *Vector_Resize2D( VectorObject * self );
PyObject *Vector_Resize3D( VectorObject * self );
PyObject *Vector_Resize4D( VectorObject * self );
PyObject *newVectorObject(float *vec, int size, int type);
#endif /* EXPP_vector_h */