4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * This is a new part of Blender.
25 * Contributor(s): Joseph Gilbert, Campbell Barton
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/python/generic/mathutils_geometry.c
37 #include "mathutils_geometry.h"
39 /* Used for PolyFill */
40 #ifndef MATH_STANDALONE /* define when building outside blender */
41 # include "MEM_guardedalloc.h"
42 # include "BLI_blenlib.h"
43 # include "BLI_boxpack2d.h"
44 # include "BKE_displist.h"
45 # include "BKE_curve.h"
49 #include "BLI_utildefines.h"
51 #define SWAP_FLOAT(a, b, tmp) tmp=a; a=b; b=tmp
55 /*-------------------------DOC STRINGS ---------------------------*/
56 PyDoc_STRVAR(M_Geometry_doc,
57 "The Blender geometry module"
60 //---------------------------------INTERSECTION FUNCTIONS--------------------
62 PyDoc_STRVAR(M_Geometry_intersect_ray_tri_doc,
63 ".. function:: intersect_ray_tri(v1, v2, v3, ray, orig, clip=True)\n"
65 " Returns the intersection between a ray and a triangle, if possible, returns None otherwise.\n"
68 " :type v1: :class:`mathutils.Vector`\n"
70 " :type v2: :class:`mathutils.Vector`\n"
72 " :type v3: :class:`mathutils.Vector`\n"
73 " :arg ray: Direction of the projection\n"
74 " :type ray: :class:`mathutils.Vector`\n"
75 " :arg orig: Origin\n"
76 " :type orig: :class:`mathutils.Vector`\n"
77 " :arg clip: When False, don't restrict the intersection to the area of the triangle, use the infinite plane defined by the triangle.\n"
78 " :type clip: boolean\n"
79 " :return: The point of intersection or None if no intersection is found\n"
80 " :rtype: :class:`mathutils.Vector` or None\n"
82 static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject* args)
84 VectorObject *ray, *ray_off, *vec1, *vec2, *vec3;
85 float dir[3], orig[3], v1[3], v2[3], v3[3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
86 float det, inv_det, u, v, t;
89 if(!PyArg_ParseTuple(args, "O!O!O!O!O!|i:intersect_ray_tri", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3, &vector_Type, &ray, &vector_Type, &ray_off , &clip)) {
92 if(vec1->size != 3 || vec2->size != 3 || vec3->size != 3 || ray->size != 3 || ray_off->size != 3) {
93 PyErr_SetString(PyExc_ValueError,
94 "only 3D vectors for all parameters");
98 if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1 || BaseMath_ReadCallback(ray) == -1 || BaseMath_ReadCallback(ray_off) == -1)
101 VECCOPY(v1, vec1->vec);
102 VECCOPY(v2, vec2->vec);
103 VECCOPY(v3, vec3->vec);
105 VECCOPY(dir, ray->vec);
108 VECCOPY(orig, ray_off->vec);
110 /* find vectors for two edges sharing v1 */
111 sub_v3_v3v3(e1, v2, v1);
112 sub_v3_v3v3(e2, v3, v1);
114 /* begin calculating determinant - also used to calculated U parameter */
115 cross_v3_v3v3(pvec, dir, e2);
117 /* if determinant is near zero, ray lies in plane of triangle */
118 det= dot_v3v3(e1, pvec);
120 if (det > -0.000001f && det < 0.000001f) {
126 /* calculate distance from v1 to ray origin */
127 sub_v3_v3v3(tvec, orig, v1);
129 /* calculate U parameter and test bounds */
130 u= dot_v3v3(tvec, pvec) * inv_det;
131 if (clip && (u < 0.0f || u > 1.0f)) {
135 /* prepare to test the V parameter */
136 cross_v3_v3v3(qvec, tvec, e1);
138 /* calculate V parameter and test bounds */
139 v= dot_v3v3(dir, qvec) * inv_det;
141 if (clip && (v < 0.0f || u + v > 1.0f)) {
145 /* calculate t, ray intersects triangle */
146 t= dot_v3v3(e2, qvec) * inv_det;
149 add_v3_v3v3(pvec, orig, dir);
151 return newVectorObject(pvec, 3, Py_NEW, NULL);
154 /* Line-Line intersection using algorithm from mathworld.wolfram.com */
156 PyDoc_STRVAR(M_Geometry_intersect_line_line_doc,
157 ".. function:: intersect_line_line(v1, v2, v3, v4)\n"
159 " Returns a tuple with the points on each line respectively closest to the other.\n"
161 " :arg v1: First point of the first line\n"
162 " :type v1: :class:`mathutils.Vector`\n"
163 " :arg v2: Second point of the first line\n"
164 " :type v2: :class:`mathutils.Vector`\n"
165 " :arg v3: First point of the second line\n"
166 " :type v3: :class:`mathutils.Vector`\n"
167 " :arg v4: Second point of the second line\n"
168 " :type v4: :class:`mathutils.Vector`\n"
169 " :rtype: tuple of :class:`mathutils.Vector`'s\n"
171 static PyObject *M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject *args)
174 VectorObject *vec1, *vec2, *vec3, *vec4;
175 float v1[3], v2[3], v3[3], v4[3], i1[3], i2[3];
177 if(!PyArg_ParseTuple(args, "O!O!O!O!:intersect_line_line", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3, &vector_Type, &vec4)) {
180 if(vec1->size != vec2->size || vec1->size != vec3->size || vec3->size != vec2->size) {
181 PyErr_SetString(PyExc_ValueError,
182 "vectors must be of the same size");
186 if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1 || BaseMath_ReadCallback(vec4) == -1)
189 if(vec1->size == 3 || vec1->size == 2) {
192 if (vec1->size == 3) {
193 VECCOPY(v1, vec1->vec);
194 VECCOPY(v2, vec2->vec);
195 VECCOPY(v3, vec3->vec);
196 VECCOPY(v4, vec4->vec);
216 result= isect_line_line_v3(v1, v2, v3, v4, i1, i2);
223 tuple= PyTuple_New(2);
224 PyTuple_SET_ITEM(tuple, 0, newVectorObject(i1, vec1->size, Py_NEW, NULL));
225 PyTuple_SET_ITEM(tuple, 1, newVectorObject(i2, vec1->size, Py_NEW, NULL));
230 PyErr_SetString(PyExc_ValueError,
231 "2D/3D vectors only");
239 //----------------------------geometry.normal() -------------------
240 PyDoc_STRVAR(M_Geometry_normal_doc,
241 ".. function:: normal(v1, v2, v3, v4=None)\n"
243 " Returns the normal of the 3D tri or quad.\n"
246 " :type v1: :class:`mathutils.Vector`\n"
248 " :type v2: :class:`mathutils.Vector`\n"
250 " :type v3: :class:`mathutils.Vector`\n"
251 " :arg v4: Point4 (optional)\n"
252 " :type v4: :class:`mathutils.Vector`\n"
253 " :rtype: :class:`mathutils.Vector`\n"
255 static PyObject *M_Geometry_normal(PyObject *UNUSED(self), PyObject* args)
257 VectorObject *vec1, *vec2, *vec3, *vec4;
260 if(PyTuple_GET_SIZE(args) == 3) {
261 if(!PyArg_ParseTuple(args, "O!O!O!:normal", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3)) {
264 if(vec1->size != vec2->size || vec1->size != vec3->size) {
265 PyErr_SetString(PyExc_ValueError,
266 "vectors must be of the same size");
270 PyErr_SetString(PyExc_ValueError,
271 "2D vectors unsupported");
275 if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1)
278 normal_tri_v3(n, vec1->vec, vec2->vec, vec3->vec);
281 if(!PyArg_ParseTuple(args, "O!O!O!O!:normal", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3, &vector_Type, &vec4)) {
284 if(vec1->size != vec2->size || vec1->size != vec3->size || vec1->size != vec4->size) {
285 PyErr_SetString(PyExc_ValueError,
286 "vectors must be of the same size");
290 PyErr_SetString(PyExc_ValueError,
291 "2D vectors unsupported");
295 if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1 || BaseMath_ReadCallback(vec4) == -1)
298 normal_quad_v3(n, vec1->vec, vec2->vec, vec3->vec, vec4->vec);
301 return newVectorObject(n, 3, Py_NEW, NULL);
304 //--------------------------------- AREA FUNCTIONS--------------------
306 PyDoc_STRVAR(M_Geometry_area_tri_doc,
307 ".. function:: area_tri(v1, v2, v3)\n"
309 " Returns the area size of the 2D or 3D triangle defined.\n"
312 " :type v1: :class:`mathutils.Vector`\n"
314 " :type v2: :class:`mathutils.Vector`\n"
316 " :type v3: :class:`mathutils.Vector`\n"
319 static PyObject *M_Geometry_area_tri(PyObject *UNUSED(self), PyObject* args)
321 VectorObject *vec1, *vec2, *vec3;
323 if(!PyArg_ParseTuple(args, "O!O!O!:area_tri", &vector_Type, &vec1, &vector_Type, &vec2, &vector_Type, &vec3)) {
327 if(vec1->size != vec2->size || vec1->size != vec3->size) {
328 PyErr_SetString(PyExc_ValueError,
329 "vectors must be of the same size");
333 if(BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1 || BaseMath_ReadCallback(vec3) == -1)
336 if (vec1->size == 3) {
337 return PyFloat_FromDouble(area_tri_v3(vec1->vec, vec2->vec, vec3->vec));
339 else if (vec1->size == 2) {
340 return PyFloat_FromDouble(area_tri_v2(vec1->vec, vec2->vec, vec3->vec));
343 PyErr_SetString(PyExc_ValueError,
344 "only 2D,3D vectors are supported");
350 PyDoc_STRVAR(M_Geometry_intersect_line_line_2d_doc,
351 ".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2)\n"
353 " Takes 2 lines (as 4 vectors) and returns a vector for their point of intersection or None.\n"
355 " :arg lineA_p1: First point of the first line\n"
356 " :type lineA_p1: :class:`mathutils.Vector`\n"
357 " :arg lineA_p2: Second point of the first line\n"
358 " :type lineA_p2: :class:`mathutils.Vector`\n"
359 " :arg lineB_p1: First point of the second line\n"
360 " :type lineB_p1: :class:`mathutils.Vector`\n"
361 " :arg lineB_p2: Second point of the second line\n"
362 " :type lineB_p2: :class:`mathutils.Vector`\n"
363 " :return: The point of intersection or None when not found\n"
364 " :rtype: :class:`mathutils.Vector` or None\n"
366 static PyObject *M_Geometry_intersect_line_line_2d(PyObject *UNUSED(self), PyObject* args)
368 VectorObject *line_a1, *line_a2, *line_b1, *line_b2;
370 if(!PyArg_ParseTuple(args, "O!O!O!O!:intersect_line_line_2d",
371 &vector_Type, &line_a1,
372 &vector_Type, &line_a2,
373 &vector_Type, &line_b1,
374 &vector_Type, &line_b2)
379 if(BaseMath_ReadCallback(line_a1) == -1 || BaseMath_ReadCallback(line_a2) == -1 || BaseMath_ReadCallback(line_b1) == -1 || BaseMath_ReadCallback(line_b2) == -1)
382 if(isect_seg_seg_v2_point(line_a1->vec, line_a2->vec, line_b1->vec, line_b2->vec, vi) == 1) {
383 return newVectorObject(vi, 2, Py_NEW, NULL);
391 PyDoc_STRVAR(M_Geometry_intersect_line_plane_doc,
392 ".. function:: intersect_line_plane(line_a, line_b, plane_co, plane_no, no_flip=False)\n"
394 " Takes 2 lines (as 4 vectors) and returns a vector for their point of intersection or None.\n"
396 " :arg line_a: First point of the first line\n"
397 " :type line_a: :class:`mathutils.Vector`\n"
398 " :arg line_b: Second point of the first line\n"
399 " :type line_b: :class:`mathutils.Vector`\n"
400 " :arg plane_co: A point on the plane\n"
401 " :type plane_co: :class:`mathutils.Vector`\n"
402 " :arg plane_no: The direction the plane is facing\n"
403 " :type plane_no: :class:`mathutils.Vector`\n"
404 " :arg no_flip: Always return an intersection on the directon defined bt line_a -> line_b\n"
405 " :type no_flip: :boolean\n"
406 " :return: The point of intersection or None when not found\n"
407 " :rtype: :class:`mathutils.Vector` or None\n"
409 static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObject* args)
411 VectorObject *line_a, *line_b, *plane_co, *plane_no;
414 if(!PyArg_ParseTuple(args, "O!O!O!O!|i:intersect_line_plane",
415 &vector_Type, &line_a,
416 &vector_Type, &line_b,
417 &vector_Type, &plane_co,
418 &vector_Type, &plane_no,
424 if( BaseMath_ReadCallback(line_a) == -1 ||
425 BaseMath_ReadCallback(line_b) == -1 ||
426 BaseMath_ReadCallback(plane_co) == -1 ||
427 BaseMath_ReadCallback(plane_no) == -1
432 if(ELEM4(2, line_a->size, line_b->size, plane_co->size, plane_no->size)) {
433 PyErr_SetString(PyExc_ValueError,
434 "geometry.intersect_line_plane(...): "
435 " can't use 2D Vectors");
439 if(isect_line_plane_v3(isect, line_a->vec, line_b->vec, plane_co->vec, plane_no->vec, no_flip) == 1) {
440 return newVectorObject(isect, 3, Py_NEW, NULL);
448 PyDoc_STRVAR(M_Geometry_intersect_line_sphere_doc,
449 ".. function:: intersect_line_sphere(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
451 " Takes a lines (as 2 vectors), a sphere as a point and a radius and\n"
452 " returns the intersection\n"
454 " :arg line_a: First point of the first line\n"
455 " :type line_a: :class:`mathutils.Vector`\n"
456 " :arg line_b: Second point of the first line\n"
457 " :type line_b: :class:`mathutils.Vector`\n"
458 " :arg sphere_co: The center of the sphere\n"
459 " :type sphere_co: :class:`mathutils.Vector`\n"
460 " :arg sphere_radius: Radius of the sphere\n"
461 " :type sphere_radius: sphere_radius\n"
462 " :return: The intersection points as a pair of vectors or None when there is no intersection\n"
463 " :rtype: A tuple pair containing :class:`mathutils.Vector` or None\n"
465 static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObject* args)
467 VectorObject *line_a, *line_b, *sphere_co;
474 if(!PyArg_ParseTuple(args, "O!O!O!f|i:intersect_line_sphere",
475 &vector_Type, &line_a,
476 &vector_Type, &line_b,
477 &vector_Type, &sphere_co,
478 &sphere_radius, &clip)
483 if( BaseMath_ReadCallback(line_a) == -1 ||
484 BaseMath_ReadCallback(line_b) == -1 ||
485 BaseMath_ReadCallback(sphere_co) == -1
490 if(ELEM3(2, line_a->size, line_b->size, sphere_co->size)) {
491 PyErr_SetString(PyExc_ValueError,
492 "geometry.intersect_line_sphere(...): "
493 " can't use 2D Vectors");
501 PyObject *ret= PyTuple_New(2);
503 switch(isect_line_sphere_v3(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) {
505 if(!(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE;
509 if(!(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE;
510 if(!(!clip || (((lambda= line_point_factor_v3(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b= FALSE;
517 if(use_a) { PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 3, Py_NEW, NULL)); }
518 else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); }
520 if(use_b) { PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 3, Py_NEW, NULL)); }
521 else { PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); }
527 /* keep in sync with M_Geometry_intersect_line_sphere */
528 PyDoc_STRVAR(M_Geometry_intersect_line_sphere_2d_doc,
529 ".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
531 " Takes a lines (as 2 vectors), a sphere as a point and a radius and\n"
532 " returns the intersection\n"
534 " :arg line_a: First point of the first line\n"
535 " :type line_a: :class:`mathutils.Vector`\n"
536 " :arg line_b: Second point of the first line\n"
537 " :type line_b: :class:`mathutils.Vector`\n"
538 " :arg sphere_co: The center of the sphere\n"
539 " :type sphere_co: :class:`mathutils.Vector`\n"
540 " :arg sphere_radius: Radius of the sphere\n"
541 " :type sphere_radius: sphere_radius\n"
542 " :return: The intersection points as a pair of vectors or None when there is no intersection\n"
543 " :rtype: A tuple pair containing :class:`mathutils.Vector` or None\n"
545 static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyObject* args)
547 VectorObject *line_a, *line_b, *sphere_co;
554 if(!PyArg_ParseTuple(args, "O!O!O!f|i:intersect_line_sphere_2d",
555 &vector_Type, &line_a,
556 &vector_Type, &line_b,
557 &vector_Type, &sphere_co,
558 &sphere_radius, &clip)
563 if( BaseMath_ReadCallback(line_a) == -1 ||
564 BaseMath_ReadCallback(line_b) == -1 ||
565 BaseMath_ReadCallback(sphere_co) == -1
574 PyObject *ret= PyTuple_New(2);
576 switch(isect_line_sphere_v2(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) {
578 if(!(!clip || (((lambda= line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE;
582 if(!(!clip || (((lambda= line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE;
583 if(!(!clip || (((lambda= line_point_factor_v2(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b= FALSE;
590 if(use_a) { PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 2, Py_NEW, NULL)); }
591 else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); }
593 if(use_b) { PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 2, Py_NEW, NULL)); }
594 else { PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); }
600 PyDoc_STRVAR(M_Geometry_intersect_point_line_doc,
601 ".. function:: intersect_point_line(pt, line_p1, line_p2)\n"
603 " Takes a point and a line and returns a tuple with the closest point on the line and its distance from the first point of the line as a percentage of the length of the line.\n"
606 " :type pt: :class:`mathutils.Vector`\n"
607 " :arg line_p1: First point of the line\n"
608 " :type line_p1: :class:`mathutils.Vector`\n"
609 " :arg line_p1: Second point of the line\n"
610 " :type line_p1: :class:`mathutils.Vector`\n"
611 " :rtype: (:class:`mathutils.Vector`, float)\n"
613 static PyObject *M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObject* args)
615 VectorObject *pt, *line_1, *line_2;
616 float pt_in[3], pt_out[3], l1[3], l2[3];
620 if(!PyArg_ParseTuple(args, "O!O!O!:intersect_point_line",
622 &vector_Type, &line_1,
623 &vector_Type, &line_2)
628 if(BaseMath_ReadCallback(pt) == -1 || BaseMath_ReadCallback(line_1) == -1 || BaseMath_ReadCallback(line_2) == -1)
631 /* accept 2d verts */
632 if (pt->size==3) { VECCOPY(pt_in, pt->vec);}
633 else { pt_in[2]=0.0; VECCOPY2D(pt_in, pt->vec) }
635 if (line_1->size==3) { VECCOPY(l1, line_1->vec);}
636 else { l1[2]=0.0; VECCOPY2D(l1, line_1->vec) }
638 if (line_2->size==3) { VECCOPY(l2, line_2->vec);}
639 else { l2[2]=0.0; VECCOPY2D(l2, line_2->vec) }
641 /* do the calculation */
642 lambda= closest_to_line_v3(pt_out, pt_in, l1, l2);
645 PyTuple_SET_ITEM(ret, 0, newVectorObject(pt_out, 3, Py_NEW, NULL));
646 PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(lambda));
650 PyDoc_STRVAR(M_Geometry_intersect_point_tri_2d_doc,
651 ".. function:: intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3)\n"
653 " Takes 4 vectors (using only the x and y coordinates): one is the point and the next 3 define the triangle. Returns 1 if the point is within the triangle, otherwise 0.\n"
656 " :type v1: :class:`mathutils.Vector`\n"
657 " :arg tri_p1: First point of the triangle\n"
658 " :type tri_p1: :class:`mathutils.Vector`\n"
659 " :arg tri_p2: Second point of the triangle\n"
660 " :type tri_p2: :class:`mathutils.Vector`\n"
661 " :arg tri_p3: Third point of the triangle\n"
662 " :type tri_p3: :class:`mathutils.Vector`\n"
665 static PyObject *M_Geometry_intersect_point_tri_2d(PyObject *UNUSED(self), PyObject* args)
667 VectorObject *pt_vec, *tri_p1, *tri_p2, *tri_p3;
669 if(!PyArg_ParseTuple(args, "O!O!O!O!:intersect_point_tri_2d",
670 &vector_Type, &pt_vec,
671 &vector_Type, &tri_p1,
672 &vector_Type, &tri_p2,
673 &vector_Type, &tri_p3)
678 if(BaseMath_ReadCallback(pt_vec) == -1 || BaseMath_ReadCallback(tri_p1) == -1 || BaseMath_ReadCallback(tri_p2) == -1 || BaseMath_ReadCallback(tri_p3) == -1)
681 return PyLong_FromLong(isect_point_tri_v2(pt_vec->vec, tri_p1->vec, tri_p2->vec, tri_p3->vec));
684 PyDoc_STRVAR(M_Geometry_intersect_point_quad_2d_doc,
685 ".. function:: intersect_point_quad_2d(pt, quad_p1, quad_p2, quad_p3, quad_p4)\n"
687 " Takes 5 vectors (using only the x and y coordinates): one is the point and the next 4 define the quad, only the x and y are used from the vectors. Returns 1 if the point is within the quad, otherwise 0.\n"
690 " :type v1: :class:`mathutils.Vector`\n"
691 " :arg quad_p1: First point of the quad\n"
692 " :type quad_p1: :class:`mathutils.Vector`\n"
693 " :arg quad_p2: Second point of the quad\n"
694 " :type quad_p2: :class:`mathutils.Vector`\n"
695 " :arg quad_p3: Third point of the quad\n"
696 " :type quad_p3: :class:`mathutils.Vector`\n"
697 " :arg quad_p4: Forth point of the quad\n"
698 " :type quad_p4: :class:`mathutils.Vector`\n"
701 static PyObject *M_Geometry_intersect_point_quad_2d(PyObject *UNUSED(self), PyObject* args)
703 VectorObject *pt_vec, *quad_p1, *quad_p2, *quad_p3, *quad_p4;
705 if(!PyArg_ParseTuple(args, "O!O!O!O!O!:intersect_point_quad_2d",
706 &vector_Type, &pt_vec,
707 &vector_Type, &quad_p1,
708 &vector_Type, &quad_p2,
709 &vector_Type, &quad_p3,
710 &vector_Type, &quad_p4)
715 if(BaseMath_ReadCallback(pt_vec) == -1 || BaseMath_ReadCallback(quad_p1) == -1 || BaseMath_ReadCallback(quad_p2) == -1 || BaseMath_ReadCallback(quad_p3) == -1 || BaseMath_ReadCallback(quad_p4) == -1)
718 return PyLong_FromLong(isect_point_quad_v2(pt_vec->vec, quad_p1->vec, quad_p2->vec, quad_p3->vec, quad_p4->vec));
721 PyDoc_STRVAR(M_Geometry_barycentric_transform_doc,
722 ".. function:: barycentric_transform(point, tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
724 " Return a transformed point, the transformation is defined by 2 triangles.\n"
726 " :arg point: The point to transform.\n"
727 " :type point: :class:`mathutils.Vector`\n"
728 " :arg tri_a1: source triangle vertex.\n"
729 " :type tri_a1: :class:`mathutils.Vector`\n"
730 " :arg tri_a2: source triangle vertex.\n"
731 " :type tri_a2: :class:`mathutils.Vector`\n"
732 " :arg tri_a3: source triangle vertex.\n"
733 " :type tri_a3: :class:`mathutils.Vector`\n"
734 " :arg tri_a1: target triangle vertex.\n"
735 " :type tri_a1: :class:`mathutils.Vector`\n"
736 " :arg tri_a2: target triangle vertex.\n"
737 " :type tri_a2: :class:`mathutils.Vector`\n"
738 " :arg tri_a3: target triangle vertex.\n"
739 " :type tri_a3: :class:`mathutils.Vector`\n"
740 " :return: The transformed point\n"
741 " :rtype: :class:`mathutils.Vector`'s\n"
743 static PyObject *M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObject *args)
745 VectorObject *vec_pt;
746 VectorObject *vec_t1_tar, *vec_t2_tar, *vec_t3_tar;
747 VectorObject *vec_t1_src, *vec_t2_src, *vec_t3_src;
750 if(!PyArg_ParseTuple(args, "O!O!O!O!O!O!O!:barycentric_transform",
751 &vector_Type, &vec_pt,
752 &vector_Type, &vec_t1_src,
753 &vector_Type, &vec_t2_src,
754 &vector_Type, &vec_t3_src,
755 &vector_Type, &vec_t1_tar,
756 &vector_Type, &vec_t2_tar,
757 &vector_Type, &vec_t3_tar)
762 if( vec_pt->size != 3 ||
763 vec_t1_src->size != 3 ||
764 vec_t2_src->size != 3 ||
765 vec_t3_src->size != 3 ||
766 vec_t1_tar->size != 3 ||
767 vec_t2_tar->size != 3 ||
768 vec_t3_tar->size != 3)
770 PyErr_SetString(PyExc_ValueError,
771 "One of more of the vector arguments wasn't a 3D vector");
775 barycentric_transform(vec, vec_pt->vec,
776 vec_t1_tar->vec, vec_t2_tar->vec, vec_t3_tar->vec,
777 vec_t1_src->vec, vec_t2_src->vec, vec_t3_src->vec);
779 return newVectorObject(vec, 3, Py_NEW, NULL);
782 #ifndef MATH_STANDALONE
784 PyDoc_STRVAR(M_Geometry_interpolate_bezier_doc,
785 ".. function:: interpolate_bezier(knot1, handle1, handle2, knot2, resolution)\n"
787 " Interpolate a bezier spline segment.\n"
789 " :arg knot1: First bezier spline point.\n"
790 " :type knot1: :class:`mathutils.Vector`\n"
791 " :arg handle1: First bezier spline handle.\n"
792 " :type handle1: :class:`mathutils.Vector`\n"
793 " :arg handle2: Second bezier spline handle.\n"
794 " :type handle2: :class:`mathutils.Vector`\n"
795 " :arg knot2: Second bezier spline point.\n"
796 " :type knot2: :class:`mathutils.Vector`\n"
797 " :arg resolution: Number of points to return.\n"
798 " :type resolution: int\n"
799 " :return: The interpolated points\n"
800 " :rtype: list of :class:`mathutils.Vector`'s\n"
802 static PyObject *M_Geometry_interpolate_bezier(PyObject *UNUSED(self), PyObject* args)
804 VectorObject *vec_k1, *vec_h1, *vec_k2, *vec_h2;
808 float *coord_array, *fp;
811 float k1[4]= {0.0, 0.0, 0.0, 0.0};
812 float h1[4]= {0.0, 0.0, 0.0, 0.0};
813 float k2[4]= {0.0, 0.0, 0.0, 0.0};
814 float h2[4]= {0.0, 0.0, 0.0, 0.0};
817 if(!PyArg_ParseTuple(args, "O!O!O!O!i:interpolate_bezier",
818 &vector_Type, &vec_k1,
819 &vector_Type, &vec_h1,
820 &vector_Type, &vec_h2,
821 &vector_Type, &vec_k2, &resolu)
827 PyErr_SetString(PyExc_ValueError,
828 "resolution must be 2 or over");
832 if(BaseMath_ReadCallback(vec_k1) == -1 || BaseMath_ReadCallback(vec_h1) == -1 || BaseMath_ReadCallback(vec_k2) == -1 || BaseMath_ReadCallback(vec_h2) == -1)
835 dims= MAX4(vec_k1->size, vec_h1->size, vec_h2->size, vec_k2->size);
837 for(i=0; i < vec_k1->size; i++) k1[i]= vec_k1->vec[i];
838 for(i=0; i < vec_h1->size; i++) h1[i]= vec_h1->vec[i];
839 for(i=0; i < vec_k2->size; i++) k2[i]= vec_k2->vec[i];
840 for(i=0; i < vec_h2->size; i++) h2[i]= vec_h2->vec[i];
842 coord_array= MEM_callocN(dims * (resolu) * sizeof(float), "interpolate_bezier");
843 for(i=0; i<dims; i++) {
844 forward_diff_bezier(k1[i], h1[i], h2[i], k2[i], coord_array+i, resolu-1, sizeof(float)*dims);
847 list= PyList_New(resolu);
849 for(i=0; i<resolu; i++, fp= fp+dims) {
850 PyList_SET_ITEM(list, i, newVectorObject(fp, dims, Py_NEW, NULL));
852 MEM_freeN(coord_array);
857 PyDoc_STRVAR(M_Geometry_tesselate_polygon_doc,
858 ".. function:: tesselate_polygon(veclist_list)\n"
860 " Takes a list of polylines (each point a vector) and returns the point indices for a polyline filled with triangles.\n"
862 " :arg veclist_list: list of polylines\n"
865 /* PolyFill function, uses Blenders scanfill to fill multiple poly lines */
866 static PyObject *M_Geometry_tesselate_polygon(PyObject *UNUSED(self), PyObject *polyLineSeq)
868 PyObject *tri_list; /*return this list of tri's */
869 PyObject *polyLine, *polyVec;
870 int i, len_polylines, len_polypoints, ls_error= 0;
872 /* display listbase */
873 ListBase dispbase={NULL, NULL};
875 float *fp; /*pointer to the array of malloced dl->verts to set the points from the vectors */
876 int index, *dl_face, totpoints=0;
878 if(!PySequence_Check(polyLineSeq)) {
879 PyErr_SetString(PyExc_TypeError,
880 "expected a sequence of poly lines");
884 len_polylines= PySequence_Size(polyLineSeq);
886 for(i= 0; i < len_polylines; ++i) {
887 polyLine= PySequence_GetItem(polyLineSeq, i);
888 if (!PySequence_Check(polyLine)) {
889 freedisplist(&dispbase);
890 Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/
891 PyErr_SetString(PyExc_TypeError,
892 "One or more of the polylines is not a sequence of mathutils.Vector's");
896 len_polypoints= PySequence_Size(polyLine);
897 if (len_polypoints>0) { /* dont bother adding edges as polylines */
899 if (EXPP_check_sequence_consistency(polyLine, &vector_Type) != 1) {
900 freedisplist(&dispbase);
902 PyErr_SetString(PyExc_TypeError,
903 "A point in one of the polylines is not a mathutils.Vector type");
907 dl= MEM_callocN(sizeof(DispList), "poly disp");
908 BLI_addtail(&dispbase, dl);
910 dl->nr= len_polypoints;
912 dl->parts= 1; /* no faces, 1 edge loop */
913 dl->col= 0; /* no material */
914 dl->verts= fp= MEM_callocN(sizeof(float)*3*len_polypoints, "dl verts");
915 dl->index= MEM_callocN(sizeof(int)*3*len_polypoints, "dl index");
917 for(index= 0; index<len_polypoints; ++index, fp+=3) {
918 polyVec= PySequence_GetItem(polyLine, index);
919 if(VectorObject_Check(polyVec)) {
921 if(BaseMath_ReadCallback((VectorObject *)polyVec) == -1)
924 fp[0]= ((VectorObject *)polyVec)->vec[0];
925 fp[1]= ((VectorObject *)polyVec)->vec[1];
926 if(((VectorObject *)polyVec)->size > 2)
927 fp[2]= ((VectorObject *)polyVec)->vec[2];
929 fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */
943 freedisplist(&dispbase); /* possible some dl was allocated */
944 PyErr_SetString(PyExc_TypeError,
945 "A point in one of the polylines "
946 "is not a mathutils.Vector type");
949 else if (totpoints) {
950 /* now make the list to return */
951 filldisplist(&dispbase, &dispbase, 0);
953 /* The faces are stored in a new DisplayList
954 thats added to the head of the listbase */
957 tri_list= PyList_New(dl->parts);
959 freedisplist(&dispbase);
960 PyErr_SetString(PyExc_RuntimeError,
961 "failed to make a new list");
967 while(index < dl->parts) {
968 PyList_SET_ITEM(tri_list, index, Py_BuildValue("iii", dl_face[0], dl_face[1], dl_face[2]));
972 freedisplist(&dispbase);
975 /* no points, do this so scripts dont barf */
976 freedisplist(&dispbase); /* possible some dl was allocated */
977 tri_list= PyList_New(0);
984 static int boxPack_FromPyObject(PyObject *value, boxPack **boxarray)
987 PyObject *list_item, *item_1, *item_2;
991 /* Error checking must already be done */
992 if(!PyList_Check(value)) {
993 PyErr_SetString(PyExc_TypeError,
994 "can only back a list of [x, y, w, h]");
998 len= PyList_GET_SIZE(value);
1000 (*boxarray)= MEM_mallocN(len*sizeof(boxPack), "boxPack box");
1003 for(i= 0; i < len; i++) {
1004 list_item= PyList_GET_ITEM(value, i);
1005 if(!PyList_Check(list_item) || PyList_GET_SIZE(list_item) < 4) {
1006 MEM_freeN(*boxarray);
1007 PyErr_SetString(PyExc_TypeError,
1008 "can only pack a list of [x, y, w, h]");
1014 item_1= PyList_GET_ITEM(list_item, 2);
1015 item_2= PyList_GET_ITEM(list_item, 3);
1017 box->w= (float)PyFloat_AsDouble(item_1);
1018 box->h= (float)PyFloat_AsDouble(item_2);
1021 /* accounts for error case too and overwrites with own error */
1022 if (box->w < 0.0f || box->h < 0.0f) {
1023 MEM_freeN(*boxarray);
1024 PyErr_SetString(PyExc_TypeError,
1025 "error parsing width and height values from list: "
1026 "[x, y, w, h], not numbers or below zero");
1030 /* verts will be added later */
1035 static void boxPack_ToPyObject(PyObject *value, boxPack **boxarray)
1038 PyObject *list_item;
1041 len= PyList_GET_SIZE(value);
1043 for(i= 0; i < len; i++) {
1045 list_item= PyList_GET_ITEM(value, box->index);
1046 PyList_SET_ITEM(list_item, 0, PyFloat_FromDouble(box->x));
1047 PyList_SET_ITEM(list_item, 1, PyFloat_FromDouble(box->y));
1049 MEM_freeN(*boxarray);
1052 PyDoc_STRVAR(M_Geometry_box_pack_2d_doc,
1053 ".. function:: box_pack_2d(boxes)\n"
1055 " Returns the normal of the 3D tri or quad.\n"
1057 " :arg boxes: list of boxes, each box is a list where the first 4 items are [x, y, width, height, ...] other items are ignored.\n"
1058 " :type boxes: list\n"
1059 " :return: the width and height of the packed bounding box\n"
1060 " :rtype: tuple, pair of floats\n"
1062 static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlist)
1064 float tot_width= 0.0f, tot_height= 0.0f;
1069 if(!PyList_Check(boxlist)) {
1070 PyErr_SetString(PyExc_TypeError,
1071 "expected a list of boxes [[x, y, w, h], ... ]");
1075 len= PyList_GET_SIZE(boxlist);
1077 boxPack *boxarray= NULL;
1078 if(boxPack_FromPyObject(boxlist, &boxarray) == -1) {
1079 return NULL; /* exception set */
1082 /* Non Python function */
1083 boxPack2D(boxarray, len, &tot_width, &tot_height);
1085 boxPack_ToPyObject(boxlist, &boxarray);
1088 ret= PyTuple_New(2);
1089 PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(tot_width));
1090 PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(tot_width));
1094 #endif /* MATH_STANDALONE */
1097 static PyMethodDef M_Geometry_methods[]= {
1098 {"intersect_ray_tri", (PyCFunction) M_Geometry_intersect_ray_tri, METH_VARARGS, M_Geometry_intersect_ray_tri_doc},
1099 {"intersect_point_line", (PyCFunction) M_Geometry_intersect_point_line, METH_VARARGS, M_Geometry_intersect_point_line_doc},
1100 {"intersect_point_tri_2d", (PyCFunction) M_Geometry_intersect_point_tri_2d, METH_VARARGS, M_Geometry_intersect_point_tri_2d_doc},
1101 {"intersect_point_quad_2d", (PyCFunction) M_Geometry_intersect_point_quad_2d, METH_VARARGS, M_Geometry_intersect_point_quad_2d_doc},
1102 {"intersect_line_line", (PyCFunction) M_Geometry_intersect_line_line, METH_VARARGS, M_Geometry_intersect_line_line_doc},
1103 {"intersect_line_line_2d", (PyCFunction) M_Geometry_intersect_line_line_2d, METH_VARARGS, M_Geometry_intersect_line_line_2d_doc},
1104 {"intersect_line_plane", (PyCFunction) M_Geometry_intersect_line_plane, METH_VARARGS, M_Geometry_intersect_line_plane_doc},
1105 {"intersect_line_sphere", (PyCFunction) M_Geometry_intersect_line_sphere, METH_VARARGS, M_Geometry_intersect_line_sphere_doc},
1106 {"intersect_line_sphere_2d", (PyCFunction) M_Geometry_intersect_line_sphere_2d, METH_VARARGS, M_Geometry_intersect_line_sphere_2d_doc},
1107 {"area_tri", (PyCFunction) M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_doc},
1108 {"normal", (PyCFunction) M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},
1109 {"barycentric_transform", (PyCFunction) M_Geometry_barycentric_transform, METH_VARARGS, M_Geometry_barycentric_transform_doc},
1110 #ifndef MATH_STANDALONE
1111 {"interpolate_bezier", (PyCFunction) M_Geometry_interpolate_bezier, METH_VARARGS, M_Geometry_interpolate_bezier_doc},
1112 {"tesselate_polygon", (PyCFunction) M_Geometry_tesselate_polygon, METH_O, M_Geometry_tesselate_polygon_doc},
1113 {"box_pack_2d", (PyCFunction) M_Geometry_box_pack_2d, METH_O, M_Geometry_box_pack_2d_doc},
1115 {NULL, NULL, 0, NULL}
1118 static struct PyModuleDef M_Geometry_module_def= {
1119 PyModuleDef_HEAD_INIT,
1120 "mathutils.geometry", /* m_name */
1121 M_Geometry_doc, /* m_doc */
1123 M_Geometry_methods, /* m_methods */
1124 NULL, /* m_reload */
1125 NULL, /* m_traverse */
1130 /*----------------------------MODULE INIT-------------------------*/
1131 PyMODINIT_FUNC PyInit_mathutils_geometry(void)
1133 PyObject *submodule= PyModule_Create(&M_Geometry_module_def);