Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mathutils_interpolate.c « mathutils « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d7841b906ad58eb3c083caf598bff4d4ae0956b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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.
 *
 * Contributor(s): Lukas Toenne
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/python/mathutils/mathutils_interpolate.c
 *  \ingroup pymathutils
 */


#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;
	
	PyObject *point, *veclist, *ret;
	int i;
	
	if (!PyArg_ParseTuple(args, "OO!:interpolation_weights",
	                      &veclist,
	                      &vector_Type, &point))
	{
		return NULL;
	}
	
	if (BaseMath_ReadCallback((VectorObject *)point) == -1)
		return NULL;
	
	fp[0] = ((VectorObject *)point)->vec[0];
	fp[1] = ((VectorObject *)point)->vec[1];
	if (((VectorObject *)point)->size > 2)
		fp[2] = ((VectorObject *)point)->vec[2];
	else
		fp[2] = 0.0f;  /* if its a 2d vector then set the z to be zero */
	
	len = mathutils_array_parse_alloc_v(((float **)&vecs), 3, veclist, "interpolation_weights");
	if (len == -1) {
		return NULL;
	}
	
	if (len) {
		float *weights = MEM_mallocN(sizeof(float) * len, "interpolation weights");
		
		interp_weights_poly_v3(weights, vecs, len, fp);
		
		ret = PyList_New(len);
		for (i = 0; i < len; i++) {
			PyList_SET_ITEM(ret, i, PyFloat_FromDouble(weights[i]));
		}

		MEM_freeN(weights);

		PyMem_Free(vecs);
	}
	else {
		ret = PyList_New(0);
	}

	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
	{NULL, NULL, 0, NULL}
};

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;
}