Blender V5.3
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
18namespace blender {
19
20struct DynStr;
21
24extern char BaseMathObject_is_valid_doc[];
25extern char BaseMathObject_owner_doc[];
26
27[[nodiscard]] PyObject *_BaseMathObject_new_impl(PyTypeObject *root_type, PyTypeObject *base_type);
28
29#define BASE_MATH_NEW(struct_name, root_type, base_type) \
30 ((struct_name *)_BaseMathObject_new_impl(&root_type, base_type))
31
33enum {
53};
54#define BASE_MATH_FLAG_DEFAULT 0
55
56#define BASE_MATH_MEMBERS(_data) \
57 \
58 PyObject_HEAD \
59 float *_data; \
60 \
61 PyObject *cb_user; \
62 \
63 unsigned char cb_type; \
64 \
68 unsigned char cb_subtype; \
69 \
70 unsigned char flag
71
72struct BaseMathObject {
74};
75
76} // namespace blender
77
78/* types */
79#include "mathutils_Color.hh" // IWYU pragma: export
80#include "mathutils_Euler.hh" // IWYU pragma: export
81#include "mathutils_Matrix.hh" // IWYU pragma: export
82#include "mathutils_Quaternion.hh" // IWYU pragma: export
83#include "mathutils_Vector.hh" // IWYU pragma: export
84
85namespace blender {
86
87/* avoid checking all types */
88#define BaseMathObject_CheckExact(v) (Py_TYPE(v)->tp_dealloc == (destructor)BaseMathObject_dealloc)
89
90[[nodiscard]] PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
91[[nodiscard]] PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *);
92[[nodiscard]] PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *);
93[[nodiscard]] PyObject *BaseMathObject_is_valid_get(BaseMathObject *self, void *);
94
95extern char BaseMathObject_freeze_doc[];
96[[nodiscard]] PyObject *BaseMathObject_freeze(BaseMathObject *self);
97
98int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg);
99int BaseMathObject_clear(BaseMathObject *self);
100void BaseMathObject_dealloc(BaseMathObject *self);
101int BaseMathObject_is_gc(BaseMathObject *self);
102
103PyMODINIT_FUNC PyInit_mathutils();
104
105[[nodiscard]] int EXPP_FloatsAreEqual(float af, float bf, int maxDiff);
106[[nodiscard]] int EXPP_VectorsAreEqual(const float *vecA,
107 const float *vecB,
108 int size,
109 int floatSteps);
110
112using BaseMathCheckFunc = int (*)(BaseMathObject *);
114using BaseMathGetFunc = int (*)(BaseMathObject *, int);
116using BaseMathSetFunc = int (*)(BaseMathObject *, int);
118using BaseMathGetIndexFunc = int (*)(BaseMathObject *, int, int);
119/** Same as #BaseMathSetFunc but only for an index. */
120using BaseMathSetIndexFunc = int (*)(BaseMathObject *, int, int);
128};
129
130[[nodiscard]] unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb);
131
132[[nodiscard]] int _BaseMathObject_CheckCallback(BaseMathObject *self);
133[[nodiscard]] int _BaseMathObject_ReadCallback(BaseMathObject *self);
134[[nodiscard]] int _BaseMathObject_WriteCallback(BaseMathObject *self);
135[[nodiscard]] int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index);
136[[nodiscard]] int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index);
139 const char *error_prefix);
141 Py_buffer *view,
142 int flags);
143
145void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self);
146
147/* since this is called so often avoid where possible */
148#define BaseMath_CheckCallback(_self) \
149 (((_self)->cb_user ? _BaseMathObject_CheckCallback((BaseMathObject *)_self) : 0))
150#define BaseMath_ReadCallback(_self) \
151 (((_self)->cb_user ? _BaseMathObject_ReadCallback((BaseMathObject *)_self) : 0))
152#define BaseMath_WriteCallback(_self) \
153 (((_self)->cb_user ? _BaseMathObject_WriteCallback((BaseMathObject *)_self) : 0))
154#define BaseMath_ReadIndexCallback(_self, _index) \
155 (((_self)->cb_user ? _BaseMathObject_ReadIndexCallback((BaseMathObject *)_self, _index) : 0))
156#define BaseMath_WriteIndexCallback(_self, _index) \
157 (((_self)->cb_user ? _BaseMathObject_WriteIndexCallback((BaseMathObject *)_self, _index) : 0))
158
159/* support BASE_MATH_FLAG_IS_FROZEN */
160#define BaseMath_ReadCallback_ForWrite(_self) \
161 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
162 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
163 (BaseMath_ReadCallback(_self)))
164
165#define BaseMath_ReadIndexCallback_ForWrite(_self, _index) \
166 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
167 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
168 (BaseMath_ReadIndexCallback(_self, _index)))
169
170#define BaseMath_Prepare_ForWrite(_self) \
171 (UNLIKELY((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) ? \
172 (_BaseMathObject_RaiseFrozenExc((BaseMathObject *)_self), -1) : \
173 0)
174
175#define BaseMathObject_Prepare_ForHash(_self) \
176 (UNLIKELY(((_self)->flag & BASE_MATH_FLAG_IS_FROZEN) == 0) ? \
177 (_BaseMathObject_RaiseNotFrozenExc((BaseMathObject *)_self), -1) : \
178 0)
180 * Helper to de-duplicate checks for in-place resizing.
181 * \return -1 and set an exception if the vector `_self` cannot be resized.
182 */
183#define BaseMathObject_Prepare_ForResize(_self, error_prefix) \
184 _BaseMathObject_ResizeOkOrRaiseExc((BaseMathObject *)_self, error_prefix)
185
189 * \param _flags: The `flags` argument forwarded from #PyBufferProcs::bf_getbuffer.
190 * \return -1 and set an exception if the vector `_self` does not support buffer access.
191 */
192#define BaseMath_Prepare_ForBufferAccess(_self, _view, _flags) \
193 _BaseMathObject_RaiseBufferViewExc((BaseMathObject *)_self, _view, _flags)
194
195/* utility func */
200[[nodiscard]] int mathutils_array_parse(
201 float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix);
205[[nodiscard]] int mathutils_array_parse_alloc(float **array,
206 int array_num_min,
207 PyObject *value,
208 const char *error_prefix);
212[[nodiscard]] int mathutils_array_parse_alloc_v(float **array,
213 int array_dim,
214 PyObject *value,
215 const char *error_prefix);
219[[nodiscard]] int mathutils_int_array_parse(int *array,
220 int array_dim,
221 PyObject *value,
222 const char *error_prefix);
226[[nodiscard]] int mathutils_array_parse_alloc_vi(int **array,
227 int array_dim,
228 PyObject *value,
229 const char *error_prefix);
233[[nodiscard]] bool mathutils_array_parse_alloc_viseq(PyObject *value,
234 const char *error_prefix,
235 Array<int> &r_offsets,
236 Array<int> &r_data);
237[[nodiscard]] int mathutils_any_to_rotmat(float rmat[3][3],
238 PyObject *value,
239 const char *error_prefix);
240
241/**
242 * Returns true when a slice does *not* address every element of an `array_num`.
243 */
244[[nodiscard]] inline bool mathutils_slice_is_subset(Py_ssize_t start,
245 Py_ssize_t step,
246 Py_ssize_t slice_length,
247 Py_ssize_t array_num)
248{
249 return !((slice_length == array_num) &&
250 /* All forward `[:]`. */
251 ((start == 0 && step == 1) ||
252 /* All reverse `[::-1]`. */
253 (start == array_num - 1 && step == -1)));
254}
255
261[[nodiscard]] Py_hash_t mathutils_array_hash(const float *array, size_t array_len);
262
264#define MU_ARRAY_ZERO (1u << 30)
269#define MU_ARRAY_SPILL (1u << 31)
270
271#define MU_ARRAY_FLAGS (MU_ARRAY_ZERO | MU_ARRAY_SPILL)
272
284[[nodiscard]] int column_vector_multiplication(float r_vec[4],
285 VectorObject *vec,
286 MatrixObject *mat);
287
288#ifndef MATH_STANDALONE
289/* dynstr as python string utility functions, frees 'ds'! */
290[[nodiscard]] PyObject *mathutils_dynstr_to_py(struct DynStr *ds);
291#endif
292
293} // namespace blender
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
int _BaseMathObject_ResizeOkOrRaiseExc(BaseMathObject *self, const char *error_prefix)
Definition mathutils.cc:637
char BaseMathObject_owner_doc[]
Definition mathutils.cc:686
char BaseMathObject_freeze_doc[]
Definition mathutils.cc:716
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
Definition mathutils.cc:511
bool mathutils_slice_is_subset(Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, Py_ssize_t array_num)
Definition mathutils.hh:241
void BaseMathObject_dealloc(BaseMathObject *self)
Definition mathutils.cc:766
bool mathutils_array_parse_alloc_viseq(PyObject *value, const char *error_prefix, Array< int > &r_offsets, Array< int > &r_data)
Definition mathutils.cc:379
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
Definition mathutils.cc:69
PyObject * mathutils_dynstr_to_py(DynStr *ds)
Definition mathutils.cc:523
int(*)(BaseMathObject *, int) BaseMathSetFunc
Definition mathutils.hh:113
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *)
Definition mathutils.cc:690
int(*)(BaseMathObject *, int) BaseMathGetFunc
Definition mathutils.hh:111
int BaseMathObject_clear(BaseMathObject *self)
Definition mathutils.cc:748
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition mathutils.cc:725
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:340
int(*)(BaseMathObject *, int, int) BaseMathGetIndexFunc
Definition mathutils.hh:115
int BaseMathObject_is_gc(BaseMathObject *self)
Definition mathutils.cc:788
int mathutils_array_parse_alloc(float **array, int array_num_min, PyObject *value, const char *error_prefix)
Definition mathutils.cc:195
int(*)(BaseMathObject *) BaseMathCheckFunc
Definition mathutils.hh:109
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *)
Definition mathutils.cc:705
int(*)(BaseMathObject *, int, int) BaseMathSetIndexFunc
Definition mathutils.hh:117
void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self)
Definition mathutils.cc:631
PyObject * BaseMathObject_is_valid_get(BaseMathObject *self, void *)
Definition mathutils.cc:711
PyMODINIT_FUNC PyInit_mathutils()
Definition mathutils.cc:846
BMesh const char void * data
PyObject * _BaseMathObject_new_impl(PyTypeObject *root_type, PyTypeObject *base_type)
Definition mathutils.cc:793
int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
Definition mathutils.cc:492
@ BASE_MATH_FLAG_IS_WRAP
Definition mathutils.hh:39
@ BASE_MATH_FLAG_IS_FROZEN
Definition mathutils.hh:44
@ BASE_MATH_FLAG_HAS_BUFFER_VIEW
Definition mathutils.hh:52
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
int column_vector_multiplication(float r_vec[4], VectorObject *vec, MatrixObject *mat)
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition mathutils.cc:543
int _BaseMathObject_WriteCallback(BaseMathObject *self)
Definition mathutils.cc:585
int _BaseMathObject_ReadCallback(BaseMathObject *self)
Definition mathutils.cc:570
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *)
Definition mathutils.cc:698
char BaseMathObject_is_valid_doc[]
Definition mathutils.cc:710
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
Definition mathutils.cc:742
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
Definition mathutils.cc:426
char BaseMathObject_is_frozen_doc[]
Definition mathutils.cc:703
int mathutils_int_array_parse(int *array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:305
void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
Definition mathutils.cc:626
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
Definition mathutils.cc:612
int _BaseMathObject_RaiseBufferViewExc(BaseMathObject *self, Py_buffer *view, int flags)
Definition mathutils.cc:660
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:262
int _BaseMathObject_CheckCallback(BaseMathObject *self)
Definition mathutils.cc:561
char BaseMathObject_is_wrapped_doc[]
Definition mathutils.cc:696
int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
Definition mathutils.cc:598
BaseMathCheckFunc check
Definition mathutils.hh:120
BaseMathGetIndexFunc get_index
Definition mathutils.hh:123
BaseMathSetIndexFunc set_index
Definition mathutils.hh:124