diff options
Diffstat (limited to 'source/blender/python/mathutils')
-rw-r--r-- | source/blender/python/mathutils/mathutils.c | 17 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 28 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Vector.c | 20 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_geometry.c | 113 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_noise.c | 45 |
5 files changed, 93 insertions, 130 deletions
diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c index ca38d7008f6..1616bbeeea9 100644 --- a/source/blender/python/mathutils/mathutils.c +++ b/source/blender/python/mathutils/mathutils.c @@ -39,18 +39,7 @@ PyDoc_STRVAR( ".. note::\n" "\n" " Classes, methods and attributes that accept vectors also accept other numeric sequences,\n" - " such as tuples, lists." - "\n\n" - "Submodules:\n" - "\n" - ".. toctree::\n" - " :maxdepth: 1\n" - "\n" - " mathutils.geometry.rst\n" - " mathutils.bvhtree.rst\n" - " mathutils.kdtree.rst\n" - " mathutils.interpolate.rst\n" - " mathutils.noise.rst\n" + " such as tuples, lists.\n" "\n" "The :mod:`mathutils` module provides the following classes:\n" "\n" @@ -546,7 +535,7 @@ int EXPP_FloatsAreEqual(float af, float bf, int maxDiff) const int test = SIGNMASK(ai ^ bi); int diff, v1, v2; - assert((0 == test) || (0xFFFFFFFF == test)); + BLI_assert((0 == test) || (0xFFFFFFFF == test)); diff = (ai ^ (test & 0x7fffffff)) - bi; v1 = maxDiff + diff; v2 = maxDiff - diff; @@ -738,7 +727,7 @@ void BaseMathObject_dealloc(BaseMathObject *self) BaseMathObject_clear(self); } - Py_TYPE(self)->tp_free(self); // PyObject_DEL(self); // breaks subtypes + Py_TYPE(self)->tp_free(self); // PyObject_DEL(self); /* breaks subtypes. */ } /*----------------------------MODULE INIT-------------------------*/ diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index c158ec1da15..87d16656d70 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -514,7 +514,7 @@ static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args) angle = angle_wrap_rad(angle); - if (matSize != 2 && matSize != 3 && matSize != 4) { + if (!ELEM(matSize, 2, 3, 4)) { PyErr_SetString(PyExc_ValueError, "Matrix.Rotation(): " "can only return a 2x2 3x3 or 4x4 matrix"); @@ -653,7 +653,7 @@ static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args) if (!PyArg_ParseTuple(args, "fi|O:Matrix.Scale", &factor, &matSize, &vec)) { return NULL; } - if (matSize != 2 && matSize != 3 && matSize != 4) { + if (!ELEM(matSize, 2, 3, 4)) { PyErr_SetString(PyExc_ValueError, "Matrix.Scale(): " "can only return a 2x2 3x3 or 4x4 matrix"); @@ -759,7 +759,7 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args) if (!PyArg_ParseTuple(args, "Oi:Matrix.OrthoProjection", &axis, &matSize)) { return NULL; } - if (matSize != 2 && matSize != 3 && matSize != 4) { + if (!ELEM(matSize, 2, 3, 4)) { PyErr_SetString(PyExc_ValueError, "Matrix.OrthoProjection(): " "can only return a 2x2 3x3 or 4x4 matrix"); @@ -895,7 +895,7 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args) if (!PyArg_ParseTuple(args, "siO:Matrix.Shear", &plane, &matSize, &fac)) { return NULL; } - if (matSize != 2 && matSize != 3 && matSize != 4) { + if (!ELEM(matSize, 2, 3, 4)) { PyErr_SetString(PyExc_ValueError, "Matrix.Shear(): " "can only return a 2x2 3x3 or 4x4 matrix"); @@ -1687,7 +1687,7 @@ PyDoc_STRVAR( "\n" " Set the matrix to its adjugate.\n" "\n" - " .. note:: When the matrix cannot be adjugated a :exc:`ValueError` exception is raised.\n" + " :raises ValueError: if the matrix cannot be adjugate.\n" "\n" " .. seealso:: `Adjugate matrix <https://en.wikipedia.org/wiki/Adjugate_matrix>`__ on " "Wikipedia.\n"); @@ -1718,16 +1718,14 @@ static PyObject *Matrix_adjugate(MatrixObject *self) Py_RETURN_NONE; } -PyDoc_STRVAR( - Matrix_adjugated_doc, - ".. method:: adjugated()\n" - "\n" - " Return an adjugated copy of the matrix.\n" - "\n" - " :return: the adjugated matrix.\n" - " :rtype: :class:`Matrix`\n" - "\n" - " .. note:: When the matrix cant be adjugated a :exc:`ValueError` exception is raised.\n"); +PyDoc_STRVAR(Matrix_adjugated_doc, + ".. method:: adjugated()\n" + "\n" + " Return an adjugated copy of the matrix.\n" + "\n" + " :return: the adjugated matrix.\n" + " :rtype: :class:`Matrix`\n" + " :raises ValueError: if the matrix cannot be adjugated\n"); static PyObject *Matrix_adjugated(MatrixObject *self) { return matrix__apply_to_copy(Matrix_adjugate, self); diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c index f7b2840f0b4..61487df1ab5 100644 --- a/source/blender/python/mathutils/mathutils_Vector.c +++ b/source/blender/python/mathutils/mathutils_Vector.c @@ -2492,7 +2492,7 @@ static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure swizzleClosure = POINTER_AS_INT(closure); /* We must first copy current vec into tvec, else some org values may be lost. - * See [#31760]. + * See T31760. * Assuming self->size can't be higher than MAX_DIMENSIONS! */ memcpy(tvec, self->vec, self->size * sizeof(float)); @@ -2504,7 +2504,7 @@ static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure } /* We must copy back the whole tvec into vec, else some changes may be lost (e.g. xz...). - * See [#31760]. */ + * See T31760. */ memcpy(self->vec, tvec, self->size * sizeof(float)); /* continue with BaseMathObject_WriteCallback at the end */ @@ -3042,11 +3042,15 @@ PyTypeObject vector_Type = { /* Methods to implement standard operations */ (destructor)BaseMathObject_dealloc, /* destructor tp_dealloc; */ - (printfunc)NULL, /* printfunc tp_print; */ - NULL, /* getattrfunc tp_getattr; */ - NULL, /* setattrfunc tp_setattr; */ - NULL, /* cmpfunc tp_compare; */ - (reprfunc)Vector_repr, /* reprfunc tp_repr; */ +#if PY_VERSION_HEX >= 0x03080000 + 0, /* tp_vectorcall_offset */ +#else + (printfunc)NULL, /* printfunc tp_print */ +#endif + NULL, /* getattrfunc tp_getattr; */ + NULL, /* setattrfunc tp_setattr; */ + NULL, /* cmpfunc tp_compare; */ + (reprfunc)Vector_repr, /* reprfunc tp_repr; */ /* Method suites for standard classes */ @@ -3061,7 +3065,7 @@ PyTypeObject vector_Type = { #ifndef MATH_STANDALONE (reprfunc)Vector_str, /* reprfunc tp_str; */ #else - NULL, /* reprfunc tp_str; */ + NULL, /* reprfunc tp_str; */ #endif NULL, /* getattrofunc tp_getattro; */ NULL, /* setattrofunc tp_setattro; */ diff --git a/source/blender/python/mathutils/mathutils_geometry.c b/source/blender/python/mathutils/mathutils_geometry.c index 1a161924f96..77ced169dab 100644 --- a/source/blender/python/mathutils/mathutils_geometry.c +++ b/source/blender/python/mathutils/mathutils_geometry.c @@ -1062,6 +1062,20 @@ static PyObject *M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObje return Vector_CreatePyObject(pt_dst, 3, NULL); } +struct PointsInPlanes_UserData { + PyObject *py_verts; + char *planes_used; +}; + +static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p) +{ + struct PointsInPlanes_UserData *user_data = user_data_p; + PyList_APPEND(user_data->py_verts, Vector_CreatePyObject(co, 3, NULL)); + user_data->planes_used[i] = true; + user_data->planes_used[j] = true; + user_data->planes_used[k] = true; +} + PyDoc_STRVAR(M_Geometry_points_in_planes_doc, ".. function:: points_in_planes(planes)\n" "\n" @@ -1073,7 +1087,6 @@ PyDoc_STRVAR(M_Geometry_points_in_planes_doc, " :return: two lists, once containing the vertices inside the planes, another " "containing the plane indices used\n" " :rtype: pair of lists\n"); -/* note: this function could be optimized by some spatial structure */ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *args) { PyObject *py_planes; @@ -1090,81 +1103,37 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a } /* note, this could be refactored into plain C easy - py bits are noted */ - const float eps = 0.0001f; - const uint len = (uint)planes_len; - uint i, j, k, l; - float n1n2[3], n2n3[3], n3n1[3]; - float potentialVertex[3]; - char *planes_used = PyMem_Malloc(sizeof(char) * len); + struct PointsInPlanes_UserData user_data = { + .py_verts = PyList_New(0), + .planes_used = PyMem_Malloc(sizeof(char) * planes_len), + }; /* python */ - PyObject *py_verts = PyList_New(0); PyObject *py_plane_index = PyList_New(0); - memset(planes_used, 0, sizeof(char) * len); + memset(user_data.planes_used, 0, sizeof(char) * planes_len); - for (i = 0; i < len; i++) { - const float *N1 = planes[i]; - for (j = i + 1; j < len; j++) { - const float *N2 = planes[j]; - cross_v3_v3v3(n1n2, N1, N2); - if (len_squared_v3(n1n2) > eps) { - for (k = j + 1; k < len; k++) { - const float *N3 = planes[k]; - cross_v3_v3v3(n2n3, N2, N3); - if (len_squared_v3(n2n3) > eps) { - cross_v3_v3v3(n3n1, N3, N1); - if (len_squared_v3(n3n1) > eps) { - const float quotient = dot_v3v3(N1, n2n3); - if (fabsf(quotient) > eps) { - /** - * <pre> - * potentialVertex = ( - * (n2n3 * N1[3] + n3n1 * N2[3] + n1n2 * N3[3]) * - * (-1.0 / quotient)); - * </pre> - */ - const float quotient_ninv = -1.0f / quotient; - potentialVertex[0] = ((n2n3[0] * N1[3]) + (n3n1[0] * N2[3]) + (n1n2[0] * N3[3])) * - quotient_ninv; - potentialVertex[1] = ((n2n3[1] * N1[3]) + (n3n1[1] * N2[3]) + (n1n2[1] * N3[3])) * - quotient_ninv; - potentialVertex[2] = ((n2n3[2] * N1[3]) + (n3n1[2] * N2[3]) + (n1n2[2] * N3[3])) * - quotient_ninv; - for (l = 0; l < len; l++) { - const float *NP = planes[l]; - if ((dot_v3v3(NP, potentialVertex) + NP[3]) > 0.000001f) { - break; - } - } - - if (l == len) { /* ok */ - /* python */ - PyList_APPEND(py_verts, Vector_CreatePyObject(potentialVertex, 3, NULL)); - planes_used[i] = planes_used[j] = planes_used[k] = true; - } - } - } - } - } - } - } - } + const float eps_coplanar = 1e-4f; + const float eps_isect = 1e-6f; + const bool has_isect = isect_planes_v3_fn( + planes, planes_len, eps_coplanar, eps_isect, points_in_planes_fn, &user_data); PyMem_Free(planes); - /* now make a list of used planes */ - for (i = 0; i < len; i++) { - if (planes_used[i]) { - PyList_APPEND(py_plane_index, PyLong_FromLong(i)); + /* Now make user_data list of used planes. */ + if (has_isect) { + for (int i = 0; i < planes_len; i++) { + if (user_data.planes_used[i]) { + PyList_APPEND(py_plane_index, PyLong_FromLong(i)); + } } } - PyMem_Free(planes_used); + PyMem_Free(user_data.planes_used); { PyObject *ret = PyTuple_New(2); - PyTuple_SET_ITEMS(ret, py_verts, py_plane_index); + PyTuple_SET_ITEMS(ret, user_data.py_verts, py_plane_index); return ret; } } @@ -1553,16 +1522,16 @@ PyDoc_STRVAR( M_Geometry_delaunay_2d_cdt_doc, ".. function:: delaunay_2d_cdt(vert_coords, edges, faces, output_type, epsilon)\n" "\n" - "Computes the Constrained Delaunay Triangulation of a set of vertices, " - "with edges and faces that must appear in the triangulation. " - "Some triangles may be eaten away, or combined with other triangles, " - "according to output type. " - "The returned verts may be in a different order from input verts, may be moved " - "slightly, and may be merged with other nearby verts. " - "The three returned orig lists give, for each of verts, edges, and faces, the list of " - "input element indices corresponding to the positionally same output element. " - "For edges, the orig indices start with the input edges and then continue " - "with the edges implied by each of the faces (n of them for an n-gon).\n" + " Computes the Constrained Delaunay Triangulation of a set of vertices,\n" + " with edges and faces that must appear in the triangulation.\n" + " Some triangles may be eaten away, or combined with other triangles,\n" + " according to output type.\n" + " The returned verts may be in a different order from input verts, may be moved\n" + " slightly, and may be merged with other nearby verts.\n" + " The three returned orig lists give, for each of verts, edges, and faces, the list of\n" + " input element indices corresponding to the positionally same output element.\n" + " For edges, the orig indices start with the input edges and then continue\n" + " with the edges implied by each of the faces (n of them for an n-gon).\n" "\n" " :arg vert_coords: Vertex coordinates (2d)\n" " :type vert_coords: list of :class:`mathutils.Vector`\n" diff --git a/source/blender/python/mathutils/mathutils_noise.c b/source/blender/python/mathutils/mathutils_noise.c index eece782f6d0..c24960f2d04 100644 --- a/source/blender/python/mathutils/mathutils_noise.c +++ b/source/blender/python/mathutils/mathutils_noise.c @@ -140,11 +140,11 @@ static void next_state(void) left = N; next = state; - for (j = N - M + 1; j--; p++) { + for (j = N - M + 1; --j; p++) { *p = p[M] ^ TWIST(p[0], p[1]); } - for (j = M; j--; p++) { + for (j = M; --j; p++) { *p = p[M - N] ^ TWIST(p[0], p[1]); } @@ -179,7 +179,7 @@ static float frand(void) y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); - return (float)y / 4294967296.f; + return (float)y / 4294967296.0f; } /*------------------------------------------------------------*/ @@ -246,7 +246,8 @@ static void noise_vector(float x, float y, float z, int nb, float v[3]) /* Simply evaluate noise at 3 different positions */ const float *ofs = state_offset_vector; for (int j = 0; j < 3; j++) { - v[j] = (2.0f * BLI_gNoise(1.0f, x + ofs[0], y + ofs[1], z + ofs[2], 0, nb) - 1.0f); + v[j] = (2.0f * BLI_noise_generic_noise(1.0f, x + ofs[0], y + ofs[1], z + ofs[2], false, nb) - + 1.0f); ofs += 3; } } @@ -257,8 +258,8 @@ static float turb( { float amp, out, t; int i; - amp = 1.f; - out = (float)(2.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f); + amp = 1.0f; + out = (float)(2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f); if (hard) { out = fabsf(out); } @@ -267,7 +268,7 @@ static float turb( x *= freqscale; y *= freqscale; z *= freqscale; - t = (float)(amp * (2.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f)); + t = (float)(amp * (2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f)); if (hard) { t = fabsf(t); } @@ -290,7 +291,7 @@ static void vTurb(float x, { float amp, t[3]; int i; - amp = 1.f; + amp = 1.0f; noise_vector(x, y, z, nb, v); if (hard) { v[0] = fabsf(v[0]); @@ -450,7 +451,8 @@ static PyObject *M_Noise_noise(PyObject *UNUSED(self), PyObject *args, PyObject } return PyFloat_FromDouble( - (2.0f * BLI_gNoise(1.0f, vec[0], vec[1], vec[2], 0, noise_basis_enum) - 1.0f)); + (2.0f * BLI_noise_generic_noise(1.0f, vec[0], vec[1], vec[2], false, noise_basis_enum) - + 1.0f)); } PyDoc_STRVAR(M_Noise_noise_vector_doc, @@ -659,7 +661,8 @@ static PyObject *M_Noise_fractal(PyObject *UNUSED(self), PyObject *args, PyObjec return NULL; } - return PyFloat_FromDouble(mg_fBm(vec[0], vec[1], vec[2], H, lac, oct, noise_basis_enum)); + return PyFloat_FromDouble( + BLI_noise_mg_fbm(vec[0], vec[1], vec[2], H, lac, oct, noise_basis_enum)); } PyDoc_STRVAR( @@ -713,7 +716,7 @@ static PyObject *M_Noise_multi_fractal(PyObject *UNUSED(self), PyObject *args, P } return PyFloat_FromDouble( - mg_MultiFractal(vec[0], vec[1], vec[2], H, lac, oct, noise_basis_enum)); + BLI_noise_mg_multi_fractal(vec[0], vec[1], vec[2], H, lac, oct, noise_basis_enum)); } PyDoc_STRVAR(M_Noise_variable_lacunarity_doc, @@ -780,8 +783,8 @@ static PyObject *M_Noise_variable_lacunarity(PyObject *UNUSED(self), PyObject *a return NULL; } - return PyFloat_FromDouble( - mg_VLNoise(vec[0], vec[1], vec[2], d, noise_type1_enum, noise_type2_enum)); + return PyFloat_FromDouble(BLI_noise_mg_variable_lacunarity( + vec[0], vec[1], vec[2], d, noise_type1_enum, noise_type2_enum)); } PyDoc_STRVAR( @@ -838,7 +841,7 @@ static PyObject *M_Noise_hetero_terrain(PyObject *UNUSED(self), PyObject *args, } return PyFloat_FromDouble( - mg_HeteroTerrain(vec[0], vec[1], vec[2], H, lac, oct, ofs, noise_basis_enum)); + BLI_noise_mg_hetero_terrain(vec[0], vec[1], vec[2], H, lac, oct, ofs, noise_basis_enum)); } PyDoc_STRVAR( @@ -899,8 +902,8 @@ static PyObject *M_Noise_hybrid_multi_fractal(PyObject *UNUSED(self), PyObject * return NULL; } - return PyFloat_FromDouble( - mg_HybridMultiFractal(vec[0], vec[1], vec[2], H, lac, oct, ofs, gn, noise_basis_enum)); + return PyFloat_FromDouble(BLI_noise_mg_hybrid_multi_fractal( + vec[0], vec[1], vec[2], H, lac, oct, ofs, gn, noise_basis_enum)); } PyDoc_STRVAR( @@ -961,8 +964,8 @@ static PyObject *M_Noise_ridged_multi_fractal(PyObject *UNUSED(self), PyObject * return NULL; } - return PyFloat_FromDouble( - mg_RidgedMultiFractal(vec[0], vec[1], vec[2], H, lac, oct, ofs, gn, noise_basis_enum)); + return PyFloat_FromDouble(BLI_noise_mg_ridged_multi_fractal( + vec[0], vec[1], vec[2], H, lac, oct, ofs, gn, noise_basis_enum)); } PyDoc_STRVAR(M_Noise_voronoi_doc, @@ -1008,7 +1011,7 @@ static PyObject *M_Noise_voronoi(PyObject *UNUSED(self), PyObject *args, PyObjec list = PyList_New(4); - voronoi(vec[0], vec[1], vec[2], da, pa, me, metric_enum); + BLI_noise_voronoi(vec[0], vec[1], vec[2], da, pa, me, metric_enum); for (i = 0; i < 4; i++) { PyObject *v = Vector_CreatePyObject(pa + 3 * i, 3, NULL); @@ -1042,7 +1045,7 @@ static PyObject *M_Noise_cell(PyObject *UNUSED(self), PyObject *args) return NULL; } - return PyFloat_FromDouble(cellNoise(vec[0], vec[1], vec[2])); + return PyFloat_FromDouble(BLI_noise_cell(vec[0], vec[1], vec[2])); } PyDoc_STRVAR(M_Noise_cell_vector_doc, @@ -1067,7 +1070,7 @@ static PyObject *M_Noise_cell_vector(PyObject *UNUSED(self), PyObject *args) return NULL; } - cellNoiseV(vec[0], vec[1], vec[2], r_vec); + BLI_noise_cell_v3(vec[0], vec[1], vec[2], r_vec); return Vector_CreatePyObject(r_vec, 3, NULL); } |