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