diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-12-19 09:14:09 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-12-19 09:14:09 +0400 |
commit | 0bc83a2f351294ecbb489764e342f2c5e354b271 (patch) | |
tree | 5f6970f3c7280e73c2b050848b21eed310d5ce2d /source/blender/python/mathutils | |
parent | c3675c0e9059831e2978500ae4f2651d6049b0ed (diff) |
- mathutils matrix creation - use memcpy rather than copying every matrix row/col individually.
- creating a new non-square matrix would use uninitialized memory.
Diffstat (limited to 'source/blender/python/mathutils')
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index 0922518b662..4e161d5e1e8 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -1956,14 +1956,13 @@ self->matrix[1][1] = self->contigPtr[4] */ pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON (i.e. it must be created here with PyMEM_malloc())*/ PyObject *Matrix_CreatePyObject(float *mat, - const unsigned short rowSize, const unsigned short colSize, + const unsigned short row_size, const unsigned short col_size, int type, PyTypeObject *base_type) { MatrixObject *self; - int row, col; - /*matrix objects can be any 2-4row x 2-4col matrix*/ - if (rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4) { + /* matrix objects can be any 2-4row x 2-4col matrix */ + if (row_size < 2 || row_size > 4 || col_size < 2 || col_size > 4) { PyErr_SetString(PyExc_RuntimeError, "Matrix(): " "row and column sizes must be between 2 and 4"); @@ -1974,8 +1973,8 @@ PyObject *Matrix_CreatePyObject(float *mat, (MatrixObject *)PyObject_GC_New(MatrixObject, &matrix_Type); if (self) { - self->row_size = rowSize; - self->col_size = colSize; + self->row_size = row_size; + self->col_size = col_size; /* init callbacks as NULL */ self->cb_user= NULL; @@ -1986,25 +1985,24 @@ PyObject *Matrix_CreatePyObject(float *mat, self->wrapped = Py_WRAP; } else if (type == Py_NEW) { - self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float)); + self->contigPtr = PyMem_Malloc(row_size * col_size * sizeof(float)); if (self->contigPtr == NULL) { /*allocation failure*/ PyErr_SetString(PyExc_MemoryError, "Matrix(): " "problem allocating pointer space"); return NULL; } - /*parse*/ + if (mat) { /*if a float array passed*/ - for (row = 0; row < rowSize; row++) { - for (col = 0; col < colSize; col++) { - MATRIX_ITEM(self, row, col) = mat[(row * colSize) + col]; - } - } + memcpy(self->contigPtr, mat, row_size * col_size * sizeof(float)); } - else if (rowSize == colSize) { /*or if no arguments are passed return identity matrix for square matrices */ + else if (row_size == col_size) { /*or if no arguments are passed return identity matrix for square matrices */ PyObject *ret_dummy= Matrix_identity(self); Py_DECREF(ret_dummy); } + else { + memset(self->contigPtr, 0, row_size * col_size * sizeof(float)); + } self->wrapped = Py_NEW; } else { |