Mathutils update

- also included is some fixes for preprocessor inclues and some clean up of the previous commit

-rewrite and bugfixes
  ----------------------------------
  Here's my changelog:
  -fixed Rand() so that it doesn't seed everytime and should generate better random numbers
  - changed a few error return types to something more appropriate
  - clean up of uninitialized variables & removal of unneccessary objects
  - NMesh returns wrapped vectors now
  - World returns wrapped matrices now
  - Object.getEuler() and Object.getBoundingBox() return Wrapped data when data is present
  - Object.getMatrix() returns wrapped data if it's worldspace, 'localspace' returns a new matrix
  - Vector, Euler, Mat, Quat, call all now internally wrap object without destroying internal datablocks
  - Removed memory allocation (unneeded) from all methods
  - Vector's resize methods are only applicable to new vectors not wrapped data.
  - Matrix(), Quat(), Euler(), Vector() now accepts ANY sequence list, including tuples, list, or a self object to copy - matrices accept multiple sequences
  - Fixed Slerp() so that it now works correctly values are clamped between 0 and 1
  - Euler.rotate does internal rotation now
  - Slice assignment now works better for all types
  - Vector * Vector and Quat * Quat are defined and return the DOT product
  - Mat * Vec and Vec * Mat are defined now
  - Moved #includes to .c file from headers. Also fixed prototypes in mathutils
  - Added new helper functions for incref'ing to genutils
  - Major cleanup of header files includes - include Mathutils.h for access to math types
  - matrix.toQuat() and .toEuler() now fixed take appropriate matrix sizes
  - Matrix() with no parameters now returns an identity matrix by default not a zero matrix
  - printf() now prints with 6 digits instead of 4
  - printf() now prints output with object descriptor
  - Matrices now support [x][y] assignment (e.g. matrix[x][y] = 5.4)
  - Matrix[index] = value now expectes a sequence not an integer. This will now set a ROW of the matrix through a sequence.  index cannot go above the row size of the matrix.
  - slice operations on matrices work with sequences now (rows of the matrix) example:  mymatrix[0:2] returns a list of 2 wrapped vectors with access to the matrix data.
  - slice assignment will no longer modify the data if the assignment operation fails
  - fixed error in matrix * scalar multiplication
  - euler.toMatrix(), toQuat() no longer causes "creep" from repeated use
  - Wrapped data will generate wrapped objects when toEuler(), toQuat(), toMatrix() is used
  - Quats can be created with angle/axis, axis/angle
  - 4x4 matrices can be multiplied by 3D vectors (by popular demand :))
  - vec *quat / quat * vec is now defined
  - vec.magnitude alias for vec.length
  - all self, internal methods return a pointer to self now so you can do print vector.internalmethod() or vector.internalmethod().nextmethod() (no more print matrix.inverse() returning 'none')
  - these methods have been deprecated (still functioning but suggested to use the corrected functionality):
    * CopyVec() - replaced by Vector() functionality
    * CopyMat() - replaced by Matrix() functionality
    * CopyQuat() - replace by Quaternion() functionality
    * CopyEuler() - replaced by Euler() functionality
    * RotateEuler() - replaced by Euler.rotate() funtionality
    * MatMultVec() - replaced by matrix * vector
    * VecMultMat() - replaced by vector * matrix
  -  New struct containers references to python object data or internally allocated blender data for wrapping
  * Explaination here:  math structs now function as a 'simple wrapper' or a 'py_object' - data that is created on the fly will now be a 'py_object' with its memory managed by python
  *    otherwise if the data is returned by blender's G.main then the math object is a 'simple wrapper' and data can be accessed directly from the struct just like other python objects.
This commit is contained in:
2005-07-14 03:34:56 +00:00
parent 1bfd0eae14
commit b89035906d
18 changed files with 3148 additions and 3730 deletions

View File

@@ -41,30 +41,27 @@
#include <io.h>
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
#include <BDR_editobject.h> /* exit_editmode() */
#include <BIF_usiblender.h>
#include <BLI_blenlib.h>
#include <BLO_writefile.h>
#include <BKE_exotic.h>
#include <BKE_global.h>
#include <BKE_packedFile.h>
#include <BKE_object.h>
#include <BKE_text.h>
#include <BPI_script.h>
#include <BSE_headerbuttons.h>
#include <DNA_ID.h>
#include <DNA_object_types.h>
#include <DNA_scene_types.h>
#include <DNA_screen_types.h> /* for SPACE_VIEW3D */
#include <DNA_space_types.h> /* for SPACE_VIEW3D */
#include <DNA_scriptlink_types.h>
#include <DNA_userdef_types.h>
#include <BKE_ipo.h>
#include <blendef.h>
#include "BKE_utildefines.h"
#include "BDR_editobject.h" /* exit_editmode() */
#include "BIF_usiblender.h"
#include "BLI_blenlib.h"
#include "BLO_writefile.h"
#include "BKE_exotic.h"
#include "BKE_global.h"
#include "BKE_packedFile.h"
#include "BKE_object.h"
#include "BKE_text.h"
#include "BPI_script.h"
#include "BSE_headerbuttons.h"
#include "DNA_ID.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h" /* for SPACE_VIEW3D */
#include "DNA_space_types.h" /* for SPACE_VIEW3D */
#include "DNA_scriptlink_types.h"
#include "DNA_userdef_types.h"
#include "BKE_ipo.h"
#include "blendef.h"
#include "EXPP_interface.h" /* for bpy_gethome() */
#include "gen_utils.h"

View File

