Blender V5.3
mathutils_geometry.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <Python.h>
10
11#include "mathutils.hh"
12#include "mathutils_geometry.hh"
13
14/* Used for PolyFill */
15#ifndef MATH_STANDALONE /* define when building outside blender */
16# include "BLI_boxpack_2d.hh"
17# include "BLI_convexhull_2d.hh"
18# include "BLI_delaunay_2d.hh"
19# include "BLI_listbase.hh"
20
21# include "BKE_curve.hh"
22# include "BKE_displist.h"
23
24# include "MEM_guardedalloc.h"
25#endif /* !MATH_STANDALONE */
26
27#include "BLI_math_geom_c.hh"
28#include "BLI_math_vector_c.hh"
29#include "BLI_offset_indices.hh"
30#include "BLI_utildefines.hh"
31
33#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
35
36namespace blender {
37
38/* ---------------------------------INTERSECTION FUNCTIONS-------------------- */
39
41 /* Wrap. */
42 M_Geometry_intersect_ray_tri_doc,
43 ".. function:: intersect_ray_tri(v1, v2, v3, ray, orig, clip=True, /)\n"
44 "\n"
45 " Returns the intersection between a ray and a triangle, if possible, returns None "
46 "otherwise.\n"
47 "\n"
48 " :param v1: Point1\n"
49 " :type v1: :class:`mathutils.Vector`\n"
50 " :param v2: Point2\n"
51 " :type v2: :class:`mathutils.Vector`\n"
52 " :param v3: Point3\n"
53 " :type v3: :class:`mathutils.Vector`\n"
54 " :param ray: Direction of the ray\n"
55 " :type ray: :class:`mathutils.Vector`\n"
56 " :param orig: Origin\n"
57 " :type orig: :class:`mathutils.Vector`\n"
58 " :param clip: When False, don't restrict the intersection to the area of the "
59 "triangle, use the infinite plane defined by the triangle.\n"
60 " :type clip: bool\n"
61 " :return: The point of intersection or None if no intersection is found\n"
62 " :rtype: :class:`mathutils.Vector` | None\n");
63static PyObject *M_Geometry_intersect_ray_tri(PyObject * /*self*/, PyObject *args)
64{
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;
69 bool clip = true;
70 int i;
71
72 if (!PyArg_ParseTuple(args,
73 "O" /* `v1` */
74 "O" /* `v2` */
75 "O" /* `v3` */
76 "O" /* `ray` */
77 "O" /* `orig` */
78 "|" /* Optional arguments. */
79 "O&" /* `clip` */
80 ":intersect_ray_tri",
81 UNPACK3_EX(&, py_tri, ),
82 &py_ray,
83 &py_ray_off,
85 &clip))
86 {
87 return nullptr;
88 }
89
90 if (((mathutils_array_parse(dir, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray, error_prefix) !=
91 -1) &&
93 orig, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray_off, error_prefix) != -1)) == 0)
94 {
95 return nullptr;
96 }
97
98 for (i = 0; i < ARRAY_SIZE(tri); i++) {
100 tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1)
101 {
102 return nullptr;
103 }
104 }
105
106 normalize_v3(dir);
107
108 /* find vectors for two edges sharing v1 */
109 sub_v3_v3v3(e1, tri[1], tri[0]);
110 sub_v3_v3v3(e2, tri[2], tri[0]);
111
112 /* begin calculating determinant - also used to calculated U parameter */
113 cross_v3_v3v3(pvec, dir, e2);
114
115 /* if determinant is near zero, ray lies in plane of triangle */
116 det = dot_v3v3(e1, pvec);
117
118 if (det > -0.000001f && det < 0.000001f) {
119 Py_RETURN_NONE;
120 }
121
122 inv_det = 1.0f / det;
123
124 /* calculate distance from v1 to ray origin */
125 sub_v3_v3v3(tvec, orig, tri[0]);
126
127 /* calculate U parameter and test bounds */
128 u = dot_v3v3(tvec, pvec) * inv_det;
129 if (clip && (u < 0.0f || u > 1.0f)) {
130 Py_RETURN_NONE;
131 }
132
133 /* prepare to test the V parameter */
134 cross_v3_v3v3(qvec, tvec, e1);
135
136 /* calculate V parameter and test bounds */
137 v = dot_v3v3(dir, qvec) * inv_det;
138
139 if (clip && (v < 0.0f || u + v > 1.0f)) {
140 Py_RETURN_NONE;
141 }
142
143 /* calculate t, ray intersects triangle */
144 t = dot_v3v3(e2, qvec) * inv_det;
145
146 /* ray hit behind */
147 if (t < 0.0f) {
148 Py_RETURN_NONE;
149 }
150
151 mul_v3_fl(dir, t);
152 add_v3_v3v3(pvec, orig, dir);
153
154 return Vector_CreatePyObject(pvec, 3, nullptr);
155}
156
157/* Line-Line intersection using algorithm from mathworld.wolfram.com */
158
160 /* Wrap. */
161 M_Geometry_intersect_line_line_doc,
162 ".. function:: intersect_line_line(v1, v2, v3, v4, /)\n"
163 "\n"
164 " Returns a tuple with the points on each line respectively closest to the other.\n"
165 "\n"
166 " :param v1: First point of the first line\n"
167 " :type v1: :class:`mathutils.Vector`\n"
168 " :param v2: Second point of the first line\n"
169 " :type v2: :class:`mathutils.Vector`\n"
170 " :param v3: First point of the second line\n"
171 " :type v3: :class:`mathutils.Vector`\n"
172 " :param v4: Second point of the second line\n"
173 " :type v4: :class:`mathutils.Vector`\n"
174 " :return: The intersection on each line or None when the lines are parallel.\n"
175 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | None\n");
176static PyObject *M_Geometry_intersect_line_line(PyObject * /*self*/, PyObject *args)
177{
178 const char *error_prefix = "intersect_line_line";
179 PyObject *tuple;
180 PyObject *py_lines[4];
181 float lines[4][3], i1[3], i2[3];
182 int ix_vec_num;
183 int result;
184
185 if (!PyArg_ParseTuple(args,
186 "O" /* `v1` */
187 "O" /* `v2` */
188 "O" /* `v3` */
189 "O" /* `v4` */
190 ":intersect_line_line",
191 UNPACK4_EX(&, py_lines, )))
192 {
193 return nullptr;
194 }
195
196 if ((((ix_vec_num = mathutils_array_parse(
197 lines[0], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[0], error_prefix)) != -1) &&
198 (mathutils_array_parse(lines[1],
199 ix_vec_num,
200 ix_vec_num | MU_ARRAY_SPILL | MU_ARRAY_ZERO,
201 py_lines[1],
202 error_prefix) != -1) &&
203 (mathutils_array_parse(lines[2],
204 ix_vec_num,
205 ix_vec_num | MU_ARRAY_SPILL | MU_ARRAY_ZERO,
206 py_lines[2],
207 error_prefix) != -1) &&
208 (mathutils_array_parse(lines[3],
209 ix_vec_num,
210 ix_vec_num | MU_ARRAY_SPILL | MU_ARRAY_ZERO,
211 py_lines[3],
212 error_prefix) != -1)) == 0)
213 {
214 return nullptr;
215 }
216
217 /* Zero 3rd axis of 2D vectors. */
218 if (ix_vec_num == 2) {
219 lines[1][2] = 0.0f;
220 lines[2][2] = 0.0f;
221 lines[3][2] = 0.0f;
222 }
223
224 result = isect_line_line_v3(UNPACK4(lines), i1, i2);
225 /* The return-code isn't exposed,
226 * this way we can check know how close the lines are. */
227 if (result == 1) {
228 closest_to_line_v3(i2, i1, lines[2], lines[3]);
229 }
230
231 if (result == 0) {
232 /* Parallel. */
233 Py_RETURN_NONE;
234 }
235
236 tuple = PyTuple_New(2);
237 PyTuple_SET_ITEMS(tuple,
238 Vector_CreatePyObject(i1, ix_vec_num, nullptr),
239 Vector_CreatePyObject(i2, ix_vec_num, nullptr));
240 return tuple;
241}
242
243/* Line-Line intersection using algorithm from mathworld.wolfram.com */
244
246 /* Wrap. */
247 M_Geometry_intersect_sphere_sphere_2d_doc,
248 ".. function:: intersect_sphere_sphere_2d(p_a, radius_a, p_b, radius_b, /)\n"
249 "\n"
250 " Returns the 2 intersection points of two circles.\n"
251 "\n"
252 " :param p_a: Center of the first circle\n"
253 " :type p_a: :class:`mathutils.Vector`\n"
254 " :param radius_a: Radius of the first circle\n"
255 " :type radius_a: float\n"
256 " :param p_b: Center of the second circle\n"
257 " :type p_b: :class:`mathutils.Vector`\n"
258 " :param radius_b: Radius of the second circle\n"
259 " :type radius_b: float\n"
260 " :return: The 2 intersection points or None when there is no intersection.\n"
261 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | "
262 "tuple[None, None]\n");
263static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject * /*self*/, PyObject *args)
264{
265 const char *error_prefix = "intersect_sphere_sphere_2d";
266 PyObject *ret;
267 PyObject *py_v_a, *py_v_b;
268 float v_a[2], v_b[2];
269 float rad_a, rad_b;
270 float v_ab[2];
271 float dist;
272
273 if (!PyArg_ParseTuple(args,
274 "O" /* `p_a` */
275 "f" /* `radius_a` */
276 "O" /* `p_b` */
277 "f" /* `radius_b` */
278 ":intersect_sphere_sphere_2d",
279 &py_v_a,
280 &rad_a,
281 &py_v_b,
282 &rad_b))
283 {
284 return nullptr;
285 }
286
287 if (((mathutils_array_parse(v_a, 2, 2, py_v_a, error_prefix) != -1) &&
288 (mathutils_array_parse(v_b, 2, 2, py_v_b, error_prefix) != -1)) == 0)
289 {
290 return nullptr;
291 }
292
293 ret = PyTuple_New(2);
294
295 sub_v2_v2v2(v_ab, v_b, v_a);
296 dist = len_v2(v_ab);
297
298 if (/* out of range */
299 (dist > rad_a + rad_b) ||
300 /* fully-contained in the other */
301 (dist < fabsf(rad_a - rad_b)) ||
302 /* co-incident */
303 (dist < FLT_EPSILON))
304 {
305 /* out of range */
306 PyTuple_SET_ITEMS(ret, Py_NewRef(Py_None), Py_NewRef(Py_None));
307 }
308 else {
309 const float dist_delta = ((rad_a * rad_a) - (rad_b * rad_b) + (dist * dist)) / (2.0f * dist);
310 const float h = powf(fabsf((rad_a * rad_a) - (dist_delta * dist_delta)), 0.5f);
311 float i_cent[2];
312 float i1[2], i2[2];
313
314 i_cent[0] = v_a[0] + ((v_ab[0] * dist_delta) / dist);
315 i_cent[1] = v_a[1] + ((v_ab[1] * dist_delta) / dist);
316
317 i1[0] = i_cent[0] + h * v_ab[1] / dist;
318 i1[1] = i_cent[1] - h * v_ab[0] / dist;
319
320 i2[0] = i_cent[0] - h * v_ab[1] / dist;
321 i2[1] = i_cent[1] + h * v_ab[0] / dist;
322
324 ret, Vector_CreatePyObject(i1, 2, nullptr), Vector_CreatePyObject(i2, 2, nullptr));
325 }
326
327 return ret;
328}
329
331 /* Wrap. */
332 M_Geometry_intersect_tri_tri_2d_doc,
333 ".. function:: intersect_tri_tri_2d(tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3, /)\n"
334 "\n"
335 " Check if two 2D triangles intersect.\n"
336 "\n"
337 " :param tri_a1: First vertex of the first triangle.\n"
338 " :type tri_a1: :class:`mathutils.Vector`\n"
339 " :param tri_a2: Second vertex of the first triangle.\n"
340 " :type tri_a2: :class:`mathutils.Vector`\n"
341 " :param tri_a3: Third vertex of the first triangle.\n"
342 " :type tri_a3: :class:`mathutils.Vector`\n"
343 " :param tri_b1: First vertex of the second triangle.\n"
344 " :type tri_b1: :class:`mathutils.Vector`\n"
345 " :param tri_b2: Second vertex of the second triangle.\n"
346 " :type tri_b2: :class:`mathutils.Vector`\n"
347 " :param tri_b3: Third vertex of the second triangle.\n"
348 " :type tri_b3: :class:`mathutils.Vector`\n"
349 " :return: True if the triangles intersect.\n"
350 " :rtype: bool\n");
351static PyObject *M_Geometry_intersect_tri_tri_2d(PyObject * /*self*/, PyObject *args)
352{
353 const char *error_prefix = "intersect_tri_tri_2d";
354 PyObject *tri_pair_py[2][3];
355 float tri_pair[2][3][2];
356
357 if (!PyArg_ParseTuple(args,
358 "O" /* `tri_a1` */
359 "O" /* `tri_a2` */
360 "O" /* `tri_a3` */
361 "O" /* `tri_b1` */
362 "O" /* `tri_b2` */
363 "O" /* `tri_b3` */
364 ":intersect_tri_tri_2d",
365 &tri_pair_py[0][0],
366 &tri_pair_py[0][1],
367 &tri_pair_py[0][2],
368 &tri_pair_py[1][0],
369 &tri_pair_py[1][1],
370 &tri_pair_py[1][2]))
371 {
372 return nullptr;
373 }
374
375 for (int i = 0; i < 2; i++) {
376 for (int j = 0; j < 3; j++) {
378 tri_pair[i][j], 2, 2 | MU_ARRAY_SPILL, tri_pair_py[i][j], error_prefix) == -1)
379 {
380 return nullptr;
381 }
382 }
383 }
384
385 const bool ret = isect_tri_tri_v2(UNPACK3(tri_pair[0]), UNPACK3(tri_pair[1]));
386 return PyBool_FromLong(ret);
387}
388
390 /* Wrap. */
391 M_Geometry_normal_doc,
392 ".. function:: normal(*vectors)\n"
393 "\n"
394 " Returns the normal of a 3D polygon.\n"
395 "\n"
396 " :param vectors: 3 or more vectors to calculate normals.\n"
397 " :type vectors: Sequence[Sequence[float]]\n"
398 " :return: The normal vector.\n"
399 " :rtype: :class:`mathutils.Vector`\n");
400static PyObject *M_Geometry_normal(PyObject * /*self*/, PyObject *args)
401{
402 float (*coords)[3];
403 int coords_len;
404 float n[3];
405 PyObject *ret = nullptr;
406
407 /* use */
408 if (PyTuple_GET_SIZE(args) == 1) {
409 args = PyTuple_GET_ITEM(args, 0);
410 }
411
412 if ((coords_len = mathutils_array_parse_alloc_v(
413 reinterpret_cast<float **>(&coords), 3 | MU_ARRAY_SPILL, args, "normal")) == -1)
414 {
415 return nullptr;
416 }
417
418 if (coords_len < 3) {
419 PyErr_SetString(PyExc_ValueError, "Expected 3 or more vectors");
420 }
421 else {
422 normal_poly_v3(n, coords, coords_len);
423 ret = Vector_CreatePyObject(n, 3, nullptr);
424 }
425
426 PyMem_Free(coords);
427 return ret;
428}
429
430/* --------------------------------- AREA FUNCTIONS-------------------- */
431
433 /* Wrap. */
434 M_Geometry_area_tri_doc,
435 ".. function:: area_tri(v1, v2, v3, /)\n"
436 "\n"
437 " Returns the area of the 2D or 3D triangle defined.\n"
438 "\n"
439 " :param v1: Point1\n"
440 " :type v1: :class:`mathutils.Vector`\n"
441 " :param v2: Point2\n"
442 " :type v2: :class:`mathutils.Vector`\n"
443 " :param v3: Point3\n"
444 " :type v3: :class:`mathutils.Vector`\n"
445 " :return: The area of the triangle.\n"
446 " :rtype: float\n");
447static PyObject *M_Geometry_area_tri(PyObject * /*self*/, PyObject *args)
448{
449 const char *error_prefix = "area_tri";
450 PyObject *py_tri[3];
451 float tri[3][3];
452 int len;
453
454 if (!PyArg_ParseTuple(args,
455 "O" /* `v1` */
456 "O" /* `v2` */
457 "O" /* `v3` */
458 ":area_tri",
459 UNPACK3_EX(&, py_tri, )))
460 {
461 return nullptr;
462 }
463
464 if ((((len = mathutils_array_parse(tri[0], 2, 3, py_tri[0], error_prefix)) != -1) &&
465 (mathutils_array_parse(tri[1], len, len, py_tri[1], error_prefix) != -1) &&
466 (mathutils_array_parse(tri[2], len, len, py_tri[2], error_prefix) != -1)) == 0)
467 {
468 return nullptr;
469 }
470
471 return PyFloat_FromDouble((len == 3 ? area_tri_v3 : area_tri_v2)(UNPACK3(tri)));
472}
473
475 /* Wrap. */
476 M_Geometry_volume_tetrahedron_doc,
477 ".. function:: volume_tetrahedron(v1, v2, v3, v4, /)\n"
478 "\n"
479 " Return the absolute (unsigned) volume formed by a tetrahedron "
480 "(points can be in any order).\n"
481 "\n"
482 " :param v1: Point1\n"
483 " :type v1: :class:`mathutils.Vector`\n"
484 " :param v2: Point2\n"
485 " :type v2: :class:`mathutils.Vector`\n"
486 " :param v3: Point3\n"
487 " :type v3: :class:`mathutils.Vector`\n"
488 " :param v4: Point4\n"
489 " :type v4: :class:`mathutils.Vector`\n"
490 " :return: The volume of the tetrahedron.\n"
491 " :rtype: float\n");
492static PyObject *M_Geometry_volume_tetrahedron(PyObject * /*self*/, PyObject *args)
493{
494 const char *error_prefix = "volume_tetrahedron";
495 PyObject *py_tet[4];
496 float tet[4][3];
497 int i;
498
499 if (!PyArg_ParseTuple(args,
500 "O" /* `v1` */
501 "O" /* `v2` */
502 "O" /* `v3` */
503 "O" /* `v4` */
504 ":volume_tetrahedron",
505 UNPACK4_EX(&, py_tet, )))
506 {
507 return nullptr;
508 }
509
510 for (i = 0; i < ARRAY_SIZE(tet); i++) {
511 if (mathutils_array_parse(tet[i], 3, 3 | MU_ARRAY_SPILL, py_tet[i], error_prefix) == -1) {
512 return nullptr;
513 }
514 }
515
516 return PyFloat_FromDouble(volume_tetrahedron_v3(UNPACK4(tet)));
517}
518
520 /* Wrap. */
521 M_Geometry_intersect_line_line_2d_doc,
522 ".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2, /)\n"
523 "\n"
524 " Takes 2 segments (defined by 4 vectors) and returns a vector for their point of "
525 "intersection or None.\n"
526 "\n"
527 " .. warning:: Despite its name, this function works on segments, and not on lines.\n"
528 "\n"
529 " :param lineA_p1: First point of the first segment\n"
530 " :type lineA_p1: :class:`mathutils.Vector`\n"
531 " :param lineA_p2: Second point of the first segment\n"
532 " :type lineA_p2: :class:`mathutils.Vector`\n"
533 " :param lineB_p1: First point of the second segment\n"
534 " :type lineB_p1: :class:`mathutils.Vector`\n"
535 " :param lineB_p2: Second point of the second segment\n"
536 " :type lineB_p2: :class:`mathutils.Vector`\n"
537 " :return: The point of intersection or None when not found\n"
538 " :rtype: :class:`mathutils.Vector` | None\n");
539static PyObject *M_Geometry_intersect_line_line_2d(PyObject * /*self*/, PyObject *args)
540{
541 const char *error_prefix = "intersect_line_line_2d";
542 PyObject *py_lines[4];
543 float lines[4][2];
544 float vi[2];
545 int i;
546
547 if (!PyArg_ParseTuple(args,
548 "O" /* `lineA_p1` */
549 "O" /* `lineA_p2` */
550 "O" /* `lineB_p1` */
551 "O" /* `lineB_p2` */
552 ":intersect_line_line_2d",
553 UNPACK4_EX(&, py_lines, )))
554 {
555 return nullptr;
556 }
557
558 for (i = 0; i < ARRAY_SIZE(lines); i++) {
559 if (mathutils_array_parse(lines[i], 2, 2 | MU_ARRAY_SPILL, py_lines[i], error_prefix) == -1) {
560 return nullptr;
561 }
562 }
563
564 if (isect_seg_seg_v2_point(UNPACK4(lines), vi) == 1) {
565 return Vector_CreatePyObject(vi, 2, nullptr);
566 }
567
568 Py_RETURN_NONE;
569}
570
572 /* Wrap. */
573 M_Geometry_intersect_line_plane_doc,
574 ".. function:: intersect_line_plane(line_a, line_b, plane_co, plane_no, no_flip=False, /)\n"
575 "\n"
576 " Calculate the intersection between a line (as 2 vectors) and a plane.\n"
577 " Returns a vector for the intersection or None.\n"
578 "\n"
579 " :param line_a: First point of the line\n"
580 " :type line_a: :class:`mathutils.Vector`\n"
581 " :param line_b: Second point of the line\n"
582 " :type line_b: :class:`mathutils.Vector`\n"
583 " :param plane_co: A point on the plane\n"
584 " :type plane_co: :class:`mathutils.Vector`\n"
585 " :param plane_no: The direction the plane is facing\n"
586 " :type plane_no: :class:`mathutils.Vector`\n"
587 " :param no_flip: Currently ignored.\n"
588 " :type no_flip: bool\n"
589 " :return: The point of intersection or None when not found\n"
590 " :rtype: :class:`mathutils.Vector` | None\n");
591static PyObject *M_Geometry_intersect_line_plane(PyObject * /*self*/, PyObject *args)
592{
593 const char *error_prefix = "intersect_line_plane";
594 PyObject *py_line_a, *py_line_b, *py_plane_co, *py_plane_no;
595 float line_a[3], line_b[3], plane_co[3], plane_no[3];
596 float isect[3];
597 const bool no_flip = false;
598
599 if (!PyArg_ParseTuple(args,
600 "O" /* `line_a` */
601 "O" /* `line_b` */
602 "O" /* `plane_co` */
603 "O" /* `plane_no` */
604 "|" /* Optional arguments. */
605 "O&" /* `no_flip` */
606 ":intersect_line_plane",
607 &py_line_a,
608 &py_line_b,
609 &py_plane_co,
610 &py_plane_no,
612 &no_flip))
613 {
614 return nullptr;
615 }
616
617 if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
618 (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
619 (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
620 (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) !=
621 -1)) == 0)
622 {
623 return nullptr;
624 }
625
626 /* TODO: implements no_flip */
627 if (isect_line_plane_v3(isect, line_a, line_b, plane_co, plane_no) == 1) {
628 return Vector_CreatePyObject(isect, 3, nullptr);
629 }
630
631 Py_RETURN_NONE;
632}
633
635 /* Wrap. */
636 M_Geometry_intersect_plane_plane_doc,
637 ".. function:: intersect_plane_plane(plane_a_co, plane_a_no, plane_b_co, plane_b_no, /)\n"
638 "\n"
639 " Return the intersection between two planes\n"
640 "\n"
641 " :param plane_a_co: Point on the first plane\n"
642 " :type plane_a_co: :class:`mathutils.Vector`\n"
643 " :param plane_a_no: Normal of the first plane\n"
644 " :type plane_a_no: :class:`mathutils.Vector`\n"
645 " :param plane_b_co: Point on the second plane\n"
646 " :type plane_b_co: :class:`mathutils.Vector`\n"
647 " :param plane_b_no: Normal of the second plane\n"
648 " :type plane_b_no: :class:`mathutils.Vector`\n"
649 " :return: The line of the intersection represented as a point and a vector or None if the "
650 "intersection can't be calculated\n"
651 " :rtype: tuple[:class:`mathutils.Vector`, :class:`mathutils.Vector`] | "
652 "tuple[None, None]\n");
653static PyObject *M_Geometry_intersect_plane_plane(PyObject * /*self*/, PyObject *args)
654{
655 const char *error_prefix = "intersect_plane_plane";
656 PyObject *ret, *ret_co, *ret_no;
657 PyObject *py_plane_a_co, *py_plane_a_no, *py_plane_b_co, *py_plane_b_no;
658 float plane_a_co[3], plane_a_no[3], plane_b_co[3], plane_b_no[3];
659 float plane_a[4], plane_b[4];
660
661 float isect_co[3];
662 float isect_no[3];
663
664 if (!PyArg_ParseTuple(args,
665 "O" /* `plane_a_co` */
666 "O" /* `plane_a_no` */
667 "O" /* `plane_b_co` */
668 "O" /* `plane_b_no` */
669 ":intersect_plane_plane",
670 &py_plane_a_co,
671 &py_plane_a_no,
672 &py_plane_b_co,
673 &py_plane_b_no))
674 {
675 return nullptr;
676 }
677
678 if (((mathutils_array_parse(plane_a_co, 3, 3 | MU_ARRAY_SPILL, py_plane_a_co, error_prefix) !=
679 -1) &&
680 (mathutils_array_parse(plane_a_no, 3, 3 | MU_ARRAY_SPILL, py_plane_a_no, error_prefix) !=
681 -1) &&
682 (mathutils_array_parse(plane_b_co, 3, 3 | MU_ARRAY_SPILL, py_plane_b_co, error_prefix) !=
683 -1) &&
684 (mathutils_array_parse(plane_b_no, 3, 3 | MU_ARRAY_SPILL, py_plane_b_no, error_prefix) !=
685 -1)) == 0)
686 {
687 return nullptr;
688 }
689
690 plane_from_point_normal_v3(plane_a, plane_a_co, plane_a_no);
691 plane_from_point_normal_v3(plane_b, plane_b_co, plane_b_no);
692
693 if (isect_plane_plane_v3(plane_a, plane_b, isect_co, isect_no)) {
694 normalize_v3(isect_no);
695
696 ret_co = Vector_CreatePyObject(isect_co, 3, nullptr);
697 ret_no = Vector_CreatePyObject(isect_no, 3, nullptr);
698 }
699 else {
700 ret_co = Py_NewRef(Py_None);
701 ret_no = Py_NewRef(Py_None);
702 }
703
704 ret = PyTuple_New(2);
705 PyTuple_SET_ITEMS(ret, ret_co, ret_no);
706 return ret;
707}
708
710 /* Wrap. */
711 M_Geometry_intersect_line_sphere_doc,
712 ".. function:: intersect_line_sphere(line_a, line_b, sphere_co, sphere_radius, clip=True, /)\n"
713 "\n"
714 " Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
715 " returns the intersection\n"
716 "\n"
717 " :param line_a: First point of the line\n"
718 " :type line_a: :class:`mathutils.Vector`\n"
719 " :param line_b: Second point of the line\n"
720 " :type line_b: :class:`mathutils.Vector`\n"
721 " :param sphere_co: The center of the sphere\n"
722 " :type sphere_co: :class:`mathutils.Vector`\n"
723 " :param sphere_radius: Radius of the sphere\n"
724 " :type sphere_radius: float\n"
725 " :param clip: When False, don't restrict the intersection to the line segment.\n"
726 " :type clip: bool\n"
727 " :return: The intersection points as a pair of vectors "
728 "(each is None when not found).\n"
729 " :rtype: tuple[:class:`mathutils.Vector` | None, :class:`mathutils.Vector` | None]\n");
730static PyObject *M_Geometry_intersect_line_sphere(PyObject * /*self*/, PyObject *args)
731{
732 const char *error_prefix = "intersect_line_sphere";
733 PyObject *py_line_a, *py_line_b, *py_sphere_co;
734 float line_a[3], line_b[3], sphere_co[3];
735 float sphere_radius;
736 bool clip = true;
737
738 float isect_a[3];
739 float isect_b[3];
740
741 if (!PyArg_ParseTuple(args,
742 "O" /* `line_a` */
743 "O" /* `line_b` */
744 "O" /* `sphere_co` */
745 "f" /* `sphere_radius` */
746 "|" /* Optional arguments. */
747 "O&" /* `clip` */
748 ":intersect_line_sphere",
749 &py_line_a,
750 &py_line_b,
751 &py_sphere_co,
752 &sphere_radius,
754 &clip))
755 {
756 return nullptr;
757 }
758
759 if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
760 (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
761 (mathutils_array_parse(sphere_co, 3, 3 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) !=
762 -1)) == 0)
763 {
764 return nullptr;
765 }
766
767 bool use_a = true;
768 bool use_b = true;
769 float lambda;
770
771 PyObject *ret = PyTuple_New(2);
772
773 switch (isect_line_sphere_v3(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
774 case 1:
775 if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) &&
776 (lambda <= 1.0f))))
777 {
778 use_a = false;
779 }
780 use_b = false;
781 break;
782 case 2:
783 if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) &&
784 (lambda <= 1.0f))))
785 {
786 use_a = false;
787 }
788 if (!(!clip || (((lambda = line_point_factor_v3(isect_b, line_a, line_b)) >= 0.0f) &&
789 (lambda <= 1.0f))))
790 {
791 use_b = false;
792 }
793 break;
794 default:
795 use_a = false;
796 use_b = false;
797 break;
798 }
799
801 use_a ? Vector_CreatePyObject(isect_a, 3, nullptr) : Py_NewRef(Py_None),
802 use_b ? Vector_CreatePyObject(isect_b, 3, nullptr) : Py_NewRef(Py_None));
803
804 return ret;
805}
806
807/* keep in sync with M_Geometry_intersect_line_sphere */
809 /* Wrap. */
810 M_Geometry_intersect_line_sphere_2d_doc,
811 ".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, "
812 "sphere_radius, clip=True, /)\n"
813 "\n"
814 " Takes a line (as 2 points) and a circle (as a point and a radius) and\n"
815 " returns the intersection\n"
816 "\n"
817 " :param line_a: First point of the line\n"
818 " :type line_a: :class:`mathutils.Vector`\n"
819 " :param line_b: Second point of the line\n"
820 " :type line_b: :class:`mathutils.Vector`\n"
821 " :param sphere_co: The center of the circle\n"
822 " :type sphere_co: :class:`mathutils.Vector`\n"
823 " :param sphere_radius: Radius of the circle\n"
824 " :type sphere_radius: float\n"
825 " :param clip: When False, don't restrict the intersection to the line segment.\n"
826 " :type clip: bool\n"
827 " :return: The intersection points as a pair of vectors "
828 "(each is None when not found).\n"
829 " :rtype: tuple[:class:`mathutils.Vector` | None, :class:`mathutils.Vector` | None]\n");
830static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject * /*self*/, PyObject *args)
831{
832 const char *error_prefix = "intersect_line_sphere_2d";
833 PyObject *py_line_a, *py_line_b, *py_sphere_co;
834 float line_a[2], line_b[2], sphere_co[2];
835 float sphere_radius;
836 bool clip = true;
837
838 float isect_a[2];
839 float isect_b[2];
840
841 if (!PyArg_ParseTuple(args,
842 "O" /* `line_a` */
843 "O" /* `line_b` */
844 "O" /* `sphere_co` */
845 "f" /* `sphere_radius` */
846 "|" /* Optional arguments. */
847 "O&" /* `clip` */
848 ":intersect_line_sphere_2d",
849 &py_line_a,
850 &py_line_b,
851 &py_sphere_co,
852 &sphere_radius,
854 &clip))
855 {
856 return nullptr;
857 }
858
859 if (((mathutils_array_parse(line_a, 2, 2 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
860 (mathutils_array_parse(line_b, 2, 2 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
861 (mathutils_array_parse(sphere_co, 2, 2 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) !=
862 -1)) == 0)
863 {
864 return nullptr;
865 }
866
867 bool use_a = true;
868 bool use_b = true;
869 float lambda;
870
871 PyObject *ret = PyTuple_New(2);
872
873 switch (isect_line_sphere_v2(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
874 case 1:
875 if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) &&
876 (lambda <= 1.0f))))
877 {
878 use_a = false;
879 }
880 use_b = false;
881 break;
882 case 2:
883 if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) &&
884 (lambda <= 1.0f))))
885 {
886 use_a = false;
887 }
888 if (!(!clip || (((lambda = line_point_factor_v2(isect_b, line_a, line_b)) >= 0.0f) &&
889 (lambda <= 1.0f))))
890 {
891 use_b = false;
892 }
893 break;
894 default:
895 use_a = false;
896 use_b = false;
897 break;
898 }
899
901 use_a ? Vector_CreatePyObject(isect_a, 2, nullptr) : Py_NewRef(Py_None),
902 use_b ? Vector_CreatePyObject(isect_b, 2, nullptr) : Py_NewRef(Py_None));
903
904 return ret;
905}
906
908 /* Wrap. */
909 M_Geometry_intersect_point_line_doc,
910 ".. function:: intersect_point_line(pt, line_p1, line_p2, /)\n"
911 "\n"
912 " Takes a point and a line and returns the closest point on the line and its "
913 "parametric distance from the first point of the line. "
914 "A value of 0.0 is the first point, 1.0 is the second, "
915 "values outside [0, 1] are extrapolated.\n"
916 "\n"
917 " :param pt: Point\n"
918 " :type pt: :class:`mathutils.Vector`\n"
919 " :param line_p1: First point of the line\n"
920 " :type line_p1: :class:`mathutils.Vector`\n"
921 " :param line_p2: Second point of the line\n"
922 " :type line_p2: :class:`mathutils.Vector`\n"
923 " :return: The closest point on the line and its parametric distance from the first point.\n"
924 " :rtype: tuple[:class:`mathutils.Vector`, float]\n");
925static PyObject *M_Geometry_intersect_point_line(PyObject * /*self*/,
926 PyObject *const *args,
927 Py_ssize_t nargs)
928{
929 const char *error_prefix = "intersect_point_line";
930 float pt[3], pt_out[3], line_a[3], line_b[3];
931 int pt_num = 2;
932
933 if (!_PyArg_CheckPositional(error_prefix, nargs, 3, 3)) {
934 return nullptr;
935 }
936
937 PyObject *py_pt = args[0];
938 PyObject *py_line_a = args[1];
939 PyObject *py_line_b = args[2];
940
941 /* Accept 2D verts. */
942 if ((((pt_num = mathutils_array_parse(
943 pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix)) != -1) &&
945 line_a, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_a, error_prefix) != -1) &&
947 line_b, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_b, error_prefix) != -1)) == 0)
948 {
949 return nullptr;
950 }
951
952 /* Do the calculation. */
953 const float lambda = closest_to_line_v3(pt_out, pt, line_a, line_b);
954
955 PyObject *ret = PyTuple_New(2);
957 ret, Vector_CreatePyObject(pt_out, pt_num, nullptr), PyFloat_FromDouble(lambda));
958 return ret;
959}
960
962 /* Wrap. */
963 M_Geometry_intersect_point_line_segment_doc,
964 ".. function:: intersect_point_line_segment(pt, seg_p1, seg_p2, /)\n"
965 "\n"
966 " Takes a point and a segment and returns the closest point on the segment "
967 "and the distance to the segment.\n"
968 "\n"
969 " :param pt: Point\n"
970 " :type pt: :class:`mathutils.Vector`\n"
971 " :param seg_p1: First point of the segment\n"
972 " :type seg_p1: :class:`mathutils.Vector`\n"
973 " :param seg_p2: Second point of the segment\n"
974 " :type seg_p2: :class:`mathutils.Vector`\n"
975 " :return: The closest point on the segment and the distance to the segment.\n"
976 " :rtype: tuple[:class:`mathutils.Vector`, float]\n");
977static PyObject *M_Geometry_intersect_point_line_segment(PyObject * /*self*/,
978 PyObject *const *args,
979 Py_ssize_t nargs)
980{
981 const char *error_prefix = "intersect_point_line_segment";
982 float pt[3], pt_out[3], seg_a[3], seg_b[3];
983 int pt_num = 2;
984
985 if (!_PyArg_CheckPositional(error_prefix, nargs, 3, 3)) {
986 return nullptr;
987 }
988
989 PyObject *py_pt = args[0];
990 PyObject *py_seq_a = args[1];
991 PyObject *py_seg_b = args[2];
992
993 /* Accept 2D verts. */
994 if ((((pt_num = mathutils_array_parse(
995 pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix)) != -1) &&
997 seg_a, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_seq_a, error_prefix) != -1) &&
999 seg_b, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_seg_b, error_prefix) != -1)) == 0)
1000 {
1001 return nullptr;
1002 }
1003
1004 /* Do the calculation. */
1005 closest_to_line_segment_v3(pt_out, pt, seg_a, seg_b);
1006 const float lambda = len_v3v3(pt_out, pt);
1007
1008 PyObject *ret = PyTuple_New(2);
1010 ret, Vector_CreatePyObject(pt_out, pt_num, nullptr), PyFloat_FromDouble(lambda));
1011 return ret;
1012}
1013
1015 /* Wrap. */
1016 M_Geometry_intersect_point_tri_doc,
1017 ".. function:: intersect_point_tri(pt, tri_p1, tri_p2, tri_p3, /)\n"
1018 "\n"
1019 " Takes 4 vectors: one is the point and the next 3 define the triangle. Projects "
1020 "the point onto the triangle plane and checks if it is within the triangle.\n"
1021 "\n"
1022 " :param pt: Point\n"
1023 " :type pt: :class:`mathutils.Vector`\n"
1024 " :param tri_p1: First point of the triangle\n"
1025 " :type tri_p1: :class:`mathutils.Vector`\n"
1026 " :param tri_p2: Second point of the triangle\n"
1027 " :type tri_p2: :class:`mathutils.Vector`\n"
1028 " :param tri_p3: Third point of the triangle\n"
1029 " :type tri_p3: :class:`mathutils.Vector`\n"
1030 " :return: Point on the triangle's plane or None if it's outside the triangle\n"
1031 " :rtype: :class:`mathutils.Vector` | None\n");
1032static PyObject *M_Geometry_intersect_point_tri(PyObject * /*self*/, PyObject *args)
1033{
1034 const char *error_prefix = "intersect_point_tri";
1035 PyObject *py_pt, *py_tri[3];
1036 float pt[3], tri[3][3];
1037 float vi[3];
1038 int i;
1039
1040 if (!PyArg_ParseTuple(args,
1041 "O" /* `pt` */
1042 "O" /* `tri_p1` */
1043 "O" /* `tri_p2` */
1044 "O" /* `tri_p3` */
1045 ":intersect_point_tri",
1046 &py_pt,
1047 UNPACK3_EX(&, py_tri, )))
1048 {
1049 return nullptr;
1050 }
1051
1052 if (mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix) == -1)
1053 {
1054 return nullptr;
1055 }
1056 for (i = 0; i < ARRAY_SIZE(tri); i++) {
1058 tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1)
1059 {
1060 return nullptr;
1061 }
1062 }
1063
1064 if (isect_point_tri_v3(pt, UNPACK3(tri), vi)) {
1065 return Vector_CreatePyObject(vi, 3, nullptr);
1066 }
1067
1068 Py_RETURN_NONE;
1069}
1070
1072 /* Wrap. */
1073 M_Geometry_closest_point_on_tri_doc,
1074 ".. function:: closest_point_on_tri(pt, tri_p1, tri_p2, tri_p3, /)\n"
1075 "\n"
1076 " Takes 4 vectors: one is the point and the next 3 define the triangle.\n"
1077 "\n"
1078 " :param pt: Point\n"
1079 " :type pt: :class:`mathutils.Vector`\n"
1080 " :param tri_p1: First point of the triangle\n"
1081 " :type tri_p1: :class:`mathutils.Vector`\n"
1082 " :param tri_p2: Second point of the triangle\n"
1083 " :type tri_p2: :class:`mathutils.Vector`\n"
1084 " :param tri_p3: Third point of the triangle\n"
1085 " :type tri_p3: :class:`mathutils.Vector`\n"
1086 " :return: The closest point of the triangle.\n"
1087 " :rtype: :class:`mathutils.Vector`\n");
1088static PyObject *M_Geometry_closest_point_on_tri(PyObject * /*self*/, PyObject *args)
1089{
1090 const char *error_prefix = "closest_point_on_tri";
1091 PyObject *py_pt, *py_tri[3];
1092 float pt[3], tri[3][3];
1093 float vi[3];
1094 int i;
1095
1096 if (!PyArg_ParseTuple(args,
1097 "O" /* `pt` */
1098 "O" /* `tri_p1` */
1099 "O" /* `tri_p2` */
1100 "O" /* `tri_p3` */
1101 ":closest_point_on_tri",
1102 &py_pt,
1103 UNPACK3_EX(&, py_tri, )))
1104 {
1105 return nullptr;
1106 }
1107
1108 if (mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix) == -1)
1109 {
1110 return nullptr;
1111 }
1112 for (i = 0; i < ARRAY_SIZE(tri); i++) {
1114 tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1)
1115 {
1116 return nullptr;
1117 }
1118 }
1119
1120 closest_on_tri_to_point_v3(vi, pt, UNPACK3(tri));
1121
1122 return Vector_CreatePyObject(vi, 3, nullptr);
1123}
1124
1126 /* Wrap. */
1127 M_Geometry_intersect_point_tri_2d_doc,
1128 ".. function:: intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3, /)\n"
1129 "\n"
1130 " Takes 4 vectors (using only the x and y coordinates): one is the point and the next 3 "
1131 "define the triangle. Returns a non-zero value if the point is within the triangle, otherwise "
1132 "0.\n"
1133 "\n"
1134 " :param pt: Point\n"
1135 " :type pt: :class:`mathutils.Vector`\n"
1136 " :param tri_p1: First point of the triangle\n"
1137 " :type tri_p1: :class:`mathutils.Vector`\n"
1138 " :param tri_p2: Second point of the triangle\n"
1139 " :type tri_p2: :class:`mathutils.Vector`\n"
1140 " :param tri_p3: Third point of the triangle\n"
1141 " :type tri_p3: :class:`mathutils.Vector`\n"
1142 " :return: 1 if inside with CCW winding, -1 if inside with CW winding, otherwise 0.\n"
1143 " :rtype: int\n");
1144static PyObject *M_Geometry_intersect_point_tri_2d(PyObject * /*self*/, PyObject *args)
1145{
1146 const char *error_prefix = "intersect_point_tri_2d";
1147 PyObject *py_pt, *py_tri[3];
1148 float pt[2], tri[3][2];
1149 int i;
1150
1151 if (!PyArg_ParseTuple(args,
1152 "O" /* `pt` */
1153 "O" /* `tri_p1` */
1154 "O" /* `tri_p2` */
1155 "O" /* `tri_p3` */
1156 ":intersect_point_tri_2d",
1157 &py_pt,
1158 UNPACK3_EX(&, py_tri, )))
1159 {
1160 return nullptr;
1161 }
1162
1163 if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
1164 return nullptr;
1165 }
1166 for (i = 0; i < ARRAY_SIZE(tri); i++) {
1167 if (mathutils_array_parse(tri[i], 2, 2 | MU_ARRAY_SPILL, py_tri[i], error_prefix) == -1) {
1168 return nullptr;
1169 }
1170 }
1171
1172 return PyLong_FromLong(isect_point_tri_v2(pt, UNPACK3(tri)));
1173}
1174
1176 /* Wrap. */
1177 M_Geometry_intersect_point_quad_2d_doc,
1178 ".. function:: intersect_point_quad_2d(pt, quad_p1, quad_p2, quad_p3, quad_p4, /)\n"
1179 "\n"
1180 " Takes 5 vectors (using only the x and y coordinates): one is the point and the "
1181 "next 4 define the quad,\n"
1182 " only the x and y are used from the vectors. Returns a non-zero value if the point is "
1183 "within the quad, otherwise 0.\n"
1184 " Works only with convex quads without singular edges.\n"
1185 "\n"
1186 " :param pt: Point\n"
1187 " :type pt: :class:`mathutils.Vector`\n"
1188 " :param quad_p1: First point of the quad\n"
1189 " :type quad_p1: :class:`mathutils.Vector`\n"
1190 " :param quad_p2: Second point of the quad\n"
1191 " :type quad_p2: :class:`mathutils.Vector`\n"
1192 " :param quad_p3: Third point of the quad\n"
1193 " :type quad_p3: :class:`mathutils.Vector`\n"
1194 " :param quad_p4: Fourth point of the quad\n"
1195 " :type quad_p4: :class:`mathutils.Vector`\n"
1196 " :return: 1 if inside with CCW winding, -1 if inside with CW winding, otherwise 0.\n"
1197 " :rtype: int\n");
1198static PyObject *M_Geometry_intersect_point_quad_2d(PyObject * /*self*/, PyObject *args)
1199{
1200 const char *error_prefix = "intersect_point_quad_2d";
1201 PyObject *py_pt, *py_quad[4];
1202 float pt[2], quad[4][2];
1203 int i;
1204
1205 if (!PyArg_ParseTuple(args,
1206 "O" /* `pt` */
1207 "O" /* `quad_p1` */
1208 "O" /* `quad_p2` */
1209 "O" /* `quad_p3` */
1210 "O" /* `quad_p4` */
1211 ":intersect_point_quad_2d",
1212 &py_pt,
1213 UNPACK4_EX(&, py_quad, )))
1214 {
1215 return nullptr;
1216 }
1217
1218 if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
1219 return nullptr;
1220 }
1221 for (i = 0; i < ARRAY_SIZE(quad); i++) {
1222 if (mathutils_array_parse(quad[i], 2, 2 | MU_ARRAY_SPILL, py_quad[i], error_prefix) == -1) {
1223 return nullptr;
1224 }
1225 }
1226
1227 return PyLong_FromLong(isect_point_quad_v2(pt, UNPACK4(quad)));
1228}
1229
1231 /* Wrap. */
1232 M_Geometry_distance_point_to_plane_doc,
1233 ".. function:: distance_point_to_plane(pt, plane_co, plane_no, /)\n"
1234 "\n"
1235 " Returns the signed distance between a point and a plane "
1236 "(negative when below the normal).\n"
1237 "\n"
1238 " :param pt: Point\n"
1239 " :type pt: :class:`mathutils.Vector`\n"
1240 " :param plane_co: A point on the plane\n"
1241 " :type plane_co: :class:`mathutils.Vector`\n"
1242 " :param plane_no: The direction the plane is facing\n"
1243 " :type plane_no: :class:`mathutils.Vector`\n"
1244 " :return: The signed distance.\n"
1245 " :rtype: float\n");
1246static PyObject *M_Geometry_distance_point_to_plane(PyObject * /*self*/, PyObject *args)
1247{
1248 const char *error_prefix = "distance_point_to_plane";
1249 PyObject *py_pt, *py_plane_co, *py_plane_no;
1250 float pt[3], plane_co[3], plane_no[3];
1251 float plane[4];
1252
1253 if (!PyArg_ParseTuple(args,
1254 "O" /* `pt` */
1255 "O" /* `plane_co` */
1256 "O" /* `plane_no` */
1257 ":distance_point_to_plane",
1258 &py_pt,
1259 &py_plane_co,
1260 &py_plane_no))
1261 {
1262 return nullptr;
1263 }
1264
1265 if (((mathutils_array_parse(pt, 3, 3 | MU_ARRAY_SPILL, py_pt, error_prefix) != -1) &&
1266 (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
1267 (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) !=
1268 -1)) == 0)
1269 {
1270 return nullptr;
1271 }
1272
1273 plane_from_point_normal_v3(plane, plane_co, plane_no);
1274 return PyFloat_FromDouble(dist_signed_to_plane_v3(pt, plane));
1275}
1276
1278 /* Wrap. */
1279 M_Geometry_barycentric_transform_doc,
1280 ".. function:: barycentric_transform(point, tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3, "
1281 "/)\n"
1282 "\n"
1283 " Return a transformed point, the transformation is defined by 2 triangles.\n"
1284 "\n"
1285 " :param point: The point to transform.\n"
1286 " :type point: :class:`mathutils.Vector`\n"
1287 " :param tri_a1: source triangle vertex.\n"
1288 " :type tri_a1: :class:`mathutils.Vector`\n"
1289 " :param tri_a2: source triangle vertex.\n"
1290 " :type tri_a2: :class:`mathutils.Vector`\n"
1291 " :param tri_a3: source triangle vertex.\n"
1292 " :type tri_a3: :class:`mathutils.Vector`\n"
1293 " :param tri_b1: target triangle vertex.\n"
1294 " :type tri_b1: :class:`mathutils.Vector`\n"
1295 " :param tri_b2: target triangle vertex.\n"
1296 " :type tri_b2: :class:`mathutils.Vector`\n"
1297 " :param tri_b3: target triangle vertex.\n"
1298 " :type tri_b3: :class:`mathutils.Vector`\n"
1299 " :return: The transformed point\n"
1300 " :rtype: :class:`mathutils.Vector`\n");
1301static PyObject *M_Geometry_barycentric_transform(PyObject * /*self*/, PyObject *args)
1302{
1303 const char *error_prefix = "barycentric_transform";
1304 PyObject *py_pt_src, *py_tri_src[3], *py_tri_dst[3];
1305 float pt_src[3], pt_dst[3], tri_src[3][3], tri_dst[3][3];
1306 int i;
1307
1308 if (!PyArg_ParseTuple(args,
1309 "O" /* `point` */
1310 "O" /* `tri_a1` */
1311 "O" /* `tri_a2` */
1312 "O" /* `tri_a3` */
1313 "O" /* `tri_b1` */
1314 "O" /* `tri_b2` */
1315 "O" /* `tri_b3` */
1316 ":barycentric_transform",
1317 &py_pt_src,
1318 UNPACK3_EX(&, py_tri_src, ),
1319 UNPACK3_EX(&, py_tri_dst, )))
1320 {
1321 return nullptr;
1322 }
1323
1324 if (mathutils_array_parse(pt_src, 3, 3 | MU_ARRAY_SPILL, py_pt_src, error_prefix) == -1) {
1325 return nullptr;
1326 }
1327 for (i = 0; i < ARRAY_SIZE(tri_src); i++) {
1328 if (((mathutils_array_parse(tri_src[i], 3, 3 | MU_ARRAY_SPILL, py_tri_src[i], error_prefix) !=
1329 -1) &&
1330 (mathutils_array_parse(tri_dst[i], 3, 3 | MU_ARRAY_SPILL, py_tri_dst[i], error_prefix) !=
1331 -1)) == 0)
1332 {
1333 return nullptr;
1334 }
1335 }
1336
1337 transform_point_by_tri_v3(pt_dst, pt_src, UNPACK3(tri_dst), UNPACK3(tri_src));
1338
1339 return Vector_CreatePyObject(pt_dst, 3, nullptr);
1340}
1341
1346
1347static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p)
1348{
1349 PointsInPlanes_UserData *user_data = static_cast<PointsInPlanes_UserData *>(user_data_p);
1350 PyList_APPEND(user_data->py_verts, Vector_CreatePyObject(co, 3, nullptr));
1351 user_data->planes_used[i] = true;
1352 user_data->planes_used[j] = true;
1353 user_data->planes_used[k] = true;
1354}
1355
1357 /* Wrap. */
1358 M_Geometry_points_in_planes_doc,
1359 ".. function:: points_in_planes(planes, epsilon_coplanar=1e-4, epsilon_isect=1e-6, /)\n"
1360 "\n"
1361 " Returns a list of points inside all planes given and a list of index values for "
1362 "the planes used.\n"
1363 "\n"
1364 " :param planes: List of planes (4D vectors).\n"
1365 " :type planes: list[:class:`mathutils.Vector`]\n"
1366 " :param epsilon_coplanar: Epsilon value for interpreting plane pairs as co-planar.\n"
1367 " :type epsilon_coplanar: float\n"
1368 " :param epsilon_isect: Epsilon value for intersection.\n"
1369 " :type epsilon_isect: float\n"
1370 " :return: Two lists, one containing the 3D coordinates inside the planes, "
1371 "another containing the plane indices used.\n"
1372 " :rtype: tuple[list[:class:`mathutils.Vector`], list[int]]\n");
1373static PyObject *M_Geometry_points_in_planes(PyObject * /*self*/, PyObject *args)
1374{
1375 PyObject *py_planes;
1376 float (*planes)[4];
1377 float eps_coplanar = 1e-4f;
1378 float eps_isect = 1e-6f;
1379 uint planes_len;
1380
1381 if (!PyArg_ParseTuple(args,
1382 "O" /* `planes` */
1383 "|" /* Optional arguments. */
1384 "f" /* `epsilon_coplanar` */
1385 "f" /* `epsilon_isect` */
1386 ":points_in_planes",
1387 &py_planes,
1388 &eps_coplanar,
1389 &eps_isect))
1390 {
1391 return nullptr;
1392 }
1393
1394 if ((planes_len = mathutils_array_parse_alloc_v(
1395 reinterpret_cast<float **>(&planes), 4, py_planes, "points_in_planes")) == -1)
1396 {
1397 return nullptr;
1398 }
1399
1400 /* NOTE: this could be refactored into plain C easy - py bits are noted. */
1401
1402 PointsInPlanes_UserData user_data{};
1403 user_data.py_verts = PyList_New(0);
1404 user_data.planes_used = static_cast<char *>(PyMem_Malloc(sizeof(char) * planes_len));
1405
1406 /* python */
1407 PyObject *py_plane_index = PyList_New(0);
1408
1409 memset(user_data.planes_used, 0, sizeof(char) * planes_len);
1410
1411 const bool has_isect = isect_planes_v3_fn(
1412 planes, planes_len, eps_coplanar, eps_isect, points_in_planes_fn, &user_data);
1413 PyMem_Free(planes);
1414
1415 /* Now make user_data list of used planes. */
1416 if (has_isect) {
1417 for (int i = 0; i < planes_len; i++) {
1418 if (user_data.planes_used[i]) {
1419 PyList_APPEND(py_plane_index, PyLong_FromLong(i));
1420 }
1421 }
1422 }
1423 PyMem_Free(user_data.planes_used);
1424
1425 {
1426 PyObject *ret = PyTuple_New(2);
1427 PyTuple_SET_ITEMS(ret, user_data.py_verts, py_plane_index);
1428 return ret;
1429 }
1430}
1431
1432#ifndef MATH_STANDALONE
1433
1435 /* Wrap. */
1436 M_Geometry_interpolate_bezier_doc,
1437 ".. function:: interpolate_bezier(knot1, handle1, handle2, knot2, resolution, /)\n"
1438 "\n"
1439 " Interpolate a bezier spline segment.\n"
1440 "\n"
1441 " :param knot1: First bezier spline point.\n"
1442 " :type knot1: :class:`mathutils.Vector`\n"
1443 " :param handle1: First bezier spline handle.\n"
1444 " :type handle1: :class:`mathutils.Vector`\n"
1445 " :param handle2: Second bezier spline handle.\n"
1446 " :type handle2: :class:`mathutils.Vector`\n"
1447 " :param knot2: Second bezier spline point.\n"
1448 " :type knot2: :class:`mathutils.Vector`\n"
1449 " :param resolution: Number of points to return.\n"
1450 " :type resolution: int\n"
1451 " :return: The interpolated points.\n"
1452 " :rtype: list[:class:`mathutils.Vector`]\n");
1453static PyObject *M_Geometry_interpolate_bezier(PyObject * /*self*/, PyObject *args)
1454{
1455 const char *error_prefix = "interpolate_bezier";
1456 PyObject *py_data[4];
1457 float data[4][4] = {{0.0f}};
1458 int resolu;
1459 int dims = 0;
1460 int i;
1461 float *coord_array, *fp;
1462 PyObject *list;
1463
1464 if (!PyArg_ParseTuple(args,
1465 "O" /* `knot1` */
1466 "O" /* `handle1` */
1467 "O" /* `handle2` */
1468 "O" /* `knot2` */
1469 "i" /* `resolution` */
1470 ":interpolate_bezier",
1471 UNPACK4_EX(&, py_data, ),
1472 &resolu))
1473 {
1474 return nullptr;
1475 }
1476
1477 for (i = 0; i < 4; i++) {
1478 int dims_tmp;
1479 if ((dims_tmp = mathutils_array_parse(
1480 data[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_data[i], error_prefix)) == -1)
1481 {
1482 return nullptr;
1483 }
1484 dims = max_ii(dims, dims_tmp);
1485 }
1486
1487 if (resolu <= 1) {
1488 PyErr_SetString(PyExc_ValueError, "resolution must be 2 or over");
1489 return nullptr;
1490 }
1491
1492 coord_array = MEM_new_array_zeroed<float>(size_t(dims) * size_t(resolu), error_prefix);
1493 for (i = 0; i < dims; i++) {
1495 UNPACK4_EX(, data, [i]), coord_array + i, resolu - 1, sizeof(float) * dims);
1496 }
1497
1498 list = PyList_New(resolu);
1499 fp = coord_array;
1500 for (i = 0; i < resolu; i++, fp = fp + dims) {
1501 PyList_SET_ITEM(list, i, Vector_CreatePyObject(fp, dims, nullptr));
1502 }
1503 MEM_delete(coord_array);
1504 return list;
1505}
1506
1508 /* Wrap. */
1509 M_Geometry_tessellate_polygon_doc,
1510 ".. function:: tessellate_polygon(polylines, /)\n"
1511 "\n"
1512 " Takes a list of polylines (each point a pair or triplet of numbers) and returns "
1513 "the point indices for a polyline filled with triangles. Does not handle degenerate "
1514 "geometry (such as zero-length lines due to consecutive identical points).\n"
1515 "\n"
1516 " :param polylines: Polygons where each polygon is a sequence of 2D or 3D points.\n"
1517 " :type polylines: Sequence[Sequence[Sequence[float]]]\n"
1518 " :return: A list of triangles.\n"
1519 " :rtype: list[tuple[int, int, int]]\n");
1520/* PolyFill function, uses Blenders scan-fill to fill multiple poly lines. */
1521static PyObject *M_Geometry_tessellate_polygon(PyObject * /*self*/, PyObject *polyLineSeq)
1522{
1523 PyObject *tri_list; /* Return this list of triangles. */
1524 PyObject *polyLine, *polyVec;
1525 int i, len_polylines, len_polypoints;
1526 bool list_parse_error = false;
1527 bool is_2d = true;
1528
1529 /* Display #ListBase. */
1530 ListBaseT<DispList> dispbase = {nullptr, nullptr};
1531 DispList *dl;
1532 float *fp; /* Pointer to the array of malloced dl->verts to set the points from the vectors. */
1533 int totpoints = 0;
1534
1535 if (!PySequence_Check(polyLineSeq)) {
1536 PyErr_SetString(PyExc_TypeError, "expected a sequence of poly lines");
1537 return nullptr;
1538 }
1539
1540 len_polylines = PySequence_Size(polyLineSeq);
1541
1542 for (i = 0; i < len_polylines; i++) {
1543 polyLine = PySequence_GetItem(polyLineSeq, i);
1544 if (!PySequence_Check(polyLine)) {
1545 BKE_displist_free(&dispbase);
1546 Py_XDECREF(polyLine); /* May be null so use #Py_XDECREF. */
1547 PyErr_SetString(PyExc_TypeError,
1548 "One or more of the polylines is not a sequence of mathutils.Vector's");
1549 return nullptr;
1550 }
1551
1552 len_polypoints = PySequence_Size(polyLine);
1553 if (len_polypoints > 0) { /* don't bother adding edges as polylines */
1554 dl = MEM_new_zeroed<DispList>("poly disp");
1555 BLI_addtail(&dispbase, dl);
1556 dl->nr = len_polypoints;
1557 dl->type = DL_POLY;
1558 dl->parts = 1; /* no faces, 1 edge loop */
1559 dl->col = 0; /* no material */
1560 dl->verts = fp = MEM_new_array_uninitialized<float>(3 * size_t(len_polypoints), "dl verts");
1561 dl->index = MEM_new_array_zeroed<int>(3 * size_t(len_polypoints), "dl index");
1562
1563 for (int index = 0; index < len_polypoints; index++, fp += 3) {
1564 polyVec = PySequence_GetItem(polyLine, index);
1565 const int polyVec_len = mathutils_array_parse(
1566 fp, 2, 3 | MU_ARRAY_SPILL, polyVec, "tessellate_polygon: parse coord");
1567 Py_DECREF(polyVec);
1568
1569 if (polyVec_len == -1) [[unlikely]] {
1570 list_parse_error = true;
1571 }
1572 else if (polyVec_len == 2) {
1573 fp[2] = 0.0f;
1574 }
1575 else if (polyVec_len == 3) {
1576 is_2d = false;
1577 }
1578
1579 totpoints++;
1580 }
1581 }
1582 Py_DECREF(polyLine);
1583 }
1584
1585 if (list_parse_error) {
1586 BKE_displist_free(&dispbase); /* possible some dl was allocated */
1587 return nullptr;
1588 }
1589 if (totpoints) {
1590 /* now make the list to return */
1591 float down_vec[3] = {0, 0, -1};
1592 BKE_displist_fill(&dispbase,
1593 &dispbase,
1594 is_2d ? down_vec : nullptr,
1595 false,
1598
1599 /* The faces are stored in a new DisplayList
1600 * that's added to the head of the #ListBase. */
1601 dl = dispbase.first();
1602
1603 tri_list = PyList_New(dl->parts);
1604 if (!tri_list) {
1605 BKE_displist_free(&dispbase);
1606 PyErr_SetString(PyExc_RuntimeError, "failed to make a new list");
1607 return nullptr;
1608 }
1609
1610 int *dl_face = dl->index;
1611 for (int index = 0; index < dl->parts; index++) {
1612 PyList_SET_ITEM(tri_list, index, PyC_Tuple_Pack_I32({dl_face[0], dl_face[1], dl_face[2]}));
1613 dl_face += 3;
1614 }
1615 BKE_displist_free(&dispbase);
1616 }
1617 else {
1618 /* no points, do this so scripts don't barf */
1619 BKE_displist_free(&dispbase); /* possible some dl was allocated */
1620 tri_list = PyList_New(0);
1621 }
1622
1623 return tri_list;
1624}
1625
1626static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray)
1627{
1628 Py_ssize_t len, i;
1629 PyObject *list_item, *item_1, *item_2;
1630 BoxPack *boxarray;
1631
1632 /* Error checking must already be done */
1633 if (!PyList_Check(value)) {
1634 PyErr_SetString(PyExc_TypeError, "can only back a list of [x, y, w, h]");
1635 return -1;
1636 }
1637
1638 len = PyList_GET_SIZE(value);
1639
1640 boxarray = MEM_new_array_uninitialized<BoxPack>(size_t(len), __func__);
1641
1642 for (i = 0; i < len; i++) {
1643 list_item = PyList_GET_ITEM(value, i);
1644 if (!PyList_Check(list_item) || PyList_GET_SIZE(list_item) < 4) {
1645 MEM_delete(boxarray);
1646 PyErr_SetString(PyExc_TypeError, "can only pack a list of [x, y, w, h]");
1647 return -1;
1648 }
1649
1650 BoxPack *box = &boxarray[i];
1651
1652 item_1 = PyList_GET_ITEM(list_item, 2);
1653 item_2 = PyList_GET_ITEM(list_item, 3);
1654
1655 box->w = float(PyFloat_AsDouble(item_1));
1656 box->h = float(PyFloat_AsDouble(item_2));
1657 box->index = i;
1658
1659 /* accounts for error case too and overwrites with own error */
1660 if (box->w < 0.0f || box->h < 0.0f) {
1661 MEM_delete(boxarray);
1662 PyErr_SetString(PyExc_TypeError,
1663 "error parsing width and height values from list: "
1664 "[x, y, w, h], not numbers or below zero");
1665 return -1;
1666 }
1667
1668 /* verts will be added later */
1669 }
1670
1671 *r_boxarray = boxarray;
1672 return 0;
1673}
1674
1675static void boxPack_ToPyObject(PyObject *value, const BoxPack *boxarray)
1676{
1677 Py_ssize_t len, i;
1678 PyObject *list_item;
1679
1680 len = PyList_GET_SIZE(value);
1681
1682 for (i = 0; i < len; i++) {
1683 const BoxPack *box = &boxarray[i];
1684 list_item = PyList_GET_ITEM(value, box->index);
1685 PyList_SetItem(list_item, 0, PyFloat_FromDouble(box->x));
1686 PyList_SetItem(list_item, 1, PyFloat_FromDouble(box->y));
1687 }
1688}
1689
1691 /* Wrap. */
1692 M_Geometry_box_pack_2d_doc,
1693 ".. function:: box_pack_2d(boxes, /)\n"
1694 "\n"
1695 " Returns a tuple with the width and height of the packed bounding box.\n"
1696 "\n"
1697 " :param boxes: list of boxes, each box is a list where the first 4 items are "
1698 "[X, Y, width, height, ...] other items are ignored. "
1699 "The X & Y values in this list are modified to set the packed positions.\n"
1700 " :type boxes: list[list[float]]\n"
1701 " :return: The width and height of the packed bounding box.\n"
1702 " :rtype: tuple[float, float]\n");
1703static PyObject *M_Geometry_box_pack_2d(PyObject * /*self*/, PyObject *boxlist)
1704{
1705 float tot_width = 0.0f, tot_height = 0.0f;
1706 Py_ssize_t len;
1707
1708 PyObject *ret;
1709
1710 if (!PyList_Check(boxlist)) {
1711 PyErr_SetString(PyExc_TypeError, "expected a list of boxes [[x, y, w, h], ... ]");
1712 return nullptr;
1713 }
1714
1715 len = PyList_GET_SIZE(boxlist);
1716 if (len) {
1717 BoxPack *boxarray = nullptr;
1718 if (boxPack_FromPyObject(boxlist, &boxarray) == -1) {
1719 return nullptr; /* exception set */
1720 }
1721
1722 const bool sort_boxes = true; /* Caution: BLI_box_pack_2d sorting is non-deterministic. */
1723 /* Non Python function */
1724 BLI_box_pack_2d(boxarray, len, sort_boxes, &tot_width, &tot_height);
1725
1726 boxPack_ToPyObject(boxlist, boxarray);
1727 MEM_delete(boxarray);
1728 }
1729
1730 ret = PyTuple_New(2);
1731 PyTuple_SET_ITEMS(ret, PyFloat_FromDouble(tot_width), PyFloat_FromDouble(tot_height));
1732 return ret;
1733}
1734
1736 /* Wrap. */
1737 M_Geometry_box_fit_2d_doc,
1738 ".. function:: box_fit_2d(points, /)\n"
1739 "\n"
1740 " Returns an angle that best fits the points to an axis aligned rectangle\n"
1741 "\n"
1742 " :param points: Sequence of 2D points.\n"
1743 " :type points: Sequence[Sequence[float]]\n"
1744 " :return: The rotation angle in radians for the best axis-aligned bounding box fit.\n"
1745 " :rtype: float\n");
1746static PyObject *M_Geometry_box_fit_2d(PyObject * /*self*/, PyObject *pointlist)
1747{
1748 float (*points)[2];
1749 Py_ssize_t len;
1750
1751 float angle = 0.0f;
1752
1754 (reinterpret_cast<float **>(&points)), 2, pointlist, "box_fit_2d");
1755 if (len == -1) {
1756 return nullptr;
1757 }
1758
1759 if (len) {
1760 /* Non Python function */
1761 angle = BLI_convexhull_aabb_fit_points_2d({reinterpret_cast<float2 *>(points), len});
1762
1763 PyMem_Free(points);
1764 }
1765
1766 return PyFloat_FromDouble(angle);
1767}
1768
1770 /* Wrap. */
1771 M_Geometry_convex_hull_2d_doc,
1772 ".. function:: convex_hull_2d(points, /)\n"
1773 "\n"
1774 " Returns the indices of the points forming the convex hull, in counter-clockwise order.\n"
1775 "\n"
1776 " :param points: Sequence of 2D points.\n"
1777 " :type points: Sequence[Sequence[float]]\n"
1778 " :return: Indices of convex hull vertices in counter-clockwise order.\n"
1779 " :rtype: list[int]\n");
1780static PyObject *M_Geometry_convex_hull_2d(PyObject * /*self*/, PyObject *pointlist)
1781{
1782 float (*points)[2];
1783 Py_ssize_t len;
1784
1785 PyObject *ret;
1786
1788 (reinterpret_cast<float **>(&points)), 2, pointlist, "convex_hull_2d");
1789 if (len == -1) {
1790 return nullptr;
1791 }
1792
1793 if (len) {
1794 int *index_map;
1795 Py_ssize_t len_ret, i;
1796
1797 index_map = MEM_new_array_uninitialized<int>(size_t(len), __func__);
1798
1799 /* Non Python function */
1800 len_ret = BLI_convexhull_2d({reinterpret_cast<float2 *>(points), len}, index_map);
1801
1802 ret = PyList_New(len_ret);
1803 for (i = 0; i < len_ret; i++) {
1804 PyList_SET_ITEM(ret, i, PyLong_FromLong(index_map[i]));
1805 }
1806
1807 MEM_delete(index_map);
1808
1809 PyMem_Free(points);
1810 }
1811 else {
1812 ret = PyList_New(0);
1813 }
1814
1815 return ret;
1816}
1817
1818/* Return a PyObject that is a list of lists, using the flattened list array
1819 * to fill values, with start_table and len_table giving the start index
1820 * and length of the toplevel_len sub-lists.
1821 */
1822template<typename T> static PyObject *list_of_lists_from_arrays(const Span<Vector<T>> data)
1823{
1824 if (data.is_empty()) {
1825 return PyList_New(0);
1826 }
1827 PyObject *ret = PyList_New(data.size());
1828 for (const int i : data.index_range()) {
1829 const Span<T> group = data[i];
1830 PyObject *sublist = PyList_New(group.size());
1831 for (const int j : group.index_range()) {
1832 PyList_SET_ITEM(sublist, j, PyLong_FromUnsignedLong(group[j]));
1833 }
1834 PyList_SET_ITEM(ret, i, sublist);
1835 }
1836 return ret;
1837}
1838
1840 /* Wrap. */
1841 M_Geometry_delaunay_2d_cdt_doc,
1842 ".. function:: delaunay_2d_cdt(vert_coords, edges, faces, output_type, epsilon, "
1843 "need_ids=True, /)\n"
1844 "\n"
1845 " Computes the Constrained Delaunay Triangulation of a set of vertices,\n"
1846 " with edges and faces that must appear in the triangulation.\n"
1847 " Some triangles may be eaten away, or combined with other triangles,\n"
1848 " according to output type.\n"
1849 " The returned verts may be in a different order from input verts, may be moved\n"
1850 " slightly, and may be merged with other nearby verts.\n"
1851 " The three returned orig lists give, for each of verts, edges, and faces, the list of\n"
1852 " input element indices corresponding to the positionally same output element.\n"
1853 " For edges, the orig indices start with the input edges and then continue\n"
1854 " with the edges implied by each of the faces (n of them for an n-gon).\n"
1855 " If the need_ids argument is supplied, and False, then the code skips the preparation\n"
1856 " of the orig arrays, which may save some time.\n"
1857 "\n"
1858 " :param vert_coords: Vertex coordinates (2d)\n"
1859 " :type vert_coords: Sequence[:class:`mathutils.Vector`]\n"
1860 " :param edges: Edges, as pairs of indices in ``vert_coords``\n"
1861 " :type edges: Sequence[tuple[int, int]]\n"
1862 " :param faces: Faces, each sublist is a face, "
1863 "as indices in ``vert_coords`` (CCW oriented).\n"
1864 " :type faces: Sequence[Sequence[int]]\n"
1865 " :param output_type: What output looks like. 0 => triangles with convex hull. "
1866 "1 => triangles inside constraints. "
1867 "2 => the input constraints, intersected. "
1868 "3 => like 2 but detect holes and omit them from output. "
1869 "4 => like 2 but with extra edges to make valid BMesh faces. "
1870 "5 => like 4 but detect holes and omit them from output.\n"
1871 " :type output_type: int\n"
1872 " :param epsilon: For nearness tests; should not be zero\n"
1873 " :type epsilon: float\n"
1874 " :param need_ids: are the orig output arrays needed?\n"
1875 " :type need_ids: bool\n"
1876 " :return: Output tuple, (vert_coords, edges, faces, orig_verts, orig_edges, orig_faces)\n"
1877 " :rtype: tuple["
1878 "list[:class:`mathutils.Vector`], "
1879 "list[tuple[int, int]], "
1880 "list[list[int]], "
1881 "list[list[int]], "
1882 "list[list[int]], "
1883 "list[list[int]]]\n");
1884static PyObject *M_Geometry_delaunay_2d_cdt(PyObject * /*self*/, PyObject *args)
1885{
1886 const char *error_prefix = "delaunay_2d_cdt";
1887 PyObject *vert_coords, *edges, *faces;
1888 int output_type;
1889 float epsilon;
1890 bool need_ids = true;
1891 float (*in_coords)[2] = nullptr;
1892 int (*in_edges)[2] = nullptr;
1893 Py_ssize_t vert_coords_len, edges_len;
1894 PyObject *out_vert_coords = nullptr;
1895 PyObject *out_edges = nullptr;
1896 PyObject *out_faces = nullptr;
1897 PyObject *out_orig_verts = nullptr;
1898 PyObject *out_orig_edges = nullptr;
1899 PyObject *out_orig_faces = nullptr;
1900 PyObject *ret_value = nullptr;
1901
1902 if (!PyArg_ParseTuple(args,
1903 "O" /* `vert_coords` */
1904 "O" /* `edges` */
1905 "O" /* `faces` */
1906 "i" /* `output_type` */
1907 "f" /* `epsilon` */
1908 "|" /* Optional arguments. */
1909 "O&" /* `need_ids` */
1910 ":delaunay_2d_cdt",
1911 &vert_coords,
1912 &edges,
1913 &faces,
1914 &output_type,
1915 &epsilon,
1917 &need_ids))
1918 {
1919 return nullptr;
1920 }
1921
1922 BLI_SCOPED_DEFER([&]() {
1923 if (in_coords != nullptr) {
1924 PyMem_Free(in_coords);
1925 }
1926 if (in_edges != nullptr) {
1927 PyMem_Free(in_edges);
1928 }
1929 });
1930
1931 vert_coords_len = mathutils_array_parse_alloc_v(
1932 reinterpret_cast<float **>(&in_coords), 2, vert_coords, error_prefix);
1933 if (vert_coords_len == -1) {
1934 return nullptr;
1935 }
1936
1938 reinterpret_cast<int **>(&in_edges), 2, edges, error_prefix);
1939 if (edges_len == -1) {
1940 return nullptr;
1941 }
1942
1943 Array<int> face_offsets;
1944 Array<int> face_vert_indices;
1945 if (!mathutils_array_parse_alloc_viseq(faces, error_prefix, face_offsets, face_vert_indices)) {
1946 return nullptr;
1947 }
1948
1949 Array<double2> verts(vert_coords_len);
1950 for (const int i : verts.index_range()) {
1951 verts[i] = {double(in_coords[i][0]), double(in_coords[i][1])};
1952 }
1953
1955 in.vert = verts;
1956 in.edge = Span(reinterpret_cast<int2 *>(in_edges), edges_len);
1957 in.face_offsets = face_offsets.as_span();
1958 in.face_vert_indices = face_vert_indices;
1959 in.epsilon = epsilon;
1960 in.needed_ids = need_ids ? (CDT_ORIG_VERTS | CDT_ORIG_EDGES | CDT_ORIG_FACES) : CDT_NO_ORIG_IDS;
1961
1963 in, CDT_output_type(output_type));
1964
1965 ret_value = PyTuple_New(6);
1966
1967 out_vert_coords = PyList_New(res.vert.size());
1968 for (const int i : res.vert.index_range()) {
1969 const float2 vert_float(res.vert[i]);
1970 PyObject *item = Vector_CreatePyObject(vert_float, 2, nullptr);
1971 if (item == nullptr) {
1972 Py_DECREF(ret_value);
1973 Py_DECREF(out_vert_coords);
1974 return nullptr;
1975 }
1976 PyList_SET_ITEM(out_vert_coords, i, item);
1977 }
1978 PyTuple_SET_ITEM(ret_value, 0, out_vert_coords);
1979
1980 out_edges = PyList_New(res.edge.size());
1981 for (const int i : res.edge.index_range()) {
1982 PyObject *item = PyTuple_New(2);
1983 PyTuple_SET_ITEM(item, 0, PyLong_FromLong(long(res.edge[i][0])));
1984 PyTuple_SET_ITEM(item, 1, PyLong_FromLong(long(res.edge[i][1])));
1985 PyList_SET_ITEM(out_edges, i, item);
1986 }
1987 PyTuple_SET_ITEM(ret_value, 1, out_edges);
1988
1989 out_faces = list_of_lists_from_arrays(res.face.as_span());
1990 PyTuple_SET_ITEM(ret_value, 2, out_faces);
1991
1992 out_orig_verts = list_of_lists_from_arrays(res.vert_orig.as_span());
1993 PyTuple_SET_ITEM(ret_value, 3, out_orig_verts);
1994
1995 out_orig_edges = list_of_lists_from_arrays(res.edge_orig.as_span());
1996 PyTuple_SET_ITEM(ret_value, 4, out_orig_edges);
1997
1998 out_orig_faces = list_of_lists_from_arrays(res.face_orig.as_span());
1999 PyTuple_SET_ITEM(ret_value, 5, out_orig_faces);
2000
2001 return ret_value;
2002}
2003
2004#endif /* MATH_STANDALONE */
2005
2006#ifdef __GNUC__
2007# ifdef __clang__
2008# pragma clang diagnostic push
2009# pragma clang diagnostic ignored "-Wcast-function-type"
2010# else
2011# pragma GCC diagnostic push
2012# pragma GCC diagnostic ignored "-Wcast-function-type"
2013# endif
2014#endif
2015
2016static PyMethodDef M_Geometry_methods[] = {
2017 {"intersect_ray_tri",
2018 static_cast<PyCFunction>(M_Geometry_intersect_ray_tri),
2019 METH_VARARGS,
2020 M_Geometry_intersect_ray_tri_doc},
2021 {"intersect_point_line",
2022 reinterpret_cast<PyCFunction>(M_Geometry_intersect_point_line),
2023 METH_FASTCALL,
2024 M_Geometry_intersect_point_line_doc},
2025 {"intersect_point_line_segment",
2026 reinterpret_cast<PyCFunction>(M_Geometry_intersect_point_line_segment),
2027 METH_FASTCALL,
2028 M_Geometry_intersect_point_line_segment_doc},
2029 {"intersect_point_tri",
2030 static_cast<PyCFunction>(M_Geometry_intersect_point_tri),
2031 METH_VARARGS,
2032 M_Geometry_intersect_point_tri_doc},
2033 {"closest_point_on_tri",
2034 static_cast<PyCFunction>(M_Geometry_closest_point_on_tri),
2035 METH_VARARGS,
2036 M_Geometry_closest_point_on_tri_doc},
2037 {"intersect_point_tri_2d",
2038 static_cast<PyCFunction>(M_Geometry_intersect_point_tri_2d),
2039 METH_VARARGS,
2040 M_Geometry_intersect_point_tri_2d_doc},
2041 {"intersect_point_quad_2d",
2042 static_cast<PyCFunction>(M_Geometry_intersect_point_quad_2d),
2043 METH_VARARGS,
2044 M_Geometry_intersect_point_quad_2d_doc},
2045 {"intersect_line_line",
2046 static_cast<PyCFunction>(M_Geometry_intersect_line_line),
2047 METH_VARARGS,
2048 M_Geometry_intersect_line_line_doc},
2049 {"intersect_line_line_2d",
2050 static_cast<PyCFunction>(M_Geometry_intersect_line_line_2d),
2051 METH_VARARGS,
2052 M_Geometry_intersect_line_line_2d_doc},
2053 {"intersect_line_plane",
2054 static_cast<PyCFunction>(M_Geometry_intersect_line_plane),
2055 METH_VARARGS,
2056 M_Geometry_intersect_line_plane_doc},
2057 {"intersect_plane_plane",
2058 static_cast<PyCFunction>(M_Geometry_intersect_plane_plane),
2059 METH_VARARGS,
2060 M_Geometry_intersect_plane_plane_doc},
2061 {"intersect_line_sphere",
2062 static_cast<PyCFunction>(M_Geometry_intersect_line_sphere),
2063 METH_VARARGS,
2064 M_Geometry_intersect_line_sphere_doc},
2065 {"intersect_line_sphere_2d",
2066 static_cast<PyCFunction>(M_Geometry_intersect_line_sphere_2d),
2067 METH_VARARGS,
2068 M_Geometry_intersect_line_sphere_2d_doc},
2069 {"distance_point_to_plane",
2070 static_cast<PyCFunction>(M_Geometry_distance_point_to_plane),
2071 METH_VARARGS,
2072 M_Geometry_distance_point_to_plane_doc},
2073 {"intersect_sphere_sphere_2d",
2074 static_cast<PyCFunction>(M_Geometry_intersect_sphere_sphere_2d),
2075 METH_VARARGS,
2076 M_Geometry_intersect_sphere_sphere_2d_doc},
2077 {"intersect_tri_tri_2d",
2078 static_cast<PyCFunction>(M_Geometry_intersect_tri_tri_2d),
2079 METH_VARARGS,
2080 M_Geometry_intersect_tri_tri_2d_doc},
2081 {"area_tri",
2082 static_cast<PyCFunction>(M_Geometry_area_tri),
2083 METH_VARARGS,
2084 M_Geometry_area_tri_doc},
2085 {"volume_tetrahedron",
2086 static_cast<PyCFunction>(M_Geometry_volume_tetrahedron),
2087 METH_VARARGS,
2088 M_Geometry_volume_tetrahedron_doc},
2089 {"normal", static_cast<PyCFunction>(M_Geometry_normal), METH_VARARGS, M_Geometry_normal_doc},
2090 {"barycentric_transform",
2091 static_cast<PyCFunction>(M_Geometry_barycentric_transform),
2092 METH_VARARGS,
2093 M_Geometry_barycentric_transform_doc},
2094 {"points_in_planes",
2095 static_cast<PyCFunction>(M_Geometry_points_in_planes),
2096 METH_VARARGS,
2097 M_Geometry_points_in_planes_doc},
2098#ifndef MATH_STANDALONE
2099 {"interpolate_bezier",
2100 static_cast<PyCFunction>(M_Geometry_interpolate_bezier),
2101 METH_VARARGS,
2102 M_Geometry_interpolate_bezier_doc},
2103 {"tessellate_polygon",
2104 static_cast<PyCFunction>(M_Geometry_tessellate_polygon),
2105 METH_O,
2106 M_Geometry_tessellate_polygon_doc},
2107 {"convex_hull_2d",
2108 static_cast<PyCFunction>(M_Geometry_convex_hull_2d),
2109 METH_O,
2110 M_Geometry_convex_hull_2d_doc},
2111 {"delaunay_2d_cdt",
2112 static_cast<PyCFunction>(M_Geometry_delaunay_2d_cdt),
2113 METH_VARARGS,
2114 M_Geometry_delaunay_2d_cdt_doc},
2115 {"box_fit_2d",
2116 static_cast<PyCFunction>(M_Geometry_box_fit_2d),
2117 METH_O,
2118 M_Geometry_box_fit_2d_doc},
2119 {"box_pack_2d",
2120 static_cast<PyCFunction>(M_Geometry_box_pack_2d),
2121 METH_O,
2122 M_Geometry_box_pack_2d_doc},
2123#endif
2124 {nullptr, nullptr, 0, nullptr},
2125};
2126
2127#ifdef __GNUC__
2128# ifdef __clang__
2129# pragma clang diagnostic pop
2130# else
2131# pragma GCC diagnostic pop
2132# endif
2133#endif
2134
2136 /* Wrap. */
2137 M_Geometry_doc,
2138 "The Blender geometry module.");
2139static PyModuleDef M_Geometry_module_def = {
2140 /*m_base*/ PyModuleDef_HEAD_INIT,
2141 /*m_name*/ "mathutils.geometry",
2142 /*m_doc*/ M_Geometry_doc,
2143 /*m_size*/ 0,
2144 /*m_methods*/ M_Geometry_methods,
2145 /*m_slots*/ nullptr,
2146 /*m_traverse*/ nullptr,
2147 /*m_clear*/ nullptr,
2148 /*m_free*/ nullptr,
2149};
2150
2151/*----------------------------MODULE INIT-------------------------*/
2152
2154{
2155 PyObject *submodule = PyModule_Create(&M_Geometry_module_def);
2156 return submodule;
2157}
2158
2159} // namespace blender
display list (or rather multi purpose list) stuff.
#define BLI_SCOPED_DEFER(function_to_defer)
Read Blender memory allocation and freeing.
#define UNPACK3_EX(pre, a, post)
#define UNPACK4(a)
#define UNPACK4_EX(pre, a, post)
#define UNPACK3(a)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
Span< T > as_span() const
Definition BLI_array.hh:254
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
Array< Vector< uint32_t > > vert_orig
Array< Vector< uint32_t > > face_orig
Array< Vector< uint32_t > > edge_orig
#define powf(x, y)
#define FLT_EPSILON
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define MU_ARRAY_ZERO
Definition mathutils.hh:261
#define MU_ARRAY_SPILL
Definition mathutils.hh:266
CDT_result< T > delaunay_2d_calc(const CDT_input< T > &input, CDT_output_type output_type)
BMesh Iterators.
static PyObject * M_Geometry_area_tri(PyObject *, PyObject *args)
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])
static PyObject * M_Geometry_delaunay_2d_cdt(PyObject *, PyObject *args)
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
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
static PyObject * M_Geometry_interpolate_bezier(PyObject *, PyObject *args)
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])
static PyModuleDef M_Geometry_module_def
int PyC_ParseBool(PyObject *o, void *p)
static PyObject * M_Geometry_intersect_point_tri(PyObject *, PyObject *args)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool mathutils_array_parse_alloc_viseq(PyObject *value, const char *error_prefix, Array< int > &r_offsets, Array< int > &r_data)
Definition mathutils.cc:379
void BKE_displist_fill(const ListBaseT< DispList > *dispbase, ListBaseT< DispList > *to, const float normal_proj[3], bool flip_normal, CurveFillSolverType fill_solver, CurveFillRuleType fill_rule)
Definition displist.cc:672
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_displist_free(ListBaseT< DispList > *lb)
Definition displist.cc:68
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 dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:511
static PyObject * M_Geometry_closest_point_on_tri(PyObject *, PyObject *args)
float normal_poly_v3(float n[3], const float verts[][3], unsigned int nr)
Definition math_geom.cc:83
static PyObject * M_Geometry_intersect_point_quad_2d(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_point_line(PyObject *, PyObject *const *args, Py_ssize_t nargs)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
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])
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])
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:403
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray)
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
static PyObject * M_Geometry_tessellate_polygon(PyObject *, PyObject *polyLineSeq)
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)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
int mathutils_array_parse_alloc_vi(int **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:340
static PyObject * M_Geometry_intersect_line_sphere(PyObject *, PyObject *args)
MINLINE void mul_v3_fl(float r[3], float f)
static PyObject * M_Geometry_intersect_line_sphere_2d(PyObject *, PyObject *args)
int BLI_convexhull_2d(Span< float2 > points, int r_points[])
static PyObject * M_Geometry_normal(PyObject *, PyObject *args)
PyObject * PyC_Tuple_Pack_I32(const Span< int > values)
static void boxPack_ToPyObject(PyObject *value, const BoxPack *boxarray)
float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:251
static PyMethodDef M_Geometry_methods[]
PyObject * Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
static PyObject * M_Geometry_barycentric_transform(PyObject *, PyObject *args)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static PyObject * M_Geometry_intersect_ray_tri(PyObject *, PyObject *args)
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
@ CU_FILL_SOLVER_SWEEP_LINE
BMesh const char void * data
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
static PyObject * M_Geometry_points_in_planes(PyObject *, PyObject *args)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
static PyObject * M_Geometry_box_fit_2d(PyObject *, PyObject *pointlist)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyObject * M_Geometry_intersect_line_line(PyObject *, PyObject *args)
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])
static PyObject * M_Geometry_intersect_tri_tri_2d(PyObject *, PyObject *args)
static void points_in_planes_fn(const float co[3], int i, int j, int k, void *user_data_p)
@ CU_FILL_RULE_EVEN_ODD
static PyObject * M_Geometry_intersect_point_line_segment(PyObject *, PyObject *const *args, Py_ssize_t nargs)
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 BLI_convexhull_aabb_fit_points_2d(Span< float2 > points)
static PyObject * M_Geometry_intersect_line_plane(PyObject *, PyObject *args)
static PyObject * M_Geometry_intersect_sphere_sphere_2d(PyObject *, PyObject *args)
MINLINE float normalize_v3(float n[3])
static PyObject * M_Geometry_box_pack_2d(PyObject *, PyObject *boxlist)
static PyObject * M_Geometry_distance_point_to_plane(PyObject *, PyObject *args)
static PyObject * list_of_lists_from_arrays(const Span< Vector< T > > data)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:104
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
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])
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])
static PyObject * M_Geometry_intersect_plane_plane(PyObject *, PyObject *args)
static PyObject * M_Geometry_volume_tetrahedron(PyObject *, PyObject *args)
PyMODINIT_FUNC PyInit_mathutils_geometry()
static PyObject * M_Geometry_intersect_line_line_2d(PyObject *, PyObject *args)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
static PyObject * M_Geometry_convex_hull_2d(PyObject *, PyObject *pointlist)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BLI_box_pack_2d(BoxPack *boxarray, unsigned int len, bool sort_boxes, float *r_tot_x, float *r_tot_y)
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:262
static PyObject * M_Geometry_intersect_point_tri_2d(PyObject *, PyObject *args)
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])
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, Py_ssize_t min, Py_ssize_t max)
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float