diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-01-09 12:16:04 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-01-09 12:16:04 +0300 |
commit | d31ebbe66681cda8e94c6593e4b40c4b4df5809c (patch) | |
tree | dbfe940f8cbde15efa5545a21d33c0dbeae91516 /source/blender/python/generic/mathutils_vector.c | |
parent | ca89269c227607923bcfd8c137c552bdb0c37c16 (diff) |
cleanup for mathutils multiplication functions, a little faster in some cases, raise more informative exceptions.
Diffstat (limited to 'source/blender/python/generic/mathutils_vector.c')
-rw-r--r-- | source/blender/python/generic/mathutils_vector.c | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/source/blender/python/generic/mathutils_vector.c b/source/blender/python/generic/mathutils_vector.c index 3d80f6bade4..2208046e6c0 100644 --- a/source/blender/python/generic/mathutils_vector.c +++ b/source/blender/python/generic/mathutils_vector.c @@ -1047,6 +1047,17 @@ static int column_vector_multiplication(float *rvec, VectorObject* vec, MatrixOb return 0; } +static PyObject *vector_mul_float(VectorObject *vec, const float scalar) +{ + float tvec[MAX_DIMENSIONS]; + int i; + + for(i = 0; i < vec->size; i++) { + tvec[i] = vec->vec[i] * scalar; + } + return newVectorObject(tvec, vec->size, Py_NEW, Py_TYPE(vec)); +} + static PyObject *Vector_mul(PyObject * v1, PyObject * v2) { VectorObject *vec1 = NULL, *vec2 = NULL; @@ -1080,55 +1091,48 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2) } return PyFloat_FromDouble(dot); } - - /* swap so vec1 is always the vector */ - /* note: it would seem from this code that the matrix multiplication below - * is communicative. however the matrix class will always handle the - * (matrix * vector) case so we can ignore it here. - * This is NOT so for Quaternions: TODO, check if communicative (vec * quat) is correct */ - if (vec2) { - vec1= vec2; - v2= v1; - } + else if (vec1) { + if (MatrixObject_Check(v2)) { + /* VEC * MATRIX */ + float tvec[MAX_DIMENSIONS]; + if(!BaseMath_ReadCallback((MatrixObject *)v2)) + return NULL; + if(column_vector_multiplication(tvec, vec1, (MatrixObject*)v2) == -1) { + return NULL; + } - if (MatrixObject_Check(v2)) { - /* VEC * MATRIX */ - float tvec[MAX_DIMENSIONS]; - if(!BaseMath_ReadCallback((MatrixObject *)v2)) - return NULL; - if(column_vector_multiplication(tvec, vec1, (MatrixObject*)v2) == -1) { - return NULL; + return newVectorObject(tvec, vec1->size, Py_NEW, Py_TYPE(vec1)); } + else if (QuaternionObject_Check(v2)) { + /* VEC * QUAT */ + QuaternionObject *quat2 = (QuaternionObject*)v2; + float tvec[3]; - return newVectorObject(tvec, vec1->size, Py_NEW, Py_TYPE(vec1)); - } else if (QuaternionObject_Check(v2)) { - /* VEC * QUAT */ - QuaternionObject *quat2 = (QuaternionObject*)v2; - float tvec[3]; - - if(vec1->size != 3) { - PyErr_SetString(PyExc_TypeError, "Vector multiplication: only 3D vector rotations (with quats) currently supported"); - return NULL; + if(vec1->size != 3) { + PyErr_SetString(PyExc_TypeError, "Vector multiplication: only 3D vector rotations (with quats) currently supported"); + return NULL; + } + if(!BaseMath_ReadCallback(quat2)) { + return NULL; + } + copy_v3_v3(tvec, vec1->vec); + mul_qt_v3(quat2->quat, tvec); + return newVectorObject(tvec, 3, Py_NEW, Py_TYPE(vec1)); } - if(!BaseMath_ReadCallback(quat2)) { - return NULL; + else if (((scalar= PyFloat_AsDouble(v2)) == -1.0 && PyErr_Occurred())==0) { /* VEC*FLOAT */ + return vector_mul_float(vec1, scalar); } - copy_v3_v3(tvec, vec1->vec); - mul_qt_v3(quat2->quat, tvec); - return newVectorObject(tvec, 3, Py_NEW, Py_TYPE(vec1)); } - else if (((scalar= PyFloat_AsDouble(v2)) == -1.0 && PyErr_Occurred())==0) { /* VEC*FLOAT */ - int i; - float vec[MAX_DIMENSIONS]; - - for(i = 0; i < vec1->size; i++) { - vec[i] = vec1->vec[i] * scalar; + else if (vec2) { + if (((scalar= PyFloat_AsDouble(v1)) == -1.0 && PyErr_Occurred())==0) { /* VEC*FLOAT */ + return vector_mul_float(vec2, scalar); } - return newVectorObject(vec, vec1->size, Py_NEW, Py_TYPE(vec1)); - } - - PyErr_SetString(PyExc_TypeError, "Vector multiplication: arguments not acceptable for this operation"); + else { + BKE_assert(!"internal error"); + } + + PyErr_Format(PyExc_TypeError, "Vector multiplication: not supported between '%.200s' and '%.200s' types", Py_TYPE(v1)->tp_name, Py_TYPE(v2)->tp_name); return NULL; } |