@@ -32,26 +32,30 @@
#include "Bone.h"
#include <BKE_main.h>
#include <BKE_global.h>
#include <BKE_object.h>
#include <BKE_armature.h>
#include <BKE_library.h>
#include <BLI_blenlib.h>
#include <DNA_action_types.h>
#include <DNA_armature_types.h>
#include <DNA_ipo_types.h>
#include <BIF_poseobject.h>
#include <BKE_action.h>
#include <BSE_editaction.h>
#include <BKE_constraint.h>
#include <MEM_guardedalloc.h>
#include "BKE_main.h"
#include "BKE_global.h"
#include "BKE_object.h"
#include "BKE_armature.h"
#include "BKE_library.h"
#include "BLI_blenlib.h"
#include "DNA_action_types.h"
#include "DNA_armature_types.h"
#include "DNA_ipo_types.h"
#include "BIF_poseobject.h"
#include "BKE_action.h"
#include "BSE_editaction.h"
#include "BKE_constraint.h"
#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "BLI_arithb.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 );
@@ -237,7 +241,6 @@ PyObject *Bone_Init( void )
//--------------- updatePyBone------------------------------------
static int updatePyBone( BPy_Bone * self )
{
int x, y;
char *parent_str = "";
if( !self->bone ) {
@@ -540,45 +543,19 @@ 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( 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 );
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 = blen_bone->quat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
blen_bone->dquat = blen_bone->quat = ( QuaternionObject *)newQuaternionObject( NULL, Py_NEW );
blen_bone->obmat = blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->parmat = blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->defmat = blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->irestmat = blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
blen_bone->posemat = blen_bone->obmat = ( MatrixObject *)newMatrixObject( NULL, 4, 4 , Py_NEW);
if( !updatePyBone( blen_bone ) )
return EXPP_ReturnPyObjError( PyExc_AttributeError,
@@ -627,45 +604,19 @@ 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( 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 );
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);
//default py values
BLI_strncpy( py_bone->name, name_str, strlen( name_str ) + 1 );
@@ -762,19 +713,17 @@ static PyObject *Bone_getWeight( BPy_Bone * self )
static PyObject *Bone_getHead( BPy_Bone * self )
{
PyObject *attr = NULL;
float *vec;
float vec[3];
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 );
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
} else {
//use bone datastruct
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
attr = newVectorObject( NULL, 3, Py_NEW );
( ( VectorObject * ) attr )->vec[0] = self->bone->head[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->head[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->head[2];
@@ -790,19 +739,17 @@ static PyObject *Bone_getHead( BPy_Bone * self )
static PyObject *Bone_getTail( BPy_Bone * self )
{
PyObject *attr = NULL;
float *vec;
float vec[3];
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 );
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
} else {
//use bone datastruct
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
attr = newVectorObject( NULL, 3, Py_NEW );
( ( VectorObject * ) attr )->vec[0] = self->bone->tail[0];
( ( VectorObject * ) attr )->vec[1] = self->bone->tail[1];
( ( VectorObject * ) attr )->vec[2] = self->bone->tail[2];
@@ -818,19 +765,17 @@ static PyObject *Bone_getTail( BPy_Bone * self )
static PyObject *Bone_getLoc( BPy_Bone * self )
{
PyObject *attr = NULL;
float *vec;
float vec[3];
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 );
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
} else {
//use bone datastruct
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
attr = newVectorObject( NULL, 3, Py_NEW );
// ( ( VectorObject * ) attr )->vec[0] = self->bone->loc[0];
// ( ( VectorObject * ) attr )->vec[1] = self->bone->loc[1];
@@ -847,19 +792,17 @@ static PyObject *Bone_getLoc( BPy_Bone * self )
static PyObject *Bone_getSize( BPy_Bone * self )
{
PyObject *attr = NULL;
float *vec;
float vec[3];
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 );
attr = ( PyObject * ) newVectorObject( vec, 3, Py_NEW );
} else {
//use bone datastruct
attr = newVectorObject( PyMem_Malloc( 3 * sizeof( float ) ),
3 );
attr = newVectorObject( NULL, 3, Py_NEW );
// ( ( VectorObject * ) attr )->vec[0] = self->bone->size[0];
// ( ( VectorObject * ) attr )->vec[1] = self->bone->size[1];
// ( ( VectorObject * ) attr )->vec[2] = self->bone->size[2];
@@ -875,20 +818,18 @@ static PyObject *Bone_getSize( BPy_Bone * self )
static PyObject *Bone_getQuat( BPy_Bone * self )
{
PyObject *attr = NULL;
float *quat;
float quat[4];
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 );
attr = ( PyObject * ) newQuaternionObject( quat, Py_NEW );
} else {
//use bone datastruct
attr = newQuaternionObject( PyMem_Malloc
( 4 * sizeof( float ) ) );
attr = newQuaternionObject( NULL, Py_NEW );
// ( ( QuaternionObject * ) attr )->quat[0] = self->bone->quat[0];
// ( ( QuaternionObject * ) attr )->quat[1] = self->bone->quat[1];
// ( ( QuaternionObject * ) attr )->quat[2] = self->bone->quat[2];
@@ -1697,7 +1638,7 @@ static PyObject *Bone_getRestMatrix( BPy_Bone * self, PyObject * args )
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
"expected 'bonespace' or 'worldspace'" ) );
matrix = newMatrixObject( PyMem_Malloc( 16 * sizeof( float ) ), 4, 4 );
matrix = newMatrixObject( NULL, 4, 4 , Py_NEW);
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,14 +29,48 @@
*
* ***** 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,12 +58,12 @@
#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 "Mathutils.h"
#include "constant.h"
#include "gen_utils.h"
@@ -759,12 +759,12 @@ static PyObject *NMVert_getattr( PyObject * self, char *name )
BPy_NMVert *mv = ( BPy_NMVert * ) self;
if( !strcmp( name, "co" ) || !strcmp( name, "loc" ) )
return newVectorProxy( mv->co, 3 );
return newVectorObject(mv->co,3,Py_WRAP);
else if( strcmp( name, "no" ) == 0 )
return newVectorProxy( mv->no, 3 );
return newVectorObject(mv->no,3,Py_WRAP);
else if( strcmp( name, "uvco" ) == 0 )
return newVectorProxy( mv->uvco, 3 );
return newVectorObject(mv->uvco,3,Py_WRAP);
else if( strcmp( name, "index" ) == 0 )
return PyInt_FromLong( mv->index );
else if( strcmp( name, "sel" ) == 0 )

View File

@@ -38,28 +38,28 @@
#include "Object.h"
#include "NLA.h"
#include "logic.h"
#include <blendef.h>
#include "blendef.h"
#include <DNA_scene_types.h>
#include <DNA_mesh_types.h>
#include <DNA_curve_types.h>
#include "DNA_scene_types.h"
#include "DNA_mesh_types.h"
#include "DNA_curve_types.h"
#include "DNA_object_force.h"
#include <DNA_property_types.h>
#include "DNA_property_types.h"
#include <BKE_depsgraph.h>
#include <BKE_font.h>
#include <BKE_property.h>
#include <BKE_mball.h>
#include <BKE_softbody.h>
#include "BKE_depsgraph.h"
#include "BKE_font.h"
#include "BKE_property.h"
#include "BKE_mball.h"
#include "BKE_softbody.h"
#include <BIF_editview.h>
#include <BSE_editipo.h>
#include <BSE_edit.h>
#include "BIF_editview.h"
#include "BSE_editipo.h"
#include "BSE_edit.h"
#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 */
#include "DNA_oops_types.h"
@@ -1109,21 +1109,20 @@ static PyObject *Object_getDrawType( BPy_Object * self )
static PyObject *Object_getEuler( BPy_Object * self )
{
EulerObject *eul;
float eul[3];
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];
eul[0] = self->object->rot[0];
eul[1] = self->object->rot[1];
eul[2] = self->object->rot[2];
return ( PyObject * ) eul;
return ( PyObject * ) newEulerObject( eul, Py_WRAP );
}
static PyObject *Object_getInverseMatrix( BPy_Object * self )
{
MatrixObject *inverse =
( MatrixObject * ) newMatrixObject( NULL, 4, 4 );
( MatrixObject * ) newMatrixObject( NULL, 4, 4, Py_NEW);
Mat4Invert( (float ( * )[4])*inverse->matrix, self->object->obmat );
return ( ( PyObject * ) inverse );
@@ -1170,35 +1169,30 @@ static PyObject *Object_getMaterials( BPy_Object * self, PyObject * args )
static PyObject *Object_getMatrix( BPy_Object * self, PyObject * args )
{
PyObject *matrix;
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};
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] ) *( ( MatrixObject * ) matrix )->matrix );
/* old behavior, prior to 2.34, check this method's doc string: */
object_to_mat4( self->object, (float (*)[4])matrix );
return newMatrixObject(matrix,4,4,Py_NEW);
} else if( BLI_streq( space, "old_worldspace" ) ) {
Mat4CpyMat4( (float ( * )[4]) *( ( MatrixObject * ) matrix )->matrix,
self->object->obmat );
/* old behavior, prior to 2.34, check this method's doc string: */
} else {
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
"wrong parameter, expected nothing or either 'worldspace' (default),\n\
'localspace' or 'old_worldspace'" ) );
}
return matrix;
return newMatrixObject((float*)self->object->obmat,4,4,Py_WRAP);
}
static PyObject *Object_getName( BPy_Object * self )
@@ -1378,7 +1372,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 );
vector = newVectorObject( NULL, 3, Py_NEW);
memcpy( ( ( VectorObject * ) vector )->vec,
tmpvec, 3 * sizeof( float ) );
PyList_SET_ITEM( bbox, i, vector );
@@ -1399,7 +1393,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 );
vector = newVectorObject( vec, 3, Py_WRAP );
PyList_SET_ITEM( bbox, i, vector );
vec += 3;
}

