Page MenuHome

row_col_switch_20111222.patch

row_col_switch_20111222.patch

Index: source/blender/python/mathutils/mathutils.c
===================================================================
--- source/blender/python/mathutils/mathutils.c (revision 42809)
+++ 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 42809)
+++ 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) {
@@ -1283,13 +1360,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]);
@@ -1387,44 +1464,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;
@@ -1441,14 +1522,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));
}
@@ -1463,8 +1544,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 */
@@ -1474,8 +1555,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);
@@ -1485,22 +1567,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;
@@ -1813,6 +1898,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 +1982,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 +2089,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 42809)
+++ 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);

File Metadata

Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
d7/c1/8fd344321ffb073863420a7028d3

Event Timeline