14 #include "../generic/py_capi_utils.h"
15 #include "../generic/python_utildefines.h"
17 #ifndef MATH_STANDALONE
23 "This module provides access to math operations.\n"
27 " Classes, methods and attributes that accept vectors also accept other numeric sequences,\n"
28 " such as tuples, lists.\n"
30 "The :mod:`mathutils` module provides the following classes:\n"
34 "- :class:`Matrix`,\n"
35 "- :class:`Quaternion`,\n"
36 "- :class:`Vector`,\n");
40 const char *error_prefix)
43 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
50 if (((
array[i] = PyFloat_AsDouble((item = value_fast_items[i]))) == -1.0f) &&
52 PyErr_Format(PyExc_TypeError,
53 "%.200s: sequence index %d expected a number, "
54 "found '%.200s' type, ",
57 Py_TYPE(item)->tp_name);
74 mult = _PyHASH_MULTIPLIER;
79 #if PY_VERSION_HEX < 0x030a0000
80 y = _Py_HashDouble((
double)(
array[i++]));
82 y = _Py_HashDouble(
NULL, (
double)(
array[i++]));
92 if (
x == (Py_uhash_t)-1) {
99 float *
array,
int array_num_min,
int array_num_max, PyObject *value,
const char *error_prefix)
101 const uint flag = array_num_max;
119 if (num > array_num_max || num < array_num_min) {
120 if (array_num_max == array_num_min) {
121 PyErr_Format(PyExc_ValueError,
122 "%.200s: sequence length is %d, expected %d",
128 PyErr_Format(PyExc_ValueError,
129 "%.200s: sequence length is %d, expected [%d - %d]",
143 PyObject *value_fast =
NULL;
146 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
151 num = PySequence_Fast_GET_SIZE(value_fast);
157 if (num > array_num_max || num < array_num_min) {
158 if (array_num_max == array_num_min) {
159 PyErr_Format(PyExc_ValueError,
160 "%.200s: sequence length is %d, expected %d",
166 PyErr_Format(PyExc_ValueError,
167 "%.200s: sequence length is %d, expected [%d - %d]",
173 Py_DECREF(value_fast);
178 Py_DECREF(value_fast);
183 const int array_num_left = array_num_max - num;
184 if (array_num_left) {
185 memset(&
array[num], 0,
sizeof(
float) * array_num_left);
196 const char *error_prefix)
209 if (num < array_num) {
210 PyErr_Format(PyExc_ValueError,
211 "%.200s: sequence size is %d, expected > %d",
218 *
array = PyMem_Malloc(num *
sizeof(
float));
225 PyObject *value_fast =
NULL;
230 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
235 num = PySequence_Fast_GET_SIZE(value_fast);
237 if (num < array_num) {
238 Py_DECREF(value_fast);
239 PyErr_Format(PyExc_ValueError,
240 "%.200s: sequence size is %d, expected > %d",
247 *
array = PyMem_Malloc(num *
sizeof(
float));
250 Py_DECREF(value_fast);
262 const char *error_prefix)
264 PyObject *value_fast;
265 const int array_dim_flag = array_dim;
269 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
274 num = PySequence_Fast_GET_SIZE(value_fast);
277 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
282 fp = *
array = PyMem_Malloc(num * array_dim *
sizeof(
float));
284 for (i = 0; i < num; i++, fp += array_dim) {
285 PyObject *item = value_fast_items[i];
296 Py_DECREF(value_fast);
303 PyObject *value_fast, **value_fast_items, *item;
305 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
310 if ((
size = PySequence_Fast_GET_SIZE(value_fast)) != array_dim) {
311 PyErr_Format(PyExc_ValueError,
312 "%.200s: sequence size is %d, expected %d",
316 Py_DECREF(value_fast);
320 value_fast_items = PySequence_Fast_ITEMS(value_fast);
324 if (((
array[i] = PyC_Long_AsI32((item = value_fast_items[i]))) == -1) && PyErr_Occurred()) {
325 PyErr_Format(PyExc_TypeError,
"%.200s: sequence index %d expected an int", error_prefix, i);
330 Py_DECREF(value_fast);
338 const char *error_prefix)
340 PyObject *value_fast;
343 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
348 size = PySequence_Fast_GET_SIZE(value_fast);
351 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
354 ip = *
array = PyMem_Malloc(
size * array_dim *
sizeof(
int));
356 for (i = 0; i <
size; i++, ip += array_dim) {
357 PyObject *item = value_fast_items[i];
368 Py_DECREF(value_fast);
373 int **
array,
int **start_table,
int **len_table, PyObject *value,
const char *error_prefix)
375 PyObject *value_fast, *subseq;
376 int i,
size, start, subseq_len;
382 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
387 size = PySequence_Fast_GET_SIZE(value_fast);
390 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
392 *start_table = PyMem_Malloc(
size *
sizeof(
int));
393 *len_table = PyMem_Malloc(
size *
sizeof(
int));
397 for (i = 0; i <
size; i++) {
398 subseq = value_fast_items[i];
399 if ((subseq_len = (
int)PySequence_Size(subseq)) == -1) {
401 PyExc_ValueError,
"%.200s: sequence expected to have subsequences", error_prefix);
402 PyMem_Free(*start_table);
403 PyMem_Free(*len_table);
404 Py_DECREF(value_fast);
409 (*start_table)[i] = start;
410 (*len_table)[i] = subseq_len;
414 ip = *
array = PyMem_Malloc(start *
sizeof(
int));
417 for (i = 0; i <
size; i++) {
418 subseq = value_fast_items[i];
419 subseq_len = (*len_table)[i];
423 PyMem_Free(*start_table);
424 PyMem_Free(*len_table);
435 Py_DECREF(value_fast);
465 PyExc_ValueError,
"%.200s: matrix must have minimum 3x3 dimensions", error_prefix);
474 PyErr_Format(PyExc_TypeError,
475 "%.200s: expected a Euler, Quaternion or Matrix type, "
478 Py_TYPE(value)->tp_name);
499 #define SIGNMASK(i) (-(int)(((uint)(i)) >> 31))
505 const int ai = *(
const int *)(&af);
506 const int bi = *(
const int *)(&bf);
510 BLI_assert((0 == test) || (0xFFFFFFFF == test));
511 diff = (ai ^ (test & 0x7fffffff)) - bi;
514 return (
v1 |
v2) >= 0;
530 #ifndef MATH_STANDALONE
534 char *ds_buf = PyMem_Malloc(ds_len + 1);
538 ret = PyUnicode_FromStringAndSize(ds_buf, ds_len);
548 #define MATHUTILS_TOT_CB 17
586 if (!PyErr_Occurred()) {
587 PyErr_Format(PyExc_RuntimeError,
"%s read, user has become invalid", Py_TYPE(
self)->tp_name);
599 if (!PyErr_Occurred()) {
600 PyErr_Format(PyExc_RuntimeError,
"%s write, user has become invalid", Py_TYPE(
self)->tp_name);
612 if (!PyErr_Occurred()) {
614 PyExc_RuntimeError,
"%s read index, user has become invalid", Py_TYPE(
self)->tp_name);
626 if (!PyErr_Occurred()) {
628 PyExc_RuntimeError,
"%s write index, user has become invalid", Py_TYPE(
self)->tp_name);
635 PyErr_Format(PyExc_TypeError,
"%s is frozen (immutable)", Py_TYPE(
self)->tp_name);
641 PyExc_TypeError,
"%s is not frozen (mutable), call freeze first", Py_TYPE(
self)->tp_name);
648 PyObject *
ret =
self->cb_user ?
self->cb_user : Py_None;
649 return Py_INCREF_RET(
ret);
653 "True when this object wraps external data (read-only).\n\n:type: boolean";
660 "True when this object has been frozen (read-only).\n\n:type: boolean";
667 "True when the owner of this data is valid.\n\n:type: boolean";
674 ".. function:: freeze()\n"
676 " Make this object immutable.\n"
678 " After this the object can be hashed, used in dictionaries & sets.\n"
680 " :return: An instance of this object.\n";
684 PyErr_SetString(PyExc_TypeError,
"Cannot freeze wrapped/owned data");
690 return Py_INCREF_RET((PyObject *)
self);
695 Py_VISIT(
self->cb_user);
701 Py_CLEAR(
self->cb_user);
709 PyMem_Free(
self->data);
713 PyObject_GC_UnTrack(
self);
717 Py_TYPE(
self)->tp_free(
self);
726 PyModuleDef_HEAD_INIT,
740 #ifndef MATH_STANDALONE
750 PyObject *sys_modules = PyImport_GetModuleDict();
785 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
791 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
793 #ifndef MATH_STANDALONE
796 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
800 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
804 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
A dynamically sized string ADT.
int BLI_dynstr_get_len(const DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
void BLI_dynstr_get_cstring_ex(const DynStr *__restrict ds, char *__restrict rets) ATTR_NONNULL()
void normalize_m3(float R[3][3]) ATTR_NONNULL()
void eulO_to_mat3(float mat[3][3], const float eul[3], short order)
float normalize_qt_qt(float r[4], const float q[4])
void quat_to_mat3(float mat[3][3], const float q[4])
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint order
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
static struct PyModuleDef M_Mathutils_module_def
static Mathutils_Callback * mathutils_callbacks[MATHUTILS_TOT_CB]
PyObject * BaseMathObject_freeze(BaseMathObject *self)
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *UNUSED(closure))
int _BaseMathObject_CheckCallback(BaseMathObject *self)
PyMODINIT_FUNC PyInit_mathutils(void)
static int mathutils_array_parse_fast(float *array, int size, PyObject *value_fast, const char *error_prefix)
int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *UNUSED(closure))
PyObject * mathutils_dynstr_to_py(struct DynStr *ds)
int mathutils_array_parse_alloc_viseq(int **array, int **start_table, int **len_table, PyObject *value, const char *error_prefix)
void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
void BaseMathObject_dealloc(BaseMathObject *self)
int _BaseMathObject_WriteCallback(BaseMathObject *self)
int mathutils_int_array_parse(int *array, int array_dim, PyObject *value, const char *error_prefix)
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
char BaseMathObject_is_valid_doc[]
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
char BaseMathObject_is_wrapped_doc[]
PyDoc_STRVAR(M_Mathutils_doc, "This module provides access to math operations.\n" "\n" ".. note::\n" "\n" " Classes, methods and attributes that accept vectors also accept other numeric sequences,\n" " such as tuples, lists.\n" "\n" "The :mod:`mathutils` module provides the following classes:\n" "\n" "- :class:`Color`,\n" "- :class:`Euler`,\n" "- :class:`Matrix`,\n" "- :class:`Quaternion`,\n" "- :class:`Vector`,\n")
char BaseMathObject_is_frozen_doc[]
static struct PyMethodDef M_Mathutils_methods[]
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
PyObject * BaseMathObject_is_valid_get(BaseMathObject *self, void *UNUSED(closure))
char BaseMathObject_owner_doc[]
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
char BaseMathObject_freeze_doc[]
int mathutils_array_parse_alloc(float **array, int array_num, PyObject *value, const char *error_prefix)
int BaseMathObject_clear(BaseMathObject *self)
int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
int _BaseMathObject_ReadCallback(BaseMathObject *self)
void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self)
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
#define BaseMath_CheckCallback(_self)
@ BASE_MATH_FLAG_IS_FROZEN
#define BaseMath_ReadCallback(_self)
#define ColorObject_Check(v)
#define EulerObject_Check(v)
Mathutils_Callback mathutils_matrix_col_cb
PyTypeObject matrix_access_Type
void matrix_as_3x3(float mat[3][3], MatrixObject *self)
Mathutils_Callback mathutils_matrix_row_cb
Mathutils_Callback mathutils_matrix_translation_cb
uchar mathutils_matrix_col_cb_index
uchar mathutils_matrix_row_cb_index
uchar mathutils_matrix_translation_cb_index
#define MatrixObject_Check(v)
PyTypeObject quaternion_Type
#define QuaternionObject_Check(v)
#define VectorObject_Check(v)
PyMODINIT_FUNC PyInit_mathutils_bvhtree(void)
PyMODINIT_FUNC PyInit_mathutils_geometry(void)
PyMODINIT_FUNC PyInit_mathutils_interpolate(void)
PyMODINIT_FUNC PyInit_mathutils_kdtree(void)
PyMODINIT_FUNC PyInit_mathutils_noise(void)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
BaseMathSetIndexFunc set_index
BaseMathGetIndexFunc get_index
ccl_device_inline int mod(int x, int m)