Page MenuHome

row_col_switch.patch

row_col_switch.patch

Index: source/blender/python/mathutils/mathutils.c
===================================================================
--- source/blender/python/mathutils/mathutils.c (revision 42754)
+++ source/blender/python/mathutils/mathutils.c (working copy)
@@ -456,6 +456,7 @@
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;
}
Index: source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- source/blender/python/mathutils/mathutils_Matrix.c (revision 42754)
+++ source/blender/python/mathutils/mathutils_Matrix.c (working copy)
@@ -42,7 +42,7 @@
static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value);
static PyObject *matrix__apply_to_copy(PyNoArgsFunction matrix_func, MatrixObject *self);
-/* matrix vector callbacks */
+/* matrix row callbacks */
int mathutils_matrix_vector_cb_index= -1;
static int mathutils_matrix_vector_check(BaseMathObject *bmo)
@@ -51,56 +51,56 @@
return BaseMath_ReadCallback(self);
}
-static int mathutils_matrix_vector_get(BaseMathObject *bmo, int col)
+static int mathutils_matrix_vector_get(BaseMathObject *bmo, int row)
{
MatrixObject *self= (MatrixObject *)bmo->cb_user;
- int row;
+ int col;
if (BaseMath_ReadCallback(self) == -1)
return -1;
- for (row=0; row < self->num_row; row++) {
- bmo->data[row] = MATRIX_ITEM(self, row, col);
+ for (col=0; col < self->num_col; col++) {
+ bmo->data[col] = MATRIX_ITEM(self, row, col);
}
return 0;
}
-static int mathutils_matrix_vector_set(BaseMathObject *bmo, int col)
+static int mathutils_matrix_vector_set(BaseMathObject *bmo, int row)
{
MatrixObject *self= (MatrixObject *)bmo->cb_user;
- int row;
+ int col;
if (BaseMath_ReadCallback(self) == -1)
return -1;
- for (row=0; row < self->num_row; row++) {
- MATRIX_ITEM(self, row, col) = bmo->data[row];
+ for (col=0; col < self->num_col; col++) {
+ MATRIX_ITEM(self, row, col) = bmo->data[col];
}
(void)BaseMath_WriteCallback(self);
return 0;
}
-static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int col, int row)
+static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int row, int col)
{
MatrixObject *self= (MatrixObject *)bmo->cb_user;
if (BaseMath_ReadCallback(self) == -1)
return -1;
- bmo->data[row]= MATRIX_ITEM(self, row, col);
+ bmo->data[col]= MATRIX_ITEM(self, row, col);
return 0;
}
-static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int col, int row)
+static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int row, int col)
{
MatrixObject *self= (MatrixObject *)bmo->cb_user;
if (BaseMath_ReadCallback(self) == -1)
return -1;
- MATRIX_ITEM(self, row, col) = bmo->data[row];
+ MATRIX_ITEM(self, row, col) = bmo->data[col];
(void)BaseMath_WriteCallback(self);
return 0;
@@ -115,6 +115,79 @@
};
/* 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 row;
+
+ if (BaseMath_ReadCallback(self) == -1)
+ return -1;
+
+ for (row = 0; row < self->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 row;
+
+ if (BaseMath_ReadCallback(self) == -1)
+ return -1;
+
+ for (row = 0; row < self->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
@@ -134,15 +207,19 @@
{
PyObject *arg= PyTuple_GET_ITEM(args, 0);
+ /* Input is now as a sequence of rows so length of sequence
+ * is the number of rows */
/* -1 is an error, size checks will accunt for this */
- const unsigned short num_col= PySequence_Size(arg);
+ const unsigned short num_row= PySequence_Size(arg);
- if (num_col >= 2 && num_col <= 4) {
+ if (num_row >= 2 && num_row <= 4) {
PyObject *item= PySequence_GetItem(arg, 0);
- const unsigned short num_row= PySequence_Size(item);
+ /* Since each item is a row, number of items is the
+ * same as the number of columns */
+ const unsigned short num_col= PySequence_Size(item);
Py_XDECREF(item);
- if (num_row >= 2 && num_row <= 4) {
+ if (num_col >= 2 && num_col <= 4) {
/* sane row & col size, new matrix and assign as slice */
PyObject *matrix= Matrix_CreatePyObject(NULL, num_col, num_row, Py_NEW, type);
if (Matrix_ass_slice((MatrixObject *)matrix, 0, INT_MAX, arg) == 0) {
@@ -761,7 +838,8 @@
);
static PyObject *Matrix_resize_4x4(MatrixObject *self)
{
- int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
+ int col;
+ float mat[16];
if (self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError,
@@ -784,30 +862,15 @@
return NULL;
}
+ unit_m4((float (*)[4])mat);
+
/*move data to new spot in array + clean*/
- for (blank_rows = (4 - self->num_col); blank_rows > 0; blank_rows--) {
- for (x = 0; x < 4; x++) {
- index = (4 * (self->num_col + (blank_rows - 1))) + x;
- if (index == 10 || index == 15) {
- self->matrix[index] = 1.0f;
- }
- else {
- self->matrix[index] = 0.0f;
- }
- }
+ for (col = 0; col < self->num_col; col++) {
+ memcpy(mat + 4 * col, MATRIX_COL_PTR(self, col), self->num_row * sizeof(float));
}
- for (x = 1; x <= self->num_col; x++) {
- first_row_elem = (self->num_row * (self->num_col - x));
- curr_pos = (first_row_elem + (self->num_row -1));
- new_pos = (4 * (self->num_col - x)) + (curr_pos - first_row_elem);
- for (blank_columns = (4 - self->num_row); blank_columns > 0; blank_columns--) {
- self->matrix[new_pos + blank_columns] = 0.0f;
- }
- for ( ; curr_pos >= first_row_elem; curr_pos--) {
- self->matrix[new_pos] = self->matrix[curr_pos];
- new_pos--;
- }
- }
+
+ memcpy(self->matrix, mat, 16 * sizeof(float));
+
self->num_col = 4;
self->num_row = 4;
@@ -1298,13 +1361,13 @@
if (BaseMath_ReadCallback(self) == -1)
return NULL;
- for (col = 0; col < self->num_col; col++) {
- rows[col]= PyTuple_New(self->num_row);
- for (row = 0; row < self->num_row; row++) {
- PyTuple_SET_ITEM(rows[col], row, PyFloat_FromDouble(MATRIX_ITEM(self, row, col)));
+ for (row = 0; row < self->num_row; row++) {
+ rows[row]= PyTuple_New(self->num_col);
+ for (col = 0; col < self->num_col; col++) {
+ PyTuple_SET_ITEM(rows[row], col, PyFloat_FromDouble(MATRIX_ITEM(self, row, col)));
}
}
- switch (self->num_col) {
+ switch (self->num_row) {
case 2: return PyUnicode_FromFormat("Matrix((%R,\n"
" %R))", rows[0], rows[1]);
@@ -1402,44 +1465,48 @@
sequence length*/
static int Matrix_len(MatrixObject *self)
{
- return (self->num_col);
+ return (self->num_row);
}
/*----------------------------object[]---------------------------
sequence accessor (get)
the wrapped vector gives direct access to the matrix data*/
-static PyObject *Matrix_item(MatrixObject *self, int i)
+static PyObject *Matrix_item(MatrixObject *self, int row)
{
if (BaseMath_ReadCallback(self) == -1)
return NULL;
- if (i < 0 || i >= self->num_col) {
+ if (row < 0 || row >= self->num_row) {
PyErr_SetString(PyExc_IndexError,
"matrix[attribute]: "
"array index out of range");
return NULL;
}
- return Vector_CreatePyObject_cb((PyObject *)self, self->num_row, mathutils_matrix_vector_cb_index, i);
+ return Vector_CreatePyObject_cb((PyObject *)self, self->num_col, mathutils_matrix_vector_cb_index, row);
}
/*----------------------------object[]-------------------------
sequence accessor (set) */
-static int Matrix_ass_item(MatrixObject *self, int i, PyObject *value)
+static int Matrix_ass_item(MatrixObject *self, int row, PyObject *value)
{
+ int col;
float vec[4];
if (BaseMath_ReadCallback(self) == -1)
return -1;
- if (i >= self->num_col || i < 0) {
+ if (row >= self->num_row || row < 0) {
PyErr_SetString(PyExc_IndexError,
- "matrix[attribute] = x: bad column");
+ "matrix[attribute] = x: bad row");
return -1;
}
- if (mathutils_array_parse(vec, self->num_row, self->num_row, value, "matrix[i] = value assignment") < 0) {
+ if (mathutils_array_parse(vec, self->num_col, self->num_col, value, "matrix[i] = value assignment") < 0) {
return -1;
}
- memcpy(MATRIX_COL_PTR(self, i), vec, self->num_row * sizeof(float));
+ /* Since we are assigning a row we cannot memcpy */
+ for (col = 0; col < self->num_col; col++) {
+ MATRIX_ITEM(self, row, col) = vec[col];
+ }
(void)BaseMath_WriteCallback(self);
return 0;
@@ -1456,14 +1523,14 @@
if (BaseMath_ReadCallback(self) == -1)
return NULL;
- CLAMP(begin, 0, self->num_col);
- CLAMP(end, 0, self->num_col);
+ CLAMP(begin, 0, self->num_row);
+ CLAMP(end, 0, self->num_row);
begin= MIN2(begin, end);
tuple= PyTuple_New(end - begin);
for (count= begin; count < end; count++) {
PyTuple_SET_ITEM(tuple, count - begin,
- Vector_CreatePyObject_cb((PyObject *)self, self->num_row, mathutils_matrix_vector_cb_index, count));
+ Vector_CreatePyObject_cb((PyObject *)self, self->num_col, mathutils_matrix_vector_cb_index, count));
}
@@ -1478,8 +1545,8 @@
if (BaseMath_ReadCallback(self) == -1)
return -1;
- CLAMP(begin, 0, self->num_col);
- CLAMP(end, 0, self->num_col);
+ CLAMP(begin, 0, self->num_row);
+ CLAMP(end, 0, self->num_row);
begin = MIN2(begin, end);
/* non list/tuple cases */
@@ -1489,8 +1556,9 @@
}
else {
const int size= end - begin;
- int i;
+ int row, col;
float mat[16];
+ float vec[4];
if (PySequence_Fast_GET_SIZE(value_fast) != size) {
Py_DECREF(value_fast);
@@ -1500,22 +1568,25 @@
return -1;
}
+ memcpy(mat, self->matrix, self->num_col * self->num_row * sizeof(float));
+
/*parse sub items*/
- for (i = 0; i < size; i++) {
+ for (row = begin; row < end; row++) {
/*parse each sub sequence*/
- PyObject *item= PySequence_Fast_GET_ITEM(value_fast, i);
+ PyObject *item= PySequence_Fast_GET_ITEM(value_fast, row - begin);
- if (mathutils_array_parse(&mat[i * self->num_row], self->num_row, self->num_row, item,
- "matrix[begin:end] = value assignment") < 0)
- {
+ if (mathutils_array_parse(vec, self->num_col, self->num_col, item, "matrix[begin:end] = value assignment") < 0)
return -1;
+
+ for (col = 0; col < self->num_col; col++) {
+ mat[col * self->num_row + row] = vec[col];
}
}
Py_DECREF(value_fast);
/*parsed well - now set in matrix*/
- memcpy(self->matrix + (begin * self->num_row), mat, sizeof(float) * (size * self->num_row));
+ memcpy(self->matrix, mat, self->num_col * self->num_row * sizeof(float));
(void)BaseMath_WriteCallback(self);
return 0;
@@ -1621,12 +1692,19 @@
double dot = 0.0f;
int col, row, item;
+ if (mat1->num_col != mat2->num_row) {
+ PyErr_SetString(PyExc_ValueError,
+ "matrix1 * matrix2: matrix1 number of columns "
+ "and the matrix2 number of rows must be the same");
+ return NULL;
+ }
+
for (col = 0; col < mat2->num_col; col++) {
for (row = 0; row < mat1->num_row; row++) {
for (item = 0; item < mat1->num_col; item++) {
dot += MATRIX_ITEM(mat1, row, item) * MATRIX_ITEM(mat2, item, col);
}
- mat[((col * mat1->num_row) + row)] = (float)dot;
+ mat[col * mat1->num_row + row] = (float)dot;
dot = 0.0f;
}
}
@@ -1640,7 +1718,7 @@
}
}
else if (mat1) {
- /*VEC * MATRIX */
+ /* MATRIX * VECTOR */
if (VectorObject_Check(m2)) {
VectorObject *vec2= (VectorObject *)m2;
float tvec[4];
@@ -1650,7 +1728,7 @@
return NULL;
}
- return Vector_CreatePyObject(tvec, vec2->size, Py_NEW, Py_TYPE(m2));
+ return Vector_CreatePyObject(tvec, mat1->num_row, Py_NEW, Py_TYPE(m2));
}
/*FLOAT/INT * MATRIX */
else if (((scalar= PyFloat_AsDouble(m2)) == -1.0f && PyErr_Occurred())==0) {
@@ -1813,6 +1891,27 @@
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);
+ ((VectorObject *)ret)->wrapped = Py_WRAP;
+
+ return ret;
+}
+
static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closure))
{
float mat[3][3];
@@ -1876,6 +1975,7 @@
{(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)NULL, (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},
@@ -1982,8 +2082,10 @@
* 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 num_col, const unsigned short num_row,
- int type, PyTypeObject *base_type)
+ const unsigned short num_col,
+ const unsigned short num_row,
+ int type,
+ PyTypeObject *base_type)
{
MatrixObject *self;
Index: source/blender/python/mathutils/mathutils_Matrix.h
===================================================================
--- source/blender/python/mathutils/mathutils_Matrix.h (revision 42754)
+++ source/blender/python/mathutils/mathutils_Matrix.h (working copy)
@@ -71,7 +71,9 @@
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);
Index: source/blender/python/mathutils/mathutils_Vector.c
===================================================================
--- source/blender/python/mathutils/mathutils_Vector.c (revision 42754)
+++ source/blender/python/mathutils/mathutils_Vector.c (working copy)
@@ -1490,7 +1490,7 @@
{
float vec_cpy[MAX_DIMENSIONS];
double dot = 0.0f;
- int x, y, z = 0;
+ int row, col, z = 0;
if (mat->num_col != vec->size) {
if (mat->num_col == 4 && vec->size == 3) {
@@ -1509,9 +1509,9 @@
rvec[3] = 1.0f;
- for (x = 0; x < mat->num_row; x++) {
- for (y = 0; y < mat->num_col; y++) {
- dot += (double)(MATRIX_ITEM(mat, y, x) * vec_cpy[y]);
+ for (row = 0; row < mat->num_row; row++) {
+ for (col = 0; col < mat->num_col; col++) {
+ dot += (double)(MATRIX_ITEM(mat, row, col) * vec_cpy[col]);
}
rvec[z++] = (float)dot;
dot = 0.0f;
@@ -1576,7 +1576,7 @@
return NULL;
}
- return Vector_CreatePyObject(tvec, vec1->size, Py_NEW, Py_TYPE(vec1));
+ return Vector_CreatePyObject(tvec, ((MatrixObject *)v2)->num_col, Py_NEW, Py_TYPE(vec1));
}
else if (QuaternionObject_Check(v2)) {
/* VEC * QUAT */
@@ -2603,7 +2603,7 @@
*/
/* ROW VECTOR Multiplication - Vector X Matrix
- * [x][y][z] * [1][4][7]
+ * [x][y][z] * [1][4][7]
* [2][5][8]
* [3][6][9]
* vector/matrix multiplication IS NOT COMMUTATIVE!!!! */
@@ -2611,7 +2611,7 @@
{
float vec_cpy[MAX_DIMENSIONS];
double dot = 0.0f;
- int x, y, z= 0, vec_size= vec->size;
+ int row, col, z= 0, vec_size= vec->size;
if (mat->num_row != vec_size) {
if (mat->num_row == 4 && vec_size != 3) {
@@ -2632,9 +2632,9 @@
rvec[3] = 1.0f;
//muliplication
- for (x = 0; x < mat->num_col; x++) {
- for (y = 0; y < mat->num_row; y++) {
- dot += MATRIX_ITEM(mat, x, y) * vec_cpy[y];
+ for (col = 0; col < mat->num_col; col++) {
+ for (row = 0; row < mat->num_row; row++) {
+ dot += MATRIX_ITEM(mat, row, col) * vec_cpy[row];
}
rvec[z++] = (float)dot;
dot = 0.0f;

File Metadata

Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f4/78/581edd2081062421d5f08a970b20

Event Timeline