Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/mathutils/mathutils_Quaternion.c
| Show First 20 Lines • Show All 828 Lines • ▼ Show 20 Lines | static PyObject *quat_mul_float(QuaternionObject *quat, const float scalar) | ||||
| mul_qt_fl(tquat, scalar); | mul_qt_fl(tquat, scalar); | ||||
| return Quaternion_CreatePyObject(tquat, Py_TYPE(quat)); | return Quaternion_CreatePyObject(tquat, Py_TYPE(quat)); | ||||
| } | } | ||||
| /*------------------------obj * obj------------------------------ | /*------------------------obj * obj------------------------------ | ||||
| * multiplication */ | * multiplication */ | ||||
| static PyObject *Quaternion_mul(PyObject *q1, PyObject *q2) | static PyObject *Quaternion_mul(PyObject *q1, PyObject *q2) | ||||
| { | { | ||||
| float quat[QUAT_SIZE], scalar; | float scalar; | ||||
| QuaternionObject *quat1 = NULL, *quat2 = NULL; | QuaternionObject *quat1 = NULL, *quat2 = NULL; | ||||
| if (QuaternionObject_Check(q1)) { | if (QuaternionObject_Check(q1)) { | ||||
| quat1 = (QuaternionObject *)q1; | quat1 = (QuaternionObject *)q1; | ||||
| if (BaseMath_ReadCallback(quat1) == -1) | if (BaseMath_ReadCallback(quat1) == -1) | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| if (QuaternionObject_Check(q2)) { | if (QuaternionObject_Check(q2)) { | ||||
| quat2 = (QuaternionObject *)q2; | quat2 = (QuaternionObject *)q2; | ||||
| if (BaseMath_ReadCallback(quat2) == -1) | if (BaseMath_ReadCallback(quat2) == -1) | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| if (quat1 && quat2) { /* QUAT * QUAT (cross product) */ | if (quat1 && quat2) { /* QUAT * QUAT (element-wise product) */ | ||||
| mul_qt_qtqt(quat, quat1->quat, quat2->quat); | #ifdef USE_MATHUTILS_ELEM_MUL | ||||
| float quat[QUAT_SIZE]; | |||||
| mul_vn_vnvn(quat, quat1->quat, quat2->quat, QUAT_SIZE); | |||||
| return Quaternion_CreatePyObject(quat, Py_TYPE(q1)); | return Quaternion_CreatePyObject(quat, Py_TYPE(q1)); | ||||
| #endif | |||||
| } | } | ||||
| /* the only case this can happen (for a supported type is "FLOAT * QUAT") */ | /* the only case this can happen (for a supported type is "FLOAT * QUAT") */ | ||||
| else if (quat2) { /* FLOAT * QUAT */ | else if (quat2) { /* FLOAT * QUAT */ | ||||
| if (((scalar = PyFloat_AsDouble(q1)) == -1.0f && PyErr_Occurred()) == 0) { | if (((scalar = PyFloat_AsDouble(q1)) == -1.0f && PyErr_Occurred()) == 0) { | ||||
| return quat_mul_float(quat2, scalar); | return quat_mul_float(quat2, scalar); | ||||
| } | } | ||||
| } | } | ||||
| else if (quat1) { /* QUAT * FLOAT */ | |||||
| if ((((scalar = PyFloat_AsDouble(q2)) == -1.0f && PyErr_Occurred()) == 0)) { | |||||
| return quat_mul_float(quat1, scalar); | |||||
| } | |||||
| } | |||||
| PyErr_Format(PyExc_TypeError, | |||||
| "Element-wise multiplication: " | |||||
| "not supported between '%.200s' and '%.200s' types", | |||||
| Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | |||||
| return NULL; | |||||
| } | |||||
| /*------------------------obj *= obj------------------------------ | |||||
| * inplace multiplication */ | |||||
| static PyObject *Quaternion_imul(PyObject *q1, PyObject *q2) | |||||
| { | |||||
| float scalar; | |||||
| QuaternionObject *quat1 = NULL, *quat2 = NULL; | |||||
| if (QuaternionObject_Check(q1)) { | |||||
| quat1 = (QuaternionObject *)q1; | |||||
| if (BaseMath_ReadCallback(quat1) == -1) | |||||
| return NULL; | |||||
| } | |||||
| if (QuaternionObject_Check(q2)) { | |||||
| quat2 = (QuaternionObject *)q2; | |||||
| if (BaseMath_ReadCallback(quat2) == -1) | |||||
| return NULL; | |||||
| } | |||||
| if (quat1 && quat2) { /* QUAT *= QUAT (inplace element-wise product) */ | |||||
| #ifdef USE_MATHUTILS_ELEM_MUL | |||||
| mul_vn_vn(quat1->quat, quat2->quat, QUAT_SIZE); | |||||
| #else | |||||
| PyErr_Format(PyExc_TypeError, | |||||
| "Inplace element-wise multiplication: " | |||||
| "not supported between '%.200s' and '%.200s' types", | |||||
| Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | |||||
| return NULL; | |||||
| #endif | |||||
| } | |||||
| else if (quat1 && (((scalar = PyFloat_AsDouble(q2)) == -1.0f && PyErr_Occurred()) == 0)) { | |||||
| /* QUAT *= FLOAT */ | |||||
| mul_qt_fl(quat1->quat, scalar); | |||||
| } | |||||
| else { | |||||
| PyErr_Format(PyExc_TypeError, | |||||
| "Element-wise multiplication: " | |||||
| "not supported between '%.200s' and '%.200s' types", | |||||
| Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | |||||
| return NULL; | |||||
| } | |||||
| (void)BaseMath_WriteCallback(quat1); | |||||
| Py_INCREF(q1); | |||||
| return q1; | |||||
| } | |||||
| /*------------------------obj @ obj------------------------------ | |||||
| * quaternion multiplication */ | |||||
| static PyObject *Quaternion_matmul(PyObject *q1, PyObject *q2) | |||||
| { | |||||
| float quat[QUAT_SIZE]; | |||||
| QuaternionObject *quat1 = NULL, *quat2 = NULL; | |||||
| if (QuaternionObject_Check(q1)) { | |||||
| quat1 = (QuaternionObject *)q1; | |||||
| if (BaseMath_ReadCallback(quat1) == -1) | |||||
| return NULL; | |||||
| } | |||||
| if (QuaternionObject_Check(q2)) { | |||||
| quat2 = (QuaternionObject *)q2; | |||||
| if (BaseMath_ReadCallback(quat2) == -1) | |||||
| return NULL; | |||||
| } | |||||
| if (quat1 && quat2) { /* QUAT @ QUAT (cross product) */ | |||||
| mul_qt_qtqt(quat, quat1->quat, quat2->quat); | |||||
| return Quaternion_CreatePyObject(quat, Py_TYPE(q1)); | |||||
| } | |||||
| else if (quat1) { | else if (quat1) { | ||||
| /* QUAT * VEC */ | /* QUAT @ VEC */ | ||||
| if (VectorObject_Check(q2)) { | if (VectorObject_Check(q2)) { | ||||
| VectorObject *vec2 = (VectorObject *)q2; | VectorObject *vec2 = (VectorObject *)q2; | ||||
| float tvec[3]; | float tvec[3]; | ||||
| if (vec2->size != 3) { | if (vec2->size != 3) { | ||||
| PyErr_SetString(PyExc_ValueError, | PyErr_SetString(PyExc_ValueError, | ||||
| "Vector multiplication: " | "Vector multiplication: " | ||||
| "only 3D vector rotations (with quats) " | "only 3D vector rotations (with quats) " | ||||
| "currently supported"); | "currently supported"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| if (BaseMath_ReadCallback(vec2) == -1) { | if (BaseMath_ReadCallback(vec2) == -1) { | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| copy_v3_v3(tvec, vec2->vec); | copy_v3_v3(tvec, vec2->vec); | ||||
| mul_qt_v3(quat1->quat, tvec); | mul_qt_v3(quat1->quat, tvec); | ||||
| return Vector_CreatePyObject(tvec, 3, Py_TYPE(vec2)); | return Vector_CreatePyObject(tvec, 3, Py_TYPE(vec2)); | ||||
| } | } | ||||
| /* QUAT * FLOAT */ | |||||
| else if ((((scalar = PyFloat_AsDouble(q2)) == -1.0f && PyErr_Occurred()) == 0)) { | |||||
| return quat_mul_float(quat1, scalar); | |||||
| } | } | ||||
| PyErr_Format(PyExc_TypeError, | |||||
| "Quaternion multiplication: " | |||||
| "not supported between '%.200s' and '%.200s' types", | |||||
| Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | |||||
| return NULL; | |||||
| } | } | ||||
| else { | /*------------------------obj @= obj------------------------------ | ||||
| BLI_assert(!"internal error"); | * inplace quaternion multiplication */ | ||||
| static PyObject *Quaternion_imatmul(PyObject *q1, PyObject *q2) | |||||
| { | |||||
| float quat[QUAT_SIZE]; | |||||
| QuaternionObject *quat1 = NULL, *quat2 = NULL; | |||||
| if (QuaternionObject_Check(q1)) { | |||||
| quat1 = (QuaternionObject *)q1; | |||||
| if (BaseMath_ReadCallback(quat1) == -1) | |||||
| return NULL; | |||||
| } | |||||
| if (QuaternionObject_Check(q2)) { | |||||
| quat2 = (QuaternionObject *)q2; | |||||
| if (BaseMath_ReadCallback(quat2) == -1) | |||||
| return NULL; | |||||
| } | } | ||||
| if (quat1 && quat2) { /* QUAT @ QUAT (cross product) */ | |||||
| mul_qt_qtqt(quat, quat1->quat, quat2->quat); | |||||
| copy_qt_qt(quat1->quat, quat); | |||||
| } | |||||
| else { | |||||
| PyErr_Format(PyExc_TypeError, | PyErr_Format(PyExc_TypeError, | ||||
| "Quaternion multiplication: " | "Inplace quaternion multiplication: " | ||||
| "not supported between '%.200s' and '%.200s' types", | "not supported between '%.200s' and '%.200s' types", | ||||
| Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | Py_TYPE(q1)->tp_name, Py_TYPE(q2)->tp_name); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| (void)BaseMath_WriteCallback(quat1); | |||||
| Py_INCREF(q1); | |||||
| return q1; | |||||
| } | |||||
| /* -obj | /* -obj | ||||
| * returns the negative of this object*/ | * returns the negative of this object*/ | ||||
| static PyObject *Quaternion_neg(QuaternionObject *self) | static PyObject *Quaternion_neg(QuaternionObject *self) | ||||
| { | { | ||||
| float tquat[QUAT_SIZE]; | float tquat[QUAT_SIZE]; | ||||
| if (BaseMath_ReadCallback(self) == -1) | if (BaseMath_ReadCallback(self) == -1) | ||||
| return NULL; | return NULL; | ||||
| Show All 40 Lines | static PyNumberMethods Quaternion_NumMethods = { | ||||
| NULL, /*nb_and*/ | NULL, /*nb_and*/ | ||||
| NULL, /*nb_xor*/ | NULL, /*nb_xor*/ | ||||
| NULL, /*nb_or*/ | NULL, /*nb_or*/ | ||||
| NULL, /*nb_int*/ | NULL, /*nb_int*/ | ||||
| NULL, /*nb_reserved*/ | NULL, /*nb_reserved*/ | ||||
| NULL, /*nb_float*/ | NULL, /*nb_float*/ | ||||
| NULL, /* nb_inplace_add */ | NULL, /* nb_inplace_add */ | ||||
| NULL, /* nb_inplace_subtract */ | NULL, /* nb_inplace_subtract */ | ||||
| NULL, /* nb_inplace_multiply */ | (binaryfunc) Quaternion_imul, /* nb_inplace_multiply */ | ||||
| NULL, /* nb_inplace_remainder */ | NULL, /* nb_inplace_remainder */ | ||||
| NULL, /* nb_inplace_power */ | NULL, /* nb_inplace_power */ | ||||
| NULL, /* nb_inplace_lshift */ | NULL, /* nb_inplace_lshift */ | ||||
| NULL, /* nb_inplace_rshift */ | NULL, /* nb_inplace_rshift */ | ||||
| NULL, /* nb_inplace_and */ | NULL, /* nb_inplace_and */ | ||||
| NULL, /* nb_inplace_xor */ | NULL, /* nb_inplace_xor */ | ||||
| NULL, /* nb_inplace_or */ | NULL, /* nb_inplace_or */ | ||||
| NULL, /* nb_floor_divide */ | NULL, /* nb_floor_divide */ | ||||
| NULL, /* nb_true_divide */ | NULL, /* nb_true_divide */ | ||||
| NULL, /* nb_inplace_floor_divide */ | NULL, /* nb_inplace_floor_divide */ | ||||
| NULL, /* nb_inplace_true_divide */ | NULL, /* nb_inplace_true_divide */ | ||||
| NULL, /* nb_index */ | NULL, /* nb_index */ | ||||
| (binaryfunc) Quaternion_matmul, /* nb_matrix_multiply */ | |||||
| (binaryfunc) Quaternion_imatmul, /* nb_inplace_matrix_multiply */ | |||||
| }; | }; | ||||
| PyDoc_STRVAR(Quaternion_axis_doc, | PyDoc_STRVAR(Quaternion_axis_doc, | ||||
| "Quaternion axis value.\n\n:type: float" | "Quaternion axis value.\n\n:type: float" | ||||
| ); | ); | ||||
| static PyObject *Quaternion_axis_get(QuaternionObject *self, void *type) | static PyObject *Quaternion_axis_get(QuaternionObject *self, void *type) | ||||
| { | { | ||||
| return Quaternion_item(self, GET_INT_FROM_POINTER(type)); | return Quaternion_item(self, GET_INT_FROM_POINTER(type)); | ||||
| ▲ Show 20 Lines • Show All 423 Lines • Show Last 20 Lines | |||||