Blender V5.0
mathutils_interpolate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <Python.h>
10
11#include "mathutils.hh"
13
14#include "BLI_math_geom.h"
15
16#ifndef MATH_STANDALONE /* define when building outside blender */
17# include "MEM_guardedalloc.h"
18#endif
19
20/* ---------------------------------WEIGHT CALCULATION ----------------------- */
21
22#ifndef MATH_STANDALONE
23
25 /* Wrap. */
26 M_Interpolate_poly_3d_calc_doc,
27 ".. function:: poly_3d_calc(veclist, pt, /)\n"
28 "\n"
29 " Calculate barycentric weights for a point on a polygon.\n"
30 "\n"
31 " :arg veclist: Sequence of 3D positions.\n"
32 " :type veclist: Sequence[Sequence[float]]\n"
33 " :arg pt: 2D or 3D position."
34 " :type pt: Sequence[float]"
35 " :return: list of per-vector weights.\n"
36 " :rtype: list[float]\n");
37static PyObject *M_Interpolate_poly_3d_calc(PyObject * /*self*/, PyObject *args)
38{
39 float fp[3];
40 float (*vecs)[3];
41 Py_ssize_t len;
42
43 PyObject *point, *veclist, *ret;
44 int i;
45
46 if (!PyArg_ParseTuple(args, "OO:poly_3d_calc", &veclist, &point)) {
47 return nullptr;
48 }
49
51 fp, 2, 3 | MU_ARRAY_ZERO, point, "pt must be a 2-3 dimensional vector") == -1)
52 {
53 return nullptr;
54 }
55
56 len = mathutils_array_parse_alloc_v(((float **)&vecs), 3, veclist, __func__);
57 if (len == -1) {
58 return nullptr;
59 }
60
61 if (len) {
62 float *weights = MEM_malloc_arrayN<float>(size_t(len), __func__);
63
64 interp_weights_poly_v3(weights, vecs, len, fp);
65
66 ret = PyList_New(len);
67 for (i = 0; i < len; i++) {
68 PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i]));
69 }
70
71 MEM_freeN(weights);
72
73 PyMem_Free(vecs);
74 }
75 else {
76 ret = PyList_New(0);
77 }
78
79 return ret;
80}
81
82#endif /* !MATH_STANDALONE */
83
84static PyMethodDef M_Interpolate_methods[] = {
85#ifndef MATH_STANDALONE
86 {"poly_3d_calc",
87 (PyCFunction)M_Interpolate_poly_3d_calc,
88 METH_VARARGS,
89 M_Interpolate_poly_3d_calc_doc},
90#endif
91 {nullptr, nullptr, 0, nullptr},
92};
93
95 /* Wrap. */
96 M_Interpolate_doc,
97 "The Blender interpolate module.");
98static PyModuleDef M_Interpolate_module_def = {
99 /*m_base*/ PyModuleDef_HEAD_INIT,
100 /*m_name*/ "mathutils.interpolate",
101 /*m_doc*/ M_Interpolate_doc,
102 /*m_size*/ 0,
103 /*m_methods*/ M_Interpolate_methods,
104 /*m_slots*/ nullptr,
105 /*m_traverse*/ nullptr,
106 /*m_clear*/ nullptr,
107 /*m_free*/ nullptr,
108};
109
110/*----------------------------MODULE INIT-------------------------*/
111
113{
114 PyObject *submodule = PyModule_Create(&M_Interpolate_module_def);
115 return submodule;
116}
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
Read Guarded memory(de)allocation.
nullptr float
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
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_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:259
#define MU_ARRAY_ZERO
Definition mathutils.hh:239
static PyMethodDef M_Interpolate_methods[]
PyMODINIT_FUNC PyInit_mathutils_interpolate()
static PyObject * M_Interpolate_poly_3d_calc(PyObject *, PyObject *args)
PyDoc_STRVAR(M_Interpolate_poly_3d_calc_doc, ".. function:: poly_3d_calc(veclist, pt, /)\n" "\n" " Calculate barycentric weights for a point on a polygon.\n" "\n" " :arg veclist: Sequence of 3D positions.\n" " :type veclist: Sequence[Sequence[float]]\n" " :arg pt: 2D or 3D position." " :type pt: Sequence[float]" " :return: list of per-vector weights.\n" " :rtype: list[float]\n")
static PyModuleDef M_Interpolate_module_def
return ret
i
Definition text_draw.cc:230
uint len