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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
32 #include "BKE_utildefines.h"
36 //-----------------------------METHODS------------------------------
38 /* note: BaseMath_ReadCallback must be called beforehand */
39 static PyObject *Quaternion_ToTupleExt(QuaternionObject *self, int ndigits)
44 ret= PyTuple_New(QUAT_SIZE);
47 for(i= 0; i < QUAT_SIZE; i++) {
48 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
52 for(i= 0; i < QUAT_SIZE; i++) {
53 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
60 static char Quaternion_ToEuler_doc[] =
61 ".. method:: to_euler(order, euler_compat)\n"
63 " Return Euler representation of the quaternion.\n"
65 " :arg order: Optional rotation order argument in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'].\n"
66 " :type order: string\n"
67 " :arg euler_compat: Optional euler argument the new euler will be made compatible with (no axis flipping between them). Useful for converting a series of matrices to animation curves.\n"
68 " :type euler_compat: :class:`Euler`\n"
69 " :return: Euler representation of the quaternion.\n"
70 " :rtype: :class:`Euler`\n";
72 static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
75 char *order_str= NULL;
76 short order= EULER_ORDER_XYZ;
77 EulerObject *eul_compat = NULL;
79 if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
82 if(!BaseMath_ReadCallback(self))
86 order= euler_order_from_string(order_str, "Matrix.to_euler()");
95 if(!BaseMath_ReadCallback(eul_compat))
98 quat_to_mat3(mat, self->quat);
100 if(order == EULER_ORDER_XYZ) mat3_to_compatible_eul(eul, eul_compat->eul, mat);
101 else mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat);
104 if(order == EULER_ORDER_XYZ) quat_to_eul(eul, self->quat);
105 else quat_to_eulO(eul, order, self->quat);
108 return newEulerObject(eul, order, Py_NEW, NULL);
110 //----------------------------Quaternion.toMatrix()------------------
111 static char Quaternion_ToMatrix_doc[] =
112 ".. method:: to_matrix(other)\n"
114 " Return a matrix representation of the quaternion.\n"
116 " :return: A 3x3 rotation matrix representation of the quaternion.\n"
117 " :rtype: :class:`Matrix`\n";
119 static PyObject *Quaternion_ToMatrix(QuaternionObject * self)
121 float mat[9]; /* all values are set */
123 if(!BaseMath_ReadCallback(self))
126 quat_to_mat3( (float (*)[3]) mat,self->quat);
127 return newMatrixObject(mat, 3, 3, Py_NEW, NULL);
130 //----------------------------Quaternion.cross(other)------------------
131 static char Quaternion_Cross_doc[] =
132 ".. method:: cross(other)\n"
134 " Return the cross product of this quaternion and another.\n"
136 " :arg other: The other quaternion to perform the cross product with.\n"
137 " :type other: :class:`Quaternion`\n"
138 " :return: The cross product.\n"
139 " :rtype: :class:`Quaternion`\n";
141 static PyObject *Quaternion_Cross(QuaternionObject * self, QuaternionObject * value)
143 float quat[QUAT_SIZE];
145 if (!QuaternionObject_Check(value)) {
146 PyErr_SetString( PyExc_TypeError, "quat.cross(value): expected a quaternion argument" );
150 if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
153 mul_qt_qtqt(quat, self->quat, value->quat);
154 return newQuaternionObject(quat, Py_NEW, NULL);
157 //----------------------------Quaternion.dot(other)------------------
158 static char Quaternion_Dot_doc[] =
159 ".. method:: dot(other)\n"
161 " Return the dot product of this quaternion and another.\n"
163 " :arg other: The other quaternion to perform the dot product with.\n"
164 " :type other: :class:`Quaternion`\n"
165 " :return: The dot product.\n"
166 " :rtype: :class:`Quaternion`\n";
168 static PyObject *Quaternion_Dot(QuaternionObject * self, QuaternionObject * value)
170 if (!QuaternionObject_Check(value)) {
171 PyErr_SetString( PyExc_TypeError, "quat.dot(value): expected a quaternion argument" );
175 if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
178 return PyFloat_FromDouble(dot_qtqt(self->quat, value->quat));
181 static char Quaternion_Difference_doc[] =
182 ".. function:: difference(other)\n"
184 " Returns a quaternion representing the rotational difference.\n"
186 " :arg other: second quaternion.\n"
187 " :type other: :class:`Quaternion`\n"
188 " :return: the rotational difference between the two quat rotations.\n"
189 " :rtype: :class:`Quaternion`\n";
191 static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject * value)
193 float quat[QUAT_SIZE];
195 if (!QuaternionObject_Check(value)) {
196 PyErr_SetString( PyExc_TypeError, "quat.difference(value): expected a quaternion argument" );
200 if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
203 rotation_between_quats_to_quat(quat, self->quat, value->quat);
205 return newQuaternionObject(quat, Py_NEW, NULL);
208 static char Quaternion_Slerp_doc[] =
209 ".. function:: slerp(other, factor)\n"
211 " Returns the interpolation of two quaternions.\n"
213 " :arg other: value to interpolate with.\n"
214 " :type other: :class:`Quaternion`\n"
215 " :arg factor: The interpolation value in [0.0, 1.0].\n"
216 " :type factor: float\n"
217 " :return: The interpolated rotation.\n"
218 " :rtype: :class:`Quaternion`\n";
220 static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args)
222 QuaternionObject *value;
223 float quat[QUAT_SIZE], fac;
225 if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) {
226 PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float");
230 if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
233 if(fac > 1.0f || fac < 0.0f) {
234 PyErr_SetString(PyExc_AttributeError, "quat.slerp(): interpolation factor must be between 0.0 and 1.0");
238 interp_qt_qtqt(quat, self->quat, value->quat, fac);
240 return newQuaternionObject(quat, Py_NEW, NULL);
243 //----------------------------Quaternion.normalize()----------------
244 //normalize the axis of rotation of [theta,vector]
245 static char Quaternion_Normalize_doc[] =
246 ".. function:: normalize()\n"
248 " Normalize the quaternion.\n"
250 " :return: an instance of itself.\n"
251 " :rtype: :class:`Quaternion`\n";
253 static PyObject *Quaternion_Normalize(QuaternionObject * self)
255 if(!BaseMath_ReadCallback(self))
258 normalize_qt(self->quat);
260 BaseMath_WriteCallback(self);
262 return (PyObject*)self;
264 //----------------------------Quaternion.inverse()------------------
265 static char Quaternion_Inverse_doc[] =
266 ".. function:: inverse()\n"
268 " Set the quaternion to its inverse.\n"
270 " :return: an instance of itself.\n"
271 " :rtype: :class:`Quaternion`\n";
273 static PyObject *Quaternion_Inverse(QuaternionObject * self)
275 if(!BaseMath_ReadCallback(self))
278 invert_qt(self->quat);
280 BaseMath_WriteCallback(self);
282 return (PyObject*)self;
284 //----------------------------Quaternion.identity()-----------------
285 static char Quaternion_Identity_doc[] =
286 ".. function:: identity()\n"
288 " Set the quaternion to an identity quaternion.\n"
290 " :return: an instance of itself.\n"
291 " :rtype: :class:`Quaternion`\n";
293 static PyObject *Quaternion_Identity(QuaternionObject * self)
295 if(!BaseMath_ReadCallback(self))
300 BaseMath_WriteCallback(self);
302 return (PyObject*)self;
304 //----------------------------Quaternion.negate()-------------------
305 static char Quaternion_Negate_doc[] =
306 ".. function:: negate()\n"
308 " Set the quaternion to its negative.\n"
310 " :return: an instance of itself.\n"
311 " :rtype: :class:`Quaternion`\n";
313 static PyObject *Quaternion_Negate(QuaternionObject * self)
315 if(!BaseMath_ReadCallback(self))
318 mul_qt_fl(self->quat, -1.0f);
320 BaseMath_WriteCallback(self);
322 return (PyObject*)self;
324 //----------------------------Quaternion.conjugate()----------------
325 static char Quaternion_Conjugate_doc[] =
326 ".. function:: conjugate()\n"
328 " Set the quaternion to its conjugate (negate x, y, z).\n"
330 " :return: an instance of itself.\n"
331 " :rtype: :class:`Quaternion`\n";
333 static PyObject *Quaternion_Conjugate(QuaternionObject * self)
335 if(!BaseMath_ReadCallback(self))
338 conjugate_qt(self->quat);
340 BaseMath_WriteCallback(self);
342 return (PyObject*)self;
344 //----------------------------Quaternion.copy()----------------
345 static char Quaternion_copy_doc[] =
346 ".. function:: copy()\n"
348 " Returns a copy of this quaternion.\n"
350 " :return: A copy of the quaternion.\n"
351 " :rtype: :class:`Quaternion`\n"
353 " .. note:: use this to get a copy of a wrapped quaternion with no reference to the original data.\n";
355 static PyObject *Quaternion_copy(QuaternionObject * self)
357 if(!BaseMath_ReadCallback(self))
360 return newQuaternionObject(self->quat, Py_NEW, Py_TYPE(self));
363 //----------------------------print object (internal)--------------
364 //print the object to screen
365 static PyObject *Quaternion_repr(QuaternionObject * self)
367 PyObject *ret, *tuple;
369 if(!BaseMath_ReadCallback(self))
372 tuple= Quaternion_ToTupleExt(self, -1);
374 ret= PyUnicode_FromFormat("Quaternion(%R)", tuple);
380 //------------------------tp_richcmpr
381 //returns -1 execption, 0 false, 1 true
382 static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
384 QuaternionObject *quatA = NULL, *quatB = NULL;
387 if(QuaternionObject_Check(objectA)) {
388 quatA = (QuaternionObject*)objectA;
389 if(!BaseMath_ReadCallback(quatA))
392 if(QuaternionObject_Check(objectB)) {
393 quatB = (QuaternionObject*)objectB;
394 if(!BaseMath_ReadCallback(quatB))
398 if (!quatA || !quatB){
399 if (comparison_type == Py_NE){
406 switch (comparison_type){
408 result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
411 result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
419 printf("The result of the comparison could not be evaluated");
429 //---------------------SEQUENCE PROTOCOLS------------------------
430 //----------------------------len(object)------------------------
432 static int Quaternion_len(QuaternionObject * self)
436 //----------------------------object[]---------------------------
437 //sequence accessor (get)
438 static PyObject *Quaternion_item(QuaternionObject * self, int i)
440 if(i<0) i= QUAT_SIZE-i;
442 if(i < 0 || i >= QUAT_SIZE) {
443 PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n");
447 if(!BaseMath_ReadIndexCallback(self, i))
450 return PyFloat_FromDouble(self->quat[i]);
453 //----------------------------object[]-------------------------
454 //sequence accessor (set)
455 static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
457 float scalar= (float)PyFloat_AsDouble(ob);
458 if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
459 PyErr_SetString(PyExc_TypeError, "quaternion[index] = x: index argument not a number\n");
463 if(i<0) i= QUAT_SIZE-i;
465 if(i < 0 || i >= QUAT_SIZE){
466 PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n");
469 self->quat[i] = scalar;
471 if(!BaseMath_WriteIndexCallback(self, i))
476 //----------------------------object[z:y]------------------------
477 //sequence slice (get)
478 static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
480 PyObject *list = NULL;
483 if(!BaseMath_ReadCallback(self))
486 CLAMP(begin, 0, QUAT_SIZE);
487 if (end<0) end= (QUAT_SIZE + 1) + end;
488 CLAMP(end, 0, QUAT_SIZE);
489 begin = MIN2(begin,end);
491 list = PyList_New(end - begin);
492 for(count = begin; count < end; count++) {
493 PyList_SetItem(list, count - begin,
494 PyFloat_FromDouble(self->quat[count]));
499 //----------------------------object[z:y]------------------------
500 //sequence slice (set)
501 static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq)
504 float quat[QUAT_SIZE];
506 if(!BaseMath_ReadCallback(self))
509 CLAMP(begin, 0, QUAT_SIZE);
510 if (end<0) end= (QUAT_SIZE + 1) + end;
511 CLAMP(end, 0, QUAT_SIZE);
512 begin = MIN2(begin,end);
514 if((size=mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
517 if(size != (end - begin)){
518 PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment");
522 /* parsed well - now set in vector */
523 for(i= 0; i < size; i++)
524 self->quat[begin + i] = quat[i];
526 BaseMath_WriteCallback(self);
531 static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
533 if (PyIndex_Check(item)) {
535 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
536 if (i == -1 && PyErr_Occurred())
540 return Quaternion_item(self, i);
541 } else if (PySlice_Check(item)) {
542 Py_ssize_t start, stop, step, slicelength;
544 if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
547 if (slicelength <= 0) {
548 return PyList_New(0);
550 else if (step == 1) {
551 return Quaternion_slice(self, start, stop);
554 PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternions");
559 PyErr_Format(PyExc_TypeError,
560 "quaternion indices must be integers, not %.200s",
561 item->ob_type->tp_name);
567 static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
569 if (PyIndex_Check(item)) {
570 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
571 if (i == -1 && PyErr_Occurred())
575 return Quaternion_ass_item(self, i, value);
577 else if (PySlice_Check(item)) {
578 Py_ssize_t start, stop, step, slicelength;
580 if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
584 return Quaternion_ass_slice(self, start, stop, value);
586 PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternion");
591 PyErr_Format(PyExc_TypeError,
592 "quaternion indices must be integers, not %.200s",
593 item->ob_type->tp_name);
598 //------------------------NUMERIC PROTOCOLS----------------------
599 //------------------------obj + obj------------------------------
601 static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
603 float quat[QUAT_SIZE];
604 QuaternionObject *quat1 = NULL, *quat2 = NULL;
606 if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
607 PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
610 quat1 = (QuaternionObject*)q1;
611 quat2 = (QuaternionObject*)q2;
613 if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
616 add_qt_qtqt(quat, quat1->quat, quat2->quat, 1.0f);
617 return newQuaternionObject(quat, Py_NEW, NULL);
619 //------------------------obj - obj------------------------------
621 static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
624 float quat[QUAT_SIZE];
625 QuaternionObject *quat1 = NULL, *quat2 = NULL;
627 if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
628 PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
632 quat1 = (QuaternionObject*)q1;
633 quat2 = (QuaternionObject*)q2;
635 if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
638 for(x = 0; x < QUAT_SIZE; x++) {
639 quat[x] = quat1->quat[x] - quat2->quat[x];
642 return newQuaternionObject(quat, Py_NEW, NULL);
644 //------------------------obj * obj------------------------------
646 static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
648 float quat[QUAT_SIZE], scalar;
649 QuaternionObject *quat1 = NULL, *quat2 = NULL;
650 VectorObject *vec = NULL;
652 if(QuaternionObject_Check(q1)) {
653 quat1 = (QuaternionObject*)q1;
654 if(!BaseMath_ReadCallback(quat1))
657 if(QuaternionObject_Check(q2)) {
658 quat2 = (QuaternionObject*)q2;
659 if(!BaseMath_ReadCallback(quat2))
663 if(quat1 && quat2) { /* QUAT*QUAT (cross product) */
664 mul_qt_qtqt(quat, quat1->quat, quat2->quat);
665 return newQuaternionObject(quat, Py_NEW, NULL);
668 /* the only case this can happen (for a supported type is "FLOAT*QUAT" ) */
669 if(!QuaternionObject_Check(q1)) {
670 scalar= PyFloat_AsDouble(q1);
671 if ((scalar == -1.0 && PyErr_Occurred())==0) { /* FLOAT*QUAT */
672 QUATCOPY(quat, quat2->quat);
673 mul_qt_fl(quat, scalar);
674 return newQuaternionObject(quat, Py_NEW, NULL);
676 PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: val * quat, val is not an acceptable type");
679 else { /* QUAT*SOMETHING */
680 if(VectorObject_Check(q2)){ /* QUAT*VEC */
682 vec = (VectorObject*)q2;
684 PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: only 3D vector rotations currently supported\n");
687 if(!BaseMath_ReadCallback(vec)) {
691 copy_v3_v3(tvec, vec->vec);
692 mul_qt_v3(quat1->quat, tvec);
693 return newVectorObject(tvec, 3, Py_NEW, NULL);
696 scalar= PyFloat_AsDouble(q2);
697 if ((scalar == -1.0 && PyErr_Occurred())==0) { /* QUAT*FLOAT */
698 QUATCOPY(quat, quat1->quat);
699 mul_qt_fl(quat, scalar);
700 return newQuaternionObject(quat, Py_NEW, NULL);
704 PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: arguments not acceptable for this operation\n");
708 //-----------------PROTOCOL DECLARATIONS--------------------------
709 static PySequenceMethods Quaternion_SeqMethods = {
710 (lenfunc) Quaternion_len, /* sq_length */
711 (binaryfunc) NULL, /* sq_concat */
712 (ssizeargfunc) NULL, /* sq_repeat */
713 (ssizeargfunc) Quaternion_item, /* sq_item */
714 (ssizessizeargfunc) NULL, /* sq_slice, deprecated */
715 (ssizeobjargproc) Quaternion_ass_item, /* sq_ass_item */
716 (ssizessizeobjargproc) NULL, /* sq_ass_slice, deprecated */
717 (objobjproc) NULL, /* sq_contains */
718 (binaryfunc) NULL, /* sq_inplace_concat */
719 (ssizeargfunc) NULL, /* sq_inplace_repeat */
722 static PyMappingMethods Quaternion_AsMapping = {
723 (lenfunc)Quaternion_len,
724 (binaryfunc)Quaternion_subscript,
725 (objobjargproc)Quaternion_ass_subscript
728 static PyNumberMethods Quaternion_NumMethods = {
729 (binaryfunc) Quaternion_add, /*nb_add*/
730 (binaryfunc) Quaternion_sub, /*nb_subtract*/
731 (binaryfunc) Quaternion_mul, /*nb_multiply*/
735 (unaryfunc) 0, /*nb_negative*/
736 (unaryfunc) 0, /*tp_positive*/
737 (unaryfunc) 0, /*tp_absolute*/
738 (inquiry) 0, /*tp_bool*/
739 (unaryfunc) 0, /*nb_invert*/
741 (binaryfunc)0, /*nb_rshift*/
748 0, /* nb_inplace_add */
749 0, /* nb_inplace_subtract */
750 0, /* nb_inplace_multiply */
751 0, /* nb_inplace_remainder */
752 0, /* nb_inplace_power */
753 0, /* nb_inplace_lshift */
754 0, /* nb_inplace_rshift */
755 0, /* nb_inplace_and */
756 0, /* nb_inplace_xor */
757 0, /* nb_inplace_or */
758 0, /* nb_floor_divide */
759 0, /* nb_true_divide */
760 0, /* nb_inplace_floor_divide */
761 0, /* nb_inplace_true_divide */
765 static PyObject *Quaternion_getAxis( QuaternionObject * self, void *type )
767 return Quaternion_item(self, GET_INT_FROM_POINTER(type));
770 static int Quaternion_setAxis( QuaternionObject * self, PyObject * value, void * type )
772 return Quaternion_ass_item(self, GET_INT_FROM_POINTER(type), value);
775 static PyObject *Quaternion_getMagnitude( QuaternionObject * self, void *type )
777 if(!BaseMath_ReadCallback(self))
780 return PyFloat_FromDouble(sqrt(dot_qtqt(self->quat, self->quat)));
783 static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
785 if(!BaseMath_ReadCallback(self))
788 return PyFloat_FromDouble(2.0 * (saacos(self->quat[0])));
791 static int Quaternion_setAngle(QuaternionObject * self, PyObject * value, void * type)
796 if(!BaseMath_ReadCallback(self))
799 quat_to_axis_angle(axis, &angle, self->quat);
801 angle = PyFloat_AsDouble(value);
803 if(angle==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
804 PyErr_SetString(PyExc_TypeError, "quaternion.angle = value: float expected");
808 /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
809 if( EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
810 EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
811 EXPP_FloatsAreEqual(axis[2], 0.0f, 10)
816 axis_angle_to_quat(self->quat, axis, angle);
818 if(!BaseMath_WriteCallback(self))
824 static PyObject *Quaternion_getAxisVec(QuaternionObject *self, void *type)
829 if(!BaseMath_ReadCallback(self))
832 quat_to_axis_angle(axis, &angle, self->quat);
834 /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
835 if( EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
836 EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
837 EXPP_FloatsAreEqual(axis[2], 0.0f, 10)
842 return (PyObject *) newVectorObject(axis, 3, Py_NEW, NULL);
845 static int Quaternion_setAxisVec(QuaternionObject *self, PyObject *value, void *type)
852 if(!BaseMath_ReadCallback(self))
855 quat_to_axis_angle(axis, &angle, self->quat);
857 if(!VectorObject_Check(value)) {
858 PyErr_SetString(PyExc_TypeError, "quaternion.axis = value: expected a 3D Vector");
862 vec= (VectorObject *)value;
863 if(!BaseMath_ReadCallback(vec))
866 axis_angle_to_quat(self->quat, vec->vec, angle);
868 if(!BaseMath_WriteCallback(self))
874 //----------------------------------mathutils.Quaternion() --------------
875 static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
879 float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
881 if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle))
884 switch(PyTuple_GET_SIZE(args)) {
888 if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
892 if (mathutils_array_parse(quat, 3, 3, seq, "mathutils.Quaternion()") == -1)
895 axis_angle_to_quat(quat, quat, angle);
897 /* PyArg_ParseTuple assures no more then 2 */
899 return newQuaternionObject(quat, Py_NEW, NULL);
903 //-----------------------METHOD DEFINITIONS ----------------------
904 static struct PyMethodDef Quaternion_methods[] = {
905 {"identity", (PyCFunction) Quaternion_Identity, METH_NOARGS, Quaternion_Identity_doc},
906 {"negate", (PyCFunction) Quaternion_Negate, METH_NOARGS, Quaternion_Negate_doc},
907 {"conjugate", (PyCFunction) Quaternion_Conjugate, METH_NOARGS, Quaternion_Conjugate_doc},
908 {"inverse", (PyCFunction) Quaternion_Inverse, METH_NOARGS, Quaternion_Inverse_doc},
909 {"normalize", (PyCFunction) Quaternion_Normalize, METH_NOARGS, Quaternion_Normalize_doc},
910 {"to_euler", (PyCFunction) Quaternion_ToEuler, METH_VARARGS, Quaternion_ToEuler_doc},
911 {"to_matrix", (PyCFunction) Quaternion_ToMatrix, METH_NOARGS, Quaternion_ToMatrix_doc},
912 {"cross", (PyCFunction) Quaternion_Cross, METH_O, Quaternion_Cross_doc},
913 {"dot", (PyCFunction) Quaternion_Dot, METH_O, Quaternion_Dot_doc},
914 {"difference", (PyCFunction) Quaternion_Difference, METH_O, Quaternion_Difference_doc},
915 {"slerp", (PyCFunction) Quaternion_Slerp, METH_VARARGS, Quaternion_Slerp_doc},
916 {"__copy__", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
917 {"copy", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
918 {NULL, NULL, 0, NULL}
921 /*****************************************************************************/
922 /* Python attributes get/set structure: */
923 /*****************************************************************************/
924 static PyGetSetDef Quaternion_getseters[] = {
925 {"w", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion W value. **type** float", (void *)0},
926 {"x", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion X axis. **type** float", (void *)1},
927 {"y", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Y axis. **type** float", (void *)2},
928 {"z", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Z axis. **type** float", (void *)3},
929 {"magnitude", (getter)Quaternion_getMagnitude, (setter)NULL, "Size of the quaternion (readonly). **type** float", NULL},
930 {"angle", (getter)Quaternion_getAngle, (setter)Quaternion_setAngle, "angle of the quaternion. **type** float", NULL},
931 {"axis",(getter)Quaternion_getAxisVec, (setter)Quaternion_setAxisVec, "quaternion axis as a vector. **type** :class:`Vector`", NULL},
932 {"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL},
933 {"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL},
934 {NULL,NULL,NULL,NULL,NULL} /* Sentinel */
937 //------------------PY_OBECT DEFINITION--------------------------
938 static char quaternion_doc[] =
939 "This object gives access to Quaternions in Blender.";
941 PyTypeObject quaternion_Type = {
942 PyVarObject_HEAD_INIT(NULL, 0)
943 "quaternion", //tp_name
944 sizeof(QuaternionObject), //tp_basicsize
946 (destructor)BaseMathObject_dealloc, //tp_dealloc
951 (reprfunc) Quaternion_repr, //tp_repr
952 &Quaternion_NumMethods, //tp_as_number
953 &Quaternion_SeqMethods, //tp_as_sequence
954 &Quaternion_AsMapping, //tp_as_mapping
961 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
962 quaternion_doc, //tp_doc
965 (richcmpfunc)Quaternion_richcmpr, //tp_richcompare
966 0, //tp_weaklistoffset
969 Quaternion_methods, //tp_methods
971 Quaternion_getseters, //tp_getset
979 Quaternion_new, //tp_new
989 //------------------------newQuaternionObject (internal)-------------
990 //creates a new quaternion object
991 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
992 (i.e. it was allocated elsewhere by MEM_mallocN())
993 pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
994 (i.e. it must be created here with PyMEM_malloc())*/
995 PyObject *newQuaternionObject(float *quat, int type, PyTypeObject *base_type)
997 QuaternionObject *self;
999 if(base_type) self = (QuaternionObject *)base_type->tp_alloc(base_type, 0);
1000 else self = PyObject_NEW(QuaternionObject, &quaternion_Type);
1002 /* init callbacks as NULL */
1003 self->cb_user= NULL;
1004 self->cb_type= self->cb_subtype= 0;
1006 if(type == Py_WRAP){
1008 self->wrapped = Py_WRAP;
1009 }else if (type == Py_NEW){
1010 self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
1011 if(!quat) { //new empty
1012 unit_qt(self->quat);
1014 QUATCOPY(self->quat, quat);
1016 self->wrapped = Py_NEW;
1020 return (PyObject *) self;
1023 PyObject *newQuaternionObject_cb(PyObject *cb_user, int cb_type, int cb_subtype)
1025 QuaternionObject *self= (QuaternionObject *)newQuaternionObject(NULL, Py_NEW, NULL);
1028 self->cb_user= cb_user;
1029 self->cb_type= (unsigned char)cb_type;
1030 self->cb_subtype= (unsigned char)cb_subtype;
1033 return (PyObject *)self;