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