2
* $Id: mathutils_quat.c 28786 2010-05-16 10:09:07Z campbellbarton $
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], tempQuat[QUAT_SIZE];
197
if (!QuaternionObject_Check(value)) {
198
PyErr_SetString( PyExc_TypeError, "quat.difference(value): expected a quaternion argument" );
202
if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
205
copy_qt_qt(tempQuat, self->quat);
206
conjugate_qt(tempQuat);
207
dot = sqrt(dot_qtqt(tempQuat, tempQuat));
209
for(x = 0; x < QUAT_SIZE; x++) {
210
tempQuat[x] /= (float)(dot * dot);
212
mul_qt_qtqt(quat, tempQuat, value->quat);
213
return newQuaternionObject(quat, Py_NEW, NULL);
216
static char Quaternion_Slerp_doc[] =
217
".. function:: slerp(other, factor)\n"
219
" Returns the interpolation of two quaternions.\n"
221
" :arg other: value to interpolate with.\n"
222
" :type other: :class:`Quaternion`\n"
223
" :arg factor: The interpolation value in [0.0, 1.0].\n"
224
" :type factor: float\n"
225
" :return: The interpolated rotation.\n"
226
" :rtype: :class:`Quaternion`\n";
228
static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args)
230
QuaternionObject *value;
231
float quat[QUAT_SIZE], fac;
233
if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) {
234
PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float");
238
if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
241
if(fac > 1.0f || fac < 0.0f) {
242
PyErr_SetString(PyExc_AttributeError, "quat.slerp(): interpolation factor must be between 0.0 and 1.0");
246
interp_qt_qtqt(quat, self->quat, value->quat, fac);
248
return newQuaternionObject(quat, Py_NEW, NULL);
251
//----------------------------Quaternion.normalize()----------------
252
//normalize the axis of rotation of [theta,vector]
253
static char Quaternion_Normalize_doc[] =
254
".. function:: normalize()\n"
256
" Normalize the quaternion.\n"
258
" :return: an instance of itself.\n"
259
" :rtype: :class:`Quaternion`\n";
261
static PyObject *Quaternion_Normalize(QuaternionObject * self)
263
if(!BaseMath_ReadCallback(self))
266
normalize_qt(self->quat);
268
BaseMath_WriteCallback(self);
270
return (PyObject*)self;
272
//----------------------------Quaternion.inverse()------------------
273
static char Quaternion_Inverse_doc[] =
274
".. function:: inverse()\n"
276
" Set the quaternion to its inverse.\n"
278
" :return: an instance of itself.\n"
279
" :rtype: :class:`Quaternion`\n";
281
static PyObject *Quaternion_Inverse(QuaternionObject * self)
283
if(!BaseMath_ReadCallback(self))
286
invert_qt(self->quat);
288
BaseMath_WriteCallback(self);
290
return (PyObject*)self;
292
//----------------------------Quaternion.identity()-----------------
293
static char Quaternion_Identity_doc[] =
294
".. function:: identity()\n"
296
" Set the quaternion to an identity quaternion.\n"
298
" :return: an instance of itself.\n"
299
" :rtype: :class:`Quaternion`\n";
301
static PyObject *Quaternion_Identity(QuaternionObject * self)
303
if(!BaseMath_ReadCallback(self))
308
BaseMath_WriteCallback(self);
310
return (PyObject*)self;
312
//----------------------------Quaternion.negate()-------------------
313
static char Quaternion_Negate_doc[] =
314
".. function:: negate()\n"
316
" Set the quaternion to its negative.\n"
318
" :return: an instance of itself.\n"
319
" :rtype: :class:`Quaternion`\n";
321
static PyObject *Quaternion_Negate(QuaternionObject * self)
323
if(!BaseMath_ReadCallback(self))
326
mul_qt_fl(self->quat, -1.0f);
328
BaseMath_WriteCallback(self);
330
return (PyObject*)self;
332
//----------------------------Quaternion.conjugate()----------------
333
static char Quaternion_Conjugate_doc[] =
334
".. function:: conjugate()\n"
336
" Set the quaternion to its conjugate (negate x, y, z).\n"
338
" :return: an instance of itself.\n"
339
" :rtype: :class:`Quaternion`\n";
341
static PyObject *Quaternion_Conjugate(QuaternionObject * self)
343
if(!BaseMath_ReadCallback(self))
346
conjugate_qt(self->quat);
348
BaseMath_WriteCallback(self);
350
return (PyObject*)self;
352
//----------------------------Quaternion.copy()----------------
353
static char Quaternion_copy_doc[] =
354
".. function:: copy()\n"
356
" Returns a copy of this quaternion.\n"
358
" :return: A copy of the quaternion.\n"
359
" :rtype: :class:`Quaternion`\n"
361
" .. note:: use this to get a copy of a wrapped quaternion with no reference to the original data.\n";
363
static PyObject *Quaternion_copy(QuaternionObject * self)
365
if(!BaseMath_ReadCallback(self))
368
return newQuaternionObject(self->quat, Py_NEW, Py_TYPE(self));
371
//----------------------------print object (internal)--------------
372
//print the object to screen
373
static PyObject *Quaternion_repr(QuaternionObject * self)
375
PyObject *ret, *tuple;
377
if(!BaseMath_ReadCallback(self))
380
tuple= Quaternion_ToTupleExt(self, -1);
382
ret= PyUnicode_FromFormat("Quaternion(%R)", tuple);
388
//------------------------tp_richcmpr
389
//returns -1 execption, 0 false, 1 true
390
static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
392
QuaternionObject *quatA = NULL, *quatB = NULL;
395
if(QuaternionObject_Check(objectA)) {
396
quatA = (QuaternionObject*)objectA;
397
if(!BaseMath_ReadCallback(quatA))
400
if(QuaternionObject_Check(objectB)) {
401
quatB = (QuaternionObject*)objectB;
402
if(!BaseMath_ReadCallback(quatB))
406
if (!quatA || !quatB){
407
if (comparison_type == Py_NE){
414
switch (comparison_type){
416
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
419
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
427
printf("The result of the comparison could not be evaluated");
437
//---------------------SEQUENCE PROTOCOLS------------------------
438
//----------------------------len(object)------------------------
440
static int Quaternion_len(QuaternionObject * self)
444
//----------------------------object[]---------------------------
445
//sequence accessor (get)
446
static PyObject *Quaternion_item(QuaternionObject * self, int i)
448
if(i<0) i= QUAT_SIZE-i;
450
if(i < 0 || i >= QUAT_SIZE) {
451
PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n");
455
if(!BaseMath_ReadIndexCallback(self, i))
458
return PyFloat_FromDouble(self->quat[i]);
461
//----------------------------object[]-------------------------
462
//sequence accessor (set)
463
static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
465
float scalar= (float)PyFloat_AsDouble(ob);
466
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
467
PyErr_SetString(PyExc_TypeError, "quaternion[index] = x: index argument not a number\n");
471
if(i<0) i= QUAT_SIZE-i;
473
if(i < 0 || i >= QUAT_SIZE){
474
PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n");
477
self->quat[i] = scalar;
479
if(!BaseMath_WriteIndexCallback(self, i))
484
//----------------------------object[z:y]------------------------
485
//sequence slice (get)
486
static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
488
PyObject *list = NULL;
491
if(!BaseMath_ReadCallback(self))
494
CLAMP(begin, 0, QUAT_SIZE);
495
if (end<0) end= (QUAT_SIZE + 1) + end;
496
CLAMP(end, 0, QUAT_SIZE);
497
begin = MIN2(begin,end);
499
list = PyList_New(end - begin);
500
for(count = begin; count < end; count++) {
501
PyList_SetItem(list, count - begin,
502
PyFloat_FromDouble(self->quat[count]));
507
//----------------------------object[z:y]------------------------
508
//sequence slice (set)
509
static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq)
512
float quat[QUAT_SIZE];
514
if(!BaseMath_ReadCallback(self))
517
CLAMP(begin, 0, QUAT_SIZE);
518
if (end<0) end= (QUAT_SIZE + 1) + end;
519
CLAMP(end, 0, QUAT_SIZE);
520
begin = MIN2(begin,end);
522
if((size=mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
525
if(size != (end - begin)){
526
PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment");
530
/* parsed well - now set in vector */
531
for(i= 0; i < size; i++)
532
self->quat[begin + i] = quat[i];
534
BaseMath_WriteCallback(self);
539
static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
541
if (PyIndex_Check(item)) {
543
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
544
if (i == -1 && PyErr_Occurred())
548
return Quaternion_item(self, i);
549
} else if (PySlice_Check(item)) {
550
Py_ssize_t start, stop, step, slicelength;
552
if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
555
if (slicelength <= 0) {
556
return PyList_New(0);
558
else if (step == 1) {
559
return Quaternion_slice(self, start, stop);
562
PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternions");
567
PyErr_Format(PyExc_TypeError,
568
"quaternion indices must be integers, not %.200s",
569
item->ob_type->tp_name);
575
static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
577
if (PyIndex_Check(item)) {
578
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
579
if (i == -1 && PyErr_Occurred())
583
return Quaternion_ass_item(self, i, value);
585
else if (PySlice_Check(item)) {
586
Py_ssize_t start, stop, step, slicelength;
588
if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
592
return Quaternion_ass_slice(self, start, stop, value);
594
PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternion");
599
PyErr_Format(PyExc_TypeError,
600
"quaternion indices must be integers, not %.200s",
601
item->ob_type->tp_name);
606
//------------------------NUMERIC PROTOCOLS----------------------
607
//------------------------obj + obj------------------------------
609
static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
611
float quat[QUAT_SIZE];
612
QuaternionObject *quat1 = NULL, *quat2 = NULL;
614
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
615
PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
618
quat1 = (QuaternionObject*)q1;
619
quat2 = (QuaternionObject*)q2;
621
if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
624
add_qt_qtqt(quat, quat1->quat, quat2->quat, 1.0f);
625
return newQuaternionObject(quat, Py_NEW, NULL);
627
//------------------------obj - obj------------------------------
629
static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
632
float quat[QUAT_SIZE];
633
QuaternionObject *quat1 = NULL, *quat2 = NULL;
635
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
636
PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
640
quat1 = (QuaternionObject*)q1;
641
quat2 = (QuaternionObject*)q2;
643
if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
646
for(x = 0; x < QUAT_SIZE; x++) {
647
quat[x] = quat1->quat[x] - quat2->quat[x];
650
return newQuaternionObject(quat, Py_NEW, NULL);
652
//------------------------obj * obj------------------------------
654
static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
656
float quat[QUAT_SIZE], scalar;
657
QuaternionObject *quat1 = NULL, *quat2 = NULL;
658
VectorObject *vec = NULL;
660
if(QuaternionObject_Check(q1)) {
661
quat1 = (QuaternionObject*)q1;
662
if(!BaseMath_ReadCallback(quat1))
665
if(QuaternionObject_Check(q2)) {
666
quat2 = (QuaternionObject*)q2;
667
if(!BaseMath_ReadCallback(quat2))
671
if(quat1 && quat2) { /* QUAT*QUAT (dot product) */
672
return PyFloat_FromDouble(dot_qtqt(quat1->quat, quat2->quat));
675
/* the only case this can happen (for a supported type is "FLOAT*QUAT" ) */
676
if(!QuaternionObject_Check(q1)) {
677
scalar= PyFloat_AsDouble(q1);
678
if ((scalar == -1.0 && PyErr_Occurred())==0) { /* FLOAT*QUAT */
679
QUATCOPY(quat, quat2->quat);
680
mul_qt_fl(quat, scalar);
681
return newQuaternionObject(quat, Py_NEW, NULL);
683
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: val * quat, val is not an acceptable type");
686
else { /* QUAT*SOMETHING */
687
if(VectorObject_Check(q2)){ /* QUAT*VEC */
688
vec = (VectorObject*)q2;
690
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: only 3D vector rotations currently supported\n");
693
return quat_rotation((PyObject*)quat1, (PyObject*)vec); /* vector updating done inside the func */
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
return PyFloat_FromDouble(sqrt(dot_qtqt(self->quat, self->quat)));
780
static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
782
return PyFloat_FromDouble(2.0 * (saacos(self->quat[0])));
785
static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type )
789
normalize_v3_v3(vec, self->quat+1);
791
/* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
792
if( EXPP_FloatsAreEqual(vec[0], 0.0f, 10) &&
793
EXPP_FloatsAreEqual(vec[1], 0.0f, 10) &&
794
EXPP_FloatsAreEqual(vec[2], 0.0f, 10) ){
797
return (PyObject *) newVectorObject(vec, 3, Py_NEW, NULL);
800
//----------------------------------mathutils.Quaternion() --------------
801
static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
805
float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
807
if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle))
810
switch(PyTuple_GET_SIZE(args)) {
814
if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
818
if (mathutils_array_parse(quat, 3, 3, seq, "mathutils.Quaternion()") == -1)
821
axis_angle_to_quat(quat, quat, angle);
823
/* PyArg_ParseTuple assures no more then 2 */
825
return newQuaternionObject(quat, Py_NEW, NULL);
829
//-----------------------METHOD DEFINITIONS ----------------------
830
static struct PyMethodDef Quaternion_methods[] = {
831
{"identity", (PyCFunction) Quaternion_Identity, METH_NOARGS, Quaternion_Identity_doc},
832
{"negate", (PyCFunction) Quaternion_Negate, METH_NOARGS, Quaternion_Negate_doc},
833
{"conjugate", (PyCFunction) Quaternion_Conjugate, METH_NOARGS, Quaternion_Conjugate_doc},
834
{"inverse", (PyCFunction) Quaternion_Inverse, METH_NOARGS, Quaternion_Inverse_doc},
835
{"normalize", (PyCFunction) Quaternion_Normalize, METH_NOARGS, Quaternion_Normalize_doc},
836
{"to_euler", (PyCFunction) Quaternion_ToEuler, METH_VARARGS, Quaternion_ToEuler_doc},
837
{"to_matrix", (PyCFunction) Quaternion_ToMatrix, METH_NOARGS, Quaternion_ToMatrix_doc},
838
{"cross", (PyCFunction) Quaternion_Cross, METH_O, Quaternion_Cross_doc},
839
{"dot", (PyCFunction) Quaternion_Dot, METH_O, Quaternion_Dot_doc},
840
{"difference", (PyCFunction) Quaternion_Difference, METH_O, Quaternion_Difference_doc},
841
{"slerp", (PyCFunction) Quaternion_Slerp, METH_VARARGS, Quaternion_Slerp_doc},
842
{"__copy__", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
843
{"copy", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
844
{NULL, NULL, 0, NULL}
847
/*****************************************************************************/
848
/* Python attributes get/set structure: */
849
/*****************************************************************************/
850
static PyGetSetDef Quaternion_getseters[] = {
851
{"w", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion W value. **type** float", (void *)0},
852
{"x", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion X axis. **type** float", (void *)1},
853
{"y", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Y axis. **type** float", (void *)2},
854
{"z", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Z axis. **type** float", (void *)3},
855
{"magnitude", (getter)Quaternion_getMagnitude, (setter)NULL, "Size of the quaternion (readonly). **type** float", NULL},
856
{"angle", (getter)Quaternion_getAngle, (setter)NULL, "angle of the quaternion (readonly). **type** float", NULL},
857
{"axis",(getter)Quaternion_getAxisVec, (setter)NULL, "quaternion axis as a vector (readonly). **type** :class:`Vector`", NULL},
858
{"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL},
859
{"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL},
860
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
863
//------------------PY_OBECT DEFINITION--------------------------
864
static char quaternion_doc[] =
865
"This object gives access to Quaternions in Blender.";
867
PyTypeObject quaternion_Type = {
868
PyVarObject_HEAD_INIT(NULL, 0)
869
"quaternion", //tp_name
870
sizeof(QuaternionObject), //tp_basicsize
872
(destructor)BaseMathObject_dealloc, //tp_dealloc
877
(reprfunc) Quaternion_repr, //tp_repr
878
&Quaternion_NumMethods, //tp_as_number
879
&Quaternion_SeqMethods, //tp_as_sequence
880
&Quaternion_AsMapping, //tp_as_mapping
887
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
888
quaternion_doc, //tp_doc
891
(richcmpfunc)Quaternion_richcmpr, //tp_richcompare
892
0, //tp_weaklistoffset
895
Quaternion_methods, //tp_methods
897
Quaternion_getseters, //tp_getset
905
Quaternion_new, //tp_new
915
//------------------------newQuaternionObject (internal)-------------
916
//creates a new quaternion object
917
/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
918
(i.e. it was allocated elsewhere by MEM_mallocN())
919
pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
920
(i.e. it must be created here with PyMEM_malloc())*/
921
PyObject *newQuaternionObject(float *quat, int type, PyTypeObject *base_type)
923
QuaternionObject *self;
925
if(base_type) self = (QuaternionObject *)base_type->tp_alloc(base_type, 0);
926
else self = PyObject_NEW(QuaternionObject, &quaternion_Type);
928
/* init callbacks as NULL */
930
self->cb_type= self->cb_subtype= 0;
934
self->wrapped = Py_WRAP;
935
}else if (type == Py_NEW){
936
self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
937
if(!quat) { //new empty
940
QUATCOPY(self->quat, quat);
942
self->wrapped = Py_NEW;
946
return (PyObject *) self;
949
PyObject *newQuaternionObject_cb(PyObject *cb_user, int cb_type, int cb_subtype)
951
QuaternionObject *self= (QuaternionObject *)newQuaternionObject(NULL, Py_NEW, NULL);
954
self->cb_user= cb_user;
955
self->cb_type= (unsigned char)cb_type;
956
self->cb_subtype= (unsigned char)cb_subtype;
959
return (PyObject *)self;