| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Michel Selten & Joseph Gilbert | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-05 01:48:10 +00:00
										 |  |  | /** \file blender/python/mathutils/mathutils_Matrix.c
 | 
					
						
							|  |  |  |  *  \ingroup pymathutils | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-14 04:15:25 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-11 12:05:27 +00:00
										 |  |  | #include "mathutils.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 |  |  | #include "BLI_math.h"
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #ifndef MATH_STANDALONE
 | 
					
						
							| 
									
										
										
										
											2014-05-17 12:05:16 +10:00
										 |  |  | #  include "BLI_string.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #  include "BLI_dynstr.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | typedef enum eMatrixAccess_t { | 
					
						
							|  |  |  | 	MAT_ACCESS_ROW, | 
					
						
							|  |  |  | 	MAT_ACCESS_COL | 
					
						
							|  |  |  | } eMatrixAccess_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | static PyObject *Matrix_copy_notest(MatrixObject *self, const float *matrix); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_copy(MatrixObject *self); | 
					
						
							| 
									
										
										
										
											2012-03-30 11:35:58 +00:00
										 |  |  | static PyObject *Matrix_deepcopy(MatrixObject *self, PyObject *args); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value); | 
					
						
							| 
									
										
										
										
											2011-02-09 09:20:17 +00:00
										 |  |  | static PyObject *matrix__apply_to_copy(PyNoArgsFunction matrix_func, MatrixObject *self); | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | static PyObject *MatrixAccess_CreatePyObject(MatrixObject *matrix, const eMatrixAccess_t type); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int matrix_row_vector_check(MatrixObject *mat, VectorObject *vec, int row) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if ((vec->size != mat->num_col) || (row >= mat->num_row)) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							|  |  |  | 		                "Matrix(): " | 
					
						
							|  |  |  | 		                "owner matrix has been resized since this row vector was created"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int matrix_col_vector_check(MatrixObject *mat, VectorObject *vec, int col) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if ((vec->size != mat->num_row) || (col >= mat->num_col)) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							|  |  |  | 		                "Matrix(): " | 
					
						
							|  |  |  | 		                "owner matrix has been resized since this column vector was created"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  |  * matrix row callbacks | 
					
						
							|  |  |  |  * this is so you can do matrix[i][j] = val OR matrix.row[i][j] = val */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-17 10:46:02 +00:00
										 |  |  | unsigned char mathutils_matrix_row_cb_index = -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_row_check(BaseMathObject *bmo) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2009-06-25 10:11:37 +00:00
										 |  |  | 	return BaseMath_ReadCallback(self); | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_row_get(BaseMathObject *bmo, int row) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	int col; | 
					
						
							| 
									
										
										
										
											2009-08-15 16:36:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	for (col = 0; col < self->num_col; col++) { | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		bmo->data[col] = MATRIX_ITEM(self, row, col); | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_row_set(BaseMathObject *bmo, int row) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	int col; | 
					
						
							| 
									
										
										
										
											2009-08-15 16:36:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	for (col = 0; col < self->num_col; col++) { | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		MATRIX_ITEM(self, row, col) = bmo->data[col]; | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 12:11:09 +00:00
										 |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_row_get_index(BaseMathObject *bmo, int row, int col) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2009-08-15 16:36:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	bmo->data[col] = MATRIX_ITEM(self, row, col); | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_row_set_index(BaseMathObject *bmo, int row, int col) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2009-08-15 16:36:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	MATRIX_ITEM(self, row, col) = bmo->data[col]; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 12:11:09 +00:00
										 |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							| 
									
										
										
										
											2011-02-28 18:42:41 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 06:13:58 +00:00
										 |  |  | Mathutils_Callback mathutils_matrix_row_cb = { | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	mathutils_matrix_row_check, | 
					
						
							|  |  |  | 	mathutils_matrix_row_get, | 
					
						
							|  |  |  | 	mathutils_matrix_row_set, | 
					
						
							|  |  |  | 	mathutils_matrix_row_get_index, | 
					
						
							|  |  |  | 	mathutils_matrix_row_set_index | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  |  * matrix row callbacks | 
					
						
							|  |  |  |  * this is so you can do matrix.col[i][j] = val */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-17 10:46:02 +00:00
										 |  |  | unsigned char mathutils_matrix_col_cb_index = -1; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_col_check(BaseMathObject *bmo) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 	return BaseMath_ReadCallback(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_col_get(BaseMathObject *bmo, int col) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 	int num_row; | 
					
						
							|  |  |  | 	int row; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* for 'translation' size will always be '3' even on 4x4 vec */ | 
					
						
							| 
									
										
										
										
											2012-10-27 11:18:54 +00:00
										 |  |  | 	num_row = min_ii(self->num_row, ((VectorObject *)bmo)->size); | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (row = 0; row < num_row; row++) { | 
					
						
							|  |  |  | 		bmo->data[row] = MATRIX_ITEM(self, row, col); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_col_set(BaseMathObject *bmo, int col) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 	int num_row; | 
					
						
							|  |  |  | 	int row; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* for 'translation' size will always be '3' even on 4x4 vec */ | 
					
						
							| 
									
										
										
										
											2012-10-27 11:18:54 +00:00
										 |  |  | 	num_row = min_ii(self->num_row, ((VectorObject *)bmo)->size); | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (row = 0; row < num_row; row++) { | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, row, col) = bmo->data[row]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_col_get_index(BaseMathObject *bmo, int col, int row) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	bmo->data[row] = MATRIX_ITEM(self, row, col); | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | static int mathutils_matrix_col_set_index(BaseMathObject *bmo, int col, int row) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	MATRIX_ITEM(self, row, col) = bmo->data[row]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 06:13:58 +00:00
										 |  |  | Mathutils_Callback mathutils_matrix_col_cb = { | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	mathutils_matrix_col_check, | 
					
						
							|  |  |  | 	mathutils_matrix_col_get, | 
					
						
							|  |  |  | 	mathutils_matrix_col_set, | 
					
						
							|  |  |  | 	mathutils_matrix_col_get_index, | 
					
						
							|  |  |  | 	mathutils_matrix_col_set_index | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  |  * matrix row callbacks | 
					
						
							|  |  |  |  * this is so you can do matrix.translation = val | 
					
						
							|  |  |  |  * note, this is _exactly like matrix.col except the 4th component is always omitted */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-17 10:46:02 +00:00
										 |  |  | unsigned char mathutils_matrix_translation_cb_index = -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int mathutils_matrix_translation_check(BaseMathObject *bmo) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							|  |  |  | 	return BaseMath_ReadCallback(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int mathutils_matrix_translation_get(BaseMathObject *bmo, int col) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							|  |  |  | 	int row; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (row = 0; row < 3; row++) { | 
					
						
							|  |  |  | 		bmo->data[row] = MATRIX_ITEM(self, row, col); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int mathutils_matrix_translation_set(BaseMathObject *bmo, int col) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							|  |  |  | 	int row; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (row = 0; row < 3; row++) { | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, row, col) = bmo->data[row]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int mathutils_matrix_translation_get_index(BaseMathObject *bmo, int col, int row) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bmo->data[row] = MATRIX_ITEM(self, row, col); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int mathutils_matrix_translation_set_index(BaseMathObject *bmo, int col, int row) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self = (MatrixObject *)bmo->cb_user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MATRIX_ITEM(self, row, col) = bmo->data[row]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Mathutils_Callback mathutils_matrix_translation_cb = { | 
					
						
							|  |  |  | 	mathutils_matrix_translation_check, | 
					
						
							|  |  |  | 	mathutils_matrix_translation_get, | 
					
						
							|  |  |  | 	mathutils_matrix_translation_set, | 
					
						
							|  |  |  | 	mathutils_matrix_translation_get_index, | 
					
						
							|  |  |  | 	mathutils_matrix_translation_set_index | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | /* matrix column callbacks, this is so you can do matrix.translation = Vector()  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | /* ----------------------------------mathutils.Matrix() ----------------- */ | 
					
						
							|  |  |  | /* mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. */ | 
					
						
							|  |  |  | /* create a new matrix type */ | 
					
						
							| 
									
										
										
										
											2010-11-28 06:03:30 +00:00
										 |  |  | static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (kwds && PyDict_Size(kwds)) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "takes no keyword args"); | 
					
						
							| 
									
										
										
										
											2010-10-13 23:25:08 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 08:50:06 +00:00
										 |  |  | 	switch (PyTuple_GET_SIZE(args)) { | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 		case 0: | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 			return Matrix_CreatePyObject(NULL, 4, 4, type); | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 		case 1: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			PyObject *arg = PyTuple_GET_ITEM(args, 0); | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 			/* Input is now as a sequence of rows so length of sequence
 | 
					
						
							|  |  |  | 			 * is the number of rows */ | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			/* -1 is an error, size checks will accunt for this */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			const unsigned short num_row = PySequence_Size(arg); | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 			if (num_row >= 2 && num_row <= 4) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 				PyObject *item = PySequence_GetItem(arg, 0); | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 				/* Since each item is a row, number of items is the
 | 
					
						
							|  |  |  | 				 * same as the number of columns */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 				const unsigned short num_col = PySequence_Size(item); | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 				Py_XDECREF(item); | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 				if (num_col >= 2 && num_col <= 4) { | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 					/* sane row & col size, new matrix and assign as slice  */ | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 					PyObject *matrix = Matrix_CreatePyObject(NULL, num_col, num_row, type); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 					if (Matrix_ass_slice((MatrixObject *)matrix, 0, INT_MAX, arg) == 0) { | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 						return matrix; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					else { /* matrix ok, slice assignment not */ | 
					
						
							|  |  |  | 						Py_DECREF(matrix); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-21 08:16:37 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* will overwrite error */ | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 	PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 	                "Matrix(): " | 
					
						
							| 
									
										
										
										
											2013-11-29 09:47:11 +11:00
										 |  |  | 	                "expects no args or a single arg containing 2-4 numeric sequences"); | 
					
						
							| 
									
										
										
										
											2011-01-09 14:30:16 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-20 02:44:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-09 09:20:17 +00:00
										 |  |  | static PyObject *matrix__apply_to_copy(PyNoArgsFunction matrix_func, MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	PyObject *ret = Matrix_copy(self); | 
					
						
							| 
									
										
										
										
											2012-10-31 03:21:13 +00:00
										 |  |  | 	if (ret) { | 
					
						
							|  |  |  | 		PyObject *ret_dummy = matrix_func(ret); | 
					
						
							|  |  |  | 		if (ret_dummy) { | 
					
						
							|  |  |  | 			Py_DECREF(ret_dummy); | 
					
						
							|  |  |  | 			return (PyObject *)ret; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { /* error */ | 
					
						
							|  |  |  | 			Py_DECREF(ret); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-02-09 09:20:17 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-31 03:21:13 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		/* copy may fail if the read callback errors out */ | 
					
						
							| 
									
										
										
										
											2011-02-09 09:20:17 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | /* when a matrix is 4x4 size but initialized as a 3x3, re-assign values for 4x4 */ | 
					
						
							|  |  |  | static void matrix_3x3_as_4x4(float mat[16]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	mat[10] = mat[8]; | 
					
						
							|  |  |  | 	mat[9] = mat[7]; | 
					
						
							|  |  |  | 	mat[8] = mat[6]; | 
					
						
							|  |  |  | 	mat[7] = 0.0f; | 
					
						
							|  |  |  | 	mat[6] = mat[5]; | 
					
						
							|  |  |  | 	mat[5] = mat[4]; | 
					
						
							|  |  |  | 	mat[4] = mat[3]; | 
					
						
							|  |  |  | 	mat[3] = 0.0f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | /*-----------------------CLASS-METHODS----------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | /* mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. */ | 
					
						
							| 
									
										
										
										
											2012-01-24 01:56:44 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_Identity_doc, | 
					
						
							|  |  |  | ".. classmethod:: Identity(size)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Create an identity matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg size: The size of the identity matrix to construct [2, 4].\n" | 
					
						
							|  |  |  | "   :type size: int\n" | 
					
						
							|  |  |  | "   :return: A new identity matrix.\n" | 
					
						
							|  |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | static PyObject *C_Matrix_Identity(PyObject *cls, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int matSize; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!PyArg_ParseTuple(args, "i:Matrix.Identity", &matSize)) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matSize < 2 || matSize > 4) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 		                "Matrix.Identity(): " | 
					
						
							|  |  |  | 		                "size must be between 2 and 4"); | 
					
						
							| 
									
										
										
										
											2012-01-24 01:56:44 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(NULL, matSize, matSize, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2012-01-24 01:56:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_Rotation_doc, | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | ".. classmethod:: Rotation(angle, size, axis)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Create a matrix representing a rotation.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg angle: The angle of rotation desired, in radians.\n" | 
					
						
							|  |  |  | "   :type angle: float\n" | 
					
						
							|  |  |  | "   :arg size: The size of the rotation matrix to construct [2, 4].\n" | 
					
						
							|  |  |  | "   :type size: int\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg axis: a string in ['X', 'Y', 'Z'] or a 3D Vector Object\n" | 
					
						
							|  |  |  | "      (optional when size is 2).\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "   :type axis: string or :class:`Vector`\n" | 
					
						
							|  |  |  | "   :return: A new rotation matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	PyObject *vec = NULL; | 
					
						
							|  |  |  | 	const char *axis = NULL; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	int matSize; | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 	double angle; /* use double because of precision problems at high values */ | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 		             0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 		             0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 		             0.0f, 0.0f, 0.0f, 1.0f}; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "di|O:Matrix.Rotation", &angle, &matSize, &vec)) { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (vec && PyUnicode_Check(vec)) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		axis = _PyUnicode_AsString((PyObject *)vec); | 
					
						
							|  |  |  | 		if (axis == NULL || axis[0] == '\0' || axis[1] != '\0' || axis[0] < 'X' || axis[0] > 'Z') { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 			                "Matrix.Rotation(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			                "3rd argument axis value must be a 3D vector " | 
					
						
							|  |  |  | 			                "or a string in 'X', 'Y', 'Z'"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			/* use the string */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			vec = NULL; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	angle = angle_wrap_rad(angle); | 
					
						
							| 
									
										
										
										
											2011-01-11 09:41:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize != 2 && matSize != 3 && matSize != 4) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.Rotation(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "can only return a 2x2 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 2 && (vec != NULL)) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.Rotation(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "cannot create a 2x2 rotation matrix around arbitrary axis"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if ((matSize == 3 || matSize == 4) && (axis == NULL) && (vec == NULL)) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.Rotation(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "axis of rotation for 3d and 4d matrices is required"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* check for valid vector/axis above */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (vec) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 		float tvec[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		if (mathutils_array_parse(tvec, 3, 3, vec, "Matrix.Rotation(angle, size, axis), invalid 'axis' arg") == -1) | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		axis_angle_to_mat3((float (*)[3])mat, tvec, angle); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-09-19 13:08:01 +00:00
										 |  |  | 	else if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2013-09-10 20:45:47 +00:00
										 |  |  | 		angle_to_mat2((float (*)[2])mat, angle); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-09-19 13:08:01 +00:00
										 |  |  | 		/* valid axis checked above */ | 
					
						
							| 
									
										
										
										
											2013-09-10 20:45:47 +00:00
										 |  |  | 		axis_angle_to_mat3_single((float (*)[3])mat, axis[0], angle); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 4) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 		matrix_3x3_as_4x4(mat); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	/* pass to matrix creation */ | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, matSize, matSize, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_Translation_doc, | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | ".. classmethod:: Translation(vector)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Create a matrix representing a translation.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg vector: The translation vector.\n" | 
					
						
							|  |  |  | "   :type vector: :class:`Vector`\n" | 
					
						
							|  |  |  | "   :return: An identity matrix with a translation.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *C_Matrix_Translation(PyObject *cls, PyObject *value) | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float mat[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	unit_m4(mat); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | 	if (mathutils_array_parse(mat[3], 3, 4, value, "mathutils.Matrix.Translation(vector), invalid vector arg") == -1) | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(&mat[0][0], 4, 4, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | /* ----------------------------------mathutils.Matrix.Scale() ------------- */ | 
					
						
							|  |  |  | /* mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. */ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_Scale_doc, | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | ".. classmethod:: Scale(factor, size, axis)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Create a matrix representing a scaling.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :arg factor: The factor of scaling to apply.\n" | 
					
						
							|  |  |  | "   :type factor: float\n" | 
					
						
							|  |  |  | "   :arg size: The size of the scale matrix to construct [2, 4].\n" | 
					
						
							|  |  |  | "   :type size: int\n" | 
					
						
							|  |  |  | "   :arg axis: Direction to influence scale. (optional).\n" | 
					
						
							|  |  |  | "   :type axis: :class:`Vector`\n" | 
					
						
							|  |  |  | "   :return: A new scale matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	PyObject *vec = NULL; | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 	int vec_size; | 
					
						
							|  |  |  | 	float tvec[3]; | 
					
						
							|  |  |  | 	float factor; | 
					
						
							|  |  |  | 	int matSize; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 1.0f}; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "fi|O:Matrix.Scale", &factor, &matSize, &vec)) { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize != 2 && matSize != 3 && matSize != 4) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "Matrix.Scale(): " | 
					
						
							|  |  |  | 		                "can only return a 2x2 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (vec) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		vec_size = (matSize == 2 ? 2 : 3); | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 		if (mathutils_array_parse(tvec, vec_size, vec_size, vec, | 
					
						
							|  |  |  | 		                          "Matrix.Scale(factor, size, axis), invalid 'axis' arg") == -1) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	if (vec == NULL) {  /* scaling along axis */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 			mat[0] = factor; | 
					
						
							|  |  |  | 			mat[3] = factor; | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 			mat[0] = factor; | 
					
						
							|  |  |  | 			mat[4] = factor; | 
					
						
							|  |  |  | 			mat[8] = factor; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		/* scaling in arbitrary direction
 | 
					
						
							|  |  |  | 		 * normalize arbitrary axis */ | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 		float norm = 0.0f; | 
					
						
							|  |  |  | 		int x; | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		for (x = 0; x < vec_size; x++) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 			norm += tvec[x] * tvec[x]; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-28 14:53:37 +11:00
										 |  |  | 		norm = sqrtf(norm); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		for (x = 0; x < vec_size; x++) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 			tvec[x] /= norm; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 			mat[0] = 1 + ((factor - 1) * (tvec[0] * tvec[0])); | 
					
						
							|  |  |  | 			mat[1] =     ((factor - 1) * (tvec[0] * tvec[1])); | 
					
						
							|  |  |  | 			mat[2] =     ((factor - 1) * (tvec[0] * tvec[1])); | 
					
						
							|  |  |  | 			mat[3] = 1 + ((factor - 1) * (tvec[1] * tvec[1])); | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 			mat[0] = 1 + ((factor - 1) * (tvec[0] * tvec[0])); | 
					
						
							|  |  |  | 			mat[1] =     ((factor - 1) * (tvec[0] * tvec[1])); | 
					
						
							|  |  |  | 			mat[2] =     ((factor - 1) * (tvec[0] * tvec[2])); | 
					
						
							|  |  |  | 			mat[3] =     ((factor - 1) * (tvec[0] * tvec[1])); | 
					
						
							|  |  |  | 			mat[4] = 1 + ((factor - 1) * (tvec[1] * tvec[1])); | 
					
						
							|  |  |  | 			mat[5] =     ((factor - 1) * (tvec[1] * tvec[2])); | 
					
						
							|  |  |  | 			mat[6] =     ((factor - 1) * (tvec[0] * tvec[2])); | 
					
						
							|  |  |  | 			mat[7] =     ((factor - 1) * (tvec[1] * tvec[2])); | 
					
						
							|  |  |  | 			mat[8] = 1 + ((factor - 1) * (tvec[2] * tvec[2])); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 4) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 		matrix_3x3_as_4x4(mat); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	/* pass to matrix creation */ | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, matSize, matSize, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | /* ----------------------------------mathutils.Matrix.OrthoProjection() --- */ | 
					
						
							|  |  |  | /* mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. */ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_OrthoProjection_doc, | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | ".. classmethod:: OrthoProjection(axis, size)\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Create a matrix to represent an orthographic projection.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg axis: Can be any of the following: ['X', 'Y', 'XY', 'XZ', 'YZ'],\n" | 
					
						
							|  |  |  | "      where a single axis is for a 2D matrix.\n" | 
					
						
							|  |  |  | "      Or a vector for an arbitrary axis\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :type axis: string or :class:`Vector`\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "   :arg size: The size of the projection matrix to construct [2, 4].\n" | 
					
						
							|  |  |  | "   :type size: int\n" | 
					
						
							|  |  |  | "   :return: A new projection matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 	PyObject *axis; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	int matSize, x; | 
					
						
							|  |  |  | 	float norm = 0.0f; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 1.0f}; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "Oi:Matrix.OrthoProjection", &axis, &matSize)) { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize != 2 && matSize != 3 && matSize != 4) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.OrthoProjection(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "can only return a 2x2 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	if (PyUnicode_Check(axis)) {  /* ortho projection onto cardinal plane */ | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 		Py_ssize_t plane_len; | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		const char *plane = _PyUnicode_AsStringAndSize(axis, &plane_len); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			if (plane_len == 1 && plane[0] == 'X') { | 
					
						
							|  |  |  | 				mat[0] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			else if (plane_len == 1 && plane[0] == 'Y') { | 
					
						
							|  |  |  | 				mat[3] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 				PyErr_Format(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 				             "Matrix.OrthoProjection(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 				             "unknown plane, expected: X, Y, not '%.200s'", | 
					
						
							|  |  |  | 				             plane); | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 				return NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			if (plane_len == 2 && plane[0] == 'X' && plane[1] == 'Y') { | 
					
						
							|  |  |  | 				mat[0] = 1.0f; | 
					
						
							|  |  |  | 				mat[4] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			else if (plane_len == 2 && plane[0] == 'X' && plane[1] == 'Z') { | 
					
						
							|  |  |  | 				mat[0] = 1.0f; | 
					
						
							|  |  |  | 				mat[8] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			else if (plane_len == 2 && plane[0] == 'Y' && plane[1] == 'Z') { | 
					
						
							|  |  |  | 				mat[4] = 1.0f; | 
					
						
							|  |  |  | 				mat[8] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 				PyErr_Format(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 				             "Matrix.OrthoProjection(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 				             "unknown plane, expected: XY, XZ, YZ, not '%.200s'", | 
					
						
							|  |  |  | 				             plane); | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 				return NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 		/* arbitrary plane */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		int vec_size = (matSize == 2 ? 2 : 3); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 		float tvec[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-29 20:22:35 +00:00
										 |  |  | 		if (mathutils_array_parse(tvec, vec_size, vec_size, axis, | 
					
						
							|  |  |  | 		                          "Matrix.OrthoProjection(axis, size), invalid 'axis' arg") == -1) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 		/* normalize arbitrary axis */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		for (x = 0; x < vec_size; x++) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			norm += tvec[x] * tvec[x]; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-28 14:53:37 +11:00
										 |  |  | 		norm = sqrtf(norm); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		for (x = 0; x < vec_size; x++) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			tvec[x] /= norm; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			mat[0] = 1 - (tvec[0] * tvec[0]); | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | 			mat[1] =   - (tvec[0] * tvec[1]); | 
					
						
							|  |  |  | 			mat[2] =   - (tvec[0] * tvec[1]); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			mat[3] = 1 - (tvec[1] * tvec[1]); | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		else if (matSize > 2) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			mat[0] = 1 - (tvec[0] * tvec[0]); | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | 			mat[1] =   - (tvec[0] * tvec[1]); | 
					
						
							|  |  |  | 			mat[2] =   - (tvec[0] * tvec[2]); | 
					
						
							|  |  |  | 			mat[3] =   - (tvec[0] * tvec[1]); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			mat[4] = 1 - (tvec[1] * tvec[1]); | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | 			mat[5] =   - (tvec[1] * tvec[2]); | 
					
						
							|  |  |  | 			mat[6] =   - (tvec[0] * tvec[2]); | 
					
						
							|  |  |  | 			mat[7] =   - (tvec[1] * tvec[2]); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 			mat[8] = 1 - (tvec[2] * tvec[2]); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 4) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 		matrix_3x3_as_4x4(mat); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	/* pass to matrix creation */ | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, matSize, matSize, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(C_Matrix_Shear_doc, | 
					
						
							| 
									
										
										
										
											2011-02-04 09:41:59 +00:00
										 |  |  | ".. classmethod:: Shear(plane, size, factor)\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Create a matrix to represent an shear transformation.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg plane: Can be any of the following: ['X', 'Y', 'XY', 'XZ', 'YZ'],\n" | 
					
						
							|  |  |  | "      where a single axis is for a 2D matrix only.\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "   :type plane: string\n" | 
					
						
							|  |  |  | "   :arg size: The size of the shear matrix to construct [2, 4].\n" | 
					
						
							|  |  |  | "   :type size: int\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg factor: The factor of shear to apply. For a 3 or 4 *size* matrix\n" | 
					
						
							| 
									
										
										
										
											2012-03-13 06:22:43 +00:00
										 |  |  | "      pass a pair of floats corresponding with the *plane* axis.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:41:59 +00:00
										 |  |  | "   :type factor: float or float pair\n" | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | "   :return: A new shear matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int matSize; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	const char *plane; | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 	PyObject *fac; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 0.0f, | 
					
						
							|  |  |  | 	                 0.0f, 0.0f, 0.0f, 1.0f}; | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "siO:Matrix.Shear", &plane, &matSize, &fac)) { | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize != 2 && matSize != 3 && matSize != 4) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.Shear(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "can only return a 2x2 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 2) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		float const factor = PyFloat_AsDouble(fac); | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		if (factor == -1.0f && PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 			                "Matrix.Shear(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			                "the factor to be a float"); | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* unit */ | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		mat[0] = 1.0f; | 
					
						
							|  |  |  | 		mat[3] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 		if (STREQ(plane, "X")) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			mat[2] = factor; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 		else if (STREQ(plane, "Y")) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			mat[1] = factor; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			                "Matrix.Shear(): " | 
					
						
							|  |  |  | 			                "expected: X, Y or wrong matrix size for shearing plane"); | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* 3 or 4, apply as 3x3, resize later if needed */ | 
					
						
							|  |  |  | 		float factor[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 10:37:39 +10:00
										 |  |  | 		if (mathutils_array_parse(factor, 2, 2, fac, "Matrix.Shear()") == -1) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* unit */ | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 		mat[0] = 1.0f; | 
					
						
							|  |  |  | 		mat[4] = 1.0f; | 
					
						
							|  |  |  | 		mat[8] = 1.0f; | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 		if (STREQ(plane, "XY")) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			mat[6] = factor[0]; | 
					
						
							|  |  |  | 			mat[7] = factor[1]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 		else if (STREQ(plane, "XZ")) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			mat[3] = factor[0]; | 
					
						
							|  |  |  | 			mat[5] = factor[1]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-03-10 06:18:03 +00:00
										 |  |  | 		else if (STREQ(plane, "YZ")) { | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			mat[1] = factor[0]; | 
					
						
							|  |  |  | 			mat[2] = factor[1]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 			                "Matrix.Shear(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			                "expected: X, Y, XY, XZ, YZ"); | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-21 03:18:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (matSize == 4) { | 
					
						
							| 
									
										
										
										
											2011-02-06 11:17:22 +00:00
										 |  |  | 		matrix_3x3_as_4x4(mat); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	/* pass to matrix creation */ | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, matSize, matSize, (PyTypeObject *)cls); | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | void matrix_as_3x3(float mat[3][3], MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 	copy_v3_v3(mat[0], MATRIX_COL_PTR(self, 0)); | 
					
						
							|  |  |  | 	copy_v3_v3(mat[1], MATRIX_COL_PTR(self, 1)); | 
					
						
							|  |  |  | 	copy_v3_v3(mat[2], MATRIX_COL_PTR(self, 2)); | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | static void matrix_copy(MatrixObject *mat_dst, const MatrixObject *mat_src) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BLI_assert((mat_dst->num_col == mat_src->num_col) && | 
					
						
							|  |  |  | 	           (mat_dst->num_row == mat_src->num_row)); | 
					
						
							|  |  |  | 	BLI_assert(mat_dst != mat_src); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memcpy(mat_dst->matrix, mat_src->matrix, sizeof(float) * (mat_dst->num_col * mat_dst->num_row)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 10:46:14 +11:00
										 |  |  | /* transposes memory layout, rol/col's don't have to match */ | 
					
						
							|  |  |  | static void matrix_transpose_internal(float mat_dst_fl[], const MatrixObject *mat_src) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned short col, row; | 
					
						
							|  |  |  | 	unsigned int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (row = 0; row < mat_src->num_row; row++) { | 
					
						
							|  |  |  | 		for (col = 0; col < mat_src->num_col; col++) { | 
					
						
							|  |  |  | 			mat_dst_fl[i++] = MATRIX_ITEM(mat_src, row, col); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | /* assumes rowsize == colsize is checked and the read callback has run */ | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | static float matrix_determinant_internal(const MatrixObject *self) | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col == 2) { | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 		return determinant_m2(MATRIX_ITEM(self, 0, 0), MATRIX_ITEM(self, 0, 1), | 
					
						
							|  |  |  | 		                      MATRIX_ITEM(self, 1, 0), MATRIX_ITEM(self, 1, 1)); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	else if (self->num_col == 3) { | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 		return determinant_m3(MATRIX_ITEM(self, 0, 0), MATRIX_ITEM(self, 0, 1), MATRIX_ITEM(self, 0, 2), | 
					
						
							|  |  |  | 		                      MATRIX_ITEM(self, 1, 0), MATRIX_ITEM(self, 1, 1), MATRIX_ITEM(self, 1, 2), | 
					
						
							|  |  |  | 		                      MATRIX_ITEM(self, 2, 0), MATRIX_ITEM(self, 2, 1), MATRIX_ITEM(self, 2, 2)); | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		return determinant_m4((float (*)[4])self->matrix); | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | static void adjoint_matrix_n(float *mat_dst, const float *mat_src, const unsigned short dim) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* calculate the classical adjoint */ | 
					
						
							|  |  |  | 	switch (dim) { | 
					
						
							|  |  |  | 		case 2: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			adjoint_m2_m2((float (*)[2])mat_dst, (float (*)[2])mat_src); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		case 3: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			adjoint_m3_m3((float (*)[3])mat_dst, (float (*)[3])mat_src); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		case 4: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			adjoint_m4_m4((float (*)[4])mat_dst, (float (*)[4])mat_src); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			BLI_assert(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void matrix_invert_with_det_n_internal(float *mat_dst, const float *mat_src, const float det, const unsigned short dim) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							|  |  |  | 	unsigned short i, j, k; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BLI_assert(det != 0.0f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	adjoint_matrix_n(mat, mat_src, dim); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* divide by determinant & set values */ | 
					
						
							|  |  |  | 	k = 0; | 
					
						
							|  |  |  | 	for (i = 0; i < dim; i++) {  /* num_col */ | 
					
						
							|  |  |  | 		for (j = 0; j < dim; j++) {  /* num_row */ | 
					
						
							|  |  |  | 			mat_dst[MATRIX_ITEM_INDEX_NUMROW(dim, j, i)] = mat[k++] / det; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  |  * \param r_mat can be from ``self->matrix`` or not. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | static bool matrix_invert_internal(const MatrixObject *self, float *r_mat) | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | { | 
					
						
							|  |  |  | 	float det; | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 	BLI_assert(self->num_col == self->num_row); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	det = matrix_determinant_internal(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (det != 0.0f) { | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 		matrix_invert_with_det_n_internal(r_mat, self->matrix, det, self->num_col); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Similar to ``matrix_invert_internal`` but should never error. | 
					
						
							|  |  |  |  * \param r_mat can be from ``self->matrix`` or not. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void matrix_invert_safe_internal(const MatrixObject *self, float *r_mat) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float det; | 
					
						
							|  |  |  | 	float *in_mat = self->matrix; | 
					
						
							|  |  |  | 	BLI_assert(self->num_col == self->num_row); | 
					
						
							|  |  |  | 	det = matrix_determinant_internal(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (det == 0.0f) { | 
					
						
							|  |  |  | 		const float eps = PSEUDOINVERSE_EPSILON; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* We will copy self->matrix into r_mat (if needed), and modify it in place to add diagonal epsilon. */ | 
					
						
							|  |  |  | 		in_mat = r_mat; | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		switch (self->num_col) { | 
					
						
							|  |  |  | 			case 2: | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 				float (*mat)[2] = (float (*)[2])in_mat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (in_mat != self->matrix) { | 
					
						
							|  |  |  | 					copy_m2_m2(mat, (float (*)[2])self->matrix); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				mat[0][0] += eps; | 
					
						
							|  |  |  | 				mat[1][1] += eps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (UNLIKELY((det = determinant_m2(mat[0][0], mat[0][1], mat[1][0], mat[1][1])) == 0.0f)) { | 
					
						
							|  |  |  | 					unit_m2(mat); | 
					
						
							|  |  |  | 					det = 1.0f; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			case 3: | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 				float (*mat)[3] = (float (*)[3])in_mat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (in_mat != self->matrix) { | 
					
						
							|  |  |  | 					copy_m3_m3(mat, (float (*)[3])self->matrix); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				mat[0][0] += eps; | 
					
						
							|  |  |  | 				mat[1][1] += eps; | 
					
						
							|  |  |  | 				mat[2][2] += eps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (UNLIKELY((det = determinant_m3_array(mat)) == 0.0f)) { | 
					
						
							|  |  |  | 					unit_m3(mat); | 
					
						
							|  |  |  | 					det = 1.0f; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			case 4: | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 				float (*mat)[4] = (float (*)[4])in_mat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (in_mat != self->matrix) { | 
					
						
							|  |  |  | 					copy_m4_m4(mat, (float (*)[4])self->matrix); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				mat[0][0] += eps; | 
					
						
							|  |  |  | 				mat[1][1] += eps; | 
					
						
							|  |  |  | 				mat[2][2] += eps; | 
					
						
							|  |  |  | 				mat[3][3] += eps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (UNLIKELY(det = determinant_m4(mat)) == 0.0f) { | 
					
						
							|  |  |  | 					unit_m4(mat); | 
					
						
							|  |  |  | 					det = 1.0f; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				BLI_assert(0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	matrix_invert_with_det_n_internal(r_mat, in_mat, det, self->num_col); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*-----------------------------METHODS----------------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_quaternion_doc, | 
					
						
							| 
									
										
										
										
											2011-02-06 10:18:20 +00:00
										 |  |  | ".. method:: to_quaternion()\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Return a quaternion representation of the rotation matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: Quaternion representation of the rotation matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Quaternion`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_to_quaternion(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float quat[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	/* must be 3-4 cols, 3-4 rows, square matrix */ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if ((self->num_row < 3) || (self->num_col < 3) || (self->num_row != self->num_col)) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.to_quat(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_row == 3) { | 
					
						
							|  |  |  | 		mat3_to_quat(quat, (float (*)[3])self->matrix); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		mat4_to_quat(quat, (float (*)[4])self->matrix); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Quaternion_CreatePyObject(quat, NULL); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.toEuler() --------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_euler_doc, | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | ".. method:: to_euler(order, euler_compat)\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   Return an Euler representation of the rotation matrix\n" | 
					
						
							|  |  |  | "   (3x3 or 4x4 matrix only).\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg order: Optional rotation order argument in\n" | 
					
						
							|  |  |  | "      ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'].\n" | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | "   :type order: string\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "   :arg euler_compat: Optional euler argument the new euler will be made\n" | 
					
						
							|  |  |  | "      compatible with (no axis flipping between them).\n" | 
					
						
							|  |  |  | "      Useful for converting a series of matrices to animation curves.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "   :type euler_compat: :class:`Euler`\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "   :return: Euler representation of the matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Euler`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_to_euler(MatrixObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	const char *order_str = NULL; | 
					
						
							|  |  |  | 	short order = EULER_ORDER_XYZ; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	float eul[3], eul_compatf[3]; | 
					
						
							|  |  |  | 	EulerObject *eul_compat = NULL; | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 	float mat[3][3]; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat)) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (eul_compat) { | 
					
						
							|  |  |  | 		if (BaseMath_ReadCallback(eul_compat) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-25 10:11:37 +00:00
										 |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-25 20:47:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-25 23:33:09 +00:00
										 |  |  | 		copy_v3_v3(eul_compatf, eul_compat->eul); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix */ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	if (self->num_row == 3 && self->num_col == 3) { | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 		copy_m3_m3(mat, (float (*)[3])self->matrix); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	else if (self->num_row == 4 && self->num_col == 4) { | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 		copy_m3_m4(mat, (float (*)[4])self->matrix); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.to_euler(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (order_str) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		order = euler_order_from_string(order_str, "Matrix.to_euler()"); | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (order == -1) | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 	normalize_m3(mat); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (eul_compat) { | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 		if (order == 1) mat3_normalized_to_compatible_eul(eul, eul_compatf, mat); | 
					
						
							|  |  |  | 		else            mat3_normalized_to_compatible_eulO(eul, eul_compatf, order, mat); | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2015-10-24 07:02:51 +11:00
										 |  |  | 		if (order == 1) mat3_normalized_to_eul(eul, mat); | 
					
						
							|  |  |  | 		else            mat3_normalized_to_eulO(eul, order, mat); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-20 19:49:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Euler_CreatePyObject(eul, order, NULL); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_resize_4x4_doc, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | ".. method:: resize_4x4()\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Resize the matrix to 4x4.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_resize_4x4(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float mat[4][4]; | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 	int col; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	if (self->flag & BASE_MATH_FLAG_IS_WRAP) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.resize_4x4(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "cannot resize wrapped data - make a copy and resize that"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (self->cb_user) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.resize_4x4(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "cannot resize owned data - make a copy and resize that"); | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	self->matrix = PyMem_Realloc(self->matrix, (sizeof(float) * (MATRIX_MAX_DIM * MATRIX_MAX_DIM))); | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->matrix == NULL) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_MemoryError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.resize_4x4(): " | 
					
						
							|  |  |  | 		                "problem allocating pointer space"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	unit_m4(mat); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 	for (col = 0; col < self->num_col; col++) { | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | 		memcpy(mat[col], MATRIX_COL_PTR(self, col), self->num_row * sizeof(float)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	copy_m4_m4((float (*)[4])self->matrix, (float (*)[4])mat); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	self->num_col = 4; | 
					
						
							|  |  |  | 	self->num_row = 4; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_4x4_doc, | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | ".. method:: to_4x4()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return a 4x4 copy of this matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: a new matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_to_4x4(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	if (self->num_row == 4 && self->num_col == 4) { | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		return Matrix_CreatePyObject(self->matrix, 4, 4, Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	else if (self->num_row == 3 && self->num_col == 3) { | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 		float mat[4][4]; | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		copy_m4_m3(mat, (float (*)[3])self->matrix); | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		return Matrix_CreatePyObject((float *)mat, 4, 4, Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/* TODO, 2x2 matrix */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 	PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 	                "Matrix.to_4x4(): " | 
					
						
							|  |  |  | 	                "inappropriate matrix size"); | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_3x3_doc, | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | ".. method:: to_3x3()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return a 3x3 copy of this matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: a new matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_to_3x3(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 	float mat[3][3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if ((self->num_row < 3) || (self->num_col < 3)) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.to_3x3(): inappropriate matrix size"); | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 	matrix_as_3x3(mat, self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject((float *)mat, 3, 3, Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_translation_doc, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | ".. method:: to_translation()\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   Return the translation part of a 4 row matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   :return: Return the translation of a matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-01-07 06:39:48 +00:00
										 |  |  | "   :rtype: :class:`Vector`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_to_translation(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if ((self->num_row < 3) || self->num_col < 4) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.to_translation(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Vector_CreatePyObject(MATRIX_COL_PTR(self, 3), 3, NULL); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_to_scale_doc, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | ".. method:: to_scale()\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   Return the scale part of a 3x3 or 4x4 matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   :return: Return the scale of a matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "   :rtype: :class:`Vector`\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. note:: This method does not return a negative scale on any axis because it is not possible to obtain this data from the matrix alone.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_to_scale(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 	float rot[3][3]; | 
					
						
							|  |  |  | 	float mat[3][3]; | 
					
						
							|  |  |  | 	float size[3]; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix */ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if ((self->num_row < 3) || (self->num_col < 3)) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.to_scale(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size, 3x3 minimum size"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	matrix_as_3x3(mat, self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* compatible mat4_to_loc_rot_size */ | 
					
						
							|  |  |  | 	mat3_to_rot_size(rot, size, mat); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Vector_CreatePyObject(size, 3, NULL); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.invert() ---------------------*/ | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* re-usable checks for invert */ | 
					
						
							|  |  |  | static bool matrix_invert_is_compat(const MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 		                "Matrix.invert(ed): " | 
					
						
							|  |  |  | 		                "only square matrices are supported"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool matrix_invert_args_check(const MatrixObject *self, PyObject *args, bool check_type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (PyTuple_GET_SIZE(args)) { | 
					
						
							|  |  |  | 		case 0: | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		case 1: | 
					
						
							|  |  |  | 			if (check_type) { | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:05 +10:00
										 |  |  | 				const MatrixObject *fallback = (MatrixObject *)PyTuple_GET_ITEM(args, 0); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 				if (!MatrixObject_Check(fallback)) { | 
					
						
							|  |  |  | 					PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 					                "Matrix.invert: " | 
					
						
							|  |  |  | 					                "expects a matrix argument or nothing"); | 
					
						
							|  |  |  | 					return false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if ((self->num_col != fallback->num_col) || | 
					
						
							|  |  |  | 				    (self->num_row != fallback->num_row)) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 					                "Matrix.invert: " | 
					
						
							|  |  |  | 					                "matrix argument has different dimensions"); | 
					
						
							|  |  |  | 					return false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 			                "Matrix.invert(ed): " | 
					
						
							|  |  |  | 			                "takes at most one argument"); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void matrix_invert_raise_degenerate(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 	                "Matrix.invert(ed): " | 
					
						
							|  |  |  | 	                "matrix does not have an inverse"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_invert_doc, | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | ".. method:: invert(fallback=None)\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Set the matrix to its inverse.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   :arg fallback: Set the matrix to this value when the inverse cannot be calculated\n" | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | "      (instead of raising a :exc:`ValueError` exception).\n" | 
					
						
							|  |  |  | "   :type fallback: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Inverse matrix <https://en.wikipedia.org/wiki/Inverse_matrix>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | static PyObject *Matrix_invert(MatrixObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	if (matrix_invert_is_compat(self) == false) { | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	if (matrix_invert_args_check(self, args, true) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	if (matrix_invert_internal(self, self->matrix)) { | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 		/* pass */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		if (PyTuple_GET_SIZE(args) == 1) { | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:05 +10:00
										 |  |  | 			MatrixObject *fallback = (MatrixObject *)PyTuple_GET_ITEM(args, 0); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (BaseMath_ReadCallback(fallback) == -1) | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 				return NULL; | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 			if (self != fallback) { | 
					
						
							|  |  |  | 				matrix_copy(self, fallback); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 		else { | 
					
						
							|  |  |  | 			matrix_invert_raise_degenerate(); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 12:11:09 +00:00
										 |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_inverted_doc, | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | ".. method:: inverted(fallback=None)\n" | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   Return an inverted copy of the matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2015-03-06 18:50:58 +11:00
										 |  |  | "   :arg fallback: return this when the inverse can't be calculated\n" | 
					
						
							|  |  |  | "      (instead of raising a :exc:`ValueError`).\n" | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | "   :type fallback: any\n" | 
					
						
							|  |  |  | "   :return: the inverted matrix or fallback when given.\n" | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | static PyObject *Matrix_inverted(MatrixObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matrix_invert_args_check(self, args, false) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matrix_invert_is_compat(self) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	if (matrix_invert_internal(self, mat)) { | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 		/* pass */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		if (PyTuple_GET_SIZE(args) == 1) { | 
					
						
							|  |  |  | 			PyObject *fallback = PyTuple_GET_ITEM(args, 0); | 
					
						
							|  |  |  | 			Py_INCREF(fallback); | 
					
						
							|  |  |  | 			return fallback; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			matrix_invert_raise_degenerate(); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	return Matrix_copy_notest(self, mat); | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *Matrix_inverted_noargs(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matrix_invert_is_compat(self) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	if (matrix_invert_internal(self, self->matrix)) { | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 		/* pass */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		matrix_invert_raise_degenerate(); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | PyDoc_STRVAR(Matrix_invert_safe_doc, | 
					
						
							|  |  |  | ".. method:: invert_safe()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Set the matrix to its inverse, will never error.\n" | 
					
						
							|  |  |  | "   If degenerated (e.g. zero scale on an axis), add some epsilon to its diagonal, to get an invertible one.\n" | 
					
						
							|  |  |  | "   If tweaked matrix is still degenerated, set to the identity matrix instead.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Inverse Matrix <https://en.wikipedia.org/wiki/Inverse_matrix>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_invert_safe(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matrix_invert_is_compat(self) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	matrix_invert_safe_internal(self, self->matrix); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_inverted_safe_doc, | 
					
						
							|  |  |  | ".. method:: inverted_safe()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return an inverted copy of the matrix, will never error.\n" | 
					
						
							|  |  |  | "   If degenerated (e.g. zero scale on an axis), add some epsilon to its diagonal, to get an invertible one.\n" | 
					
						
							|  |  |  | "   If tweaked matrix is still degenerated, return the identity matrix instead.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: the inverted matrix.\n" | 
					
						
							|  |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_inverted_safe(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (matrix_invert_is_compat(self) == false) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	matrix_invert_safe_internal(self, mat); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Matrix_copy_notest(self, mat); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | /*---------------------------matrix.adjugate() ---------------------*/ | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_adjugate_doc, | 
					
						
							|  |  |  | ".. method:: adjugate()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Set the matrix to its adjugate.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. note:: When the matrix cannot be adjugated a :exc:`ValueError` exception is raised.\n" | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Adjugate matrix <https://en.wikipedia.org/wiki/Adjugate_matrix>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_adjugate(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 		                "Matrix.adjugate(d): " | 
					
						
							|  |  |  | 		                "only square matrices are supported"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* calculate the classical adjoint */ | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 	if (self->num_col <= 4) { | 
					
						
							|  |  |  | 		adjoint_matrix_n(self->matrix, self->matrix, self->num_col); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2014-09-08 11:22:40 +10:00
										 |  |  | 		PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  | 		             "Matrix adjugate(d): size (%d) unsupported", | 
					
						
							|  |  |  | 		             (int)self->num_col); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_adjugated_doc, | 
					
						
							|  |  |  | ".. method:: adjugated()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return an adjugated copy of the matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: the adjugated matrix.\n" | 
					
						
							|  |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   .. note:: When the matrix cant be adjugated a :exc:`ValueError` exception is raised.\n" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_adjugated(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return matrix__apply_to_copy((PyNoArgsFunction)Matrix_adjugate, self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_rotate_doc, | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | ".. method:: rotate(other)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   Rotates the matrix by another mathutils value.\n" | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   :arg other: rotation component of mathutils value\n" | 
					
						
							|  |  |  | "   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   .. note:: If any of the columns are not unit length this may not have desired results.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | static PyObject *Matrix_rotate(MatrixObject *self, PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float self_rmat[3][3], other_rmat[3][3], rmat[3][3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (mathutils_any_to_rotmat(other_rmat, value, "matrix.rotate(value)") == -1) | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_row != 3 || self->num_col != 3) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.rotate(): " | 
					
						
							|  |  |  | 		                "must have 3x3 dimensions"); | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	matrix_as_3x3(self_rmat, self); | 
					
						
							| 
									
										
										
										
											2011-11-24 19:36:12 +00:00
										 |  |  | 	mul_m3_m3m3(rmat, other_rmat, self_rmat); | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	copy_m3_m3((float (*)[3])(self->matrix), rmat); | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.decompose() ---------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_decompose_doc, | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | ".. method:: decompose()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-04-21 15:48:53 +02:00
										 |  |  | "   Return the translation, rotation and scale components of this matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-04-21 15:48:53 +02:00
										 |  |  | "   :return: trans, rot, scale triple.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: (:class:`Vector`, :class:`Quaternion`, :class:`Vector`)" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_decompose(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *ret; | 
					
						
							|  |  |  | 	float loc[3]; | 
					
						
							|  |  |  | 	float rot[3][3]; | 
					
						
							|  |  |  | 	float quat[4]; | 
					
						
							|  |  |  | 	float size[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_row != 4 || self->num_col != 4) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.decompose(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	mat4_to_loc_rot_size(loc, rot, size, (float (*)[4])self->matrix); | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | 	mat3_to_quat(quat, rot); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	ret = PyTuple_New(3); | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | 	PyTuple_SET_ITEMS(ret, | 
					
						
							|  |  |  | 	        Vector_CreatePyObject(loc, 3, NULL), | 
					
						
							|  |  |  | 	        Quaternion_CreatePyObject(quat, NULL), | 
					
						
							|  |  |  | 	        Vector_CreatePyObject(size, 3, NULL)); | 
					
						
							| 
									
										
										
										
											2010-10-26 12:48:07 +00:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_lerp_doc, | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | ".. function:: lerp(other, factor)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-10-17 02:20:53 +00:00
										 |  |  | "   Returns the interpolation of two matrices.\n" | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "   :arg other: value to interpolate with.\n" | 
					
						
							|  |  |  | "   :type other: :class:`Matrix`\n" | 
					
						
							|  |  |  | "   :arg factor: The interpolation value in [0.0, 1.0].\n" | 
					
						
							|  |  |  | "   :type factor: float\n" | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:41 +11:00
										 |  |  | "   :return: The interpolated matrix.\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_lerp(MatrixObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *mat2 = NULL; | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	float fac, mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "O!f:lerp", &matrix_Type, &mat2, &fac)) | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col != mat2->num_col || self->num_row != mat2->num_row) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.lerp(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "expects both matrix objects of the same dimensions"); | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1 || BaseMath_ReadCallback(mat2) == -1) | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* TODO, different sized matrix */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	if (self->num_col == 4 && self->num_row == 4) { | 
					
						
							| 
									
										
										
										
											2017-05-26 17:00:20 +10:00
										 |  |  | 		interp_m4_m4m4((float (*)[4])mat, (float (*)[4])self->matrix, (float (*)[4])mat2->matrix, fac); | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	else if (self->num_col == 3 && self->num_row == 3) { | 
					
						
							| 
									
										
										
										
											2017-05-26 17:00:20 +10:00
										 |  |  | 		interp_m3_m3m3((float (*)[3])mat, (float (*)[3])self->matrix, (float (*)[3])mat2->matrix, fac); | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.lerp(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "only 3x3 and 4x4 matrices supported"); | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, self->num_col, self->num_row, Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2010-11-22 10:39:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.determinant() ----------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_determinant_doc, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | ".. method:: determinant()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return the determinant of a matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2014-12-19 00:42:50 +09:00
										 |  |  | "   :return: Return the determinant of a matrix.\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "   :rtype: float\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Determinant <https://en.wikipedia.org/wiki/Determinant>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_determinant(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.determinant(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "only square matrices are supported"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	return PyFloat_FromDouble((double)matrix_determinant_internal(self)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.transpose() ------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_transpose_doc, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | ".. method:: transpose()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Set the matrix to its transpose.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Transpose <https://en.wikipedia.org/wiki/Transpose>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_transpose(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.transpose(d): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "only square matrices are supported"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col == 2) { | 
					
						
							| 
									
										
										
										
											2011-12-19 03:12:10 +00:00
										 |  |  | 		const float t = MATRIX_ITEM(self, 1, 0); | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, 1, 0) = MATRIX_ITEM(self, 0, 1); | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, 0, 1) = t; | 
					
						
							| 
									
										
										
										
											2011-11-16 03:56:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	else if (self->num_col == 3) { | 
					
						
							|  |  |  | 		transpose_m3((float (*)[3])self->matrix); | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		transpose_m4((float (*)[4])self->matrix); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 12:11:09 +00:00
										 |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_transposed_doc, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | ".. method:: transposed()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Return a new, transposed matrix.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :return: a transposed matrix\n" | 
					
						
							|  |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_transposed(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-09 09:20:17 +00:00
										 |  |  | 	return matrix__apply_to_copy((PyNoArgsFunction)Matrix_transpose, self); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | /*---------------------------matrix.normalize() ------------------*/ | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_normalize_doc, | 
					
						
							|  |  |  | ".. method:: normalize()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 10:28:52 +00:00
										 |  |  | "   Normalize each of the matrix columns.\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_normalize(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 		                "Matrix.normalize(): " | 
					
						
							|  |  |  | 		                "only square matrices are supported"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (self->num_col == 3) { | 
					
						
							|  |  |  | 		normalize_m3((float (*)[3])self->matrix); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (self->num_col == 4) { | 
					
						
							|  |  |  | 		normalize_m4((float (*)[4])self->matrix); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 		                "Matrix.normalize(): " | 
					
						
							|  |  |  | 		                "can only use a 3x3 or 4x4 matrix"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_normalized_doc, | 
					
						
							|  |  |  | ".. method:: normalized()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | "   Return a column normalized matrix\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | "   :return: a column normalized matrix\n" | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_normalized(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return matrix__apply_to_copy((PyNoArgsFunction)Matrix_normalize, self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.zero() -----------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_zero_doc, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | ".. method:: zero()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Set all the matrix values to zero.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_zero(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_Prepare_ForWrite(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 17:08:29 +10:00
										 |  |  | 	copy_vn_fl(self->matrix, self->num_col * self->num_row, 0.0f); | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_WriteCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2014-12-28 15:13:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | /*---------------------------matrix.identity(() ------------------*/ | 
					
						
							| 
									
										
										
										
											2014-12-28 15:13:01 +11:00
										 |  |  | static void matrix_identity_internal(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BLI_assert((self->num_col == self->num_row) && (self->num_row <= 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (self->num_col == 2) { | 
					
						
							|  |  |  | 		unit_m2((float (*)[2])self->matrix); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (self->num_col == 3) { | 
					
						
							|  |  |  | 		unit_m3((float (*)[3])self->matrix); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		unit_m4((float (*)[4])self->matrix); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_identity_doc, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | ".. method:: identity()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Set the matrix to the identity matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. note:: An object with a location and rotation of zero, and a scale of one\n" | 
					
						
							| 
									
										
										
										
											2011-05-26 19:13:01 +00:00
										 |  |  | "      will have an identity matrix.\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2017-03-22 14:49:12 -04:00
										 |  |  | "   .. seealso:: `Identity matrix <https://en.wikipedia.org/wiki/Identity_matrix>` on Wikipedia.\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_identity(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_col != self->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.identity(): " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "only square matrices are supported"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 15:13:01 +11:00
										 |  |  | 	matrix_identity_internal(self); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_WriteCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | /*---------------------------Matrix.copy() ------------------*/ | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject *Matrix_copy_notest(MatrixObject *self, const float *matrix) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject((float *)matrix, self->num_col, self->num_row, Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_copy_doc, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | ".. method:: copy()\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   Returns a copy of this matrix.\n" | 
					
						
							| 
									
										
										
										
											2010-01-27 21:33:39 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | "   :return: an instance of itself\n" | 
					
						
							| 
									
										
										
										
											2011-02-04 09:35:20 +00:00
										 |  |  | "   :rtype: :class:`Matrix`\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | static PyObject *Matrix_copy(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 20:41:28 +10:00
										 |  |  | 	return Matrix_copy_notest(self, self->matrix); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-03-30 11:35:58 +00:00
										 |  |  | static PyObject *Matrix_deepcopy(MatrixObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!mathutils_deepcopy_args_check(args)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	return Matrix_copy(self); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*----------------------------print object (internal)-------------*/ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | /* print the object to screen */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_repr(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 	int col, row; | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	PyObject *rows[MATRIX_MAX_DIM] = {NULL}; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2010-04-19 22:02:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	for (row = 0; row < self->num_row; row++) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		rows[row] = PyTuple_New(self->num_col); | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		for (col = 0; col < self->num_col; col++) { | 
					
						
							|  |  |  | 			PyTuple_SET_ITEM(rows[row], col, PyFloat_FromDouble(MATRIX_ITEM(self, row, col))); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	switch (self->num_row) { | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 		case 2: return PyUnicode_FromFormat("Matrix((%R,\n" | 
					
						
							|  |  |  | 		                                    "        %R))", rows[0], rows[1]); | 
					
						
							| 
									
										
										
											
												bugfix [#24665] mathutils.Matrix initialization is counter-intuitive and generates bugs
was printing transposed, also nicer printing.
>>> from mathutils import Matrix
>>> Matrix()
Matrix((1.0, 0.0, 0.0, 0.0),
       (0.0, 1.0, 0.0, 0.0),
       (0.0, 0.0, 1.0, 0.0),
       (0.0, 0.0, 0.0, 1.0))
was...
Matrix((1.000000, 0.000000, 0.000000, 0.000000), (0.000000, 1.000000, 0.000000, 0.000000), (0.000000, 0.000000, 1.000000, 0.000000), (0.000000, 0.000000, 0.000000, 1.000000))
											
										 
											2010-11-12 02:50:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 		case 3: return PyUnicode_FromFormat("Matrix((%R,\n" | 
					
						
							|  |  |  | 		                                    "        %R,\n" | 
					
						
							|  |  |  | 		                                    "        %R))", rows[0], rows[1], rows[2]); | 
					
						
							| 
									
										
										
											
												bugfix [#24665] mathutils.Matrix initialization is counter-intuitive and generates bugs
was printing transposed, also nicer printing.
>>> from mathutils import Matrix
>>> Matrix()
Matrix((1.0, 0.0, 0.0, 0.0),
       (0.0, 1.0, 0.0, 0.0),
       (0.0, 0.0, 1.0, 0.0),
       (0.0, 0.0, 0.0, 1.0))
was...
Matrix((1.000000, 0.000000, 0.000000, 0.000000), (0.000000, 1.000000, 0.000000, 0.000000), (0.000000, 0.000000, 1.000000, 0.000000), (0.000000, 0.000000, 0.000000, 1.000000))
											
										 
											2010-11-12 02:50:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 		case 4: return PyUnicode_FromFormat("Matrix((%R,\n" | 
					
						
							|  |  |  | 		                                    "        %R,\n" | 
					
						
							|  |  |  | 		                                    "        %R,\n" | 
					
						
							|  |  |  | 		                                    "        %R))", rows[0], rows[1], rows[2], rows[3]); | 
					
						
							| 
									
										
										
											
												bugfix [#24665] mathutils.Matrix initialization is counter-intuitive and generates bugs
was printing transposed, also nicer printing.
>>> from mathutils import Matrix
>>> Matrix()
Matrix((1.0, 0.0, 0.0, 0.0),
       (0.0, 1.0, 0.0, 0.0),
       (0.0, 0.0, 1.0, 0.0),
       (0.0, 0.0, 0.0, 1.0))
was...
Matrix((1.000000, 0.000000, 0.000000, 0.000000), (0.000000, 1.000000, 0.000000, 0.000000), (0.000000, 0.000000, 1.000000, 0.000000), (0.000000, 0.000000, 0.000000, 1.000000))
											
										 
											2010-11-12 02:50:57 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-21 02:00:29 +00:00
										 |  |  | 	Py_FatalError("Matrix(): invalid row size!"); | 
					
						
							| 
									
										
										
											
												bugfix [#24665] mathutils.Matrix initialization is counter-intuitive and generates bugs
was printing transposed, also nicer printing.
>>> from mathutils import Matrix
>>> Matrix()
Matrix((1.0, 0.0, 0.0, 0.0),
       (0.0, 1.0, 0.0, 0.0),
       (0.0, 0.0, 1.0, 0.0),
       (0.0, 0.0, 0.0, 1.0))
was...
Matrix((1.000000, 0.000000, 0.000000, 0.000000), (0.000000, 1.000000, 0.000000, 0.000000), (0.000000, 0.000000, 1.000000, 0.000000), (0.000000, 0.000000, 0.000000, 1.000000))
											
										 
											2010-11-12 02:50:57 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
											
												bugfix [#24665] mathutils.Matrix initialization is counter-intuitive and generates bugs
was printing transposed, also nicer printing.
>>> from mathutils import Matrix
>>> Matrix()
Matrix((1.0, 0.0, 0.0, 0.0),
       (0.0, 1.0, 0.0, 0.0),
       (0.0, 0.0, 1.0, 0.0),
       (0.0, 0.0, 0.0, 1.0))
was...
Matrix((1.000000, 0.000000, 0.000000, 0.000000), (0.000000, 1.000000, 0.000000, 0.000000), (0.000000, 0.000000, 1.000000, 0.000000), (0.000000, 0.000000, 0.000000, 1.000000))
											
										 
											2010-11-12 02:50:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #ifndef MATH_STANDALONE
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static PyObject *Matrix_str(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	DynStr *ds; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 03:37:55 +00:00
										 |  |  | 	int maxsize[MATRIX_MAX_DIM]; | 
					
						
							|  |  |  | 	int row, col; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-01 01:42:36 +00:00
										 |  |  | 	char dummy_buf[64]; | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	ds = BLI_dynstr_new(); | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* First determine the maximum width for each column */ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	for (col = 0; col < self->num_col; col++) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		maxsize[col] = 0; | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		for (row = 0; row < self->num_row; row++) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			int size = BLI_snprintf(dummy_buf, sizeof(dummy_buf), "%.4f", MATRIX_ITEM(self, row, col)); | 
					
						
							| 
									
										
										
										
											2012-10-23 16:21:55 +00:00
										 |  |  | 			maxsize[col] = max_ii(maxsize[col], size); | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Now write the unicode string to be printed */ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	BLI_dynstr_appendf(ds, "<Matrix %dx%d (", self->num_row, self->num_col); | 
					
						
							|  |  |  | 	for (row = 0; row < self->num_row; row++) { | 
					
						
							|  |  |  | 		for (col = 0; col < self->num_col; col++) { | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 			BLI_dynstr_appendf(ds, col ? ", %*.4f" : "%*.4f", maxsize[col], MATRIX_ITEM(self, row, col)); | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-02-01 01:42:36 +00:00
										 |  |  | 		BLI_dynstr_append(ds, row + 1 != self->num_row ? ")\n            (" : ")"); | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	BLI_dynstr_append(ds, ">"); | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 03:37:55 +00:00
										 |  |  | 	return mathutils_dynstr_to_py(ds); /* frees ds */ | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-12-20 02:54:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static PyObject *Matrix_richcmpr(PyObject *a, PyObject *b, int op) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-05 10:40:42 +00:00
										 |  |  | 	PyObject *res; | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	int ok = -1; /* zero is true */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-05 10:40:42 +00:00
										 |  |  | 	if (MatrixObject_Check(a) && MatrixObject_Check(b)) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		MatrixObject *matA = (MatrixObject *)a; | 
					
						
							|  |  |  | 		MatrixObject *matB = (MatrixObject *)b; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (BaseMath_ReadCallback(matA) == -1 || BaseMath_ReadCallback(matB) == -1) | 
					
						
							| 
									
										
										
										
											2011-02-05 10:40:42 +00:00
										 |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 		ok = ((matA->num_row == matB->num_row) && | 
					
						
							|  |  |  | 		      (matA->num_col == matB->num_col) && | 
					
						
							|  |  |  | 		      EXPP_VectorsAreEqual(matA->matrix, matB->matrix, (matA->num_col * matA->num_row), 1) | 
					
						
							|  |  |  | 		      ) ? 0 : -1; | 
					
						
							| 
									
										
										
										
											2011-02-05 10:40:42 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (op) { | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 		case Py_NE: | 
					
						
							| 
									
										
										
										
											2013-07-21 08:16:37 +00:00
										 |  |  | 			ok = !ok; | 
					
						
							| 
									
										
										
										
											2017-05-20 14:01:03 +10:00
										 |  |  | 			ATTR_FALLTHROUGH; | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 		case Py_EQ: | 
					
						
							|  |  |  | 			res = ok ? Py_False : Py_True; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case Py_LT: | 
					
						
							|  |  |  | 		case Py_LE: | 
					
						
							|  |  |  | 		case Py_GT: | 
					
						
							|  |  |  | 		case Py_GE: | 
					
						
							|  |  |  | 			res = Py_NotImplemented; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			PyErr_BadArgument(); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-05 10:40:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | 	return Py_INCREF_RET(res); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-30 00:42:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 10:46:14 +11:00
										 |  |  | static Py_hash_t Matrix_hash(MatrixObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-22 17:12:54 +01:00
										 |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2015-02-15 10:46:14 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMathObject_Prepare_ForHash(self) == -1) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	matrix_transpose_internal(mat, self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return mathutils_array_hash(mat, self->num_row * self->num_col); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*---------------------SEQUENCE PROTOCOLS------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * ----------------------------len(object)------------------------ | 
					
						
							|  |  |  |  * sequence length */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static int Matrix_len(MatrixObject *self) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-07-03 21:03:39 +00:00
										 |  |  | 	return self->num_row; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*----------------------------object[]---------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * sequence accessor (get) | 
					
						
							|  |  |  |  * the wrapped vector gives direct access to the matrix data */ | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | static PyObject *Matrix_item_row(MatrixObject *self, int row) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	if (row < 0 || row >= self->num_row) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_IndexError, | 
					
						
							|  |  |  | 		                "matrix[attribute]: " | 
					
						
							|  |  |  | 		                "array index out of range"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-24 06:13:58 +00:00
										 |  |  | 	return Vector_CreatePyObject_cb((PyObject *)self, self->num_col, mathutils_matrix_row_cb_index, row); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | /* same but column access */ | 
					
						
							|  |  |  | static PyObject *Matrix_item_col(MatrixObject *self, int col) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (col < 0 || col >= self->num_col) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_IndexError, | 
					
						
							|  |  |  | 		                "matrix[attribute]: " | 
					
						
							|  |  |  | 		                "array index out of range"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return Vector_CreatePyObject_cb((PyObject *)self, self->num_row, mathutils_matrix_col_cb_index, col); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*----------------------------object[]-------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * sequence accessor (set) */ | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | static int Matrix_ass_item_row(MatrixObject *self, int row, PyObject *value) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	int col; | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float vec[MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	if (row >= self->num_row || row < 0) { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		PyErr_SetString(PyExc_IndexError, | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		                "matrix[attribute] = x: bad row"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 10:37:39 +10:00
										 |  |  | 	if (mathutils_array_parse(vec, self->num_col, self->num_col, value, "matrix[i] = value assignment") == -1) { | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	/* Since we are assigning a row we cannot memcpy */ | 
					
						
							|  |  |  | 	for (col = 0; col < self->num_col; col++) { | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, row, col) = vec[col]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | static int Matrix_ass_item_col(MatrixObject *self, int col, PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int row; | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float vec[MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (col >= self->num_col || col < 0) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_IndexError, | 
					
						
							|  |  |  | 		                "matrix[attribute] = x: bad col"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 10:37:39 +10:00
										 |  |  | 	if (mathutils_array_parse(vec, self->num_row, self->num_row, value, "matrix[i] = value assignment") == -1) { | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Since we are assigning a row we cannot memcpy */ | 
					
						
							|  |  |  | 	for (row = 0; row < self->num_row; row++) { | 
					
						
							|  |  |  | 		MATRIX_ITEM(self, row, col) = vec[row]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*----------------------------object[z:y]------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * sequence slice (get)*/ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_slice(MatrixObject *self, int begin, int end) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-24 03:51:34 +00:00
										 |  |  | 	PyObject *tuple; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	int count; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	CLAMP(begin, 0, self->num_row); | 
					
						
							|  |  |  | 	CLAMP(end, 0, self->num_row); | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	begin = MIN2(begin, end); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	tuple = PyTuple_New(end - begin); | 
					
						
							|  |  |  | 	for (count = begin; count < end; count++) { | 
					
						
							| 
									
										
										
										
											2010-12-24 03:51:34 +00:00
										 |  |  | 		PyTuple_SET_ITEM(tuple, count - begin, | 
					
						
							| 
									
										
										
										
											2012-03-17 22:31:57 +00:00
										 |  |  | 		                 Vector_CreatePyObject_cb((PyObject *)self, self->num_col, mathutils_matrix_row_cb_index, count)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-24 03:51:34 +00:00
										 |  |  | 	return tuple; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*----------------------------object[z:y]------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * sequence slice (set)*/ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-29 09:58:10 +10:00
										 |  |  | 	PyObject *value_fast; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 	CLAMP(begin, 0, self->num_row); | 
					
						
							|  |  |  | 	CLAMP(end, 0, self->num_row); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	begin = MIN2(begin, end); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-09 12:38:22 +00:00
										 |  |  | 	/* non list/tuple cases */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	if (!(value_fast = PySequence_Fast(value, "matrix[begin:end] = value"))) { | 
					
						
							| 
									
										
										
										
											2011-01-09 12:38:22 +00:00
										 |  |  | 		/* PySequence_Fast sets the error */ | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2015-07-29 09:58:10 +10:00
										 |  |  | 		PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast); | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		const int size = end - begin; | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		int row, col; | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 		float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		float vec[4]; | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (PySequence_Fast_GET_SIZE(value_fast) != size) { | 
					
						
							| 
									
										
										
										
											2011-01-09 12:38:22 +00:00
										 |  |  | 			Py_DECREF(value_fast); | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 			                "matrix[begin:end] = []: " | 
					
						
							|  |  |  | 			                "size mismatch in slice assignment"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		memcpy(mat, self->matrix, self->num_col * self->num_row * sizeof(float)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		/* parse sub items */ | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		for (row = begin; row < end; row++) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			/* parse each sub sequence */ | 
					
						
							| 
									
										
										
										
											2015-07-29 09:58:10 +10:00
										 |  |  | 			PyObject *item = value_fast_items[row - begin]; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 			if (mathutils_array_parse(vec, self->num_col, self->num_col, item, | 
					
						
							| 
									
										
										
										
											2015-07-29 10:37:39 +10:00
										 |  |  | 			                          "matrix[begin:end] = value assignment") == -1) | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2015-07-29 09:49:02 +10:00
										 |  |  | 				Py_DECREF(value_fast); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 				return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			for (col = 0; col < self->num_col; col++) { | 
					
						
							|  |  |  | 				mat[col * self->num_row + row] = vec[col]; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-09 12:38:22 +00:00
										 |  |  | 		Py_DECREF(value_fast); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		/*parsed well - now set in matrix*/ | 
					
						
							| 
									
										
										
										
											2011-12-22 01:05:03 +00:00
										 |  |  | 		memcpy(self->matrix, mat, self->num_col * self->num_row * sizeof(float)); | 
					
						
							| 
									
										
										
										
											2011-01-09 12:09:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 12:11:09 +00:00
										 |  |  | 		(void)BaseMath_WriteCallback(self); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*------------------------NUMERIC PROTOCOLS----------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  *------------------------obj + obj------------------------------*/ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_add(PyObject *m1, PyObject *m2) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	MatrixObject *mat1 = NULL, *mat2 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	mat1 = (MatrixObject *)m1; | 
					
						
							|  |  |  | 	mat2 = (MatrixObject *)m2; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) { | 
					
						
							| 
									
										
										
										
											2011-11-13 09:20:04 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "Matrix addition: (%s + %s) " | 
					
						
							|  |  |  | 		             "invalid type for this operation", | 
					
						
							|  |  |  | 		             Py_TYPE(m1)->tp_name, Py_TYPE(m2)->tp_name); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (mat1->num_col != mat2->num_col || mat1->num_row != mat2->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "Matrix addition: " | 
					
						
							|  |  |  | 		                "matrices must have the same dimensions for this operation"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	add_vn_vnvn(mat, mat1->matrix, mat2->matrix, mat1->num_col * mat1->num_row); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, mat1->num_col, mat1->num_row, Py_TYPE(mat1)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*------------------------obj - obj------------------------------
 | 
					
						
							| 
									
										
										
										
											2012-03-03 20:36:09 +00:00
										 |  |  |  * subtraction */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | static PyObject *Matrix_sub(PyObject *m1, PyObject *m2) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	MatrixObject *mat1 = NULL, *mat2 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	mat1 = (MatrixObject *)m1; | 
					
						
							|  |  |  | 	mat2 = (MatrixObject *)m2; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) { | 
					
						
							| 
									
										
										
										
											2011-11-13 09:20:04 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "Matrix subtraction: (%s - %s) " | 
					
						
							|  |  |  | 		             "invalid type for this operation", | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 		             Py_TYPE(m1)->tp_name, Py_TYPE(m2)->tp_name); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (mat1->num_col != mat2->num_col || mat1->num_row != mat2->num_row) { | 
					
						
							| 
									
										
										
										
											2012-12-16 04:10:57 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "Matrix addition: " | 
					
						
							|  |  |  | 		                "matrices must have the same dimensions for this operation"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	sub_vn_vnvn(mat, mat1->matrix, mat2->matrix, mat1->num_col * mat1->num_row); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(mat, mat1->num_col, mat1->num_row, Py_TYPE(mat1)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*------------------------obj * obj------------------------------
 | 
					
						
							| 
									
										
										
										
											2012-09-26 20:05:38 +00:00
										 |  |  |  * multiplication */ | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | static PyObject *matrix_mul_float(MatrixObject *mat, const float scalar) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 	float tmat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	mul_vn_vn_fl(tmat, mat->matrix, mat->num_col * mat->num_row, scalar); | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	return Matrix_CreatePyObject(tmat, mat->num_col, mat->num_row, Py_TYPE(mat)); | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +00:00
										 |  |  | static PyObject *Matrix_mul(PyObject *m1, PyObject *m2) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float scalar; | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 	int vec_size; | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	MatrixObject *mat1 = NULL, *mat2 = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (MatrixObject_Check(m1)) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		mat1 = (MatrixObject *)m1; | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (BaseMath_ReadCallback(mat1) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (MatrixObject_Check(m2)) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		mat2 = (MatrixObject *)m2; | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (BaseMath_ReadCallback(mat2) == -1) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (mat1 && mat2) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		/* MATRIX * MATRIX */ | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 		float mat[MATRIX_MAX_DIM * MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 		int col, row, item; | 
					
						
							| 
									
										
										
										
											2011-07-21 02:00:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 		if (mat1->num_col != mat2->num_row) { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 			                "matrix1 * matrix2: matrix1 number of columns " | 
					
						
							|  |  |  | 			                "and the matrix2 number of rows must be the same"); | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 		for (col = 0; col < mat2->num_col; col++) { | 
					
						
							|  |  |  | 			for (row = 0; row < mat1->num_row; row++) { | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 				double dot = 0.0f; | 
					
						
							| 
									
										
										
										
											2011-12-20 05:48:35 +00:00
										 |  |  | 				for (item = 0; item < mat1->num_col; item++) { | 
					
						
							| 
									
										
										
										
											2012-11-04 07:18:29 +00:00
										 |  |  | 					dot += (double)(MATRIX_ITEM(mat1, row, item) * MATRIX_ITEM(mat2, item, col)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 				mat[(col * mat1->num_row) + row] = (float)dot; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-07-21 02:00:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		return Matrix_CreatePyObject(mat, mat2->num_col, mat1->num_row, Py_TYPE(mat1)); | 
					
						
							| 
									
										
										
										
											2009-06-18 23:12:29 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	else if (mat2) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		/*FLOAT/INT * MATRIX */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		if (((scalar = PyFloat_AsDouble(m1)) == -1.0f && PyErr_Occurred()) == 0) { | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | 			return matrix_mul_float(mat2, scalar); | 
					
						
							| 
									
										
										
										
											2009-06-18 23:12:29 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	else if (mat1) { | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 		/* MATRIX * VECTOR */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (VectorObject_Check(m2)) { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 			VectorObject *vec2 = (VectorObject *)m2; | 
					
						
							| 
									
										
										
										
											2014-04-29 18:12:44 +10:00
										 |  |  | 			float tvec[MATRIX_MAX_DIM]; | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 			if (BaseMath_ReadCallback(vec2) == -1) | 
					
						
							| 
									
										
										
										
											2011-07-25 01:44:19 +00:00
										 |  |  | 				return NULL; | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 			if (column_vector_multiplication(tvec, vec2, mat1) == -1) { | 
					
						
							| 
									
										
										
										
											2011-07-25 01:44:19 +00:00
										 |  |  | 				return NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-21 21:21:27 +00:00
										 |  |  | 			if (mat1->num_col == 4 && vec2->size == 3) { | 
					
						
							|  |  |  | 				vec_size = 3; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				vec_size = mat1->num_row; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 			return Vector_CreatePyObject(tvec, vec_size, Py_TYPE(m2)); | 
					
						
							| 
									
										
										
										
											2011-07-25 01:44:19 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		/*FLOAT/INT * MATRIX */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		else if (((scalar = PyFloat_AsDouble(m2)) == -1.0f && PyErr_Occurred()) == 0) { | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | 			return matrix_mul_float(mat1, scalar); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-01-09 09:16:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-01-09 15:12:08 +00:00
										 |  |  | 		BLI_assert(!"internal error"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 	PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 	             "Matrix multiplication: " | 
					
						
							|  |  |  | 	             "not supported between '%.200s' and '%.200s' types", | 
					
						
							|  |  |  | 	             Py_TYPE(m1)->tp_name, Py_TYPE(m2)->tp_name); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*-----------------PROTOCOL DECLARATIONS--------------------------*/ | 
					
						
							|  |  |  | static PySequenceMethods Matrix_SeqMethods = { | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	(lenfunc) Matrix_len,                       /* sq_length */ | 
					
						
							|  |  |  | 	(binaryfunc) NULL,                          /* sq_concat */ | 
					
						
							|  |  |  | 	(ssizeargfunc) NULL,                        /* sq_repeat */ | 
					
						
							|  |  |  | 	(ssizeargfunc) Matrix_item_row,             /* sq_item */ | 
					
						
							|  |  |  | 	(ssizessizeargfunc) NULL,                   /* sq_slice, deprecated */ | 
					
						
							|  |  |  | 	(ssizeobjargproc) Matrix_ass_item_row,      /* sq_ass_item */ | 
					
						
							|  |  |  | 	(ssizessizeobjargproc) NULL,                /* sq_ass_slice, deprecated */ | 
					
						
							|  |  |  | 	(objobjproc) NULL,                          /* sq_contains */ | 
					
						
							|  |  |  | 	(binaryfunc) NULL,                          /* sq_inplace_concat */ | 
					
						
							|  |  |  | 	(ssizeargfunc) NULL,                        /* sq_inplace_repeat */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-06-28 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static PyObject *Matrix_subscript(MatrixObject *self, PyObject *item) | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (PyIndex_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t i; | 
					
						
							|  |  |  | 		i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
					
						
							|  |  |  | 		if (i == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		if (i < 0) | 
					
						
							| 
									
										
										
										
											2011-12-24 06:13:58 +00:00
										 |  |  | 			i += self->num_row; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 		return Matrix_item_row(self, i); | 
					
						
							| 
									
										
										
										
											2011-11-16 03:56:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else if (PySlice_Check(item)) { | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 		Py_ssize_t start, stop, step, slicelength; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-05 13:48:42 +00:00
										 |  |  | 		if (PySlice_GetIndicesEx(item, self->num_row, &start, &stop, &step, &slicelength) < 0) | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (slicelength <= 0) { | 
					
						
							| 
									
										
										
										
											2011-02-04 03:39:06 +00:00
										 |  |  | 			return PyTuple_New(0); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else if (step == 1) { | 
					
						
							|  |  |  | 			return Matrix_slice(self, start, stop); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_IndexError, | 
					
						
							| 
									
										
										
										
											2011-10-17 02:20:53 +00:00
										 |  |  | 			                "slice steps not supported with matrices"); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 		             "matrix indices must be integers, not %.200s", | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		             Py_TYPE(item)->tp_name); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static int Matrix_ass_subscript(MatrixObject *self, PyObject *item, PyObject *value) | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (PyIndex_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
					
						
							|  |  |  | 		if (i == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		if (i < 0) | 
					
						
							| 
									
										
										
										
											2011-12-24 06:13:58 +00:00
										 |  |  | 			i += self->num_row; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 		return Matrix_ass_item_row(self, i, value); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else if (PySlice_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t start, stop, step, slicelength; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-05 13:48:42 +00:00
										 |  |  | 		if (PySlice_GetIndicesEx(item, self->num_row, &start, &stop, &step, &slicelength) < 0) | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (step == 1) | 
					
						
							|  |  |  | 			return Matrix_ass_slice(self, start, stop, value); | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2011-07-14 09:54:03 +00:00
										 |  |  | 			PyErr_SetString(PyExc_IndexError, | 
					
						
							| 
									
										
										
										
											2011-10-17 02:20:53 +00:00
										 |  |  | 			                "slice steps not supported with matrices"); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "matrix indices must be integers, not %.200s", | 
					
						
							|  |  |  | 		             Py_TYPE(item)->tp_name); | 
					
						
							| 
									
										
										
										
											2009-07-01 13:31:36 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMappingMethods Matrix_AsMapping = { | 
					
						
							|  |  |  | 	(lenfunc)Matrix_len, | 
					
						
							|  |  |  | 	(binaryfunc)Matrix_subscript, | 
					
						
							|  |  |  | 	(objobjargproc)Matrix_ass_subscript | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 13:27:06 +00:00
										 |  |  | static PyNumberMethods Matrix_NumMethods = { | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	(binaryfunc)    Matrix_add,     /*nb_add*/ | 
					
						
							|  |  |  | 	(binaryfunc)    Matrix_sub,     /*nb_subtract*/ | 
					
						
							|  |  |  | 	(binaryfunc)    Matrix_mul,     /*nb_multiply*/ | 
					
						
							|  |  |  | 	NULL,                               /*nb_remainder*/ | 
					
						
							|  |  |  | 	NULL,                               /*nb_divmod*/ | 
					
						
							|  |  |  | 	NULL,                               /*nb_power*/ | 
					
						
							|  |  |  | 	(unaryfunc)     0,      /*nb_negative*/ | 
					
						
							|  |  |  | 	(unaryfunc)     0,      /*tp_positive*/ | 
					
						
							|  |  |  | 	(unaryfunc)     0,      /*tp_absolute*/ | 
					
						
							|  |  |  | 	(inquiry)   0,      /*tp_bool*/ | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	(unaryfunc) Matrix_inverted_noargs,        /*nb_invert*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	NULL,                   /*nb_lshift*/ | 
					
						
							|  |  |  | 	(binaryfunc)0,      /*nb_rshift*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_and*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_xor*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_or*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_int*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_reserved*/ | 
					
						
							|  |  |  | 	NULL,                   /*nb_float*/ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_add */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_subtract */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_multiply */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_remainder */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_power */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_lshift */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_rshift */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_and */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_xor */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_or */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_floor_divide */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_true_divide */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_floor_divide */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_inplace_true_divide */ | 
					
						
							|  |  |  | 	NULL,                   /* nb_index */ | 
					
						
							| 
									
										
										
										
											2009-06-28 13:27:06 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-06-18 23:12:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_translation_doc, | 
					
						
							|  |  |  | "The translation component of the matrix.\n\n:type: Vector" | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | static PyObject *Matrix_translation_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 4x4 square matrix*/ | 
					
						
							|  |  |  | 	if (self->num_row != 4 || self->num_col != 4) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							|  |  |  | 		                "Matrix.translation: " | 
					
						
							|  |  |  | 		                "inappropriate matrix size, must be 4x4"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:05:41 +00:00
										 |  |  | 	ret = (PyObject *)Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_translation_cb_index, 3); | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int Matrix_translation_set(MatrixObject *self, PyObject *value, void *UNUSED(closure)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float tvec[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	if (BaseMath_ReadCallback_ForWrite(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 4x4 square matrix*/ | 
					
						
							|  |  |  | 	if (self->num_row != 4 || self->num_col != 4) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							|  |  |  | 		                "Matrix.translation: " | 
					
						
							|  |  |  | 		                "inappropriate matrix size, must be 4x4"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	if ((mathutils_array_parse(tvec, 3, 3, value, "Matrix.translation")) == -1) { | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2011-12-26 00:42:35 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-21 23:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	copy_v3_v3(((float (*)[4])self->matrix)[3], tvec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(void)BaseMath_WriteCallback(self); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_row_doc, | 
					
						
							| 
									
										
										
										
											2012-02-24 06:44:04 +00:00
										 |  |  | "Access the matix by rows (default), (read-only).\n\n:type: Matrix Access" | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_row_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return MatrixAccess_CreatePyObject(self, MAT_ACCESS_ROW); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_col_doc, | 
					
						
							| 
									
										
										
										
											2012-02-24 06:44:04 +00:00
										 |  |  | "Access the matix by colums, 3x3 and 4x4 only, (read-only).\n\n:type: Matrix Access" | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | ); | 
					
						
							|  |  |  | static PyObject *Matrix_col_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return MatrixAccess_CreatePyObject(self, MAT_ACCESS_COL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(Matrix_median_scale_doc, | 
					
						
							| 
									
										
										
										
											2012-02-24 06:44:04 +00:00
										 |  |  | "The average scale applied to each axis (read-only).\n\n:type: float" | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2010-01-02 22:47:56 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float mat[3][3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2010-01-02 22:47:56 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix*/ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if ((self->num_row < 3) || (self->num_col < 3)) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.median_scale: " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size, 3x3 minimum"); | 
					
						
							| 
									
										
										
										
											2010-01-02 22:47:56 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-25 16:32:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	matrix_as_3x3(mat, self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 22:47:56 +00:00
										 |  |  | 	return PyFloat_FromDouble(mat3_to_scale(mat)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_is_negative_doc, | 
					
						
							| 
									
										
										
										
											2012-02-24 06:44:04 +00:00
										 |  |  | "True if this matrix results in a negative scale, 3x3 and 4x4 only, (read-only).\n\n:type: bool" | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | static PyObject *Matrix_is_negative_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2010-01-30 13:15:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2010-01-30 13:15:39 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix*/ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_row == 4 && self->num_col == 4) | 
					
						
							|  |  |  | 		return PyBool_FromLong(is_negative_m4((float (*)[4])self->matrix)); | 
					
						
							|  |  |  | 	else if (self->num_row == 3 && self->num_col == 3) | 
					
						
							|  |  |  | 		return PyBool_FromLong(is_negative_m3((float (*)[3])self->matrix)); | 
					
						
							| 
									
										
										
										
											2010-01-30 13:15:39 +00:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.is_negative: " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2010-01-30 13:15:39 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_is_orthogonal_doc, | 
					
						
							| 
									
										
										
										
											2012-02-24 06:44:04 +00:00
										 |  |  | "True if this matrix is orthogonal, 3x3 and 4x4 only, (read-only).\n\n:type: bool" | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | static PyObject *Matrix_is_orthogonal_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix*/ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (self->num_row == 4 && self->num_col == 4) | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 		return PyBool_FromLong(is_orthonormal_m4((float (*)[4])self->matrix)); | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	else if (self->num_row == 3 && self->num_col == 3) | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 		return PyBool_FromLong(is_orthonormal_m3((float (*)[3])self->matrix)); | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2011-09-19 14:29:21 +00:00
										 |  |  | 		                "Matrix.is_orthogonal: " | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 3x3 or 4x4 matrix"); | 
					
						
							| 
									
										
										
										
											2011-05-18 15:31:00 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-01-30 13:15:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | PyDoc_STRVAR(Matrix_is_orthogonal_axis_vectors_doc, | 
					
						
							|  |  |  | "True if this matrix has got orthogonal axis vectors, 3x3 and 4x4 only, (read-only).\n\n:type: bool" | 
					
						
							| 
									
										
										
										
											2012-04-01 00:14:41 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | static PyObject *Matrix_is_orthogonal_axis_vectors_get(MatrixObject *self, void *UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2012-04-01 00:14:41 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(self) == -1) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*must be 3-4 cols, 3-4 rows, square matrix*/ | 
					
						
							|  |  |  | 	if (self->num_row == 4 && self->num_col == 4) | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 		return PyBool_FromLong(is_orthogonal_m4((float (*)[4])self->matrix)); | 
					
						
							| 
									
										
										
										
											2012-04-01 00:14:41 +00:00
										 |  |  | 	else if (self->num_row == 3 && self->num_col == 3) | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 		return PyBool_FromLong(is_orthogonal_m3((float (*)[3])self->matrix)); | 
					
						
							| 
									
										
										
										
											2012-04-01 00:14:41 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 		                "Matrix.is_orthogonal_axis_vectors: " | 
					
						
							| 
									
										
										
										
											2012-04-01 00:14:41 +00:00
										 |  |  | 		                "inappropriate matrix size - expects 3x3 or 4x4 matrix"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-18 23:12:29 +00:00
										 |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Python attributes get/set structure:                                      */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | static PyGetSetDef Matrix_getseters[] = { | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | 	{(char *)"median_scale", (getter)Matrix_median_scale_get, (setter)NULL, Matrix_median_scale_doc, NULL}, | 
					
						
							|  |  |  | 	{(char *)"translation", (getter)Matrix_translation_get, (setter)Matrix_translation_set, Matrix_translation_doc, NULL}, | 
					
						
							|  |  |  | 	{(char *)"row", (getter)Matrix_row_get, (setter)NULL, Matrix_row_doc, NULL}, | 
					
						
							|  |  |  | 	{(char *)"col", (getter)Matrix_col_get, (setter)NULL, Matrix_col_doc, NULL}, | 
					
						
							|  |  |  | 	{(char *)"is_negative", (getter)Matrix_is_negative_get, (setter)NULL, Matrix_is_negative_doc, NULL}, | 
					
						
							|  |  |  | 	{(char *)"is_orthogonal", (getter)Matrix_is_orthogonal_get, (setter)NULL, Matrix_is_orthogonal_doc, NULL}, | 
					
						
							| 
									
										
										
										
											2012-04-02 12:36:00 +00:00
										 |  |  | 	{(char *)"is_orthogonal_axis_vectors", (getter)Matrix_is_orthogonal_axis_vectors_get, (setter)NULL, Matrix_is_orthogonal_axis_vectors_doc, NULL}, | 
					
						
							| 
									
										
										
										
											2011-12-25 11:36:26 +00:00
										 |  |  | 	{(char *)"is_wrapped", (getter)BaseMathObject_is_wrapped_get, (setter)NULL, BaseMathObject_is_wrapped_doc, NULL}, | 
					
						
							| 
									
										
										
										
											2015-02-15 14:10:46 +11:00
										 |  |  | 	{(char *)"is_frozen",  (getter)BaseMathObject_is_frozen_get,  (setter)NULL, BaseMathObject_is_frozen_doc, NULL}, | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	{(char *)"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL}, | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	{NULL, NULL, NULL, NULL, NULL}  /* Sentinel */ | 
					
						
							| 
									
										
										
										
											2009-06-18 23:12:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | /*-----------------------METHOD DEFINITIONS ----------------------*/ | 
					
						
							|  |  |  | static struct PyMethodDef Matrix_methods[] = { | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	/* derived values */ | 
					
						
							|  |  |  | 	{"determinant", (PyCFunction) Matrix_determinant, METH_NOARGS, Matrix_determinant_doc}, | 
					
						
							|  |  |  | 	{"decompose", (PyCFunction) Matrix_decompose, METH_NOARGS, Matrix_decompose_doc}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* in place only */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	{"zero", (PyCFunction) Matrix_zero, METH_NOARGS, Matrix_zero_doc}, | 
					
						
							|  |  |  | 	{"identity", (PyCFunction) Matrix_identity, METH_NOARGS, Matrix_identity_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* operate on original or copy */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	{"transpose", (PyCFunction) Matrix_transpose, METH_NOARGS, Matrix_transpose_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	{"transposed", (PyCFunction) Matrix_transposed, METH_NOARGS, Matrix_transposed_doc}, | 
					
						
							| 
									
										
										
										
											2012-12-16 04:05:16 +00:00
										 |  |  | 	{"normalize", (PyCFunction) Matrix_normalize, METH_NOARGS, Matrix_normalize_doc}, | 
					
						
							|  |  |  | 	{"normalized", (PyCFunction) Matrix_normalized, METH_NOARGS, Matrix_normalized_doc}, | 
					
						
							| 
									
										
										
										
											2014-04-29 19:58:37 +10:00
										 |  |  | 	{"invert", (PyCFunction) Matrix_invert, METH_VARARGS, Matrix_invert_doc}, | 
					
						
							|  |  |  | 	{"inverted", (PyCFunction) Matrix_inverted, METH_VARARGS, Matrix_inverted_doc}, | 
					
						
							| 
									
										
										
										
											2014-09-06 14:54:08 +02:00
										 |  |  | 	{"invert_safe", (PyCFunction) Matrix_invert_safe, METH_NOARGS, Matrix_invert_safe_doc}, | 
					
						
							|  |  |  | 	{"inverted_safe", (PyCFunction) Matrix_inverted_safe, METH_NOARGS, Matrix_inverted_safe_doc}, | 
					
						
							| 
									
										
										
										
											2012-10-29 03:36:55 +00:00
										 |  |  | 	{"adjugate", (PyCFunction) Matrix_adjugate, METH_NOARGS, Matrix_adjugate_doc}, | 
					
						
							|  |  |  | 	{"adjugated", (PyCFunction) Matrix_adjugated, METH_NOARGS, Matrix_adjugated_doc}, | 
					
						
							| 
									
										
										
										
											2010-02-23 09:39:47 +00:00
										 |  |  | 	{"to_3x3", (PyCFunction) Matrix_to_3x3, METH_NOARGS, Matrix_to_3x3_doc}, | 
					
						
							| 
									
										
										
										
											2012-07-06 23:56:59 +00:00
										 |  |  | 	/* TODO. {"resize_3x3", (PyCFunction) Matrix_resize3x3, METH_NOARGS, Matrix_resize3x3_doc}, */ | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	{"to_4x4", (PyCFunction) Matrix_to_4x4, METH_NOARGS, Matrix_to_4x4_doc}, | 
					
						
							|  |  |  | 	{"resize_4x4", (PyCFunction) Matrix_resize_4x4, METH_NOARGS, Matrix_resize_4x4_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-05 09:57:02 +00:00
										 |  |  | 	{"rotate", (PyCFunction) Matrix_rotate, METH_O, Matrix_rotate_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* return converted representation */ | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 	{"to_euler", (PyCFunction) Matrix_to_euler, METH_VARARGS, Matrix_to_euler_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-05 06:14:50 +00:00
										 |  |  | 	{"to_quaternion", (PyCFunction) Matrix_to_quaternion, METH_NOARGS, Matrix_to_quaternion_doc}, | 
					
						
							|  |  |  | 	{"to_scale", (PyCFunction) Matrix_to_scale, METH_NOARGS, Matrix_to_scale_doc}, | 
					
						
							|  |  |  | 	{"to_translation", (PyCFunction) Matrix_to_translation, METH_NOARGS, Matrix_to_translation_doc}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* operation between 2 or more types  */ | 
					
						
							|  |  |  | 	{"lerp", (PyCFunction) Matrix_lerp, METH_VARARGS, Matrix_lerp_doc}, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | 	{"copy", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc}, | 
					
						
							|  |  |  | 	{"__copy__", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc}, | 
					
						
							| 
									
										
										
										
											2012-03-30 11:35:58 +00:00
										 |  |  | 	{"__deepcopy__", (PyCFunction) Matrix_deepcopy, METH_VARARGS, Matrix_copy_doc}, | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-15 11:26:31 +11:00
										 |  |  | 	/* base-math methods */ | 
					
						
							|  |  |  | 	{"freeze", (PyCFunction)BaseMathObject_freeze, METH_NOARGS, BaseMathObject_freeze_doc}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	/* class methods */ | 
					
						
							| 
									
										
										
										
											2012-01-24 01:56:44 +00:00
										 |  |  | 	{"Identity", (PyCFunction) C_Matrix_Identity, METH_VARARGS | METH_CLASS, C_Matrix_Identity_doc}, | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  | 	{"Rotation", (PyCFunction) C_Matrix_Rotation, METH_VARARGS | METH_CLASS, C_Matrix_Rotation_doc}, | 
					
						
							|  |  |  | 	{"Scale", (PyCFunction) C_Matrix_Scale, METH_VARARGS | METH_CLASS, C_Matrix_Scale_doc}, | 
					
						
							|  |  |  | 	{"Shear", (PyCFunction) C_Matrix_Shear, METH_VARARGS | METH_CLASS, C_Matrix_Shear_doc}, | 
					
						
							|  |  |  | 	{"Translation", (PyCFunction) C_Matrix_Translation, METH_O | METH_CLASS, C_Matrix_Translation_doc}, | 
					
						
							|  |  |  | 	{"OrthoProjection", (PyCFunction) C_Matrix_OrthoProjection,  METH_VARARGS | METH_CLASS, C_Matrix_OrthoProjection_doc}, | 
					
						
							| 
									
										
										
											
												Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
 - Mathutils.MidpointVecs --> vector.lerp(other, fac)
 - Mathutils.AngleBetweenVecs --> vector.angle(other)
 - Mathutils.ProjectVecs --> vector.project(other)
 - Mathutils.DifferenceQuats --> quat.difference(other)
 - Mathutils.Slerp --> quat.slerp(other, fac)
 - Mathutils.Rand: removed, use pythons random module
 - Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
 - Matrix.scalePart --> Matrix.scale_part
 - Matrix.translationPart --> Matrix.translation_part
 - Matrix.rotationPart --> Matrix.rotation_part
 - toMatrix --> to_matrix
 - toEuler --> to_euler
 - toQuat --> to_quat
 - Vector.toTrackQuat --> Vector.to_track_quat
											
										 
											2010-01-25 09:44:04 +00:00
										 |  |  | 	{NULL, NULL, 0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /*------------------PY_OBECT DEFINITION--------------------------*/ | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | PyDoc_STRVAR(matrix_doc, | 
					
						
							| 
									
										
										
										
											2015-02-01 16:06:32 +01:00
										 |  |  | ".. class:: Matrix([rows])\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   This object gives access to Matrices in Blender, supporting square and rectangular\n" | 
					
						
							|  |  |  | "   matrices from 2x2 up to 4x4.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "   :param rows: Sequence of rows.\n" | 
					
						
							| 
									
										
										
										
											2015-05-12 17:59:37 +10:00
										 |  |  | "      When ommitted, a 4x4 identity matrix is constructed.\n" | 
					
						
							| 
									
										
										
										
											2015-02-01 16:06:32 +01:00
										 |  |  | "   :type rows: 2d number sequence\n" | 
					
						
							| 
									
										
										
										
											2011-05-24 16:05:51 +00:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | PyTypeObject matrix_Type = { | 
					
						
							|  |  |  | 	PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							| 
									
										
										
										
											2012-04-15 14:54:15 +00:00
										 |  |  | 	"Matrix",                           /*tp_name*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	sizeof(MatrixObject),               /*tp_basicsize*/ | 
					
						
							|  |  |  | 	0,                                  /*tp_itemsize*/ | 
					
						
							|  |  |  | 	(destructor)BaseMathObject_dealloc, /*tp_dealloc*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_print*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_getattr*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_setattr*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_compare*/ | 
					
						
							|  |  |  | 	(reprfunc) Matrix_repr,             /*tp_repr*/ | 
					
						
							|  |  |  | 	&Matrix_NumMethods,                 /*tp_as_number*/ | 
					
						
							|  |  |  | 	&Matrix_SeqMethods,                 /*tp_as_sequence*/ | 
					
						
							|  |  |  | 	&Matrix_AsMapping,                  /*tp_as_mapping*/ | 
					
						
							| 
									
										
										
										
											2015-02-15 10:46:14 +11:00
										 |  |  | 	(hashfunc)Matrix_hash,              /*tp_hash*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	NULL,                               /*tp_call*/ | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #ifndef MATH_STANDALONE
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	(reprfunc) Matrix_str,              /*tp_str*/ | 
					
						
							| 
									
										
										
										
											2012-12-08 01:16:59 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	NULL,                               /*tp_str*/ | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	NULL,                               /*tp_getattro*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_setattro*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_as_buffer*/ | 
					
						
							| 
									
										
										
										
											2011-02-24 04:58:51 +00:00
										 |  |  | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	matrix_doc,                         /*tp_doc*/ | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	(traverseproc)BaseMathObject_traverse,  /* tp_traverse */ | 
					
						
							|  |  |  | 	(inquiry)BaseMathObject_clear,      /*tp_clear*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	(richcmpfunc)Matrix_richcmpr,       /*tp_richcompare*/ | 
					
						
							|  |  |  | 	0,                                  /*tp_weaklistoffset*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_iter*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_iternext*/ | 
					
						
							|  |  |  | 	Matrix_methods,                     /*tp_methods*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_members*/ | 
					
						
							|  |  |  | 	Matrix_getseters,                   /*tp_getset*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_base*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_dict*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_descr_get*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_descr_set*/ | 
					
						
							|  |  |  | 	0,                                  /*tp_dictoffset*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_init*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_alloc*/ | 
					
						
							|  |  |  | 	Matrix_new,                         /*tp_new*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_free*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_is_gc*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_bases*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_mro*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_cache*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_subclasses*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_weaklist*/ | 
					
						
							|  |  |  | 	NULL                                /*tp_del*/ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | PyObject *Matrix_CreatePyObject( | 
					
						
							|  |  |  |         const float *mat, | 
					
						
							|  |  |  |         const unsigned short num_col, const unsigned short num_row, | 
					
						
							|  |  |  |         PyTypeObject *base_type) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 	MatrixObject *self; | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	float *mat_alloc; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-19 05:14:09 +00:00
										 |  |  | 	/* matrix objects can be any 2-4row x 2-4col matrix */ | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 	if (num_col < 2 || num_col > 4 || num_row < 2 || num_row > 4) { | 
					
						
							| 
									
										
										
										
											2011-07-14 01:25:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  | 		                "Matrix(): " | 
					
						
							|  |  |  | 		                "row and column sizes must be between 2 and 4"); | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	mat_alloc = PyMem_Malloc(num_col * num_row * sizeof(float)); | 
					
						
							|  |  |  | 	if (UNLIKELY(mat_alloc == NULL)) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 		                "Matrix(): " | 
					
						
							|  |  |  | 		                "problem allocating data"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	self = BASE_MATH_NEW(MatrixObject, matrix_Type, base_type); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (self) { | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		self->matrix = mat_alloc; | 
					
						
							| 
									
										
										
										
											2011-12-20 04:11:23 +00:00
										 |  |  | 		self->num_col = num_col; | 
					
						
							|  |  |  | 		self->num_row = num_row; | 
					
						
							| 
									
										
										
										
											2011-02-04 03:06:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 		/* init callbacks as NULL */ | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 		self->cb_user = NULL; | 
					
						
							|  |  |  | 		self->cb_type = self->cb_subtype = 0; | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		if (mat) {  /*if a float array passed*/ | 
					
						
							|  |  |  | 			memcpy(self->matrix, mat, num_col * num_row * sizeof(float)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		else if (num_col == num_row) { | 
					
						
							|  |  |  | 			/* or if no arguments are passed return identity matrix for square matrices */ | 
					
						
							|  |  |  | 			matrix_identity_internal(self); | 
					
						
							| 
									
										
										
										
											2011-02-24 04:58:51 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 			/* otherwise zero everything */ | 
					
						
							|  |  |  | 			memset(self->matrix, 0, num_col * num_row * sizeof(float)); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 		self->flag = BASE_MATH_FLAG_DEFAULT; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyMem_Free(mat_alloc); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (PyObject *)self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *Matrix_CreatePyObject_wrap( | 
					
						
							|  |  |  |         float *mat, | 
					
						
							|  |  |  |         const unsigned short num_col, const unsigned short num_row, | 
					
						
							|  |  |  |         PyTypeObject *base_type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject *self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* matrix objects can be any 2-4row x 2-4col matrix */ | 
					
						
							|  |  |  | 	if (num_col < 2 || num_col > 4 || num_row < 2 || num_row > 4) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  | 		                "Matrix(): " | 
					
						
							|  |  |  | 		                "row and column sizes must be between 2 and 4"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	self = BASE_MATH_NEW(MatrixObject, matrix_Type, base_type); | 
					
						
							|  |  |  | 	if (self) { | 
					
						
							|  |  |  | 		self->num_col = num_col; | 
					
						
							|  |  |  | 		self->num_row = num_row; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* init callbacks as NULL */ | 
					
						
							|  |  |  | 		self->cb_user = NULL; | 
					
						
							|  |  |  | 		self->cb_type = self->cb_subtype = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		self->matrix = mat; | 
					
						
							|  |  |  | 		self->flag = BASE_MATH_FLAG_DEFAULT | BASE_MATH_FLAG_IS_WRAP; | 
					
						
							| 
									
										
										
										
											2011-02-24 04:58:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 	return (PyObject *) self; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-22 04:26:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 04:58:01 +00:00
										 |  |  | PyObject *Matrix_CreatePyObject_cb(PyObject *cb_user, | 
					
						
							|  |  |  |                                    const unsigned short num_col, const unsigned short num_row, | 
					
						
							| 
									
										
										
										
											2012-03-17 22:31:57 +00:00
										 |  |  |                                    unsigned char cb_type, unsigned char cb_subtype) | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-04 17:03:54 +11:00
										 |  |  | 	MatrixObject *self = (MatrixObject *)Matrix_CreatePyObject(NULL, num_col, num_row, NULL); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (self) { | 
					
						
							| 
									
										
										
										
											2011-02-24 05:46:57 +00:00
										 |  |  | 		Py_INCREF(cb_user); | 
					
						
							| 
									
										
										
										
											2012-03-17 22:31:57 +00:00
										 |  |  | 		self->cb_user         = cb_user; | 
					
						
							|  |  |  | 		self->cb_type         = cb_type; | 
					
						
							|  |  |  | 		self->cb_subtype      = cb_subtype; | 
					
						
							| 
									
										
										
										
											2011-03-03 06:01:31 +00:00
										 |  |  | 		PyObject_GC_Track(self); | 
					
						
							| 
									
										
										
										
											2009-06-23 13:34:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return (PyObject *) self; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-19 21:36:50 +10:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Use with PyArg_ParseTuple's "O&" formatting. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static bool Matrix_ParseCheck(MatrixObject *pymat) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!MatrixObject_Check(pymat)) { | 
					
						
							|  |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "expected a mathutils.Matrix, not a %.200s", | 
					
						
							|  |  |  | 		             Py_TYPE(pymat)->tp_name); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* sets error */ | 
					
						
							|  |  |  | 	if (BaseMath_ReadCallback(pymat) == -1) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Matrix_ParseAny(PyObject *o, void *p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject **pymat_p = p; | 
					
						
							|  |  |  | 	MatrixObject  *pymat = (MatrixObject *)o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!Matrix_ParseCheck(pymat)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	*pymat_p = pymat; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Matrix_Parse3x3(PyObject *o, void *p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject **pymat_p = p; | 
					
						
							|  |  |  | 	MatrixObject  *pymat = (MatrixObject *)o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!Matrix_ParseCheck(pymat)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((pymat->num_col != 3) || | 
					
						
							|  |  |  | 	    (pymat->num_row != 3)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, "matrix must be 3x3"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*pymat_p = pymat; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Matrix_Parse4x4(PyObject *o, void *p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MatrixObject **pymat_p = p; | 
					
						
							|  |  |  | 	MatrixObject  *pymat = (MatrixObject *)o; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!Matrix_ParseCheck(pymat)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((pymat->num_col != 4) || | 
					
						
							|  |  |  | 	    (pymat->num_row != 4)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, "matrix must be 4x4"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*pymat_p = pymat; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2012-07-16 23:23:33 +00:00
										 |  |  |  * special type for alternate access */ | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  | 	PyObject_HEAD /* required python macro   */ | 
					
						
							|  |  |  | 	MatrixObject *matrix_user; | 
					
						
							|  |  |  | 	eMatrixAccess_t type; | 
					
						
							|  |  |  | } MatrixAccessObject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int MatrixAccess_traverse(MatrixAccessObject *self, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Py_VISIT(self->matrix_user); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | static int MatrixAccess_clear(MatrixAccessObject *self) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Py_CLEAR(self->matrix_user); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | static void MatrixAccess_dealloc(MatrixAccessObject *self) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (self->matrix_user) { | 
					
						
							|  |  |  | 		PyObject_GC_UnTrack(self); | 
					
						
							|  |  |  | 		MatrixAccess_clear(self); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_TYPE(self)->tp_free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* sequence access */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int MatrixAccess_len(MatrixAccessObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (self->type == MAT_ACCESS_ROW) ? | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	       self->matrix_user->num_row : | 
					
						
							|  |  |  | 	       self->matrix_user->num_col; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | static PyObject *MatrixAccess_slice(MatrixAccessObject *self, int begin, int end) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *tuple; | 
					
						
							|  |  |  | 	int count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* row/col access */ | 
					
						
							|  |  |  | 	MatrixObject *matrix_user = self->matrix_user; | 
					
						
							|  |  |  | 	int matrix_access_len; | 
					
						
							|  |  |  | 	PyObject *(*Matrix_item_new)(MatrixObject *, int); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (self->type == MAT_ACCESS_ROW) { | 
					
						
							|  |  |  | 		matrix_access_len = matrix_user->num_row; | 
					
						
							|  |  |  | 		Matrix_item_new = Matrix_item_row; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { /* MAT_ACCESS_ROW */ | 
					
						
							|  |  |  | 		matrix_access_len = matrix_user->num_col; | 
					
						
							|  |  |  | 		Matrix_item_new = Matrix_item_col; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CLAMP(begin, 0, matrix_access_len); | 
					
						
							|  |  |  | 	if (end < 0) end = (matrix_access_len + 1) + end; | 
					
						
							|  |  |  | 	CLAMP(end, 0, matrix_access_len); | 
					
						
							|  |  |  | 	begin = MIN2(begin, end); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tuple = PyTuple_New(end - begin); | 
					
						
							|  |  |  | 	for (count = begin; count < end; count++) { | 
					
						
							|  |  |  | 		PyTuple_SET_ITEM(tuple, count - begin, Matrix_item_new(matrix_user, count)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tuple; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static PyObject *MatrixAccess_subscript(MatrixAccessObject *self, PyObject *item) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *matrix_user = self->matrix_user; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (PyIndex_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t i; | 
					
						
							|  |  |  | 		i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
					
						
							|  |  |  | 		if (i == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		if (self->type == MAT_ACCESS_ROW) { | 
					
						
							|  |  |  | 			if (i < 0) | 
					
						
							|  |  |  | 				i += matrix_user->num_row; | 
					
						
							|  |  |  | 			return Matrix_item_row(matrix_user, i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { /* MAT_ACCESS_ROW */ | 
					
						
							|  |  |  | 			if (i < 0) | 
					
						
							|  |  |  | 				i += matrix_user->num_col; | 
					
						
							|  |  |  | 			return Matrix_item_col(matrix_user, i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | 	else if (PySlice_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t start, stop, step, slicelength; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-05 13:48:42 +00:00
										 |  |  | 		if (PySlice_GetIndicesEx(item, MatrixAccess_len(self), &start, &stop, &step, &slicelength) < 0) | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (slicelength <= 0) { | 
					
						
							|  |  |  | 			return PyTuple_New(0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (step == 1) { | 
					
						
							|  |  |  | 			return MatrixAccess_slice(self, start, stop); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_IndexError, | 
					
						
							|  |  |  | 			                "slice steps not supported with matrix accessors"); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "matrix indices must be integers, not %.200s", | 
					
						
							|  |  |  | 		             Py_TYPE(item)->tp_name); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | static int MatrixAccess_ass_subscript(MatrixAccessObject *self, PyObject *item, PyObject *value) | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixObject *matrix_user = self->matrix_user; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (PyIndex_Check(item)) { | 
					
						
							|  |  |  | 		Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
					
						
							|  |  |  | 		if (i == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (self->type == MAT_ACCESS_ROW) { | 
					
						
							|  |  |  | 			if (i < 0) | 
					
						
							|  |  |  | 				i += matrix_user->num_row; | 
					
						
							|  |  |  | 			return Matrix_ass_item_row(matrix_user, i, value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { /* MAT_ACCESS_ROW */ | 
					
						
							|  |  |  | 			if (i < 0) | 
					
						
							|  |  |  | 				i += matrix_user->num_col; | 
					
						
							|  |  |  | 			return Matrix_ass_item_col(matrix_user, i, value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* TODO, slice */ | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 		             "matrix indices must be integers, not %.200s", | 
					
						
							|  |  |  | 		             Py_TYPE(item)->tp_name); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | static PyObject *MatrixAccess_iter(MatrixAccessObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Try get values from a collection */ | 
					
						
							|  |  |  | 	PyObject *ret; | 
					
						
							|  |  |  | 	PyObject *iter = NULL; | 
					
						
							|  |  |  | 	ret = MatrixAccess_slice(self, 0, MATRIX_MAX_DIM); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* we know this is a tuple so no need to PyIter_Check
 | 
					
						
							|  |  |  | 	 * otherwise it could be NULL (unlikely) if conversion failed */ | 
					
						
							|  |  |  | 	if (ret) { | 
					
						
							|  |  |  | 		iter = PyObject_GetIter(ret); | 
					
						
							|  |  |  | 		Py_DECREF(ret); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return iter; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyMappingMethods MatrixAccess_AsMapping = { | 
					
						
							|  |  |  | 	(lenfunc)MatrixAccess_len, | 
					
						
							|  |  |  | 	(binaryfunc)MatrixAccess_subscript, | 
					
						
							|  |  |  | 	(objobjargproc) MatrixAccess_ass_subscript | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyTypeObject matrix_access_Type = { | 
					
						
							|  |  |  | 	PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	"MatrixAccess",                     /*tp_name*/ | 
					
						
							|  |  |  | 	sizeof(MatrixAccessObject),         /*tp_basicsize*/ | 
					
						
							|  |  |  | 	0,                                  /*tp_itemsize*/ | 
					
						
							|  |  |  | 	(destructor)MatrixAccess_dealloc,   /*tp_dealloc*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_print*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_getattr*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_setattr*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_compare*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_repr*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_as_number*/ | 
					
						
							|  |  |  | 	NULL /*&MatrixAccess_SeqMethods*/ /* TODO */,           /*tp_as_sequence*/ | 
					
						
							|  |  |  | 	&MatrixAccess_AsMapping,            /*tp_as_mapping*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_hash*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_call*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_str*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_getattro*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_setattro*/ | 
					
						
							|  |  |  | 	NULL,                               /*tp_as_buffer*/ | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	NULL,                               /*tp_doc*/ | 
					
						
							| 
									
										
										
										
											2012-10-20 20:20:02 +00:00
										 |  |  | 	(traverseproc)MatrixAccess_traverse,/*tp_traverse*/ | 
					
						
							|  |  |  | 	(inquiry)MatrixAccess_clear,        /*tp_clear*/ | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 	NULL /* (richcmpfunc)MatrixAccess_richcmpr */ /* TODO*/, /*tp_richcompare*/ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	0,                                  /*tp_weaklistoffset*/ | 
					
						
							| 
									
										
										
										
											2012-01-02 09:04:37 +00:00
										 |  |  | 	(getiterfunc)MatrixAccess_iter, /* getiterfunc tp_iter; */ | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *MatrixAccess_CreatePyObject(MatrixObject *matrix, const eMatrixAccess_t type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	MatrixAccessObject *matrix_access = (MatrixAccessObject *)PyObject_GC_New(MatrixObject, &matrix_access_Type); | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	matrix_access->matrix_user = matrix; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 	Py_INCREF(matrix); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-24 13:26:30 +00:00
										 |  |  | 	matrix_access->type = type; | 
					
						
							| 
									
										
										
										
											2011-12-24 07:03:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return (PyObject *)matrix_access; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* end special access
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ |