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