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