28 #include "../generic/py_capi_utils.h"
29 #include "../generic/python_utildefines.h"
31 #ifndef MATH_STANDALONE
37 "This module provides access to math operations.\n"
41 " Classes, methods and attributes that accept vectors also accept other numeric sequences,\n"
42 " such as tuples, lists.\n"
44 "The :mod:`mathutils` module provides the following classes:\n"
48 "- :class:`Matrix`,\n"
49 "- :class:`Quaternion`,\n"
50 "- :class:`Vector`,\n");
54 const char *error_prefix)
57 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
64 if (((
array[i] = PyFloat_AsDouble((item = value_fast_items[i]))) == -1.0f) &&
66 PyErr_Format(PyExc_TypeError,
67 "%.200s: sequence index %d expected a number, "
68 "found '%.200s' type, ",
71 Py_TYPE(item)->tp_name);
93 mult = _PyHASH_MULTIPLIER;
98 #if PY_VERSION_HEX >= 0x30a0000
99 y = _Py_HashDouble(
NULL, (
double)(
array[i++]));
101 y = _Py_HashDouble((
double)(
array[i++]));
111 if (
x == (Py_uhash_t)-1) {
119 float *
array,
int array_min,
int array_max, PyObject *value,
const char *error_prefix)
121 const uint flag = array_max;
140 if (
size > array_max ||
size < array_min) {
141 if (array_max == array_min) {
142 PyErr_Format(PyExc_ValueError,
143 "%.200s: sequence size is %d, expected %d",
149 PyErr_Format(PyExc_ValueError,
150 "%.200s: sequence size is %d, expected [%d - %d]",
164 PyObject *value_fast =
NULL;
167 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
172 size = PySequence_Fast_GET_SIZE(value_fast);
178 if (
size > array_max ||
size < array_min) {
179 if (array_max == array_min) {
180 PyErr_Format(PyExc_ValueError,
181 "%.200s: sequence size is %d, expected %d",
187 PyErr_Format(PyExc_ValueError,
188 "%.200s: sequence size is %d, expected [%d - %d]",
194 Py_DECREF(value_fast);
199 Py_DECREF(value_fast);
204 const int size_left = array_max -
size;
206 memset(&
array[
size], 0,
sizeof(
float) * size_left);
218 const char *error_prefix)
232 if (
size < array_min) {
233 PyErr_Format(PyExc_ValueError,
234 "%.200s: sequence size is %d, expected > %d",
241 *
array = PyMem_Malloc(
size *
sizeof(
float));
248 PyObject *value_fast =
NULL;
253 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
258 size = PySequence_Fast_GET_SIZE(value_fast);
260 if (
size < array_min) {
261 Py_DECREF(value_fast);
262 PyErr_Format(PyExc_ValueError,
263 "%.200s: sequence size is %d, expected > %d",
270 *
array = PyMem_Malloc(
size *
sizeof(
float));
273 Py_DECREF(value_fast);
286 const char *error_prefix)
288 PyObject *value_fast;
289 const int array_dim_flag = array_dim;
293 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
298 size = PySequence_Fast_GET_SIZE(value_fast);
301 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
306 fp = *
array = PyMem_Malloc(
size * array_dim *
sizeof(
float));
308 for (i = 0; i <
size; i++, fp += array_dim) {
309 PyObject *item = value_fast_items[i];
320 Py_DECREF(value_fast);
328 PyObject *value_fast, **value_fast_items, *item;
330 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
335 if ((
size = PySequence_Fast_GET_SIZE(value_fast)) != array_dim) {
336 PyErr_Format(PyExc_ValueError,
337 "%.200s: sequence size is %d, expected %d",
341 Py_DECREF(value_fast);
345 value_fast_items = PySequence_Fast_ITEMS(value_fast);
349 if (((
array[i] = PyC_Long_AsI32((item = value_fast_items[i]))) == -1) && PyErr_Occurred()) {
350 PyErr_Format(PyExc_TypeError,
"%.200s: sequence index %d expected an int", error_prefix, i);
355 Py_DECREF(value_fast);
364 const char *error_prefix)
366 PyObject *value_fast;
369 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
374 size = PySequence_Fast_GET_SIZE(value_fast);
377 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
380 ip = *
array = PyMem_Malloc(
size * array_dim *
sizeof(
int));
382 for (i = 0; i <
size; i++, ip += array_dim) {
383 PyObject *item = value_fast_items[i];
394 Py_DECREF(value_fast);
405 int **
array,
int **start_table,
int **len_table, PyObject *value,
const char *error_prefix)
407 PyObject *value_fast, *subseq;
408 int i,
size, start, subseq_len;
414 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
419 size = PySequence_Fast_GET_SIZE(value_fast);
422 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
424 *start_table = PyMem_Malloc(
size *
sizeof(
int));
425 *len_table = PyMem_Malloc(
size *
sizeof(
int));
429 for (i = 0; i <
size; i++) {
430 subseq = value_fast_items[i];
431 if ((subseq_len = (
int)PySequence_Size(subseq)) == -1) {
433 PyExc_ValueError,
"%.200s: sequence expected to have subsequences", error_prefix);
434 PyMem_Free(*start_table);
435 PyMem_Free(*len_table);
436 Py_DECREF(value_fast);
441 (*start_table)[i] = start;
442 (*len_table)[i] = subseq_len;
446 ip = *
array = PyMem_Malloc(start *
sizeof(
int));
449 for (i = 0; i <
size; i++) {
450 subseq = value_fast_items[i];
451 subseq_len = (*len_table)[i];
455 PyMem_Free(*start_table);
456 PyMem_Free(*len_table);
467 Py_DECREF(value_fast);
497 PyExc_ValueError,
"%.200s: matrix must have minimum 3x3 dimensions", error_prefix);
506 PyErr_Format(PyExc_TypeError,
507 "%.200s: expected a Euler, Quaternion or Matrix type, "
510 Py_TYPE(value)->tp_name);
531 #define SIGNMASK(i) (-(int)(((uint)(i)) >> 31))
537 const int ai = *(
int *)(&af);
538 const int bi = *(
int *)(&bf);
542 BLI_assert((0 == test) || (0xFFFFFFFF == test));
543 diff = (ai ^ (test & 0x7fffffff)) - bi;
546 return (
v1 |
v2) >= 0;
562 #ifndef MATH_STANDALONE
567 char *ds_buf = PyMem_Malloc(ds_len + 1);
571 ret = PyUnicode_FromStringAndSize(ds_buf, ds_len);
581 #define MATHUTILS_TOT_CB 17
610 if (!PyErr_Occurred()) {
611 PyErr_Format(PyExc_RuntimeError,
"%s read, user has become invalid", Py_TYPE(
self)->tp_name);
623 if (!PyErr_Occurred()) {
624 PyErr_Format(PyExc_RuntimeError,
"%s write, user has become invalid", Py_TYPE(
self)->tp_name);
636 if (!PyErr_Occurred()) {
638 PyExc_RuntimeError,
"%s read index, user has become invalid", Py_TYPE(
self)->tp_name);
650 if (!PyErr_Occurred()) {
652 PyExc_RuntimeError,
"%s write index, user has become invalid", Py_TYPE(
self)->tp_name);
659 PyErr_Format(PyExc_TypeError,
"%s is frozen (immutable)", Py_TYPE(
self)->tp_name);
665 PyExc_TypeError,
"%s is not frozen (mutable), call freeze first", Py_TYPE(
self)->tp_name);
672 PyObject *
ret =
self->cb_user ?
self->cb_user : Py_None;
673 return Py_INCREF_RET(
ret);
677 "True when this object wraps external data (read-only).\n\n:type: boolean";
684 "True when this object has been frozen (read-only).\n\n:type: boolean";
691 ".. function:: freeze()\n"
693 " Make this object immutable.\n"
695 " After this the object can be hashed, used in dictionaries & sets.\n"
697 " :return: An instance of this object.\n";
701 PyErr_SetString(PyExc_TypeError,
"Cannot freeze wrapped/owned data");
707 return Py_INCREF_RET((PyObject *)
self);
712 Py_VISIT(
self->cb_user);
718 Py_CLEAR(
self->cb_user);
726 PyMem_Free(
self->data);
730 PyObject_GC_UnTrack(
self);
734 Py_TYPE(
self)->tp_free(
self);
743 PyModuleDef_HEAD_INIT,
757 #ifndef MATH_STANDALONE
767 PyObject *sys_modules = PyImport_GetModuleDict();
802 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
808 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
810 #ifndef MATH_STANDALONE
813 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
817 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
821 PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
A dynamically sized string ADT.
int BLI_dynstr_get_len(DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
void BLI_dynstr_get_cstring_ex(DynStr *__restrict ds, char *__restrict rets) ATTR_NONNULL()
void normalize_m3(float R[3][3]) ATTR_NONNULL()
float normalize_qt_qt(float r[4], const float q[4])
void eulO_to_mat3(float mat[3][3], const float eul[3], const short order)
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))
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_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)
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_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
char BaseMathObject_owner_doc[]
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
char BaseMathObject_freeze_doc[]
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)
int mathutils_array_parse_alloc(float **array, int array_min, PyObject *value, const char *error_prefix)
@ 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)