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