Blender V4.5
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/*-------------------------DOC STRINGS ---------------------------*/
22 /* Wrap. */
23 M_Interpolate_doc,
24 "The Blender interpolate module");
25
26/* ---------------------------------WEIGHT CALCULATION ----------------------- */
27
28#ifndef MATH_STANDALONE
29
31 /* Wrap. */
32 M_Interpolate_poly_3d_calc_doc,
33 ".. function:: poly_3d_calc(veclist, pt)\n"
34 "\n"
35 " Calculate barycentric weights for a point on a polygon.\n"
36 "\n"
37 " :arg veclist: Sequence of 3D positions.\n"
38 " :type veclist: Sequence[Sequence[float]]\n"
39 " :arg pt: 2D or 3D position."
40 " :type pt: Sequence[float]"
41 " :return: list of per-vector weights.\n"
42 " :rtype: list[float]\n");
43static PyObject *M_Interpolate_poly_3d_calc(PyObject * /*self*/, PyObject *args)
44{
45 float fp[3];
46 float(*vecs)[3];
47 Py_ssize_t len;
48
49 PyObject *point, *veclist, *ret;
50 int i;
51
52 if (!PyArg_ParseTuple(args, "OO:poly_3d_calc", &veclist, &point)) {
53 return nullptr;
54 }
55
57 fp, 2, 3 | MU_ARRAY_ZERO, point, "pt must be a 2-3 dimensional vector") == -1)
58 {
59 return nullptr;
60 }
61
62 len = mathutils_array_parse_alloc_v(((float **)&vecs), 3, veclist, __func__);
63 if (len == -1) {
64 return nullptr;
65 }
66
67 if (len) {
68 float *weights = MEM_malloc_arrayN<float>(size_t(len), __func__);
69
70 interp_weights_poly_v3(weights, vecs, len, fp);
71
72 ret = PyList_New(len);
73 for (i = 0; i < len; i++) {
74 PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i]));
75 }
76
77 MEM_freeN(weights);
78
79 PyMem_Free(vecs);
80 }
81 else {
82 ret = PyList_New(0);
83 }
84
85 return ret;
86}
87
88#endif /* !MATH_STANDALONE */
89
90static PyMethodDef M_Interpolate_methods[] = {
91#ifndef MATH_STANDALONE
92 {"poly_3d_calc",
93 (PyCFunction)M_Interpolate_poly_3d_calc,
94 METH_VARARGS,
95 M_Interpolate_poly_3d_calc_doc},
96#endif
97 {nullptr, nullptr, 0, nullptr},
98};
99
100static PyModuleDef M_Interpolate_module_def = {
101 /*m_base*/ PyModuleDef_HEAD_INIT,
102 /*m_name*/ "mathutils.interpolate",
103 /*m_doc*/ M_Interpolate_doc,
104 /*m_size*/ 0,
105 /*m_methods*/ M_Interpolate_methods,
106 /*m_slots*/ nullptr,
107 /*m_traverse*/ nullptr,
108 /*m_clear*/ nullptr,
109 /*m_free*/ nullptr,
110};
111
112/*----------------------------MODULE INIT-------------------------*/
113
115{
116 PyObject *submodule = PyModule_Create(&M_Interpolate_module_def);
117 return submodule;
118}
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
Read Guarded memory(de)allocation.
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:213
PyDoc_STRVAR(M_Interpolate_doc, "The Blender interpolate module")
static PyMethodDef M_Interpolate_methods[]
PyMODINIT_FUNC PyInit_mathutils_interpolate()
static PyObject * M_Interpolate_poly_3d_calc(PyObject *, PyObject *args)
static PyModuleDef M_Interpolate_module_def
return ret
i
Definition text_draw.cc:230
uint len