View File

@@ -35,34 +35,31 @@
#include <Python.h>
#include <stdio.h>
#include <BDR_editobject.h>
#include <BKE_armature.h>
#include <BKE_curve.h>
#include <BKE_global.h>
#include <BKE_library.h>
#include <BKE_lattice.h>
#include <BKE_main.h>
#include <BKE_mesh.h>
#include <BKE_object.h>
#include <BKE_scene.h>
#include <BKE_displist.h> /* for makeDispList */
#include <BKE_font.h> /* for text_to_font */
#include <BLI_arithb.h>
#include <BLI_blenlib.h>
#include <DNA_armature_types.h>
#include <DNA_action_types.h>
#include <DNA_ID.h>
#include <DNA_listBase.h>
#include <DNA_scene_types.h>
#include <DNA_userdef_types.h>
#include <DNA_view3d_types.h>
#include <DNA_action_types.h>
#include "BDR_editobject.h"
#include "BKE_armature.h"
#include "BKE_curve.h"
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_lattice.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_object.h"
#include "BKE_scene.h"
#include "BKE_displist.h" /* for makeDispList */
#include "BKE_font.h" /* for text_to_font */
#include "BLI_arithb.h"
#include "BLI_blenlib.h"
#include "DNA_armature_types.h"
#include "DNA_action_types.h"
#include "DNA_ID.h"
#include "DNA_listBase.h"
#include "DNA_scene_types.h"
#include "DNA_userdef_types.h"
#include "DNA_view3d_types.h"
#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

