index 818f30e..d935949 100644 (file)
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
- * All rights reserved.
- *
- * This is a new part of Blender.
- *
* Contributor(s): Joseph Gilbert, Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****

#include <Python.h>

+#include "mathutils.h"
#include "mathutils_geometry.h"

/* Used for PolyFill */
#ifndef MATH_STANDALONE /* define when building outside blender */
#  include "MEM_guardedalloc.h"
#  include "BLI_blenlib.h"
-#  include "BLI_boxpack2d.h"
+#  include "BLI_boxpack_2d.h"
+#  include "BLI_convexhull_2d.h"
#  include "BKE_displist.h"
#  include "BKE_curve.h"
#endif
#include "BLI_math.h"
#include "BLI_utildefines.h"

+#include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
+
/*-------------------------DOC STRINGS ---------------------------*/
PyDoc_STRVAR(M_Geometry_doc,
"The Blender geometry module"
);

-//---------------------------------INTERSECTION FUNCTIONS--------------------
+/* ---------------------------------INTERSECTION FUNCTIONS-------------------- */

PyDoc_STRVAR(M_Geometry_intersect_ray_tri_doc,
".. function:: intersect_ray_tri(v1, v2, v3, ray, orig, clip=True)\n"
@@ -75,48 +75,39 @@ PyDoc_STRVAR(M_Geometry_intersect_ray_tri_doc,
);
static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *ray, *ray_off, *vec1, *vec2, *vec3;
-       float dir[3], orig[3], v1[3], v2[3], v3[3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
+       const char *error_prefix = "intersect_ray_tri";
+       PyObject *py_ray, *py_ray_off, *py_tri[3];
+       float dir[3], orig[3], tri[3][3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
float det, inv_det, u, v, t;
-       int clip = 1;
-
-       if (!PyArg_ParseTuple(args,
-                             "O!O!O!O!O!|i:intersect_ray_tri",
-                             &vector_Type, &vec1,
-                             &vector_Type, &vec2,
-                             &vector_Type, &vec3,
-                             &vector_Type, &ray,
-                             &vector_Type, &ray_off, &clip))
+       bool clip = true;
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOOO|O&:intersect_ray_tri",
+               UNPACK3_EX(&, py_tri, ),
+               &py_ray, &py_ray_off,
+               PyC_ParseBool, &clip))
{
return NULL;
}
-       if (vec1->size != 3 || vec2->size != 3 || vec3->size != 3 || ray->size != 3 || ray_off->size != 3) {
-               PyErr_SetString(PyExc_ValueError,
-                               "only 3D vectors for all parameters");
-               return NULL;
-       }

-       if (BaseMath_ReadCallback(vec1) == -1 ||
+       if (((mathutils_array_parse(dir, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray, error_prefix) != -1) &&
+            (mathutils_array_parse(orig, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_ray_off, error_prefix) != -1)) == 0)
{
return NULL;
}

-       copy_v3_v3(v1, vec1->vec);
-       copy_v3_v3(v2, vec2->vec);
-       copy_v3_v3(v3, vec3->vec);
+       for (i = 0; i < ARRAY_SIZE(tri); i++) {
+               if (mathutils_array_parse(tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1) {
+                       return NULL;
+               }
+       }

-       copy_v3_v3(dir, ray->vec);
normalize_v3(dir);

-       copy_v3_v3(orig, ray_off->vec);
-
/* find vectors for two edges sharing v1 */
-       sub_v3_v3v3(e1, v2, v1);
-       sub_v3_v3v3(e2, v3, v1);
+       sub_v3_v3v3(e1, tri[1], tri[0]);
+       sub_v3_v3v3(e2, tri[2], tri[0]);

/* begin calculating determinant - also used to calculated U parameter */
cross_v3_v3v3(pvec, dir, e2);
@@ -131,7 +122,7 @@ static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *
inv_det = 1.0f / det;

/* calculate distance from v1 to ray origin */
-       sub_v3_v3v3(tvec, orig, v1);
+       sub_v3_v3v3(tvec, orig, tri[0]);

/* calculate U parameter and test bounds */
u = dot_v3v3(tvec, pvec) * inv_det;
@@ -152,10 +143,15 @@ static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *
/* calculate t, ray intersects triangle */
t = dot_v3v3(e2, qvec) * inv_det;

+       /* ray hit behind */
+       if (t < 0.0f) {
+               Py_RETURN_NONE;
+       }
+
mul_v3_fl(dir, t);

-       return Vector_CreatePyObject(pvec, 3, Py_NEW, NULL);
+       return Vector_CreatePyObject(pvec, 3, NULL);
}

/* Line-Line intersection using algorithm from mathworld.wolfram.com */
@@ -177,168 +173,169 @@ PyDoc_STRVAR(M_Geometry_intersect_line_line_doc,
);
static PyObject *M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject *args)
{
+       const char *error_prefix = "intersect_line_line";
PyObject *tuple;
-       VectorObject *vec1, *vec2, *vec3, *vec4;
-       float v1[3], v2[3], v3[3], v4[3], i1[3], i2[3];
-
-       if (!PyArg_ParseTuple(args, "O!O!O!O!:intersect_line_line",
-                             &vector_Type, &vec1,
-                             &vector_Type, &vec2,
-                             &vector_Type, &vec3,
-                             &vector_Type, &vec4))
+       PyObject *py_lines[4];
+       float lines[4][3], i1[3], i2[3];
+       int len;
+       int result;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOO:intersect_line_line",
+               UNPACK4_EX(&, py_lines, )))
{
return NULL;
}

