removing files that should not be in blender2.5, added (by mistake?) r19226
[blender.git] / source / blender / python / generic / matrix.c
1 /*
2  * $Id: matrix.c 20249 2009-05-18 04:27:48Z campbellbarton $
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_arithb.h"
32 #include "BLI_blenlib.h"
33
34 static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec); /* utility func */
35
36 /*-------------------------DOC STRINGS ---------------------------*/
37 static char Matrix_Zero_doc[] = "() - set all values in the matrix to 0";
38 static char Matrix_Identity_doc[] = "() - set the square matrix to it's identity matrix";
39 static char Matrix_Transpose_doc[] = "() - set the matrix to it's transpose";
40 static char Matrix_Determinant_doc[] = "() - return the determinant of the matrix";
41 static char Matrix_Invert_doc[] =  "() - set the matrix to it's inverse if an inverse is possible";
42 static char Matrix_TranslationPart_doc[] = "() - return a vector encompassing the translation of the matrix";
43 static char Matrix_RotationPart_doc[] = "() - return a vector encompassing the rotation of the matrix";
44 static char Matrix_scalePart_doc[] = "() - convert matrix to a 3D vector";
45 static char Matrix_Resize4x4_doc[] = "() - resize the matrix to a 4x4 square matrix";
46 static char Matrix_toEuler_doc[] = "(eul_compat) - convert matrix to a euler angle rotation, optional euler argument that the new euler will be made compatible with.";
47 static char Matrix_toQuat_doc[] = "() - convert matrix to a quaternion rotation";
48 static char Matrix_copy_doc[] = "() - return a copy of the matrix";
49
50 static PyObject *Matrix_Zero( MatrixObject * self );
51 static PyObject *Matrix_Identity( MatrixObject * self );
52 static PyObject *Matrix_Transpose( MatrixObject * self );
53 static PyObject *Matrix_Determinant( MatrixObject * self );
54 static PyObject *Matrix_Invert( MatrixObject * self );
55 static PyObject *Matrix_TranslationPart( MatrixObject * self );
56 static PyObject *Matrix_RotationPart( MatrixObject * self );
57 static PyObject *Matrix_scalePart( MatrixObject * self );
58 static PyObject *Matrix_Resize4x4( MatrixObject * self );
59 static PyObject *Matrix_toEuler( MatrixObject * self, PyObject *args );
60 static PyObject *Matrix_toQuat( MatrixObject * self );
61 static PyObject *Matrix_copy( MatrixObject * self );
62
63 /*-----------------------METHOD DEFINITIONS ----------------------*/
64 static struct PyMethodDef Matrix_methods[] = {
65         {"zero", (PyCFunction) Matrix_Zero, METH_NOARGS, Matrix_Zero_doc},
66         {"identity", (PyCFunction) Matrix_Identity, METH_NOARGS, Matrix_Identity_doc},
67         {"transpose", (PyCFunction) Matrix_Transpose, METH_NOARGS, Matrix_Transpose_doc},
68         {"determinant", (PyCFunction) Matrix_Determinant, METH_NOARGS, Matrix_Determinant_doc},
69         {"invert", (PyCFunction) Matrix_Invert, METH_NOARGS, Matrix_Invert_doc},
70         {"translationPart", (PyCFunction) Matrix_TranslationPart, METH_NOARGS, Matrix_TranslationPart_doc},
71         {"rotationPart", (PyCFunction) Matrix_RotationPart, METH_NOARGS, Matrix_RotationPart_doc},
72         {"scalePart", (PyCFunction) Matrix_scalePart, METH_NOARGS, Matrix_scalePart_doc},
73         {"resize4x4", (PyCFunction) Matrix_Resize4x4, METH_NOARGS, Matrix_Resize4x4_doc},
74         {"toEuler", (PyCFunction) Matrix_toEuler, METH_VARARGS, Matrix_toEuler_doc},
75         {"toQuat", (PyCFunction) Matrix_toQuat, METH_NOARGS, Matrix_toQuat_doc},
76         {"copy", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc},
77         {"__copy__", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc},
78         {NULL, NULL, 0, NULL}
79 };
80
81 //----------------------------------Mathutils.Matrix() -----------------
82 //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc.
83 //create a new matrix type
84 static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85 {
86         PyObject *argObject, *m, *s;
87         MatrixObject *mat;
88         int argSize, seqSize = 0, i, j;
89         float matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
90                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
91         float scalar;
92
93         argSize = PyTuple_GET_SIZE(args);
94         if(argSize > 4){        //bad arg nums
95                 PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
96                 return NULL;
97         } else if (argSize == 0) { //return empty 4D matrix
98                 return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW);
99         }else if (argSize == 1){
100                 //copy constructor for matrix objects
101                 argObject = PyTuple_GET_ITEM(args, 0);
102                 if(MatrixObject_Check(argObject)){
103                         mat = (MatrixObject*)argObject;
104
105                         argSize = mat->rowSize; //rows
106                         seqSize = mat->colSize; //col
107                         for(i = 0; i < (seqSize * argSize); i++){
108                                 matrix[i] = mat->contigPtr[i];
109                         }
110                 }
111         }else{ //2-4 arguments (all seqs? all same size?)
112                 for(i =0; i < argSize; i++){
113                         argObject = PyTuple_GET_ITEM(args, i);
114                         if (PySequence_Check(argObject)) { //seq?
115                                 if(seqSize){ //0 at first
116                                         if(PySequence_Length(argObject) != seqSize){ //seq size not same
117                                                 PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
118                                                 return NULL;
119                                         }
120                                 }
121                                 seqSize = PySequence_Length(argObject);
122                         }else{ //arg not a sequence
123                                 PyErr_SetString(PyExc_TypeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
124                                 return NULL;
125                         }
126                 }
127                 //all is well... let's continue parsing
128                 for (i = 0; i < argSize; i++){
129                         m = PyTuple_GET_ITEM(args, i);
130                         if (m == NULL) { // Failed to read sequence
131                                 PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n");
132                                 return NULL;
133                         }
134
135                         for (j = 0; j < seqSize; j++) {
136                                 s = PySequence_GetItem(m, j);
137                                 if (s == NULL) { // Failed to read sequence
138                                         PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n");
139                                         return NULL;
140                                 }
141                                 
142                                 scalar= (float)PyFloat_AsDouble(s);
143                                 Py_DECREF(s);
144                                 
145                                 if(scalar==-1 && PyErr_Occurred()) { // parsed item is not a number
146                                         PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
147                                         return NULL;
148                                 }
149
150                                 matrix[(seqSize*i)+j]= scalar;
151                         }
152                 }
153         }
154         return newMatrixObject(matrix, argSize, seqSize, Py_NEW);
155 }
156
157 /*-----------------------------METHODS----------------------------*/
158 /*---------------------------Matrix.toQuat() ---------------------*/
159 static PyObject *Matrix_toQuat(MatrixObject * self)
160 {
161         float quat[4];
162
163         /*must be 3-4 cols, 3-4 rows, square matrix*/
164         if(self->colSize < 3 || self->rowSize < 3 || (self->colSize != self->rowSize)) {
165                 PyErr_SetString(PyExc_AttributeError, "Matrix.toQuat(): inappropriate matrix size - expects 3x3 or 4x4 matrix");
166                 return NULL;
167         } 
168         if(self->colSize == 3){
169         Mat3ToQuat((float (*)[3])*self->matrix, quat);
170         }else{
171                 Mat4ToQuat((float (*)[4])*self->matrix, quat);
172         }
173         
174         return newQuaternionObject(quat, Py_NEW);
175 }
176 /*---------------------------Matrix.toEuler() --------------------*/
177 PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
178 {
179         float eul[3], eul_compatf[3];
180         EulerObject *eul_compat = NULL;
181         int x;
182         
183         if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat))
184                 return NULL;
185         
186         if(eul_compat) {
187                 for(x = 0; x < 3; x++) {
188                         eul_compatf[x] = eul_compat->eul[x] * ((float)Py_PI / 180);
189                 }
190         }
191         
192         /*must be 3-4 cols, 3-4 rows, square matrix*/
193         if(self->colSize ==3 && self->rowSize ==3) {
194                 if(eul_compat)  Mat3ToCompatibleEul((float (*)[3])*self->matrix, eul, eul_compatf);
195                 else                    Mat3ToEul((float (*)[3])*self->matrix, eul);
196         }else if (self->colSize ==4 && self->rowSize ==4) {
197                 float tempmat3[3][3];
198                 Mat3CpyMat4(tempmat3, (float (*)[4])*self->matrix);
199                 Mat3ToEul(tempmat3, eul);
200                 if(eul_compat)  Mat3ToCompatibleEul(tempmat3, eul, eul_compatf);
201                 else                    Mat3ToEul(tempmat3, eul);
202                 
203         }else {
204                 PyErr_SetString(PyExc_AttributeError, "Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
205                 return NULL;
206         }
207         /*have to convert to degrees*/
208         for(x = 0; x < 3; x++) {
209                 eul[x] *= (float) (180 / Py_PI);
210         }
211         return newEulerObject(eul, Py_NEW);
212 }
213 /*---------------------------Matrix.resize4x4() ------------------*/
214 PyObject *Matrix_Resize4x4(MatrixObject * self)
215 {
216         int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
217
218         if(self->data.blend_data){
219                 PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - only python matrices");
220                 return NULL;
221         }
222
223         self->data.py_data = PyMem_Realloc(self->data.py_data, (sizeof(float) * 16));
224         if(self->data.py_data == NULL) {
225                 PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
226                 return NULL;
227         }
228         self->contigPtr = self->data.py_data;  /*force*/
229         self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4));
230         if(self->matrix == NULL) {
231                 PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
232                 return NULL;
233         }
234         /*set row pointers*/
235         for(x = 0; x < 4; x++) {
236                 self->matrix[x] = self->contigPtr + (x * 4);
237         }
238         /*move data to new spot in array + clean*/
239         for(blank_rows = (4 - self->rowSize); blank_rows > 0; blank_rows--){
240                 for(x = 0; x < 4; x++){
241                         index = (4 * (self->rowSize + (blank_rows - 1))) + x;
242                         if (index == 10 || index == 15){
243                                 self->contigPtr[index] = 1.0f;
244                         }else{
245                                 self->contigPtr[index] = 0.0f;
246                         }
247                 }
248         }
249         for(x = 1; x <= self->rowSize; x++){
250                 first_row_elem = (self->colSize * (self->rowSize - x));
251                 curr_pos = (first_row_elem + (self->colSize -1));
252                 new_pos = (4 * (self->rowSize - x )) + (curr_pos - first_row_elem);
253                 for(blank_columns = (4 - self->colSize); blank_columns > 0; blank_columns--){
254                         self->contigPtr[new_pos + blank_columns] = 0.0f;
255                 }
256                 for(curr_pos = curr_pos; curr_pos >= first_row_elem; curr_pos--){
257                         self->contigPtr[new_pos] = self->contigPtr[curr_pos];
258                         new_pos--;
259                 }
260         }
261         self->rowSize = 4;
262         self->colSize = 4;
263         
264         Py_INCREF(self);
265         return (PyObject *)self;
266 }
267 /*---------------------------Matrix.translationPart() ------------*/
268 PyObject *Matrix_TranslationPart(MatrixObject * self)
269 {
270         float vec[4];
271
272         if(self->colSize < 3 || self->rowSize < 4){
273                 PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size");
274                 return NULL;
275         }
276
277         vec[0] = self->matrix[3][0];
278         vec[1] = self->matrix[3][1];
279         vec[2] = self->matrix[3][2];
280
281         return newVectorObject(vec, 3, Py_NEW);
282 }
283 /*---------------------------Matrix.rotationPart() ---------------*/
284 PyObject *Matrix_RotationPart(MatrixObject * self)
285 {
286         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
287                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
288
289         if(self->colSize < 3 || self->rowSize < 3){
290                 PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n");
291                 return NULL;
292         }
293
294         mat[0] = self->matrix[0][0];
295         mat[1] = self->matrix[0][1];
296         mat[2] = self->matrix[0][2];
297         mat[3] = self->matrix[1][0];
298         mat[4] = self->matrix[1][1];
299         mat[5] = self->matrix[1][2];
300         mat[6] = self->matrix[2][0];
301         mat[7] = self->matrix[2][1];
302         mat[8] = self->matrix[2][2];
303
304         return newMatrixObject(mat, 3, 3, Py_NEW);
305 }
306 /*---------------------------Matrix.scalePart() --------------------*/
307 PyObject *Matrix_scalePart(MatrixObject * self)
308 {
309         float scale[3], rot[3];
310         float mat[3][3], imat[3][3], tmat[3][3];
311
312         /*must be 3-4 cols, 3-4 rows, square matrix*/
313         if(self->colSize == 4 && self->rowSize == 4)
314                 Mat3CpyMat4(mat, (float (*)[4])*self->matrix);
315         else if(self->colSize == 3 && self->rowSize == 3)
316                 Mat3CpyMat3(mat, (float (*)[3])*self->matrix);
317         else {
318                 PyErr_SetString(PyExc_AttributeError, "Matrix.scalePart(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
319                 return NULL;
320         }
321         /* functionality copied from editobject.c apply_obmat */
322         Mat3ToEul(mat, rot);
323         EulToMat3(rot, tmat);
324         Mat3Inv(imat, tmat);
325         Mat3MulMat3(tmat, imat, mat);
326         
327         scale[0]= tmat[0][0];
328         scale[1]= tmat[1][1];
329         scale[2]= tmat[2][2];
330         return newVectorObject(scale, 3, Py_NEW);
331 }
332 /*---------------------------Matrix.invert() ---------------------*/
333 PyObject *Matrix_Invert(MatrixObject * self)
334 {
335         
336         int x, y, z = 0;
337         float det = 0.0f;
338         PyObject *f = NULL;
339         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
340                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
341
342         if(self->rowSize != self->colSize){
343                 PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported");
344                 return NULL;
345         }
346
347         /*calculate the determinant*/
348         f = Matrix_Determinant(self);
349         det = (float)PyFloat_AS_DOUBLE(f); /*Increfs, so we need to decref*/
350         Py_DECREF(f);
351
352         if(det != 0) {
353                 /*calculate the classical adjoint*/
354                 if(self->rowSize == 2) {
355                         mat[0] = self->matrix[1][1];
356                         mat[1] = -self->matrix[0][1];
357                         mat[2] = -self->matrix[1][0];
358                         mat[3] = self->matrix[0][0];
359                 } else if(self->rowSize == 3) {
360                         Mat3Adj((float (*)[3]) mat,(float (*)[3]) *self->matrix);
361                 } else if(self->rowSize == 4) {
362                         Mat4Adj((float (*)[4]) mat, (float (*)[4]) *self->matrix);
363                 }
364                 /*divide by determinate*/
365                 for(x = 0; x < (self->rowSize * self->colSize); x++) {
366                         mat[x] /= det;
367                 }
368                 /*set values*/
369                 for(x = 0; x < self->rowSize; x++) {
370                         for(y = 0; y < self->colSize; y++) {
371                                 self->matrix[x][y] = mat[z];
372                                 z++;
373                         }
374                 }
375                 /*transpose
376                 Matrix_Transpose(self);*/
377         } else {
378                 PyErr_SetString(PyExc_ValueError, "matrix does not have an inverse");
379                 return NULL;
380         }
381         
382         Py_INCREF(self);
383         return (PyObject *)self;
384 }
385
386
387 /*---------------------------Matrix.determinant() ----------------*/
388 PyObject *Matrix_Determinant(MatrixObject * self)
389 {
390         float det = 0.0f;
391
392         if(self->rowSize != self->colSize){
393                 PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported");
394                 return NULL;
395         }
396
397         if(self->rowSize == 2) {
398                 det = Det2x2(self->matrix[0][0], self->matrix[0][1],
399                                          self->matrix[1][0], self->matrix[1][1]);
400         } else if(self->rowSize == 3) {
401                 det = Det3x3(self->matrix[0][0], self->matrix[0][1],
402                                          self->matrix[0][2], self->matrix[1][0],
403                                          self->matrix[1][1], self->matrix[1][2],
404                                          self->matrix[2][0], self->matrix[2][1],
405                                          self->matrix[2][2]);
406         } else {
407                 det = Det4x4((float (*)[4]) *self->matrix);
408         }
409
410         return PyFloat_FromDouble( (double) det );
411 }
412 /*---------------------------Matrix.transpose() ------------------*/
413 PyObject *Matrix_Transpose(MatrixObject * self)
414 {
415         float t = 0.0f;
416
417         if(self->rowSize != self->colSize){
418                 PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported");
419                 return NULL;
420         }
421
422         if(self->rowSize == 2) {
423                 t = self->matrix[1][0];
424                 self->matrix[1][0] = self->matrix[0][1];
425                 self->matrix[0][1] = t;
426         } else if(self->rowSize == 3) {
427                 Mat3Transp((float (*)[3])*self->matrix);
428         } else {
429                 Mat4Transp((float (*)[4])*self->matrix);
430         }
431
432         Py_INCREF(self);
433         return (PyObject *)self;
434 }
435
436
437 /*---------------------------Matrix.zero() -----------------------*/
438 PyObject *Matrix_Zero(MatrixObject * self)
439 {
440         int row, col;
441
442         for(row = 0; row < self->rowSize; row++) {
443                 for(col = 0; col < self->colSize; col++) {
444                         self->matrix[row][col] = 0.0f;
445                 }
446         }
447         Py_INCREF(self);
448         return (PyObject *)self;
449 }
450 /*---------------------------Matrix.identity(() ------------------*/
451 PyObject *Matrix_Identity(MatrixObject * self)
452 {
453         if(self->rowSize != self->colSize){
454                 PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n");
455                 return NULL;
456         }
457
458         if(self->rowSize == 2) {
459                 self->matrix[0][0] = 1.0f;
460                 self->matrix[0][1] = 0.0f;
461                 self->matrix[1][0] = 0.0f;
462                 self->matrix[1][1] = 1.0f;
463         } else if(self->rowSize == 3) {
464                 Mat3One((float (*)[3]) *self->matrix);
465         } else {
466                 Mat4One((float (*)[4]) *self->matrix);
467         }
468
469         Py_INCREF(self);
470         return (PyObject *)self;
471 }
472
473 /*---------------------------Matrix.inverted() ------------------*/
474 PyObject *Matrix_copy(MatrixObject * self)
475 {
476         return (PyObject*)(MatrixObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW);
477 }
478
479 /*----------------------------dealloc()(internal) ----------------*/
480 /*free the py_object*/
481 static void Matrix_dealloc(MatrixObject * self)
482 {
483         PyMem_Free(self->matrix);
484         /*only free py_data*/
485         if(self->data.py_data){
486                 PyMem_Free(self->data.py_data);
487         }
488         PyObject_DEL(self);
489 }
490
491 /*----------------------------print object (internal)-------------*/
492 /*print the object to screen*/
493 static PyObject *Matrix_repr(MatrixObject * self)
494 {
495         int x, y;
496         char buffer[48], str[1024];
497
498         BLI_strncpy(str,"",1024);
499         for(x = 0; x < self->rowSize; x++){
500                 sprintf(buffer, "[");
501                 strcat(str,buffer);
502                 for(y = 0; y < (self->colSize - 1); y++) {
503                         sprintf(buffer, "%.6f, ", self->matrix[x][y]);
504                         strcat(str,buffer);
505                 }
506                 if(x < (self->rowSize-1)){
507                         sprintf(buffer, "%.6f](matrix [row %d])\n", self->matrix[x][y], x);
508                         strcat(str,buffer);
509                 }else{
510                         sprintf(buffer, "%.6f](matrix [row %d])", self->matrix[x][y], x);
511                         strcat(str,buffer);
512                 }
513         }
514
515         return PyUnicode_FromString(str);
516 }
517 /*------------------------tp_richcmpr*/
518 /*returns -1 execption, 0 false, 1 true*/
519 static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
520 {
521         MatrixObject *matA = NULL, *matB = NULL;
522         int result = 0;
523
524         if (!MatrixObject_Check(objectA) || !MatrixObject_Check(objectB)){
525                 if (comparison_type == Py_NE){
526                         Py_RETURN_TRUE;
527                 }else{
528                         Py_RETURN_FALSE;
529                 }
530         }
531         matA = (MatrixObject*)objectA;
532         matB = (MatrixObject*)objectB;
533
534         if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
535                 if (comparison_type == Py_NE){
536                         Py_RETURN_TRUE;
537                 }else{
538                         Py_RETURN_FALSE;
539                 }
540         }
541
542         switch (comparison_type){
543                 case Py_EQ:
544                         /*contigPtr is basically a really long vector*/
545                         result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr,
546                                 (matA->rowSize * matA->colSize), 1);
547                         break;
548                 case Py_NE:
549                         result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr,
550                                 (matA->rowSize * matA->colSize), 1);
551                         if (result == 0){
552                                 result = 1;
553                         }else{
554                                 result = 0;
555                         }
556                         break;
557                 default:
558                         printf("The result of the comparison could not be evaluated");
559                         break;
560         }
561         if (result == 1){
562                 Py_RETURN_TRUE;
563         }else{
564                 Py_RETURN_FALSE;
565         }
566 }
567 /*------------------------tp_doc*/
568 static char MatrixObject_doc[] = "This is a wrapper for matrix objects.";
569 /*---------------------SEQUENCE PROTOCOLS------------------------
570   ----------------------------len(object)------------------------
571   sequence length*/
572 static int Matrix_len(MatrixObject * self)
573 {
574         return (self->rowSize);
575 }
576 /*----------------------------object[]---------------------------
577   sequence accessor (get)
578   the wrapped vector gives direct access to the matrix data*/
579 static PyObject *Matrix_item(MatrixObject * self, int i)
580 {
581         if(i < 0 || i >= self->rowSize) {
582                 PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range");
583                 return NULL;
584         }
585         return newVectorObject(self->matrix[i], self->colSize, Py_WRAP);
586 }
587 /*----------------------------object[]-------------------------
588   sequence accessor (set)*/
589 static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
590 {
591         int y, x, size = 0;
592         float vec[4];
593         PyObject *m, *f;
594
595         if(i >= self->rowSize || i < 0){
596                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad row\n");
597                 return -1;
598         }
599
600         if(PySequence_Check(ob)){
601                 size = PySequence_Length(ob);
602                 if(size != self->colSize){
603                         PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad sequence size\n");
604                         return -1;
605                 }
606                 for (x = 0; x < size; x++) {
607                         m = PySequence_GetItem(ob, x);
608                         if (m == NULL) { /*Failed to read sequence*/
609                                 PyErr_SetString(PyExc_RuntimeError, "matrix[attribute] = x: unable to read sequence\n");
610                                 return -1;
611                         }
612
613                         f = PyNumber_Float(m);
614                         if(f == NULL) { /*parsed item not a number*/
615                                 Py_DECREF(m);
616                                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: sequence argument not a number\n");
617                                 return -1;
618                         }
619
620                         vec[x] = (float)PyFloat_AS_DOUBLE(f);
621                         Py_DECREF(m);
622                         Py_DECREF(f);
623                 }
624                 /*parsed well - now set in matrix*/
625                 for(y = 0; y < size; y++){
626                         self->matrix[i][y] = vec[y];
627                 }
628                 return 0;
629         }else{
630                 PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n");
631                 return -1;
632         }
633 }
634 /*----------------------------object[z:y]------------------------
635   sequence slice (get)*/
636 static PyObject *Matrix_slice(MatrixObject * self, int begin, int end)
637 {
638
639         PyObject *list = NULL;
640         int count;
641
642         CLAMP(begin, 0, self->rowSize);
643         CLAMP(end, 0, self->rowSize);
644         begin = MIN2(begin,end);
645
646         list = PyList_New(end - begin);
647         for(count = begin; count < end; count++) {
648                 PyList_SetItem(list, count - begin,
649                                 newVectorObject(self->matrix[count], self->colSize, Py_WRAP));
650         }
651
652         return list;
653 }
654 /*----------------------------object[z:y]------------------------
655   sequence slice (set)*/
656 static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
657                              PyObject * seq)
658 {
659         int i, x, y, size, sub_size = 0;
660         float mat[16], f;
661         PyObject *subseq;
662         PyObject *m;
663
664         CLAMP(begin, 0, self->rowSize);
665         CLAMP(end, 0, self->rowSize);
666         begin = MIN2(begin,end);
667
668         if(PySequence_Check(seq)){
669                 size = PySequence_Length(seq);
670                 if(size != (end - begin)){
671                         PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
672                         return -1;
673                 }
674                 /*parse sub items*/
675                 for (i = 0; i < size; i++) {
676                         /*parse each sub sequence*/
677                         subseq = PySequence_GetItem(seq, i);
678                         if (subseq == NULL) { /*Failed to read sequence*/
679                                 PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence");
680                                 return -1;
681                         }
682
683                         if(PySequence_Check(subseq)){
684                                 /*subsequence is also a sequence*/
685                                 sub_size = PySequence_Length(subseq);
686                                 if(sub_size != self->colSize){
687                                         Py_DECREF(subseq);
688                                         PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
689                                         return -1;
690                                 }
691                                 for (y = 0; y < sub_size; y++) {
692                                         m = PySequence_GetItem(subseq, y);
693                                         if (m == NULL) { /*Failed to read sequence*/
694                                                 Py_DECREF(subseq);
695                                                 PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence\n");
696                                                 return -1;
697                                         }
698                                         
699                                         f = PyFloat_AsDouble(m); /* faster to assume a float and raise an error after */
700                                         if(f == -1 && PyErr_Occurred()) { /*parsed item not a number*/
701                                                 Py_DECREF(m);
702                                                 Py_DECREF(subseq);
703                                                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: sequence argument not a number\n");
704                                                 return -1;
705                                         }
706
707                                         mat[(i * self->colSize) + y] = f;
708                                         Py_DECREF(m);
709                                 }
710                         }else{
711                                 Py_DECREF(subseq);
712                                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
713                                 return -1;
714                         }
715                         Py_DECREF(subseq);
716                 }
717                 /*parsed well - now set in matrix*/
718                 for(x = 0; x < (size * sub_size); x++){
719                         self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x];
720                 }
721                 return 0;
722         }else{
723                 PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
724                 return -1;
725         }
726 }
727 /*------------------------NUMERIC PROTOCOLS----------------------
728   ------------------------obj + obj------------------------------*/
729 static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
730 {
731         int x, y;
732         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
733                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
734         MatrixObject *mat1 = NULL, *mat2 = NULL;
735
736         mat1 = (MatrixObject*)m1;
737         mat2 = (MatrixObject*)m2;
738
739         if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
740                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
741                 return NULL;
742         }
743         if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
744                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
745                 return NULL;
746         }
747
748         for(x = 0; x < mat1->rowSize; x++) {
749                 for(y = 0; y < mat1->colSize; y++) {
750                         mat[((x * mat1->colSize) + y)] = mat1->matrix[x][y] + mat2->matrix[x][y];
751                 }
752         }
753
754         return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
755 }
756 /*------------------------obj - obj------------------------------
757   subtraction*/
758 static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
759 {
760         int x, y;
761         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
762                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
763         MatrixObject *mat1 = NULL, *mat2 = NULL;
764
765         mat1 = (MatrixObject*)m1;
766         mat2 = (MatrixObject*)m2;
767
768         if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
769                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
770                 return NULL;
771         }
772         if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
773                 PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
774                 return NULL;
775         }
776
777         for(x = 0; x < mat1->rowSize; x++) {
778                 for(y = 0; y < mat1->colSize; y++) {
779                         mat[((x * mat1->colSize) + y)] = mat1->matrix[x][y] - mat2->matrix[x][y];
780                 }
781         }
782
783         return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
784 }
785 /*------------------------obj * obj------------------------------
786   mulplication*/
787 static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
788 {
789         int x, y, z;
790         float scalar;
791         float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
792                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
793         double dot = 0.0f;
794         MatrixObject *mat1 = NULL, *mat2 = NULL;
795
796         if(MatrixObject_Check(m1))      mat1 = (MatrixObject*)m1;
797         if(MatrixObject_Check(m2))      mat2 = (MatrixObject*)m2;
798
799         if(mat1 && mat2) { /*MATRIX * MATRIX*/
800                 if(mat1->colSize != mat2->rowSize){
801                         PyErr_SetString(PyExc_AttributeError,"Matrix multiplication: matrix A rowsize must equal matrix B colsize");
802                         return NULL;
803                 }
804                 for(x = 0; x < mat1->rowSize; x++) {
805                         for(y = 0; y < mat2->colSize; y++) {
806                                 for(z = 0; z < mat1->colSize; z++) {
807                                         dot += (mat1->matrix[x][z] * mat2->matrix[z][y]);
808                                 }
809                                 mat[((x * mat1->rowSize) + y)] = (float)dot;
810                                 dot = 0.0f;
811                         }
812                 }
813                 
814                 return newMatrixObject(mat, mat1->rowSize, mat2->colSize, Py_NEW);
815         }
816         
817         if(mat1==NULL){
818                 scalar=PyFloat_AsDouble(m1); // may not be a float...
819                 if ((scalar == -1.0 && PyErr_Occurred())==0) { /*FLOAT/INT * MATRIX, this line annoys theeth, lets see if he finds it */
820                         for(x = 0; x < mat2->rowSize; x++) {
821                                 for(y = 0; y < mat2->colSize; y++) {
822                                         mat[((x * mat2->colSize) + y)] = scalar * mat2->matrix[x][y];
823                                 }
824                         }
825                         return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW);
826                 }
827                 
828                 PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
829                 return NULL;
830         }
831         else /* if(mat1) { */ {
832                 
833                 if(VectorObject_Check(m2)) { /* MATRIX*VECTOR */
834                         return column_vector_multiplication(mat1, (VectorObject *)m2); /* vector update done inside the function */
835                 }
836                 else {
837                         scalar= PyFloat_AsDouble(m2);
838                         if ((scalar == -1.0 && PyErr_Occurred())==0) { /* MATRIX*FLOAT/INT */
839                                 for(x = 0; x < mat1->rowSize; x++) {
840                                         for(y = 0; y < mat1->colSize; y++) {
841                                                 mat[((x * mat1->colSize) + y)] = scalar * mat1->matrix[x][y];
842                                         }
843                                 }
844                                 return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
845                         }
846                 }
847                 PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
848                 return NULL;
849         }
850
851         PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation\n");
852         return NULL;
853 }
854 static PyObject* Matrix_inv(MatrixObject *self)
855 {
856         return Matrix_Invert(self);
857 }
858
859 /*-----------------PROTOCOL DECLARATIONS--------------------------*/
860 static PySequenceMethods Matrix_SeqMethods = {
861         (inquiry) Matrix_len,                                   /* sq_length */
862         (binaryfunc) 0,                                                 /* sq_concat */
863         (ssizeargfunc) 0,                                                       /* sq_repeat */
864         (ssizeargfunc) Matrix_item,                             /* sq_item */
865         (ssizessizeargfunc) Matrix_slice,                       /* sq_slice */
866         (ssizeobjargproc) Matrix_ass_item,              /* sq_ass_item */
867         (ssizessizeobjargproc) Matrix_ass_slice,        /* sq_ass_slice */
868 };
869 static PyNumberMethods Matrix_NumMethods = {
870         (binaryfunc) Matrix_add,                                /* __add__ */
871         (binaryfunc) Matrix_sub,                                /* __sub__ */
872         (binaryfunc) Matrix_mul,                                /* __mul__ */
873         (binaryfunc) 0,                                                 /* __div__ */
874         (binaryfunc) 0,                                                 /* __mod__ */
875         (binaryfunc) 0,                                                 /* __divmod__ */
876         (ternaryfunc) 0,                                                /* __pow__ */
877         (unaryfunc) 0,                                                  /* __neg__ */
878         (unaryfunc) 0,                                                  /* __pos__ */
879         (unaryfunc) 0,                                                  /* __abs__ */
880         (inquiry) 0,                                                    /* __nonzero__ */
881         (unaryfunc) Matrix_inv,                                 /* __invert__ */
882         (binaryfunc) 0,                                                 /* __lshift__ */
883         (binaryfunc) 0,                                                 /* __rshift__ */
884         (binaryfunc) 0,                                                 /* __and__ */
885         (binaryfunc) 0,                                                 /* __xor__ */
886         (binaryfunc) 0,                                                 /* __or__ */
887         /*(coercion)*/ 0,                                                       /* __coerce__ */
888         (unaryfunc) 0,                                                  /* __int__ */
889         (unaryfunc) 0,                                                  /* __long__ */
890         (unaryfunc) 0,                                                  /* __float__ */
891         (unaryfunc) 0,                                                  /* __oct__ */
892         (unaryfunc) 0,                                                  /* __hex__ */
893 };
894
895 static PyObject *Matrix_getRowSize( MatrixObject * self, void *type )
896 {
897         return PyLong_FromLong((long) self->rowSize);
898 }
899
900 static PyObject *Matrix_getColSize( MatrixObject * self, void *type )
901 {
902         return PyLong_FromLong((long) self->colSize);
903 }
904
905 static PyObject *Matrix_getWrapped( MatrixObject * self, void *type )
906 {
907         if (self->wrapped == Py_WRAP)
908                 Py_RETURN_TRUE;
909         else
910                 Py_RETURN_FALSE;
911 }
912
913 /*****************************************************************************/
914 /* Python attributes get/set structure:                                      */
915 /*****************************************************************************/
916 static PyGetSetDef Matrix_getseters[] = {
917         {"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL},
918         {"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL},
919         {"wrapped", (getter)Matrix_getWrapped, (setter)NULL, "", NULL},
920         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
921 };
922
923 /*------------------PY_OBECT DEFINITION--------------------------*/
924 PyTypeObject matrix_Type = {
925 #if (PY_VERSION_HEX >= 0x02060000)
926         PyVarObject_HEAD_INIT(NULL, 0)
927 #else
928         /* python 2.5 and below */
929         PyObject_HEAD_INIT( NULL )  /* required py macro */
930         0,                          /* ob_size */
931 #endif
932         "matrix",                                               /*tp_name*/
933         sizeof(MatrixObject),                   /*tp_basicsize*/
934         0,                                                              /*tp_itemsize*/
935         (destructor)Matrix_dealloc,             /*tp_dealloc*/
936         0,                                                              /*tp_print*/
937         0,                                                              /*tp_getattr*/
938         0,                                                              /*tp_setattr*/
939         0,                                                              /*tp_compare*/
940         (reprfunc) Matrix_repr,                 /*tp_repr*/
941         &Matrix_NumMethods,                             /*tp_as_number*/
942         &Matrix_SeqMethods,                             /*tp_as_sequence*/
943         0,                                                              /*tp_as_mapping*/
944         0,                                                              /*tp_hash*/
945         0,                                                              /*tp_call*/
946         0,                                                              /*tp_str*/
947         0,                                                              /*tp_getattro*/
948         0,                                                              /*tp_setattro*/
949         0,                                                              /*tp_as_buffer*/
950         Py_TPFLAGS_DEFAULT,                             /*tp_flags*/
951         MatrixObject_doc,                               /*tp_doc*/
952         0,                                                              /*tp_traverse*/
953         0,                                                              /*tp_clear*/
954         (richcmpfunc)Matrix_richcmpr,   /*tp_richcompare*/
955         0,                                                              /*tp_weaklistoffset*/
956         0,                                                              /*tp_iter*/
957         0,                                                              /*tp_iternext*/
958         Matrix_methods,                                 /*tp_methods*/
959         0,                                                              /*tp_members*/
960         Matrix_getseters,                               /*tp_getset*/
961         0,                                                              /*tp_base*/
962         0,                                                              /*tp_dict*/
963         0,                                                              /*tp_descr_get*/
964         0,                                                              /*tp_descr_set*/
965         0,                                                              /*tp_dictoffset*/
966         0,                                                              /*tp_init*/
967         0,                                                              /*tp_alloc*/
968         Matrix_new,                                             /*tp_new*/
969         0,                                                              /*tp_free*/
970         0,                                                              /*tp_is_gc*/
971         0,                                                              /*tp_bases*/
972         0,                                                              /*tp_mro*/
973         0,                                                              /*tp_cache*/
974         0,                                                              /*tp_subclasses*/
975         0,                                                              /*tp_weaklist*/
976         0                                                               /*tp_del*/
977 };
978
979 /*------------------------newMatrixObject (internal)-------------
980 creates a new matrix object
981 self->matrix     self->contiguous_ptr (reference to data.xxx)
982        [0]------------->[0]
983                         [1]
984                         [2]
985        [1]------------->[3]
986                         [4]
987                         [5]
988                      ....
989 self->matrix[1][1] = self->contiguous_ptr[4] = self->data.xxx_data[4]*/
990
991 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
992  (i.e. it was allocated elsewhere by MEM_mallocN())
993   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
994  (i.e. it must be created here with PyMEM_malloc())*/
995 PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
996 {
997         MatrixObject *self;
998         int x, row, col;
999
1000         /*matrix objects can be any 2-4row x 2-4col matrix*/
1001         if(rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4){
1002                 PyErr_SetString(PyExc_RuntimeError, "matrix(): row and column sizes must be between 2 and 4");
1003                 return NULL;
1004         }
1005
1006         self = PyObject_NEW(MatrixObject, &matrix_Type);
1007         self->data.blend_data = NULL;
1008         self->data.py_data = NULL;
1009         self->rowSize = rowSize;
1010         self->colSize = colSize;
1011
1012         if(type == Py_WRAP){
1013                 self->data.blend_data = mat;
1014                 self->contigPtr = self->data.blend_data;
1015                 /*create pointer array*/
1016                 self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
1017                 if(self->matrix == NULL) { /*allocation failure*/
1018                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
1019                         return NULL;
1020                 }
1021                 /*pointer array points to contigous memory*/
1022                 for(x = 0; x < rowSize; x++) {
1023                         self->matrix[x] = self->contigPtr + (x * colSize);
1024                 }
1025                 self->wrapped = Py_WRAP;
1026         }else if (type == Py_NEW){
1027                 self->data.py_data = PyMem_Malloc(rowSize * colSize * sizeof(float));
1028                 if(self->data.py_data == NULL) { /*allocation failure*/
1029                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n");
1030                         return NULL;
1031                 }
1032                 self->contigPtr = self->data.py_data;
1033                 /*create pointer array*/
1034                 self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
1035                 if(self->matrix == NULL) { /*allocation failure*/
1036                         PyMem_Free(self->data.py_data);
1037                         PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
1038                         return NULL;
1039                 }
1040                 /*pointer array points to contigous memory*/
1041                 for(x = 0; x < rowSize; x++) {
1042                         self->matrix[x] = self->contigPtr + (x * colSize);
1043                 }
1044                 /*parse*/
1045                 if(mat) {       /*if a float array passed*/
1046                         for(row = 0; row < rowSize; row++) {
1047                                 for(col = 0; col < colSize; col++) {
1048                                         self->matrix[row][col] = mat[(row * colSize) + col];
1049                                 }
1050                         }
1051                 } else if (rowSize == colSize ) { /*or if no arguments are passed return identity matrix for square matrices */
1052                         Matrix_Identity(self);
1053                         Py_DECREF(self);
1054                 }
1055                 self->wrapped = Py_NEW;
1056         }else{ /*bad type*/
1057                 return NULL;
1058         }
1059         return (PyObject *) self;
1060 }
1061
1062 //----------------column_vector_multiplication (internal)---------
1063 //COLUMN VECTOR Multiplication (Matrix X Vector)
1064 // [1][2][3]   [a]
1065 // [4][5][6] * [b]
1066 // [7][8][9]   [c]
1067 //vector/matrix multiplication IS NOT COMMUTATIVE!!!!
1068 static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec)
1069 {
1070         float vecNew[4], vecCopy[4];
1071         double dot = 0.0f;
1072         int x, y, z = 0;
1073
1074         if(!Vector_ReadCallback(vec))
1075                 return NULL;
1076         
1077         if(mat->rowSize != vec->size){
1078                 if(mat->rowSize == 4 && vec->size != 3){
1079                         PyErr_SetString(PyExc_AttributeError, "matrix * vector: matrix row size and vector size must be the same");
1080                         return NULL;
1081                 }else{
1082                         vecCopy[3] = 1.0f;
1083                 }
1084         }
1085
1086         for(x = 0; x < vec->size; x++){
1087                 vecCopy[x] = vec->vec[x];
1088                 }
1089
1090         for(x = 0; x < mat->rowSize; x++) {
1091                 for(y = 0; y < mat->colSize; y++) {
1092                         dot += mat->matrix[x][y] * vecCopy[y];
1093                 }
1094                 vecNew[z++] = (float)dot;
1095                 dot = 0.0f;
1096         }
1097         return newVectorObject(vecNew, vec->size, Py_NEW);
1098 }