2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2014-12-15 19:45:01 +01:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pymathutils
|
2014-12-15 19:45:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
2024-09-24 12:56:16 +02:00
|
|
|
#include "mathutils.hh"
|
|
|
|
|
#include "mathutils_interpolate.hh"
|
2014-12-15 19:45:01 +01:00
|
|
|
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_geom.h"
|
2014-12-15 19:45:01 +01:00
|
|
|
|
|
|
|
|
#ifndef MATH_STANDALONE /* define when building outside blender */
|
|
|
|
|
# include "MEM_guardedalloc.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*-------------------------DOC STRINGS ---------------------------*/
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
M_Interpolate_doc,
|
|
|
|
|
"The Blender interpolate module");
|
2014-12-15 19:45:01 +01:00
|
|
|
|
|
|
|
|
/* ---------------------------------WEIGHT CALCULATION ----------------------- */
|
|
|
|
|
|
|
|
|
|
#ifndef MATH_STANDALONE
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
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"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :arg veclist: Sequence of 3D positions.\n"
|
|
|
|
|
" :type veclist: Sequence[Sequence[float]]\n"
|
|
|
|
|
" :arg pt: 2D or 3D position."
|
|
|
|
|
" :type pt: Sequence[float]"
|
|
|
|
|
" :return: list of per-vector weights.\n"
|
|
|
|
|
" :rtype: list[float]\n");
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *M_Interpolate_poly_3d_calc(PyObject * /*self*/, PyObject *args)
|
2014-12-15 19:45:01 +01:00
|
|
|
{
|
|
|
|
|
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)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-12-15 19:45:01 +01:00
|
|
|
}
|
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)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
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) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-12-15 19:45:01 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-12-15 19:45:01 +01:00
|
|
|
if (len) {
|
2025-03-12 11:21:53 +01:00
|
|
|
float *weights = MEM_malloc_arrayN<float>(size_t(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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 10:35:23 +11:00
|
|
|
#endif /* !MATH_STANDALONE */
|
2014-12-15 19:45:01 +01:00
|
|
|
|
|
|
|
|
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
|
2023-07-21 02:18:59 +02:00
|
|
|
{nullptr, nullptr, 0, nullptr},
|
2014-12-15 19:45:01 +01:00
|
|
|
};
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
static PyModuleDef M_Interpolate_module_def = {
|
2023-07-16 17:43:31 +10:00
|
|
|
/*m_base*/ PyModuleDef_HEAD_INIT,
|
2022-11-08 11:13:58 +11:00
|
|
|
/*m_name*/ "mathutils.interpolate",
|
|
|
|
|
/*m_doc*/ M_Interpolate_doc,
|
|
|
|
|
/*m_size*/ 0,
|
|
|
|
|
/*m_methods*/ M_Interpolate_methods,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*m_slots*/ nullptr,
|
|
|
|
|
/*m_traverse*/ nullptr,
|
|
|
|
|
/*m_clear*/ nullptr,
|
|
|
|
|
/*m_free*/ nullptr,
|
2014-12-15 19:45:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*----------------------------MODULE INIT-------------------------*/
|
2023-03-29 14:16:31 +11:00
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
PyMODINIT_FUNC PyInit_mathutils_interpolate()
|
2014-12-15 19:45:01 +01:00
|
|
|
{
|
|
|
|
|
PyObject *submodule = PyModule_Create(&M_Interpolate_module_def);
|
|
|
|
|
return submodule;
|
|
|
|
|
}
|