15#ifndef MATH_STANDALONE
36 "The Blender geometry module");
42 M_Geometry_intersect_ray_tri_doc,
43 ".. function:: intersect_ray_tri(v1, v2, v3, ray, orig, clip=True)\n"
45 " Returns the intersection between a ray and a triangle, if possible, returns None "
49 " :type v1: :class:`mathutils.Vector`\n"
51 " :type v2: :class:`mathutils.Vector`\n"
53 " :type v3: :class:`mathutils.Vector`\n"
54 " :arg ray: Direction of the projection\n"
55 " :type ray: :class:`mathutils.Vector`\n"
56 " :arg orig: Origin\n"
57 " :type orig: :class:`mathutils.Vector`\n"
58 " :arg clip: When False, don't restrict the intersection to the area of the "
59 "triangle, use the infinite plane defined by the triangle.\n"
61 " :return: The point of intersection or None if no intersection is found\n"
62 " :rtype: :class:`mathutils.Vector` | None\n");
65 const char *error_prefix =
"intersect_ray_tri";
66 PyObject *py_ray, *py_ray_off, *py_tri[3];
67 float dir[3], orig[3], tri[3][3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
68 float det, inv_det, u,
v, t;
72 if (!PyArg_ParseTuple(args,
73 "OOOOO|O&:intersect_ray_tri",
111 if (det > -0.000001f && det < 0.000001f) {
115 inv_det = 1.0f / det;
122 if (clip && (u < 0.0f || u > 1.0f)) {
154 M_Geometry_intersect_line_line_doc,
155 ".. function:: intersect_line_line(v1, v2, v3, v4)\n"
157 " Returns a tuple with the points on each line respectively closest to the other.\n"
159 " :arg v1: First point of the first line\n"
160 " :type v1: :class:`mathutils.Vector`\n"
161 " :arg v2: Second point of the first line\n"
162 " :type v2: :class:`mathutils.Vector`\n"
163 " :arg v3: First point of the second line\n"
164 " :type v3: :class:`mathutils.Vector`\n"
165 " :arg v4: Second point of the second line\n"
166 " :type v4: :class:`mathutils.Vector`\n"
167 " :return: The intersection on each line or None when the lines are co-linear.\n"
168 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | None\n");
171 const char *error_prefix =
"intersect_line_line";
173 PyObject *py_lines[4];
174 float lines[4][3], i1[3], i2[3];
178 if (!PyArg_ParseTuple(args,
"OOOO:intersect_line_line",
UNPACK4_EX(&, py_lines, ))) {
188 error_prefix) != -1) &&
193 error_prefix) != -1) &&
198 error_prefix) != -1)) == 0)
204 if (ix_vec_num == 2) {
222 tuple = PyTuple_New(2);
233 M_Geometry_intersect_sphere_sphere_2d_doc,
234 ".. function:: intersect_sphere_sphere_2d(p_a, radius_a, p_b, radius_b)\n"
236 " Returns 2 points on between intersecting circles.\n"
238 " :arg p_a: Center of the first circle\n"
239 " :type p_a: :class:`mathutils.Vector`\n"
240 " :arg radius_a: Radius of the first circle\n"
241 " :type radius_a: float\n"
242 " :arg p_b: Center of the second circle\n"
243 " :type p_b: :class:`mathutils.Vector`\n"
244 " :arg radius_b: Radius of the second circle\n"
245 " :type radius_b: float\n"
246 " :return: 2 points on between intersecting circles or None when there is no intersection.\n"
247 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | tuple[None, "
251 const char *error_prefix =
"intersect_sphere_sphere_2d";
253 PyObject *py_v_a, *py_v_b;
254 float v_a[2], v_b[2];
259 if (!PyArg_ParseTuple(args,
"OfOf:intersect_sphere_sphere_2d", &py_v_a, &rad_a, &py_v_b, &rad_b))
270 ret = PyTuple_New(2);
276 (dist > rad_a + rad_b) ||
278 (dist <
fabsf(rad_a - rad_b)) ||
280 (dist < FLT_EPSILON))
286 const float dist_delta = ((rad_a * rad_a) - (rad_b * rad_b) + (dist * dist)) / (2.0f * dist);
287 const float h =
powf(
fabsf((rad_a * rad_a) - (dist_delta * dist_delta)), 0.5f);
291 i_cent[0] = v_a[0] + ((v_ab[0] * dist_delta) / dist);
292 i_cent[1] = v_a[1] + ((v_ab[1] * dist_delta) / dist);
294 i1[0] = i_cent[0] + h * v_ab[1] / dist;
295 i1[1] = i_cent[1] - h * v_ab[0] / dist;
297 i2[0] = i_cent[0] - h * v_ab[1] / dist;
298 i2[1] = i_cent[1] + h * v_ab[0] / dist;
309 M_Geometry_intersect_tri_tri_2d_doc,
310 ".. function:: intersect_tri_tri_2d(tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
312 " Check if two 2D triangles intersect.\n"
317 const char *error_prefix =
"intersect_tri_tri_2d";
318 PyObject *tri_pair_py[2][3];
319 float tri_pair[2][3][2];
321 if (!PyArg_ParseTuple(args,
322 "OOOOOO:intersect_tri_tri_2d",
333 for (
int i = 0; i < 2; i++) {
334 for (
int j = 0; j < 3; j++) {
336 tri_pair[i][j], 2, 2 |
MU_ARRAY_SPILL, tri_pair_py[i][j], error_prefix) == -1)
344 return PyBool_FromLong(
ret);
349 M_Geometry_normal_doc,
350 ".. function:: normal(vectors)\n"
352 " Returns the normal of a 3D polygon.\n"
354 " :arg vectors: 3 or more vectors to calculate normals.\n"
355 " :type vectors: Sequence[Sequence[float]]\n"
356 " :rtype: :class:`mathutils.Vector`\n");
362 PyObject *
ret =
nullptr;
365 if (PyTuple_GET_SIZE(args) == 1) {
366 args = PyTuple_GET_ITEM(args, 0);
375 if (coords_len < 3) {
376 PyErr_SetString(PyExc_ValueError,
"Expected 3 or more vectors");
392 M_Geometry_area_tri_doc,
393 ".. function:: area_tri(v1, v2, v3)\n"
395 " Returns the area size of the 2D or 3D triangle defined.\n"
398 " :type v1: :class:`mathutils.Vector`\n"
400 " :type v2: :class:`mathutils.Vector`\n"
402 " :type v3: :class:`mathutils.Vector`\n"
406 const char *error_prefix =
"area_tri";
411 if (!PyArg_ParseTuple(args,
"OOO:area_tri",
UNPACK3_EX(&, py_tri, ))) {
427 M_Geometry_volume_tetrahedron_doc,
428 ".. function:: volume_tetrahedron(v1, v2, v3, v4)\n"
430 " Return the volume formed by a tetrahedron (points can be in any order).\n"
433 " :type v1: :class:`mathutils.Vector`\n"
435 " :type v2: :class:`mathutils.Vector`\n"
437 " :type v3: :class:`mathutils.Vector`\n"
439 " :type v4: :class:`mathutils.Vector`\n"
443 const char *error_prefix =
"volume_tetrahedron";
448 if (!PyArg_ParseTuple(args,
"OOOO:volume_tetrahedron",
UNPACK4_EX(&, py_tet, ))) {
463 M_Geometry_intersect_line_line_2d_doc,
464 ".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2)\n"
466 " Takes 2 segments (defined by 4 vectors) and returns a vector for their point of "
467 "intersection or None.\n"
469 " .. warning:: Despite its name, this function works on segments, and not on lines.\n"
471 " :arg lineA_p1: First point of the first line\n"
472 " :type lineA_p1: :class:`mathutils.Vector`\n"
473 " :arg lineA_p2: Second point of the first line\n"
474 " :type lineA_p2: :class:`mathutils.Vector`\n"
475 " :arg lineB_p1: First point of the second line\n"
476 " :type lineB_p1: :class:`mathutils.Vector`\n"
477 " :arg lineB_p2: Second point of the second line\n"
478 " :type lineB_p2: :class:`mathutils.Vector`\n"
479 " :return: The point of intersection or None when not found\n"
480 " :rtype: :class:`mathutils.Vector` | None\n");
483 const char *error_prefix =
"intersect_line_line_2d";
484 PyObject *py_lines[4];
489 if (!PyArg_ParseTuple(args,
"OOOO:intersect_line_line_2d",
UNPACK4_EX(&, py_lines, ))) {
508 M_Geometry_intersect_line_plane_doc,
509 ".. function:: intersect_line_plane(line_a, line_b, plane_co, plane_no, no_flip=False)\n"
511 " Calculate the intersection between a line (as 2 vectors) and a plane.\n"
512 " Returns a vector for the intersection or None.\n"
514 " :arg line_a: First point of the first line\n"
515 " :type line_a: :class:`mathutils.Vector`\n"
516 " :arg line_b: Second point of the first line\n"
517 " :type line_b: :class:`mathutils.Vector`\n"
518 " :arg plane_co: A point on the plane\n"
519 " :type plane_co: :class:`mathutils.Vector`\n"
520 " :arg plane_no: The direction the plane is facing\n"
521 " :type plane_no: :class:`mathutils.Vector`\n"
522 " :return: The point of intersection or None when not found\n"
523 " :rtype: :class:`mathutils.Vector` | None\n");
526 const char *error_prefix =
"intersect_line_plane";
527 PyObject *py_line_a, *py_line_b, *py_plane_co, *py_plane_no;
528 float line_a[3], line_b[3], plane_co[3], plane_no[3];
530 const bool no_flip =
false;
532 if (!PyArg_ParseTuple(args,
533 "OOOO|O&:intersect_line_plane",
563 M_Geometry_intersect_plane_plane_doc,
564 ".. function:: intersect_plane_plane(plane_a_co, plane_a_no, plane_b_co, plane_b_no)\n"
566 " Return the intersection between two planes\n"
568 " :arg plane_a_co: Point on the first plane\n"
569 " :type plane_a_co: :class:`mathutils.Vector`\n"
570 " :arg plane_a_no: Normal of the first plane\n"
571 " :type plane_a_no: :class:`mathutils.Vector`\n"
572 " :arg plane_b_co: Point on the second plane\n"
573 " :type plane_b_co: :class:`mathutils.Vector`\n"
574 " :arg plane_b_no: Normal of the second plane\n"
575 " :type plane_b_no: :class:`mathutils.Vector`\n"
576 " :return: The line of the intersection represented as a point and a vector or None if the "
577 "intersection can't be calculated\n"
578 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | tuple[None, "
582 const char *error_prefix =
"intersect_plane_plane";
583 PyObject *
ret, *ret_co, *ret_no;
584 PyObject *py_plane_a_co, *py_plane_a_no, *py_plane_b_co, *py_plane_b_no;
585 float plane_a_co[3], plane_a_no[3], plane_b_co[3], plane_b_no[3];
586 float plane_a[4], plane_b[4];
591 if (!PyArg_ParseTuple(args,
592 "OOOO:intersect_plane_plane",
623 ret_co = Py_NewRef(Py_None);
624 ret_no = Py_NewRef(Py_None);
627 ret = PyTuple_New(2);
634 M_Geometry_intersect_line_sphere_doc,
635 ".. function:: intersect_line_sphere(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
637 " Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
638 " returns the intersection\n"
640 " :arg line_a: First point of the line\n"
641 " :type line_a: :class:`mathutils.Vector`\n"
642 " :arg line_b: Second point of the line\n"
643 " :type line_b: :class:`mathutils.Vector`\n"
644 " :arg sphere_co: The center of the sphere\n"
645 " :type sphere_co: :class:`mathutils.Vector`\n"
646 " :arg sphere_radius: Radius of the sphere\n"
647 " :type sphere_radius: float\n"
648 " :return: The intersection points as a pair of vectors or None when there is no "
650 " :rtype: tuple[:class:`mathutils.Vector` | None, :class:`mathutils.Vector` | None]\n");
653 const char *error_prefix =
"intersect_line_sphere";
654 PyObject *py_line_a, *py_line_b, *py_sphere_co;
655 float line_a[3], line_b[3], sphere_co[3];
662 if (!PyArg_ParseTuple(args,
663 "OOOf|O&:intersect_line_sphere",
686 PyObject *
ret = PyTuple_New(2);
725 M_Geometry_intersect_line_sphere_2d_doc,
726 ".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
728 " Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
729 " returns the intersection\n"
731 " :arg line_a: First point of the line\n"
732 " :type line_a: :class:`mathutils.Vector`\n"
733 " :arg line_b: Second point of the line\n"
734 " :type line_b: :class:`mathutils.Vector`\n"
735 " :arg sphere_co: The center of the sphere\n"
736 " :type sphere_co: :class:`mathutils.Vector`\n"
737 " :arg sphere_radius: Radius of the sphere\n"
738 " :type sphere_radius: float\n"
739 " :return: The intersection points as a pair of vectors or None when there is no "
741 " :rtype: tuple[:class:`mathutils.Vector` | None, :class:`mathutils.Vector` | None]\n");
744 const char *error_prefix =
"intersect_line_sphere_2d";
745 PyObject *py_line_a, *py_line_b, *py_sphere_co;
746 float line_a[2], line_b[2], sphere_co[2];
753 if (!PyArg_ParseTuple(args,
754 "OOOf|O&:intersect_line_sphere_2d",
777 PyObject *
ret = PyTuple_New(2);
815 M_Geometry_intersect_point_line_doc,
816 ".. function:: intersect_point_line(pt, line_p1, line_p2)\n"
818 " Takes a point and a line and returns a tuple with the closest point on the line and its "
819 "distance from the first point of the line as a percentage of the length of the line.\n"
822 " :type pt: :class:`mathutils.Vector`\n"
823 " :arg line_p1: First point of the line\n"
824 " :type line_p1: :class:`mathutils.Vector`\n"
825 " :arg line_p1: Second point of the line\n"
826 " :type line_p1: :class:`mathutils.Vector`\n"
827 " :rtype: tuple[:class:`mathutils.Vector`, float]\n");
830 const char *error_prefix =
"intersect_point_line";
831 PyObject *py_pt, *py_line_a, *py_line_b;
832 float pt[3], pt_out[3], line_a[3], line_b[3];
837 if (!PyArg_ParseTuple(args,
"OOO:intersect_point_line", &py_pt, &py_line_a, &py_line_b)) {
855 ret = PyTuple_New(2);
863 M_Geometry_intersect_point_tri_doc,
864 ".. function:: intersect_point_tri(pt, tri_p1, tri_p2, tri_p3)\n"
866 " Takes 4 vectors: one is the point and the next 3 define the triangle. Projects "
867 "the point onto the triangle plane and checks if it is within the triangle.\n"
870 " :type pt: :class:`mathutils.Vector`\n"
871 " :arg tri_p1: First point of the triangle\n"
872 " :type tri_p1: :class:`mathutils.Vector`\n"
873 " :arg tri_p2: Second point of the triangle\n"
874 " :type tri_p2: :class:`mathutils.Vector`\n"
875 " :arg tri_p3: Third point of the triangle\n"
876 " :type tri_p3: :class:`mathutils.Vector`\n"
877 " :return: Point on the triangles plane or None if its outside the triangle\n"
878 " :rtype: :class:`mathutils.Vector` | None\n");
881 const char *error_prefix =
"intersect_point_tri";
882 PyObject *py_pt, *py_tri[3];
883 float pt[3], tri[3][3];
887 if (!PyArg_ParseTuple(args,
"OOOO:intersect_point_tri", &py_pt,
UNPACK3_EX(&, py_tri, ))) {
912 M_Geometry_closest_point_on_tri_doc,
913 ".. function:: closest_point_on_tri(pt, tri_p1, tri_p2, tri_p3)\n"
915 " Takes 4 vectors: one is the point and the next 3 define the triangle.\n"
918 " :type pt: :class:`mathutils.Vector`\n"
919 " :arg tri_p1: First point of the triangle\n"
920 " :type tri_p1: :class:`mathutils.Vector`\n"
921 " :arg tri_p2: Second point of the triangle\n"
922 " :type tri_p2: :class:`mathutils.Vector`\n"
923 " :arg tri_p3: Third point of the triangle\n"
924 " :type tri_p3: :class:`mathutils.Vector`\n"
925 " :return: The closest point of the triangle.\n"
926 " :rtype: :class:`mathutils.Vector`\n");
929 const char *error_prefix =
"closest_point_on_tri";
930 PyObject *py_pt, *py_tri[3];
931 float pt[3], tri[3][3];
935 if (!PyArg_ParseTuple(args,
"OOOO:closest_point_on_tri", &py_pt,
UNPACK3_EX(&, py_tri, ))) {
958 M_Geometry_intersect_point_tri_2d_doc,
959 ".. function:: intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3)\n"
961 " Takes 4 vectors (using only the x and y coordinates): one is the point and the next 3 "
962 "define the triangle. Returns 1 if the point is within the triangle, otherwise 0.\n"
965 " :type pt: :class:`mathutils.Vector`\n"
966 " :arg tri_p1: First point of the triangle\n"
967 " :type tri_p1: :class:`mathutils.Vector`\n"
968 " :arg tri_p2: Second point of the triangle\n"
969 " :type tri_p2: :class:`mathutils.Vector`\n"
970 " :arg tri_p3: Third point of the triangle\n"
971 " :type tri_p3: :class:`mathutils.Vector`\n"
975 const char *error_prefix =
"intersect_point_tri_2d";
976 PyObject *py_pt, *py_tri[3];
977 float pt[2], tri[3][2];
980 if (!PyArg_ParseTuple(args,
"OOOO:intersect_point_tri_2d", &py_pt,
UNPACK3_EX(&, py_tri, ))) {
998 M_Geometry_intersect_point_quad_2d_doc,
999 ".. function:: intersect_point_quad_2d(pt, quad_p1, quad_p2, quad_p3, quad_p4)\n"
1001 " Takes 5 vectors (using only the x and y coordinates): one is the point and the "
1002 "next 4 define the quad,\n"
1003 " only the x and y are used from the vectors. Returns 1 if the point is within the "
1004 "quad, otherwise 0.\n"
1005 " Works only with convex quads without singular edges.\n"
1008 " :type pt: :class:`mathutils.Vector`\n"
1009 " :arg quad_p1: First point of the quad\n"
1010 " :type quad_p1: :class:`mathutils.Vector`\n"
1011 " :arg quad_p2: Second point of the quad\n"
1012 " :type quad_p2: :class:`mathutils.Vector`\n"
1013 " :arg quad_p3: Third point of the quad\n"
1014 " :type quad_p3: :class:`mathutils.Vector`\n"
1015 " :arg quad_p4: Fourth point of the quad\n"
1016 " :type quad_p4: :class:`mathutils.Vector`\n"
1020 const char *error_prefix =
"intersect_point_quad_2d";
1021 PyObject *py_pt, *py_quad[4];
1022 float pt[2],
quad[4][2];
1025 if (!PyArg_ParseTuple(args,
"OOOOO:intersect_point_quad_2d", &py_pt,
UNPACK4_EX(&, py_quad, ))) {
1043 M_Geometry_distance_point_to_plane_doc,
1044 ".. function:: distance_point_to_plane(pt, plane_co, plane_no)\n"
1046 " Returns the signed distance between a point and a plane "
1047 " (negative when below the normal).\n"
1050 " :type pt: :class:`mathutils.Vector`\n"
1051 " :arg plane_co: A point on the plane\n"
1052 " :type plane_co: :class:`mathutils.Vector`\n"
1053 " :arg plane_no: The direction the plane is facing\n"
1054 " :type plane_no: :class:`mathutils.Vector`\n"
1055 " :rtype: float\n");
1058 const char *error_prefix =
"distance_point_to_plane";
1059 PyObject *py_pt, *py_plane_co, *py_plane_no;
1060 float pt[3], plane_co[3], plane_no[3];
1063 if (!PyArg_ParseTuple(args,
"OOO:distance_point_to_plane", &py_pt, &py_plane_co, &py_plane_no)) {
1081 M_Geometry_barycentric_transform_doc,
1082 ".. function:: barycentric_transform(point, tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
1084 " Return a transformed point, the transformation is defined by 2 triangles.\n"
1086 " :arg point: The point to transform.\n"
1087 " :type point: :class:`mathutils.Vector`\n"
1088 " :arg tri_a1: source triangle vertex.\n"
1089 " :type tri_a1: :class:`mathutils.Vector`\n"
1090 " :arg tri_a2: source triangle vertex.\n"
1091 " :type tri_a2: :class:`mathutils.Vector`\n"
1092 " :arg tri_a3: source triangle vertex.\n"
1093 " :type tri_a3: :class:`mathutils.Vector`\n"
1094 " :arg tri_b1: target triangle vertex.\n"
1095 " :type tri_b1: :class:`mathutils.Vector`\n"
1096 " :arg tri_b2: target triangle vertex.\n"
1097 " :type tri_b2: :class:`mathutils.Vector`\n"
1098 " :arg tri_b3: target triangle vertex.\n"
1099 " :type tri_b3: :class:`mathutils.Vector`\n"
1100 " :return: The transformed point\n"
1101 " :rtype: :class:`mathutils.Vector`\n");
1104 const char *error_prefix =
"barycentric_transform";
1105 PyObject *py_pt_src, *py_tri_src[3], *py_tri_dst[3];
1106 float pt_src[3], pt_dst[3], tri_src[3][3], tri_dst[3][3];
1109 if (!PyArg_ParseTuple(args,
1110 "OOOOOOO:barycentric_transform",
1152 M_Geometry_points_in_planes_doc,
1153 ".. function:: points_in_planes(planes, epsilon_coplanar=1e-4, epsilon_isect=1e-6)\n"
1155 " Returns a list of points inside all planes given and a list of index values for "
1156 "the planes used.\n"
1158 " :arg planes: List of planes (4D vectors).\n"
1159 " :type planes: list[:class:`mathutils.Vector`]\n"
1160 " :arg epsilon_coplanar: Epsilon value for interpreting plane pairs as co-plannar.\n"
1161 " :type epsilon_coplanar: float\n"
1162 " :arg epsilon_isect: Epsilon value for intersection.\n"
1163 " :type epsilon_isect: float\n"
1164 " :return: Two lists, once containing the 3D coordinates inside the planes, "
1165 "another containing the plane indices used.\n"
1166 " :rtype: tuple[list[:class:`mathutils.Vector`], list[int]]\n");
1169 PyObject *py_planes;
1171 float eps_coplanar = 1e-4f;
1172 float eps_isect = 1e-6f;
1175 if (!PyArg_ParseTuple(args,
"O|ff:points_in_planes", &py_planes, &eps_coplanar, &eps_isect)) {
1180 (
float **)&planes, 4, py_planes,
"points_in_planes")) == -1)
1188 user_data.
py_verts = PyList_New(0);
1189 user_data.planes_used =
static_cast<char *
>(PyMem_Malloc(
sizeof(
char) * planes_len));
1192 PyObject *py_plane_index = PyList_New(0);
1194 memset(user_data.planes_used, 0,
sizeof(
char) * planes_len);
1202 for (
int i = 0; i < planes_len; i++) {
1203 if (user_data.planes_used[i]) {
1204 PyList_APPEND(py_plane_index, PyLong_FromLong(i));
1208 PyMem_Free(user_data.planes_used);
1211 PyObject *
ret = PyTuple_New(2);
1217#ifndef MATH_STANDALONE
1221 M_Geometry_interpolate_bezier_doc,
1222 ".. function:: interpolate_bezier(knot1, handle1, handle2, knot2, resolution)\n"
1224 " Interpolate a bezier spline segment.\n"
1226 " :arg knot1: First bezier spline point.\n"
1227 " :type knot1: :class:`mathutils.Vector`\n"
1228 " :arg handle1: First bezier spline handle.\n"
1229 " :type handle1: :class:`mathutils.Vector`\n"
1230 " :arg handle2: Second bezier spline handle.\n"
1231 " :type handle2: :class:`mathutils.Vector`\n"
1232 " :arg knot2: Second bezier spline point.\n"
1233 " :type knot2: :class:`mathutils.Vector`\n"
1234 " :arg resolution: Number of points to return.\n"
1235 " :type resolution: int\n"
1236 " :return: The interpolated points.\n"
1237 " :rtype: list[:class:`mathutils.Vector`]\n");
1240 const char *error_prefix =
"interpolate_bezier";
1241 PyObject *py_data[4];
1242 float data[4][4] = {{0.0f}};
1246 float *coord_array, *fp;
1249 if (!PyArg_ParseTuple(args,
"OOOOi:interpolate_bezier",
UNPACK4_EX(&, py_data, ), &resolu)) {
1253 for (i = 0; i < 4; i++) {
1260 dims =
max_ii(dims, dims_tmp);
1264 PyErr_SetString(PyExc_ValueError,
"resolution must be 2 or over");
1268 coord_array =
static_cast<float *
>(
MEM_callocN(dims * (resolu) *
sizeof(
float), error_prefix));
1269 for (i = 0; i < dims; i++) {
1271 UNPACK4_EX(, data, [i]), coord_array + i, resolu - 1,
sizeof(
float) * dims);
1274 list = PyList_New(resolu);
1276 for (i = 0; i < resolu; i++, fp = fp + dims) {
1285 M_Geometry_tessellate_polygon_doc,
1286 ".. function:: tessellate_polygon(polylines)\n"
1288 " Takes a list of polylines (each point a pair or triplet of numbers) and returns "
1289 "the point indices for a polyline filled with triangles. Does not handle degenerate "
1290 "geometry (such as zero-length lines due to consecutive identical points).\n"
1292 " :arg polylines: Polygons where each polygon is a sequence of 2D or 3D points.\n"
1293 " :type polylines: Sequence[Sequence[Sequence[float]]]"
1294 " :return: A list of triangles.\n"
1295 " :rtype: list[tuple[int, int, int]]\n");
1300 PyObject *polyLine, *polyVec;
1301 int i, len_polylines, len_polypoints;
1302 bool list_parse_error =
false;
1306 ListBase dispbase = {
nullptr,
nullptr};
1311 if (!PySequence_Check(polyLineSeq)) {
1312 PyErr_SetString(PyExc_TypeError,
"expected a sequence of poly lines");
1316 len_polylines = PySequence_Size(polyLineSeq);
1318 for (i = 0; i < len_polylines; i++) {
1319 polyLine = PySequence_GetItem(polyLineSeq, i);
1320 if (!PySequence_Check(polyLine)) {
1322 Py_XDECREF(polyLine);
1323 PyErr_SetString(PyExc_TypeError,
1324 "One or more of the polylines is not a sequence of mathutils.Vector's");
1328 len_polypoints = PySequence_Size(polyLine);
1329 if (len_polypoints > 0) {
1332 dl->
nr = len_polypoints;
1336 dl->
verts = fp =
static_cast<float *
>(
1337 MEM_mallocN(
sizeof(
float[3]) * len_polypoints,
"dl verts"));
1338 dl->
index =
static_cast<int *
>(
MEM_callocN(
sizeof(
int[3]) * len_polypoints,
"dl index"));
1340 for (
int index = 0; index < len_polypoints; index++, fp += 3) {
1341 polyVec = PySequence_GetItem(polyLine, index);
1343 fp, 2, 3 |
MU_ARRAY_SPILL, polyVec,
"tessellate_polygon: parse coord");
1347 list_parse_error =
true;
1349 else if (polyVec_len == 2) {
1352 else if (polyVec_len == 3) {
1359 Py_DECREF(polyLine);
1362 if (list_parse_error) {
1368 float down_vec[3] = {0, 0, -1};
1375 tri_list = PyList_New(dl->parts);
1378 PyErr_SetString(PyExc_RuntimeError,
"failed to make a new list");
1382 int *dl_face = dl->index;
1383 for (
int index = 0; index < dl->parts; index++) {
1384 PyList_SET_ITEM(tri_list, index,
PyC_Tuple_Pack_I32({dl_face[0], dl_face[1], dl_face[2]}));
1392 tri_list = PyList_New(0);
1401 PyObject *list_item, *item_1, *item_2;
1405 if (!PyList_Check(value)) {
1406 PyErr_SetString(PyExc_TypeError,
"can only back a list of [x, y, w, h]");
1410 len = PyList_GET_SIZE(value);
1414 for (i = 0; i <
len; i++) {
1415 list_item = PyList_GET_ITEM(value, i);
1416 if (!PyList_Check(list_item) || PyList_GET_SIZE(list_item) < 4) {
1418 PyErr_SetString(PyExc_TypeError,
"can only pack a list of [x, y, w, h]");
1424 item_1 = PyList_GET_ITEM(list_item, 2);
1425 item_2 = PyList_GET_ITEM(list_item, 3);
1427 box->w =
float(PyFloat_AsDouble(item_1));
1428 box->h =
float(PyFloat_AsDouble(item_2));
1432 if (box->w < 0.0f || box->h < 0.0f) {
1434 PyErr_SetString(PyExc_TypeError,
1435 "error parsing width and height values from list: "
1436 "[x, y, w, h], not numbers or below zero");
1443 *r_boxarray = boxarray;
1450 PyObject *list_item;
1452 len = PyList_GET_SIZE(value);
1454 for (i = 0; i <
len; i++) {
1455 const BoxPack *box = &boxarray[i];
1456 list_item = PyList_GET_ITEM(value, box->index);
1457 PyList_SetItem(list_item, 0, PyFloat_FromDouble(box->x));
1458 PyList_SetItem(list_item, 1, PyFloat_FromDouble(box->y));
1464 M_Geometry_box_pack_2d_doc,
1465 ".. function:: box_pack_2d(boxes)\n"
1467 " Returns a tuple with the width and height of the packed bounding box.\n"
1469 " :arg boxes: list of boxes, each box is a list where the first 4 items are "
1470 "[X, Y, width, height, ...] other items are ignored. "
1471 "The X & Y values in this list are modified to set the packed positions.\n"
1472 " :type boxes: list[list[float, float, float, float, ...]]\n"
1473 " :return: The width and height of the packed bounding box.\n"
1474 " :rtype: tuple[float, float]\n");
1477 float tot_width = 0.0f, tot_height = 0.0f;
1482 if (!PyList_Check(boxlist)) {
1483 PyErr_SetString(PyExc_TypeError,
"expected a list of boxes [[x, y, w, h], ... ]");
1487 len = PyList_GET_SIZE(boxlist);
1494 const bool sort_boxes =
true;
1502 ret = PyTuple_New(2);
1509 M_Geometry_box_fit_2d_doc,
1510 ".. function:: box_fit_2d(points)\n"
1512 " Returns an angle that best fits the points to an axis aligned rectangle\n"
1514 " :arg points: Sequence of 2D points.\n"
1515 " :type points: Sequence[Sequence[float]]\n"
1517 " :rtype: float\n");
1537 return PyFloat_FromDouble(angle);
1542 M_Geometry_convex_hull_2d_doc,
1543 ".. function:: convex_hull_2d(points)\n"
1545 " Returns a list of indices into the list given\n"
1547 " :arg points: Sequence of 2D points.\n"
1548 " :type points: Sequence[Sequence[float]]\n"
1549 " :return: a list of indices\n"
1550 " :rtype: list[int]\n");
1565 Py_ssize_t len_ret, i;
1567 index_map =
static_cast<int *
>(
MEM_mallocN(
sizeof(*index_map) *
len, __func__));
1572 ret = PyList_New(len_ret);
1573 for (i = 0; i < len_ret; i++) {
1574 PyList_SET_ITEM(
ret, i, PyLong_FromLong(index_map[i]));
1582 ret = PyList_New(0);
1594 if (data.is_empty()) {
1595 return PyList_New(0);
1597 PyObject *
ret = PyList_New(data.size());
1598 for (
const int i : data.index_range()) {
1600 PyObject *sublist = PyList_New(group.size());
1601 for (
const int j : group.index_range()) {
1602 PyList_SET_ITEM(sublist, j, PyLong_FromLong(group[j]));
1604 PyList_SET_ITEM(
ret, i, sublist);
1611 M_Geometry_delaunay_2d_cdt_doc,
1612 ".. function:: delaunay_2d_cdt(vert_coords, edges, faces, output_type, epsilon, "
1615 " Computes the Constrained Delaunay Triangulation of a set of vertices,\n"
1616 " with edges and faces that must appear in the triangulation.\n"
1617 " Some triangles may be eaten away, or combined with other triangles,\n"
1618 " according to output type.\n"
1619 " The returned verts may be in a different order from input verts, may be moved\n"
1620 " slightly, and may be merged with other nearby verts.\n"
1621 " The three returned orig lists give, for each of verts, edges, and faces, the list of\n"
1622 " input element indices corresponding to the positionally same output element.\n"
1623 " For edges, the orig indices start with the input edges and then continue\n"
1624 " with the edges implied by each of the faces (n of them for an n-gon).\n"
1625 " If the need_ids argument is supplied, and False, then the code skips the preparation\n"
1626 " of the orig arrays, which may save some time.\n"
1628 " :arg vert_coords: Vertex coordinates (2d)\n"
1629 " :type vert_coords: Sequence[:class:`mathutils.Vector`]\n"
1630 " :arg edges: Edges, as pairs of indices in ``vert_coords``\n"
1631 " :type edges: Sequence[Sequence[int, int]]\n"
1632 " :arg faces: Faces, each sublist is a face, as indices in `vert_coords` (CCW oriented)\n"
1633 " :type faces: Sequence[Sequence[int]]\n"
1634 " :arg output_type: What output looks like. 0 => triangles with convex hull. "
1635 "1 => triangles inside constraints. "
1636 "2 => the input constraints, intersected. "
1637 "3 => like 2 but detect holes and omit them from output. "
1638 "4 => like 2 but with extra edges to make valid BMesh faces. "
1639 "5 => like 4 but detect holes and omit them from output.\n"
1640 " :type output_type: int\n"
1641 " :arg epsilon: For nearness tests; should not be zero\n"
1642 " :type epsilon: float\n"
1643 " :arg need_ids: are the orig output arrays needed?\n"
1644 " :type need_args: bool\n"
1645 " :return: Output tuple, (vert_coords, edges, faces, orig_verts, orig_edges, orig_faces)\n"
1647 "list[:class:`mathutils.Vector`], "
1648 "list[tuple[int, int]], "
1652 "list[list[int]]]\n"
1657 const char *error_prefix =
"delaunay_2d_cdt";
1658 PyObject *vert_coords, *edges, *
faces;
1661 bool need_ids =
true;
1662 float(*in_coords)[2] =
nullptr;
1663 int(*in_edges)[2] =
nullptr;
1664 Py_ssize_t vert_coords_len, edges_len;
1665 PyObject *out_vert_coords =
nullptr;
1666 PyObject *out_edges =
nullptr;
1667 PyObject *out_faces =
nullptr;
1668 PyObject *out_orig_verts =
nullptr;
1669 PyObject *out_orig_edges =
nullptr;
1670 PyObject *out_orig_faces =
nullptr;
1671 PyObject *ret_value =
nullptr;
1673 if (!PyArg_ParseTuple(args,
1674 "OOOif|p:delaunay_2d_cdt",
1686 if (in_coords !=
nullptr) {
1687 PyMem_Free(in_coords);
1689 if (in_edges !=
nullptr) {
1690 PyMem_Free(in_edges);
1695 (
float **)&in_coords, 2, vert_coords, error_prefix);
1696 if (vert_coords_len == -1) {
1701 if (edges_len == -1) {
1711 for (
const int i :
verts.index_range()) {
1717 in.edge =
Span(
reinterpret_cast<std::pair<int, int> *
>(in_edges), edges_len);
1718 in.face = std::move(in_faces);
1719 in.epsilon = epsilon;
1720 in.need_ids = need_ids;
1725 ret_value = PyTuple_New(6);
1727 out_vert_coords = PyList_New(res.
vert.size());
1728 for (
const int i : res.
vert.index_range()) {
1731 if (item ==
nullptr) {
1732 Py_DECREF(ret_value);
1733 Py_DECREF(out_vert_coords);
1736 PyList_SET_ITEM(out_vert_coords, i, item);
1738 PyTuple_SET_ITEM(ret_value, 0, out_vert_coords);
1740 out_edges = PyList_New(res.
edge.
size());
1742 PyObject *item = PyTuple_New(2);
1743 PyTuple_SET_ITEM(item, 0, PyLong_FromLong(
long(res.
edge[i].
first)));
1744 PyTuple_SET_ITEM(item, 1, PyLong_FromLong(
long(res.
edge[i].second)));
1745 PyList_SET_ITEM(out_edges, i, item);
1747 PyTuple_SET_ITEM(ret_value, 1, out_edges);
1750 PyTuple_SET_ITEM(ret_value, 2, out_faces);
1753 PyTuple_SET_ITEM(ret_value, 3, out_orig_verts);
1756 PyTuple_SET_ITEM(ret_value, 4, out_orig_edges);
1759 PyTuple_SET_ITEM(ret_value, 5, out_orig_faces);
1767 {
"intersect_ray_tri",
1770 M_Geometry_intersect_ray_tri_doc},
1771 {
"intersect_point_line",
1774 M_Geometry_intersect_point_line_doc},
1775 {
"intersect_point_tri",
1778 M_Geometry_intersect_point_tri_doc},
1779 {
"closest_point_on_tri",
1782 M_Geometry_closest_point_on_tri_doc},
1783 {
"intersect_point_tri_2d",
1786 M_Geometry_intersect_point_tri_2d_doc},
1787 {
"intersect_point_quad_2d",
1790 M_Geometry_intersect_point_quad_2d_doc},
1791 {
"intersect_line_line",
1794 M_Geometry_intersect_line_line_doc},
1795 {
"intersect_line_line_2d",
1798 M_Geometry_intersect_line_line_2d_doc},
1799 {
"intersect_line_plane",
1802 M_Geometry_intersect_line_plane_doc},
1803 {
"intersect_plane_plane",
1806 M_Geometry_intersect_plane_plane_doc},
1807 {
"intersect_line_sphere",
1810 M_Geometry_intersect_line_sphere_doc},
1811 {
"intersect_line_sphere_2d",
1814 M_Geometry_intersect_line_sphere_2d_doc},
1815 {
"distance_point_to_plane",
1818 M_Geometry_distance_point_to_plane_doc},
1819 {
"intersect_sphere_sphere_2d",
1822 M_Geometry_intersect_sphere_sphere_2d_doc},
1823 {
"intersect_tri_tri_2d",
1826 M_Geometry_intersect_tri_tri_2d_doc},
1828 {
"volume_tetrahedron",
1831 M_Geometry_volume_tetrahedron_doc},
1832 {
"normal", (PyCFunction)
M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},
1833 {
"barycentric_transform",
1836 M_Geometry_barycentric_transform_doc},
1837 {
"points_in_planes",
1840 M_Geometry_points_in_planes_doc},
1841#ifndef MATH_STANDALONE
1842 {
"interpolate_bezier",
1845 M_Geometry_interpolate_bezier_doc},
1846 {
"tessellate_polygon",
1849 M_Geometry_tessellate_polygon_doc},
1853 M_Geometry_convex_hull_2d_doc},
1857 M_Geometry_delaunay_2d_cdt_doc},
1861 {
nullptr,
nullptr, 0,
nullptr},
1865 PyModuleDef_HEAD_INIT,
1866 "mathutils.geometry",
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
display list (or rather multi purpose list) stuff.
void BKE_displist_free(struct ListBase *lb)
void BKE_displist_fill(const struct ListBase *dispbase, struct ListBase *to, const float normal_proj[3], bool flip_normal)
void BLI_box_pack_2d(BoxPack *boxarray, unsigned int len, bool sort_boxes, float *r_tot_x, float *r_tot_y)
int BLI_convexhull_2d(const float(*points)[2], int points_num, int r_points[])
float BLI_convexhull_aabb_fit_points_2d(const float(*points)[2], int points_num)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
MINLINE int max_ii(int a, int b)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
int isect_point_quad_v2(const float p[2], const float v1[2], const float v2[2], const float v3[2], const float v4[2])
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
bool isect_plane_plane_v3(const float plane_a[4], const float plane_b[4], float r_isect_co[3], float r_isect_no[3]) ATTR_WARN_UNUSED_RESULT
bool isect_tri_tri_v2(const float t_a0[2], const float t_a1[2], const float t_a2[2], const float t_b0[2], const float t_b1[2], const float t_b2[2])
void transform_point_by_tri_v3(float pt_tar[3], float const pt_src[3], const float tri_tar_p1[3], const float tri_tar_p2[3], const float tri_tar_p3[3], const float tri_src_p1[3], const float tri_src_p2[3], const float tri_src_p3[3])
int isect_line_sphere_v3(const float l1[3], const float l2[3], const float sp[3], float r, float r_p1[3], float r_p2[3])
bool isect_point_tri_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], float r_isect_co[3])
int isect_line_sphere_v2(const float l1[2], const float l2[2], const float sp[2], float r, float r_p1[2], float r_p2[2])
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
bool isect_line_plane_v3(float r_isect_co[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3]) ATTR_WARN_UNUSED_RESULT
float normal_poly_v3(float n[3], const float verts[][3], unsigned int nr)
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
int isect_seg_seg_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
bool isect_planes_v3_fn(const float planes[][4], int planes_len, float eps_coplanar, float eps_isect, void(*callback_fn)(const float co[3], int i, int j, int k, void *user_data), void *user_data)
float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float normalize_v3(float n[3])
#define BLI_SCOPED_DEFER(function_to_defer)
#define UNPACK3_EX(pre, a, post)
#define UNPACK4_EX(pre, a, post)
typedef double(DMatrix)[4][4]
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v
IndexRange index_range() const
Array< std::pair< int, int > > edge
Array< Vector< int > > face
Array< Vector< int > > vert_orig
Array< Vector< int > > face_orig
Array< VecBase< T, 2 > > vert
Array< Vector< int > > edge_orig
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
blender::gpu::Batch * quad
void *(* MEM_mallocN)(size_t len, const char *str)
void MEM_freeN(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
bool mathutils_array_parse_alloc_viseq(PyObject *value, const char *error_prefix, blender::Array< blender::Vector< int > > &r_data)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
PyObject * Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
static PyObject * M_Geometry_intersect_line_sphere_2d(PyObject *, PyObject *args)
static PyObject * list_of_lists_from_arrays(const blender::Span< blender::Vector< int > > data)
static PyObject * M_Geometry_points_in_planes(PyObject *, PyObject *args)
PyDoc_STRVAR(M_Geometry_doc, "The Blender geometry module")
static PyModuleDef M_Geometry_module_def
static PyObject * M_Geometry_area_tri(PyObject *, PyObject *args)
static PyObject * M_Geometry_distance_point_to_plane(PyObject *, PyObject *args)
static PyObject * M_Geometry_interpolate_bezier(PyObject *, PyObject *args)
static PyObject * M_Geometry_volume_tetrahedron(PyObject *, PyObject *args)
static PyObject * M_Geometry_tessellate_polygon(PyObject *, PyObject *polyLineSeq)
static PyObject * M_Geometry_intersect_point_line(PyObject *, PyObject *args)
static PyObject * M_Geometry_normal(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_plane_plane(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_line_plane(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_point_tri(PyObject *, PyObject *args)
static PyObject * M_Geometry_box_fit_2d(PyObject *, PyObject *pointlist)
static PyObject * M_Geometry_intersect_line_line(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_ray_tri(PyObject *, PyObject *args)
static PyObject * M_Geometry_box_pack_2d(PyObject *, PyObject *boxlist)
static PyObject * M_Geometry_closest_point_on_tri(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_line_line_2d(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_tri_tri_2d(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_line_sphere(PyObject *, PyObject *args)
static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray)
static PyObject * M_Geometry_intersect_point_quad_2d(PyObject *, PyObject *args)
static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p)
PyMODINIT_FUNC PyInit_mathutils_geometry()
static PyObject * M_Geometry_convex_hull_2d(PyObject *, PyObject *pointlist)
static PyObject * M_Geometry_intersect_sphere_sphere_2d(PyObject *, PyObject *args)
static PyObject * M_Geometry_barycentric_transform(PyObject *, PyObject *args)
static void boxPack_ToPyObject(PyObject *value, const BoxPack *boxarray)
static PyMethodDef M_Geometry_methods[]
static PyObject * M_Geometry_delaunay_2d_cdt(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_point_tri_2d(PyObject *, PyObject *args)
int PyC_ParseBool(PyObject *o, void *p)
PyObject * PyC_Tuple_Pack_I32(const blender::Span< int > values)
#define PyTuple_SET_ITEMS(op_arg,...)