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