4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Joseph Gilbert
26 * ***** END GPL LICENSE BLOCK *****
29 #include "Mathutils.h"
31 #include "BLI_arithb.h"
32 #include "BKE_utildefines.h"
33 #include "BLI_blenlib.h"
36 //-------------------------DOC STRINGS ---------------------------
37 static char Euler_Zero_doc[] = "() - set all values in the euler to 0";
38 static char Euler_Unique_doc[] ="() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock";
39 static char Euler_ToMatrix_doc[] = "() - returns a rotation matrix representing the euler rotation";
40 static char Euler_ToQuat_doc[] = "() - returns a quaternion representing the euler rotation";
41 static char Euler_Rotate_doc[] = "() - rotate a euler by certain amount around an axis of rotation";
42 static char Euler_copy_doc[] = "() - returns a copy of the euler.";
43 static char Euler_MakeCompatible_doc[] = "(euler) - Make this user compatible with another (no axis flipping).";
45 static PyObject *Euler_Zero( EulerObject * self );
46 static PyObject *Euler_Unique( EulerObject * self );
47 static PyObject *Euler_ToMatrix( EulerObject * self );
48 static PyObject *Euler_ToQuat( EulerObject * self );
49 static PyObject *Euler_Rotate( EulerObject * self, PyObject *args );
50 static PyObject *Euler_MakeCompatible( EulerObject * self, EulerObject *value );
51 static PyObject *Euler_copy( EulerObject * self, PyObject *args );
53 //-----------------------METHOD DEFINITIONS ----------------------
54 static struct PyMethodDef Euler_methods[] = {
55 {"zero", (PyCFunction) Euler_Zero, METH_NOARGS, Euler_Zero_doc},
56 {"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
57 {"toMatrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
58 {"toQuat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
59 {"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, Euler_Rotate_doc},
60 {"makeCompatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc},
61 {"__copy__", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
62 {"copy", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
66 //----------------------------------Mathutils.Euler() -------------------
67 //makes a new euler for you to play with
68 static PyObject *Euler_new(PyObject * self, PyObject * args)
71 PyObject *listObject = NULL;
76 size = PyTuple_GET_SIZE(args);
78 listObject = PyTuple_GET_ITEM(args, 0);
79 if (PySequence_Check(listObject)) {
80 size = PySequence_Length(listObject);
81 } else { // Single argument was not a sequence
82 PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
85 } else if (size == 0) {
86 //returns a new empty 3d euler
87 return newEulerObject(NULL, Py_NEW);
92 if (size != 3) { // Invalid euler size
93 PyErr_SetString(PyExc_AttributeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
97 for (i=0; i<size; i++) {
98 e = PySequence_GetItem(listObject, i);
99 if (e == NULL) { // Failed to read sequence
100 Py_DECREF(listObject);
101 PyErr_SetString(PyExc_RuntimeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
105 scalar= (float)PyFloat_AsDouble(e);
108 if(scalar==-1 && PyErr_Occurred()) { // parsed item is not a number
109 PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
115 return newEulerObject(eul, Py_NEW);
118 //-----------------------------METHODS----------------------------
119 //----------------------------Euler.toQuat()----------------------
120 //return a quaternion representation of the euler
121 static PyObject *Euler_ToQuat(EulerObject * self)
123 float eul[3], quat[4];
126 if(!BaseMath_ReadCallback(self))
129 for(x = 0; x < 3; x++) {
130 eul[x] = self->eul[x] * ((float)Py_PI / 180);
132 EulToQuat(eul, quat);
133 return newQuaternionObject(quat, Py_NEW);
135 //----------------------------Euler.toMatrix()---------------------
136 //return a matrix representation of the euler
137 static PyObject *Euler_ToMatrix(EulerObject * self)
140 float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
143 if(!BaseMath_ReadCallback(self))
146 for(x = 0; x < 3; x++) {
147 eul[x] = self->eul[x] * ((float)Py_PI / 180);
149 EulToMat3(eul, (float (*)[3]) mat);
150 return newMatrixObject(mat, 3, 3 , Py_NEW);
152 //----------------------------Euler.unique()-----------------------
153 //sets the x,y,z values to a unique euler rotation
154 static PyObject *Euler_Unique(EulerObject * self)
156 double heading, pitch, bank;
157 double pi2 = Py_PI * 2.0f;
158 double piO2 = Py_PI / 2.0f;
159 double Opi2 = 1.0f / pi2;
161 if(!BaseMath_ReadCallback(self))
165 heading = self->eul[0] * (float)Py_PI / 180;
166 pitch = self->eul[1] * (float)Py_PI / 180;
167 bank = self->eul[2] * (float)Py_PI / 180;
169 //wrap heading in +180 / -180
171 pitch -= floor(pitch * Opi2) * pi2;
176 pitch = -Py_PI - pitch;
179 } else if(pitch > piO2) {
180 pitch = Py_PI - pitch;
185 if(fabs(pitch) > piO2 - 1e-4) {
190 bank -= (floor(bank * Opi2)) * pi2;
195 heading -= (floor(heading * Opi2)) * pi2;
199 self->eul[0] = (float)(heading * 180 / (float)Py_PI);
200 self->eul[1] = (float)(pitch * 180 / (float)Py_PI);
201 self->eul[2] = (float)(bank * 180 / (float)Py_PI);
203 BaseMath_WriteCallback(self);
205 return (PyObject *)self;
207 //----------------------------Euler.zero()-------------------------
208 //sets the euler to 0,0,0
209 static PyObject *Euler_Zero(EulerObject * self)
215 BaseMath_WriteCallback(self);
217 return (PyObject *)self;
219 //----------------------------Euler.rotate()-----------------------
220 //rotates a euler a certain amount and returns the result
221 //should return a unique euler rotation (i.e. no 720 degree pitches :)
222 static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
228 if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
229 PyErr_SetString(PyExc_TypeError, "euler.rotate():expected angle (float) and axis (x,y,z)");
232 if(!STREQ3(axis,"x","y","z")){
233 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'x', 'y' or 'z'");
237 if(!BaseMath_ReadCallback(self))
241 angle *= ((float)Py_PI / 180);
242 for(x = 0; x < 3; x++) {
243 self->eul[x] *= ((float)Py_PI / 180);
245 euler_rot(self->eul, angle, *axis);
246 //convert back from radians
247 for(x = 0; x < 3; x++) {
248 self->eul[x] *= (180 / (float)Py_PI);
251 BaseMath_WriteCallback(self);
253 return (PyObject *)self;
256 static PyObject *Euler_MakeCompatible(EulerObject * self, EulerObject *value)
258 float eul_from_rad[3];
261 if(!EulerObject_Check(value)) {
262 PyErr_SetString(PyExc_TypeError, "euler.makeCompatible(euler):expected a single euler argument.");
266 if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
270 for(x = 0; x < 3; x++) {
271 self->eul[x] = self->eul[x] * ((float)Py_PI / 180);
272 eul_from_rad[x] = value->eul[x] * ((float)Py_PI / 180);
274 compatible_eul(self->eul, eul_from_rad);
275 //convert back from radians
276 for(x = 0; x < 3; x++) {
277 self->eul[x] *= (180 / (float)Py_PI);
280 BaseMath_WriteCallback(self);
282 return (PyObject *)self;
285 //----------------------------Euler.rotate()-----------------------
286 // return a copy of the euler
287 static PyObject *Euler_copy(EulerObject * self, PyObject *args)
289 if(!BaseMath_ReadCallback(self))
292 return newEulerObject(self->eul, Py_NEW);
295 //----------------------------print object (internal)--------------
296 //print the object to screen
297 static PyObject *Euler_repr(EulerObject * self)
301 if(!BaseMath_ReadCallback(self))
304 sprintf(str, "[%.6f, %.6f, %.6f](euler)", self->eul[0], self->eul[1], self->eul[2]);
305 return PyUnicode_FromString(str);
307 //------------------------tp_richcmpr
308 //returns -1 execption, 0 false, 1 true
309 static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
311 EulerObject *eulA = NULL, *eulB = NULL;
314 if(EulerObject_Check(objectA)) {
315 eulA = (EulerObject*)objectA;
316 if(!BaseMath_ReadCallback(eulA))
319 if(EulerObject_Check(objectB)) {
320 eulB = (EulerObject*)objectB;
321 if(!BaseMath_ReadCallback(eulB))
326 if (comparison_type == Py_NE){
332 eulA = (EulerObject*)objectA;
333 eulB = (EulerObject*)objectB;
335 switch (comparison_type){
337 result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
340 result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
348 printf("The result of the comparison could not be evaluated");
357 //------------------------tp_doc
358 static char EulerObject_doc[] = "This is a wrapper for euler objects.";
359 //---------------------SEQUENCE PROTOCOLS------------------------
360 //----------------------------len(object)------------------------
362 static int Euler_len(EulerObject * self)
366 //----------------------------object[]---------------------------
367 //sequence accessor (get)
368 static PyObject *Euler_item(EulerObject * self, int i)
372 if(i < 0 || i >= 3) {
373 PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
377 if(!BaseMath_ReadIndexCallback(self, i))
380 return PyFloat_FromDouble(self->eul[i]);
383 //----------------------------object[]-------------------------
384 //sequence accessor (set)
385 static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
387 float f = PyFloat_AsDouble(value);
389 if(f == -1 && PyErr_Occurred()) { // parsed item not a number
390 PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
397 PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n");
403 if(!BaseMath_WriteIndexCallback(self, i))
408 //----------------------------object[z:y]------------------------
409 //sequence slice (get)
410 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
412 PyObject *list = NULL;
415 if(!BaseMath_ReadCallback(self))
419 if (end<0) end= 4+end;
421 begin = MIN2(begin,end);
423 list = PyList_New(end - begin);
424 for(count = begin; count < end; count++) {
425 PyList_SetItem(list, count - begin,
426 PyFloat_FromDouble(self->eul[count]));
431 //----------------------------object[z:y]------------------------
432 //sequence slice (set)
433 static int Euler_ass_slice(EulerObject * self, int begin, int end,
440 if(!BaseMath_ReadCallback(self))
444 if (end<0) end= 4+end;
446 begin = MIN2(begin,end);
448 size = PySequence_Length(seq);
449 if(size != (end - begin)){
450 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
454 for (i = 0; i < size; i++) {
455 e = PySequence_GetItem(seq, i);
456 if (e == NULL) { // Failed to read sequence
457 PyErr_SetString(PyExc_RuntimeError, "euler[begin:end] = []: unable to read sequence");
461 eul[i] = (float)PyFloat_AsDouble(e);
464 if(eul[i]==-1 && PyErr_Occurred()) { // parsed item not a number
465 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: sequence argument not a number");
469 //parsed well - now set in vector
470 for(y = 0; y < 3; y++){
471 self->eul[begin + y] = eul[y];
474 BaseMath_WriteCallback(self);
477 //-----------------PROTCOL DECLARATIONS--------------------------
478 static PySequenceMethods Euler_SeqMethods = {
479 (inquiry) Euler_len, /* sq_length */
480 (binaryfunc) 0, /* sq_concat */
481 (ssizeargfunc) 0, /* sq_repeat */
482 (ssizeargfunc) Euler_item, /* sq_item */
483 (ssizessizeargfunc) Euler_slice, /* sq_slice */
484 (ssizeobjargproc) Euler_ass_item, /* sq_ass_item */
485 (ssizessizeobjargproc) Euler_ass_slice, /* sq_ass_slice */
490 * vector axis, vector.x/y/z/w
493 static PyObject *Euler_getAxis( EulerObject * self, void *type )
495 return Euler_item(self, GET_INT_FROM_POINTER(type));
498 static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
500 return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
503 /*****************************************************************************/
504 /* Python attributes get/set structure: */
505 /*****************************************************************************/
506 static PyGetSetDef Euler_getseters[] = {
507 {"x", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler X axis", (void *)0},
508 {"y", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler Y axis", (void *)1},
509 {"z", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler Z axis", (void *)2},
511 {"wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, "True when this wraps blenders internal data", NULL},
512 {"__owner__", (getter)BaseMathObject_getOwner, (setter)NULL, "Read only owner for vectors that depend on another object", NULL},
513 {NULL,NULL,NULL,NULL,NULL} /* Sentinel */
516 //------------------PY_OBECT DEFINITION--------------------------
517 PyTypeObject euler_Type = {
518 #if (PY_VERSION_HEX >= 0x02060000)
519 PyVarObject_HEAD_INIT(NULL, 0)
521 /* python 2.5 and below */
522 PyObject_HEAD_INIT( NULL ) /* required py macro */
526 sizeof(EulerObject), //tp_basicsize
528 (destructor)BaseMathObject_dealloc, //tp_dealloc
533 (reprfunc) Euler_repr, //tp_repr
535 &Euler_SeqMethods, //tp_as_sequence
543 Py_TPFLAGS_DEFAULT, //tp_flags
544 EulerObject_doc, //tp_doc
547 (richcmpfunc)Euler_richcmpr, //tp_richcompare
548 0, //tp_weaklistoffset
551 Euler_methods, //tp_methods
553 Euler_getseters, //tp_getset
571 //------------------------newEulerObject (internal)-------------
572 //creates a new euler object
573 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
574 (i.e. it was allocated elsewhere by MEM_mallocN())
575 pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
576 (i.e. it must be created here with PyMEM_malloc())*/
577 PyObject *newEulerObject(float *eul, int type)
582 self = PyObject_NEW(EulerObject, &euler_Type);
584 /* init callbacks as NULL */
586 self->cb_type= self->cb_subtype= 0;
590 self->wrapped = Py_WRAP;
591 }else if (type == Py_NEW){
592 self->eul = PyMem_Malloc(3 * sizeof(float));
593 if(!eul) { //new empty
594 for(x = 0; x < 3; x++) {
598 VECCOPY(self->eul, eul);
600 self->wrapped = Py_NEW;
604 return (PyObject *)self;
607 PyObject *newEulerObject_cb(PyObject *cb_user, int cb_type, int cb_subtype)
609 EulerObject *self= (EulerObject *)newEulerObject(NULL, Py_NEW);
612 self->cb_user= cb_user;
613 self->cb_type= (unsigned char)cb_type;
614 self->cb_subtype= (unsigned char)cb_subtype;