[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [56189] trunk/blender/source/blender: utility function volume_tetrahedron(), for mathutils and BLI math.

Campbell Barton ideasman42 at gmail.com
Sun Apr 21 15:24:45 CEST 2013


Revision: 56189
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=56189
Author:   campbellbarton
Date:     2013-04-21 13:24:45 +0000 (Sun, 21 Apr 2013)
Log Message:
-----------
utility function volume_tetrahedron(), for mathutils and BLI math.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_geom.h
    trunk/blender/source/blender/blenlib/intern/math_geom.c
    trunk/blender/source/blender/python/mathutils/mathutils_geometry.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_geom.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-04-21 13:10:05 UTC (rev 56188)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-04-21 13:24:45 UTC (rev 56189)
@@ -61,6 +61,8 @@
 float area_poly_v3(int nr, float verts[][3], const float normal[3]);
 float area_poly_v2(int nr, float verts[][2]);
 
+float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]);
+
 int is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]);
 int is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2]);
 

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-04-21 13:10:05 UTC (rev 56188)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-04-21 13:24:45 UTC (rev 56189)
@@ -183,6 +183,21 @@
 	return fabsf(0.5f * area);
 }
 
+/********************************* Volume **********************************/
+
+/**
+ * The volume from a tetrahedron, points can be in any order
+ */
+float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
+{
+	float m[3][3];
+	sub_v3_v3v3(m[0], v1, v2);
+	sub_v3_v3v3(m[1], v2, v3);
+	sub_v3_v3v3(m[2], v3, v4);
+	return fabsf(determinant_m3_array(m)) / 6.0f;
+}
+
+
 /********************************* Distance **********************************/
 
 /* distance p to line v1-v2

Modified: trunk/blender/source/blender/python/mathutils/mathutils_geometry.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_geometry.c	2013-04-21 13:10:05 UTC (rev 56188)
+++ trunk/blender/source/blender/python/mathutils/mathutils_geometry.c	2013-04-21 13:24:45 UTC (rev 56189)
@@ -467,7 +467,52 @@
 	}
 }
 
+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)
+{
+	VectorObject *vec1, *vec2, *vec3, *vec4;
 
+	if (!PyArg_ParseTuple(args, "O!O!O!O!:volume_tetrahedron",
+	                      &vector_Type, &vec1,
+	                      &vector_Type, &vec2,
+	                      &vector_Type, &vec3,
+	                      &vector_Type, &vec4))
+	{
+		return NULL;
+	}
+
+	if (vec1->size < 3 || vec2->size < 3 || vec3->size < 3 || vec4->size < 3) {
+		PyErr_SetString(PyExc_ValueError,
+		                "geometry.volume_tetrahedron(...): "
+		                " can't use 2D Vectors");
+		return NULL;
+	}
+
+	if (BaseMath_ReadCallback(vec1) == -1 ||
+	    BaseMath_ReadCallback(vec2) == -1 ||
+	    BaseMath_ReadCallback(vec3) == -1 ||
+	    BaseMath_ReadCallback(vec4) == -1)
+	{
+		return NULL;
+	}
+
+	return PyFloat_FromDouble(volume_tetrahedron_v3(vec1->vec, vec2->vec, vec3->vec, vec4->vec));
+}
+
 PyDoc_STRVAR(M_Geometry_intersect_line_line_2d_doc,
 ".. function:: intersect_line_line_2d(lineA_p1, lineA_p2, lineB_p1, lineB_p2)\n"
 "\n"
@@ -1458,6 +1503,7 @@
 	{"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},




More information about the Bf-blender-cvs mailing list