Blender  V2.93
mathutils_geometry.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <Python.h>
22 
23 #include "mathutils.h"
24 #include "mathutils_geometry.h"
25 
26 /* Used for PolyFill */
27 #ifndef MATH_STANDALONE /* define when building outside blender */
28 # include "BKE_curve.h"
29 # include "BKE_displist.h"
30 # include "BLI_blenlib.h"
31 # include "BLI_boxpack_2d.h"
32 # include "BLI_convexhull_2d.h"
33 # include "BLI_delaunay_2d.h"
34 # include "MEM_guardedalloc.h"
35 #endif
36 
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39 
40 #include "../generic/py_capi_utils.h"
41 #include "../generic/python_utildefines.h"
42 
43 /*-------------------------DOC STRINGS ---------------------------*/
44 PyDoc_STRVAR(M_Geometry_doc, "The Blender geometry module");
45 
46 /* ---------------------------------INTERSECTION FUNCTIONS-------------------- */
47 
48 PyDoc_STRVAR(M_Geometry_intersect_ray_tri_doc,
49  ".. function:: intersect_ray_tri(v1, v2, v3, ray, orig, clip=True)\n"
50  "\n"
51  " Returns the intersection between a ray and a triangle, if possible, returns None "
52  "otherwise.\n"
53  "\n"
54  " :arg v1: Point1\n"
55  " :type v1: :class:`mathutils.Vector`\n"
56  " :arg v2: Point2\n"
57  " :type v2: :class:`mathutils.Vector`\n"
58  " :arg v3: Point3\n"
59  " :type v3: :class:`mathutils.Vector`\n"
60  " :arg ray: Direction of the projection\n"
61  " :type ray: :class:`mathutils.Vector`\n"
62  " :arg orig: Origin\n"
63  " :type orig: :class:`mathutils.Vector`\n"
64  " :arg clip: When False, don't restrict the intersection to the area of the "
65  "triangle, use the infinite plane defined by the triangle.\n"
66  " :type clip: boolean\n"
67  " :return: The point of intersection or None if no intersection is found\n"
68  " :rtype: :class:`mathutils.Vector` or None\n");
69 static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *args)
70 {
71  const char *error_prefix = "intersect_ray_tri";
72  PyObject *py_ray, *py_ray_off, *py_tri[3];
73  float dir[3], orig[3], tri[3][3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
74  float det, inv_det, u, v, t;
75  bool clip = true;
76  int i;
77 
78  if (!PyArg_ParseTuple(args,
79  "OOOOO|O&:intersect_ray_tri",
80  UNPACK3_EX(&, py_tri, ),
81  &py_ray,
82  &py_ray_off,
84  &clip)) {
85  return NULL;
86  }
87 
88  if (((mathutils_array_parse(dir, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray, error_prefix) !=
89  -1) &&
91  orig, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray_off, error_prefix) != -1)) == 0) {
92  return NULL;
93  }
94 
95  for (i = 0; i < ARRAY_SIZE(tri); i++) {
97  tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1) {
98  return NULL;
99  }
100  }
101 
102  normalize_v3(dir);
103 
104  /* find vectors for two edges sharing v1 */
105  sub_v3_v3v3(e1, tri[1], tri[0]);
106  sub_v3_v3v3(e2, tri[2], tri[0]);
107 
108  /* begin calculating determinant - also used to calculated U parameter */
109  cross_v3_v3v3(pvec, dir, e2);
110 
111  /* if determinant is near zero, ray lies in plane of triangle */
112  det = dot_v3v3(e1, pvec);
113 
114  if (det > -0.000001f && det < 0.000001f) {
115  Py_RETURN_NONE;
116  }
117 
118  inv_det = 1.0f / det;
119 
120  /* calculate distance from v1 to ray origin */
121  sub_v3_v3v3(tvec, orig, tri[0]);
122 
123  /* calculate U parameter and test bounds */
124  u = dot_v3v3(tvec, pvec) * inv_det;
125  if (clip && (u < 0.0f || u > 1.0f)) {
126  Py_RETURN_NONE;
127  }
128 
129  /* prepare to test the V parameter */
130  cross_v3_v3v3(qvec, tvec, e1);
131 
132  /* calculate V parameter and test bounds */
133  v = dot_v3v3(dir, qvec) * inv_det;
134 
135  if (clip && (v < 0.0f || u + v > 1.0f)) {
136  Py_RETURN_NONE;
137  }
138 
139  /* calculate t, ray intersects triangle */
140  t = dot_v3v3(e2, qvec) * inv_det;
141 
142  /* ray hit behind */
143  if (t < 0.0f) {
144  Py_RETURN_NONE;
145  }
146 
147  mul_v3_fl(dir, t);
148  add_v3_v3v3(pvec, orig, dir);
149 
150  return Vector_CreatePyObject(pvec, 3, NULL);
151 }
152 
153 /* Line-Line intersection using algorithm from mathworld.wolfram.com */
154 
155 PyDoc_STRVAR(M_Geometry_intersect_line_line_doc,
156  ".. function:: intersect_line_line(v1, v2, v3, v4)\n"
157  "\n"
158  " Returns a tuple with the points on each line respectively closest to the other.\n"
159  "\n"
160  " :arg v1: First point of the first line\n"
161  " :type v1: :class:`mathutils.Vector`\n"
162  " :arg v2: Second point of the first line\n"
163  " :type v2: :class:`mathutils.Vector`\n"
164  " :arg v3: First point of the second line\n"
165  " :type v3: :class:`mathutils.Vector`\n"
166  " :arg v4: Second point of the second line\n"
167  " :type v4: :class:`mathutils.Vector`\n"
168  " :rtype: tuple of :class:`mathutils.Vector`'s\n");
169 static PyObject *M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject *args)
170 {
171  const char *error_prefix = "intersect_line_line";
172  PyObject *tuple;
173  PyObject *py_lines[4];
174  float lines[4][3], i1[3], i2[3];
175  int len;
176  int result;
177 
178  if (!PyArg_ParseTuple(args, "OOOO:intersect_line_line", UNPACK4_EX(&, py_lines, ))) {
179  return NULL;
180  }
181 
182  if ((((len = mathutils_array_parse(
183  lines[0], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[0], error_prefix)) != -1) &&
185  lines[1], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[1], error_prefix) !=
186  -1) &&
188  lines[2], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[2], error_prefix) !=
189  -1) &&
191  lines[3], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[3], error_prefix) !=
192  -1)) == 0) {
193  return NULL;
194  }
195 
196  /* Zero 3rd axis of 2D vectors. */
197  if (len == 2) {
198  lines[1][2] = 0.0f;
199  lines[2][2] = 0.0f;
200  lines[3][2] = 0.0f;
201  }
202 
203  result = isect_line_line_v3(UNPACK4(lines), i1, i2);
204  /* The return-code isn't exposed,
205  * this way we can check know how close the lines are. */
206  if (result == 1) {
207  closest_to_line_v3(i2, i1, lines[2], lines[3]);
208  }
209 
210  if (result == 0) {
211  /* collinear */
212  Py_RETURN_NONE;
213  }
214 
215  tuple = PyTuple_New(2);
218  return tuple;
219 }
220 
221 /* Line-Line intersection using algorithm from mathworld.wolfram.com */
222 
224  M_Geometry_intersect_sphere_sphere_2d_doc,
225  ".. function:: intersect_sphere_sphere_2d(p_a, radius_a, p_b, radius_b)\n"
226  "\n"
227  " Returns 2 points on between intersecting circles.\n"
228  "\n"
229  " :arg p_a: Center of the first circle\n"
230  " :type p_a: :class:`mathutils.Vector`\n"
231  " :arg radius_a: Radius of the first circle\n"
232  " :type radius_a: float\n"
233  " :arg p_b: Center of the second circle\n"
234  " :type p_b: :class:`mathutils.Vector`\n"
235  " :arg radius_b: Radius of the second circle\n"
236  " :type radius_b: float\n"
237  " :rtype: tuple of :class:`mathutils.Vector`'s or None when there is no intersection\n");
238 static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), PyObject *args)
239 {
240  const char *error_prefix = "intersect_sphere_sphere_2d";
241  PyObject *ret;
242  PyObject *py_v_a, *py_v_b;
243  float v_a[2], v_b[2];
244  float rad_a, rad_b;
245  float v_ab[2];
246  float dist;
247 
248  if (!PyArg_ParseTuple(
249  args, "OfOf:intersect_sphere_sphere_2d", &py_v_a, &rad_a, &py_v_b, &rad_b)) {
250  return NULL;
251  }
252 
253  if (((mathutils_array_parse(v_a, 2, 2, py_v_a, error_prefix) != -1) &&
254  (mathutils_array_parse(v_b, 2, 2, py_v_b, error_prefix) != -1)) == 0) {
255  return NULL;
256  }
257 
258  ret = PyTuple_New(2);
259 
260  sub_v2_v2v2(v_ab, v_b, v_a);
261  dist = len_v2(v_ab);
262 
263  if (/* out of range */
264  (dist > rad_a + rad_b) ||
265  /* fully-contained in the other */
266  (dist < fabsf(rad_a - rad_b)) ||
267  /* co-incident */
268  (dist < FLT_EPSILON)) {
269  /* out of range */
270  PyTuple_SET_ITEMS(ret, Py_INCREF_RET(Py_None), Py_INCREF_RET(Py_None));
271  }
272  else {
273  const float dist_delta = ((rad_a * rad_a) - (rad_b * rad_b) + (dist * dist)) / (2.0f * dist);
274  const float h = powf(fabsf((rad_a * rad_a) - (dist_delta * dist_delta)), 0.5f);
275  float i_cent[2];
276  float i1[2], i2[2];
277 
278  i_cent[0] = v_a[0] + ((v_ab[0] * dist_delta) / dist);
279  i_cent[1] = v_a[1] + ((v_ab[1] * dist_delta) / dist);
280 
281  i1[0] = i_cent[0] + h * v_ab[1] / dist;
282  i1[1] = i_cent[1] - h * v_ab[0] / dist;
283 
284  i2[0] = i_cent[0] - h * v_ab[1] / dist;
285  i2[1] = i_cent[1] + h * v_ab[0] / dist;
286 
288  }
289 
290  return ret;
291 }
292 
293 PyDoc_STRVAR(M_Geometry_intersect_tri_tri_2d_doc,
294  ".. function:: intersect_tri_tri_2d(tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
295  "\n"
296  " Check if two 2D triangles intersect.\n"
297  "\n"
298  " :rtype: bool\n");
299 static PyObject *M_Geometry_intersect_tri_tri_2d(PyObject *UNUSED(self), PyObject *args)
300 {
301  const char *error_prefix = "intersect_tri_tri_2d";
302  PyObject *tri_pair_py[2][3];
303  float tri_pair[2][3][2];
304 
305  if (!PyArg_ParseTuple(args,
306  "OOOOOO:intersect_tri_tri_2d",
307  &tri_pair_py[0][0],
308  &tri_pair_py[0][1],
309  &tri_pair_py[0][2],
310  &tri_pair_py[1][0],
311  &tri_pair_py[1][1],
312  &tri_pair_py[1][2])) {
313  return NULL;
314  }
315 
316  for (int i = 0; i < 2; i++) {
317  for (int j = 0; j < 3; j++) {
319  tri_pair[i][j], 2, 2 | MU_ARRAY_SPILL, tri_pair_py[i][j], error_prefix) == -1) {
320  return NULL;
321  }
322  }
323  }
324 
325  const bool ret = isect_tri_tri_v2(UNPACK3(tri_pair[0]), UNPACK3(tri_pair[1]));
326  return PyBool_FromLong(ret);
327 }
328 
329 PyDoc_STRVAR(M_Geometry_normal_doc,
330  ".. function:: normal(vectors)\n"
331  "\n"
332  " Returns the normal of a 3D polygon.\n"
333  "\n"
334  " :arg vectors: Vectors to calculate normals with\n"
335  " :type vectors: sequence of 3 or more 3d vector\n"
336  " :rtype: :class:`mathutils.Vector`\n");
337 static PyObject *M_Geometry_normal(PyObject *UNUSED(self), PyObject *args)
338 {
339  float(*coords)[3];
340  int coords_len;
341  float n[3];
342  PyObject *ret = NULL;
343 
344  /* use */
345  if (PyTuple_GET_SIZE(args) == 1) {
346  args = PyTuple_GET_ITEM(args, 0);
347  }
348 
349  if ((coords_len = mathutils_array_parse_alloc_v(
350  (float **)&coords, 3 | MU_ARRAY_SPILL, args, "normal")) == -1) {
351  return NULL;
352  }
353 
354  if (coords_len < 3) {
355  PyErr_SetString(PyExc_ValueError, "Expected 3 or more vectors");
356  goto finally;
357  }
358 
359  normal_poly_v3(n, (const float(*)[3])coords, coords_len);
360  ret = Vector_CreatePyObject(n, 3, NULL);
361 
362 finally:
363  PyMem_Free(coords);
364  return ret;
365 }
366 
367 /* --------------------------------- AREA FUNCTIONS-------------------- */
368 
369 PyDoc_STRVAR(M_Geometry_area_tri_doc,
370  ".. function:: area_tri(v1, v2, v3)\n"
371  "\n"
372  " Returns the area size of the 2D or 3D triangle defined.\n"
373  "\n"
374  " :arg v1: Point1\n"
375  " :type v1: :class:`mathutils.Vector`\n"
376  " :arg v2: Point2\n"
377  " :type v2: :class:`mathutils.Vector`\n"
378  " :arg v3: Point3\n"
379  " :type v3: :class:`mathutils.Vector`\n"
380  " :rtype: float\n");
381 static PyObject *M_Geometry_area_tri(PyObject *UNUSED(self), PyObject *args)
382 {
383  const char *error_prefix = "area_tri";
384  PyObject *py_tri[3];
385  float tri[3][3];
386  int len;
387 
388  if (!PyArg_ParseTuple(args, "OOO:area_tri", UNPACK3_EX(&, py_tri, ))) {
389  return NULL;
390  }
391 
392  if ((((len = mathutils_array_parse(tri[0], 2, 3, py_tri[0], error_prefix)) != -1) &&
393  (mathutils_array_parse(tri[1], len, len, py_tri[1], error_prefix) != -1) &&
394  (mathutils_array_parse(tri[2], len, len, py_tri[2], error_prefix) != -1)) == 0) {
395  return NULL;
396  }
397 
398  return PyFloat_FromDouble((len == 3 ? area_tri_v3 : area_tri_v2)(UNPACK3(tri)));
399 }
400 
401 PyDoc_STRVAR(M_Geometry_volume_tetrahedron_doc,
402  ".. function:: volume_tetrahedron(v1, v2, v3, v4)\n"
403  "\n"
404  " Return the volume formed by a tetrahedron (points can be in any order).\n"
405  "\n"
406  " :arg v1: Point1\n"
407  " :type v1: :class:`mathutils.Vector`\n"
408  " :arg v2: Point2\n"
409  " :type v2: :class:`mathutils.Vector`\n"
410  " :arg v3: Point3\n"
411  " :type v3: :class:`mathutils.Vector`\n"
412  " :arg v4: Point4\n"
413  " :type v4: :class:`mathutils.Vector`\n"
414  " :rtype: float\n");
415 static PyObject *M_Geometry_volume_tetrahedron(PyObject *UNUSED(self), PyObject *args)
416 {
417  const char *error_prefix = "volume_tetrahedron";
418  PyObject *py_tet[4];
419  float tet[4][3];
420  int i;
421 
422  if (!PyArg_ParseTuple(args, "OOOO:volume_tetrahedron", UNPACK4_EX(&, py_tet, ))) {
423  return NULL;
424  }
425 
426  for (i = 0; i < ARRAY_SIZE(tet); i++) {
427  if (mathutils_array_parse(tet[i], 3, 3 | MU_ARRAY_SPILL, py_tet[i], error_prefix) == -1) {
428  return NULL;
429  }
430  }
431 
432  return PyFloat_FromDouble(volume_tetrahedron_v3(UNPACK4(tet)));
433 }
434 
436  M_Geometry_intersect_line_line_2d_doc,
437  ".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2)\n"
438  "\n"
439  " Takes 2 segments (defined by 4 vectors) and returns a vector for their point of "
440  "intersection or None.\n"
441  "\n"
442  " .. warning:: Despite its name, this function works on segments, and not on lines.\n"
443  "\n"
444  " :arg lineA_p1: First point of the first line\n"
445  " :type lineA_p1: :class:`mathutils.Vector`\n"
446  " :arg lineA_p2: Second point of the first line\n"
447  " :type lineA_p2: :class:`mathutils.Vector`\n"
448  " :arg lineB_p1: First point of the second line\n"
449  " :type lineB_p1: :class:`mathutils.Vector`\n"
450  " :arg lineB_p2: Second point of the second line\n"
451  " :type lineB_p2: :class:`mathutils.Vector`\n"
452  " :return: The point of intersection or None when not found\n"
453  " :rtype: :class:`mathutils.Vector` or None\n");
454 static PyObject *M_Geometry_intersect_line_line_2d(PyObject *UNUSED(self), PyObject *args)
455 {
456  const char *error_prefix = "intersect_line_line_2d";
457  PyObject *py_lines[4];
458  float lines[4][2];
459  float vi[2];
460  int i;
461 
462  if (!PyArg_ParseTuple(args, "OOOO:intersect_line_line_2d", UNPACK4_EX(&, py_lines, ))) {
463  return NULL;
464  }
465 
466  for (i = 0; i < ARRAY_SIZE(lines); i++) {
467  if (mathutils_array_parse(lines[i], 2, 2 | MU_ARRAY_SPILL, py_lines[i], error_prefix) == -1) {
468  return NULL;
469  }
470  }
471 
472  if (isect_seg_seg_v2_point(UNPACK4(lines), vi) == 1) {
473  return Vector_CreatePyObject(vi, 2, NULL);
474  }
475 
476  Py_RETURN_NONE;
477 }
478 
480  M_Geometry_intersect_line_plane_doc,
481  ".. function:: intersect_line_plane(line_a, line_b, plane_co, plane_no, no_flip=False)\n"
482  "\n"
483  " Calculate the intersection between a line (as 2 vectors) and a plane.\n"
484  " Returns a vector for the intersection or None.\n"
485  "\n"
486  " :arg line_a: First point of the first line\n"
487  " :type line_a: :class:`mathutils.Vector`\n"
488  " :arg line_b: Second point of the first line\n"
489  " :type line_b: :class:`mathutils.Vector`\n"
490  " :arg plane_co: A point on the plane\n"
491  " :type plane_co: :class:`mathutils.Vector`\n"
492  " :arg plane_no: The direction the plane is facing\n"
493  " :type plane_no: :class:`mathutils.Vector`\n"
494  " :return: The point of intersection or None when not found\n"
495  " :rtype: :class:`mathutils.Vector` or None\n");
496 static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObject *args)
497 {
498  const char *error_prefix = "intersect_line_plane";
499  PyObject *py_line_a, *py_line_b, *py_plane_co, *py_plane_no;
500  float line_a[3], line_b[3], plane_co[3], plane_no[3];
501  float isect[3];
502  const bool no_flip = false;
503 
504  if (!PyArg_ParseTuple(args,
505  "OOOO|O&:intersect_line_plane",
506  &py_line_a,
507  &py_line_b,
508  &py_plane_co,
509  &py_plane_no,
511  &no_flip)) {
512  return NULL;
513  }
514 
515  if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
516  (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
517  (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
518  (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) !=
519  -1)) == 0) {
520  return NULL;
521  }
522 
523  /* TODO: implements no_flip */
524  if (isect_line_plane_v3(isect, line_a, line_b, plane_co, plane_no) == 1) {
525  return Vector_CreatePyObject(isect, 3, NULL);
526  }
527 
528  Py_RETURN_NONE;
529 }
530 
532  M_Geometry_intersect_plane_plane_doc,
533  ".. function:: intersect_plane_plane(plane_a_co, plane_a_no, plane_b_co, plane_b_no)\n"
534  "\n"
535  " Return the intersection between two planes\n"
536  "\n"
537  " :arg plane_a_co: Point on the first plane\n"
538  " :type plane_a_co: :class:`mathutils.Vector`\n"
539  " :arg plane_a_no: Normal of the first plane\n"
540  " :type plane_a_no: :class:`mathutils.Vector`\n"
541  " :arg plane_b_co: Point on the second plane\n"
542  " :type plane_b_co: :class:`mathutils.Vector`\n"
543  " :arg plane_b_no: Normal of the second plane\n"
544  " :type plane_b_no: :class:`mathutils.Vector`\n"
545  " :return: The line of the intersection represented as a point and a vector\n"
546  " :rtype: tuple pair of :class:`mathutils.Vector` or None if the intersection can't be "
547  "calculated\n");
548 static PyObject *M_Geometry_intersect_plane_plane(PyObject *UNUSED(self), PyObject *args)
549 {
550  const char *error_prefix = "intersect_plane_plane";
551  PyObject *ret, *ret_co, *ret_no;
552  PyObject *py_plane_a_co, *py_plane_a_no, *py_plane_b_co, *py_plane_b_no;
553  float plane_a_co[3], plane_a_no[3], plane_b_co[3], plane_b_no[3];
554  float plane_a[4], plane_b[4];
555 
556  float isect_co[3];
557  float isect_no[3];
558 
559  if (!PyArg_ParseTuple(args,
560  "OOOO:intersect_plane_plane",
561  &py_plane_a_co,
562  &py_plane_a_no,
563  &py_plane_b_co,
564  &py_plane_b_no)) {
565  return NULL;
566  }
567 
568  if (((mathutils_array_parse(plane_a_co, 3, 3 | MU_ARRAY_SPILL, py_plane_a_co, error_prefix) !=
569  -1) &&
570  (mathutils_array_parse(plane_a_no, 3, 3 | MU_ARRAY_SPILL, py_plane_a_no, error_prefix) !=
571  -1) &&
572  (mathutils_array_parse(plane_b_co, 3, 3 | MU_ARRAY_SPILL, py_plane_b_co, error_prefix) !=
573  -1) &&
574  (mathutils_array_parse(plane_b_no, 3, 3 | MU_ARRAY_SPILL, py_plane_b_no, error_prefix) !=
575  -1)) == 0) {
576  return NULL;
577  }
578 
579  plane_from_point_normal_v3(plane_a, plane_a_co, plane_a_no);
580  plane_from_point_normal_v3(plane_b, plane_b_co, plane_b_no);
581 
582  if (isect_plane_plane_v3(plane_a, plane_b, isect_co, isect_no)) {
583  normalize_v3(isect_no);
584 
585  ret_co = Vector_CreatePyObject(isect_co, 3, NULL);
586  ret_no = Vector_CreatePyObject(isect_no, 3, NULL);
587  }
588  else {
589  ret_co = Py_INCREF_RET(Py_None);
590  ret_no = Py_INCREF_RET(Py_None);
591  }
592 
593  ret = PyTuple_New(2);
594  PyTuple_SET_ITEMS(ret, ret_co, ret_no);
595  return ret;
596 }
597 
599  M_Geometry_intersect_line_sphere_doc,
600  ".. function:: intersect_line_sphere(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
601  "\n"
602  " Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
603  " returns the intersection\n"
604  "\n"
605  " :arg line_a: First point of the line\n"
606  " :type line_a: :class:`mathutils.Vector`\n"
607  " :arg line_b: Second point of the line\n"
608  " :type line_b: :class:`mathutils.Vector`\n"
609  " :arg sphere_co: The center of the sphere\n"
610  " :type sphere_co: :class:`mathutils.Vector`\n"
611  " :arg sphere_radius: Radius of the sphere\n"
612  " :type sphere_radius: sphere_radius\n"
613  " :return: The intersection points as a pair of vectors or None when there is no "
614  "intersection\n"
615  " :rtype: A tuple pair containing :class:`mathutils.Vector` or None\n");
616 static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObject *args)
617 {
618  const char *error_prefix = "intersect_line_sphere";
619  PyObject *py_line_a, *py_line_b, *py_sphere_co;
620  float line_a[3], line_b[3], sphere_co[3];
621  float sphere_radius;
622  bool clip = true;
623 
624  float isect_a[3];
625  float isect_b[3];
626 
627  if (!PyArg_ParseTuple(args,
628  "OOOf|O&:intersect_line_sphere",
629  &py_line_a,
630  &py_line_b,
631  &py_sphere_co,
632  &sphere_radius,
634  &clip)) {
635  return NULL;
636  }
637 
638  if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
639  (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
640  (mathutils_array_parse(sphere_co, 3, 3 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) !=
641  -1)) == 0) {
642  return NULL;
643  }
644 
645  bool use_a = true;
646  bool use_b = true;
647  float lambda;
648 
649  PyObject *ret = PyTuple_New(2);
650 
651  switch (isect_line_sphere_v3(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
652  case 1:
653  if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) &&
654  (lambda <= 1.0f)))) {
655  use_a = false;
656  }
657  use_b = false;
658  break;
659  case 2:
660  if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) &&
661  (lambda <= 1.0f)))) {
662  use_a = false;
663  }
664  if (!(!clip || (((lambda = line_point_factor_v3(isect_b, line_a, line_b)) >= 0.0f) &&
665  (lambda <= 1.0f)))) {
666  use_b = false;
667  }
668  break;
669  default:
670  use_a = false;
671  use_b = false;
672  break;
673  }
674 
676  use_a ? Vector_CreatePyObject(isect_a, 3, NULL) : Py_INCREF_RET(Py_None),
677  use_b ? Vector_CreatePyObject(isect_b, 3, NULL) : Py_INCREF_RET(Py_None));
678 
679  return ret;
680 }
681 
682 /* keep in sync with M_Geometry_intersect_line_sphere */
684  M_Geometry_intersect_line_sphere_2d_doc,
685  ".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
686  "\n"
687  " Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
688  " returns the intersection\n"
689  "\n"
690  " :arg line_a: First point of the line\n"
691  " :type line_a: :class:`mathutils.Vector`\n"
692  " :arg line_b: Second point of the line\n"
693  " :type line_b: :class:`mathutils.Vector`\n"
694  " :arg sphere_co: The center of the sphere\n"
695  " :type sphere_co: :class:`mathutils.Vector`\n"
696  " :arg sphere_radius: Radius of the sphere\n"
697  " :type sphere_radius: sphere_radius\n"
698  " :return: The intersection points as a pair of vectors or None when there is no "
699  "intersection\n"
700  " :rtype: A tuple pair containing :class:`mathutils.Vector` or None\n");
701 static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyObject *args)
702 {
703  const char *error_prefix = "intersect_line_sphere_2d";
704  PyObject *py_line_a, *py_line_b, *py_sphere_co;
705  float line_a[2], line_b[2], sphere_co[2];
706  float sphere_radius;
707  bool clip = true;
708 
709  float isect_a[2];
710  float isect_b[2];
711 
712  if (!PyArg_ParseTuple(args,
713  "OOOf|O&:intersect_line_sphere_2d",
714  &py_line_a,
715  &py_line_b,
716  &py_sphere_co,
717  &sphere_radius,
719  &clip)) {
720  return NULL;
721  }
722 
723  if (((mathutils_array_parse(line_a, 2, 2 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
724  (mathutils_array_parse(line_b, 2, 2 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
725  (mathutils_array_parse(sphere_co, 2, 2 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) !=
726  -1)) == 0) {
727  return NULL;
728  }
729 
730  bool use_a = true;
731  bool use_b = true;
732  float lambda;
733 
734  PyObject *ret = PyTuple_New(2);
735 
736  switch (isect_line_sphere_v2(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
737  case 1:
738  if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) &&
739  (lambda <= 1.0f)))) {
740  use_a = false;
741  }
742  use_b = false;
743  break;
744  case 2:
745  if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) &&
746  (lambda <= 1.0f)))) {
747  use_a = false;
748  }
749  if (!(!clip || (((lambda = line_point_factor_v2(isect_b, line_a, line_b)) >= 0.0f) &&
750  (lambda <= 1.0f)))) {
751  use_b = false;
752  }
753  break;
754  default:
755  use_a = false;
756  use_b = false;
757  break;
758  }
759 
761  use_a ? Vector_CreatePyObject(isect_a, 2, NULL) : Py_INCREF_RET(Py_None),
762  use_b ? Vector_CreatePyObject(isect_b, 2, NULL) : Py_INCREF_RET(Py_None));
763 
764  return ret;
765 }
766 
768  M_Geometry_intersect_point_line_doc,
769  ".. function:: intersect_point_line(pt, line_p1, line_p2)\n"
770  "\n"
771  " Takes a point and a line and returns a tuple with the closest point on the line and its "
772  "distance from the first point of the line as a percentage of the length of the line.\n"
773  "\n"
774  " :arg pt: Point\n"
775  " :type pt: :class:`mathutils.Vector`\n"
776  " :arg line_p1: First point of the line\n"
777  " :type line_p1: :class:`mathutils.Vector`\n"
778  " :arg line_p1: Second point of the line\n"
779  " :type line_p1: :class:`mathutils.Vector`\n"
780  " :rtype: (:class:`mathutils.Vector`, float)\n");
781 static PyObject *M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObject *args)
782 {
783  const char *error_prefix = "intersect_point_line";
784  PyObject *py_pt, *py_line_a, *py_line_b;
785  float pt[3], pt_out[3], line_a[3], line_b[3];
786  float lambda;
787  PyObject *ret;
788  int size = 2;
789 
790  if (!PyArg_ParseTuple(args, "OOO:intersect_point_line", &py_pt, &py_line_a, &py_line_b)) {
791  return NULL;
792  }
793 
794  /* accept 2d verts */
795  if ((((size = mathutils_array_parse(
796  pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix)) != -1) &&
798  line_a, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_a, error_prefix) != -1) &&
800  line_b, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_b, error_prefix) != -1)) == 0) {
801  return NULL;
802  }
803 
804  /* do the calculation */
805  lambda = closest_to_line_v3(pt_out, pt, line_a, line_b);
806 
807  ret = PyTuple_New(2);
808  PyTuple_SET_ITEMS(ret, Vector_CreatePyObject(pt_out, size, NULL), PyFloat_FromDouble(lambda));
809  return ret;
810 }
811 
812 PyDoc_STRVAR(M_Geometry_intersect_point_tri_doc,
813  ".. function:: intersect_point_tri(pt, tri_p1, tri_p2, tri_p3)\n"
814  "\n"
815  " Takes 4 vectors: one is the point and the next 3 define the triangle. Projects "
816  "the point onto the triangle plane and checks if it is within the triangle.\n"
817  "\n"
818  " :arg pt: Point\n"
819  " :type pt: :class:`mathutils.Vector`\n"
820  " :arg tri_p1: First point of the triangle\n"
821  " :type tri_p1: :class:`mathutils.Vector`\n"
822  " :arg tri_p2: Second point of the triangle\n"
823  " :type tri_p2: :class:`mathutils.Vector`\n"
824  " :arg tri_p3: Third point of the triangle\n"
825  " :type tri_p3: :class:`mathutils.Vector`\n"
826  " :return: Point on the triangles plane or None if its outside the triangle\n"
827  " :rtype: :class:`mathutils.Vector` or None\n");
828 static PyObject *M_Geometry_intersect_point_tri(PyObject *UNUSED(self), PyObject *args)
829 {
830  const char *error_prefix = "intersect_point_tri";
831  PyObject *py_pt, *py_tri[3];
832  float pt[3], tri[3][3];
833  float vi[3];
834  int i;
835 
836  if (!PyArg_ParseTuple(args, "OOOO:intersect_point_tri", &py_pt, UNPACK3_EX(&, py_tri, ))) {
837  return NULL;
838  }
839 
840  if (mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix) ==
841  -1) {
842  return NULL;
843  }
844  for (i = 0; i < ARRAY_SIZE(tri); i++) {
846  tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1) {
847  return NULL;
848  }
849  }
850 
851  if (isect_point_tri_v3(pt, UNPACK3(tri), vi)) {
852  return Vector_CreatePyObject(vi, 3, NULL);
853  }
854 
855  Py_RETURN_NONE;
856 }
857 
858 PyDoc_STRVAR(M_Geometry_closest_point_on_tri_doc,
859  ".. function:: closest_point_on_tri(pt, tri_p1, tri_p2, tri_p3)\n"
860  "\n"
861  " Takes 4 vectors: one is the point and the next 3 define the triangle.\n"
862  "\n"
863  " :arg pt: Point\n"
864  " :type pt: :class:`mathutils.Vector`\n"
865  " :arg tri_p1: First point of the triangle\n"
866  " :type tri_p1: :class:`mathutils.Vector`\n"
867  " :arg tri_p2: Second point of the triangle\n"
868  " :type tri_p2: :class:`mathutils.Vector`\n"
869  " :arg tri_p3: Third point of the triangle\n"
870  " :type tri_p3: :class:`mathutils.Vector`\n"
871  " :return: The closest point of the triangle.\n"
872  " :rtype: :class:`mathutils.Vector`\n");
873 static PyObject *M_Geometry_closest_point_on_tri(PyObject *UNUSED(self), PyObject *args)
874 {
875  const char *error_prefix = "closest_point_on_tri";
876  PyObject *py_pt, *py_tri[3];
877  float pt[3], tri[3][3];
878  float vi[3];
879  int i;
880 
881  if (!PyArg_ParseTuple(args, "OOOO:closest_point_on_tri", &py_pt, UNPACK3_EX(&, py_tri, ))) {
882  return NULL;
883  }
884 
885  if (mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix) ==
886  -1) {
887  return NULL;
888  }
889  for (i = 0; i < ARRAY_SIZE(tri); i++) {
891  tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1) {
892  return NULL;
893  }
894  }
895 
896  closest_on_tri_to_point_v3(vi, pt, UNPACK3(tri));
897 
898  return Vector_CreatePyObject(vi, 3, NULL);
899 }
900 
902  M_Geometry_intersect_point_tri_2d_doc,
903  ".. function:: intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3)\n"
904  "\n"
905  " Takes 4 vectors (using only the x and y coordinates): one is the point and the next 3 "
906  "define the triangle. Returns 1 if the point is within the triangle, otherwise 0.\n"
907  "\n"
908  " :arg pt: Point\n"
909  " :type pt: :class:`mathutils.Vector`\n"
910  " :arg tri_p1: First point of the triangle\n"
911  " :type tri_p1: :class:`mathutils.Vector`\n"
912  " :arg tri_p2: Second point of the triangle\n"
913  " :type tri_p2: :class:`mathutils.Vector`\n"
914  " :arg tri_p3: Third point of the triangle\n"
915  " :type tri_p3: :class:`mathutils.Vector`\n"
916  " :rtype: int\n");
917 static PyObject *M_Geometry_intersect_point_tri_2d(PyObject *UNUSED(self), PyObject *args)
918 {
919  const char *error_prefix = "intersect_point_tri_2d";
920  PyObject *py_pt, *py_tri[3];
921  float pt[2], tri[3][2];
922  int i;
923 
924  if (!PyArg_ParseTuple(args, "OOOO:intersect_point_tri_2d", &py_pt, UNPACK3_EX(&, py_tri, ))) {
925  return NULL;
926  }
927 
928  if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
929  return NULL;
930  }
931  for (i = 0; i < ARRAY_SIZE(tri); i++) {
932  if (mathutils_array_parse(tri[i], 2, 2 | MU_ARRAY_SPILL, py_tri[i], error_prefix) == -1) {
933  return NULL;
934  }
935  }
936 
937  return PyLong_FromLong(isect_point_tri_v2(pt, UNPACK3(tri)));
938 }
939 
940 PyDoc_STRVAR(M_Geometry_intersect_point_quad_2d_doc,
941  ".. function:: intersect_point_quad_2d(pt, quad_p1, quad_p2, quad_p3, quad_p4)\n"
942  "\n"
943  " Takes 5 vectors (using only the x and y coordinates): one is the point and the "
944  "next 4 define the quad,\n"
945  " only the x and y are used from the vectors. Returns 1 if the point is within the "
946  "quad, otherwise 0.\n"
947  " Works only with convex quads without singular edges.\n"
948  "\n"
949  " :arg pt: Point\n"
950  " :type pt: :class:`mathutils.Vector`\n"
951  " :arg quad_p1: First point of the quad\n"
952  " :type quad_p1: :class:`mathutils.Vector`\n"
953  " :arg quad_p2: Second point of the quad\n"
954  " :type quad_p2: :class:`mathutils.Vector`\n"
955  " :arg quad_p3: Third point of the quad\n"
956  " :type quad_p3: :class:`mathutils.Vector`\n"
957  " :arg quad_p4: Fourth point of the quad\n"
958  " :type quad_p4: :class:`mathutils.Vector`\n"
959  " :rtype: int\n");
960 static PyObject *M_Geometry_intersect_point_quad_2d(PyObject *UNUSED(self), PyObject *args)
961 {
962  const char *error_prefix = "intersect_point_quad_2d";
963  PyObject *py_pt, *py_quad[4];
964  float pt[2], quad[4][2];
965  int i;
966 
967  if (!PyArg_ParseTuple(args, "OOOOO:intersect_point_quad_2d", &py_pt, UNPACK4_EX(&, py_quad, ))) {
968  return NULL;
969  }
970 
971  if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
972  return NULL;
973  }
974  for (i = 0; i < ARRAY_SIZE(quad); i++) {
975  if (mathutils_array_parse(quad[i], 2, 2 | MU_ARRAY_SPILL, py_quad[i], error_prefix) == -1) {
976  return NULL;
977  }
978  }
979 
980  return PyLong_FromLong(isect_point_quad_v2(pt, UNPACK4(quad)));
981 }
982 
983 PyDoc_STRVAR(M_Geometry_distance_point_to_plane_doc,
984  ".. function:: distance_point_to_plane(pt, plane_co, plane_no)\n"
985  "\n"
986  " Returns the signed distance between a point and a plane "
987  " (negative when below the normal).\n"
988  "\n"
989  " :arg pt: Point\n"
990  " :type pt: :class:`mathutils.Vector`\n"
991  " :arg plane_co: A point on the plane\n"
992  " :type plane_co: :class:`mathutils.Vector`\n"
993  " :arg plane_no: The direction the plane is facing\n"
994  " :type plane_no: :class:`mathutils.Vector`\n"
995  " :rtype: float\n");
996 static PyObject *M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyObject *args)
997 {
998  const char *error_prefix = "distance_point_to_plane";
999  PyObject *py_pt, *py_plane_co, *py_plane_no;
1000  float pt[3], plane_co[3], plane_no[3];
1001  float plane[4];
1002 
1003  if (!PyArg_ParseTuple(args, "OOO:distance_point_to_plane", &py_pt, &py_plane_co, &py_plane_no)) {
1004  return NULL;
1005  }
1006 
1007  if (((mathutils_array_parse(pt, 3, 3 | MU_ARRAY_SPILL, py_pt, error_prefix) != -1) &&
1008  (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
1009  (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) !=
1010  -1)) == 0) {
1011  return NULL;
1012  }
1013 
1014  plane_from_point_normal_v3(plane, plane_co, plane_no);
1015  return PyFloat_FromDouble(dist_signed_to_plane_v3(pt, plane));
1016 }
1017 
1019  M_Geometry_barycentric_transform_doc,
1020  ".. function:: barycentric_transform(point, tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
1021  "\n"
1022  " Return a transformed point, the transformation is defined by 2 triangles.\n"
1023  "\n"
1024  " :arg point: The point to transform.\n"
1025  " :type point: :class:`mathutils.Vector`\n"
1026  " :arg tri_a1: source triangle vertex.\n"
1027  " :type tri_a1: :class:`mathutils.Vector`\n"
1028  " :arg tri_a2: source triangle vertex.\n"
1029  " :type tri_a2: :class:`mathutils.Vector`\n"
1030  " :arg tri_a3: source triangle vertex.\n"
1031  " :type tri_a3: :class:`mathutils.Vector`\n"
1032  " :arg tri_b1: target triangle vertex.\n"
1033  " :type tri_b1: :class:`mathutils.Vector`\n"
1034  " :arg tri_b2: target triangle vertex.\n"
1035  " :type tri_b2: :class:`mathutils.Vector`\n"
1036  " :arg tri_b3: target triangle vertex.\n"
1037  " :type tri_b3: :class:`mathutils.Vector`\n"
1038  " :return: The transformed point\n"
1039  " :rtype: :class:`mathutils.Vector`'s\n");
1040 static PyObject *M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObject *args)
1041 {
1042  const char *error_prefix = "barycentric_transform";
1043  PyObject *py_pt_src, *py_tri_src[3], *py_tri_dst[3];
1044  float pt_src[3], pt_dst[3], tri_src[3][3], tri_dst[3][3];
1045  int i;
1046 
1047  if (!PyArg_ParseTuple(args,
1048  "OOOOOOO:barycentric_transform",
1049  &py_pt_src,
1050  UNPACK3_EX(&, py_tri_src, ),
1051  UNPACK3_EX(&, py_tri_dst, ))) {
1052  return NULL;
1053  }
1054 
1055  if (mathutils_array_parse(pt_src, 3, 3 | MU_ARRAY_SPILL, py_pt_src, error_prefix) == -1) {
1056  return NULL;
1057  }
1058  for (i = 0; i < ARRAY_SIZE(tri_src); i++) {
1059  if (((mathutils_array_parse(tri_src[i], 3, 3 | MU_ARRAY_SPILL, py_tri_src[i], error_prefix) !=
1060  -1) &&
1061  (mathutils_array_parse(tri_dst[i], 3, 3 | MU_ARRAY_SPILL, py_tri_dst[i], error_prefix) !=
1062  -1)) == 0) {
1063  return NULL;
1064  }
1065  }
1066 
1067  transform_point_by_tri_v3(pt_dst, pt_src, UNPACK3(tri_dst), UNPACK3(tri_src));
1068 
1069  return Vector_CreatePyObject(pt_dst, 3, NULL);
1070 }
1071 
1073  PyObject *py_verts;
1075 };
1076 
1077 static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p)
1078 {
1079  struct PointsInPlanes_UserData *user_data = user_data_p;
1080  PyList_APPEND(user_data->py_verts, Vector_CreatePyObject(co, 3, NULL));
1081  user_data->planes_used[i] = true;
1082  user_data->planes_used[j] = true;
1083  user_data->planes_used[k] = true;
1084 }
1085 
1086 PyDoc_STRVAR(M_Geometry_points_in_planes_doc,
1087  ".. function:: points_in_planes(planes)\n"
1088  "\n"
1089  " Returns a list of points inside all planes given and a list of index values for "
1090  "the planes used.\n"
1091  "\n"
1092  " :arg planes: List of planes (4D vectors).\n"
1093  " :type planes: list of :class:`mathutils.Vector`\n"
1094  " :return: two lists, once containing the vertices inside the planes, another "
1095  "containing the plane indices used\n"
1096  " :rtype: pair of lists\n");
1097 static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *args)
1098 {
1099  PyObject *py_planes;
1100  float(*planes)[4];
1101  uint planes_len;
1102 
1103  if (!PyArg_ParseTuple(args, "O:points_in_planes", &py_planes)) {
1104  return NULL;
1105  }
1106 
1107  if ((planes_len = mathutils_array_parse_alloc_v(
1108  (float **)&planes, 4, py_planes, "points_in_planes")) == -1) {
1109  return NULL;
1110  }
1111 
1112  /* note, this could be refactored into plain C easy - py bits are noted */
1113 
1115  .py_verts = PyList_New(0),
1116  .planes_used = PyMem_Malloc(sizeof(char) * planes_len),
1117  };
1118 
1119  /* python */
1120  PyObject *py_plane_index = PyList_New(0);
1121 
1122  memset(user_data.planes_used, 0, sizeof(char) * planes_len);
1123 
1124  const float eps_coplanar = 1e-4f;
1125  const float eps_isect = 1e-6f;
1126 
1127  const bool has_isect = isect_planes_v3_fn(
1128  planes, planes_len, eps_coplanar, eps_isect, points_in_planes_fn, &user_data);
1129  PyMem_Free(planes);
1130 
1131  /* Now make user_data list of used planes. */
1132  if (has_isect) {
1133  for (int i = 0; i < planes_len; i++) {
1134  if (user_data.planes_used[i]) {
1135  PyList_APPEND(py_plane_index, PyLong_FromLong(i));
1136  }
1137  }
1138  }
1139  PyMem_Free(user_data.planes_used);
1140 
1141  {
1142  PyObject *ret = PyTuple_New(2);
1143  PyTuple_SET_ITEMS(ret, user_data.py_verts, py_plane_index);
1144  return ret;
1145  }
1146 }
1147 
1148 #ifndef MATH_STANDALONE
1149 
1150 PyDoc_STRVAR(M_Geometry_interpolate_bezier_doc,
1151  ".. function:: interpolate_bezier(knot1, handle1, handle2, knot2, resolution)\n"
1152  "\n"
1153  " Interpolate a bezier spline segment.\n"
1154  "\n"
1155  " :arg knot1: First bezier spline point.\n"
1156  " :type knot1: :class:`mathutils.Vector`\n"
1157  " :arg handle1: First bezier spline handle.\n"
1158  " :type handle1: :class:`mathutils.Vector`\n"
1159  " :arg handle2: Second bezier spline handle.\n"
1160  " :type handle2: :class:`mathutils.Vector`\n"
1161  " :arg knot2: Second bezier spline point.\n"
1162  " :type knot2: :class:`mathutils.Vector`\n"
1163  " :arg resolution: Number of points to return.\n"
1164  " :type resolution: int\n"
1165  " :return: The interpolated points\n"
1166  " :rtype: list of :class:`mathutils.Vector`'s\n");
1167 static PyObject *M_Geometry_interpolate_bezier(PyObject *UNUSED(self), PyObject *args)
1168 {
1169  const char *error_prefix = "interpolate_bezier";
1170  PyObject *py_data[4];
1171  float data[4][4] = {{0.0f}};
1172  int resolu;
1173  int dims = 0;
1174  int i;
1175  float *coord_array, *fp;
1176  PyObject *list;
1177 
1178  if (!PyArg_ParseTuple(args, "OOOOi:interpolate_bezier", UNPACK4_EX(&, py_data, ), &resolu)) {
1179  return NULL;
1180  }
1181 
1182  for (i = 0; i < 4; i++) {
1183  int dims_tmp;
1184  if ((dims_tmp = mathutils_array_parse(
1185  data[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_data[i], error_prefix)) == -1) {
1186  return NULL;
1187  }
1188  dims = max_ii(dims, dims_tmp);
1189  }
1190 
1191  if (resolu <= 1) {
1192  PyErr_SetString(PyExc_ValueError, "resolution must be 2 or over");
1193  return NULL;
1194  }
1195 
1196  coord_array = MEM_callocN(dims * (resolu) * sizeof(float), error_prefix);
1197  for (i = 0; i < dims; i++) {
1199  UNPACK4_EX(, data, [i]), coord_array + i, resolu - 1, sizeof(float) * dims);
1200  }
1201 
1202  list = PyList_New(resolu);
1203  fp = coord_array;
1204  for (i = 0; i < resolu; i++, fp = fp + dims) {
1205  PyList_SET_ITEM(list, i, Vector_CreatePyObject(fp, dims, NULL));
1206  }
1207  MEM_freeN(coord_array);
1208  return list;
1209 }
1210 
1211 PyDoc_STRVAR(M_Geometry_tessellate_polygon_doc,
1212  ".. function:: tessellate_polygon(veclist_list)\n"
1213  "\n"
1214  " Takes a list of polylines (each point a pair or triplet of numbers) and returns "
1215  "the point indices for a polyline filled with triangles. Does not handle degenerate "
1216  "geometry (such as zero-length lines due to consecutive identical points).\n"
1217  "\n"
1218  " :arg veclist_list: list of polylines\n"
1219  " :rtype: list\n");
1220 /* PolyFill function, uses Blenders scanfill to fill multiple poly lines */
1221 static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject *polyLineSeq)
1222 {
1223  PyObject *tri_list; /*return this list of tri's */
1224  PyObject *polyLine, *polyVec;
1225  int i, len_polylines, len_polypoints;
1226  bool list_parse_error = false;
1227  bool is_2d = true;
1228 
1229  /* Display #ListBase. */
1230  ListBase dispbase = {NULL, NULL};
1231  DispList *dl;
1232  float *fp; /*pointer to the array of malloced dl->verts to set the points from the vectors */
1233  int totpoints = 0;
1234 
1235  if (!PySequence_Check(polyLineSeq)) {
1236  PyErr_SetString(PyExc_TypeError, "expected a sequence of poly lines");
1237  return NULL;
1238  }
1239 
1240  len_polylines = PySequence_Size(polyLineSeq);
1241 
1242  for (i = 0; i < len_polylines; i++) {
1243  polyLine = PySequence_GetItem(polyLineSeq, i);
1244  if (!PySequence_Check(polyLine)) {
1245  BKE_displist_free(&dispbase);
1246  Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/
1247  PyErr_SetString(PyExc_TypeError,
1248  "One or more of the polylines is not a sequence of mathutils.Vector's");
1249  return NULL;
1250  }
1251 
1252  len_polypoints = PySequence_Size(polyLine);
1253  if (len_polypoints > 0) { /* don't bother adding edges as polylines */
1254  dl = MEM_callocN(sizeof(DispList), "poly disp");
1255  BLI_addtail(&dispbase, dl);
1256  dl->type = DL_INDEX3;
1257  dl->nr = len_polypoints;
1258  dl->type = DL_POLY;
1259  dl->parts = 1; /* no faces, 1 edge loop */
1260  dl->col = 0; /* no material */
1261  dl->verts = fp = MEM_mallocN(sizeof(float[3]) * len_polypoints, "dl verts");
1262  dl->index = MEM_callocN(sizeof(int[3]) * len_polypoints, "dl index");
1263 
1264  for (int index = 0; index < len_polypoints; index++, fp += 3) {
1265  polyVec = PySequence_GetItem(polyLine, index);
1266  const int polyVec_len = mathutils_array_parse(
1267  fp, 2, 3 | MU_ARRAY_SPILL, polyVec, "tessellate_polygon: parse coord");
1268  Py_DECREF(polyVec);
1269 
1270  if (UNLIKELY(polyVec_len == -1)) {
1271  list_parse_error = true;
1272  }
1273  else if (polyVec_len == 2) {
1274  fp[2] = 0.0f;
1275  }
1276  else if (polyVec_len == 3) {
1277  is_2d = false;
1278  }
1279 
1280  totpoints++;
1281  }
1282  }
1283  Py_DECREF(polyLine);
1284  }
1285 
1286  if (list_parse_error) {
1287  BKE_displist_free(&dispbase); /* possible some dl was allocated */
1288  return NULL;
1289  }
1290  if (totpoints) {
1291  /* now make the list to return */
1292  BKE_displist_fill(&dispbase, &dispbase, is_2d ? ((const float[3]){0, 0, -1}) : NULL, false);
1293 
1294  /* The faces are stored in a new DisplayList
1295  * that's added to the head of the #ListBase. */
1296  dl = dispbase.first;
1297 
1298  tri_list = PyList_New(dl->parts);
1299  if (!tri_list) {
1300  BKE_displist_free(&dispbase);
1301  PyErr_SetString(PyExc_RuntimeError, "failed to make a new list");
1302  return NULL;
1303  }
1304 
1305  int *dl_face = dl->index;
1306  for (int index = 0; index < dl->parts; index++) {
1307  PyList_SET_ITEM(tri_list, index, PyC_Tuple_Pack_I32(dl_face[0], dl_face[1], dl_face[2]));
1308  dl_face += 3;
1309  }
1310  BKE_displist_free(&dispbase);
1311  }
1312  else {
1313  /* no points, do this so scripts don't barf */
1314  BKE_displist_free(&dispbase); /* possible some dl was allocated */
1315  tri_list = PyList_New(0);
1316  }
1317 
1318  return tri_list;
1319 }
1320 
1321 static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray)
1322 {
1323  Py_ssize_t len, i;
1324  PyObject *list_item, *item_1, *item_2;
1325  BoxPack *boxarray;
1326 
1327  /* Error checking must already be done */
1328  if (!PyList_Check(value)) {
1329  PyErr_SetString(PyExc_TypeError, "can only back a list of [x, y, w, h]");
1330  return -1;
1331  }
1332 
1333  len = PyList_GET_SIZE(value);
1334 
1335  boxarray = MEM_mallocN(sizeof(BoxPack) * len, __func__);
1336 
1337  for (i = 0; i < len; i++) {
1338  list_item = PyList_GET_ITEM(value, i);
1339  if (!PyList_Check(list_item) || PyList_GET_SIZE(list_item) < 4) {
1340  MEM_freeN(boxarray);
1341  PyErr_SetString(PyExc_TypeError, "can only pack a list of [x, y, w, h]");
1342  return -1;
1343  }
1344 
1345  BoxPack *box = &boxarray[i];
1346 
1347  item_1 = PyList_GET_ITEM(list_item, 2);
1348  item_2 = PyList_GET_ITEM(list_item, 3);
1349 
1350  box->w = (float)PyFloat_AsDouble(item_1);
1351  box->h = (float)PyFloat_AsDouble(item_2);
1352  box->index = i;
1353 
1354  /* accounts for error case too and overwrites with own error */
1355  if (box->w < 0.0f || box->h < 0.0f) {
1356  MEM_freeN(boxarray);
1357  PyErr_SetString(PyExc_TypeError,
1358  "error parsing width and height values from list: "
1359  "[x, y, w, h], not numbers or below zero");
1360  return -1;
1361  }
1362 
1363  /* verts will be added later */
1364  }
1365 
1366  *r_boxarray = boxarray;
1367  return 0;
1368 }
1369 
1370 static void boxPack_ToPyObject(PyObject *value, const BoxPack *boxarray)
1371 {
1372  Py_ssize_t len, i;
1373  PyObject *list_item;
1374 
1375  len = PyList_GET_SIZE(value);
1376 
1377  for (i = 0; i < len; i++) {
1378  const BoxPack *box = &boxarray[i];
1379  list_item = PyList_GET_ITEM(value, box->index);
1380  PyList_SET_ITEM(list_item, 0, PyFloat_FromDouble(box->x));
1381  PyList_SET_ITEM(list_item, 1, PyFloat_FromDouble(box->y));
1382  }
1383 }
1384 
1385 PyDoc_STRVAR(M_Geometry_box_pack_2d_doc,
1386  ".. function:: box_pack_2d(boxes)\n"
1387  "\n"
1388  " Returns a tuple with the width and height of the packed bounding box.\n"
1389  "\n"
1390  " :arg boxes: list of boxes, each box is a list where the first 4 items are [x, y, "
1391  "width, height, ...] other items are ignored.\n"
1392  " :type boxes: list\n"
1393  " :return: the width and height of the packed bounding box\n"
1394  " :rtype: tuple, pair of floats\n");
1395 static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlist)
1396 {
1397  float tot_width = 0.0f, tot_height = 0.0f;
1398  Py_ssize_t len;
1399 
1400  PyObject *ret;
1401 
1402  if (!PyList_Check(boxlist)) {
1403  PyErr_SetString(PyExc_TypeError, "expected a list of boxes [[x, y, w, h], ... ]");
1404  return NULL;
1405  }
1406 
1407  len = PyList_GET_SIZE(boxlist);
1408  if (len) {
1409  BoxPack *boxarray = NULL;
1410  if (boxPack_FromPyObject(boxlist, &boxarray) == -1) {
1411  return NULL; /* exception set */
1412  }
1413 
1414  /* Non Python function */
1415  BLI_box_pack_2d(boxarray, len, &tot_width, &tot_height);
1416 
1417  boxPack_ToPyObject(boxlist, boxarray);
1418  MEM_freeN(boxarray);
1419  }
1420 
1421  ret = PyTuple_New(2);
1422  PyTuple_SET_ITEMS(ret, PyFloat_FromDouble(tot_width), PyFloat_FromDouble(tot_height));
1423  return ret;
1424 }
1425 
1426 PyDoc_STRVAR(M_Geometry_box_fit_2d_doc,
1427  ".. function:: box_fit_2d(points)\n"
1428  "\n"
1429  " Returns an angle that best fits the points to an axis aligned rectangle\n"
1430  "\n"
1431  " :arg points: list of 2d points.\n"
1432  " :type points: list\n"
1433  " :return: angle\n"
1434  " :rtype: float\n");
1435 static PyObject *M_Geometry_box_fit_2d(PyObject *UNUSED(self), PyObject *pointlist)
1436 {
1437  float(*points)[2];
1438  Py_ssize_t len;
1439 
1440  float angle = 0.0f;
1441 
1442  len = mathutils_array_parse_alloc_v(((float **)&points), 2, pointlist, "box_fit_2d");
1443  if (len == -1) {
1444  return NULL;
1445  }
1446 
1447  if (len) {
1448  /* Non Python function */
1450 
1451  PyMem_Free(points);
1452  }
1453 
1454  return PyFloat_FromDouble(angle);
1455 }
1456 
1457 PyDoc_STRVAR(M_Geometry_convex_hull_2d_doc,
1458  ".. function:: convex_hull_2d(points)\n"
1459  "\n"
1460  " Returns a list of indices into the list given\n"
1461  "\n"
1462  " :arg points: list of 2d points.\n"
1463  " :type points: list\n"
1464  " :return: a list of indices\n"
1465  " :rtype: list of ints\n");
1466 static PyObject *M_Geometry_convex_hull_2d(PyObject *UNUSED(self), PyObject *pointlist)
1467 {
1468  float(*points)[2];
1469  Py_ssize_t len;
1470 
1471  PyObject *ret;
1472 
1473  len = mathutils_array_parse_alloc_v(((float **)&points), 2, pointlist, "convex_hull_2d");
1474  if (len == -1) {
1475  return NULL;
1476  }
1477 
1478  if (len) {
1479  int *index_map;
1480  Py_ssize_t len_ret, i;
1481 
1482  index_map = MEM_mallocN(sizeof(*index_map) * len * 2, __func__);
1483 
1484  /* Non Python function */
1485  len_ret = BLI_convexhull_2d(points, len, index_map);
1486 
1487  ret = PyList_New(len_ret);
1488  for (i = 0; i < len_ret; i++) {
1489  PyList_SET_ITEM(ret, i, PyLong_FromLong(index_map[i]));
1490  }
1491 
1492  MEM_freeN(index_map);
1493 
1494  PyMem_Free(points);
1495  }
1496  else {
1497  ret = PyList_New(0);
1498  }
1499 
1500  return ret;
1501 }
1502 
1503 /* Return a PyObject that is a list of lists, using the flattened list array
1504  * to fill values, with start_table and len_table giving the start index
1505  * and length of the toplevel_len sub-lists.
1506  */
1507 static PyObject *list_of_lists_from_arrays(const int *array,
1508  const int *start_table,
1509  const int *len_table,
1510  int toplevel_len)
1511 {
1512  PyObject *ret, *sublist;
1513  int i, j, sublist_len, sublist_start, val;
1514 
1515  ret = PyList_New(toplevel_len);
1516  for (i = 0; i < toplevel_len; i++) {
1517  sublist_len = len_table[i];
1518  sublist = PyList_New(sublist_len);
1519  sublist_start = start_table[i];
1520  for (j = 0; j < sublist_len; j++) {
1521  val = array[sublist_start + j];
1522  PyList_SET_ITEM(sublist, j, PyLong_FromLong(val));
1523  }
1524  PyList_SET_ITEM(ret, i, sublist);
1525  }
1526  return ret;
1527 }
1528 
1529 PyDoc_STRVAR(
1530  M_Geometry_delaunay_2d_cdt_doc,
1531  ".. function:: delaunay_2d_cdt(vert_coords, edges, faces, output_type, epsilon)\n"
1532  "\n"
1533  " Computes the Constrained Delaunay Triangulation of a set of vertices,\n"
1534  " with edges and faces that must appear in the triangulation.\n"
1535  " Some triangles may be eaten away, or combined with other triangles,\n"
1536  " according to output type.\n"
1537  " The returned verts may be in a different order from input verts, may be moved\n"
1538  " slightly, and may be merged with other nearby verts.\n"
1539  " The three returned orig lists give, for each of verts, edges, and faces, the list of\n"
1540  " input element indices corresponding to the positionally same output element.\n"
1541  " For edges, the orig indices start with the input edges and then continue\n"
1542  " with the edges implied by each of the faces (n of them for an n-gon).\n"
1543  "\n"
1544  " :arg vert_coords: Vertex coordinates (2d)\n"
1545  " :type vert_coords: list of :class:`mathutils.Vector`\n"
1546  " :arg edges: Edges, as pairs of indices in `vert_coords`\n"
1547  " :type edges: list of (int, int)\n"
1548  " :arg faces: Faces, each sublist is a face, as indices in `vert_coords` (CCW oriented)\n"
1549  " :type faces: list of list of int\n"
1550  " :arg output_type: What output looks like. 0 => triangles with convex hull. "
1551  "1 => triangles inside constraints. "
1552  "2 => the input constraints, intersected. "
1553  "3 => like 2 but with extra edges to make valid BMesh faces.\n"
1554  " :type output_type: int\\n"
1555  " :arg epsilon: For nearness tests; should not be zero\n"
1556  " :type epsilon: float\n"
1557  " :return: Output tuple, (vert_coords, edges, faces, orig_verts, orig_edges, orig_faces)\n"
1558  " :rtype: (list of `mathutils.Vector`, "
1559  "list of (int, int), "
1560  "list of list of int, "
1561  "list of list of int, "
1562  "list of list of int, "
1563  "list of list of int)\n"
1564  "\n");
1565 static PyObject *M_Geometry_delaunay_2d_cdt(PyObject *UNUSED(self), PyObject *args)
1566 {
1567  const char *error_prefix = "delaunay_2d_cdt";
1568  PyObject *vert_coords, *edges, *faces, *item;
1569  int output_type;
1570  float epsilon;
1571  float(*in_coords)[2] = NULL;
1572  int(*in_edges)[2] = NULL;
1573  int *in_faces = NULL;
1574  int *in_faces_start_table = NULL;
1575  int *in_faces_len_table = NULL;
1576  Py_ssize_t vert_coords_len, edges_len, faces_len;
1577  CDT_input in;
1578  CDT_result *res = NULL;
1579  PyObject *out_vert_coords = NULL;
1580  PyObject *out_edges = NULL;
1581  PyObject *out_faces = NULL;
1582  PyObject *out_orig_verts = NULL;
1583  PyObject *out_orig_edges = NULL;
1584  PyObject *out_orig_faces = NULL;
1585  PyObject *ret_value = NULL;
1586  int i;
1587 
1588  if (!PyArg_ParseTuple(
1589  args, "OOOif:delaunay_2d_cdt", &vert_coords, &edges, &faces, &output_type, &epsilon)) {
1590  return NULL;
1591  }
1592 
1593  vert_coords_len = mathutils_array_parse_alloc_v(
1594  (float **)&in_coords, 2, vert_coords, error_prefix);
1595  if (vert_coords_len == -1) {
1596  return NULL;
1597  }
1598 
1599  edges_len = mathutils_array_parse_alloc_vi((int **)&in_edges, 2, edges, error_prefix);
1600  if (edges_len == -1) {
1601  goto exit_cdt;
1602  }
1603 
1605  &in_faces, &in_faces_start_table, &in_faces_len_table, faces, error_prefix);
1606  if (faces_len == -1) {
1607  goto exit_cdt;
1608  }
1609 
1610  in.verts_len = (int)vert_coords_len;
1611  in.vert_coords = in_coords;
1612  in.edges_len = edges_len;
1613  in.faces_len = faces_len;
1614  in.edges = in_edges;
1615  in.faces = in_faces;
1616  in.faces_start_table = in_faces_start_table;
1617  in.faces_len_table = in_faces_len_table;
1618  in.epsilon = epsilon;
1619 
1620  res = BLI_delaunay_2d_cdt_calc(&in, output_type);
1621 
1622  ret_value = PyTuple_New(6);
1623 
1624  out_vert_coords = PyList_New(res->verts_len);
1625  for (i = 0; i < res->verts_len; i++) {
1626  item = Vector_CreatePyObject(res->vert_coords[i], 2, NULL);
1627  if (item == NULL) {
1628  Py_DECREF(ret_value);
1629  Py_DECREF(out_vert_coords);
1630  goto exit_cdt;
1631  }
1632  PyList_SET_ITEM(out_vert_coords, i, item);
1633  }
1634  PyTuple_SET_ITEM(ret_value, 0, out_vert_coords);
1635 
1636  out_edges = PyList_New(res->edges_len);
1637  for (i = 0; i < res->edges_len; i++) {
1638  item = PyTuple_New(2);
1639  PyTuple_SET_ITEM(item, 0, PyLong_FromLong((long)res->edges[i][0]));
1640  PyTuple_SET_ITEM(item, 1, PyLong_FromLong((long)res->edges[i][1]));
1641  PyList_SET_ITEM(out_edges, i, item);
1642  }
1643  PyTuple_SET_ITEM(ret_value, 1, out_edges);
1644 
1645  out_faces = list_of_lists_from_arrays(
1646  res->faces, res->faces_start_table, res->faces_len_table, res->faces_len);
1647  PyTuple_SET_ITEM(ret_value, 2, out_faces);
1648 
1649  out_orig_verts = list_of_lists_from_arrays(
1651  PyTuple_SET_ITEM(ret_value, 3, out_orig_verts);
1652 
1653  out_orig_edges = list_of_lists_from_arrays(
1655  PyTuple_SET_ITEM(ret_value, 4, out_orig_edges);
1656 
1657  out_orig_faces = list_of_lists_from_arrays(
1659  PyTuple_SET_ITEM(ret_value, 5, out_orig_faces);
1660 
1661 exit_cdt:
1662  if (in_coords != NULL) {
1663  PyMem_Free(in_coords);
1664  }
1665  if (in_edges != NULL) {
1666  PyMem_Free(in_edges);
1667  }
1668  if (in_faces != NULL) {
1669  PyMem_Free(in_faces);
1670  }
1671  if (in_faces_start_table != NULL) {
1672  PyMem_Free(in_faces_start_table);
1673  }
1674  if (in_faces_len_table != NULL) {
1675  PyMem_Free(in_faces_len_table);
1676  }
1677  if (res) {
1679  }
1680  return ret_value;
1681 }
1682 
1683 #endif /* MATH_STANDALONE */
1684 
1685 static PyMethodDef M_Geometry_methods[] = {
1686  {"intersect_ray_tri",
1687  (PyCFunction)M_Geometry_intersect_ray_tri,
1688  METH_VARARGS,
1689  M_Geometry_intersect_ray_tri_doc},
1690  {"intersect_point_line",
1691  (PyCFunction)M_Geometry_intersect_point_line,
1692  METH_VARARGS,
1693  M_Geometry_intersect_point_line_doc},
1694  {"intersect_point_tri",
1695  (PyCFunction)M_Geometry_intersect_point_tri,
1696  METH_VARARGS,
1697  M_Geometry_intersect_point_tri_doc},
1698  {"closest_point_on_tri",
1699  (PyCFunction)M_Geometry_closest_point_on_tri,
1700  METH_VARARGS,
1701  M_Geometry_closest_point_on_tri_doc},
1702  {"intersect_point_tri_2d",
1703  (PyCFunction)M_Geometry_intersect_point_tri_2d,
1704  METH_VARARGS,
1705  M_Geometry_intersect_point_tri_2d_doc},
1706  {"intersect_point_quad_2d",
1708  METH_VARARGS,
1709  M_Geometry_intersect_point_quad_2d_doc},
1710  {"intersect_line_line",
1711  (PyCFunction)M_Geometry_intersect_line_line,
1712  METH_VARARGS,
1713  M_Geometry_intersect_line_line_doc},
1714  {"intersect_line_line_2d",
1715  (PyCFunction)M_Geometry_intersect_line_line_2d,
1716  METH_VARARGS,
1717  M_Geometry_intersect_line_line_2d_doc},
1718  {"intersect_line_plane",
1719  (PyCFunction)M_Geometry_intersect_line_plane,
1720  METH_VARARGS,
1721  M_Geometry_intersect_line_plane_doc},
1722  {"intersect_plane_plane",
1723  (PyCFunction)M_Geometry_intersect_plane_plane,
1724  METH_VARARGS,
1725  M_Geometry_intersect_plane_plane_doc},
1726  {"intersect_line_sphere",
1727  (PyCFunction)M_Geometry_intersect_line_sphere,
1728  METH_VARARGS,
1729  M_Geometry_intersect_line_sphere_doc},
1730  {"intersect_line_sphere_2d",
1732  METH_VARARGS,
1733  M_Geometry_intersect_line_sphere_2d_doc},
1734  {"distance_point_to_plane",
1736  METH_VARARGS,
1737  M_Geometry_distance_point_to_plane_doc},
1738  {"intersect_sphere_sphere_2d",
1740  METH_VARARGS,
1741  M_Geometry_intersect_sphere_sphere_2d_doc},
1742  {"intersect_tri_tri_2d",
1743  (PyCFunction)M_Geometry_intersect_tri_tri_2d,
1744  METH_VARARGS,
1745  M_Geometry_intersect_tri_tri_2d_doc},
1746  {"area_tri", (PyCFunction)M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_doc},
1747  {"volume_tetrahedron",
1748  (PyCFunction)M_Geometry_volume_tetrahedron,
1749  METH_VARARGS,
1750  M_Geometry_volume_tetrahedron_doc},
1751  {"normal", (PyCFunction)M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},
1752  {"barycentric_transform",
1753  (PyCFunction)M_Geometry_barycentric_transform,
1754  METH_VARARGS,
1755  M_Geometry_barycentric_transform_doc},
1756  {"points_in_planes",
1757  (PyCFunction)M_Geometry_points_in_planes,
1758  METH_VARARGS,
1759  M_Geometry_points_in_planes_doc},
1760 #ifndef MATH_STANDALONE
1761  {"interpolate_bezier",
1762  (PyCFunction)M_Geometry_interpolate_bezier,
1763  METH_VARARGS,
1764  M_Geometry_interpolate_bezier_doc},
1765  {"tessellate_polygon",
1766  (PyCFunction)M_Geometry_tessellate_polygon,
1767  METH_O,
1768  M_Geometry_tessellate_polygon_doc},
1769  {"convex_hull_2d",
1770  (PyCFunction)M_Geometry_convex_hull_2d,
1771  METH_O,
1772  M_Geometry_convex_hull_2d_doc},
1773  {"delaunay_2d_cdt",
1774  (PyCFunction)M_Geometry_delaunay_2d_cdt,
1775  METH_VARARGS,
1776  M_Geometry_delaunay_2d_cdt_doc},
1777  {"box_fit_2d", (PyCFunction)M_Geometry_box_fit_2d, METH_O, M_Geometry_box_fit_2d_doc},
1778  {"box_pack_2d", (PyCFunction)M_Geometry_box_pack_2d, METH_O, M_Geometry_box_pack_2d_doc},
1779 #endif
1780  {NULL, NULL, 0, NULL},
1781 };
1782 
1783 static struct PyModuleDef M_Geometry_module_def = {
1784  PyModuleDef_HEAD_INIT,
1785  "mathutils.geometry", /* m_name */
1786  M_Geometry_doc, /* m_doc */
1787  0, /* m_size */
1788  M_Geometry_methods, /* m_methods */
1789  NULL, /* m_reload */
1790  NULL, /* m_traverse */
1791  NULL, /* m_clear */
1792  NULL, /* m_free */
1793 };
1794 
1795 /*----------------------------MODULE INIT-------------------------*/
1796 PyMODINIT_FUNC PyInit_mathutils_geometry(void)
1797 {
1798  PyObject *submodule = PyModule_Create(&M_Geometry_module_def);
1799  return submodule;
1800 }
typedef float(TangentPoint)[2]
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition: curve.c:1804
display list (or rather multi purpose list) stuff.
void BKE_displist_free(struct ListBase *lb)
Definition: displist.c:81
@ DL_INDEX3
Definition: BKE_displist.h:42
@ DL_POLY
Definition: BKE_displist.h:36
void BKE_displist_fill(const struct ListBase *dispbase, struct ListBase *to, const float normal_proj[3], const bool flip_normal)
void BLI_box_pack_2d(BoxPack *boxarray, const unsigned int len, float *r_tot_x, float *r_tot_y)
Definition: boxpack_2d.c:294
float BLI_convexhull_aabb_fit_points_2d(const float(*points)[2], unsigned int n)
int BLI_convexhull_2d(const float(*points)[2], const int n, int r_points[])
CDT_result * BLI_delaunay_2d_cdt_calc(const CDT_input *input, const CDT_output_type output_type)
void BLI_delaunay_2d_cdt_free(CDT_result *result)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
MINLINE int max_ii(int a, int b)
bool isect_tri_tri_v2(const float p1[2], const float q1[2], const float r1[2], const float p2[2], const float q2[2], const float r2[2])
Definition: math_geom.c:2695
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition: math_geom.c:243
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])
Definition: math_geom.c:1616
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])
Definition: math_geom.c:3103
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
Definition: math_geom.c:2265
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])
Definition: math_geom.c:4121
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])
Definition: math_geom.c:3612
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
Definition: math_geom.c:3469
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])
Definition: math_geom.c:1023
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition: math_geom.c:468
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:116
int isect_line_sphere_v3(const float l1[3], const float l2[3], const float sp[3], const float r, float r_p1[3], float r_p2[3])
Definition: math_geom.c:1431
int isect_seg_seg_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float vi[2])
Definition: math_geom.c:1353
int isect_line_sphere_v2(const float l1[2], const float l2[2], const float sp[2], const float r, float r_p1[2], float r_p2[2])
Definition: math_geom.c:1494
bool isect_planes_v3_fn(const float planes[][4], const int planes_len, const float eps_coplanar, const float eps_isect, void(*callback_fn)(const float co[3], int i, int j, int k, void *user_data), void *user_data)
Definition: math_geom.c:2312
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
Definition: math_geom.c:3449
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition: math_geom.c:3364
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
Definition: math_geom.c:2191
float normal_poly_v3(float n[3], const float verts[][3], unsigned int nr)
Definition: math_geom.c:94
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
Definition: math_geom.c:1595
float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition: math_geom.c:274
MINLINE float normalize_v3(float r[3])
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 len_v2(const float a[2]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNPACK3_EX(pre, a, post)
#define UNPACK4(a)
#define ARRAY_SIZE(arr)
#define UNPACK4_EX(pre, a, post)
#define UNUSED(x)
#define UNPACK3(a)
#define UNLIKELY(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint i1
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
void * user_data
GPUBatch * quad
#define powf(x, y)
#define fabsf(x)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
int mathutils_array_parse_alloc_viseq(int **array, int **start_table, int **len_table, PyObject *value, const char *error_prefix)
Definition: mathutils.c:404
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:361
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition: mathutils.c:283
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
Definition: mathutils.c:118
#define MU_ARRAY_ZERO
Definition: mathutils.h:184
#define MU_ARRAY_SPILL
Definition: mathutils.h:187
PyObject * Vector_CreatePyObject(const float *vec, const int size, PyTypeObject *base_type)
static PyObject * M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *args)
PyDoc_STRVAR(M_Geometry_doc, "The Blender geometry module")
static PyObject * M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_line_line_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_point_quad_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_closest_point_on_tri(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_volume_tetrahedron(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_normal(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_plane_plane(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_point_tri(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_point_tri_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObject *args)
static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray)
static PyObject * M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObject *args)
static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p)
static PyObject * M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlist)
static PyObject * M_Geometry_interpolate_bezier(PyObject *UNUSED(self), PyObject *args)
static void boxPack_ToPyObject(PyObject *value, const BoxPack *boxarray)
static PyObject * M_Geometry_intersect_tri_tri_2d(PyObject *UNUSED(self), PyObject *args)
static PyObject * M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject *polyLineSeq)
static PyObject * M_Geometry_area_tri(PyObject *UNUSED(self), PyObject *args)
PyMODINIT_FUNC PyInit_mathutils_geometry(void)
static char faces[256]
static double epsilon
int PyC_ParseBool(PyObject *o, void *p)
#define PyC_Tuple_Pack_I32(...)
Definition: py_capi_utils.h:67
#define PyTuple_SET_ITEMS(op_arg,...)
return ret
float(* vert_coords)[2]
int(* edges)[2]
int * faces_len_table
int * faces_start_table
int * faces_start_table
int * verts_orig_len_table
int * edges_orig_len_table
int * verts_orig_start_table
int * faces_orig_start_table
int * edges_orig_start_table
int * faces_orig_len_table
int(* edges)[2]
int * faces_len_table
float(* vert_coords)[2]
short type
Definition: BKE_displist.h:71
short col
Definition: BKE_displist.h:73
float * verts
Definition: BKE_displist.h:74
int * index
Definition: BKE_displist.h:75
void * first
Definition: DNA_listBase.h:47
uint len