diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-12-19 07:12:10 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-12-19 07:12:10 +0400 |
commit | c3675c0e9059831e2978500ae4f2651d6049b0ed (patch) | |
tree | 7246b2766259ff0e480ce4b39bae75e1ac964d6b /source/blender/python | |
parent | d0aa012b0a451f52409a5d4df33dcdcac2763562 (diff) |
remove mathutils internal variable for storing pointers to each matrix row, instead use macros to access row/column's.
also add an assert so invalid index access will raise an error in debug mode, without this it was quite easy to access invalid memory without meaning to.
no functional change.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 87 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.h | 16 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Vector.c | 4 |
3 files changed, 54 insertions, 53 deletions
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index bc68d469fc1..0922518b662 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -52,13 +52,14 @@ static int mathutils_matrix_vector_check(BaseMathObject *bmo) static int mathutils_matrix_vector_get(BaseMathObject *bmo, int subtype) { MatrixObject *self= (MatrixObject *)bmo->cb_user; - int i; + int index; if (BaseMath_ReadCallback(self) == -1) return -1; - for (i=0; i < self->col_size; i++) - bmo->data[i]= self->matrix[subtype][i]; + for (index=0; index < self->col_size; index++) { + bmo->data[index] = MATRIX_ITEM(self, subtype, index); + } return 0; } @@ -66,13 +67,14 @@ static int mathutils_matrix_vector_get(BaseMathObject *bmo, int subtype) static int mathutils_matrix_vector_set(BaseMathObject *bmo, int subtype) { MatrixObject *self= (MatrixObject *)bmo->cb_user; - int i; + int index; if (BaseMath_ReadCallback(self) == -1) return -1; - for (i=0; i < self->col_size; i++) - self->matrix[subtype][i]= bmo->data[i]; + for (index=0; index < self->col_size; index++) { + MATRIX_ITEM(self, subtype, index) = bmo->data[index]; + } (void)BaseMath_WriteCallback(self); return 0; @@ -85,7 +87,7 @@ static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int subtype, i if (BaseMath_ReadCallback(self) == -1) return -1; - bmo->data[index]= self->matrix[subtype][index]; + bmo->data[index]= MATRIX_ITEM(self, subtype, index); return 0; } @@ -96,7 +98,7 @@ static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int subtype, i if (BaseMath_ReadCallback(self) == -1) return -1; - self->matrix[subtype][index]= bmo->data[index]; + MATRIX_ITEM(self, subtype, index) = bmo->data[index]; (void)BaseMath_WriteCallback(self); return 0; @@ -620,24 +622,22 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args) void matrix_as_3x3(float mat[3][3], MatrixObject *self) { - copy_v3_v3(mat[0], self->matrix[0]); - copy_v3_v3(mat[1], self->matrix[1]); - copy_v3_v3(mat[2], self->matrix[2]); + copy_v3_v3(mat[0], MATRIX_ROW_PTR(self, 0)); + copy_v3_v3(mat[1], MATRIX_ROW_PTR(self, 1)); + copy_v3_v3(mat[2], MATRIX_ROW_PTR(self, 2)); } /* assumes rowsize == colsize is checked and the read callback has run */ static float matrix_determinant_internal(MatrixObject *self) { if (self->row_size == 2) { - return determinant_m2(self->matrix[0][0], self->matrix[0][1], - self->matrix[1][0], self->matrix[1][1]); + return determinant_m2(MATRIX_ITEM(self, 0, 0), MATRIX_ITEM(self, 0, 1), + MATRIX_ITEM(self, 1, 0), MATRIX_ITEM(self, 1, 1)); } else if (self->row_size == 3) { - return determinant_m3(self->matrix[0][0], self->matrix[0][1], - self->matrix[0][2], self->matrix[1][0], - self->matrix[1][1], self->matrix[1][2], - self->matrix[2][0], self->matrix[2][1], - self->matrix[2][2]); + return determinant_m3(MATRIX_ITEM(self, 0, 0), MATRIX_ITEM(self, 0, 1), MATRIX_ITEM(self, 0, 2), + MATRIX_ITEM(self, 1, 0), MATRIX_ITEM(self, 1, 1), MATRIX_ITEM(self, 1, 2), + MATRIX_ITEM(self, 2, 0), MATRIX_ITEM(self, 2, 1), MATRIX_ITEM(self, 2, 2)); } else { return determinant_m4((float (*)[4])self->contigPtr); @@ -781,10 +781,7 @@ static PyObject *Matrix_resize_4x4(MatrixObject *self) "problem allocating pointer space"); return NULL; } - /*set row pointers*/ - for (x = 0; x < 4; x++) { - self->matrix[x] = self->contigPtr + (x * 4); - } + /*move data to new spot in array + clean*/ for (blank_rows = (4 - self->row_size); blank_rows > 0; blank_rows--) { for (x = 0; x < 4; x++) { @@ -890,7 +887,7 @@ static PyObject *Matrix_to_translation(MatrixObject *self) return NULL; } - return Vector_CreatePyObject(self->matrix[3], 3, Py_NEW, NULL); + return Vector_CreatePyObject(MATRIX_ROW_PTR(self, 3), 3, Py_NEW, NULL); } PyDoc_STRVAR(Matrix_to_scale_doc, @@ -964,10 +961,10 @@ static PyObject *Matrix_invert(MatrixObject *self) if (det != 0) { /*calculate the classical adjoint*/ if (self->row_size == 2) { - mat[0] = self->matrix[1][1]; - mat[1] = -self->matrix[0][1]; - mat[2] = -self->matrix[1][0]; - mat[3] = self->matrix[0][0]; + mat[0] = MATRIX_ITEM(self, 1, 1); + mat[1] = -MATRIX_ITEM(self, 0, 1); + mat[2] = -MATRIX_ITEM(self, 1, 0); + mat[3] = MATRIX_ITEM(self, 0, 0); } else if (self->row_size == 3) { adjoint_m3_m3((float (*)[3]) mat,(float (*)[3])self->contigPtr); @@ -982,7 +979,7 @@ static PyObject *Matrix_invert(MatrixObject *self) /*set values*/ for (x = 0; x < self->row_size; x++) { for (y = 0; y < self->col_size; y++) { - self->matrix[x][y] = mat[z]; + MATRIX_ITEM(self, x, y) = mat[z]; z++; } } @@ -1173,8 +1170,6 @@ PyDoc_STRVAR(Matrix_transpose_doc, ); static PyObject *Matrix_transpose(MatrixObject *self) { - float t = 0.0f; - if (BaseMath_ReadCallback(self) == -1) return NULL; @@ -1186,9 +1181,9 @@ static PyObject *Matrix_transpose(MatrixObject *self) } if (self->row_size == 2) { - t = self->matrix[1][0]; - self->matrix[1][0] = self->matrix[0][1]; - self->matrix[0][1] = t; + const float t = MATRIX_ITEM(self, 1, 0); + MATRIX_ITEM(self, 1, 0) = MATRIX_ITEM(self, 0, 1); + MATRIX_ITEM(self, 0, 1) = t; } else if (self->row_size == 3) { transpose_m3((float (*)[3])self->contigPtr); @@ -1256,10 +1251,10 @@ static PyObject *Matrix_identity(MatrixObject *self) } if (self->row_size == 2) { - self->matrix[0][0] = 1.0f; - self->matrix[0][1] = 0.0f; - self->matrix[1][0] = 0.0f; - self->matrix[1][1] = 1.0f; + MATRIX_ITEM(self, 0, 0) = 1.0f; + MATRIX_ITEM(self, 0, 1) = 0.0f; + MATRIX_ITEM(self, 1, 0) = 0.0f; + MATRIX_ITEM(self, 1, 1) = 1.0f; } else if (self->row_size == 3) { unit_m3((float (*)[3])self->contigPtr); @@ -1304,7 +1299,7 @@ static PyObject *Matrix_repr(MatrixObject *self) for (x = 0; x < self->row_size; x++) { rows[x]= PyTuple_New(self->col_size); for (y = 0; y < self->col_size; y++) { - PyTuple_SET_ITEM(rows[x], y, PyFloat_FromDouble(self->matrix[x][y])); + PyTuple_SET_ITEM(rows[x], y, PyFloat_FromDouble(MATRIX_ITEM(self, x, y))); } } switch (self->row_size) { @@ -1406,7 +1401,7 @@ static int Matrix_ass_item(MatrixObject *self, int i, PyObject *value) return -1; } - memcpy(self->matrix[i], vec, self->col_size *sizeof(float)); + memcpy(MATRIX_ROW_PTR(self, i), vec, self->col_size * sizeof(float)); (void)BaseMath_WriteCallback(self); return 0; @@ -1591,7 +1586,7 @@ static PyObject *Matrix_mul(PyObject *m1, PyObject *m2) for (x = 0; x < mat2->row_size; x++) { for (y = 0; y < mat1->col_size; y++) { for (z = 0; z < mat1->row_size; z++) { - dot += (mat1->matrix[z][y] * mat2->matrix[x][z]); + dot += MATRIX_ITEM(mat1, z, y) * MATRIX_ITEM(mat2, x, z); } mat[((x * mat1->col_size) + y)] = (float)dot; dot = 0.0f; @@ -1965,7 +1960,7 @@ PyObject *Matrix_CreatePyObject(float *mat, int type, PyTypeObject *base_type) { MatrixObject *self; - int x, row, col; + int row, col; /*matrix objects can be any 2-4row x 2-4col matrix*/ if (rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4) { @@ -1988,10 +1983,6 @@ PyObject *Matrix_CreatePyObject(float *mat, if (type == Py_WRAP) { self->contigPtr = mat; - /*pointer array points to contigous memory*/ - for (x = 0; x < rowSize; x++) { - self->matrix[x] = self->contigPtr + (x * colSize); - } self->wrapped = Py_WRAP; } else if (type == Py_NEW) { @@ -2002,15 +1993,11 @@ PyObject *Matrix_CreatePyObject(float *mat, "problem allocating pointer space"); return NULL; } - /*pointer array points to contigous memory*/ - for (x = 0; x < rowSize; x++) { - self->matrix[x] = self->contigPtr + (x * colSize); - } /*parse*/ if (mat) { /*if a float array passed*/ for (row = 0; row < rowSize; row++) { for (col = 0; col < colSize; col++) { - self->matrix[row][col] = mat[(row * colSize) + col]; + MATRIX_ITEM(self, row, col) = mat[(row * colSize) + col]; } } } diff --git a/source/blender/python/mathutils/mathutils_Matrix.h b/source/blender/python/mathutils/mathutils_Matrix.h index 6dfcbb1faf9..98253bf7659 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.h +++ b/source/blender/python/mathutils/mathutils_Matrix.h @@ -38,9 +38,23 @@ extern PyTypeObject matrix_Type; #define MatrixObject_Check(_v) PyObject_TypeCheck((_v), &matrix_Type) #define MATRIX_MAX_DIM 4 +/* to remove pymat->matrix[row][col] */ + +#ifdef DEBUG +# define MATRIX_ASSERT(_mat, _row, _col) (BLI_assert(_row < (_mat)->row_size && _col < (_mat)->col_size)) +#else +# define MATRIX_ASSERT(_mat, _row, _col) (void)0 +#endif + +#define MATRIX_ITEM_INDEX(_mat, _row, _col) (MATRIX_ASSERT(_mat, _row, _col), (((_mat)->col_size * (_row)) + (_col))) +#define MATRIX_ITEM_PTR( _mat, _row, _col) ((_mat)->contigPtr + MATRIX_ITEM_INDEX(_mat, _row, _col)) +#define MATRIX_ITEM( _mat, _row, _col) ((_mat)->contigPtr [MATRIX_ITEM_INDEX(_mat, _row, _col)]) + +#define MATRIX_ROW_INDEX(_mat, _row) (MATRIX_ITEM_INDEX(_mat, _row, 0)) +#define MATRIX_ROW_PTR( _mat, _row) ((_mat)->contigPtr + MATRIX_ROW_INDEX(_mat, _row)) + typedef struct { BASE_MATH_MEMBERS(contigPtr); - float *matrix[MATRIX_MAX_DIM]; /* ptr to the contigPtr (accessor) */ unsigned short row_size; unsigned short col_size; } MatrixObject; diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c index b9bfde58248..aedf60e7191 100644 --- a/source/blender/python/mathutils/mathutils_Vector.c +++ b/source/blender/python/mathutils/mathutils_Vector.c @@ -1487,7 +1487,7 @@ int column_vector_multiplication(float rvec[MAX_DIMENSIONS], VectorObject* vec, for (x = 0; x < mat->col_size; x++) { for (y = 0; y < mat->row_size; y++) { - dot += (double)(mat->matrix[y][x] * vec_cpy[y]); + dot += (double)(MATRIX_ITEM(mat, y, x) * vec_cpy[y]); } rvec[z++] = (float)dot; dot = 0.0f; @@ -2610,7 +2610,7 @@ static int row_vector_multiplication(float rvec[MAX_DIMENSIONS], VectorObject *v //muliplication for (x = 0; x < mat->row_size; x++) { for (y = 0; y < mat->col_size; y++) { - dot += mat->matrix[x][y] * vec_cpy[y]; + dot += MATRIX_ITEM(mat, x, y) * vec_cpy[y]; } rvec[z++] = (float)dot; dot = 0.0f; |