mathutils.Matrix.Scale(factor, size, axis)

- 'axis' arg was not coerced from a tuple like other args now do.
- 'axis' arg was being modified in-place (VERY BAD).
- also made new function matrix_3x3_as_4x4().
This commit is contained in:
Campbell Barton 2011-02-06 11:17:22 +00:00
parent 1cdab667bc
commit d9aa3f66a6

@ -45,7 +45,6 @@
static PyObject *Matrix_copy(MatrixObject *self); static PyObject *Matrix_copy(MatrixObject *self);
static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value); static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value);
/* matrix vector callbacks */ /* matrix vector callbacks */
@ -162,9 +161,21 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
/* when a matrix is 4x4 size but initialized as a 3x3, re-assign values for 4x4 */
static void matrix_3x3_as_4x4(float mat[16])
{
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[7] = 0.0f;
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0.0f;
}
/*-----------------------CLASS-METHODS----------------------------*/ /*-----------------------CLASS-METHODS----------------------------*/
//----------------------------------mathutils.RotationMatrix() ----------
//mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc.
static char C_Matrix_Rotation_doc[] = static char C_Matrix_Rotation_doc[] =
".. classmethod:: Rotation(angle, size, axis)\n" ".. classmethod:: Rotation(angle, size, axis)\n"
@ -266,15 +277,7 @@ static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args)
} }
if(matSize == 4) { if(matSize == 4) {
//resize matrix matrix_3x3_as_4x4(mat);
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[7] = 0.0f;
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0.0f;
} }
//pass to matrix creation //pass to matrix creation
return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls); return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
@ -321,28 +324,26 @@ static char C_Matrix_Scale_doc[] =
; ;
static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args) static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args)
{ {
VectorObject *vec = NULL; PyObject *vec= NULL;
float norm = 0.0f, factor; int vec_size;
int matSize, x; float tvec[3];
float factor;
int matSize;
float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
if(!PyArg_ParseTuple(args, "fi|O!:Matrix.Scale", &factor, &matSize, &vector_Type, &vec)) { if(!PyArg_ParseTuple(args, "fi|O!:Matrix.Scale", &factor, &matSize, &vec)) {
return NULL; return NULL;
} }
if(matSize != 2 && matSize != 3 && matSize != 4) { if(matSize != 2 && matSize != 3 && matSize != 4) {
PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix.Scale(): can only return a 2x2 3x3 or 4x4 matrix"); PyErr_SetString(PyExc_AttributeError, "Matrix.Scale(): can only return a 2x2 3x3 or 4x4 matrix");
return NULL; return NULL;
} }
if(vec) { if(vec) {
if(vec->size > 2 && matSize == 2) { vec_size= (matSize == 2 ? 2 : 3);
PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix.Scale(): 2D vectors when scaling in 2D required"); if(mathutils_array_parse(tvec, vec_size, vec_size, vec, "Matrix.Scale(factor, size, axis), invalid 'axis' arg") == -1) {
return NULL; return NULL;
} }
if(!BaseMath_ReadCallback(vec))
return NULL;
} }
if(vec == NULL) { //scaling along axis if(vec == NULL) { //scaling along axis
if(matSize == 2) { if(matSize == 2) {
@ -353,42 +354,37 @@ static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args)
mat[4] = factor; mat[4] = factor;
mat[8] = factor; mat[8] = factor;
} }
} else { //scaling in arbitrary direction }
else { //scaling in arbitrary direction
//normalize arbitrary axis //normalize arbitrary axis
for(x = 0; x < vec->size; x++) { float norm = 0.0f;
norm += vec->vec[x] * vec->vec[x]; int x;
for(x = 0; x < vec_size; x++) {
norm += tvec[x] * tvec[x];
} }
norm = (float) sqrt(norm); norm = (float) sqrt(norm);
for(x = 0; x < vec->size; x++) { for(x = 0; x < vec_size; x++) {
vec->vec[x] /= norm; tvec[x] /= norm;
} }
if(matSize == 2) { if(matSize == 2) {
mat[0] = 1 +((factor - 1) *(vec->vec[0] * vec->vec[0])); mat[0] = 1 + ((factor - 1) *(tvec[0] * tvec[0]));
mat[1] =((factor - 1) *(vec->vec[0] * vec->vec[1])); mat[1] = ((factor - 1) *(tvec[0] * tvec[1]));
mat[2] =((factor - 1) *(vec->vec[0] * vec->vec[1])); mat[2] = ((factor - 1) *(tvec[0] * tvec[1]));
mat[3] = 1 + ((factor - 1) *(vec->vec[1] * vec->vec[1])); mat[3] = 1 + ((factor - 1) *(tvec[1] * tvec[1]));
} else { } else {
mat[0] = 1 + ((factor - 1) *(vec->vec[0] * vec->vec[0])); mat[0] = 1 + ((factor - 1) *(tvec[0] * tvec[0]));
mat[1] =((factor - 1) *(vec->vec[0] * vec->vec[1])); mat[1] = ((factor - 1) *(tvec[0] * tvec[1]));
mat[2] =((factor - 1) *(vec->vec[0] * vec->vec[2])); mat[2] = ((factor - 1) *(tvec[0] * tvec[2]));
mat[3] =((factor - 1) *(vec->vec[0] * vec->vec[1])); mat[3] = ((factor - 1) *(tvec[0] * tvec[1]));
mat[4] = 1 + ((factor - 1) *(vec->vec[1] * vec->vec[1])); mat[4] = 1 + ((factor - 1) *(tvec[1] * tvec[1]));
mat[5] =((factor - 1) *(vec->vec[1] * vec->vec[2])); mat[5] = ((factor - 1) *(tvec[1] * tvec[2]));
mat[6] =((factor - 1) *(vec->vec[0] * vec->vec[2])); mat[6] = ((factor - 1) *(tvec[0] * tvec[2]));
mat[7] =((factor - 1) *(vec->vec[1] * vec->vec[2])); mat[7] = ((factor - 1) *(tvec[1] * tvec[2]));
mat[8] = 1 + ((factor - 1) *(vec->vec[2] * vec->vec[2])); mat[8] = 1 + ((factor - 1) *(tvec[2] * tvec[2]));
} }
} }
if(matSize == 4) { if(matSize == 4) {
//resize matrix matrix_3x3_as_4x4(mat);
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[7] = 0.0f;
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0.0f;
} }
//pass to matrix creation //pass to matrix creation
return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls); return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
@ -495,15 +491,7 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
} }
} }
if(matSize == 4) { if(matSize == 4) {
//resize matrix matrix_3x3_as_4x4(mat);
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[7] = 0.0f;
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0.0f;
} }
//pass to matrix creation //pass to matrix creation
return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls); return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
@ -594,15 +582,7 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args)
} }
if(matSize == 4) { if(matSize == 4) {
//resize matrix matrix_3x3_as_4x4(mat);
mat[10] = mat[8];
mat[9] = mat[7];
mat[8] = mat[6];
mat[7] = 0.0f;
mat[6] = mat[5];
mat[5] = mat[4];
mat[4] = mat[3];
mat[3] = 0.0f;
} }
//pass to matrix creation //pass to matrix creation
return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls); return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);