[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42720] trunk/blender/source/blender/ python/mathutils/mathutils_Matrix.c: - mathutils matrix creation - use memcpy rather than copying every matrix row /col individually.

Campbell Barton ideasman42 at gmail.com
Mon Dec 19 06:14:20 CET 2011


Revision: 42720
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42720
Author:   campbellbarton
Date:     2011-12-19 05:14:09 +0000 (Mon, 19 Dec 2011)
Log Message:
-----------
- mathutils matrix creation - use memcpy rather than copying every matrix row/col individually.
- creating a new non-square matrix would use uninitialized memory.

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-19 03:12:10 UTC (rev 42719)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-19 05:14:09 UTC (rev 42720)
@@ -1956,14 +1956,13 @@
   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 @@
 						(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 @@
 			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 {




More information about the Bf-blender-cvs mailing list