-       if (vec1->size != vec2->size || vec1->size != vec3->size || vec3->size != vec2->size) {
-               PyErr_SetString(PyExc_ValueError,
-                               "vectors must be of the same size");
+       if ((((len = mathutils_array_parse(lines[0], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[0], error_prefix)) != -1) &&
+            (mathutils_array_parse(lines[1], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[1], error_prefix) != -1) &&
+            (mathutils_array_parse(lines[2], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[2], error_prefix) != -1) &&
+            (mathutils_array_parse(lines[3], len, len | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_lines[3], error_prefix) != -1)) == 0)
+       {
return NULL;
}

-       if (BaseMath_ReadCallback(vec1) == -1 ||
+       result = isect_line_line_v3(UNPACK4(lines), i1, i2);
+       /* The return-code isnt exposed,
+        * this way we can check know how close the lines are. */
+       if (result == 1) {
+               closest_to_line_v3(i2, i1, lines[2], lines[3]);
+       }
+
+       if (result == 0) {
+               /* collinear */
+               Py_RETURN_NONE;
+       }
+       else {
+               tuple = PyTuple_New(2);
+               PyTuple_SET_ITEMS(tuple,
+                       Vector_CreatePyObject(i1, len, NULL),
+                       Vector_CreatePyObject(i2, len, NULL));
+               return tuple;
+       }
+}
+
+/* Line-Line intersection using algorithm from mathworld.wolfram.com */
+
+PyDoc_STRVAR(M_Geometry_intersect_sphere_sphere_2d_doc,
+"\n"
+"   Returns 2 points on between intersecting circles.\n"
+"\n"
+"   :arg p_a: Center of the first circle\n"
+"   :type p_a: :class:`mathutils.Vector`\n"
+"   :arg p_b: Center of the second circle\n"
+"   :type p_b: :class:`mathutils.Vector`\n"
+"   :rtype: tuple of :class:`mathutils.Vector`'s or None when there is no intersection\n"
+);
+static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), PyObject *args)
+{
+       const char *error_prefix = "intersect_sphere_sphere_2d";
+       PyObject *ret;
+       PyObject *py_v_a, *py_v_b;
+       float v_a[2], v_b[2];
+       float v_ab[2];
+       float dist;
+
+       if (!PyArg_ParseTuple(
+               args, "OfOf:intersect_sphere_sphere_2d",
{
return NULL;
}

-       if (vec1->size == 3 || vec1->size == 2) {
-               int result;
+       if (((mathutils_array_parse(v_a, 2, 2, py_v_a, error_prefix) != -1) &&
+            (mathutils_array_parse(v_b, 2, 2, py_v_b, error_prefix) != -1)) == 0)
+       {
+               return NULL;
+       }

-               if (vec1->size == 3) {
-                       copy_v3_v3(v1, vec1->vec);
-                       copy_v3_v3(v2, vec2->vec);
-                       copy_v3_v3(v3, vec3->vec);
-                       copy_v3_v3(v4, vec4->vec);
-               }
-               else {
-                       v1[0] = vec1->vec[0];
-                       v1[1] = vec1->vec[1];
-                       v1[2] = 0.0f;
-
-                       v2[0] = vec2->vec[0];
-                       v2[1] = vec2->vec[1];
-                       v2[2] = 0.0f;
-
-                       v3[0] = vec3->vec[0];
-                       v3[1] = vec3->vec[1];
-                       v3[2] = 0.0f;
-
-                       v4[0] = vec4->vec[0];
-                       v4[1] = vec4->vec[1];
-                       v4[2] = 0.0f;
-               }
+       ret = PyTuple_New(2);

-               result = isect_line_line_v3(v1, v2, v3, v4, i1, i2);
+       sub_v2_v2v2(v_ab, v_b, v_a);
+       dist = len_v2(v_ab);

-               if (result == 0) {
-                       /* colinear */
-                       Py_RETURN_NONE;
-               }
-               else {
-                       tuple = PyTuple_New(2);
-                       PyTuple_SET_ITEM(tuple, 0, Vector_CreatePyObject(i1, vec1->size, Py_NEW, NULL));
-                       PyTuple_SET_ITEM(tuple, 1, Vector_CreatePyObject(i2, vec1->size, Py_NEW, NULL));
-                       return tuple;
-               }
+       if (/* out of range */
+           /* fully-contained in the other */
+           /* co-incident */
+           (dist < FLT_EPSILON))
+       {
+               /* out of range */
+               PyTuple_SET_ITEMS(ret,
+                       Py_INCREF_RET(Py_None),
+                       Py_INCREF_RET(Py_None));
}
else {
-               PyErr_SetString(PyExc_ValueError,
-                               "2D/3D vectors only");
-               return NULL;
-       }
-}
+               const float h = powf(fabsf((rad_a * rad_a) - (dist_delta * dist_delta)), 0.5f);
+               float i_cent[2];
+               float i1[2], i2[2];
+
+               i_cent[0] = v_a[0] + ((v_ab[0] * dist_delta) / dist);
+               i_cent[1] = v_a[1] + ((v_ab[1] * dist_delta) / dist);

+               i1[0] = i_cent[0] + h * v_ab[1] / dist;
+               i1[1] = i_cent[1] - h * v_ab[0] / dist;

+               i2[0] = i_cent[0] - h * v_ab[1] / dist;
+               i2[1] = i_cent[1] + h * v_ab[0] / dist;
+
+               PyTuple_SET_ITEMS(ret,
+                       Vector_CreatePyObject(i1, 2, NULL),
+                       Vector_CreatePyObject(i2, 2, NULL));
+       }

+       return ret;
+}

-//----------------------------geometry.normal() -------------------
PyDoc_STRVAR(M_Geometry_normal_doc,
-".. function:: normal(v1, v2, v3, v4=None)\n"
+".. function:: normal(vectors)\n"
"\n"
-"   Returns the normal of the 3D tri or quad.\n"
+"   Returns the normal of a 3D polygon.\n"
"\n"
-"   :arg v1: Point1\n"
-"   :type v1: :class:`mathutils.Vector`\n"
-"   :arg v2: Point2\n"
-"   :type v2: :class:`mathutils.Vector`\n"
-"   :arg v3: Point3\n"
-"   :type v3: :class:`mathutils.Vector`\n"
-"   :arg v4: Point4 (optional)\n"
-"   :type v4: :class:`mathutils.Vector`\n"
+"   :arg vectors: Vectors to calculate normals with\n"
+"   :type vectors: sequence of 3 or more 3d vector\n"
"   :rtype: :class:`mathutils.Vector`\n"
);
static PyObject *M_Geometry_normal(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *vec1, *vec2, *vec3, *vec4;
+       float (*coords)[3];
+       int coords_len;
float n[3];
+       PyObject *ret = NULL;

-       if (PyTuple_GET_SIZE(args) == 3) {
-               if (!PyArg_ParseTuple(args, "O!O!O!:normal",
-                                     &vector_Type, &vec1,
-                                     &vector_Type, &vec2,
-                                     &vector_Type, &vec3))
-               {
-                       return NULL;
-               }
-
-               if (vec1->size != vec2->size || vec1->size != vec3->size) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "vectors must be of the same size");
-                       return NULL;
-               }
-               if (vec1->size < 3) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "2D vectors unsupported");
-                       return NULL;
-               }
-
-               if (BaseMath_ReadCallback(vec1) == -1 ||
-               {
-                       return NULL;
-               }
-
-               normal_tri_v3(n, vec1->vec, vec2->vec, vec3->vec);
+       /* use */
+       if (PyTuple_GET_SIZE(args) == 1) {
+               args = PyTuple_GET_ITEM(args, 0);
}
-       else {
-               if (!PyArg_ParseTuple(args, "O!O!O!O!:normal",
-                                     &vector_Type, &vec1,
-                                     &vector_Type, &vec2,
-                                     &vector_Type, &vec3,
-                                     &vector_Type, &vec4))
-               {
-                       return NULL;
-               }
-               if (vec1->size != vec2->size || vec1->size != vec3->size || vec1->size != vec4->size) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "vectors must be of the same size");
-                       return NULL;
-               }
-               if (vec1->size < 3) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "2D vectors unsupported");
-                       return NULL;
-               }

-               if (BaseMath_ReadCallback(vec1) == -1 ||
-               {
-                       return NULL;
-               }
+       if ((coords_len = mathutils_array_parse_alloc_v((float **)&coords, 3 | MU_ARRAY_SPILL, args, "normal")) == -1) {
+               return NULL;
+       }

-               normal_quad_v3(n, vec1->vec, vec2->vec, vec3->vec, vec4->vec);
+       if (coords_len < 3) {
+               PyErr_SetString(PyExc_ValueError,
+                               "Expected 3 or more vectors");
+               goto finally;
}

-       return Vector_CreatePyObject(n, 3, Py_NEW, NULL);
+       normal_poly_v3(n, (const float (*)[3])coords, coords_len);
+       ret = Vector_CreatePyObject(n, 3, NULL);
+
+finally:
+       PyMem_Free(coords);
+       return ret;
}

-//--------------------------------- AREA FUNCTIONS--------------------
+/* --------------------------------- AREA FUNCTIONS-------------------- */

