Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-11-12 14:06:56 +0400
committerCampbell Barton <ideasman42@gmail.com>2011-11-12 14:06:56 +0400
commit817b6cb9a8990483a2bfce7a11e690c829e75ddc (patch)
tree0ae158c6e2b6302851e19dd269d7b749d7af38c0 /source/blender
parente2c06d5fc91f4f1125cf79280cb585400f6e056c (diff)
mathutils.geometry.distance_point_to_plane(pt, plane_co, plane_no) - utility function, BLI math version too.
Diffstat (limited to 'source/blender')
-rw-r--r--source/blender/blenlib/BLI_math_geom.h1
-rw-r--r--source/blender/blenlib/intern/math_geom.c12
-rw-r--r--source/blender/python/mathutils/mathutils_geometry.c43
3 files changed, 53 insertions, 3 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h
index 8a42414ea9f..99687ae8bb4 100644
--- a/source/blender/blenlib/BLI_math_geom.h
+++ b/source/blender/blenlib/BLI_math_geom.h
@@ -60,6 +60,7 @@ float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2]);
float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]);
void closest_to_line_segment_v2(float closest[2], const float p[2], const float l1[2], const float l2[2]);
+float dist_to_plane_v3(const float p[2], const float plane_co[3], const float plane_no[2]);
float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]);
float closest_to_line_v3(float r[3], const float p[3], const float l1[3], const float l2[3]);
float closest_to_line_v2(float r[2], const float p[2], const float l1[2], const float l2[2]);
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c
index 99ead8e9d6a..a135cb43882 100644
--- a/source/blender/blenlib/intern/math_geom.c
+++ b/source/blender/blenlib/intern/math_geom.c
@@ -237,6 +237,18 @@ void closest_to_line_segment_v3(float closest[3], const float v1[3], const float
copy_v3_v3(closest, cp);
}
+/* signed distance from the point to the plane in 3D */
+float dist_to_plane_v3(const float p[2], const float plane_co[3], const float plane_no[2])
+{
+ float plane_no_unit[3];
+ float plane_co_other[3];
+
+ normalize_v3_v3(plane_no_unit, plane_no);
+ add_v3_v3v3(plane_co_other, plane_co, plane_no_unit);
+
+ return -line_point_factor_v3(p, plane_co, plane_co_other);
+}
+
/* distance v1 to line-piece v2-v3 in 3D */
float dist_to_line_segment_v3(const float v1[3], const float v2[3], const float v3[3])
{
diff --git a/source/blender/python/mathutils/mathutils_geometry.c b/source/blender/python/mathutils/mathutils_geometry.c
index 3518c8008c6..a0a6ba277cf 100644
--- a/source/blender/python/mathutils/mathutils_geometry.c
+++ b/source/blender/python/mathutils/mathutils_geometry.c
@@ -420,7 +420,7 @@ static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObjec
return NULL;
}
- if ( BaseMath_ReadCallback(line_a) == -1 ||
+ if ( BaseMath_ReadCallback(line_a) == -1 ||
BaseMath_ReadCallback(line_b) == -1 ||
BaseMath_ReadCallback(plane_co) == -1 ||
BaseMath_ReadCallback(plane_no) == -1
@@ -559,7 +559,7 @@ static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyO
return NULL;
}
- if ( BaseMath_ReadCallback(line_a) == -1 ||
+ if ( BaseMath_ReadCallback(line_a) == -1 ||
BaseMath_ReadCallback(line_b) == -1 ||
BaseMath_ReadCallback(sphere_co) == -1
) {
@@ -686,7 +686,7 @@ PyDoc_STRVAR(M_Geometry_intersect_point_quad_2d_doc,
" Takes 5 vectors (using only the x and y coordinates): one is the point and the next 4 define the quad, only the x and y are used from the vectors. Returns 1 if the point is within the quad, otherwise 0.\n"
"\n"
" :arg pt: Point\n"
-" :type v1: :class:`mathutils.Vector`\n"
+" :type pt: :class:`mathutils.Vector`\n"
" :arg quad_p1: First point of the quad\n"
" :type quad_p1: :class:`mathutils.Vector`\n"
" :arg quad_p2: Second point of the quad\n"
@@ -717,6 +717,42 @@ static PyObject *M_Geometry_intersect_point_quad_2d(PyObject *UNUSED(self), PyOb
return PyLong_FromLong(isect_point_quad_v2(pt_vec->vec, quad_p1->vec, quad_p2->vec, quad_p3->vec, quad_p4->vec));
}
+PyDoc_STRVAR(M_Geometry_distance_point_to_plane_doc,
+".. function:: distance_point_to_plane(pt, plane_co, plane_no)\n"
+"\n"
+" Returns the signed distance between a point and a plane "
+" (negative when below the normal).\n"
+"\n"
+" :arg pt: Point\n"
+" :type pt: :class:`mathutils.Vector`\n"
+" :arg plane_co: First point of the quad\n"
+" :type plane_co: :class:`mathutils.Vector`\n"
+" :arg plane_no: Second point of the quad\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)
+ ) {
+ return NULL;
+ }
+
+ if ( BaseMath_ReadCallback(pt) == -1 ||
+ BaseMath_ReadCallback(plene_co) == -1 ||
+ BaseMath_ReadCallback(plane_no) == -1)
+ {
+ return NULL;
+ }
+
+ return PyFloat_FromDouble(dist_to_plane_v3(pt->vec, plene_co->vec, plane_no->vec));
+}
+
PyDoc_STRVAR(M_Geometry_barycentric_transform_doc,
".. function:: barycentric_transform(point, tri_a1, tri_a2, tri_a3, tri_b1, tri_b2, tri_b3)\n"
"\n"
@@ -1103,6 +1139,7 @@ static PyMethodDef M_Geometry_methods[]= {
{"intersect_line_plane", (PyCFunction) M_Geometry_intersect_line_plane, METH_VARARGS, M_Geometry_intersect_line_plane_doc},
{"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},
{"area_tri", (PyCFunction) M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_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},