Blender  V2.93
mathutils.h
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 
17 #pragma once
18 
23 /* Can cast different mathutils types to this, use for generic funcs */
24 
25 #include "BLI_compiler_attrs.h"
26 
27 struct DynStr;
28 
29 extern char BaseMathObject_is_wrapped_doc[];
30 extern char BaseMathObject_is_frozen_doc[];
31 extern char BaseMathObject_owner_doc[];
32 
33 #define BASE_MATH_NEW(struct_name, root_type, base_type) \
34  ((struct_name *)((base_type ? (base_type)->tp_alloc(base_type, 0) : \
35  _PyObject_GC_New(&(root_type)))))
36 
38 enum {
50 };
51 #define BASE_MATH_FLAG_DEFAULT 0
52 
53 #define BASE_MATH_MEMBERS(_data) \
54  \
55  PyObject_VAR_HEAD \
56  float *_data; \
57  \
58  PyObject *cb_user; \
59  \
60  unsigned char cb_type; \
61  \
63  unsigned char cb_subtype; \
64  \
65  unsigned char flag
66 
67 typedef struct {
70 
71 /* types */
72 #include "mathutils_Color.h"
73 #include "mathutils_Euler.h"
74 #include "mathutils_Matrix.h"
75 #include "mathutils_Quaternion.h"
76 #include "mathutils_Vector.h"
77 
78 /* avoid checking all types */
79 #define BaseMathObject_CheckExact(v) (Py_TYPE(v)->tp_dealloc == (destructor)BaseMathObject_dealloc)
80 
81 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
83 PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *);
84 
85 extern char BaseMathObject_freeze_doc[];
86 PyObject *BaseMathObject_freeze(BaseMathObject *self);
87 
88 int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg);
91 
92 PyMODINIT_FUNC PyInit_mathutils(void);
93 
94 int EXPP_FloatsAreEqual(float A, float B, int maxDiff);
95 int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps);
96 
98 
100 typedef int (*BaseMathCheckFunc)(BaseMathObject *);
102 typedef int (*BaseMathGetFunc)(BaseMathObject *, int);
104 typedef int (*BaseMathSetFunc)(BaseMathObject *, int);
106 typedef int (*BaseMathGetIndexFunc)(BaseMathObject *, int, int);
108 typedef int (*BaseMathSetIndexFunc)(BaseMathObject *, int, int);
109 
116 };
117 
119 
124 
127 
128 /* since this is called so often avoid where possible */
129 #define BaseMath_ReadCallback(_self) \
130  (((_self)->cb_user ? _BaseMathObject_ReadCallback((BaseMathObject *)_self) : 0))
131 #define BaseMath_WriteCallback(_self) \
132  (((_self)->cb_user ? _BaseMathObject_WriteCallback((BaseMathObject *)_self) : 0))
133 #define BaseMath_ReadIndexCallback(_self, _index) \
134  (((_self)->cb_user ? _BaseMathObject_ReadIndexCallback((BaseMathObject *)_self, _index) : 0))
135 #define BaseMath_WriteIndexCallback(_self, _index) \
136  (((_self)->cb_user ? _BaseMathObject_WriteIndexCallback((BaseMathObject *)_self, _index) : 0))
137 
138 /* support BASE_MATH_FLAG_IS_FROZEN */
139 #define BaseMath_ReadCallback_ForWrite(_self) \
140  (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
141  (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
142  (BaseMath_ReadCallback(_self)))
143 
144 #define BaseMath_ReadIndexCallback_ForWrite(_self, _index) \
145  (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
146  (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
147  (BaseMath_ReadIndexCallback(_self, _index)))
148 
149 #define BaseMath_Prepare_ForWrite(_self) \
150  (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
151  (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
152  0)
153 
154 #define BaseMathObject_Prepare_ForHash(_self) \
155  (UNLIKELY(((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) == 0) ? \
156  (_BaseMathObject_RaiseNotFrozenExc((BaseMathObject *)_self), -1) : \
157  0)
158 
159 /* utility func */
161  float *array, int array_min, int array_max, PyObject *value, const char *error_prefix);
163  int array_min,
164  PyObject *value,
165  const char *error_prefix);
167  int array_dim,
168  PyObject *value,
169  const char *error_prefix);
171  int array_dim,
172  PyObject *value,
173  const char *error_prefix);
175  int array_dim,
176  PyObject *value,
177  const char *error_prefix);
179  int **array, int **start_table, int **len_table, PyObject *value, const char *error_prefix);
180 int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix);
181 
182 Py_hash_t mathutils_array_hash(const float *float_array, size_t array_len);
183 
184 /* zero remaining unused elements of the array */
185 #define MU_ARRAY_ZERO (1u << 30)
186 /* ignore larger py sequences than requested (just use first elements),
187  * handy when using 3d vectors as 2d */
188 #define MU_ARRAY_SPILL (1u << 31)
189 
190 #define MU_ARRAY_FLAGS (MU_ARRAY_ZERO | MU_ARRAY_SPILL)
191 
192 int column_vector_multiplication(float r_vec[4], VectorObject *vec, MatrixObject *mat);
193 
194 #ifndef MATH_STANDALONE
195 /* dynstr as python string utility functions */
196 PyObject *mathutils_dynstr_to_py(struct DynStr *ds);
197 #endif
#define A
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
int(* BaseMathGetFunc)(BaseMathObject *, int)
Definition: mathutils.h:101
int EXPP_FloatsAreEqual(float A, float B, int maxDiff)
Definition: mathutils.c:533
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition: mathutils.c:698
PyMODINIT_FUNC PyInit_mathutils(void)
Definition: mathutils.c:763
int(* BaseMathGetIndexFunc)(BaseMathObject *, int, int)
Definition: mathutils.h:105
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *)
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
int(* BaseMathSetIndexFunc)(BaseMathObject *, int, int)
Definition: mathutils.h:107
void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
Definition: mathutils.c:657
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(* BaseMathSetFunc)(BaseMathObject *, int)
Definition: mathutils.h:103
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:361
Py_hash_t mathutils_array_hash(const float *float_array, size_t array_len)
Definition: mathutils.c:85
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:283
char BaseMathObject_is_wrapped_doc[]
Definition: mathutils.c:676
char BaseMathObject_is_frozen_doc[]
Definition: mathutils.c:683
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
int column_vector_multiplication(float r_vec[4], VectorObject *vec, MatrixObject *mat)
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *)
@ BASE_MATH_FLAG_IS_WRAP
Definition: mathutils.h:44
@ BASE_MATH_FLAG_IS_FROZEN
Definition: mathutils.h:49
#define BASE_MATH_MEMBERS(_data)
Definition: mathutils.h:53
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
int(* BaseMathCheckFunc)(BaseMathObject *)
Definition: mathutils.h:99
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *)
unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition: mathutils.c:584
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 B
BaseMathSetIndexFunc set_index
Definition: mathutils.h:114
BaseMathCheckFunc check
Definition: mathutils.h:110
BaseMathGetIndexFunc get_index
Definition: mathutils.h:113
BaseMathSetFunc set
Definition: mathutils.h:112
BaseMathGetFunc get
Definition: mathutils.h:111