PyDoc_STRVAR(M_Geometry_area_tri_doc,
".. function:: area_tri(v1, v2, v3)\n"
@@ -355,47 +352,72 @@ PyDoc_STRVAR(M_Geometry_area_tri_doc,
);
static PyObject *M_Geometry_area_tri(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *vec1, *vec2, *vec3;
-
-       if (!PyArg_ParseTuple(args, "O!O!O!:area_tri",
-                             &vector_Type, &vec1,
-                             &vector_Type, &vec2,
-                             &vector_Type, &vec3))
+       const char *error_prefix = "area_tri";
+       PyObject *py_tri[3];
+       float tri[3][3];
+       int len;
+
+       if (!PyArg_ParseTuple(
+               args, "OOO:area_tri",
+               UNPACK3_EX(&, py_tri, )))
{
return NULL;
}

-       if (vec1->size != vec2->size || vec1->size != vec3->size) {
-               PyErr_SetString(PyExc_ValueError,
-                               "vectors must be of the same size");
+       if ((((len = mathutils_array_parse(tri[0], 2, 3, py_tri[0], error_prefix)) != -1) &&
+            (mathutils_array_parse(tri[1], len, len, py_tri[1], error_prefix) != -1) &&
+            (mathutils_array_parse(tri[2], len, len, py_tri[2], error_prefix) != -1)) == 0)
+       {
return NULL;
}

-       if (BaseMath_ReadCallback(vec1) == -1 ||
+       return PyFloat_FromDouble((len == 3 ? area_tri_v3 : area_tri_v2)(UNPACK3(tri)));
+}
+
+PyDoc_STRVAR(M_Geometry_volume_tetrahedron_doc,
+".. function:: volume_tetrahedron(v1, v2, v3, v4)\n"
+"\n"
+"   Return the volume formed by a tetrahedron (points can be in any order).\n"
+"\n"
+"   :arg v1: Point1\n"
+"   :type v1: :class:`mathutils.Vector`\n"
+"   :arg v2: Point2\n"
+"   :type v2: :class:`mathutils.Vector`\n"
+"   :arg v3: Point3\n"
+"   :type v3: :class:`mathutils.Vector`\n"
+"   :arg v4: Point4\n"
+"   :type v4: :class:`mathutils.Vector`\n"
+"   :rtype: float\n"
+);
+static PyObject *M_Geometry_volume_tetrahedron(PyObject *UNUSED(self), PyObject *args)
+{
+       const char *error_prefix = "volume_tetrahedron";
+       PyObject *py_tet[4];
+       float tet[4][3];
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOO:volume_tetrahedron",
+               UNPACK4_EX(&, py_tet, )))
{
return NULL;
}

-       if (vec1->size == 3) {
-               return PyFloat_FromDouble(area_tri_v3(vec1->vec, vec2->vec, vec3->vec));
-       }
-       else if (vec1->size == 2) {
-               return PyFloat_FromDouble(area_tri_v2(vec1->vec, vec2->vec, vec3->vec));
-       }
-       else {
-               PyErr_SetString(PyExc_ValueError,
-                               "only 2D,3D vectors are supported");
-               return NULL;
+       for (i = 0; i < ARRAY_SIZE(tet); i++) {
+               if (mathutils_array_parse(tet[i], 3, 3 | MU_ARRAY_SPILL, py_tet[i], error_prefix) == -1) {
+                       return NULL;
+               }
}
-}

+       return PyFloat_FromDouble(volume_tetrahedron_v3(UNPACK4(tet)));
+}

PyDoc_STRVAR(M_Geometry_intersect_line_line_2d_doc,
".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2)\n"
"\n"
-"   Takes 2 lines (as 4 vectors) and returns a vector for their point of intersection or None.\n"
+"   Takes 2 segments (defined by 4 vectors) and returns a vector for their point of intersection or None.\n"
+"\n"
+"   .. warning:: Despite its name, this function works on segments, and not on lines.\n"
"\n"
"   :arg lineA_p1: First point of the first line\n"
"   :type lineA_p1: :class:`mathutils.Vector`\n"
@@ -410,27 +432,27 @@ PyDoc_STRVAR(M_Geometry_intersect_line_line_2d_doc,
);
static PyObject *M_Geometry_intersect_line_line_2d(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *line_a1, *line_a2, *line_b1, *line_b2;
+       const char *error_prefix = "intersect_line_line_2d";
+       PyObject *py_lines[4];
+       float lines[4][2];
float vi[2];
-       if (!PyArg_ParseTuple(args, "O!O!O!O!:intersect_line_line_2d",
-                             &vector_Type, &line_a1,
-                             &vector_Type, &line_a2,
-                             &vector_Type, &line_b1,
-                             &vector_Type, &line_b2))
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOO:intersect_line_line_2d",
+               UNPACK4_EX(&, py_lines, )))
{
return NULL;
}
-
-       if (BaseMath_ReadCallback(line_a1) == -1 ||
-       {
-               return NULL;
+
+       for (i = 0; i < ARRAY_SIZE(lines); i++) {
+               if (mathutils_array_parse(lines[i], 2, 2 | MU_ARRAY_SPILL, py_lines[i], error_prefix) == -1) {
+                       return NULL;
+               }
}

-       if (isect_seg_seg_v2_point(line_a1->vec, line_a2->vec, line_b1->vec, line_b2->vec, vi) == 1) {
-               return Vector_CreatePyObject(vi, 2, Py_NEW, NULL);
+       if (isect_seg_seg_v2_point(UNPACK4(lines), vi) == 1) {
+               return Vector_CreatePyObject(vi, 2, NULL);
}
else {
Py_RETURN_NONE;
@@ -452,43 +474,36 @@ PyDoc_STRVAR(M_Geometry_intersect_line_plane_doc,
"   :type plane_co: :class:`mathutils.Vector`\n"
"   :arg plane_no: The direction the plane is facing\n"
"   :type plane_no: :class:`mathutils.Vector`\n"
-"   :arg no_flip: Always return an intersection on the directon defined bt line_a -> line_b\n"
-"   :type no_flip: :boolean\n"
"   :return: The point of intersection or None when not found\n"
"   :rtype: :class:`mathutils.Vector` or None\n"
);
static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *line_a, *line_b, *plane_co, *plane_no;
-       int no_flip = 0;
+       const char *error_prefix = "intersect_line_plane";
+       PyObject *py_line_a, *py_line_b, *py_plane_co, *py_plane_no;
+       float line_a[3], line_b[3], plane_co[3], plane_no[3];
float isect[3];
-       if (!PyArg_ParseTuple(args, "O!O!O!O!|i:intersect_line_plane",
-                             &vector_Type, &line_a,
-                             &vector_Type, &line_b,
-                             &vector_Type, &plane_co,
-                             &vector_Type, &plane_no,
-                             &no_flip))
-       {
-               return NULL;
-       }
+       bool no_flip = false;

-       if (BaseMath_ReadCallback(line_a) == -1 ||
+       if (!PyArg_ParseTuple(
+               args, "OOOO|O&:intersect_line_plane",
+               &py_line_a, &py_line_b, &py_plane_co, &py_plane_no,
+               PyC_ParseBool, &no_flip))
{
return NULL;
}

-       if (ELEM4(2, line_a->size, line_b->size, plane_co->size, plane_no->size)) {
-               PyErr_SetString(PyExc_ValueError,
-                               "geometry.intersect_line_plane(...): "
-                               " can't use 2D Vectors");
+       if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
+            (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) != -1)) == 0)
+       {
return NULL;
}

-       if (isect_line_plane_v3(isect, line_a->vec, line_b->vec, plane_co->vec, plane_no->vec, no_flip) == 1) {
-               return Vector_CreatePyObject(isect, 3, Py_NEW, NULL);
+       /* TODO: implements no_flip */
+       if (isect_line_plane_v3(isect, line_a, line_b, plane_co, plane_no) == 1) {
+               return Vector_CreatePyObject(isect, 3, NULL);
}
else {
Py_RETURN_NONE;
@@ -509,61 +524,67 @@ PyDoc_STRVAR(M_Geometry_intersect_plane_plane_doc,
"   :arg plane_b_no: Normal of the second plane\n"
"   :type plane_b_no: :class:`mathutils.Vector`\n"
"   :return: The line of the intersection represented as a point and a vector\n"
-"   :rtype: tuple pair of :class:`mathutils.Vector`\n"
+"   :rtype: tuple pair of :class:`mathutils.Vector` or None if the intersection can't be calculated\n"
);
static PyObject *M_Geometry_intersect_plane_plane(PyObject *UNUSED(self), PyObject *args)
{
-       PyObject *ret;
-       VectorObject *plane_a_co, *plane_a_no, *plane_b_co, *plane_b_no;
+       const char *error_prefix = "intersect_plane_plane";
+       PyObject *ret, *ret_co, *ret_no;
+       PyObject *py_plane_a_co, *py_plane_a_no, *py_plane_b_co, *py_plane_b_no;
+       float plane_a_co[3], plane_a_no[3], plane_b_co[3], plane_b_no[3];
+       float plane_a[4], plane_b[4];

float isect_co[3];
float isect_no[3];

-       if (!PyArg_ParseTuple(args, "O!O!O!O!|i:intersect_plane_plane",
-                             &vector_Type, &plane_a_co,
-                             &vector_Type, &plane_a_no,
-                             &vector_Type, &plane_b_co,
-                             &vector_Type, &plane_b_no))
+       if (!PyArg_ParseTuple(
+               args, "OOOO:intersect_plane_plane",
+               &py_plane_a_co, &py_plane_a_no, &py_plane_b_co, &py_plane_b_no))
{
return NULL;
}

-       if (BaseMath_ReadCallback(plane_a_co) == -1 ||
+       if (((mathutils_array_parse(plane_a_co, 3, 3 | MU_ARRAY_SPILL, py_plane_a_co, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_a_no, 3, 3 | MU_ARRAY_SPILL, py_plane_a_no, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_b_co, 3, 3 | MU_ARRAY_SPILL, py_plane_b_co, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_b_no, 3, 3 | MU_ARRAY_SPILL, py_plane_b_no, error_prefix) != -1)) == 0)
{
return NULL;
}

-       if (ELEM4(2, plane_a_co->size, plane_a_no->size, plane_b_co->size, plane_b_no->size)) {
-               PyErr_SetString(PyExc_ValueError,
-                               "geometry.intersect_plane_plane(...): "
-                               " can't use 2D Vectors");
-               return NULL;
-       }
+       plane_from_point_normal_v3(plane_a, plane_a_co, plane_a_no);
+       plane_from_point_normal_v3(plane_b, plane_b_co, plane_b_no);

-       isect_plane_plane_v3(isect_co, isect_no,
-                            plane_a_co->vec, plane_a_no->vec,
-                            plane_b_co->vec, plane_b_no->vec);
+       if (isect_plane_plane_v3(
+               plane_a, plane_b,
+               isect_co, isect_no))
+       {
+               normalize_v3(isect_no);

-       normalize_v3(isect_no);
+               ret_co = Vector_CreatePyObject(isect_co, 3, NULL);
+               ret_no = Vector_CreatePyObject(isect_no, 3, NULL);
+       }
+       else {
+               ret_co = Py_INCREF_RET(Py_None);
+               ret_no = Py_INCREF_RET(Py_None);
+       }

ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(isect_co, 3, Py_NEW, NULL));
-       PyTuple_SET_ITEM(ret, 1, Vector_CreatePyObject(isect_no, 3, Py_NEW, NULL));
+       PyTuple_SET_ITEMS(ret,
+               ret_co,
+               ret_no);
return ret;
}

PyDoc_STRVAR(M_Geometry_intersect_line_sphere_doc,
".. function:: intersect_line_sphere(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
"\n"
-"   Takes a lines (as 2 vectors), a sphere as a point and a radius and\n"
+"   Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
"   returns the intersection\n"
"\n"
-"   :arg line_a: First point of the first line\n"
+"   :arg line_a: First point of the line\n"
"   :type line_a: :class:`mathutils.Vector`\n"
-"   :arg line_b: Second point of the first line\n"
+"   :arg line_b: Second point of the line\n"
"   :type line_b: :class:`mathutils.Vector`\n"
"   :arg sphere_co: The center of the sphere\n"
"   :type sphere_co: :class:`mathutils.Vector`\n"
@@ -574,61 +595,54 @@ PyDoc_STRVAR(M_Geometry_intersect_line_sphere_doc,
);
static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *line_a, *line_b, *sphere_co;
+       const char *error_prefix = "intersect_line_sphere";
+       PyObject *py_line_a, *py_line_b, *py_sphere_co;
+       float line_a[3], line_b[3], sphere_co[3];
-       int clip = TRUE;
+       bool clip = true;

float isect_a[3];
float isect_b[3];

-       if (!PyArg_ParseTuple(args, "O!O!O!f|i:intersect_line_sphere",
-                             &vector_Type, &line_a,
-                             &vector_Type, &line_b,
-                             &vector_Type, &sphere_co,
+       if (!PyArg_ParseTuple(
+               args, "OOOf|O&:intersect_line_sphere",
+               PyC_ParseBool, &clip))
{
return NULL;
}

-       if (BaseMath_ReadCallback(line_a) == -1 ||
+       if (((mathutils_array_parse(line_a, 3, 3 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
+            (mathutils_array_parse(line_b, 3, 3 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
+            (mathutils_array_parse(sphere_co, 3, 3 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) != -1)) == 0)
{
return NULL;
}
-
-       if (ELEM3(2, line_a->size, line_b->size, sphere_co->size)) {
-               PyErr_SetString(PyExc_ValueError,
-                               "geometry.intersect_line_sphere(...): "
-                               " can't use 2D Vectors");
-               return NULL;
-       }
else {
-               short use_a = TRUE;
-               short use_b = TRUE;
+               bool use_a = true;
+               bool use_b = true;
float lambda;

PyObject *ret = PyTuple_New(2);

-               switch (isect_line_sphere_v3(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) {
+               switch (isect_line_sphere_v3(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
case 1:
-                               if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a = FALSE;
-                               use_b = FALSE;
+                               if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_a = false;
+                               use_b = false;
break;
case 2:
-                               if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a = FALSE;
-                               if (!(!clip || (((lambda = line_point_factor_v3(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b = FALSE;
+                               if (!(!clip || (((lambda = line_point_factor_v3(isect_a, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_a = false;
+                               if (!(!clip || (((lambda = line_point_factor_v3(isect_b, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_b = false;
break;
default:
-                               use_a = FALSE;
-                               use_b = FALSE;
+                               use_a = false;
+                               use_b = false;
+                               break;
}

-               if (use_a) { PyTuple_SET_ITEM(ret, 0,  Vector_CreatePyObject(isect_a, 3, Py_NEW, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 0,  Py_None); Py_INCREF(Py_None); }
-
-               if (use_b) { PyTuple_SET_ITEM(ret, 1,  Vector_CreatePyObject(isect_b, 3, Py_NEW, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 1,  Py_None); Py_INCREF(Py_None); }
+               PyTuple_SET_ITEMS(ret,
+                       use_a ? Vector_CreatePyObject(isect_a, 3, NULL) : Py_INCREF_RET(Py_None),
+                       use_b ? Vector_CreatePyObject(isect_b, 3, NULL) : Py_INCREF_RET(Py_None));

return ret;
}
@@ -638,12 +652,12 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje
PyDoc_STRVAR(M_Geometry_intersect_line_sphere_2d_doc,
".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, sphere_radius, clip=True)\n"
"\n"
-"   Takes a lines (as 2 vectors), a sphere as a point and a radius and\n"
+"   Takes a line (as 2 points) and a sphere (as a point and a radius) and\n"
"   returns the intersection\n"
"\n"
-"   :arg line_a: First point of the first line\n"
+"   :arg line_a: First point of the line\n"
"   :type line_a: :class:`mathutils.Vector`\n"
-"   :arg line_b: Second point of the first line\n"
+"   :arg line_b: Second point of the line\n"
"   :type line_b: :class:`mathutils.Vector`\n"
"   :arg sphere_co: The center of the sphere\n"
"   :type sphere_co: :class:`mathutils.Vector`\n"
@@ -654,54 +668,54 @@ PyDoc_STRVAR(M_Geometry_intersect_line_sphere_2d_doc,
);
static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *line_a, *line_b, *sphere_co;
+       const char *error_prefix = "intersect_line_sphere_2d";
+       PyObject *py_line_a, *py_line_b, *py_sphere_co;
+       float line_a[2], line_b[2], sphere_co[2];
-       int clip = TRUE;
+       bool clip = true;

-       float isect_a[3];
-       float isect_b[3];
+       float isect_a[2];
+       float isect_b[2];

-       if (!PyArg_ParseTuple(args, "O!O!O!f|i:intersect_line_sphere_2d",
-                             &vector_Type, &line_a,
-                             &vector_Type, &line_b,
-                             &vector_Type, &sphere_co,
+       if (!PyArg_ParseTuple(
+               args, "OOOf|O&:intersect_line_sphere_2d",
+               PyC_ParseBool, &clip))
{
return NULL;
}

-       if (BaseMath_ReadCallback(line_a) == -1 ||
+       if (((mathutils_array_parse(line_a, 2, 2 | MU_ARRAY_SPILL, py_line_a, error_prefix) != -1) &&
+            (mathutils_array_parse(line_b, 2, 2 | MU_ARRAY_SPILL, py_line_b, error_prefix) != -1) &&
+            (mathutils_array_parse(sphere_co, 2, 2 | MU_ARRAY_SPILL, py_sphere_co, error_prefix) != -1)) == 0)
{
return NULL;
}
else {
-               short use_a = TRUE;
-               short use_b = TRUE;
+               bool use_a = true;
+               bool use_b = true;
float lambda;

PyObject *ret = PyTuple_New(2);

-               switch (isect_line_sphere_v2(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) {
+               switch (isect_line_sphere_v2(line_a, line_b, sphere_co, sphere_radius, isect_a, isect_b)) {
case 1:
-                               if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a = FALSE;
-                               use_b = FALSE;
+                               if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_a = false;
+                               use_b = false;
break;
case 2:
-                               if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a = FALSE;
-                               if (!(!clip || (((lambda = line_point_factor_v2(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b = FALSE;
+                               if (!(!clip || (((lambda = line_point_factor_v2(isect_a, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_a = false;
+                               if (!(!clip || (((lambda = line_point_factor_v2(isect_b, line_a, line_b)) >= 0.0f) && (lambda <= 1.0f)))) use_b = false;
break;
default:
-                               use_a = FALSE;
-                               use_b = FALSE;
+                               use_a = false;
+                               use_b = false;
+                               break;
}

-               if (use_a) { PyTuple_SET_ITEM(ret, 0,  Vector_CreatePyObject(isect_a, 2, Py_NEW, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 0,  Py_None); Py_INCREF(Py_None); }
-
-               if (use_b) { PyTuple_SET_ITEM(ret, 1,  Vector_CreatePyObject(isect_b, 2, Py_NEW, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 1,  Py_None); Py_INCREF(Py_None); }
+               PyTuple_SET_ITEMS(ret,
+                       use_a ? Vector_CreatePyObject(isect_a, 2, NULL) : Py_INCREF_RET(Py_None),
+                       use_b ? Vector_CreatePyObject(isect_b, 2, NULL) : Py_INCREF_RET(Py_None));

return ret;
}
@@ -722,53 +736,93 @@ PyDoc_STRVAR(M_Geometry_intersect_point_line_doc,
);
static PyObject *M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *pt, *line_1, *line_2;
-       float pt_in[3], pt_out[3], l1[3], l2[3];
+       const char *error_prefix = "intersect_point_line";
+       PyObject *py_pt, *py_line_a, *py_line_b;
+       float pt[3], pt_out[3], line_a[3], line_b[3];
float lambda;
PyObject *ret;
int size = 2;
-
-       if (!PyArg_ParseTuple(args, "O!O!O!:intersect_point_line",
-                             &vector_Type, &pt,
-                             &vector_Type, &line_1,
-                             &vector_Type, &line_2))
+
+       if (!PyArg_ParseTuple(
+               args, "OOO:intersect_point_line",
+               &py_pt, &py_line_a, &py_line_b))
{
return NULL;
}

-       if (BaseMath_ReadCallback(pt) == -1 ||
+       /* accept 2d verts */
+       if ((((size = mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix)) != -1) &&
+            (mathutils_array_parse(line_a, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_a, error_prefix) != -1) &&
+            (mathutils_array_parse(line_b, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_line_b, error_prefix) != -1)) == 0)
{
return NULL;
}

-       /* accept 2d verts */
-       if (pt->size >= 3)     { copy_v3_v3(pt_in, pt->vec); size = 3; }
-       else                   { copy_v2_v2(pt_in, pt->vec); pt_in[2] = 0.0f; }
-
-       if (line_1->size >= 3) { copy_v3_v3(l1, line_1->vec); size = 3; }
-       else                   { copy_v2_v2(l1, line_1->vec); l1[2] = 0.0f; }
-
-       if (line_2->size >= 3) { copy_v3_v3(l2, line_2->vec); size = 3; }
-       else                   { copy_v2_v2(l2, line_2->vec); l2[2] = 0.0f; }
-
/* do the calculation */
-       lambda = closest_to_line_v3(pt_out, pt_in, l1, l2);
-
+       lambda = closest_to_line_v3(pt_out, pt, line_a, line_b);
+
ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(pt_out, size, Py_NEW, NULL));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(lambda));
+       PyTuple_SET_ITEMS(ret,
+               Vector_CreatePyObject(pt_out, size, NULL),
+               PyFloat_FromDouble(lambda));
return ret;
}

+PyDoc_STRVAR(M_Geometry_intersect_point_tri_doc,
+".. function:: intersect_point_tri(pt, tri_p1, tri_p2, tri_p3)\n"
+"\n"
+"   Takes 4 vectors: one is the point and the next 3 define the triangle.\n"
+"\n"
+"   :arg pt: Point\n"
+"   :type pt: :class:`mathutils.Vector`\n"
+"   :arg tri_p1: First point of the triangle\n"
+"   :type tri_p1: :class:`mathutils.Vector`\n"
+"   :arg tri_p2: Second point of the triangle\n"
+"   :type tri_p2: :class:`mathutils.Vector`\n"
+"   :arg tri_p3: Third point of the triangle\n"
+"   :type tri_p3: :class:`mathutils.Vector`\n"
+"   :return: Point on the triangles plane or None if its outside the triangle\n"
+"   :rtype: :class:`mathutils.Vector` or None\n"
+);
+static PyObject *M_Geometry_intersect_point_tri(PyObject *UNUSED(self), PyObject *args)
+{
+       const char *error_prefix = "intersect_point_tri";
+       PyObject *py_pt, *py_tri[3];
+       float pt[3], tri[3][3];
+       float vi[3];
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOO:intersect_point_tri",
+               &py_pt, UNPACK3_EX(&, py_tri, )))
+       {
+               return NULL;
+       }
+
+       if (mathutils_array_parse(pt, 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_pt, error_prefix) == -1) {
+               return NULL;
+       }
+       for (i = 0; i < ARRAY_SIZE(tri); i++) {
+               if (mathutils_array_parse(tri[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_tri[i], error_prefix) == -1) {
+                       return NULL;
+               }
+       }
+
+       if (isect_point_tri_v3(pt, UNPACK3(tri), vi)) {
+               return Vector_CreatePyObject(vi, 3, NULL);
+       }
+       else {
+               Py_RETURN_NONE;
+       }
+}
+
PyDoc_STRVAR(M_Geometry_intersect_point_tri_2d_doc,
".. function:: intersect_point_tri_2d(pt, tri_p1, tri_p2, tri_p3)\n"
"\n"
"   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"
"\n"
"   :arg pt: Point\n"
-"   :type v1: :class:`mathutils.Vector`\n"
+"   :type pt: :class:`mathutils.Vector`\n"
"   :arg tri_p1: First point of the triangle\n"
"   :type tri_p1: :class:`mathutils.Vector`\n"
"   :arg tri_p2: Second point of the triangle\n"
@@ -779,26 +833,28 @@ PyDoc_STRVAR(M_Geometry_intersect_point_tri_2d_doc,
);
static PyObject *M_Geometry_intersect_point_tri_2d(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *pt_vec, *tri_p1, *tri_p2, *tri_p3;
-
-       if (!PyArg_ParseTuple(args, "O!O!O!O!:intersect_point_tri_2d",
-                             &vector_Type, &pt_vec,
-                             &vector_Type, &tri_p1,
-                             &vector_Type, &tri_p2,
-                             &vector_Type, &tri_p3))
+       const char *error_prefix = "intersect_point_tri_2d";
+       PyObject *py_pt, *py_tri[3];
+       float pt[2], tri[3][2];
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOO:intersect_point_tri_2d",
+               &py_pt, UNPACK3_EX(&, py_tri, )))
{
return NULL;
}
-
-       if (BaseMath_ReadCallback(pt_vec) == -1 ||
-       {
+
+       if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
return NULL;
}
+       for (i = 0; i < ARRAY_SIZE(tri); i++) {
+               if (mathutils_array_parse(tri[i], 2, 2 | MU_ARRAY_SPILL, py_tri[i], error_prefix) == -1) {
+                       return NULL;
+               }
+       }

-       return PyLong_FromLong(isect_point_tri_v2(pt_vec->vec, tri_p1->vec, tri_p2->vec, tri_p3->vec));
+       return PyLong_FromLong(isect_point_tri_v2(pt, UNPACK3(tri)));
}

"\n"
"   Takes 5 vectors (using only the x and y coordinates): one is the point and the next 4 define the quad, \n"
"   only the x and y are used from the vectors. Returns 1 if the point is within the quad, otherwise 0.\n"
-"   Works only with convex quads without singular edges."
+"   Works only with convex quads without singular edges.\n"
"\n"
"   :arg pt: Point\n"
"   :type pt: :class:`mathutils.Vector`\n"
"   :rtype: int\n"
);
static PyObject *M_Geometry_intersect_point_quad_2d(PyObject *UNUSED(self), PyObject *args)
{
-
-                             &vector_Type, &pt_vec,
+       const char *error_prefix = "intersect_point_quad_2d";
+       int i;
+
+       if (!PyArg_ParseTuple(
{
return NULL;
}

-       if (BaseMath_ReadCallback(pt_vec)  == -1 ||
-       {
+       if (mathutils_array_parse(pt, 2, 2 | MU_ARRAY_SPILL, py_pt, error_prefix) == -1) {
return NULL;
}
+       for (i = 0; i < ARRAY_SIZE(quad); i++) {
+               if (mathutils_array_parse(quad[i], 2, 2 | MU_ARRAY_SPILL, py_quad[i], error_prefix) == -1) {
+                       return NULL;
+               }
+       }

}

PyDoc_STRVAR(M_Geometry_distance_point_to_plane_doc,
@@ -854,32 +910,35 @@ PyDoc_STRVAR(M_Geometry_distance_point_to_plane_doc,
"\n"
"   :arg pt: Point\n"
"   :type pt: :class:`mathutils.Vector`\n"
-"   :arg plane_co: First point of the quad\n"
+"   :arg plane_co: A point on the plane\n"
"   :type plane_co: :class:`mathutils.Vector`\n"
-"   :arg plane_no: Second point of the quad\n"
+"   :arg plane_no: The direction the plane is facing\n"
"   :type plane_no: :class:`mathutils.Vector`\n"
"   :rtype: float\n"
);
static PyObject *M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *pt, *plene_co, *plane_no;
-
-       if (!PyArg_ParseTuple(args, "O!O!O!:distance_point_to_plane",
-                             &vector_Type, &pt,
-                             &vector_Type, &plene_co,
-                             &vector_Type, &plane_no))
+       const char *error_prefix = "distance_point_to_plane";
+       PyObject *py_pt, *py_plane_co, *py_plane_no;
+       float pt[3], plane_co[3], plane_no[3];
+       float plane[4];
+
+       if (!PyArg_ParseTuple(
+               args, "OOO:distance_point_to_plane",
+               &py_pt, &py_plane_co, &py_plane_no))
{
return NULL;
}

-       if (BaseMath_ReadCallback(pt) == -1 ||
+       if (((mathutils_array_parse(pt,       3, 3 | MU_ARRAY_SPILL, py_pt,       error_prefix) != -1) &&
+            (mathutils_array_parse(plane_co, 3, 3 | MU_ARRAY_SPILL, py_plane_co, error_prefix) != -1) &&
+            (mathutils_array_parse(plane_no, 3, 3 | MU_ARRAY_SPILL, py_plane_no, error_prefix) != -1)) == 0)
{
return NULL;
}

-       return PyFloat_FromDouble(dist_to_plane_v3(pt->vec, plene_co->vec, plane_no->vec));
+       plane_from_point_normal_v3(plane, plane_co, plane_no);
+       return PyFloat_FromDouble(dist_signed_to_plane_v3(pt, plane));
}

PyDoc_STRVAR(M_Geometry_barycentric_transform_doc,
@@ -895,52 +954,48 @@ PyDoc_STRVAR(M_Geometry_barycentric_transform_doc,
"   :type tri_a2: :class:`mathutils.Vector`\n"
"   :arg tri_a3: source triangle vertex.\n"
"   :type tri_a3: :class:`mathutils.Vector`\n"
-"   :arg tri_a1: target triangle vertex.\n"
-"   :type tri_a1: :class:`mathutils.Vector`\n"
-"   :arg tri_a2: target triangle vertex.\n"
-"   :type tri_a2: :class:`mathutils.Vector`\n"
-"   :arg tri_a3: target triangle vertex.\n"
-"   :type tri_a3: :class:`mathutils.Vector`\n"
+"   :arg tri_b1: target triangle vertex.\n"
+"   :type tri_b1: :class:`mathutils.Vector`\n"
+"   :arg tri_b2: target triangle vertex.\n"
+"   :type tri_b2: :class:`mathutils.Vector`\n"
+"   :arg tri_b3: target triangle vertex.\n"
+"   :type tri_b3: :class:`mathutils.Vector`\n"
"   :return: The transformed point\n"
"   :rtype: :class:`mathutils.Vector`'s\n"
);
static PyObject *M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *vec_pt;
-       VectorObject *vec_t1_tar, *vec_t2_tar, *vec_t3_tar;
-       VectorObject *vec_t1_src, *vec_t2_src, *vec_t3_src;
-       float vec[3];
-
-       if (!PyArg_ParseTuple(args, "O!O!O!O!O!O!O!:barycentric_transform",
-                             &vector_Type, &vec_pt,
-                             &vector_Type, &vec_t1_src,
-                             &vector_Type, &vec_t2_src,
-                             &vector_Type, &vec_t3_src,
-                             &vector_Type, &vec_t1_tar,
-                             &vector_Type, &vec_t2_tar,
-                             &vector_Type, &vec_t3_tar))
+       const char *error_prefix = "barycentric_transform";
+       PyObject *py_pt_src, *py_tri_src[3], *py_tri_dst[3];
+       float pt_src[3], pt_dst[3], tri_src[3][3], tri_dst[3][3];
+       int i;
+
+       if (!PyArg_ParseTuple(
+               args, "OOOOOOO:barycentric_transform",
+               &py_pt_src,
+               UNPACK3_EX(&, py_tri_src, ),
+               UNPACK3_EX(&, py_tri_dst, )))
{
return NULL;
}

-       if (vec_pt->size != 3 ||
-           vec_t1_src->size != 3 ||
-           vec_t2_src->size != 3 ||
-           vec_t3_src->size != 3 ||
-           vec_t1_tar->size != 3 ||
-           vec_t2_tar->size != 3 ||
-           vec_t3_tar->size != 3)
-       {
-               PyErr_SetString(PyExc_ValueError,
-                               "One of more of the vector arguments wasn't a 3D vector");
+       if (mathutils_array_parse(pt_src, 3, 3 | MU_ARRAY_SPILL, py_pt_src, error_prefix) == -1) {
return NULL;
}
+       for (i = 0; i < ARRAY_SIZE(tri_src); i++) {
+               if (((mathutils_array_parse(tri_src[i], 3, 3 | MU_ARRAY_SPILL, py_tri_src[i], error_prefix) != -1) &&
+                    (mathutils_array_parse(tri_dst[i], 3, 3 | MU_ARRAY_SPILL, py_tri_dst[i], error_prefix) != -1)) == 0)
+               {
+                       return NULL;
+               }
+       }

-       barycentric_transform(vec, vec_pt->vec,
-                             vec_t1_tar->vec, vec_t2_tar->vec, vec_t3_tar->vec,
-                             vec_t1_src->vec, vec_t2_src->vec, vec_t3_src->vec);
+       transform_point_by_tri_v3(
+               pt_dst, pt_src,
+               UNPACK3(tri_dst),
+               UNPACK3(tri_src));

-       return Vector_CreatePyObject(vec, 3, Py_NEW, NULL);
+       return Vector_CreatePyObject(pt_dst, 3, NULL);
}

PyDoc_STRVAR(M_Geometry_points_in_planes_doc,
@@ -950,7 +1005,7 @@ PyDoc_STRVAR(M_Geometry_points_in_planes_doc,
"\n"
"   :arg planes: List of planes (4D vectors).\n"
"   :type planes: list of :class:`mathutils.Vector`\n"
-"   :return: two lists, once containing the vertices inside the planes, another containing the plane indicies used\n"
+"   :return: two lists, once containing the vertices inside the planes, another containing the plane indices used\n"
"   :rtype: pair of lists\n"
);
/* note: this function could be optimized by some spatial structure */
@@ -960,8 +1015,9 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a
float (*planes)[4];
unsigned int planes_len;

-       if (!PyArg_ParseTuple(args, "O:points_in_planes",
-                             &py_planes))
+       if (!PyArg_ParseTuple(
+               args, "O:points_in_planes",
+               &py_planes))
{
return NULL;
}
@@ -977,11 +1033,13 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a

float n1n2[3], n2n3[3], n3n1[3];
float potentialVertex[3];
-               char *planes_used = MEM_callocN(sizeof(char) * len, __func__);
+               char *planes_used = PyMem_Malloc(sizeof(char) * len);

/* python */
PyObject *py_verts = PyList_New(0);
-               PyObject *py_plene_index = PyList_New(0);
+               PyObject *py_plane_index = PyList_New(0);
+
+               memset(planes_used, 0, sizeof(char) * len);

for (i = 0; i < len; i++) {
const float *N1 = planes[i];
@@ -1011,11 +1069,8 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a

if (l == len) { /* ok */
/* python */
-                                                                               PyObject *item = Vector_CreatePyObject(potentialVertex, 3, Py_NEW, NULL);
-                                                                               PyList_Append(py_verts, item);
-                                                                               Py_DECREF(item);
-
-                                                                               planes_used[i] = planes_used[j] = planes_used[k] = TRUE;
+                                                                               PyList_APPEND(py_verts, Vector_CreatePyObject(potentialVertex, 3, NULL));
+                                                                               planes_used[i] = planes_used[j] = planes_used[k] = true;
}
}
}
@@ -1030,17 +1085,16 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a
/* now make a list of used planes */
for (i = 0; i < len; i++) {
if (planes_used[i]) {
-                               PyObject *item = PyLong_FromLong(i);
-                               PyList_Append(py_plene_index, item);
-                               Py_DECREF(item);
+                               PyList_APPEND(py_plane_index, PyLong_FromLong(i));
}
}
-               MEM_freeN(planes_used);
+               PyMem_Free(planes_used);

{
PyObject *ret = PyTuple_New(2);
-                       PyTuple_SET_ITEM(ret, 0, py_verts);
-                       PyTuple_SET_ITEM(ret, 1, py_plene_index);
+                       PyTuple_SET_ITEMS(ret,
+                               py_verts,
+                               py_plane_index);
return ret;
}
}
@@ -1068,58 +1122,45 @@ PyDoc_STRVAR(M_Geometry_interpolate_bezier_doc,
);
static PyObject *M_Geometry_interpolate_bezier(PyObject *UNUSED(self), PyObject *args)
{
-       VectorObject *vec_k1, *vec_h1, *vec_k2, *vec_h2;
+       const char *error_prefix = "interpolate_bezier";
+       PyObject *py_data[4];
+       float data[4][4] = {{0.0f}};
int resolu;
-       int dims;
+       int dims = 0;
int i;
float *coord_array, *fp;
PyObject *list;

-       float k1[4] = {0.0, 0.0, 0.0, 0.0};
-       float h1[4] = {0.0, 0.0, 0.0, 0.0};
-       float k2[4] = {0.0, 0.0, 0.0, 0.0};
-       float h2[4] = {0.0, 0.0, 0.0, 0.0};
-
-
-       if (!PyArg_ParseTuple(args, "O!O!O!O!i:interpolate_bezier",
-                             &vector_Type, &vec_k1,
-                             &vector_Type, &vec_h1,
-                             &vector_Type, &vec_h2,
-                             &vector_Type, &vec_k2, &resolu))
+       if (!PyArg_ParseTuple(
+               args, "OOOOi:interpolate_bezier",
+               UNPACK4_EX(&, py_data, ), &resolu))
{
return NULL;
}

+       for (i = 0; i < 4; i++) {
+               int dims_tmp;
+               if ((dims_tmp = mathutils_array_parse(data[i], 2, 3 | MU_ARRAY_SPILL | MU_ARRAY_ZERO, py_data[i], error_prefix)) == -1) {
+                       return NULL;
+               }
+               dims = max_ii(dims, dims_tmp);
+       }
+
if (resolu <= 1) {
PyErr_SetString(PyExc_ValueError,
"resolution must be 2 or over");
return NULL;
}

-       if (BaseMath_ReadCallback(vec_k1) == -1 ||
-       {
-               return NULL;
-       }
-
-       dims = MAX4(vec_k1->size, vec_h1->size, vec_h2->size, vec_k2->size);
-
-       for (i = 0; i < vec_k1->size; i++) k1[i] = vec_k1->vec[i];
-       for (i = 0; i < vec_h1->size; i++) h1[i] = vec_h1->vec[i];
-       for (i = 0; i < vec_k2->size; i++) k2[i] = vec_k2->vec[i];
-       for (i = 0; i < vec_h2->size; i++) h2[i] = vec_h2->vec[i];
-
-       coord_array = MEM_callocN(dims * (resolu) * sizeof(float), "interpolate_bezier");
+       coord_array = MEM_callocN(dims * (resolu) * sizeof(float), error_prefix);
for (i = 0; i < dims; i++) {
-               BKE_curve_forward_diff_bezier(k1[i], h1[i], h2[i], k2[i], coord_array + i, resolu - 1, sizeof(float) * dims);
+               BKE_curve_forward_diff_bezier(UNPACK4_EX(, data, [i]), coord_array + i, resolu - 1, sizeof(float) * dims);
}

list = PyList_New(resolu);
fp = coord_array;
for (i = 0; i < resolu; i++, fp = fp + dims) {
-               PyList_SET_ITEM(list, i, Vector_CreatePyObject(fp, dims, Py_NEW, NULL));
+               PyList_SET_ITEM(list, i, Vector_CreatePyObject(fp, dims, NULL));
}
MEM_freeN(coord_array);
return list;
@@ -1220,7 +1261,8 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
}
else if (totpoints) {
/* now make the list to return */
-               BKE_displist_fill(&dispbase, &dispbase, 0);
+               /* TODO, add normal arg */
+               BKE_displist_fill(&dispbase, &dispbase, NULL, false);

/* The faces are stored in a new DisplayList
@@ -1237,7 +1279,7 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
index = 0;
dl_face = dl->index;
while (index < dl->parts) {
-                       PyList_SET_ITEM(tri_list, index, Py_BuildValue("iii", dl_face[0], dl_face[1], dl_face[2]));
+                       PyList_SET_ITEM(tri_list, index, PyC_Tuple_Pack_I32(dl_face[0], dl_face[1], dl_face[2]));
dl_face += 3;
index++;
}
@@ -1352,14 +1394,96 @@ static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlis
}

/* Non Python function */
-               BLI_box_pack_2D(boxarray, len, &tot_width, &tot_height);
+               BLI_box_pack_2d(boxarray, len, &tot_width, &tot_height);

boxPack_ToPyObject(boxlist, &boxarray);
}

ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(tot_width));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(tot_width));
+       PyTuple_SET_ITEMS(ret,
+               PyFloat_FromDouble(tot_width),
+               PyFloat_FromDouble(tot_height));
+       return ret;
+}
+
+PyDoc_STRVAR(M_Geometry_box_fit_2d_doc,
+".. function:: box_fit_2d(points)\n"
+"\n"
+"   Returns an angle that best fits the points to an axis aligned rectangle\n"
+"\n"
+"   :arg points: list of 2d points.\n"
+"   :type points: list\n"
+"   :return: angle\n"
+"   :rtype: float\n"
+);
+static PyObject *M_Geometry_box_fit_2d(PyObject *UNUSED(self), PyObject *pointlist)
+{
+       float (*points)[2];
+       Py_ssize_t len;
+
+       float angle = 0.0f;
+
+       len = mathutils_array_parse_alloc_v(((float **)&points), 2, pointlist, "box_fit_2d");
+       if (len == -1) {
+               return NULL;
+       }
+
+       if (len) {
+               /* Non Python function */
+               angle = BLI_convexhull_aabb_fit_points_2d(points, len);
+
+               PyMem_Free(points);
+       }
+
+
+       return PyFloat_FromDouble(angle);
+}
+
+PyDoc_STRVAR(M_Geometry_convex_hull_2d_doc,
+".. function:: convex_hull_2d(points)\n"
+"\n"
+"   Returns a list of indices into the list given\n"
+"\n"
+"   :arg points: list of 2d points.\n"
+"   :type points: list\n"
+"   :return: a list of indices\n"
+"   :rtype: list of ints\n"
+);
+static PyObject *M_Geometry_convex_hull_2d(PyObject *UNUSED(self), PyObject *pointlist)
+{
+       float (*points)[2];
+       Py_ssize_t len;
+
+       PyObject *ret;
+
+       len = mathutils_array_parse_alloc_v(((float **)&points), 2, pointlist, "convex_hull_2d");
+       if (len == -1) {
+               return NULL;
+       }
+
+       if (len) {
+               int *index_map;
+               Py_ssize_t len_ret, i;
+
+               index_map  = MEM_mallocN(sizeof(*index_map) * len * 2, __func__);
+
+               /* Non Python function */
+               len_ret = BLI_convexhull_2d(points, len, index_map);
+
+               ret = PyList_New(len_ret);
+               for (i = 0; i < len_ret; i++) {
+                       PyList_SET_ITEM(ret, i, PyLong_FromLong(index_map[i]));
+               }
+
+               MEM_freeN(index_map);
+
+               PyMem_Free(points);
+       }
+       else {
+               ret = PyList_New(0);
+       }
+
+
return ret;
}

@@ -1369,6 +1493,7 @@ static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlis
static PyMethodDef M_Geometry_methods[] = {
{"intersect_ray_tri", (PyCFunction) M_Geometry_intersect_ray_tri, METH_VARARGS, M_Geometry_intersect_ray_tri_doc},
{"intersect_point_line", (PyCFunction) M_Geometry_intersect_point_line, METH_VARARGS, M_Geometry_intersect_point_line_doc},
+       {"intersect_point_tri", (PyCFunction) M_Geometry_intersect_point_tri, METH_VARARGS, M_Geometry_intersect_point_tri_doc},
{"intersect_point_tri_2d", (PyCFunction) M_Geometry_intersect_point_tri_2d, METH_VARARGS, M_Geometry_intersect_point_tri_2d_doc},
{"intersect_line_line", (PyCFunction) M_Geometry_intersect_line_line, METH_VARARGS, M_Geometry_intersect_line_line_doc},
@@ -1378,13 +1503,17 @@ static PyMethodDef M_Geometry_methods[] = {
{"intersect_line_sphere", (PyCFunction) M_Geometry_intersect_line_sphere, METH_VARARGS, M_Geometry_intersect_line_sphere_doc},
{"intersect_line_sphere_2d", (PyCFunction) M_Geometry_intersect_line_sphere_2d, METH_VARARGS, M_Geometry_intersect_line_sphere_2d_doc},
{"distance_point_to_plane", (PyCFunction) M_Geometry_distance_point_to_plane, METH_VARARGS, M_Geometry_distance_point_to_plane_doc},
+       {"intersect_sphere_sphere_2d", (PyCFunction) M_Geometry_intersect_sphere_sphere_2d, METH_VARARGS, M_Geometry_intersect_sphere_sphere_2d_doc},
{"area_tri", (PyCFunction) M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_doc},
+       {"volume_tetrahedron", (PyCFunction) M_Geometry_volume_tetrahedron, METH_VARARGS, M_Geometry_volume_tetrahedron_doc},
{"normal", (PyCFunction) M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},
{"barycentric_transform", (PyCFunction) M_Geometry_barycentric_transform, METH_VARARGS, M_Geometry_barycentric_transform_doc},
{"points_in_planes", (PyCFunction) M_Geometry_points_in_planes, METH_VARARGS, M_Geometry_points_in_planes_doc},
#ifndef MATH_STANDALONE
{"interpolate_bezier", (PyCFunction) M_Geometry_interpolate_bezier, METH_VARARGS, M_Geometry_interpolate_bezier_doc},
{"tessellate_polygon", (PyCFunction) M_Geometry_tessellate_polygon, METH_O, M_Geometry_tessellate_polygon_doc},
+       {"convex_hull_2d", (PyCFunction) M_Geometry_convex_hull_2d, METH_O, M_Geometry_convex_hull_2d_doc},
+       {"box_fit_2d", (PyCFunction) M_Geometry_box_fit_2d, METH_O, M_Geometry_box_fit_2d_doc},
{"box_pack_2d", (PyCFunction) M_Geometry_box_pack_2d, METH_O, M_Geometry_box_pack_2d_doc},
#endif
{NULL, NULL, 0, NULL}