From 26f69488ca61e13d4c9146eba735351bb87496de Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 21 Dec 2011 23:12:16 +0000 Subject: Matrix.translation wrapper vector, continent accessing to matrix[3][0:3]. this is a part of patch 29534, being applied separately from patch [#29534] Change Matrix Representation and Access in Python to Conform with Standard Notation by Andrew Hale (trumanblending) --- source/blender/python/mathutils/mathutils.c | 1 + source/blender/python/mathutils/mathutils_Matrix.c | 127 +++++++++++++++++++++ source/blender/python/mathutils/mathutils_Matrix.h | 2 + 3 files changed, 130 insertions(+) (limited to 'source') diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c index 1dfd7c574dd..1147930ff65 100644 --- a/source/blender/python/mathutils/mathutils.c +++ b/source/blender/python/mathutils/mathutils.c @@ -456,6 +456,7 @@ PyMODINIT_FUNC PyInit_mathutils(void) Py_INCREF(item); mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb); + mathutils_matrix_column_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_column_cb); return submodule; } diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index 47b101c73dc..19675d2a863 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -115,6 +115,87 @@ Mathutils_Callback mathutils_matrix_vector_cb = { }; /* matrix vector callbacks, this is so you can do matrix[i][j] = val */ +/* matrix row callbacks */ +int mathutils_matrix_column_cb_index= -1; + +static int mathutils_matrix_column_check(BaseMathObject *bmo) +{ + MatrixObject *self= (MatrixObject *)bmo->cb_user; + return BaseMath_ReadCallback(self); +} + +static int mathutils_matrix_column_get(BaseMathObject *bmo, int col) +{ + MatrixObject *self= (MatrixObject *)bmo->cb_user; + int num_row; + int row; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + /* for 'translation' size will always be '3' even on 4x4 vec */ + num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size); + + for (row = 0; row < num_row; row++) { + bmo->data[row] = MATRIX_ITEM(self, row, col); + } + + return 0; +} + +static int mathutils_matrix_column_set(BaseMathObject *bmo, int col) +{ + MatrixObject *self= (MatrixObject *)bmo->cb_user; + int num_row; + int row; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + /* for 'translation' size will always be '3' even on 4x4 vec */ + num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size); + + for (row = 0; row < num_row; row++) { + MATRIX_ITEM(self, row, col) = bmo->data[row]; + } + + (void)BaseMath_WriteCallback(self); + return 0; +} + +static int mathutils_matrix_column_get_index(BaseMathObject *bmo, int col, int row) +{ + MatrixObject *self= (MatrixObject *)bmo->cb_user; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + bmo->data[row]= MATRIX_ITEM(self, row, col); + return 0; +} + +static int mathutils_matrix_column_set_index(BaseMathObject *bmo, int col, int row) +{ + MatrixObject *self= (MatrixObject *)bmo->cb_user; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + MATRIX_ITEM(self, row, col) = bmo->data[row]; + + (void)BaseMath_WriteCallback(self); + return 0; +} + +Mathutils_Callback mathutils_matrix_column_cb = { + mathutils_matrix_column_check, + mathutils_matrix_column_get, + mathutils_matrix_column_set, + mathutils_matrix_column_get_index, + mathutils_matrix_column_set_index +}; +/* matrix column callbacks, this is so you can do matrix.translation = Vector() */ + //----------------------------------mathutils.Matrix() ----------------- //mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. //create a new matrix type @@ -1813,6 +1894,51 @@ static PyObject *Matrix_getColSize(MatrixObject *self, void *UNUSED(closure)) return PyLong_FromLong((long) self->num_row); } +static PyObject *Matrix_translation_get(MatrixObject *self, void *UNUSED(closure)) +{ + PyObject *ret; + + if (BaseMath_ReadCallback(self) == -1) + return NULL; + + /*must be 4x4 square matrix*/ + if (self->num_row != 4 || self->num_col != 4) { + PyErr_SetString(PyExc_AttributeError, + "Matrix.translation: " + "inappropriate matrix size, must be 4x4"); + return NULL; + } + + ret = (PyObject *)Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_column_cb_index, 3); + + return ret; +} + +static int Matrix_translation_set(MatrixObject *self, PyObject *value, void *UNUSED(closure)) +{ + float tvec[3]; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + /*must be 4x4 square matrix*/ + if (self->num_row != 4 || self->num_col != 4) { + PyErr_SetString(PyExc_AttributeError, + "Matrix.translation: " + "inappropriate matrix size, must be 4x4"); + return -1; + } + + if ((mathutils_array_parse(tvec, 3, 3, value, "Matrix.translation")) == -1) + return -1; + + copy_v3_v3(((float (*)[4])self->matrix)[3], tvec); + + (void)BaseMath_WriteCallback(self); + + return 0; +} + static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closure)) { float mat[3][3]; @@ -1876,6 +2002,7 @@ static PyGetSetDef Matrix_getseters[] = { {(char *)"row_size", (getter)Matrix_getRowSize, (setter)NULL, (char *)"The row size of the matrix (readonly).\n\n:type: int", NULL}, {(char *)"col_size", (getter)Matrix_getColSize, (setter)NULL, (char *)"The column size of the matrix (readonly).\n\n:type: int", NULL}, {(char *)"median_scale", (getter)Matrix_median_scale_get, (setter)NULL, (char *)"The average scale applied to each axis (readonly).\n\n:type: float", NULL}, + {(char *)"translation", (getter)Matrix_translation_get, (setter)Matrix_translation_set, (char *)"The translation component of the matrix.\n\n:type: Vector", NULL}, {(char *)"is_negative", (getter)Matrix_is_negative_get, (setter)NULL, (char *)"True if this matrix results in a negative scale, 3x3 and 4x4 only, (readonly).\n\n:type: bool", NULL}, {(char *)"is_orthogonal", (getter)Matrix_is_orthogonal_get, (setter)NULL, (char *)"True if this matrix is orthogonal, 3x3 and 4x4 only, (readonly).\n\n:type: bool", NULL}, {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL}, diff --git a/source/blender/python/mathutils/mathutils_Matrix.h b/source/blender/python/mathutils/mathutils_Matrix.h index 91a276b648e..e7bfc06b98b 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.h +++ b/source/blender/python/mathutils/mathutils_Matrix.h @@ -71,7 +71,9 @@ PyObject *Matrix_CreatePyObject(float *mat, PyObject *Matrix_CreatePyObject_cb(PyObject *user, int num_col, int num_row, int cb_type, int cb_subtype); extern int mathutils_matrix_vector_cb_index; +extern int mathutils_matrix_column_cb_index; extern struct Mathutils_Callback mathutils_matrix_vector_cb; +extern struct Mathutils_Callback mathutils_matrix_column_cb; void matrix_as_3x3(float mat[3][3], MatrixObject *self); -- cgit v1.2.3