Blender  V2.93
mathutils_interpolate.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <Python.h>
22 
23 #include "mathutils.h"
24 #include "mathutils_interpolate.h"
25 
26 #include "BLI_math.h"
27 #include "BLI_utildefines.h"
28 
29 #ifndef MATH_STANDALONE /* define when building outside blender */
30 # include "MEM_guardedalloc.h"
31 #endif
32 
33 /*-------------------------DOC STRINGS ---------------------------*/
34 PyDoc_STRVAR(M_Interpolate_doc, "The Blender interpolate module");
35 
36 /* ---------------------------------WEIGHT CALCULATION ----------------------- */
37 
38 #ifndef MATH_STANDALONE
39 
40 PyDoc_STRVAR(M_Interpolate_poly_3d_calc_doc,
41  ".. function:: poly_3d_calc(veclist, pt)\n"
42  "\n"
43  " Calculate barycentric weights for a point on a polygon.\n"
44  "\n"
45  " :arg veclist: list of vectors\n"
46  " :arg pt: point"
47  " :rtype: list of per-vector weights\n");
48 static PyObject *M_Interpolate_poly_3d_calc(PyObject *UNUSED(self), PyObject *args)
49 {
50  float fp[3];
51  float(*vecs)[3];
52  Py_ssize_t len;
53 
54  PyObject *point, *veclist, *ret;
55  int i;
56 
57  if (!PyArg_ParseTuple(args, "OO!:poly_3d_calc", &veclist, &vector_Type, &point)) {
58  return NULL;
59  }
60 
61  if (BaseMath_ReadCallback((VectorObject *)point) == -1) {
62  return NULL;
63  }
64 
65  fp[0] = ((VectorObject *)point)->vec[0];
66  fp[1] = ((VectorObject *)point)->vec[1];
67  if (((VectorObject *)point)->size > 2) {
68  fp[2] = ((VectorObject *)point)->vec[2];
69  }
70  else {
71  /* if its a 2d vector then set the z to be zero */
72  fp[2] = 0.0f;
73  }
74 
75  len = mathutils_array_parse_alloc_v(((float **)&vecs), 3, veclist, __func__);
76  if (len == -1) {
77  return NULL;
78  }
79 
80  if (len) {
81  float *weights = MEM_mallocN(sizeof(float) * len, __func__);
82 
83  interp_weights_poly_v3(weights, vecs, len, fp);
84 
85  ret = PyList_New(len);
86  for (i = 0; i < len; i++) {
87  PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i]));
88  }
89 
90  MEM_freeN(weights);
91 
92  PyMem_Free(vecs);
93  }
94  else {
95  ret = PyList_New(0);
96  }
97 
98  return ret;
99 }
100 
101 #endif /* MATH_STANDALONE */
102 
103 static PyMethodDef M_Interpolate_methods[] = {
104 #ifndef MATH_STANDALONE
105  {"poly_3d_calc",
106  (PyCFunction)M_Interpolate_poly_3d_calc,
107  METH_VARARGS,
108  M_Interpolate_poly_3d_calc_doc},
109 #endif
110  {NULL, NULL, 0, NULL},
111 };
112 
113 static struct PyModuleDef M_Interpolate_module_def = {
114  PyModuleDef_HEAD_INIT,
115  "mathutils.interpolate", /* m_name */
116  M_Interpolate_doc, /* m_doc */
117  0, /* m_size */
118  M_Interpolate_methods, /* m_methods */
119  NULL, /* m_reload */
120  NULL, /* m_traverse */
121  NULL, /* m_clear */
122  NULL, /* m_free */
123 };
124 
125 /*----------------------------MODULE INIT-------------------------*/
126 PyMODINIT_FUNC PyInit_mathutils_interpolate(void)
127 {
128  PyObject *submodule = PyModule_Create(&M_Interpolate_module_def);
129  return submodule;
130 }
typedef float(TangentPoint)[2]
void interp_weights_poly_v3(float w[], float v[][3], const int n, const float co[3])
#define UNUSED(x)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:283
#define BaseMath_ReadCallback(_self)
Definition: mathutils.h:128
PyTypeObject vector_Type
PyDoc_STRVAR(M_Interpolate_doc, "The Blender interpolate module")
static PyMethodDef M_Interpolate_methods[]
static struct PyModuleDef M_Interpolate_module_def
PyMODINIT_FUNC PyInit_mathutils_interpolate(void)
static PyObject * M_Interpolate_poly_3d_calc(PyObject *UNUSED(self), PyObject *args)
return ret
uint len