Blender V5.0
mathutils.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <Python.h>
12
13/* Can cast different mathutils types to this, use for generic functions. */
14
15#include "BLI_array.hh"
16#include "BLI_vector.hh"
17
18struct DynStr;
19
22extern char BaseMathObject_is_valid_doc[];
23extern char BaseMathObject_owner_doc[];
24
25[[nodiscard]] PyObject *_BaseMathObject_new_impl(PyTypeObject *root_type, PyTypeObject *base_type);
26
27#define BASE_MATH_NEW(struct_name, root_type, base_type) \
28 ((struct_name *)_BaseMathObject_new_impl(&root_type, base_type))
29
31enum {
51};
52#define BASE_MATH_FLAG_DEFAULT 0
53
54#define BASE_MATH_MEMBERS(_data) \
55 \
56 PyObject_VAR_HEAD \
57 float *_data; \
58 \
59 PyObject *cb_user; \
60 \
61 unsigned char cb_type; \
62 \
64 unsigned char cb_subtype; \
65 \
66 unsigned char flag
70};
71
72/* types */
73#include "mathutils_Color.hh" // IWYU pragma: export
74#include "mathutils_Euler.hh" // IWYU pragma: export
75#include "mathutils_Matrix.hh" // IWYU pragma: export
76#include "mathutils_Quaternion.hh" // IWYU pragma: export
77#include "mathutils_Vector.hh" // IWYU pragma: export
78
79/* avoid checking all types */
80#define BaseMathObject_CheckExact(v) (Py_TYPE(v)->tp_dealloc == (destructor)BaseMathObject_dealloc)
81
82[[nodiscard]] PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
83[[nodiscard]] PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *);
84[[nodiscard]] PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *);
85[[nodiscard]] PyObject *BaseMathObject_is_valid_get(BaseMathObject *self, void *);
86
87extern char BaseMathObject_freeze_doc[];
88[[nodiscard]] PyObject *BaseMathObject_freeze(BaseMathObject *self);
89
90int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg);
94
95PyMODINIT_FUNC PyInit_mathutils();
96
97[[nodiscard]] int EXPP_FloatsAreEqual(float af, float bf, int maxDiff);
98[[nodiscard]] int EXPP_VectorsAreEqual(const float *vecA,
99 const float *vecB,
100 int size,
101 int floatSteps);
102
104using BaseMathCheckFunc = int (*)(BaseMathObject *);
106using BaseMathGetFunc = int (*)(BaseMathObject *, int);
108using BaseMathSetFunc = int (*)(BaseMathObject *, int);
110using BaseMathGetIndexFunc = int (*)(BaseMathObject *, int, int);
112using BaseMathSetIndexFunc = int (*)(BaseMathObject *, int, int);
120};
121
122[[nodiscard]] unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb);
123
127[[nodiscard]] int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index);
128[[nodiscard]] int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index);
131 const char *error_prefix);
133 Py_buffer *view,
134 int flags);
135
138
139/* since this is called so often avoid where possible */
140#define BaseMath_CheckCallback(_self) \
141 (((_self)->cb_user ? _BaseMathObject_CheckCallback((BaseMathObject *)_self) : 0))
142#define BaseMath_ReadCallback(_self) \
143 (((_self)->cb_user ? _BaseMathObject_ReadCallback((BaseMathObject *)_self) : 0))
144#define BaseMath_WriteCallback(_self) \
145 (((_self)->cb_user ? _BaseMathObject_WriteCallback((BaseMathObject *)_self) : 0))
146#define BaseMath_ReadIndexCallback(_self, _index) \
147 (((_self)->cb_user ? _BaseMathObject_ReadIndexCallback((BaseMathObject *)_self, _index) : 0))
148#define BaseMath_WriteIndexCallback(_self, _index) \
149 (((_self)->cb_user ? _BaseMathObject_WriteIndexCallback((BaseMathObject *)_self, _index) : 0))
150
151/* support BASE_MATH_FLAG_IS_FROZEN */
152#define BaseMath_ReadCallback_ForWrite(_self) \
153 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
154 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
155 (BaseMath_ReadCallback(_self)))
157#define BaseMath_ReadIndexCallback_ForWrite(_self, _index) \
158 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
159 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
160 (BaseMath_ReadIndexCallback(_self, _index)))
162#define BaseMath_Prepare_ForWrite(_self) \
163 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
164 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
165 0)
167#define BaseMathObject_Prepare_ForHash(_self) \
168 (UNLIKELY(((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) == 0) ? \
169 (_BaseMathObject_RaiseNotFrozenExc((BaseMathObject *)_self), -1) : \
170 0)
174 */
175#define BaseMathObject_Prepare_ForResize(_self, error_prefix) \
176 _BaseMathObject_ResizeOkOrRaiseExc((BaseMathObject *)_self, error_prefix)
177
183 */
184#define BaseMath_Prepare_ForBufferAccess(_self, _view, _flags) \
185 _BaseMathObject_RaiseBufferViewExc((BaseMathObject *)_self, _view, _flags)
186
187/* utility func */
192[[nodiscard]] int mathutils_array_parse(
193 float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix);
197[[nodiscard]] int mathutils_array_parse_alloc(float **array,
198 int array_num_min,
199 PyObject *value,
200 const char *error_prefix);
204[[nodiscard]] int mathutils_array_parse_alloc_v(float **array,
205 int array_dim,
206 PyObject *value,
207 const char *error_prefix);
211[[nodiscard]] int mathutils_int_array_parse(int *array,
212 int array_dim,
213 PyObject *value,
214 const char *error_prefix);
218[[nodiscard]] int mathutils_array_parse_alloc_vi(int **array,
219 int array_dim,
220 PyObject *value,
221 const char *error_prefix);
225[[nodiscard]] bool mathutils_array_parse_alloc_viseq(PyObject *value,
226 const char *error_prefix,
228[[nodiscard]] int mathutils_any_to_rotmat(float rmat[3][3],
229 PyObject *value,
230 const char *error_prefix);
231
237[[nodiscard]] Py_hash_t mathutils_array_hash(const float *array, size_t array_len);
238
239/* zero remaining unused elements of the array */
240#define MU_ARRAY_ZERO (1u << 30)
241/* ignore larger py sequences than requested (just use first elements),
242 * handy when using 3d vectors as 2d */
243#define MU_ARRAY_SPILL (1u << 31)
245#define MU_ARRAY_FLAGS (MU_ARRAY_ZERO | MU_ARRAY_SPILL)
246
258[[nodiscard]] int column_vector_multiplication(float r_vec[4],
259 VectorObject *vec,
260 MatrixObject *mat);
261
262#ifndef MATH_STANDALONE
263/* dynstr as python string utility functions */
264/* dynstr as python string utility functions, frees 'ds'! */
265[[nodiscard]] PyObject *mathutils_dynstr_to_py(struct DynStr *ds);
266#endif
static AppView * view
BMesh const char void * data
PyObject * self
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
char BaseMathObject_is_valid_doc[]
Definition mathutils.cc:685
char BaseMathObject_is_wrapped_doc[]
Definition mathutils.cc:671
char BaseMathObject_is_frozen_doc[]
Definition mathutils.cc:678
char BaseMathObject_owner_doc[]
Definition mathutils.cc:664
char BaseMathObject_freeze_doc[]
Definition mathutils.cc:691
int _BaseMathObject_CheckCallback(BaseMathObject *self)
Definition mathutils.cc:539
int _BaseMathObject_RaiseBufferViewExc(BaseMathObject *self, Py_buffer *view, int flags)
Definition mathutils.cc:638
int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
Definition mathutils.cc:470
bool mathutils_array_parse_alloc_viseq(PyObject *value, const char *error_prefix, blender::Array< blender::Vector< int > > &r_data)
Definition mathutils.cc:372
PyMODINIT_FUNC PyInit_mathutils()
Definition mathutils.cc:816
int mathutils_array_parse_alloc(float **array, int array_num_min, PyObject *value, const char *error_prefix)
Definition mathutils.cc:192
int BaseMathObject_is_gc(BaseMathObject *self)
Definition mathutils.cc:762
void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
Definition mathutils.cc:604
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
Definition mathutils.cc:68
void BaseMathObject_dealloc(BaseMathObject *self)
Definition mathutils.cc:740
int _BaseMathObject_WriteCallback(BaseMathObject *self)
Definition mathutils.cc:563
int mathutils_int_array_parse(int *array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:300
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
Definition mathutils.cc:489
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:96
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:335
int(*)(BaseMathObject *, int) BaseMathGetFunc
Definition mathutils.hh:105
int(*)(BaseMathObject *, int) BaseMathSetFunc
Definition mathutils.hh:107
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:259
int _BaseMathObject_ResizeOkOrRaiseExc(BaseMathObject *self, const char *error_prefix)
Definition mathutils.cc:615
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *)
Definition mathutils.cc:665
PyObject * _BaseMathObject_new_impl(PyTypeObject *root_type, PyTypeObject *base_type)
Definition mathutils.cc:767
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
Definition mathutils.cc:408
int column_vector_multiplication(float r_vec[4], VectorObject *vec, MatrixObject *mat)
int(*)(BaseMathObject *, int, int) BaseMathSetIndexFunc
Definition mathutils.hh:111
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *)
Definition mathutils.cc:680
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition mathutils.cc:699
int(*)(BaseMathObject *) BaseMathCheckFunc
Definition mathutils.hh:103
int(*)(BaseMathObject *, int, int) BaseMathGetIndexFunc
Definition mathutils.hh:109
#define BASE_MATH_MEMBERS(_data)
Definition mathutils.hh:54
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *)
Definition mathutils.cc:673
PyObject * mathutils_dynstr_to_py(struct DynStr *ds)
Definition mathutils.cc:501
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
Definition mathutils.cc:590
PyObject * BaseMathObject_is_valid_get(BaseMathObject *self, void *)
Definition mathutils.cc:686
int BaseMathObject_clear(BaseMathObject *self)
Definition mathutils.cc:722
int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
Definition mathutils.cc:576
int _BaseMathObject_ReadCallback(BaseMathObject *self)
Definition mathutils.cc:548
unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition mathutils.cc:521
void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self)
Definition mathutils.cc:609
@ BASE_MATH_FLAG_HAS_BUFFER_VIEW
Definition mathutils.hh:50
@ BASE_MATH_FLAG_IS_WRAP
Definition mathutils.hh:37
@ BASE_MATH_FLAG_IS_FROZEN
Definition mathutils.hh:42
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
Definition mathutils.cc:716
BaseMathSetIndexFunc set_index
Definition mathutils.hh:118
BaseMathCheckFunc check
Definition mathutils.hh:114
BaseMathGetIndexFunc get_index
Definition mathutils.hh:117
BaseMathSetFunc set
Definition mathutils.hh:116
BaseMathGetFunc get
Definition mathutils.hh:115