a1ef4b53615c63a2bf14190ae48345653dae5233
[blender-staging.git] / source / blender / python / generic / matrix.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Michel Selten & Joseph Gilbert
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "Mathutils.h"
29
30 #include "BKE_utildefines.h"
31 #include "BLI_math.h"
32 #include "BLI_blenlib.h"
33
34 static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec); /* utility func */
35
36
37 /* matrix vector callbacks */
38 int mathutils_matrix_vector_cb_index= -1;
39
40 static int mathutils_matrix_vector_check(PyObject *self_p)
41 {
42         MatrixObject *self= (MatrixObject*)self_p;
43         return BaseMath_ReadCallback(self);
44 }
45
46 static int mathutils_matrix_vector_get(PyObject *self_p, int subtype, float *vec_from)
47 {
48         MatrixObject *self= (MatrixObject*)self_p;
49         int i;
50
51         if(!BaseMath_ReadCallback(self))
52                 return 0;
53
54         for(i=0; i<self->colSize; i++)
55                 vec_from[i]= self->matrix[subtype][i];
56
57         return 1;
58 }
59
60 static int mathutils_matrix_vector_set(PyObject *self_p, int subtype, float *vec_to)
61 {
62         MatrixObject *self= (MatrixObject*)self_p;
63         int i;
64
65         if(!BaseMath_ReadCallback(self))
66                 return 0;
67
68         for(i=0; i<self->colSize; i++)
69                 self->matrix[subtype][i]= vec_to[i];
70
71         BaseMath_WriteCallback(self);
72         return 1;
73 }
74
75 static int mathutils_matrix_vector_get_index(PyObject *self_p, int subtype, float *vec_from, int index)
76 {
77         MatrixObject *self= (MatrixObject*)self_p;
78
79         if(!BaseMath_ReadCallback(self))
80                 return 0;
81
82         vec_from[index]= self->matrix[subtype][index];
83         return 1;
84 }
85
86 static int mathutils_matrix_vector_set_index(PyObject *self_p, int subtype, float *vec_to, int index)
87 {
88         MatrixObject *self= (MatrixObject*)self_p;
89
90         if(!BaseMath_ReadCallback(self))
91                 return 0;
92
93         self->matrix[subtype][index]= vec_to[index];
94
95         BaseMath_WriteCallback(self);
96         return 1;
97 }
98
99 Mathutils_Callback mathutils_matrix_vector_cb = {
100         mathutils_matrix_vector_check,
101         mathutils_matrix_vector_get,
102         mathutils_matrix_vector_set,
103         mathutils_matrix_vector_get_index,
104         mathutils_matrix_vector_set_index
105 };
106 /* matrix vector callbacks, this is so you can do matrix[i][j] = val  */
107
108 /*-------------------------DOC STRINGS ---------------------------*/
109
110 static PyObject *Matrix_Zero( MatrixObject * self );
111 static PyObject *Matrix_Identity( MatrixObject * self );
112 static PyObject *Matrix_Transpose( MatrixObject * self );
113 static PyObject *Matrix_Determinant( MatrixObject * self );
114 static PyObject *Matrix_Invert( MatrixObject * self );
115 static PyObject *Matrix_TranslationPart( MatrixObject * self );
116 static PyObject *Matrix_RotationPart( MatrixObject * self );
117 static PyObject *Matrix_scalePart( MatrixObject * self );
118 static PyObject *Matrix_Resize4x4( MatrixObject * self );
119 static PyObject *Matrix_toEuler( MatrixObject * self, PyObject *args );
120 static PyObject *Matrix_toQuat( MatrixObject * self );
121 static PyObject *Matrix_copy( MatrixObject * self );
122
123 /*-----------------------METHOD DEFINITIONS ----------------------*/
124 static struct PyMethodDef Matrix_methods[] = {
125         {"zero", (PyCFunction) Matrix_Zero, METH_NOARGS, NULL},
126         {"identity", (PyCFunction) Matrix_Identity, METH_NOARGS, NULL},
127         {"transpose", (PyCFunction) Matrix_Transpose, METH_NOARGS, NULL},
128         {"determinant", (PyCFunction) Matrix_Determinant, METH_NOARGS, NULL},
129         {"invert", (PyCFunction) Matrix_Invert, METH_NOARGS, NULL},
130         {"translationPart", (PyCFunction) Matrix_TranslationPart, METH_NOARGS, NULL},
131         {"rotationPart", (PyCFunction) Matrix_RotationPart, METH_NOARGS, NULL},
132         {"scalePart", (PyCFunction) Matrix_scalePart, METH_NOARGS, NULL},
133         {"resize4x4", (PyCFunction) Matrix_Resize4x4, METH_NOARGS, NULL},
134         {"toEuler", (PyCFunction) Matrix_toEuler, METH_VARARGS, NULL},
135         {"toQuat", (PyCFunction) Matrix_toQuat, METH_NOARGS, NULL},
136         {"copy", (PyCFunction) Matrix_copy, METH_NOARGS, NULL},
137         {"__copy__", (PyCFunction) Matrix_copy, METH_NOARGS, NULL},
138         {NULL, NULL, 0, NULL}
139 };
140
141 //----------------------------------Mathutils.Matrix() -----------------
142 //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc.
143 //create a new matrix type
144 static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
145 {
146         PyObject *argObject, *m, *s;
147         MatrixObject *mat;
148         int argSize, seqSize = 0, i, j;
149         float matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
150                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
151         float scalar;
152
153         argSize = PyTuple_GET_SIZE(args);
154         if(argSize > 4){        //bad arg nums
155                 PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
156                 return NULL;
157         } else if (argSize == 0) { //return empty 4D matrix
158                 return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW, NULL);
159         }else if (argSize == 1){
160                 //copy constructor for matrix objects
161                 argObject = PyTuple_GET_ITEM(args, 0);
162                 if(MatrixObject_Check(argObject)){
163                         mat = (MatrixObject*)argObject;
164                         if(!BaseMath_ReadCallback(mat))
165                                 return NULL;
166
167                         memcpy(matrix, mat->contigPtr, sizeof(float) * mat->rowSize * mat->colSize);
168                         argSize = mat->rowSize;
169                         seqSize = mat->colSize;
170                 }
171         }else{ //2-4 arguments (all seqs? all same size?)
172                 for(i =0; i < argSize; i++){
173                         argObject = PyTuple_GET_ITEM(args, i);
174                         if (PySequence_Check(argObject)) { //seq?
175                                 if(seqSize){ //0 at first
176                                         if(PySequence_Length(argObject) != seqSize){ //seq size not same
177                                                 PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
178                                                 return NULL;
179                                         }
180                                 }
181                                 seqSize = PySequence_Length(argObject);
182                         }else{ //arg not a sequence
183                                 PyErr_SetString(PyExc_TypeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
184                                 return NULL;
185                         }
186                 }
187                 //all is well... let's continue parsing
188                 for (i = 0; i < argSize; i++){
189                         m = PyTuple_GET_ITEM(args, i);
190                         if (m == NULL) { // Failed to read sequence
191                                 PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n");
192                                 return NULL;
193                         }
194
195                         for (j = 0; j < seqSize; j++) {
196                                 s = PySequence_GetItem(m, j);
197                                 if (s == NULL) { // Failed to read sequence
198                                         PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n");
199                                         return NULL;
200                                 }
201                                 
202                                 scalar= (float)PyFloat_AsDouble(s);
203                                 Py_DECREF(s);
204                                 
205                                 if(scalar==-1 && PyErr_Occurred()) { // parsed item is not a number
206                                         PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
207                                         return NULL;
208                                 }
209
210                                 matrix[(seqSize*i)+j]= scalar;
211                         }
212                 }
213         }
214         return newMatrixObject(matrix, argSize, seqSize, Py_NEW, NULL);
215 }
216
217 /*-----------------------------METHODS----------------------------*/
218 /*---------------------------Matrix.toQuat() ---------------------*/
219 static PyObject *Matrix_toQuat(MatrixObject * self)
220 {
221         float quat[4];
222
223         if(!BaseMath_ReadCallback(self))
224                 return NULL;
225         
226         /*must be 3-4 cols, 3-4 rows, square matrix*/
227         if(self->colSize < 3 || self->rowSize < 3 || (self->colSize != self->rowSize)) {
228                 PyErr_SetString(PyExc_AttributeError, "Matrix.toQuat(): inappropriate matrix size - expects 3x3 or 4x4 matrix");
229                 return NULL;
230         } 
231         if(self->colSize == 3){
232         mat3_to_quat( quat,(float (*)[3])*self->matrix);
233         }else{
234                 mat4_to_quat( quat,(float (*)[4])*self->matrix);
235         }
236         
237         return newQuaternionObject(quat, Py_NEW, NULL);
238 }
239 /*---------------------------Matrix.toEuler() --------------------*/
240 PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
241 {
242         float eul[3], eul_compatf[3];
243         EulerObject *eul_compat = NULL;
244 #ifdef USE_MATHUTILS_DEG
245         int x;
246 #endif
247         
248         if(!BaseMath_ReadCallback(self))
249                 return NULL;
250         
251         if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat))
252                 return NULL;
253         
254         if(eul_compat) {
255                 if(!BaseMath_ReadCallback(eul_compat))
256                         return NULL;
257
258 #ifdef USE_MATHUTILS_DEG
259                 for(x = 0; x < 3; x++) {
260                         eul_compatf[x] = eul_compat->eul[x] * ((float)Py_PI / 180);
261                 }
262 #else
263                 VECCOPY(eul_compatf, eul_compat->eul);
264 #endif
265         }
266         
267         /*must be 3-4 cols, 3-4 rows, square matrix*/
268         if(self->colSize ==3 && self->rowSize ==3) {
269                 if(eul_compat)  mat3_to_compatible_eul( eul, eul_compatf,(float (*)[3])*self->matrix);
270                 else                    mat3_to_eul( eul,(float (*)[3])*self->matrix);
271         }else if (self->colSize ==4 && self->rowSize ==4) {
272                 float tempmat3[3][3];
273                 copy_m3_m4(tempmat3, (float (*)[4])*self->matrix);
274                 mat3_to_eul( eul,tempmat3);
275                 if(eul_compat)  mat3_to_compatible_eul( eul, eul_compatf,tempmat3);
276                 else                    mat3_to_eul( eul,tempmat3);
277                 
278         }else {
279                 PyErr_SetString(PyExc_AttributeError, "Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
280                 return NULL;
281         }
282 #ifdef USE_MATHUTILS_DEG
283         /*have to convert to degrees*/
284         for(x = 0; x < 3; x++) {
285                 eul[x] *= (float) (180 / Py_PI);
286         }
287 #endif
288         return newEulerObject(eul, Py_NEW, NULL);
289 }
290 /*---------------------------Matrix.resize4x4() ------------------*/
291 PyObject *Matrix_Resize4x4(MatrixObject * self)
292 {
293         int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
294
295         if(self->wrapped==Py_WRAP){
296                 PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - make a copy and resize that");
297                 return NULL;
298         }
299         if(self->cb_user){
300                 PyErr_SetString(PyExc_TypeError, "cannot resize owned data - make a copy and resize that");
301                 return NULL;
302         }
303         
304         self->contigPtr = PyMem_Realloc(self->contigPtr, (sizeof(float) * 16));
305         if(self->contigPtr == NULL) {
306                 PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
307                 return NULL;
308         }
309         self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4));
310         if(self->matrix == NULL) {
311                 PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
312                 return NULL;
313         }
314         /*set row pointers*/
315         for(x = 0; x < 4; x++) {
316                 self->matrix[x] = self->contigPtr + (x * 4);
317         }
318         /*move data to new spot in array + clean*/
319         for(blank_rows = (4 - self->rowSize); blank_rows > 0; blank_rows--){
320                 for(x = 0; x < 4; x++){
321                         index = (4 * (self->rowSize + (blank_rows - 1))) + x;
322                         if (index == 10 || index == 15){
323                                 self->contigPtr[index] = 1.0f;
324                         }else{
325                                 self->contigPtr[index] = 0.0f;
326                         }
327                 }
328         }
329         for(x = 1; x <= self->rowSize; x++){
330                 first_row_elem = (self->colSize * (self->rowSize - x));
331                 curr_pos = (first_row_elem + (self->colSize -1));
332                 new_pos = (4 * (self->rowSize - x )) + (curr_pos - first_row_elem);
333                 for(blank_columns = (4 - self->colSize); blank_columns > 0; blank_columns--){
334                         self->contigPtr[new_pos + blank_columns] = 0.0f;
335                 }
336                 for(curr_pos = curr_pos; curr_pos >= first_row_elem; curr_pos--){
337                         self->contigPtr[new_pos] = self->contigPtr[curr_pos];
338                         new_pos--;
339                 }
340         }
341         self->rowSize = 4;
342         self->colSize = 4;
343         
344         Py_INCREF(self);
345         return (PyObject *)self;
346 }
347 /*---------------------------Matrix.translationPart() ------------*/
348 PyObject *Matrix_TranslationPart(MatrixObject * self)
349 {
350         float vec[4];
351         
352         if(!BaseMath_ReadCallback(self))
353                 return NULL;
354         
355         if(self->colSize < 3 || self->rowSize < 4){
356                 PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size");
357                 return NULL;
358         }
359
360         vec[0] = self->matrix[3][0];
361         vec[1] = self->matrix[3][1];
362         vec[2] = self->matrix[3][2];
363
364         return newVectorObject(vec, 3, Py_NEW, NULL);
365 }
366 /*---------------------------Matrix.rotationPart() ---------------*/
367 PyObject *Matrix_RotationPart(MatrixObject * self)
368 {
369         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
370                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
371
372         if(!BaseMath_ReadCallback(self))
373                 return NULL;
374
375         if(self->colSize < 3 || self->rowSize < 3){
376                 PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n");
377                 return NULL;
378         }
379
380         mat[0] = self->matrix[0][0];
381         mat[1] = self->matrix[0][1];
382         mat[2] = self->matrix[0][2];
383         mat[3] = self->matrix[1][0];
384         mat[4] = self->matrix[1][1];
385         mat[5] = self->matrix[1][2];
386         mat[6] = self->matrix[2][0];
387         mat[7] = self->matrix[2][1];
388         mat[8] = self->matrix[2][2];
389
390         return newMatrixObject(mat, 3, 3, Py_NEW, Py_TYPE(self));
391 }
392 /*---------------------------Matrix.scalePart() --------------------*/
393 PyObject *Matrix_scalePart(MatrixObject * self)
394 {
395         float scale[3], rot[3];
396         float mat[3][3], imat[3][3], tmat[3][3];
397
398         if(!BaseMath_ReadCallback(self))
399                 return NULL;
400         
401         /*must be 3-4 cols, 3-4 rows, square matrix*/
402         if(self->colSize == 4 && self->rowSize == 4)
403                 copy_m3_m4(mat, (float (*)[4])*self->matrix);
404         else if(self->colSize == 3 && self->rowSize == 3)
405                 copy_m3_m3(mat, (float (*)[3])*self->matrix);
406         else {
407                 PyErr_SetString(PyExc_AttributeError, "Matrix.scalePart(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
408                 return NULL;
409         }
410         /* functionality copied from editobject.c apply_obmat */
411         mat3_to_eul( rot,mat);
412         eul_to_mat3( tmat,rot);
413         invert_m3_m3(imat, tmat);
414         mul_m3_m3m3(tmat, imat, mat);
415         
416         scale[0]= tmat[0][0];
417         scale[1]= tmat[1][1];
418         scale[2]= tmat[2][2];
419         return newVectorObject(scale, 3, Py_NEW, NULL);
420 }
421 /*---------------------------Matrix.invert() ---------------------*/
422 PyObject *Matrix_Invert(MatrixObject * self)
423 {
424         
425         int x, y, z = 0;
426         float det = 0.0f;
427         PyObject *f = NULL;
428         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
429                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
430
431         if(!BaseMath_ReadCallback(self))
432                 return NULL;
433
434         if(self->rowSize != self->colSize){
435                 PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported");
436                 return NULL;
437         }
438
439         /*calculate the determinant*/
440         f = Matrix_Determinant(self);
441         det = (float)PyFloat_AS_DOUBLE(f); /*Increfs, so we need to decref*/
442         Py_DECREF(f);
443
444         if(det != 0) {
445                 /*calculate the classical adjoint*/
446                 if(self->rowSize == 2) {
447                         mat[0] = self->matrix[1][1];
448                         mat[1] = -self->matrix[0][1];
449                         mat[2] = -self->matrix[1][0];
450                         mat[3] = self->matrix[0][0];
451                 } else if(self->rowSize == 3) {
452                         adjoint_m3_m3((float (*)[3]) mat,(float (*)[3]) *self->matrix);
453                 } else if(self->rowSize == 4) {
454                         adjoint_m4_m4((float (*)[4]) mat, (float (*)[4]) *self->matrix);
455                 }
456                 /*divide by determinate*/
457                 for(x = 0; x < (self->rowSize * self->colSize); x++) {
458                         mat[x] /= det;
459                 }
460                 /*set values*/
461                 for(x = 0; x < self->rowSize; x++) {
462                         for(y = 0; y < self->colSize; y++) {
463                                 self->matrix[x][y] = mat[z];
464                                 z++;
465                         }
466                 }
467                 /*transpose
468                 Matrix_Transpose(self);*/
469         } else {
470                 PyErr_SetString(PyExc_ValueError, "matrix does not have an inverse");
471                 return NULL;
472         }
473         
474         BaseMath_WriteCallback(self);
475         Py_INCREF(self);
476         return (PyObject *)self;
477 }
478
479
480 /*---------------------------Matrix.determinant() ----------------*/
481 PyObject *Matrix_Determinant(MatrixObject * self)
482 {
483         float det = 0.0f;
484
485         if(!BaseMath_ReadCallback(self))
486                 return NULL;
487         
488         if(self->rowSize != self->colSize){
489                 PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported");
490                 return NULL;
491         }
492
493         if(self->rowSize == 2) {
494                 det = determinant_m2(self->matrix[0][0], self->matrix[0][1],
495                                          self->matrix[1][0], self->matrix[1][1]);
496         } else if(self->rowSize == 3) {
497                 det = determinant_m3(self->matrix[0][0], self->matrix[0][1],
498                                          self->matrix[0][2], self->matrix[1][0],
499                                          self->matrix[1][1], self->matrix[1][2],
500                                          self->matrix[2][0], self->matrix[2][1],
501                                          self->matrix[2][2]);
502         } else {
503                 det = determinant_m4((float (*)[4]) *self->matrix);
504         }
505
506         return PyFloat_FromDouble( (double) det );
507 }
508 /*---------------------------Matrix.transpose() ------------------*/
509 PyObject *Matrix_Transpose(MatrixObject * self)
510 {
511         float t = 0.0f;
512
513         if(!BaseMath_ReadCallback(self))
514                 return NULL;
515         
516         if(self->rowSize != self->colSize){
517                 PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported");
518                 return NULL;
519         }
520
521         if(self->rowSize == 2) {
522                 t = self->matrix[1][0];
523                 self->matrix[1][0] = self->matrix[0][1];
524                 self->matrix[0][1] = t;
525         } else if(self->rowSize == 3) {
526                 transpose_m3((float (*)[3])*self->matrix);
527         } else {
528                 transpose_m4((float (*)[4])*self->matrix);
529         }
530
531         BaseMath_WriteCallback(self);
532         Py_INCREF(self);
533         return (PyObject *)self;
534 }
535
536
537 /*---------------------------Matrix.zero() -----------------------*/
538 PyObject *Matrix_Zero(MatrixObject * self)
539 {
540         int row, col;
541         
542         for(row = 0; row < self->rowSize; row++) {
543                 for(col = 0; col < self->colSize; col++) {
544                         self->matrix[row][col] = 0.0f;
545                 }
546         }
547         
548         if(!BaseMath_WriteCallback(self))
549                 return NULL;
550         
551         Py_INCREF(self);
552         return (PyObject *)self;
553 }
554 /*---------------------------Matrix.identity(() ------------------*/
555 PyObject *Matrix_Identity(MatrixObject * self)
556 {
557         if(!BaseMath_ReadCallback(self))
558                 return NULL;
559         
560         if(self->rowSize != self->colSize){
561                 PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n");
562                 return NULL;
563         }
564
565         if(self->rowSize == 2) {
566                 self->matrix[0][0] = 1.0f;
567                 self->matrix[0][1] = 0.0f;
568                 self->matrix[1][0] = 0.0f;
569                 self->matrix[1][1] = 1.0f;
570         } else if(self->rowSize == 3) {
571                 unit_m3((float (*)[3]) *self->matrix);
572         } else {
573                 unit_m4((float (*)[4]) *self->matrix);
574         }
575
576         if(!BaseMath_WriteCallback(self))
577                 return NULL;
578         
579         Py_INCREF(self);
580         return (PyObject *)self;
581 }
582
583 /*---------------------------Matrix.inverted() ------------------*/
584 PyObject *Matrix_copy(MatrixObject * self)
585 {
586         if(!BaseMath_ReadCallback(self))
587                 return NULL;
588         
589         return (PyObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW, Py_TYPE(self));
590 }
591
592 /*----------------------------print object (internal)-------------*/
593 /*print the object to screen*/
594 static PyObject *Matrix_repr(MatrixObject * self)
595 {
596         int x, y;
597         char buffer[48], str[1024];
598
599         if(!BaseMath_ReadCallback(self))
600                 return NULL;
601         
602         BLI_strncpy(str,"",1024);
603         for(x = 0; x < self->colSize; x++){
604                 sprintf(buffer, "[");
605                 strcat(str,buffer);
606                 for(y = 0; y < (self->rowSize - 1); y++) {
607                         sprintf(buffer, "%.6f, ", self->matrix[y][x]);
608                         strcat(str,buffer);
609                 }
610                 if(x < (self->colSize-1)){
611                         sprintf(buffer, "%.6f](matrix [row %d])\n", self->matrix[y][x], x);
612                         strcat(str,buffer);
613                 }else{
614                         sprintf(buffer, "%.6f](matrix [row %d])", self->matrix[y][x], x);
615                         strcat(str,buffer);
616                 }
617         }
618
619         return PyUnicode_FromString(str);
620 }
621 /*------------------------tp_richcmpr*/
622 /*returns -1 execption, 0 false, 1 true*/
623 static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
624 {
625         MatrixObject *matA = NULL, *matB = NULL;
626         int result = 0;
627
628         if (!MatrixObject_Check(objectA) || !MatrixObject_Check(objectB)){
629                 if (comparison_type == Py_NE){
630                         Py_RETURN_TRUE;
631                 }else{
632                         Py_RETURN_FALSE;
633                 }
634         }
635         matA = (MatrixObject*)objectA;
636         matB = (MatrixObject*)objectB;
637
638         if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB))
639                 return NULL;
640         
641         if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
642                 if (comparison_type == Py_NE){
643                         Py_RETURN_TRUE;
644                 }else{
645                         Py_RETURN_FALSE;
646                 }
647         }
648
649         switch (comparison_type){
650                 case Py_EQ:
651                         /*contigPtr is basically a really long vector*/
652                         result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr,
653                                 (matA->rowSize * matA->colSize), 1);
654                         break;
655                 case Py_NE:
656                         result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr,
657                                 (matA->rowSize * matA->colSize), 1);
658                         if (result == 0){
659                                 result = 1;
660                         }else{
661                                 result = 0;
662                         }
663                         break;
664                 default:
665                         printf("The result of the comparison could not be evaluated");
666                         break;
667         }
668         if (result == 1){
669                 Py_RETURN_TRUE;
670         }else{
671                 Py_RETURN_FALSE;
672         }
673 }
674
675 /*---------------------SEQUENCE PROTOCOLS------------------------
676   ----------------------------len(object)------------------------
677   sequence length*/
678 static int Matrix_len(MatrixObject * self)
679 {
680         return (self->rowSize);
681 }
682 /*----------------------------object[]---------------------------
683   sequence accessor (get)
684   the wrapped vector gives direct access to the matrix data*/
685 static PyObject *Matrix_item(MatrixObject * self, int i)
686 {
687         if(!BaseMath_ReadCallback(self))
688                 return NULL;
689         
690         if(i < 0 || i >= self->rowSize) {
691                 PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range");
692                 return NULL;
693         }
694         return newVectorObject_cb((PyObject *)self, self->colSize, mathutils_matrix_vector_cb_index, i);
695 }
696 /*----------------------------object[]-------------------------
697   sequence accessor (set)*/
698 static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
699 {
700         int y, x, size = 0;
701         float vec[4];
702         PyObject *m, *f;
703
704         if(!BaseMath_ReadCallback(self))
705                 return -1;
706         
707         if(i >= self->rowSize || i < 0){
708                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad column\n");
709                 return -1;
710         }
711
712         if(PySequence_Check(ob)){
713                 size = PySequence_Length(ob);
714                 if(size != self->colSize){
715                         PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad sequence size\n");
716                         return -1;
717                 }
718                 for (x = 0; x < size; x++) {
719                         m = PySequence_GetItem(ob, x);
720                         if (m == NULL) { /*Failed to read sequence*/
721                                 PyErr_SetString(PyExc_RuntimeError, "matrix[attribute] = x: unable to read sequence\n");
722                                 return -1;
723                         }
724
725                         f = PyNumber_Float(m);
726                         if(f == NULL) { /*parsed item not a number*/
727                                 Py_DECREF(m);
728                                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: sequence argument not a number\n");
729                                 return -1;
730                         }
731
732                         vec[x] = (float)PyFloat_AS_DOUBLE(f);
733                         Py_DECREF(m);
734                         Py_DECREF(f);
735                 }
736                 /*parsed well - now set in matrix*/
737                 for(y = 0; y < size; y++){
738                         self->matrix[i][y] = vec[y];
739                 }
740                 
741                 BaseMath_WriteCallback(self);
742                 return 0;
743         }else{
744                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n");
745                 return -1;
746         }
747 }
748 /*----------------------------object[z:y]------------------------
749   sequence slice (get)*/
750 static PyObject *Matrix_slice(MatrixObject * self, int begin, int end)
751 {
752
753         PyObject *list = NULL;
754         int count;
755         
756         if(!BaseMath_ReadCallback(self))
757                 return NULL;
758
759         CLAMP(begin, 0, self->rowSize);
760         CLAMP(end, 0, self->rowSize);
761         begin = MIN2(begin,end);
762
763         list = PyList_New(end - begin);
764         for(count = begin; count < end; count++) {
765                 PyList_SetItem(list, count - begin,
766                                 newVectorObject_cb((PyObject *)self, self->colSize, mathutils_matrix_vector_cb_index, count));
767
768         }
769
770         return list;
771 }
772 /*----------------------------object[z:y]------------------------
773   sequence slice (set)*/
774 static int Matrix_ass_slice(MatrixObject * self, int begin, int end, PyObject * seq)
775 {
776         int i, x, y, size, sub_size = 0;
777         float mat[16], f;
778         PyObject *subseq;
779         PyObject *m;
780
781         if(!BaseMath_ReadCallback(self))
782                 return -1;
783         
784         CLAMP(begin, 0, self->rowSize);
785         CLAMP(end, 0, self->rowSize);
786         begin = MIN2(begin,end);
787
788         if(PySequence_Check(seq)){
789                 size = PySequence_Length(seq);
790                 if(size != (end - begin)){
791                         PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
792                         return -1;
793                 }
794                 /*parse sub items*/
795                 for (i = 0; i < size; i++) {
796                         /*parse each sub sequence*/
797                         subseq = PySequence_GetItem(seq, i);
798                         if (subseq == NULL) { /*Failed to read sequence*/
799                                 PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence");
800                                 return -1;
801                         }
802
803                         if(PySequence_Check(subseq)){
804                                 /*subsequence is also a sequence*/
805                                 sub_size = PySequence_Length(subseq);
806                                 if(sub_size != self->colSize){
807                                         Py_DECREF(subseq);
808                                         PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
809                                         return -1;
810                                 }
811                                 for (y = 0; y < sub_size; y++) {
812                                         m = PySequence_GetItem(subseq, y);
813                                         if (m == NULL) { /*Failed to read sequence*/
814                                                 Py_DECREF(subseq);
815                                                 PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence\n");
816                                                 return -1;
817                                         }
818                                         
819                                         f = PyFloat_AsDouble(m); /* faster to assume a float and raise an error after */
820                                         if(f == -1 && PyErr_Occurred()) { /*parsed item not a number*/
821                                                 Py_DECREF(m);
822                                                 Py_DECREF(subseq);
823                                                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: sequence argument not a number\n");
824                                                 return -1;
825                                         }
826
827                                         mat[(i * self->colSize) + y] = f;
828                                         Py_DECREF(m);
829                                 }
830                         }else{
831                                 Py_DECREF(subseq);
832                                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
833                                 return -1;
834                         }
835                         Py_DECREF(subseq);
836                 }
837                 /*parsed well - now set in matrix*/
838                 for(x = 0; x < (size * sub_size); x++){
839                         self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x];
840                 }
841                 
842                 BaseMath_WriteCallback(self);
843                 return 0;
844         }else{
845                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
846                 return -1;
847         }
848 }
849 /*------------------------NUMERIC PROTOCOLS----------------------
850   ------------------------obj + obj------------------------------*/
851 static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
852 {
853         int x, y;
854         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
855                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
856         MatrixObject *mat1 = NULL, *mat2 = NULL;
857
858         mat1 = (MatrixObject*)m1;
859         mat2 = (MatrixObject*)m2;
860
861         if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
862                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
863                 return NULL;
864         }
865         
866         if(!BaseMath_ReadCallback(mat1) || !BaseMath_ReadCallback(mat2))
867                 return NULL;
868         
869         if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
870                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
871                 return NULL;
872         }
873
874         for(x = 0; x < mat1->rowSize; x++) {
875                 for(y = 0; y < mat1->colSize; y++) {
876                         mat[((x * mat1->colSize) + y)] = mat1->matrix[x][y] + mat2->matrix[x][y];
877                 }
878         }
879
880         return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
881 }
882 /*------------------------obj - obj------------------------------
883   subtraction*/
884 static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
885 {
886         int x, y;
887         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
888                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
889         MatrixObject *mat1 = NULL, *mat2 = NULL;
890
891         mat1 = (MatrixObject*)m1;
892         mat2 = (MatrixObject*)m2;
893
894         if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
895                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
896                 return NULL;
897         }
898         
899         if(!BaseMath_ReadCallback(mat1) || !BaseMath_ReadCallback(mat2))
900                 return NULL;
901         
902         if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
903                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
904                 return NULL;
905         }
906
907         for(x = 0; x < mat1->rowSize; x++) {
908                 for(y = 0; y < mat1->colSize; y++) {
909                         mat[((x * mat1->colSize) + y)] = mat1->matrix[x][y] - mat2->matrix[x][y];
910                 }
911         }
912
913         return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
914 }
915 /*------------------------obj * obj------------------------------
916   mulplication*/
917 static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
918 {
919         int x, y, z;
920         float scalar;
921         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
922                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
923         double dot = 0.0f;
924         MatrixObject *mat1 = NULL, *mat2 = NULL;
925
926         if(MatrixObject_Check(m1)) {
927                 mat1 = (MatrixObject*)m1;
928                 if(!BaseMath_ReadCallback(mat1))
929                         return NULL;
930         }
931         if(MatrixObject_Check(m2)) {
932                 mat2 = (MatrixObject*)m2;
933                 if(!BaseMath_ReadCallback(mat2))
934                         return NULL;
935         }
936
937         if(mat1 && mat2) { /*MATRIX * MATRIX*/
938                 if(mat1->rowSize != mat2->colSize){
939                         PyErr_SetString(PyExc_AttributeError,"Matrix multiplication: matrix A rowsize must equal matrix B colsize");
940                         return NULL;
941                 }
942                 for(x = 0; x < mat2->rowSize; x++) {
943                         for(y = 0; y < mat1->colSize; y++) {
944                                 for(z = 0; z < mat1->rowSize; z++) {
945                                         dot += (mat1->matrix[z][y] * mat2->matrix[x][z]);
946                                 }
947                                 mat[((x * mat1->colSize) + y)] = (float)dot;
948                                 dot = 0.0f;
949                         }
950                 }
951                 
952                 return newMatrixObject(mat, mat2->rowSize, mat1->colSize, Py_NEW, NULL);
953         }
954         
955         if(mat1==NULL){
956                 scalar=PyFloat_AsDouble(m1); // may not be a float...
957                 if ((scalar == -1.0 && PyErr_Occurred())==0) { /*FLOAT/INT * MATRIX, this line annoys theeth, lets see if he finds it */
958                         for(x = 0; x < mat2->rowSize; x++) {
959                                 for(y = 0; y < mat2->colSize; y++) {
960                                         mat[((x * mat2->colSize) + y)] = scalar * mat2->matrix[x][y];
961                                 }
962                         }
963                         return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW, NULL);
964                 }
965                 
966                 PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
967                 return NULL;
968         }
969         else /* if(mat1) { */ {
970                 
971                 if(VectorObject_Check(m2)) { /* MATRIX*VECTOR */
972                         return column_vector_multiplication(mat1, (VectorObject *)m2); /* vector update done inside the function */
973                 }
974                 else {
975                         scalar= PyFloat_AsDouble(m2);
976                         if ((scalar == -1.0 && PyErr_Occurred())==0) { /* MATRIX*FLOAT/INT */
977                                 for(x = 0; x < mat1->rowSize; x++) {
978                                         for(y = 0; y < mat1->colSize; y++) {
979                                                 mat[((x * mat1->colSize) + y)] = scalar * mat1->matrix[x][y];
980                                         }
981                                 }
982                                 return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
983                         }
984                 }
985                 PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
986                 return NULL;
987         }
988
989         PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation\n");
990         return NULL;
991 }
992 static PyObject* Matrix_inv(MatrixObject *self)
993 {
994         if(!BaseMath_ReadCallback(self))
995                 return NULL;
996         
997         return Matrix_Invert(self);
998 }
999
1000 /*-----------------PROTOCOL DECLARATIONS--------------------------*/
1001 static PySequenceMethods Matrix_SeqMethods = {
1002         (lenfunc) Matrix_len,                                   /* sq_length */
1003         (binaryfunc) 0,                                                 /* sq_concat */
1004         (ssizeargfunc) 0,                                                       /* sq_repeat */
1005         (ssizeargfunc) Matrix_item,                             /* sq_item */
1006         (ssizessizeargfunc) Matrix_slice,                       /* sq_slice */
1007         (ssizeobjargproc) Matrix_ass_item,              /* sq_ass_item */
1008         (ssizessizeobjargproc) Matrix_ass_slice,        /* sq_ass_slice */
1009 };
1010
1011
1012 static PyObject *Matrix_subscript(MatrixObject* self, PyObject* item)
1013 {
1014         if (PyIndex_Check(item)) {
1015                 Py_ssize_t i;
1016                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1017                 if (i == -1 && PyErr_Occurred())
1018                         return NULL;
1019                 if (i < 0)
1020                         i += self->rowSize;
1021                 return Matrix_item(self, i);
1022         } else if (PySlice_Check(item)) {
1023                 Py_ssize_t start, stop, step, slicelength;
1024
1025                 if (PySlice_GetIndicesEx((PySliceObject*)item, self->rowSize, &start, &stop, &step, &slicelength) < 0)
1026                         return NULL;
1027
1028                 if (slicelength <= 0) {
1029                         return PyList_New(0);
1030                 }
1031                 else if (step == 1) {
1032                         return Matrix_slice(self, start, stop);
1033                 }
1034                 else {
1035                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with matricies");
1036                         return NULL;
1037                 }
1038         }
1039         else {
1040                 PyErr_Format(PyExc_TypeError,
1041                              "vector indices must be integers, not %.200s",
1042                              item->ob_type->tp_name);
1043                 return NULL;
1044         }
1045 }
1046
1047 static int Matrix_ass_subscript(MatrixObject* self, PyObject* item, PyObject* value)
1048 {
1049         if (PyIndex_Check(item)) {
1050                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1051                 if (i == -1 && PyErr_Occurred())
1052                         return -1;
1053                 if (i < 0)
1054                         i += self->rowSize;
1055                 return Matrix_ass_item(self, i, value);
1056         }
1057         else if (PySlice_Check(item)) {
1058                 Py_ssize_t start, stop, step, slicelength;
1059
1060                 if (PySlice_GetIndicesEx((PySliceObject*)item, self->rowSize, &start, &stop, &step, &slicelength) < 0)
1061                         return -1;
1062
1063                 if (step == 1)
1064                         return Matrix_ass_slice(self, start, stop, value);
1065                 else {
1066                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with matricies");
1067                         return -1;
1068                 }
1069         }
1070         else {
1071                 PyErr_Format(PyExc_TypeError,
1072                              "matrix indices must be integers, not %.200s",
1073                              item->ob_type->tp_name);
1074                 return -1;
1075         }
1076 }
1077
1078 static PyMappingMethods Matrix_AsMapping = {
1079         (lenfunc)Matrix_len,
1080         (binaryfunc)Matrix_subscript,
1081         (objobjargproc)Matrix_ass_subscript
1082 };
1083
1084
1085 static PyNumberMethods Matrix_NumMethods = {
1086                 (binaryfunc)    Matrix_add,     /*nb_add*/
1087                 (binaryfunc)    Matrix_sub,     /*nb_subtract*/
1088                 (binaryfunc)    Matrix_mul,     /*nb_multiply*/
1089                 0,                                                      /*nb_remainder*/
1090                 0,                                                      /*nb_divmod*/
1091                 0,                                                      /*nb_power*/
1092                 (unaryfunc)     0,      /*nb_negative*/
1093                 (unaryfunc)     0,      /*tp_positive*/
1094                 (unaryfunc)     0,      /*tp_absolute*/
1095                 (inquiry)       0,      /*tp_bool*/
1096                 (unaryfunc)     Matrix_inv,     /*nb_invert*/
1097                 0,                              /*nb_lshift*/
1098                 (binaryfunc)0,  /*nb_rshift*/
1099                 0,                              /*nb_and*/
1100                 0,                              /*nb_xor*/
1101                 0,                              /*nb_or*/
1102                 0,                              /*nb_int*/
1103                 0,                              /*nb_reserved*/
1104                 0,                              /*nb_float*/
1105                 0,                              /* nb_inplace_add */
1106                 0,                              /* nb_inplace_subtract */
1107                 0,                              /* nb_inplace_multiply */
1108                 0,                              /* nb_inplace_remainder */
1109                 0,                              /* nb_inplace_power */
1110                 0,                              /* nb_inplace_lshift */
1111                 0,                              /* nb_inplace_rshift */
1112                 0,                              /* nb_inplace_and */
1113                 0,                              /* nb_inplace_xor */
1114                 0,                              /* nb_inplace_or */
1115                 0,                              /* nb_floor_divide */
1116                 0,                              /* nb_true_divide */
1117                 0,                              /* nb_inplace_floor_divide */
1118                 0,                              /* nb_inplace_true_divide */
1119                 0,                              /* nb_index */
1120 };
1121
1122 static PyObject *Matrix_getRowSize( MatrixObject * self, void *type )
1123 {
1124         return PyLong_FromLong((long) self->rowSize);
1125 }
1126
1127 static PyObject *Matrix_getColSize( MatrixObject * self, void *type )
1128 {
1129         return PyLong_FromLong((long) self->colSize);
1130 }
1131
1132 /*****************************************************************************/
1133 /* Python attributes get/set structure:                                      */
1134 /*****************************************************************************/
1135 static PyGetSetDef Matrix_getseters[] = {
1136         {"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL},
1137         {"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL},
1138         {"wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, "", NULL},
1139         {"__owner__",(getter)BaseMathObject_getOwner, (setter)NULL, "",
1140          NULL},
1141         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
1142 };
1143
1144 /*------------------PY_OBECT DEFINITION--------------------------*/
1145 PyTypeObject matrix_Type = {
1146         PyVarObject_HEAD_INIT(NULL, 0)
1147         "matrix",                                               /*tp_name*/
1148         sizeof(MatrixObject),                   /*tp_basicsize*/
1149         0,                                                              /*tp_itemsize*/
1150         (destructor)BaseMathObject_dealloc,             /*tp_dealloc*/
1151         0,                                                              /*tp_print*/
1152         0,                                                              /*tp_getattr*/
1153         0,                                                              /*tp_setattr*/
1154         0,                                                              /*tp_compare*/
1155         (reprfunc) Matrix_repr,                 /*tp_repr*/
1156         &Matrix_NumMethods,                             /*tp_as_number*/
1157         &Matrix_SeqMethods,                             /*tp_as_sequence*/
1158         &Matrix_AsMapping,                              /*tp_as_mapping*/
1159         0,                                                              /*tp_hash*/
1160         0,                                                              /*tp_call*/
1161         0,                                                              /*tp_str*/
1162         0,                                                              /*tp_getattro*/
1163         0,                                                              /*tp_setattro*/
1164         0,                                                              /*tp_as_buffer*/
1165         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1166         0,                                                              /*tp_doc*/
1167         0,                                                              /*tp_traverse*/
1168         0,                                                              /*tp_clear*/
1169         (richcmpfunc)Matrix_richcmpr,   /*tp_richcompare*/
1170         0,                                                              /*tp_weaklistoffset*/
1171         0,                                                              /*tp_iter*/
1172         0,                                                              /*tp_iternext*/
1173         Matrix_methods,                                 /*tp_methods*/
1174         0,                                                              /*tp_members*/
1175         Matrix_getseters,                               /*tp_getset*/
1176         0,                                                              /*tp_base*/
1177         0,                                                              /*tp_dict*/
1178         0,                                                              /*tp_descr_get*/
1179         0,                                                              /*tp_descr_set*/
1180         0,                                                              /*tp_dictoffset*/
1181         0,                                                              /*tp_init*/
1182         0,                                                              /*tp_alloc*/
1183         Matrix_new,                                             /*tp_new*/
1184         0,                                                              /*tp_free*/
1185         0,                                                              /*tp_is_gc*/
1186         0,                                                              /*tp_bases*/
1187         0,                                                              /*tp_mro*/
1188         0,                                                              /*tp_cache*/
1189         0,                                                              /*tp_subclasses*/
1190         0,                                                              /*tp_weaklist*/
1191         0                                                               /*tp_del*/
1192 };
1193
1194 /*------------------------newMatrixObject (internal)-------------
1195 creates a new matrix object
1196 self->matrix     self->contiguous_ptr (reference to data.xxx)
1197        [0]------------->[0]
1198                         [1]
1199                         [2]
1200        [1]------------->[3]
1201                         [4]
1202                         [5]
1203                      ....
1204 self->matrix[1][1] = self->contigPtr[4] */
1205
1206 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
1207  (i.e. it was allocated elsewhere by MEM_mallocN())
1208   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
1209  (i.e. it must be created here with PyMEM_malloc())*/
1210 PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type, PyTypeObject *base_type)
1211 {
1212         MatrixObject *self;
1213         int x, row, col;
1214
1215         /*matrix objects can be any 2-4row x 2-4col matrix*/
1216         if(rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4){
1217                 PyErr_SetString(PyExc_RuntimeError, "matrix(): row and column sizes must be between 2 and 4");
1218                 return NULL;
1219         }
1220
1221         if(base_type)   self = (MatrixObject *)base_type->tp_alloc(base_type, 0);
1222         else                    self = PyObject_NEW(MatrixObject, &matrix_Type);
1223
1224         self->rowSize = rowSize;
1225         self->colSize = colSize;
1226         
1227         /* init callbacks as NULL */
1228         self->cb_user= NULL;
1229         self->cb_type= self->cb_subtype= 0;
1230
1231         if(type == Py_WRAP){
1232                 self->contigPtr = mat;
1233                 /*create pointer array*/
1234                 self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
1235                 if(self->matrix == NULL) { /*allocation failure*/
1236                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
1237                         return NULL;
1238                 }
1239                 /*pointer array points to contigous memory*/
1240                 for(x = 0; x < rowSize; x++) {
1241                         self->matrix[x] = self->contigPtr + (x * colSize);
1242                 }
1243                 self->wrapped = Py_WRAP;
1244         }else if (type == Py_NEW){
1245                 self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float));
1246                 if(self->contigPtr == NULL) { /*allocation failure*/
1247                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n");
1248                         return NULL;
1249                 }
1250                 /*create pointer array*/
1251                 self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
1252                 if(self->matrix == NULL) { /*allocation failure*/
1253                         PyMem_Free(self->contigPtr);
1254                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
1255                         return NULL;
1256                 }
1257                 /*pointer array points to contigous memory*/
1258                 for(x = 0; x < rowSize; x++) {
1259                         self->matrix[x] = self->contigPtr + (x * colSize);
1260                 }
1261                 /*parse*/
1262                 if(mat) {       /*if a float array passed*/
1263                         for(row = 0; row < rowSize; row++) {
1264                                 for(col = 0; col < colSize; col++) {
1265                                         self->matrix[row][col] = mat[(row * colSize) + col];
1266                                 }
1267                         }
1268                 } else if (rowSize == colSize ) { /*or if no arguments are passed return identity matrix for square matrices */
1269                         Matrix_Identity(self);
1270                         Py_DECREF(self);
1271                 }
1272                 self->wrapped = Py_NEW;
1273         }else{ /*bad type*/
1274                 return NULL;
1275         }
1276         return (PyObject *) self;
1277 }
1278
1279 PyObject *newMatrixObject_cb(PyObject *cb_user, int rowSize, int colSize, int cb_type, int cb_subtype)
1280 {
1281         MatrixObject *self= (MatrixObject *)newMatrixObject(NULL, rowSize, colSize, Py_NEW, NULL);
1282         if(self) {
1283                 Py_INCREF(cb_user);
1284                 self->cb_user=                  cb_user;
1285                 self->cb_type=                  (unsigned char)cb_type;
1286                 self->cb_subtype=               (unsigned char)cb_subtype;
1287         }
1288         return (PyObject *) self;
1289 }
1290
1291 //----------------column_vector_multiplication (internal)---------
1292 //COLUMN VECTOR Multiplication (Matrix X Vector)
1293 // [1][4][7]   [a]
1294 // [2][5][8] * [b]
1295 // [3][6][9]   [c]
1296 //vector/matrix multiplication IS NOT COMMUTATIVE!!!!
1297 static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec)
1298 {
1299         float vecNew[4], vecCopy[4];
1300         double dot = 0.0f;
1301         int x, y, z = 0;
1302
1303         if(!BaseMath_ReadCallback(mat) || !BaseMath_ReadCallback(vec))
1304                 return NULL;
1305         
1306         if(mat->rowSize != vec->size){
1307                 if(mat->rowSize == 4 && vec->size != 3){
1308                         PyErr_SetString(PyExc_AttributeError, "matrix * vector: matrix row size and vector size must be the same");
1309                         return NULL;
1310                 }else{
1311                         vecCopy[3] = 1.0f;
1312                 }
1313         }
1314
1315         for(x = 0; x < vec->size; x++){
1316                 vecCopy[x] = vec->vec[x];
1317         }
1318         vecNew[3] = 1.0f;
1319
1320         for(x = 0; x < mat->colSize; x++) {
1321                 for(y = 0; y < mat->rowSize; y++) {
1322                         dot += mat->matrix[y][x] * vecCopy[y];
1323                 }
1324                 vecNew[z++] = (float)dot;
1325                 dot = 0.0f;
1326         }
1327         return newVectorObject(vecNew, vec->size, Py_NEW, NULL);
1328 }