| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup pymathutils | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mathutils.h"
 | 
					
						
							|  |  |  | #include "mathutils_interpolate.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_math.h"
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MATH_STANDALONE /* define when building outside blender */
 | 
					
						
							|  |  |  | #  include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*-------------------------DOC STRINGS ---------------------------*/ | 
					
						
							|  |  |  | PyDoc_STRVAR(M_Interpolate_doc, "The Blender interpolate module"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ---------------------------------WEIGHT CALCULATION ----------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MATH_STANDALONE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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: list of vectors\n" | 
					
						
							|  |  |  |              "   :arg pt: point" | 
					
						
							|  |  |  |              "   :rtype: list of per-vector weights\n"); | 
					
						
							|  |  |  | static PyObject *M_Interpolate_poly_3d_calc(PyObject *UNUSED(self), PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   float fp[3]; | 
					
						
							|  |  |  |   float(*vecs)[3]; | 
					
						
							|  |  |  |   Py_ssize_t len; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |   PyObject *point, *veclist, *ret; | 
					
						
							|  |  |  |   int i; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 14:09:03 +10:00
										 |  |  |   if (!PyArg_ParseTuple(args, "OO:poly_3d_calc", &veclist, &point)) { | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 14:09:03 +10:00
										 |  |  |   if (mathutils_array_parse( | 
					
						
							|  |  |  |           fp, 2, 3 | MU_ARRAY_ZERO, point, "pt must be a 2-3 dimensional vector") == -1) { | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2019-03-30 06:12:48 +11:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 08:51:27 +11:00
										 |  |  |   len = mathutils_array_parse_alloc_v(((float **)&vecs), 3, veclist, __func__); | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |   if (len == -1) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |   if (len) { | 
					
						
							| 
									
										
										
										
											2016-02-29 08:51:27 +11:00
										 |  |  |     float *weights = MEM_mallocN(sizeof(float) * len, __func__); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     interp_weights_poly_v3(weights, vecs, len, fp); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     ret = PyList_New(len); | 
					
						
							|  |  |  |     for (i = 0; i < len; i++) { | 
					
						
							|  |  |  |       PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i])); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     MEM_freeN(weights); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |     PyMem_Free(vecs); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     ret = PyList_New(0); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* MATH_STANDALONE */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef M_Interpolate_methods[] = { | 
					
						
							|  |  |  | #ifndef MATH_STANDALONE
 | 
					
						
							|  |  |  |     {"poly_3d_calc", | 
					
						
							|  |  |  |      (PyCFunction)M_Interpolate_poly_3d_calc, | 
					
						
							|  |  |  |      METH_VARARGS, | 
					
						
							|  |  |  |      M_Interpolate_poly_3d_calc_doc}, | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-01-19 13:21:18 +11:00
										 |  |  |     {NULL, NULL, 0, NULL}, | 
					
						
							| 
									
										
										
										
											2014-12-15 19:45:01 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef M_Interpolate_module_def = { | 
					
						
							|  |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     "mathutils.interpolate", /* m_name */ | 
					
						
							|  |  |  |     M_Interpolate_doc,       /* m_doc */ | 
					
						
							|  |  |  |     0,                       /* m_size */ | 
					
						
							|  |  |  |     M_Interpolate_methods,   /* m_methods */ | 
					
						
							|  |  |  |     NULL,                    /* m_reload */ | 
					
						
							|  |  |  |     NULL,                    /* m_traverse */ | 
					
						
							|  |  |  |     NULL,                    /* m_clear */ | 
					
						
							|  |  |  |     NULL,                    /* m_free */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*----------------------------MODULE INIT-------------------------*/ | 
					
						
							|  |  |  | PyMODINIT_FUNC PyInit_mathutils_interpolate(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   PyObject *submodule = PyModule_Create(&M_Interpolate_module_def); | 
					
						
							|  |  |  |   return submodule; | 
					
						
							|  |  |  | } |