@@ -34,29 +34,29 @@
#include <Python.h>
#include <stdio.h>
#include <blendef.h> /* OBACT */
#include <BDR_editobject.h> /* enter / leave editmode */
#include <BKE_global.h>
#include <BKE_library.h>
#include <BKE_object.h> /* for during_script() and during_scriptlink() */
#include <BKE_scene.h> /* scene_find_camera() */
#include <BIF_usiblender.h>
#include <BIF_mywindow.h>
#include <BSE_headerbuttons.h>
#include <BSE_filesel.h>
#include <BIF_editmesh.h> /* for undo_push_mesh() */
#include <BIF_screen.h>
#include <BIF_space.h>
#include <BIF_drawtext.h>
#include <BIF_mywindow.h> /* L/M/R_MOUSE bitflags */
#include <BIF_spacetypes.h>
#include <mydevice.h>
#include <DNA_view3d_types.h>
#include <DNA_screen_types.h>
#include <DNA_space_types.h>
#include <DNA_scene_types.h>
#include <DNA_text_types.h>
#include <DNA_vec_types.h> /* for rcti struct */
#include "blendef.h" /* OBACT */
#include "BDR_editobject.h" /* enter / leave editmode */
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_object.h" /* for during_script() and during_scriptlink() */
#include "BKE_scene.h" /* scene_find_camera() */
#include "BIF_usiblender.h"
#include "BIF_mywindow.h"
#include "BSE_headerbuttons.h"
#include "BSE_filesel.h"
#include "BIF_editmesh.h" /* for undo_push_mesh() */
#include "BIF_screen.h"
#include "BIF_space.h"
#include "BIF_drawtext.h"
#include "BIF_mywindow.h" /* L/M/R_MOUSE bitflags */
#include "BIF_spacetypes.h"
#include "mydevice.h"
#include "DNA_view3d_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_scene_types.h"
#include "DNA_text_types.h"
#include "DNA_vec_types.h" /* for rcti struct */
#include "windowTheme.h"
#include "gen_utils.h"
@@ -856,7 +856,7 @@ static PyObject *M_Window_GetViewMatrix( PyObject * self )
viewmat =
( PyObject * ) newMatrixObject( ( float * ) G.vd->viewmat, 4,
4 );
4, Py_WRAP );
if( !viewmat )
return EXPP_ReturnPyObjError( PyExc_MemoryError,
@@ -880,7 +880,7 @@ static PyObject *M_Window_GetPerspMatrix( PyObject * self )
perspmat =
( PyObject * ) newMatrixObject( ( float * ) G.vd->persmat, 4,
4 );
4, Py_WRAP );
if( !perspmat )
return EXPP_ReturnPyObjError( PyExc_MemoryError,

View File

@@ -29,329 +29,384 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include "euler.h"
#include "BLI_arithb.h"
#include "BKE_utildefines.h"
#include "Mathutils.h"
#include "gen_utils.h"
#include "BLI_blenlib.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";
//methods table
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 ----------------------
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},
{"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},
{NULL, NULL, 0, NULL}
};
/*****************************/
// Euler Python Object
/*****************************/
//euler methods
PyObject *Euler_ToQuat( EulerObject * self )
//-----------------------------METHODS----------------------------
//----------------------------Euler.toQuat()----------------------
//return a quaternion representation of the euler
PyObject *Euler_ToQuat(EulerObject * self)
{
float *quat;
float eul[3];
float quat[4];
int x;
for( x = 0; x < 3; x++ ) {
self->eul[x] *= ( float ) ( Py_PI / 180 );
for(x = 0; x < 3; x++) {
eul[x] = self->eul[x] * ((float)Py_PI / 180);
}
quat = PyMem_Malloc( 4 * sizeof( float ) );
EulToQuat( self->eul, quat );
for( x = 0; x < 3; x++ ) {
self->eul[x] *= ( float ) ( 180 / Py_PI );
}
return ( PyObject * ) newQuaternionObject( quat );
EulToQuat(eul, quat);
if(self->data.blend_data)
return (PyObject *) newQuaternionObject(quat, Py_WRAP);
else
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}
PyObject *Euler_ToMatrix( EulerObject * self )
//----------------------------Euler.toMatrix()---------------------
//return a matrix representation of the euler
PyObject *Euler_ToMatrix(EulerObject * self)
{
float *mat;
float eul[3];
float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
int x;
for( x = 0; x < 3; x++ ) {
self->eul[x] *= ( float ) ( Py_PI / 180 );
for(x = 0; x < 3; x++) {
eul[x] = self->eul[x] * ((float)Py_PI / 180);
}
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 );
}
return ( PyObject * ) newMatrixObject( mat, 3, 3 );
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);
}
PyObject *Euler_Unique( EulerObject * self )
//----------------------------Euler.unique()-----------------------
//sets the x,y,z values to a unique euler rotation
PyObject *Euler_Unique(EulerObject * self)
{
float heading, pitch, bank;
float pi2 = ( float ) Py_PI * 2.0f;
float piO2 = ( float ) Py_PI / 2.0f;
float Opi2 = 1.0f / pi2;
double heading, pitch, bank;
double pi2 = Py_PI * 2.0f;
double piO2 = Py_PI / 2.0f;
double 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 += ( float ) Py_PI;
pitch -= ( float ) floor( pitch * Opi2 ) * pi2;
pitch -= ( float ) Py_PI;
pitch += Py_PI;
pitch -= floor(pitch * Opi2) * pi2;
pitch -= Py_PI;
if( pitch < -piO2 ) {
pitch = ( float ) -Py_PI - pitch;
heading += ( float ) Py_PI;
bank += ( float ) Py_PI;
} else if( pitch > piO2 ) {
pitch = ( float ) Py_PI - pitch;
heading += ( float ) Py_PI;
bank += ( float ) Py_PI;
if(pitch < -piO2) {
pitch = -Py_PI - pitch;
heading += Py_PI;
bank += Py_PI;
} else if(pitch > piO2) {
pitch = Py_PI - pitch;
heading += Py_PI;
bank += Py_PI;
}
//gimbal lock test
if( fabs( pitch ) > piO2 - 1e-4 ) {
if(fabs(pitch) > piO2 - 1e-4) {
heading += bank;
bank = 0.0f;
} else {
bank += ( float ) Py_PI;
bank -= ( float ) ( floor( bank * Opi2 ) ) * pi2;
bank -= ( float ) Py_PI;
bank += Py_PI;
bank -= (floor(bank * Opi2)) * pi2;
bank -= Py_PI;
}
heading += ( float ) Py_PI;
heading -= ( float ) ( floor( heading * Opi2 ) ) * pi2;
heading -= ( float ) Py_PI;
heading += Py_PI;
heading -= (floor(heading * Opi2)) * pi2;
heading -= Py_PI;
//back to degrees
self->eul[0] = heading * ( float ) ( 180 / Py_PI );
self->eul[1] = pitch * ( float ) ( 180 / Py_PI );
self->eul[2] = bank * ( float ) ( 180 / Py_PI );
self->eul[0] = heading * 180 / (float)Py_PI;
self->eul[1] = pitch * 180 / (float)Py_PI;
self->eul[2] = bank * 180 / (float)Py_PI;
return EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
PyObject *Euler_Zero( EulerObject * self )
//----------------------------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 EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
static void Euler_dealloc( EulerObject * 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)
{
/* since we own this memory... */
PyMem_Free( self->eul );
float angle = 0.0f;
char *axis;
int x;
PyObject_DEL( self );
}
static PyObject *Euler_getattr( EulerObject * self, char *name )
{
if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
return PyFloat_FromDouble( self->eul[name[0] - 'x'] );
if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"euler.rotate():expected angle (float) and axis (x,y,z)");
}
return Py_FindMethod( Euler_methods, ( PyObject * ) self, name );
}
static int Euler_setattr( EulerObject * self, char *name, PyObject * e )
{
float val;
if( !PyArg_Parse( e, "f", &val ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"unable to parse float argument\n" );
if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
self->eul[name[0] - 'x'] = val;
return 0;
} else
return -1;
}
/* Eulers Sequence methods */
static PyObject *Euler_item( EulerObject * self, int i )
{
if( i < 0 || i >= 3 )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"array index out of range\n" );
return Py_BuildValue( "f", self->eul[i] );
}
static PyObject *Euler_slice( EulerObject * self, int begin, int end )
{
PyObject *list;
int count;
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] ) );
if(!STREQ3(axis,"x","y","z")){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"euler.rotate(): expected axis to be 'x', 'y' or 'z'");
}
return list;
}
static int Euler_ass_item( EulerObject * self, int i, PyObject * ob )
{
if( i < 0 || i >= 3 )
return EXPP_ReturnIntError( PyExc_IndexError,
"array assignment index out of range\n" );
if( !PyNumber_Check( ob ) )
return EXPP_ReturnIntError( PyExc_IndexError,
"Euler 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->eul[i] = ( float ) PyFloat_AsDouble( ob );
//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);
}
PyObject_DEL(self);
}
//----------------------------getattr()(internal) ------------------
//object.attribute access (get)
static PyObject *Euler_getattr(EulerObject * self, char *name)
{
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]);
}
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;
f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"euler.attribute = x: argument not a number\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);
return 0;
}
static int Euler_ass_slice( EulerObject * self, int begin, int end,
PyObject * seq )
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Euler_repr(EulerObject * self)
{
int count, z;
int i;
char buffer[48], str[1024];
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;
}
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);
}
}
return 0;
strcat(str, "](euler)");
return EXPP_incr_ret(PyString_FromString(str));
}
static PyObject *Euler_repr( EulerObject * self )
//---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------
//sequence length
static int Euler_len(EulerObject * self)
{
int i, maxindex = 3 - 1;
char ftoa[24];
PyObject *str1, *str2;
return 3;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
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");
str1 = PyString_FromString( "[" );
return Py_BuildValue("f", self->eul[i]);
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 );
}
//----------------------------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");
}
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" );
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;
int count;
CLAMP(begin, 0, 3);
CLAMP(end, 0, 3);
begin = MIN2(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)
{
int i, y, size = 0;
float eul[3];
CLAMP(begin, 0, 3);
CLAMP(end, 0, 3);
begin = MIN2(begin,end);
size = PySequence_Length(seq);
if(size != (end - begin)){
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];
}
return 0;
}
//-----------------PROTCOL DECLARATIONS--------------------------
static PySequenceMethods Euler_SeqMethods = {
( inquiry ) 0, /* sq_length */
( binaryfunc ) 0, /* sq_concat */
( intargfunc ) 0, /* sq_repeat */
( intargfunc ) Euler_item, /* sq_item */
( intintargfunc ) Euler_slice, /* sq_slice */
( intobjargproc ) Euler_ass_item, /* sq_ass_item */
( intintobjargproc ) Euler_ass_slice, /* sq_ass_slice */
(inquiry) Euler_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(intargfunc) 0, /* sq_repeat */
(intargfunc) Euler_item, /* sq_item */
(intintargfunc) Euler_slice, /* sq_slice */
(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 */
"euler", /*tp_name */
sizeof( EulerObject ), /*tp_basicsize */
0, /*tp_itemsize */
( destructor ) Euler_dealloc, /*tp_dealloc */
( printfunc ) 0, /*tp_print */
( getattrfunc ) Euler_getattr, /*tp_getattr */
( setattrfunc ) Euler_setattr, /*tp_setattr */
0, /*tp_compare */
( reprfunc ) Euler_repr, /*tp_repr */
0, /*tp_as_number */
&Euler_SeqMethods, /*tp_as_sequence */
PyObject_HEAD_INIT(NULL)
0, /*ob_size */
"euler", /*tp_name */
sizeof(EulerObject), /*tp_basicsize */
0, /*tp_itemsize */
(destructor) Euler_dealloc, /*tp_dealloc */
(printfunc) 0, /*tp_print */
(getattrfunc) Euler_getattr, /*tp_getattr */
(setattrfunc) Euler_setattr, /*tp_setattr */
0, /*tp_compare */
(reprfunc) Euler_repr, /*tp_repr */
0, /*tp_as_number */
&Euler_SeqMethods, /*tp_as_sequence */
};
PyObject *newEulerObject( float *eul )
//------------------------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)
{
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;
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;
}
} else{
for( x = 0; x < 3; x++){
self->eul[x] = eul[x];
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
for(x = 0; x < 3; x++) {
self->eul[x] = 0.0f;
}
}else{
for(x = 0; x < 3; x++){
self->eul[x] = eul[x];
}
}
}else{ //bad type
return NULL;
}
return ( PyObject * ) self;
return (PyObject *) EXPP_incr_ret((PyObject *)self);
}

