Blender  V2.93
mathutils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <Python.h>
22 
23 #include "mathutils.h"
24 
25 #include "BLI_math.h"
26 #include "BLI_utildefines.h"
27 
28 #include "../generic/py_capi_utils.h"
29 #include "../generic/python_utildefines.h"
30 
31 #ifndef MATH_STANDALONE
32 # include "BLI_dynstr.h"
33 #endif
34 
36  M_Mathutils_doc,
37  "This module provides access to math operations.\n"
38  "\n"
39  ".. note::\n"
40  "\n"
41  " Classes, methods and attributes that accept vectors also accept other numeric sequences,\n"
42  " such as tuples, lists.\n"
43  "\n"
44  "The :mod:`mathutils` module provides the following classes:\n"
45  "\n"
46  "- :class:`Color`,\n"
47  "- :class:`Euler`,\n"
48  "- :class:`Matrix`,\n"
49  "- :class:`Quaternion`,\n"
50  "- :class:`Vector`,\n");
52  int size,
53  PyObject *value_fast,
54  const char *error_prefix)
55 {
56  PyObject *item;
57  PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
58 
59  int i;
60 
61  i = size;
62  do {
63  i--;
64  if (((array[i] = PyFloat_AsDouble((item = value_fast_items[i]))) == -1.0f) &&
65  PyErr_Occurred()) {
66  PyErr_Format(PyExc_TypeError,
67  "%.200s: sequence index %d expected a number, "
68  "found '%.200s' type, ",
69  error_prefix,
70  i,
71  Py_TYPE(item)->tp_name);
72  size = -1;
73  break;
74  }
75  } while (i);
76 
77  return size;
78 }
79 
85 Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
86 {
87  int i;
88  Py_uhash_t x; /* Unsigned for defined overflow behavior. */
89  Py_hash_t y;
90  Py_uhash_t mult;
91  Py_ssize_t len;
92 
93  mult = _PyHASH_MULTIPLIER;
94  len = array_len;
95  x = 0x345678UL;
96  i = 0;
97  while (--len >= 0) {
98 #if PY_VERSION_HEX >= 0x30a0000 /* Version: 3.10. */
99  y = _Py_HashDouble(NULL, (double)(array[i++]));
100 #else
101  y = _Py_HashDouble((double)(array[i++]));
102 #endif
103  if (y == -1) {
104  return -1;
105  }
106  x = (x ^ y) * mult;
107  /* the cast might truncate len; that doesn't change hash stability */
108  mult += (Py_hash_t)(82520UL + len + len);
109  }
110  x += 97531UL;
111  if (x == (Py_uhash_t)-1) {
112  x = -2;
113  }
114  return x;
115 }
116 
117 /* helper function returns length of the 'value', -1 on error */
119  float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
120 {
121  const uint flag = array_max;
122  int size;
123 
124  array_max &= ~MU_ARRAY_FLAGS;
125 
126 #if 1 /* approx 6x speedup for mathutils types */
127 
128  if ((size = VectorObject_Check(value) ? ((VectorObject *)value)->size : 0) ||
129  (size = EulerObject_Check(value) ? 3 : 0) ||
130  (size = QuaternionObject_Check(value) ? 4 : 0) ||
131  (size = ColorObject_Check(value) ? 3 : 0)) {
132  if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
133  return -1;
134  }
135 
136  if (flag & MU_ARRAY_SPILL) {
137  CLAMP_MAX(size, array_max);
138  }
139 
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",
144  error_prefix,
145  size,
146  array_max);
147  }
148  else {
149  PyErr_Format(PyExc_ValueError,
150  "%.200s: sequence size is %d, expected [%d - %d]",
151  error_prefix,
152  size,
153  array_min,
154  array_max);
155  }
156  return -1;
157  }
158 
159  memcpy(array, ((BaseMathObject *)value)->data, size * sizeof(float));
160  }
161  else
162 #endif
163  {
164  PyObject *value_fast = NULL;
165 
166  /* non list/tuple cases */
167  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
168  /* PySequence_Fast sets the error */
169  return -1;
170  }
171 
172  size = PySequence_Fast_GET_SIZE(value_fast);
173 
174  if (flag & MU_ARRAY_SPILL) {
175  CLAMP_MAX(size, array_max);
176  }
177 
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",
182  error_prefix,
183  size,
184  array_max);
185  }
186  else {
187  PyErr_Format(PyExc_ValueError,
188  "%.200s: sequence size is %d, expected [%d - %d]",
189  error_prefix,
190  size,
191  array_min,
192  array_max);
193  }
194  Py_DECREF(value_fast);
195  return -1;
196  }
197 
198  size = mathutils_array_parse_fast(array, size, value_fast, error_prefix);
199  Py_DECREF(value_fast);
200  }
201 
202  if (size != -1) {
203  if (flag & MU_ARRAY_ZERO) {
204  const int size_left = array_max - size;
205  if (size_left) {
206  memset(&array[size], 0, sizeof(float) * size_left);
207  }
208  }
209  }
210 
211  return size;
212 }
213 
214 /* on error, -1 is returned and no allocation is made */
216  int array_min,
217  PyObject *value,
218  const char *error_prefix)
219 {
220  int size;
221 
222 #if 1 /* approx 6x speedup for mathutils types */
223 
224  if ((size = VectorObject_Check(value) ? ((VectorObject *)value)->size : 0) ||
225  (size = EulerObject_Check(value) ? 3 : 0) ||
226  (size = QuaternionObject_Check(value) ? 4 : 0) ||
227  (size = ColorObject_Check(value) ? 3 : 0)) {
228  if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
229  return -1;
230  }
231 
232  if (size < array_min) {
233  PyErr_Format(PyExc_ValueError,
234  "%.200s: sequence size is %d, expected > %d",
235  error_prefix,
236  size,
237  array_min);
238  return -1;
239  }
240 
241  *array = PyMem_Malloc(size * sizeof(float));
242  memcpy(*array, ((BaseMathObject *)value)->data, size * sizeof(float));
243  return size;
244  }
245 
246 #endif
247 
248  PyObject *value_fast = NULL;
249  // *array = NULL;
250  int ret;
251 
252  /* non list/tuple cases */
253  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
254  /* PySequence_Fast sets the error */
255  return -1;
256  }
257 
258  size = PySequence_Fast_GET_SIZE(value_fast);
259 
260  if (size < array_min) {
261  Py_DECREF(value_fast);
262  PyErr_Format(PyExc_ValueError,
263  "%.200s: sequence size is %d, expected > %d",
264  error_prefix,
265  size,
266  array_min);
267  return -1;
268  }
269 
270  *array = PyMem_Malloc(size * sizeof(float));
271 
272  ret = mathutils_array_parse_fast(*array, size, value_fast, error_prefix);
273  Py_DECREF(value_fast);
274 
275  if (ret == -1) {
276  PyMem_Free(*array);
277  }
278 
279  return ret;
280 }
281 
282 /* parse an array of vectors */
284  int array_dim,
285  PyObject *value,
286  const char *error_prefix)
287 {
288  PyObject *value_fast;
289  const int array_dim_flag = array_dim;
290  int i, size;
291 
292  /* non list/tuple cases */
293  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
294  /* PySequence_Fast sets the error */
295  return -1;
296  }
297 
298  size = PySequence_Fast_GET_SIZE(value_fast);
299 
300  if (size != 0) {
301  PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
302  float *fp;
303 
304  array_dim &= ~MU_ARRAY_FLAGS;
305 
306  fp = *array = PyMem_Malloc(size * array_dim * sizeof(float));
307 
308  for (i = 0; i < size; i++, fp += array_dim) {
309  PyObject *item = value_fast_items[i];
310 
311  if (mathutils_array_parse(fp, array_dim, array_dim_flag, item, error_prefix) == -1) {
312  PyMem_Free(*array);
313  *array = NULL;
314  size = -1;
315  break;
316  }
317  }
318  }
319 
320  Py_DECREF(value_fast);
321  return size;
322 }
323 
324 /* Parse an sequence array_dim integers into array. */
325 int mathutils_int_array_parse(int *array, int array_dim, PyObject *value, const char *error_prefix)
326 {
327  int size, i;
328  PyObject *value_fast, **value_fast_items, *item;
329 
330  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
331  /* PySequence_Fast sets the error */
332  return -1;
333  }
334 
335  if ((size = PySequence_Fast_GET_SIZE(value_fast)) != array_dim) {
336  PyErr_Format(PyExc_ValueError,
337  "%.200s: sequence size is %d, expected %d",
338  error_prefix,
339  size,
340  array_dim);
341  Py_DECREF(value_fast);
342  return -1;
343  }
344 
345  value_fast_items = PySequence_Fast_ITEMS(value_fast);
346  i = size;
347  while (i > 0) {
348  i--;
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);
351  size = -1;
352  break;
353  }
354  }
355  Py_DECREF(value_fast);
356 
357  return size;
358 }
359 
360 /* Parse sequence of array_dim sequences of integers and return allocated result. */
362  int array_dim,
363  PyObject *value,
364  const char *error_prefix)
365 {
366  PyObject *value_fast;
367  int i, size;
368 
369  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
370  /* PySequence_Fast sets the error */
371  return -1;
372  }
373 
374  size = PySequence_Fast_GET_SIZE(value_fast);
375 
376  if (size != 0) {
377  PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
378  int *ip;
379 
380  ip = *array = PyMem_Malloc(size * array_dim * sizeof(int));
381 
382  for (i = 0; i < size; i++, ip += array_dim) {
383  PyObject *item = value_fast_items[i];
384 
385  if (mathutils_int_array_parse(ip, array_dim, item, error_prefix) == -1) {
386  PyMem_Free(*array);
387  *array = NULL;
388  size = -1;
389  break;
390  }
391  }
392  }
393 
394  Py_DECREF(value_fast);
395  return size;
396 }
397 
398 /* Parse sequence of variable-length sequences of int and return allocated
399  * triple of arrays to represent the result:
400  * The flattened sequences are put into *array.
401  * The start index of each sequence goes into start_table.
402  * The length of each index goes into len_table.
403  */
405  int **array, int **start_table, int **len_table, PyObject *value, const char *error_prefix)
406 {
407  PyObject *value_fast, *subseq;
408  int i, size, start, subseq_len;
409  int *ip;
410 
411  *array = NULL;
412  *start_table = NULL;
413  *len_table = NULL;
414  if (!(value_fast = PySequence_Fast(value, error_prefix))) {
415  /* PySequence_Fast sets the error */
416  return -1;
417  }
418 
419  size = PySequence_Fast_GET_SIZE(value_fast);
420 
421  if (size != 0) {
422  PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
423 
424  *start_table = PyMem_Malloc(size * sizeof(int));
425  *len_table = PyMem_Malloc(size * sizeof(int));
426 
427  /* First pass to set starts and len, and calculate size of array needed */
428  start = 0;
429  for (i = 0; i < size; i++) {
430  subseq = value_fast_items[i];
431  if ((subseq_len = (int)PySequence_Size(subseq)) == -1) {
432  PyErr_Format(
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);
437  *start_table = NULL;
438  *len_table = NULL;
439  return -1;
440  }
441  (*start_table)[i] = start;
442  (*len_table)[i] = subseq_len;
443  start += subseq_len;
444  }
445 
446  ip = *array = PyMem_Malloc(start * sizeof(int));
447 
448  /* Second pass to parse the subsequences into array */
449  for (i = 0; i < size; i++) {
450  subseq = value_fast_items[i];
451  subseq_len = (*len_table)[i];
452 
453  if (mathutils_int_array_parse(ip, subseq_len, subseq, error_prefix) == -1) {
454  PyMem_Free(*array);
455  PyMem_Free(*start_table);
456  PyMem_Free(*len_table);
457  *array = NULL;
458  *len_table = NULL;
459  *start_table = NULL;
460  size = -1;
461  break;
462  }
463  ip += subseq_len;
464  }
465  }
466 
467  Py_DECREF(value_fast);
468  return size;
469 }
470 
471 int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
472 {
473  if (EulerObject_Check(value)) {
474  if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
475  return -1;
476  }
477 
478  eulO_to_mat3(rmat, ((EulerObject *)value)->eul, ((EulerObject *)value)->order);
479  return 0;
480  }
481  if (QuaternionObject_Check(value)) {
482  if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
483  return -1;
484  }
485 
486  float tquat[4];
487  normalize_qt_qt(tquat, ((QuaternionObject *)value)->quat);
488  quat_to_mat3(rmat, tquat);
489  return 0;
490  }
491  if (MatrixObject_Check(value)) {
492  if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
493  return -1;
494  }
495  if (((MatrixObject *)value)->num_row < 3 || ((MatrixObject *)value)->num_col < 3) {
496  PyErr_Format(
497  PyExc_ValueError, "%.200s: matrix must have minimum 3x3 dimensions", error_prefix);
498  return -1;
499  }
500 
501  matrix_as_3x3(rmat, (MatrixObject *)value);
502  normalize_m3(rmat);
503  return 0;
504  }
505 
506  PyErr_Format(PyExc_TypeError,
507  "%.200s: expected a Euler, Quaternion or Matrix type, "
508  "found %.200s",
509  error_prefix,
510  Py_TYPE(value)->tp_name);
511  return -1;
512 }
513 
514 /* ----------------------------------MATRIX FUNCTIONS-------------------- */
515 
516 /* Utility functions */
517 
518 /* LomontRRDCompare4, Ever Faster Float Comparisons by Randy Dillon */
519 /* XXX We may want to use 'safer' BLI's compare_ff_relative ultimately?
520  * LomontRRDCompare4() is an optimized version of Dawson's AlmostEqual2sComplement()
521  * (see [1] and [2]).
522  * Dawson himself now claims this is not a 'safe' thing to do
523  * (pushing ULP method beyond its limits),
524  * an recommends using work from [3] instead, which is done in BLI func...
525  *
526  * [1] http://www.randydillon.org/Papers/2007/everfast.htm
527  * [2] http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
528  * [3] https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
529  * instead.
530  */
531 #define SIGNMASK(i) (-(int)(((uint)(i)) >> 31))
532 
533 int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
534 {
535  /* solid, fast routine across all platforms
536  * with constant time behavior */
537  const int ai = *(int *)(&af);
538  const int bi = *(int *)(&bf);
539  const int test = SIGNMASK(ai ^ bi);
540  int diff, v1, v2;
541 
542  BLI_assert((0 == test) || (0xFFFFFFFF == test));
543  diff = (ai ^ (test & 0x7fffffff)) - bi;
544  v1 = maxDiff + diff;
545  v2 = maxDiff - diff;
546  return (v1 | v2) >= 0;
547 }
548 
549 /*---------------------- EXPP_VectorsAreEqual -------------------------
550  * Builds on EXPP_FloatsAreEqual to test vectors */
551 int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
552 {
553  int x;
554  for (x = 0; x < size; x++) {
555  if (EXPP_FloatsAreEqual(vecA[x], vecB[x], floatSteps) == 0) {
556  return 0;
557  }
558  }
559  return 1;
560 }
561 
562 #ifndef MATH_STANDALONE
563 /* dynstr as python string utility functions, frees 'ds'! */
564 PyObject *mathutils_dynstr_to_py(struct DynStr *ds)
565 {
566  const int ds_len = BLI_dynstr_get_len(ds); /* space for \0 */
567  char *ds_buf = PyMem_Malloc(ds_len + 1);
568  PyObject *ret;
569  BLI_dynstr_get_cstring_ex(ds, ds_buf);
570  BLI_dynstr_free(ds);
571  ret = PyUnicode_FromStringAndSize(ds_buf, ds_len);
572  PyMem_Free(ds_buf);
573  return ret;
574 }
575 #endif
576 
577 /* Mathutils Callbacks */
578 
579 /* For mathutils internal use only,
580  * eventually should re-alloc but to start with we only have a few users. */
581 #define MATHUTILS_TOT_CB 17
583 
585 {
586  uchar i;
587 
588  /* find the first free slot */
589  for (i = 0; mathutils_callbacks[i]; i++) {
590  if (mathutils_callbacks[i] == cb) {
591  /* already registered? */
592  return i;
593  }
594  }
595 
596  BLI_assert(i + 1 < MATHUTILS_TOT_CB);
597 
598  mathutils_callbacks[i] = cb;
599  return i;
600 }
601 
602 /* use macros to check for NULL */
604 {
605  Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
606  if (LIKELY(cb->get(self, self->cb_subtype) != -1)) {
607  return 0;
608  }
609 
610  if (!PyErr_Occurred()) {
611  PyErr_Format(PyExc_RuntimeError, "%s read, user has become invalid", Py_TYPE(self)->tp_name);
612  }
613  return -1;
614 }
615 
617 {
618  Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
619  if (LIKELY(cb->set(self, self->cb_subtype) != -1)) {
620  return 0;
621  }
622 
623  if (!PyErr_Occurred()) {
624  PyErr_Format(PyExc_RuntimeError, "%s write, user has become invalid", Py_TYPE(self)->tp_name);
625  }
626  return -1;
627 }
628 
630 {
631  Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
632  if (LIKELY(cb->get_index(self, self->cb_subtype, index) != -1)) {
633  return 0;
634  }
635 
636  if (!PyErr_Occurred()) {
637  PyErr_Format(
638  PyExc_RuntimeError, "%s read index, user has become invalid", Py_TYPE(self)->tp_name);
639  }
640  return -1;
641 }
642 
644 {
645  Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
646  if (LIKELY(cb->set_index(self, self->cb_subtype, index) != -1)) {
647  return 0;
648  }
649 
650  if (!PyErr_Occurred()) {
651  PyErr_Format(
652  PyExc_RuntimeError, "%s write index, user has become invalid", Py_TYPE(self)->tp_name);
653  }
654  return -1;
655 }
656 
658 {
659  PyErr_Format(PyExc_TypeError, "%s is frozen (immutable)", Py_TYPE(self)->tp_name);
660 }
661 
663 {
664  PyErr_Format(
665  PyExc_TypeError, "%s is not frozen (mutable), call freeze first", Py_TYPE(self)->tp_name);
666 }
667 
668 /* BaseMathObject generic functions for all mathutils types */
669 char BaseMathObject_owner_doc[] = "The item this is wrapping or None (read-only).";
670 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
671 {
672  PyObject *ret = self->cb_user ? self->cb_user : Py_None;
673  return Py_INCREF_RET(ret);
674 }
675 
677  "True when this object wraps external data (read-only).\n\n:type: boolean";
678 PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *UNUSED(closure))
679 {
680  return PyBool_FromLong((self->flag & BASE_MATH_FLAG_IS_WRAP) != 0);
681 }
682 
684  "True when this object has been frozen (read-only).\n\n:type: boolean";
685 PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *UNUSED(closure))
686 {
687  return PyBool_FromLong((self->flag & BASE_MATH_FLAG_IS_FROZEN) != 0);
688 }
689 
691  ".. function:: freeze()\n"
692  "\n"
693  " Make this object immutable.\n"
694  "\n"
695  " After this the object can be hashed, used in dictionaries & sets.\n"
696  "\n"
697  " :return: An instance of this object.\n";
699 {
700  if ((self->flag & BASE_MATH_FLAG_IS_WRAP) || (self->cb_user != NULL)) {
701  PyErr_SetString(PyExc_TypeError, "Cannot freeze wrapped/owned data");
702  return NULL;
703  }
704 
705  self->flag |= BASE_MATH_FLAG_IS_FROZEN;
706 
707  return Py_INCREF_RET((PyObject *)self);
708 }
709 
710 int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
711 {
712  Py_VISIT(self->cb_user);
713  return 0;
714 }
715 
717 {
718  Py_CLEAR(self->cb_user);
719  return 0;
720 }
721 
723 {
724  /* only free non wrapped */
725  if ((self->flag & BASE_MATH_FLAG_IS_WRAP) == 0) {
726  PyMem_Free(self->data);
727  }
728 
729  if (self->cb_user) {
730  PyObject_GC_UnTrack(self);
731  BaseMathObject_clear(self);
732  }
733 
734  Py_TYPE(self)->tp_free(self); // PyObject_DEL(self); /* breaks subtypes. */
735 }
736 
737 /*----------------------------MODULE INIT-------------------------*/
738 static struct PyMethodDef M_Mathutils_methods[] = {
739  {NULL, NULL, 0, NULL},
740 };
741 
742 static struct PyModuleDef M_Mathutils_module_def = {
743  PyModuleDef_HEAD_INIT,
744  "mathutils", /* m_name */
745  M_Mathutils_doc, /* m_doc */
746  0, /* m_size */
747  M_Mathutils_methods, /* m_methods */
748  NULL, /* m_reload */
749  NULL, /* m_traverse */
750  NULL, /* m_clear */
751  NULL, /* m_free */
752 };
753 
754 /* submodules only */
755 #include "mathutils_geometry.h"
756 #include "mathutils_interpolate.h"
757 #ifndef MATH_STANDALONE
758 # include "mathutils_bvhtree.h"
759 # include "mathutils_kdtree.h"
760 # include "mathutils_noise.h"
761 #endif
762 
763 PyMODINIT_FUNC PyInit_mathutils(void)
764 {
765  PyObject *mod;
766  PyObject *submodule;
767  PyObject *sys_modules = PyImport_GetModuleDict();
768 
769  if (PyType_Ready(&vector_Type) < 0) {
770  return NULL;
771  }
772  if (PyType_Ready(&matrix_Type) < 0) {
773  return NULL;
774  }
775  if (PyType_Ready(&matrix_access_Type) < 0) {
776  return NULL;
777  }
778  if (PyType_Ready(&euler_Type) < 0) {
779  return NULL;
780  }
781  if (PyType_Ready(&quaternion_Type) < 0) {
782  return NULL;
783  }
784  if (PyType_Ready(&color_Type) < 0) {
785  return NULL;
786  }
787 
788  mod = PyModule_Create(&M_Mathutils_module_def);
789 
790  /* each type has its own new() function */
791  PyModule_AddType(mod, &vector_Type);
792  PyModule_AddType(mod, &matrix_Type);
793  PyModule_AddType(mod, &euler_Type);
794  PyModule_AddType(mod, &quaternion_Type);
795  PyModule_AddType(mod, &color_Type);
796 
797  /* submodule */
798  PyModule_AddObject(mod, "geometry", (submodule = PyInit_mathutils_geometry()));
799  /* XXX, python doesn't do imports with this usefully yet
800  * 'from mathutils.geometry import PolyFill'
801  * ...fails without this. */
802  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
803 
804  PyModule_AddObject(mod, "interpolate", (submodule = PyInit_mathutils_interpolate()));
805  /* XXX, python doesn't do imports with this usefully yet
806  * 'from mathutils.geometry import PolyFill'
807  * ...fails without this. */
808  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
809 
810 #ifndef MATH_STANDALONE
811  /* Noise submodule */
812  PyModule_AddObject(mod, "noise", (submodule = PyInit_mathutils_noise()));
813  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
814 
815  /* BVHTree submodule */
816  PyModule_AddObject(mod, "bvhtree", (submodule = PyInit_mathutils_bvhtree()));
817  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
818 
819  /* KDTree_3d submodule */
820  PyModule_AddObject(mod, "kdtree", (submodule = PyInit_mathutils_kdtree()));
821  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
822 #endif
823 
828 
829  return mod;
830 }
#define BLI_assert(a)
Definition: BLI_assert.h:58
A dynamically sized string ADT.
int BLI_dynstr_get_len(DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: BLI_dynstr.c:286
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
Definition: BLI_dynstr.c:358
void BLI_dynstr_get_cstring_ex(DynStr *__restrict ds, char *__restrict rets) ATTR_NONNULL()
Definition: BLI_dynstr.c:299
void normalize_m3(float R[3][3]) ATTR_NONNULL()
Definition: math_matrix.c:1919
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])
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#define CLAMP_MAX(a, c)
#define UNUSED(x)
#define LIKELY(x)
_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
PyObject * self
Definition: bpy_driver.c:185
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE void mult(const btTransform &t1, const btTransform &t2)
Set the current transform as the value of the product of two transforms.
Definition: btTransform.h:76
static struct PyModuleDef M_Mathutils_module_def
Definition: mathutils.c:742
static Mathutils_Callback * mathutils_callbacks[MATHUTILS_TOT_CB]
Definition: mathutils.c:582
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition: mathutils.c:698
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *UNUSED(closure))
Definition: mathutils.c:685
PyMODINIT_FUNC PyInit_mathutils(void)
Definition: mathutils.c:763
static int mathutils_array_parse_fast(float *array, int size, PyObject *value_fast, const char *error_prefix)
Definition: mathutils.c:51
int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
Definition: mathutils.c:533
#define SIGNMASK(i)
Definition: mathutils.c:531
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *UNUSED(closure))
Definition: mathutils.c:678
PyObject * mathutils_dynstr_to_py(struct DynStr *ds)
Definition: mathutils.c:564
int mathutils_array_parse_alloc_viseq(int **array, int **start_table, int **len_table, PyObject *value, const char *error_prefix)
Definition: mathutils.c:404
#define MATHUTILS_TOT_CB
Definition: mathutils.c:581
void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
Definition: mathutils.c:657
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
Definition: mathutils.c:85
void BaseMathObject_dealloc(BaseMathObject *self)
Definition: mathutils.c:722
int _BaseMathObject_WriteCallback(BaseMathObject *self)
Definition: mathutils.c:616
int mathutils_int_array_parse(int *array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:325
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
Definition: mathutils.c:551
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:361
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition: mathutils.c:584
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:283
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
Definition: mathutils.c:670
char BaseMathObject_is_wrapped_doc[]
Definition: mathutils.c:676
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[]
Definition: mathutils.c:683
static struct PyMethodDef M_Mathutils_methods[]
Definition: mathutils.c:738
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
Definition: mathutils.c:118
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
Definition: mathutils.c:471
char BaseMathObject_owner_doc[]
Definition: mathutils.c:669
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
Definition: mathutils.c:643
char BaseMathObject_freeze_doc[]
Definition: mathutils.c:690
int BaseMathObject_clear(BaseMathObject *self)
Definition: mathutils.c:716
int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
Definition: mathutils.c:629
int _BaseMathObject_ReadCallback(BaseMathObject *self)
Definition: mathutils.c:603
void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self)
Definition: mathutils.c:662
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
Definition: mathutils.c:710
int mathutils_array_parse_alloc(float **array, int array_min, PyObject *value, const char *error_prefix)
Definition: mathutils.c:215
#define MU_ARRAY_ZERO
Definition: mathutils.h:184
#define MU_ARRAY_FLAGS
Definition: mathutils.h:189
@ BASE_MATH_FLAG_IS_WRAP
Definition: mathutils.h:44
@ BASE_MATH_FLAG_IS_FROZEN
Definition: mathutils.h:49
#define MU_ARRAY_SPILL
Definition: mathutils.h:187
#define BaseMath_ReadCallback(_self)
Definition: mathutils.h:128
PyTypeObject color_Type
#define ColorObject_Check(v)
PyTypeObject euler_Type
#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
PyTypeObject matrix_Type
#define MatrixObject_Check(v)
PyTypeObject quaternion_Type
#define QuaternionObject_Check(v)
PyTypeObject vector_Type
#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)
return ret
BaseMathSetIndexFunc set_index
Definition: mathutils.h:114
BaseMathGetIndexFunc get_index
Definition: mathutils.h:113
BaseMathSetFunc set
Definition: mathutils.h:112
BaseMathGetFunc get
Definition: mathutils.h:111
ccl_device_inline int mod(int x, int m)
Definition: util_math.h:405
uint len