pep8 edits and avoid naming conflicts with python builtins
[blender.git] / source / blender / python / mathutils / mathutils_Quaternion.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Joseph Gilbert
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/python/mathutils/mathutils_Quaternion.c
28  *  \ingroup pymathutils
29  */
30
31
32 #include <Python.h>
33
34 #include "mathutils.h"
35
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38
39 #define QUAT_SIZE 4
40
41 static PyObject *quat__apply_to_copy(PyNoArgsFunction quat_func, QuaternionObject *self);
42 static void      quat__axis_angle_sanitize(float axis[3], float *angle);
43 static PyObject *Quaternion_copy(QuaternionObject *self);
44
45 //-----------------------------METHODS------------------------------
46
47 /* note: BaseMath_ReadCallback must be called beforehand */
48 static PyObject *Quaternion_to_tuple_ext(QuaternionObject *self, int ndigits)
49 {
50         PyObject *ret;
51         int i;
52
53         ret= PyTuple_New(QUAT_SIZE);
54
55         if (ndigits >= 0) {
56                 for (i= 0; i < QUAT_SIZE; i++) {
57                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
58                 }
59         }
60         else {
61                 for (i= 0; i < QUAT_SIZE; i++) {
62                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
63                 }
64         }
65
66         return ret;
67 }
68
69 PyDoc_STRVAR(Quaternion_to_euler_doc,
70 ".. method:: to_euler(order, euler_compat)\n"
71 "\n"
72 "   Return Euler representation of the quaternion.\n"
73 "\n"
74 "   :arg order: Optional rotation order argument in\n"
75 "      ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'].\n"
76 "   :type order: string\n"
77 "   :arg euler_compat: Optional euler argument the new euler will be made\n"
78 "      compatible with (no axis flipping between them).\n"
79 "      Useful for converting a series of matrices to animation curves.\n"
80 "   :type euler_compat: :class:`Euler`\n"
81 "   :return: Euler representation of the quaternion.\n"
82 "   :rtype: :class:`Euler`\n"
83 );
84 static PyObject *Quaternion_to_euler(QuaternionObject *self, PyObject *args)
85 {
86         float tquat[4];
87         float eul[3];
88         const char *order_str= NULL;
89         short order= EULER_ORDER_XYZ;
90         EulerObject *eul_compat = NULL;
91
92         if (!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
93                 return NULL;
94
95         if (BaseMath_ReadCallback(self) == -1)
96                 return NULL;
97
98         if (order_str) {
99                 order= euler_order_from_string(order_str, "Matrix.to_euler()");
100
101                 if (order == -1)
102                         return NULL;
103         }
104
105         normalize_qt_qt(tquat, self->quat);
106
107         if (eul_compat) {
108                 float mat[3][3];
109
110                 if (BaseMath_ReadCallback(eul_compat) == -1)
111                         return NULL;
112
113                 quat_to_mat3(mat, tquat);
114
115                 if (order == EULER_ORDER_XYZ)   mat3_to_compatible_eul(eul, eul_compat->eul, mat);
116                 else                                                    mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat);
117         }
118         else {
119                 if (order == EULER_ORDER_XYZ)   quat_to_eul(eul, tquat);
120                 else                                                    quat_to_eulO(eul, order, tquat);
121         }
122
123         return Euler_CreatePyObject(eul, order, Py_NEW, NULL);
124 }
125 //----------------------------Quaternion.toMatrix()------------------
126 PyDoc_STRVAR(Quaternion_to_matrix_doc,
127 ".. method:: to_matrix()\n"
128 "\n"
129 "   Return a matrix representation of the quaternion.\n"
130 "\n"
131 "   :return: A 3x3 rotation matrix representation of the quaternion.\n"
132 "   :rtype: :class:`Matrix`\n"
133 );
134 static PyObject *Quaternion_to_matrix(QuaternionObject *self)
135 {
136         float mat[9]; /* all values are set */
137
138         if (BaseMath_ReadCallback(self) == -1)
139                 return NULL;
140
141         quat_to_mat3((float (*)[3])mat, self->quat);
142         return Matrix_CreatePyObject(mat, 3, 3, Py_NEW, NULL);
143 }
144
145 //----------------------------Quaternion.toMatrix()------------------
146 PyDoc_STRVAR(Quaternion_to_axis_angle_doc,
147 ".. method:: to_axis_angle()\n"
148 "\n"
149 "   Return the axis, angle representation of the quaternion.\n"
150 "\n"
151 "   :return: axis, angle.\n"
152 "   :rtype: (:class:`Vector`, float) pair\n"
153 );
154 static PyObject *Quaternion_to_axis_angle(QuaternionObject *self)
155 {
156         PyObject *ret;
157
158         float tquat[4];
159
160         float axis[3];
161         float angle;
162
163         if (BaseMath_ReadCallback(self) == -1)
164                 return NULL;
165
166         normalize_qt_qt(tquat, self->quat);
167         quat_to_axis_angle(axis, &angle, tquat);
168
169         quat__axis_angle_sanitize(axis, &angle);
170
171         ret= PyTuple_New(2);
172         PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(axis, 3, Py_NEW, NULL));
173         PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(angle));
174         return ret;
175 }
176
177
178 //----------------------------Quaternion.cross(other)------------------
179 PyDoc_STRVAR(Quaternion_cross_doc,
180 ".. method:: cross(other)\n"
181 "\n"
182 "   Return the cross product of this quaternion and another.\n"
183 "\n"
184 "   :arg other: The other quaternion to perform the cross product with.\n"
185 "   :type other: :class:`Quaternion`\n"
186 "   :return: The cross product.\n"
187 "   :rtype: :class:`Quaternion`\n"
188 );
189 static PyObject *Quaternion_cross(QuaternionObject *self, PyObject *value)
190 {
191         float quat[QUAT_SIZE], tquat[QUAT_SIZE];
192
193         if (BaseMath_ReadCallback(self) == -1)
194                 return NULL;
195
196         if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.cross(other), invalid 'other' arg") == -1)
197                 return NULL;
198
199         mul_qt_qtqt(quat, self->quat, tquat);
200         return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(self));
201 }
202
203 //----------------------------Quaternion.dot(other)------------------
204 PyDoc_STRVAR(Quaternion_dot_doc,
205 ".. method:: dot(other)\n"
206 "\n"
207 "   Return the dot product of this quaternion and another.\n"
208 "\n"
209 "   :arg other: The other quaternion to perform the dot product with.\n"
210 "   :type other: :class:`Quaternion`\n"
211 "   :return: The dot product.\n"
212 "   :rtype: :class:`Quaternion`\n"
213 );
214 static PyObject *Quaternion_dot(QuaternionObject *self, PyObject *value)
215 {
216         float tquat[QUAT_SIZE];
217
218         if (BaseMath_ReadCallback(self) == -1)
219                 return NULL;
220
221         if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.dot(other), invalid 'other' arg") == -1)
222                 return NULL;
223
224         return PyFloat_FromDouble(dot_qtqt(self->quat, tquat));
225 }
226
227 PyDoc_STRVAR(Quaternion_rotation_difference_doc,
228 ".. function:: rotation_difference(other)\n"
229 "\n"
230 "   Returns a quaternion representing the rotational difference.\n"
231 "\n"
232 "   :arg other: second quaternion.\n"
233 "   :type other: :class:`Quaternion`\n"
234 "   :return: the rotational difference between the two quat rotations.\n"
235 "   :rtype: :class:`Quaternion`\n"
236 );
237 static PyObject *Quaternion_rotation_difference(QuaternionObject *self, PyObject *value)
238 {
239         float tquat[QUAT_SIZE], quat[QUAT_SIZE];
240
241         if (BaseMath_ReadCallback(self) == -1)
242                 return NULL;
243
244         if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.difference(other), invalid 'other' arg") == -1)
245                 return NULL;
246
247         rotation_between_quats_to_quat(quat, self->quat, tquat);
248
249         return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(self));
250 }
251
252 PyDoc_STRVAR(Quaternion_slerp_doc,
253 ".. function:: slerp(other, factor)\n"
254 "\n"
255 "   Returns the interpolation of two quaternions.\n"
256 "\n"
257 "   :arg other: value to interpolate with.\n"
258 "   :type other: :class:`Quaternion`\n"
259 "   :arg factor: The interpolation value in [0.0, 1.0].\n"
260 "   :type factor: float\n"
261 "   :return: The interpolated rotation.\n"
262 "   :rtype: :class:`Quaternion`\n"
263 );
264 static PyObject *Quaternion_slerp(QuaternionObject *self, PyObject *args)
265 {
266         PyObject *value;
267         float tquat[QUAT_SIZE], quat[QUAT_SIZE], fac;
268
269         if (!PyArg_ParseTuple(args, "Of:slerp", &value, &fac)) {
270                 PyErr_SetString(PyExc_TypeError,
271                                 "quat.slerp(): "
272                                 "expected Quaternion types and float");
273                 return NULL;
274         }
275
276         if (BaseMath_ReadCallback(self) == -1)
277                 return NULL;
278
279         if (mathutils_array_parse(tquat, QUAT_SIZE, QUAT_SIZE, value, "Quaternion.slerp(other), invalid 'other' arg") == -1)
280                 return NULL;
281
282         if (fac > 1.0f || fac < 0.0f) {
283                 PyErr_SetString(PyExc_ValueError,
284                                 "quat.slerp(): "
285                                 "interpolation factor must be between 0.0 and 1.0");
286                 return NULL;
287         }
288
289         interp_qt_qtqt(quat, self->quat, tquat, fac);
290
291         return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(self));
292 }
293
294 PyDoc_STRVAR(Quaternion_rotate_doc,
295 ".. method:: rotate(other)\n"
296 "\n"
297 "   Rotates the quaternion a by another mathutils value.\n"
298 "\n"
299 "   :arg other: rotation component of mathutils value\n"
300 "   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
301 );
302 static PyObject *Quaternion_rotate(QuaternionObject *self, PyObject *value)
303 {
304         float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
305         float tquat[4], length;
306
307         if (BaseMath_ReadCallback(self) == -1)
308                 return NULL;
309
310         if (mathutils_any_to_rotmat(other_rmat, value, "Quaternion.rotate(value)") == -1)
311                 return NULL;
312
313         length= normalize_qt_qt(tquat, self->quat);
314         quat_to_mat3(self_rmat, tquat);
315         mul_m3_m3m3(rmat, other_rmat, self_rmat);
316
317         mat3_to_quat(self->quat, rmat);
318         mul_qt_fl(self->quat, length); /* maintain length after rotating */
319
320         (void)BaseMath_WriteCallback(self);
321         Py_RETURN_NONE;
322 }
323
324 //----------------------------Quaternion.normalize()----------------
325 //normalize the axis of rotation of [theta, vector]
326 PyDoc_STRVAR(Quaternion_normalize_doc,
327 ".. function:: normalize()\n"
328 "\n"
329 "   Normalize the quaternion.\n"
330 );
331 static PyObject *Quaternion_normalize(QuaternionObject *self)
332 {
333         if (BaseMath_ReadCallback(self) == -1)
334                 return NULL;
335
336         normalize_qt(self->quat);
337
338         (void)BaseMath_WriteCallback(self);
339         Py_RETURN_NONE;
340 }
341 PyDoc_STRVAR(Quaternion_normalized_doc,
342 ".. function:: normalized()\n"
343 "\n"
344 "   Return a new normalized quaternion.\n"
345 "\n"
346 "   :return: a normalized copy.\n"
347 "   :rtype: :class:`Quaternion`\n"
348 );
349 static PyObject *Quaternion_normalized(QuaternionObject *self)
350 {
351         return quat__apply_to_copy((PyNoArgsFunction)Quaternion_normalize, self);
352 }
353
354 //----------------------------Quaternion.invert()------------------
355 PyDoc_STRVAR(Quaternion_invert_doc,
356 ".. function:: invert()\n"
357 "\n"
358 "   Set the quaternion to its inverse.\n"
359 );
360 static PyObject *Quaternion_invert(QuaternionObject *self)
361 {
362         if (BaseMath_ReadCallback(self) == -1)
363                 return NULL;
364
365         invert_qt(self->quat);
366
367         (void)BaseMath_WriteCallback(self);
368         Py_RETURN_NONE;
369 }
370 PyDoc_STRVAR(Quaternion_inverted_doc,
371 ".. function:: inverted()\n"
372 "\n"
373 "   Return a new, inverted quaternion.\n"
374 "\n"
375 "   :return: the inverted value.\n"
376 "   :rtype: :class:`Quaternion`\n"
377 );
378 static PyObject *Quaternion_inverted(QuaternionObject *self)
379 {
380         return quat__apply_to_copy((PyNoArgsFunction)Quaternion_invert, self);
381 }
382
383 //----------------------------Quaternion.identity()-----------------
384 PyDoc_STRVAR(Quaternion_identity_doc,
385 ".. function:: identity()\n"
386 "\n"
387 "   Set the quaternion to an identity quaternion.\n"
388 "\n"
389 "   :return: an instance of itself.\n"
390 "   :rtype: :class:`Quaternion`\n"
391 );
392 static PyObject *Quaternion_identity(QuaternionObject *self)
393 {
394         if (BaseMath_ReadCallback(self) == -1)
395                 return NULL;
396
397         unit_qt(self->quat);
398
399         (void)BaseMath_WriteCallback(self);
400         Py_RETURN_NONE;
401 }
402 //----------------------------Quaternion.negate()-------------------
403 PyDoc_STRVAR(Quaternion_negate_doc,
404 ".. function:: negate()\n"
405 "\n"
406 "   Set the quaternion to its negative.\n"
407 "\n"
408 "   :return: an instance of itself.\n"
409 "   :rtype: :class:`Quaternion`\n"
410 );
411 static PyObject *Quaternion_negate(QuaternionObject *self)
412 {
413         if (BaseMath_ReadCallback(self) == -1)
414                 return NULL;
415
416         mul_qt_fl(self->quat, -1.0f);
417
418         (void)BaseMath_WriteCallback(self);
419         Py_RETURN_NONE;
420 }
421 //----------------------------Quaternion.conjugate()----------------
422 PyDoc_STRVAR(Quaternion_conjugate_doc,
423 ".. function:: conjugate()\n"
424 "\n"
425 "   Set the quaternion to its conjugate (negate x, y, z).\n"
426 );
427 static PyObject *Quaternion_conjugate(QuaternionObject *self)
428 {
429         if (BaseMath_ReadCallback(self) == -1)
430                 return NULL;
431
432         conjugate_qt(self->quat);
433
434         (void)BaseMath_WriteCallback(self);
435         Py_RETURN_NONE;
436 }
437 PyDoc_STRVAR(Quaternion_conjugated_doc,
438 ".. function:: conjugated()\n"
439 "\n"
440 "   Return a new conjugated quaternion.\n"
441 "\n"
442 "   :return: a new quaternion.\n"
443 "   :rtype: :class:`Quaternion`\n"
444 );
445 static PyObject *Quaternion_conjugated(QuaternionObject *self)
446 {
447         return quat__apply_to_copy((PyNoArgsFunction)Quaternion_conjugate, self);
448 }
449
450 //----------------------------Quaternion.copy()----------------
451 PyDoc_STRVAR(Quaternion_copy_doc,
452 ".. function:: copy()\n"
453 "\n"
454 "   Returns a copy of this quaternion.\n"
455 "\n"
456 "   :return: A copy of the quaternion.\n"
457 "   :rtype: :class:`Quaternion`\n"
458 "\n"
459 "   .. note:: use this to get a copy of a wrapped quaternion with\n"
460 "      no reference to the original data.\n"
461 );
462 static PyObject *Quaternion_copy(QuaternionObject *self)
463 {
464         if (BaseMath_ReadCallback(self) == -1)
465                 return NULL;
466
467         return Quaternion_CreatePyObject(self->quat, Py_NEW, Py_TYPE(self));
468 }
469
470 //----------------------------print object (internal)--------------
471 //print the object to screen
472 static PyObject *Quaternion_repr(QuaternionObject *self)
473 {
474         PyObject *ret, *tuple;
475
476         if (BaseMath_ReadCallback(self) == -1)
477                 return NULL;
478
479         tuple= Quaternion_to_tuple_ext(self, -1);
480
481         ret= PyUnicode_FromFormat("Quaternion(%R)", tuple);
482
483         Py_DECREF(tuple);
484         return ret;
485 }
486
487 static PyObject* Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
488 {
489         PyObject *res;
490         int ok= -1; /* zero is true */
491
492         if (QuaternionObject_Check(a) && QuaternionObject_Check(b)) {
493                 QuaternionObject *quatA= (QuaternionObject *)a;
494                 QuaternionObject *quatB= (QuaternionObject *)b;
495
496                 if (BaseMath_ReadCallback(quatA) == -1 || BaseMath_ReadCallback(quatB) == -1)
497                         return NULL;
498
499                 ok= (EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1)) ? 0 : -1;
500         }
501
502         switch (op) {
503         case Py_NE:
504                 ok = !ok; /* pass through */
505         case Py_EQ:
506                 res = ok ? Py_False : Py_True;
507                 break;
508
509         case Py_LT:
510         case Py_LE:
511         case Py_GT:
512         case Py_GE:
513                 res = Py_NotImplemented;
514                 break;
515         default:
516                 PyErr_BadArgument();
517                 return NULL;
518         }
519
520         return Py_INCREF(res), res;
521 }
522
523 //---------------------SEQUENCE PROTOCOLS------------------------
524 //----------------------------len(object)------------------------
525 //sequence length
526 static int Quaternion_len(QuaternionObject *UNUSED(self))
527 {
528         return QUAT_SIZE;
529 }
530 //----------------------------object[]---------------------------
531 //sequence accessor (get)
532 static PyObject *Quaternion_item(QuaternionObject *self, int i)
533 {
534         if (i<0)        i= QUAT_SIZE-i;
535
536         if (i < 0 || i >= QUAT_SIZE) {
537                 PyErr_SetString(PyExc_IndexError,
538                                 "quaternion[attribute]: "
539                                 "array index out of range");
540                 return NULL;
541         }
542
543         if (BaseMath_ReadIndexCallback(self, i) == -1)
544                 return NULL;
545
546         return PyFloat_FromDouble(self->quat[i]);
547
548 }
549 //----------------------------object[]-------------------------
550 //sequence accessor (set)
551 static int Quaternion_ass_item(QuaternionObject *self, int i, PyObject *ob)
552 {
553         float scalar= (float)PyFloat_AsDouble(ob);
554         if (scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
555                 PyErr_SetString(PyExc_TypeError,
556                                 "quaternion[index] = x: "
557                                 "index argument not a number");
558                 return -1;
559         }
560
561         if (i<0)        i= QUAT_SIZE-i;
562
563         if (i < 0 || i >= QUAT_SIZE) {
564                 PyErr_SetString(PyExc_IndexError,
565                                 "quaternion[attribute] = x: "
566                                 "array assignment index out of range");
567                 return -1;
568         }
569         self->quat[i] = scalar;
570
571         if (BaseMath_WriteIndexCallback(self, i) == -1)
572                 return -1;
573
574         return 0;
575 }
576 //----------------------------object[z:y]------------------------
577 //sequence slice (get)
578 static PyObject *Quaternion_slice(QuaternionObject *self, int begin, int end)
579 {
580         PyObject *tuple;
581         int count;
582
583         if (BaseMath_ReadCallback(self) == -1)
584                 return NULL;
585
586         CLAMP(begin, 0, QUAT_SIZE);
587         if (end<0) end= (QUAT_SIZE + 1) + end;
588         CLAMP(end, 0, QUAT_SIZE);
589         begin= MIN2(begin, end);
590
591         tuple= PyTuple_New(end - begin);
592         for (count= begin; count < end; count++) {
593                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->quat[count]));
594         }
595
596         return tuple;
597 }
598 //----------------------------object[z:y]------------------------
599 //sequence slice (set)
600 static int Quaternion_ass_slice(QuaternionObject *self, int begin, int end, PyObject *seq)
601 {
602         int i, size;
603         float quat[QUAT_SIZE];
604
605         if (BaseMath_ReadCallback(self) == -1)
606                 return -1;
607
608         CLAMP(begin, 0, QUAT_SIZE);
609         if (end<0) end= (QUAT_SIZE + 1) + end;
610         CLAMP(end, 0, QUAT_SIZE);
611         begin = MIN2(begin, end);
612
613         if ((size=mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
614                 return -1;
615
616         if (size != (end - begin)) {
617                 PyErr_SetString(PyExc_ValueError,
618                                 "quaternion[begin:end] = []: "
619                                 "size mismatch in slice assignment");
620                 return -1;
621         }
622
623         /* parsed well - now set in vector */
624         for (i= 0; i < size; i++)
625                 self->quat[begin + i] = quat[i];
626
627         (void)BaseMath_WriteCallback(self);
628         return 0;
629 }
630
631
632 static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
633 {
634         if (PyIndex_Check(item)) {
635                 Py_ssize_t i;
636                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
637                 if (i == -1 && PyErr_Occurred())
638                         return NULL;
639                 if (i < 0)
640                         i += QUAT_SIZE;
641                 return Quaternion_item(self, i);
642         }
643         else if (PySlice_Check(item)) {
644                 Py_ssize_t start, stop, step, slicelength;
645
646                 if (PySlice_GetIndicesEx((void *)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
647                         return NULL;
648
649                 if (slicelength <= 0) {
650                         return PyTuple_New(0);
651                 }
652                 else if (step == 1) {
653                         return Quaternion_slice(self, start, stop);
654                 }
655                 else {
656                         PyErr_SetString(PyExc_IndexError,
657                                         "slice steps not supported with quaternions");
658                         return NULL;
659                 }
660         }
661         else {
662                 PyErr_Format(PyExc_TypeError,
663                              "quaternion indices must be integers, not %.200s",
664                              Py_TYPE(item)->tp_name);
665                 return NULL;
666         }
667 }
668
669
670 static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
671 {
672         if (PyIndex_Check(item)) {
673                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
674                 if (i == -1 && PyErr_Occurred())
675                         return -1;
676                 if (i < 0)
677                         i += QUAT_SIZE;
678                 return Quaternion_ass_item(self, i, value);
679         }
680         else if (PySlice_Check(item)) {
681                 Py_ssize_t start, stop, step, slicelength;
682
683                 if (PySlice_GetIndicesEx((void *)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
684                         return -1;
685
686                 if (step == 1)
687                         return Quaternion_ass_slice(self, start, stop, value);
688                 else {
689                         PyErr_SetString(PyExc_IndexError,
690                                         "slice steps not supported with quaternion");
691                         return -1;
692                 }
693         }
694         else {
695                 PyErr_Format(PyExc_TypeError,
696                              "quaternion indices must be integers, not %.200s",
697                              Py_TYPE(item)->tp_name);
698                 return -1;
699         }
700 }
701
702 //------------------------NUMERIC PROTOCOLS----------------------
703 //------------------------obj + obj------------------------------
704 //addition
705 static PyObject *Quaternion_add(PyObject *q1, PyObject *q2)
706 {
707         float quat[QUAT_SIZE];
708         QuaternionObject *quat1 = NULL, *quat2 = NULL;
709
710         if (!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
711                 PyErr_Format(PyExc_TypeError,
712                              "Quaternion addition: (%s + %s) "
713                              "invalid type for this operation",
714                              Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name);
715                 return NULL;
716         }
717         quat1 = (QuaternionObject*)q1;
718         quat2 = (QuaternionObject*)q2;
719
720         if (BaseMath_ReadCallback(quat1) == -1 || BaseMath_ReadCallback(quat2) == -1)
721                 return NULL;
722
723         add_qt_qtqt(quat, quat1->quat, quat2->quat, 1.0f);
724         return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(q1));
725 }
726 //------------------------obj - obj------------------------------
727 //subtraction
728 static PyObject *Quaternion_sub(PyObject *q1, PyObject *q2)
729 {
730         int x;
731         float quat[QUAT_SIZE];
732         QuaternionObject *quat1 = NULL, *quat2 = NULL;
733
734         if (!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
735                 PyErr_Format(PyExc_TypeError,
736                              "Quaternion subtraction: (%s - %s) "
737                              "invalid type for this operation",
738                              Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name);
739                 return NULL;
740         }
741
742         quat1 = (QuaternionObject*)q1;
743         quat2 = (QuaternionObject*)q2;
744
745         if (BaseMath_ReadCallback(quat1) == -1 || BaseMath_ReadCallback(quat2) == -1)
746                 return NULL;
747
748         for (x = 0; x < QUAT_SIZE; x++) {
749                 quat[x] = quat1->quat[x] - quat2->quat[x];
750         }
751
752         return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(q1));
753 }
754
755 static PyObject *quat_mul_float(QuaternionObject *quat, const float scalar)
756 {
757         float tquat[4];
758         copy_qt_qt(tquat, quat->quat);
759         mul_qt_fl(tquat, scalar);
760         return Quaternion_CreatePyObject(tquat, Py_NEW, Py_TYPE(quat));
761 }
762
763 //------------------------obj * obj------------------------------
764 //mulplication
765 static PyObject *Quaternion_mul(PyObject *q1, PyObject *q2)
766 {
767         float quat[QUAT_SIZE], scalar;
768         QuaternionObject *quat1 = NULL, *quat2 = NULL;
769
770         if (QuaternionObject_Check(q1)) {
771                 quat1 = (QuaternionObject*)q1;
772                 if (BaseMath_ReadCallback(quat1) == -1)
773                         return NULL;
774         }
775         if (QuaternionObject_Check(q2)) {
776                 quat2 = (QuaternionObject*)q2;
777                 if (BaseMath_ReadCallback(quat2) == -1)
778                         return NULL;
779         }
780
781         if (quat1 && quat2) { /* QUAT*QUAT (cross product) */
782                 mul_qt_qtqt(quat, quat1->quat, quat2->quat);
783                 return Quaternion_CreatePyObject(quat, Py_NEW, Py_TYPE(q1));
784         }
785         /* the only case this can happen (for a supported type is "FLOAT*QUAT") */
786         else if (quat2) { /* FLOAT*QUAT */
787                 if (((scalar= PyFloat_AsDouble(q1)) == -1.0f && PyErr_Occurred())==0) {
788                         return quat_mul_float(quat2, scalar);
789                 }
790         }
791         else if (quat1) {
792                 /* QUAT * VEC */
793                 if (VectorObject_Check(q2)) {
794                         VectorObject *vec2 = (VectorObject *)q2;
795                         float tvec[3];
796
797                         if (vec2->size != 3) {
798                                 PyErr_SetString(PyExc_ValueError,
799                                                                 "Vector multiplication: "
800                                                                 "only 3D vector rotations (with quats) "
801                                                 "currently supported");
802                                 return NULL;
803                         }
804                         if (BaseMath_ReadCallback(vec2) == -1) {
805                                 return NULL;
806                         }
807
808                         copy_v3_v3(tvec, vec2->vec);
809                         mul_qt_v3(quat1->quat, tvec);
810
811                         return Vector_CreatePyObject(tvec, 3, Py_NEW, Py_TYPE(vec2));
812                 }
813                 /* QUAT * FLOAT */
814                 else if ((((scalar= PyFloat_AsDouble(q2)) == -1.0f && PyErr_Occurred())==0)) {
815                         return quat_mul_float(quat1, scalar);
816                 }
817         }
818         else {
819                 BLI_assert(!"internal error");
820         }
821
822         PyErr_Format(PyExc_TypeError,
823                      "Quaternion multiplication: "
824                      "not supported between '%.200s' and '%.200s' types",
825                      Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name);
826         return NULL;
827 }
828
829 /* -obj
830   returns the negative of this object*/
831 static PyObject *Quaternion_neg(QuaternionObject *self)
832 {
833         float tquat[QUAT_SIZE];
834
835         if (BaseMath_ReadCallback(self) == -1)
836                 return NULL;
837
838         negate_v4_v4(tquat, self->quat);
839         return Quaternion_CreatePyObject(tquat, Py_NEW, Py_TYPE(self));
840 }
841
842
843 //-----------------PROTOCOL DECLARATIONS--------------------------
844 static PySequenceMethods Quaternion_SeqMethods = {
845         (lenfunc) Quaternion_len,                               /* sq_length */
846         (binaryfunc) NULL,                                              /* sq_concat */
847         (ssizeargfunc) NULL,                                    /* sq_repeat */
848         (ssizeargfunc) Quaternion_item,                 /* sq_item */
849         (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated */
850         (ssizeobjargproc) Quaternion_ass_item,  /* sq_ass_item */
851         (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
852         (objobjproc) NULL,                                              /* sq_contains */
853         (binaryfunc) NULL,                                              /* sq_inplace_concat */
854         (ssizeargfunc) NULL,                                    /* sq_inplace_repeat */
855 };
856
857 static PyMappingMethods Quaternion_AsMapping = {
858         (lenfunc)Quaternion_len,
859         (binaryfunc)Quaternion_subscript,
860         (objobjargproc)Quaternion_ass_subscript
861 };
862
863 static PyNumberMethods Quaternion_NumMethods = {
864         (binaryfunc)    Quaternion_add, /*nb_add*/
865         (binaryfunc)    Quaternion_sub, /*nb_subtract*/
866         (binaryfunc)    Quaternion_mul, /*nb_multiply*/
867         NULL,                                                   /*nb_remainder*/
868         NULL,                                                   /*nb_divmod*/
869         NULL,                                                   /*nb_power*/
870         (unaryfunc)     Quaternion_neg, /*nb_negative*/
871         (unaryfunc)     0,      /*tp_positive*/
872         (unaryfunc)     0,      /*tp_absolute*/
873         (inquiry)       0,      /*tp_bool*/
874         (unaryfunc)     0,      /*nb_invert*/
875         NULL,                           /*nb_lshift*/
876         (binaryfunc)0,  /*nb_rshift*/
877         NULL,                           /*nb_and*/
878         NULL,                           /*nb_xor*/
879         NULL,                           /*nb_or*/
880         NULL,                           /*nb_int*/
881         NULL,                           /*nb_reserved*/
882         NULL,                           /*nb_float*/
883         NULL,                           /* nb_inplace_add */
884         NULL,                           /* nb_inplace_subtract */
885         NULL,                           /* nb_inplace_multiply */
886         NULL,                           /* nb_inplace_remainder */
887         NULL,                           /* nb_inplace_power */
888         NULL,                           /* nb_inplace_lshift */
889         NULL,                           /* nb_inplace_rshift */
890         NULL,                           /* nb_inplace_and */
891         NULL,                           /* nb_inplace_xor */
892         NULL,                           /* nb_inplace_or */
893         NULL,                           /* nb_floor_divide */
894         NULL,                           /* nb_true_divide */
895         NULL,                           /* nb_inplace_floor_divide */
896         NULL,                           /* nb_inplace_true_divide */
897         NULL,                           /* nb_index */
898 };
899
900 static PyObject *Quaternion_getAxis(QuaternionObject *self, void *type)
901 {
902         return Quaternion_item(self, GET_INT_FROM_POINTER(type));
903 }
904
905 static int Quaternion_setAxis(QuaternionObject *self, PyObject *value, void *type)
906 {
907         return Quaternion_ass_item(self, GET_INT_FROM_POINTER(type), value);
908 }
909
910 static PyObject *Quaternion_getMagnitude(QuaternionObject *self, void *UNUSED(closure))
911 {
912         if (BaseMath_ReadCallback(self) == -1)
913                 return NULL;
914
915         return PyFloat_FromDouble(sqrt(dot_qtqt(self->quat, self->quat)));
916 }
917
918 static PyObject *Quaternion_getAngle(QuaternionObject *self, void *UNUSED(closure))
919 {
920         float tquat[4];
921         float angle;
922
923         if (BaseMath_ReadCallback(self) == -1)
924                 return NULL;
925
926         normalize_qt_qt(tquat, self->quat);
927
928         angle= 2.0f * saacos(tquat[0]);
929
930         quat__axis_angle_sanitize(NULL, &angle);
931
932         return PyFloat_FromDouble(angle);
933 }
934
935 static int Quaternion_setAngle(QuaternionObject *self, PyObject *value, void *UNUSED(closure))
936 {
937         float tquat[4];
938         float len;
939
940         float axis[3], angle_dummy;
941         float angle;
942
943         if (BaseMath_ReadCallback(self) == -1)
944                 return -1;
945
946         len= normalize_qt_qt(tquat, self->quat);
947         quat_to_axis_angle(axis, &angle_dummy, tquat);
948
949         angle= PyFloat_AsDouble(value);
950
951         if (angle==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
952                 PyErr_SetString(PyExc_TypeError,
953                                 "Quaternion.angle = value: float expected");
954                 return -1;
955         }
956
957         angle= angle_wrap_rad(angle);
958
959         quat__axis_angle_sanitize(axis, &angle);
960
961         axis_angle_to_quat(self->quat, axis, angle);
962         mul_qt_fl(self->quat, len);
963
964         if (BaseMath_WriteCallback(self) == -1)
965                 return -1;
966
967         return 0;
968 }
969
970 static PyObject *Quaternion_getAxisVec(QuaternionObject *self, void *UNUSED(closure))
971 {
972         float tquat[4];
973
974         float axis[3];
975         float angle_dummy;
976
977         if (BaseMath_ReadCallback(self) == -1)
978                 return NULL;
979
980         normalize_qt_qt(tquat, self->quat);
981         quat_to_axis_angle(axis, &angle_dummy, tquat);
982
983         quat__axis_angle_sanitize(axis, NULL);
984
985         return (PyObject *) Vector_CreatePyObject(axis, 3, Py_NEW, NULL);
986 }
987
988 static int Quaternion_setAxisVec(QuaternionObject *self, PyObject *value, void *UNUSED(closure))
989 {
990         float tquat[4];
991         float len;
992
993         float axis[3];
994         float angle;
995
996         if (BaseMath_ReadCallback(self) == -1)
997                 return -1;
998
999         len= normalize_qt_qt(tquat, self->quat);
1000         quat_to_axis_angle(axis, &angle, tquat); /* axis value is unused */
1001
1002         if (mathutils_array_parse(axis, 3, 3, value, "quat.axis = other") == -1)
1003                 return -1;
1004
1005         quat__axis_angle_sanitize(axis, &angle);
1006
1007         axis_angle_to_quat(self->quat, axis, angle);
1008         mul_qt_fl(self->quat, len);
1009
1010         if (BaseMath_WriteCallback(self) == -1)
1011                 return -1;
1012
1013         return 0;
1014 }
1015
1016 //----------------------------------mathutils.Quaternion() --------------
1017 static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1018 {
1019         PyObject *seq= NULL;
1020         double angle = 0.0f;
1021         float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
1022
1023         if (kwds && PyDict_Size(kwds)) {
1024                 PyErr_SetString(PyExc_TypeError,
1025                                 "mathutils.Quaternion(): "
1026                                 "takes no keyword args");
1027                 return NULL;
1028         }
1029
1030         if (!PyArg_ParseTuple(args, "|Od:mathutils.Quaternion", &seq, &angle))
1031                 return NULL;
1032
1033         switch(PyTuple_GET_SIZE(args)) {
1034         case 0:
1035                 break;
1036         case 1:
1037                 if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
1038                         return NULL;
1039                 break;
1040         case 2:
1041                 if (mathutils_array_parse(quat, 3, 3, seq, "mathutils.Quaternion()") == -1)
1042                         return NULL;
1043                 angle= angle_wrap_rad(angle); /* clamp because of precision issues */
1044                 axis_angle_to_quat(quat, quat, angle);
1045                 break;
1046         /* PyArg_ParseTuple assures no more then 2 */
1047         }
1048         return Quaternion_CreatePyObject(quat, Py_NEW, type);
1049 }
1050
1051 static PyObject *quat__apply_to_copy(PyNoArgsFunction quat_func, QuaternionObject *self)
1052 {
1053         PyObject *ret= Quaternion_copy(self);
1054         PyObject *ret_dummy= quat_func(ret);
1055         if (ret_dummy) {
1056                 Py_DECREF(ret_dummy);
1057                 return (PyObject *)ret;
1058         }
1059         else { /* error */
1060                 Py_DECREF(ret);
1061                 return NULL;
1062         }
1063 }
1064
1065 /* axis vector suffers from precission errors, use this function to ensure */
1066 static void quat__axis_angle_sanitize(float axis[3], float *angle)
1067 {
1068         if (axis) {
1069                 if (   !finite(axis[0]) ||
1070                            !finite(axis[1]) ||
1071                            !finite(axis[2]))
1072                 {
1073                         axis[0]= 1.0f;
1074                         axis[1]= 0.0f;
1075                         axis[2]= 0.0f;
1076                 }
1077                 else if (    EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
1078                                          EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
1079                                          EXPP_FloatsAreEqual(axis[2], 0.0f, 10)
1080                 ) {
1081                         axis[0] = 1.0f;
1082                 }
1083         }
1084
1085         if (angle) {
1086                 if (!finite(*angle)) {
1087                         *angle= 0.0f;
1088                 }
1089         }
1090 }
1091
1092 //-----------------------METHOD DEFINITIONS ----------------------
1093 static struct PyMethodDef Quaternion_methods[] = {
1094         /* in place only */
1095         {"identity", (PyCFunction) Quaternion_identity, METH_NOARGS, Quaternion_identity_doc},
1096         {"negate", (PyCFunction) Quaternion_negate, METH_NOARGS, Quaternion_negate_doc},
1097
1098         /* operate on original or copy */
1099         {"conjugate", (PyCFunction) Quaternion_conjugate, METH_NOARGS, Quaternion_conjugate_doc},
1100         {"conjugated", (PyCFunction) Quaternion_conjugated, METH_NOARGS, Quaternion_conjugated_doc},
1101
1102         {"invert", (PyCFunction) Quaternion_invert, METH_NOARGS, Quaternion_invert_doc},
1103         {"inverted", (PyCFunction) Quaternion_inverted, METH_NOARGS, Quaternion_inverted_doc},
1104
1105         {"normalize", (PyCFunction) Quaternion_normalize, METH_NOARGS, Quaternion_normalize_doc},
1106         {"normalized", (PyCFunction) Quaternion_normalized, METH_NOARGS, Quaternion_normalized_doc},
1107
1108         /* return converted representation */
1109         {"to_euler", (PyCFunction) Quaternion_to_euler, METH_VARARGS, Quaternion_to_euler_doc},
1110         {"to_matrix", (PyCFunction) Quaternion_to_matrix, METH_NOARGS, Quaternion_to_matrix_doc},
1111         {"to_axis_angle", (PyCFunction) Quaternion_to_axis_angle, METH_NOARGS, Quaternion_to_axis_angle_doc},
1112
1113         /* operation between 2 or more types  */
1114         {"cross", (PyCFunction) Quaternion_cross, METH_O, Quaternion_cross_doc},
1115         {"dot", (PyCFunction) Quaternion_dot, METH_O, Quaternion_dot_doc},
1116         {"rotation_difference", (PyCFunction) Quaternion_rotation_difference, METH_O, Quaternion_rotation_difference_doc},
1117         {"slerp", (PyCFunction) Quaternion_slerp, METH_VARARGS, Quaternion_slerp_doc},
1118         {"rotate", (PyCFunction) Quaternion_rotate, METH_O, Quaternion_rotate_doc},
1119
1120         {"__copy__", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
1121         {"copy", (PyCFunction) Quaternion_copy, METH_NOARGS, Quaternion_copy_doc},
1122         {NULL, NULL, 0, NULL}
1123 };
1124
1125 /*****************************************************************************/
1126 /* Python attributes get/set structure:                                      */
1127 /*****************************************************************************/
1128 static PyGetSetDef Quaternion_getseters[] = {
1129         {(char *)"w", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, (char *)"Quaternion W value.\n\n:type: float", (void *)0},
1130         {(char *)"x", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, (char *)"Quaternion X axis.\n\n:type: float", (void *)1},
1131         {(char *)"y", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, (char *)"Quaternion Y axis.\n\n:type: float", (void *)2},
1132         {(char *)"z", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, (char *)"Quaternion Z axis.\n\n:type: float", (void *)3},
1133         {(char *)"magnitude", (getter)Quaternion_getMagnitude, (setter)NULL, (char *)"Size of the quaternion (readonly).\n\n:type: float", NULL},
1134         {(char *)"angle", (getter)Quaternion_getAngle, (setter)Quaternion_setAngle, (char *)"angle of the quaternion.\n\n:type: float", NULL},
1135         {(char *)"axis",(getter)Quaternion_getAxisVec, (setter)Quaternion_setAxisVec, (char *)"quaternion axis as a vector.\n\n:type: :class:`Vector`", NULL},
1136         {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
1137         {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
1138         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
1139 };
1140
1141 //------------------PY_OBECT DEFINITION--------------------------
1142 PyDoc_STRVAR(quaternion_doc,
1143 "This object gives access to Quaternions in Blender."
1144 );
1145 PyTypeObject quaternion_Type = {
1146         PyVarObject_HEAD_INIT(NULL, 0)
1147         "mathutils.Quaternion",                                         //tp_name
1148         sizeof(QuaternionObject),                       //tp_basicsize
1149         0,                                                              //tp_itemsize
1150         (destructor)BaseMathObject_dealloc,             //tp_dealloc
1151         NULL,                                                           //tp_print
1152         NULL,                                                           //tp_getattr
1153         NULL,                                                           //tp_setattr
1154         NULL,                                                           //tp_compare
1155         (reprfunc) Quaternion_repr,             //tp_repr
1156         &Quaternion_NumMethods,                 //tp_as_number
1157         &Quaternion_SeqMethods,                 //tp_as_sequence
1158         &Quaternion_AsMapping,                  //tp_as_mapping
1159         NULL,                                                           //tp_hash
1160         NULL,                                                           //tp_call
1161         NULL,                                                           //tp_str
1162         NULL,                                                           //tp_getattro
1163         NULL,                                                           //tp_setattro
1164         NULL,                                                           //tp_as_buffer
1165         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
1166         quaternion_doc, //tp_doc
1167         (traverseproc)BaseMathObject_traverse,  //tp_traverse
1168         (inquiry)BaseMathObject_clear,  //tp_clear
1169         (richcmpfunc)Quaternion_richcmpr,       //tp_richcompare
1170         0,                                                              //tp_weaklistoffset
1171         NULL,                                                           //tp_iter
1172         NULL,                                                           //tp_iternext
1173         Quaternion_methods,                             //tp_methods
1174         NULL,                                                           //tp_members
1175         Quaternion_getseters,                   //tp_getset
1176         NULL,                                                           //tp_base
1177         NULL,                                                           //tp_dict
1178         NULL,                                                           //tp_descr_get
1179         NULL,                                                           //tp_descr_set
1180         0,                                                              //tp_dictoffset
1181         NULL,                                                           //tp_init
1182         NULL,                                                           //tp_alloc
1183         Quaternion_new,                                 //tp_new
1184         NULL,                                                           //tp_free
1185         NULL,                                                           //tp_is_gc
1186         NULL,                                                           //tp_bases
1187         NULL,                                                           //tp_mro
1188         NULL,                                                           //tp_cache
1189         NULL,                                                           //tp_subclasses
1190         NULL,                                                           //tp_weaklist
1191         NULL,                                                           //tp_del
1192 };
1193 //------------------------Quaternion_CreatePyObject (internal)-------------
1194 //creates a new quaternion object
1195 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
1196  (i.e. it was allocated elsewhere by MEM_mallocN())
1197   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
1198  (i.e. it must be created here with PyMEM_malloc())*/
1199 PyObject *Quaternion_CreatePyObject(float *quat, int type, PyTypeObject *base_type)
1200 {
1201         QuaternionObject *self;
1202
1203         self= base_type ?       (QuaternionObject *)base_type->tp_alloc(base_type, 0) :
1204                                                 (QuaternionObject *)PyObject_GC_New(QuaternionObject, &quaternion_Type);
1205
1206         if (self) {
1207                 /* init callbacks as NULL */
1208                 self->cb_user= NULL;
1209                 self->cb_type= self->cb_subtype= 0;
1210
1211                 if (type == Py_WRAP) {
1212                         self->quat = quat;
1213                         self->wrapped = Py_WRAP;
1214                 }
1215                 else if (type == Py_NEW) {
1216                         self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
1217                         if (!quat) { //new empty
1218                                 unit_qt(self->quat);
1219                         }
1220                         else {
1221                                 copy_qt_qt(self->quat, quat);
1222                         }
1223                         self->wrapped = Py_NEW;
1224                 }
1225                 else {
1226                         Py_FatalError("Quaternion(): invalid type!");
1227                 }
1228         }
1229         return (PyObject *) self;
1230 }
1231
1232 PyObject *Quaternion_CreatePyObject_cb(PyObject *cb_user, int cb_type, int cb_subtype)
1233 {
1234         QuaternionObject *self= (QuaternionObject *)Quaternion_CreatePyObject(NULL, Py_NEW, NULL);
1235         if (self) {
1236                 Py_INCREF(cb_user);
1237                 self->cb_user=                  cb_user;
1238                 self->cb_type=                  (unsigned char)cb_type;
1239                 self->cb_subtype=               (unsigned char)cb_subtype;
1240                 PyObject_GC_Track(self);
1241         }
1242
1243         return (PyObject *)self;
1244 }
1245