View File

@@ -1,4 +1,3 @@
/*
* $Id$
*
@@ -35,33 +34,28 @@
#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 float *eul;
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
float *eul; //1D array of data (alias)
} 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

@@ -30,10 +30,10 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include <DNA_text_types.h>
#include <MEM_guardedalloc.h>
#include <BLI_blenlib.h>
#include <BIF_space.h>
#include "DNA_text_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BIF_space.h"
#include "gen_utils.h"
#include "constant.h"
@@ -141,6 +141,31 @@ PyObject *EXPP_incr_ret_True()
return Py_BuildValue("i", 1);
}
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 );
}
/*****************************************************************************/
/* Description: This function maps the event identifier to a string. */
/*****************************************************************************/

View File

@@ -39,17 +39,19 @@
#include <stdio.h>
#include <string.h>
#include <BKE_global.h>
#include <BKE_main.h>
#include <BPI_script.h>
#include "BKE_global.h"
#include "BKE_main.h"
#include "BPI_script.h"
#include <DNA_ID.h>
#include <DNA_object_types.h>
#include <DNA_material_types.h>
#include <DNA_scriptlink_types.h>
#include <DNA_listBase.h>
#include "DNA_ID.h"
#include "DNA_object_types.h"
#include "DNA_material_types.h"
#include "DNA_scriptlink_types.h"
#include "DNA_listBase.h"
#define Py_PI 3.14159265358979323846
#define Py_WRAP 1024
#define Py_NEW 2048
/*
Py_RETURN_NONE
@@ -72,6 +74,10 @@ 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,37 +33,31 @@
#ifndef EXPP_matrix_h
#define EXPP_matrix_h
#include "Python.h"
#include "BLI_arithb.h"
#include "vector.h"
#include "gen_utils.h"
#include "Types.h"
#include "quat.h"
#include "euler.h"
#define MatrixObject_Check(v) ((v)->ob_type == &matrix_Type)
#define Matrix_CheckPyObject(v) ((v)->ob_type == &matrix_Type)
/*****************************/
/* Matrix Python Object */
/*****************************/
typedef float **ptRow;
typedef struct _Matrix {
PyObject_VAR_HEAD /* standard python macro */
ptRow matrix;
float *contigPtr;
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)
int rowSize;
int colSize;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
PyObject *coerced_object;
} 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*/
/*****************************************************************************/
/* Python API function prototypes. */
/*****************************************************************************/
PyObject *newMatrixObject( float *mat, int rowSize, int colSize );
/*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 *Matrix_Zero( MatrixObject * self );
PyObject *Matrix_Identity( MatrixObject * self );
PyObject *Matrix_Transpose( MatrixObject * self );
@@ -74,5 +68,6 @@ 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,545 +29,570 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#include "quat.h"
#include "BLI_arithb.h"
#include "BKE_utildefines.h"
#include "Mathutils.h"
#include "gen_utils.h"
#include "BLI_blenlib.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";
//methods table
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 ----------------------
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}
};
/* ****** 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 )
//-----------------------------METHODS------------------------------
//----------------------------Quaternion.toEuler()------------------
//return the quat as a euler
PyObject *Quaternion_ToEuler(QuaternionObject * self)
{
float *eul;
float eul[3];
int x;
eul = PyMem_Malloc( 3 * sizeof( float ) );
QuatToEul( self->quat, eul );
for( x = 0; x < 3; x++ ) {
eul[x] *= ( float ) ( 180 / Py_PI );
QuatToEul(self->quat, eul);
for(x = 0; x < 3; x++) {
eul[x] *= (180 / (float)Py_PI);
}
return ( PyObject * ) newEulerObject( eul );
if(self->data.blend_data)
return newEulerObject(eul, Py_WRAP);
else
return newEulerObject(eul, Py_NEW);
}
PyObject *Quaternion_ToMatrix( QuaternionObject * self )
//----------------------------Quaternion.toMatrix()------------------
//return the quat as a matrix
PyObject *Quaternion_ToMatrix(QuaternionObject * self)
{
float *mat;
float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
QuatToMat3(self->quat, (float (*)[3]) mat);
mat = PyMem_Malloc( 3 * 3 * sizeof( float ) );
QuatToMat3( self->quat, ( float ( * )[3] ) mat );
return ( PyObject * ) newMatrixObject( mat, 3, 3 );
if(self->data.blend_data)
return newMatrixObject(mat, 3, 3, Py_WRAP);
else
return newMatrixObject(mat, 3, 3, Py_NEW);
}
//----------------------------Quaternion.normalize()----------------
//normalize the axis of rotation of [theta,vector]
PyObject *Quaternion_Normalize( QuaternionObject * self )
PyObject *Quaternion_Normalize(QuaternionObject * self)
{
NormalQuat( self->quat );
return EXPP_incr_ret( Py_None );
NormalQuat(self->quat);
return (PyObject*)self;
}
PyObject *Quaternion_Inverse( QuaternionObject * self )
//----------------------------Quaternion.inverse()------------------
//invert the quat
PyObject *Quaternion_Inverse(QuaternionObject * self)
{
float mag = 0.0f;
double mag = 0.0f;
int x;
for( x = 1; x < 4; x++ ) {
for(x = 1; x < 4; x++) {
self->quat[x] = -self->quat[x];
}
for( x = 0; x < 4; x++ ) {
mag += ( self->quat[x] * self->quat[x] );
for(x = 0; x < 4; x++) {
mag += (self->quat[x] * self->quat[x]);
}
mag = ( float ) sqrt( mag );
for( x = 0; x < 4; x++ ) {
self->quat[x] /= ( mag * mag );
mag = sqrt(mag);
for(x = 0; x < 4; x++) {
self->quat[x] /= (mag * mag);
}
return EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
PyObject *Quaternion_Identity( QuaternionObject * self )
//----------------------------Quaternion.identity()-----------------
//generate the identity quaternion
PyObject *Quaternion_Identity(QuaternionObject * self)
{
self->quat[0] = 1.0;
self->quat[1] = 0.0;
self->quat[2] = 0.0;
self->quat[3] = 0.0;
return EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
PyObject *Quaternion_Negate( QuaternionObject * self )
//----------------------------Quaternion.negate()-------------------
//negate the quat
PyObject *Quaternion_Negate(QuaternionObject * self)
{
int x;
for( x = 0; x < 4; x++ ) {
for(x = 0; x < 4; x++) {
self->quat[x] = -self->quat[x];
}
return EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
PyObject *Quaternion_Conjugate( QuaternionObject * self )
//----------------------------Quaternion.conjugate()----------------
//negate the vector part
PyObject *Quaternion_Conjugate(QuaternionObject * self)
{
int x;
for( x = 1; x < 4; x++ ) {
for(x = 1; x < 4; x++) {
self->quat[x] = -self->quat[x];
}
return EXPP_incr_ret( Py_None );
return (PyObject*)self;
}
static void Quaternion_dealloc( QuaternionObject * self )
//----------------------------dealloc()(internal) ------------------
//free the py_object
static void Quaternion_dealloc(QuaternionObject * self)
{
PyMem_Free( self->quat );
PyObject_DEL( self );
//only free py_data
if(self->data.py_data){
PyMem_Free(self->data.py_data);
}
PyObject_DEL(self);
}
static PyObject *Quaternion_getattr( QuaternionObject * self, char *name )
//----------------------------getattr()(internal) ------------------
//object.attribute access (get)
static PyObject *Quaternion_getattr(QuaternionObject * self, char *name)
{
int x;
double mag = 0.0f;
float *vec = NULL;
int x;
PyObject *retval;
float vec[3];
if( ELEM4( name[0], 'w', 'x', 'y', 'z' ) && name[1] == 0 ) {
return PyFloat_FromDouble( self->quat[name[0] - 'w'] );
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( strcmp( name, "magnitude" ) == 0 ) {
for( x = 0; x < 4; x++ ) {
if(STREQ(name, "magnitude")) {
for(x = 0; x < 4; x++) {
mag += self->quat[x] * self->quat[x];
}
mag = ( float ) sqrt( mag );
return PyFloat_FromDouble( mag );
mag = sqrt(mag);
return PyFloat_FromDouble(mag);
}
if( strcmp( name, "angle" ) == 0 ) {
if(STREQ(name, "angle")) {
mag = self->quat[0];
mag = 2 * ( acos( mag ) );
mag *= ( 180 / Py_PI );
return PyFloat_FromDouble( mag );
mag = 2 * (acos(mag));
mag *= (180 / Py_PI);
return PyFloat_FromDouble(mag);
}
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] / ( ( float ) ( mag ) ) );
if(STREQ(name, "axis")) {
mag = self->quat[0] * (Py_PI / 180);
mag = 2 * (acos(mag));
mag = sin(mag / 2);
for(x = 0; x < 3; x++) {
vec[x] = (self->quat[x + 1] / mag);
}
Normalise( vec );
retval = ( PyObject * ) newVectorObject( vec, 3 );
PyMem_Free( vec );
return retval;
Normalise(vec);
return (PyObject *) newVectorObject(vec, 3, Py_NEW);
}
return Py_FindMethod( Quaternion_methods, ( PyObject * ) self, name );
return Py_FindMethod(Quaternion_methods, (PyObject *) self, name);
}
static int Quaternion_setattr( QuaternionObject * self, char *name,
PyObject * v )
//----------------------------setattr()(internal) ------------------
//object.attribute access (set)
static int Quaternion_setattr(QuaternionObject * self, char *name, PyObject * q)
{
float val;
PyObject *f = NULL;
if( !PyFloat_Check( v ) && !PyInt_Check( v ) ) {
return EXPP_ReturnIntError( PyExc_TypeError,
"int or float expected\n" );
} else {
if( !PyArg_Parse( v, "f", &val ) )
return EXPP_ReturnIntError( PyExc_TypeError,
"unable to parse float argument\n" );
f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion.attribute = x: argument not a number\n");
}
if( ELEM4( name[0], 'w', 'x', 'y', 'z' ) && name[1] == 0 ) {
self->quat[name[0] - 'w'] = val;
} else
return -1;
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);
}else{
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_AttributeError,
"quaternion.attribute = x: unknown attribute\n");
}
Py_DECREF(f);
return 0;
}
/* Quaternions Sequence methods */
static PyObject *Quaternion_item( QuaternionObject * self, int i )
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Quaternion_repr(QuaternionObject * self)
{
if( i < 0 || i >= 4 )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"array index out of range\n" );
int i;
char buffer[48], str[1024];
return Py_BuildValue( "f", self->quat[i] );
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));
}
static PyObject *Quaternion_slice( QuaternionObject * self, int begin,
int end )
//---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------
//sequence length
static int Quaternion_len(QuaternionObject * self)
{
PyObject *list;
return 4;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
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");
return Py_BuildValue("f", self->quat[i]);
}
//----------------------------object[]-------------------------
//sequence accessor (set)
static int Quaternion_ass_item(QuaternionObject * 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,
"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;
int count;
if( begin < 0 )
begin = 0;
if( end > 4 )
end = 4;
if( begin > end )
begin = end;
CLAMP(begin, 0, 4);
CLAMP(end, 0, 4);
begin = MIN2(begin,end);
list = PyList_New( end - begin );
for( count = begin; count < end; count++ ) {
PyList_SetItem( list, count - begin,
PyFloat_FromDouble( self->quat[count] ) );
list = PyList_New(end - begin);
for(count = begin; count < end; count++) {
PyList_SetItem(list, count - begin,
PyFloat_FromDouble(self->quat[count]));
}
return list;
}
static int Quaternion_ass_item( QuaternionObject * self, int i, PyObject * ob )
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end,
PyObject * seq)
{
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" );
int i, y, size = 0;
float quat[4];
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 );
CLAMP(begin, 0, 4);
CLAMP(end, 0, 4);
begin = MIN2(begin,end);
size = PySequence_Length(seq);
if(size != (end - begin)){
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[begin:end] = []: size mismatch in slice assignment\n");
}
return 0;
}
static int Quaternion_ass_slice( QuaternionObject * self, int begin, int end,
PyObject * seq )
{
int count, z;
for (i = 0; i < size; i++) {
PyObject *q, *f;
if( begin < 0 )
begin = 0;
if( end > 4 )
end = 4;
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->quat[count] ) ) {
Py_DECREF( ob );
return -1;
}
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);
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[begin:end] = []: sequence argument not a number\n");
}
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;
}
static PyObject *Quaternion_repr( QuaternionObject * self )
//------------------------NUMERIC PROTOCOLS----------------------
//------------------------obj + obj------------------------------
//addition
static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
{
int i, maxindex = 4 - 1;
char ftoa[24];
PyObject *str1, *str2;
int x;
float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
str1 = PyString_FromString( "[" );
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
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 );
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];
}
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" );
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}
PyObject *Quaternion_add( PyObject * q1, PyObject * q2 )
//------------------------obj - obj------------------------------
//subtraction
static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
{
float *quat = NULL;
PyObject *retval;
int x;
float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
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" );
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
quat = PyMem_Malloc( 4 * sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] =
( ( ( QuaternionObject * ) q1 )->quat[x] ) +
( ( ( QuaternionObject * ) q2 )->quat[x] );
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];
}
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}
PyObject *Quaternion_sub( PyObject * q1, PyObject * q2 )
//------------------------obj * obj------------------------------
//mulplication
static PyObject *Quaternion_mul(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;
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" );
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
quat = PyMem_Malloc( 4 * sizeof( float ) );
for( x = 0; x < 4; x++ ) {
quat[x] =
( ( ( QuaternionObject * ) q1 )->quat[x] ) -
( ( ( QuaternionObject * ) q2 )->quat[x] );
}
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
}
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,
"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];
}
retval = ( PyObject * ) newQuaternionObject( quat );
PyMem_Free( quat );
return retval;
}
//coercion of unknown types to type QuaternionObject for numeric protocols
int Quaternion_coerce( PyObject ** q1, PyObject ** q2 )
{
long *tempI = NULL;
double *tempF = NULL;
float *quat = NULL;
int x;
if( QuaternionObject_Check( *q1 ) ) {
if( QuaternionObject_Check( *q2 ) ) { //two Quaternions
Py_INCREF( *q1 );
Py_INCREF( *q2 );
return 0;
} else {
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;
}
PyMem_Free( tempF );
*q2 = newQuaternionObject( quat );
PyMem_Free( quat );
( ( QuaternionObject * ) * q2 )->flag = 2; //float coercion
Py_INCREF( *q1 ); /* fixme: is this needed? */
return 0;
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);
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);
}
//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{ //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);
}
} else {
printf( "numeric protocol failure...\n" );
return -1; //this should not occur - fail
}
return -1;
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
}
//------------------------coerce(obj, obj)-----------------------
//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)
{
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;
}else{
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion.coerce(): unknown operand - can't coerce for numeric protocols\n");
}
}
EXPP_incr2(*q1, *q2);
return 0;
}
//-----------------PROTCOL DECLARATIONS--------------------------
static PySequenceMethods Quaternion_SeqMethods = {
( inquiry ) 0, /* sq_length */
( binaryfunc ) 0, /* sq_concat */
( intargfunc ) 0, /* sq_repeat */
( intargfunc ) Quaternion_item, /* sq_item */
( intintargfunc ) Quaternion_slice, /* sq_slice */
( intobjargproc ) Quaternion_ass_item, /* sq_ass_item */
( intintobjargproc ) Quaternion_ass_slice, /* sq_ass_slice */
(inquiry) Quaternion_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(intargfunc) 0, /* sq_repeat */
(intargfunc) Quaternion_item, /* sq_item */
(intintargfunc) Quaternion_slice, /* sq_slice */
(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__ */
( binaryfunc ) Quaternion_mul, /* __mul__ */
( binaryfunc ) 0, /* __div__ */
( binaryfunc ) 0, /* __mod__ */
( binaryfunc ) 0, /* __divmod__ */
( ternaryfunc ) 0, /* __pow__ */
( unaryfunc ) 0, /* __neg__ */
( unaryfunc ) 0, /* __pos__ */
( unaryfunc ) 0, /* __abs__ */
( inquiry ) 0, /* __nonzero__ */
( unaryfunc ) 0, /* __invert__ */
( binaryfunc ) 0, /* __lshift__ */
( binaryfunc ) 0, /* __rshift__ */
( binaryfunc ) 0, /* __and__ */
( binaryfunc ) 0, /* __xor__ */
( binaryfunc ) 0, /* __or__ */
( coercion ) Quaternion_coerce, /* __coerce__ */
( unaryfunc ) 0, /* __int__ */
( unaryfunc ) 0, /* __long__ */
( unaryfunc ) 0, /* __float__ */
( unaryfunc ) 0, /* __oct__ */
( unaryfunc ) 0, /* __hex__ */
(binaryfunc) Quaternion_add, /* __add__ */
(binaryfunc) Quaternion_sub, /* __sub__ */
(binaryfunc) Quaternion_mul, /* __mul__ */
(binaryfunc) 0, /* __div__ */
(binaryfunc) 0, /* __mod__ */
(binaryfunc) 0, /* __divmod__ */
(ternaryfunc) 0, /* __pow__ */
(unaryfunc) 0, /* __neg__ */
(unaryfunc) 0, /* __pos__ */
(unaryfunc) 0, /* __abs__ */
(inquiry) 0, /* __nonzero__ */
(unaryfunc) 0, /* __invert__ */
(binaryfunc) 0, /* __lshift__ */
(binaryfunc) 0, /* __rshift__ */
(binaryfunc) 0, /* __and__ */
(binaryfunc) 0, /* __xor__ */
(binaryfunc) 0, /* __or__ */
(coercion) Quaternion_coerce, /* __coerce__ */
(unaryfunc) 0, /* __int__ */
(unaryfunc) 0, /* __long__ */
(unaryfunc) 0, /* __float__ */
(unaryfunc) 0, /* __oct__ */
(unaryfunc) 0, /* __hex__ */
};
//------------------PY_OBECT DEFINITION--------------------------
PyTypeObject quaternion_Type = {
PyObject_HEAD_INIT( NULL )
0, /*ob_size */
"quaternion", /*tp_name */
sizeof( QuaternionObject ), /*tp_basicsize */
0, /*tp_itemsize */
( destructor ) Quaternion_dealloc, /*tp_dealloc */
( printfunc ) 0, /*tp_print */
( getattrfunc ) Quaternion_getattr, /*tp_getattr */
( setattrfunc ) Quaternion_setattr, /*tp_setattr */
0, /*tp_compare */
( reprfunc ) Quaternion_repr, /*tp_repr */
&Quaternion_NumMethods, /*tp_as_number */
&Quaternion_SeqMethods, /*tp_as_sequence */
PyObject_HEAD_INIT(NULL)
0, /*ob_size */
"quaternion", /*tp_name */
sizeof(QuaternionObject), /*tp_basicsize */
0, /*tp_itemsize */
(destructor) Quaternion_dealloc, /*tp_dealloc */
(printfunc) 0, /*tp_print */
(getattrfunc) Quaternion_getattr, /*tp_getattr */
(setattrfunc) Quaternion_setattr, /*tp_setattr */
0, /*tp_compare */
(reprfunc) Quaternion_repr, /*tp_repr */
&Quaternion_NumMethods, /*tp_as_number */
&Quaternion_SeqMethods, /*tp_as_sequence */
};
/** 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 )
//------------------------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)
{
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;
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];
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
Quaternion_Identity(self);
}else{
for(x = 0; x < 4; x++){
self->quat[x] = quat[x];
}
}
}else{ //bad type
return NULL;
}
self->flag = 0;
return ( PyObject * ) self;
return (PyObject *) EXPP_incr_ret((PyObject *)self);
}

View File

@@ -34,34 +34,27 @@
#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 float *quat;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
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;
} 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 );
@@ -69,5 +62,6 @@ 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,40 +33,34 @@
#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 float *vec;
PyObject_VAR_HEAD
struct{
float *py_data; //python managed
float *blend_data; //blender managed
}data;
float *vec; //1D array of data (alias)
int size;
int flag;
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
int delete_pymem; /* flag to delete the memory vec points at */
PyObject *coerced_object;
} 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 */