Blender V5.3
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_c.hh"
15
16namespace blender {
17
18#ifndef MATH_STANDALONE /* define when building outside blender */
19# include "MEM_guardedalloc.h"
20#endif
21
22/* ---------------------------------WEIGHT CALCULATION ----------------------- */
23
24#ifndef MATH_STANDALONE
25
27 /* Wrap. */
28 M_Interpolate_poly_3d_calc_doc,
29 ".. function:: poly_3d_calc(veclist, pt, /)\n"
30 "\n"
31 " Calculate barycentric weights for a point on a polygon.\n"
32 "\n"
33 " :param veclist: Sequence of 3D positions.\n"
34 " :type veclist: Sequence[Sequence[float]]\n"
35 " :param pt: 2D or 3D position.\n"
36 " :type pt: Sequence[float]\n"
37 " :return: A list of weights, one per vertex in *veclist*.\n"
38 " :rtype: list[float]\n");
39static PyObject *M_Interpolate_poly_3d_calc(PyObject * /*self*/, PyObject *args)
40{
41 float fp[3];
42 float (*vecs)[3];
43 Py_ssize_t len;
44
45 PyObject *point, *veclist, *ret;
46 int i;
47
48 if (!PyArg_ParseTuple(args,
49 "O" /* `veclist` */
50 "O" /* `pt` */
51 ":poly_3d_calc",
52 &veclist,
53 &point))
54 {
55 return nullptr;
56 }
57
59 fp, 2, 3 | MU_ARRAY_ZERO, point, "pt must be a 2-3 dimensional vector") == -1)
60 {
61 return nullptr;
62 }
63
64 len = mathutils_array_parse_alloc_v((reinterpret_cast<float **>(&vecs)), 3, veclist, __func__);
65 if (len == -1) {
66 return nullptr;
67 }
68
69 if (len) {
70 float *weights = MEM_new_array_uninitialized<float>(size_t(len), __func__);
71
72 interp_weights_poly_v3(weights, vecs, len, fp);
73
74 ret = PyList_New(len);
75 for (i = 0; i < len; i++) {
76 PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i]));
77 }
78
79 MEM_delete(weights);
80
81 PyMem_Free(vecs);
82 }
83 else {
84 ret = PyList_New(0);
85 }
86
87 return ret;
88}
89
90#endif /* !MATH_STANDALONE */
91
92static PyMethodDef M_Interpolate_methods[] = {
93#ifndef MATH_STANDALONE
94 {"poly_3d_calc",
95 static_cast<PyCFunction>(M_Interpolate_poly_3d_calc),
96 METH_VARARGS,
97 M_Interpolate_poly_3d_calc_doc},
98#endif
99 {nullptr, nullptr, 0, nullptr},
100};
101
103 /* Wrap. */
104 M_Interpolate_doc,
105 "The Blender interpolate module.");
106static PyModuleDef M_Interpolate_module_def = {
107 /*m_base*/ PyModuleDef_HEAD_INIT,
108 /*m_name*/ "mathutils.interpolate",
109 /*m_doc*/ M_Interpolate_doc,
110 /*m_size*/ 0,
111 /*m_methods*/ M_Interpolate_methods,
112 /*m_slots*/ nullptr,
113 /*m_traverse*/ nullptr,
114 /*m_clear*/ nullptr,
115 /*m_free*/ nullptr,
116};
117
118/*----------------------------MODULE INIT-------------------------*/
119
121{
122 PyObject *submodule = PyModule_Create(&M_Interpolate_module_def);
123 return submodule;
124}
125
126} // namespace blender
Read Blender memory allocation and freeing.
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define MU_ARRAY_ZERO
Definition mathutils.hh:261
BMesh Iterators.
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyMethodDef M_Interpolate_methods[]
static PyObject * M_Interpolate_poly_3d_calc(PyObject *, PyObject *args)
PyMODINIT_FUNC PyInit_mathutils_interpolate()
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
static PyModuleDef M_Interpolate_module_def
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:262
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float