Commit: a444480d481eceffd887a5f33762aa0e5f4d949d
Author: Campbell Barton
Date:   Thu Jul 16 02:28:45 2015 +1000
Branches: mathutils_bvhtree
https://developer.blender.org/rBa444480d481eceffd887a5f33762aa0e5f4d949d

share generic parsing functions

===================================================================

M       source/blender/python/mathutils/mathutils_bvhtree.c

===================================================================

diff --git a/source/blender/python/mathutils/mathutils_bvhtree.c 
b/source/blender/python/mathutils/mathutils_bvhtree.c
index 933e018..e44f630 100644
--- a/source/blender/python/mathutils/mathutils_bvhtree.c
+++ b/source/blender/python/mathutils/mathutils_bvhtree.c
@@ -95,7 +95,7 @@ typedef struct PyBVHTree_Custom {
 
 
 /* -------------------------------------------------------------------- */
-/* Docstring snippets */
+/* Docstring (snippets) */
 
 #define PYBVH_FIND_GENERIC_MAX_DIST_DOC \
 "   :arg max_dist: Maximum distance threshold.\n" \
@@ -108,8 +108,8 @@ typedef struct PyBVHTree_Custom {
 
 #define PYBVH_DM_TYPES_STR "'TESSFACE', 'POLY', 'EDGE', 'VERT'"
 
+/* sqrt(FLT_MAX) */
 #define PYBVH_MAX_DIST_STR "1.84467e+19"
-
 static const float max_dist_default = 1.844674352395373e+19f;
 
 
@@ -168,6 +168,81 @@ static DerivedMesh *bvh_get_derived_mesh(const char 
*funcname, struct Scene *sce
        }
 }
 
+PyDoc_STRVAR(py_BVHTree_generic_ray_cast_doc,
+".. method:: ray_cast(co, direction, max_dist=sys.float_info.max)\n"
+"\n"
+"   Cast a ray onto the mesh.\n"
+"\n"
+"   :arg co: Start location of the ray in object space.\n"
+"   :type co: :class:`Vector`\n"
+"   :arg direction: Direction of the ray in object space.\n"
+"   :type direction: :class:`Vector`\n"
+PYBVH_FIND_GENERIC_MAX_DIST_DOC
+PYBVH_FIND_GENERIC_RETURN_DOC
+);
+static bool py_BVHTreeDerivedMesh_ray_cast_parse_args(
+        PyObject *args, const char *error_prefix,
+        /* generic args */
+        float r_co[3], float r_direction[3],
+        float *r_max_dist)
+{
+       PyObject *py_co, *py_direction;
+
+       *r_max_dist = FLT_MAX ;
+
+       if (!PyArg_ParseTuple(
+               args, (char *)"OO|f:ray_cast",
+               &py_co, &py_direction, r_max_dist))
+       {
+               return false;
+       }
+
+       if (mathutils_array_parse(r_co, 2, 3, py_co, error_prefix) == -1 ||
+           mathutils_array_parse(r_direction, 2, 3, py_direction, 
error_prefix) == -1)
+       {
+               return false;
+       }
+
+       normalize_v3(r_direction);
+
+       return true;
+}
+
+PyDoc_STRVAR(py_BVHTree_generic_find_nearest_doc,
+".. method:: find_nearest(co, max_dist=" PYBVH_MAX_DIST_STR ")\n"
+"\n"
+"   Find the nearest element to a point.\n"
+"\n"
+"   :arg co: Find nearest element to this point.\n"
+"   :type co: :class:`Vector`\n"
+PYBVH_FIND_GENERIC_MAX_DIST_DOC
+PYBVH_FIND_GENERIC_RETURN_DOC
+);
+static bool py_BVHTreeDerivedMesh_find_nearest_parse_args(
+        PyObject *args, const char *error_prefix,
+        /* generic args */
+        float r_co[3],
+        float *r_max_dist)
+{
+       PyObject *py_co;
+
+       *r_max_dist = max_dist_default ;
+
+       if (!PyArg_ParseTuple(
+               args, (char *)"O|f:find_nearest",
+               &py_co, r_max_dist))
+       {
+               return false;
+       }
+
+       if (mathutils_array_parse(r_co, 2, 3, py_co, error_prefix) == -1) {
+               return false;
+       }
+
+       return true;
+}
+
+
 static int dm_tessface_to_poly_index(DerivedMesh *dm, int tessface_index)
 {
        if (tessface_index != ORIGINDEX_NONE && tessface_index < 
dm->getNumTessFaces(dm)) {
@@ -351,52 +426,28 @@ static void 
PyBVHTreeDerivedMesh__tp_dealloc(PyBVHTree_DerivedMesh *self)
        Py_TYPE(self)->tp_free((PyObject *)self);
 }
 
-PyDoc_STRVAR(py_BVHTreeDerivedMesh_ray_cast_doc,
-".. method:: ray_cast(ray_start, ray_end)\n"
-"\n"
-"   Cast a ray onto the mesh.\n"
-"\n"
-"   :arg ray_point: Start location of the ray in object space.\n"
-"   :type ray_point: :class:`Vector`\n"
-"   :arg ray_direction: Direction of the ray in object space.\n"
-"   :type ray_direction: :class:`Vector`\n"
-PYBVH_FIND_GENERIC_MAX_DIST_DOC
-PYBVH_FIND_GENERIC_RETURN_DOC
-);
+/* py_BVHTree_generic_ray_cast_doc */
 static PyObject *py_BVHTreeDerivedMesh_ray_cast(PyBVHTree_DerivedMesh *self, 
PyObject *args)
 {
        const char *error_prefix = "ray_cast";
+       float co[3], direction[3];
+       float max_dist;
        
        BVHTreeFromMesh *meshdata = &self->meshdata;
        Object *ob = self->ob;
-       
-       PyObject *py_point, *py_direction;
-       float ray_point[3], ray_direction[3];
-       float ray_maxdist = FLT_MAX;
-       
-       if (!PyArg_ParseTuple(args, (char *)"OO|f:ray_cast", &py_point, 
&py_direction, &ray_maxdist))
-       {
-               return NULL;
-       }
-       
-       if (mathutils_array_parse(ray_point, 2, 3, py_point, error_prefix) == 
-1 ||
-           mathutils_array_parse(ray_direction, 2, 3, py_direction, 
error_prefix) == -1)
-       {
-               return NULL;
-       }
-       
-       normalize_v3(ray_direction);
+
+       py_BVHTreeDerivedMesh_ray_cast_parse_args(args, error_prefix, co, 
direction, &max_dist);
        
        /* may fail if the mesh has no faces, in that case the ray-cast misses 
*/
        if (meshdata->tree && meshdata->raycast_callback && ob->derivedFinal) {
                BVHTreeRayHit hit;
-               hit.dist = ray_maxdist;
+               hit.dist = max_dist;
                hit.index = -1;
                
-               if (BLI_bvhtree_ray_cast(meshdata->tree, ray_point, 
ray_direction, 0.0f, &hit,
+               if (BLI_bvhtree_ray_cast(meshdata->tree, co, direction, 0.0f, 
&hit,
                                         meshdata->raycast_callback, meshdata) 
!= -1)
                {
-                       if (hit.dist <= ray_maxdist) {
+                       if (hit.dist <= max_dist) {
                                int ret_index = self->use_poly_index ? 
dm_tessface_to_poly_index(ob->derivedFinal, hit.index) : hit.index;
                                return bvhtree_ray_hit_to_py(hit.co, hit.no, 
ret_index, hit.dist);
                        }
@@ -406,34 +457,19 @@ static PyObject 
*py_BVHTreeDerivedMesh_ray_cast(PyBVHTree_DerivedMesh *self, PyO
        return bvhtree_ray_hit_to_py(NULL, NULL, -1, 0.0f);
 }
 
-PyDoc_STRVAR(py_BVHTreeDerivedMesh_find_nearest_doc,
-".. method:: find_nearest(point, max_dist=" PYBVH_MAX_DIST_STR ")\n"
-"\n"
-"   Find the nearest element to a point.\n"
-"\n"
-"   :arg point: Find nearest element to this point.\n"
-"   :type point: :class:`Vector`\n"
-PYBVH_FIND_GENERIC_MAX_DIST_DOC
-PYBVH_FIND_GENERIC_RETURN_DOC
-);
+/* use py_BVHTree_generic_find_nearest_doc */
 static PyObject *py_BVHTreeDerivedMesh_find_nearest(PyBVHTree_DerivedMesh 
*self, PyObject *args)
 {
        const char *error_prefix = "find_nearest";
+       float co[3];
+       float max_dist;
 
        BVHTreeFromMesh *meshdata = &self->meshdata;
        Object *ob = self->ob;
-       
-       PyObject *py_point;
-       float point[3];
-       float max_dist = max_dist_default;
+
        BVHTreeNearest nearest;
-       
-       if (!PyArg_ParseTuple(args, (char *)"O|f:find_nearest", &py_point, 
&max_dist))
-       {
-               return NULL;
-       }
-       
-       if (mathutils_array_parse(point, 2, 3, py_point, error_prefix) == -1) {
+
+       if (!py_BVHTreeDerivedMesh_find_nearest_parse_args(args, error_prefix, 
co, &max_dist)) {
                return NULL;
        }
        
@@ -442,7 +478,7 @@ static PyObject 
*py_BVHTreeDerivedMesh_find_nearest(PyBVHTree_DerivedMesh *self,
        
        /* may fail if the mesh has no faces, in that case the ray-cast misses 
*/
        if (meshdata->tree && meshdata->nearest_callback && ob->derivedFinal) {
-               if (BLI_bvhtree_find_nearest(meshdata->tree, point, &nearest,
+               if (BLI_bvhtree_find_nearest(meshdata->tree, co, &nearest,
                                             meshdata->nearest_callback, 
meshdata) != -1)
                {
                        int ret_index = self->use_poly_index ? 
dm_tessface_to_poly_index(ob->derivedFinal, nearest.index) : nearest.index;
@@ -454,8 +490,8 @@ static PyObject 
*py_BVHTreeDerivedMesh_find_nearest(PyBVHTree_DerivedMesh *self,
 }
 
 static PyMethodDef PyBVHTreeDerivedMesh_methods[] = {
-       {"ray_cast", (PyCFunction)py_BVHTreeDerivedMesh_ray_cast, METH_VARARGS, 
py_BVHTreeDerivedMesh_ray_cast_doc},
-       {"find_nearest", (PyCFunction)py_BVHTreeDerivedMesh_find_nearest, 
METH_VARARGS, py_BVHTreeDerivedMesh_find_nearest_doc},
+       {"ray_cast", (PyCFunction)py_BVHTreeDerivedMesh_ray_cast, METH_VARARGS, 
py_BVHTree_generic_ray_cast_doc},
+       {"find_nearest", (PyCFunction)py_BVHTreeDerivedMesh_find_nearest, 
METH_VARARGS, py_BVHTree_generic_find_nearest_doc},
        {NULL, NULL, 0, NULL}
 };
 
@@ -565,50 +601,26 @@ static void PyBVHTreeBMesh__tp_dealloc(PyBVHTree_BMesh 
*self)
        Py_TYPE(self)->tp_free((PyObject *)self);
 }
 
-PyDoc_STRVAR(py_BVHTreeBMesh_ray_cast_doc,
-".. method:: ray_cast(co, direction)\n"
-"\n"
-"   Cast a ray onto the mesh.\n"
-"\n"
-"   :arg co: Start location of the ray in object space.\n"
-"   :type co: :class:`Vector`\n"
-"   :arg direction: Direction of the ray in object space.\n"
-"   :type direction: :class:`Vector`\n"
-PYBVH_FIND_GENERIC_MAX_DIST_DOC
-PYBVH_FIND_GENERIC_RETURN_DOC
-);
+/* py_BVHTree_generic_ray_cast_doc */
 static PyObject *py_BVHTreeBMesh_ray_cast(PyBVHTree_BMesh *self, PyObject 
*args)
 {
        const char *error_prefix = "ray_cast";
+       float co[3], direction[3];
+       float max_dist;
        
        BMBVHTree *bmdata = self->bmdata;
-       
-       PyObject *py_ray_point, *py_ray_direction;
-       float ray_point[3], ray_direction[3];
-       float ray_maxdist = FLT_MAX;
-       
-       if (!PyArg_ParseTuple(args, (char *)"OO|f:ray_cast", &py_ray_point, 
&py_ray_direction, &ray_maxdist))
-       {
-               return NULL;
-       }
-       
-       if (mathutils_array_parse(ray_point, 2, 3, py_ray_point, error_prefix) 
== -1 ||
-           mathutils_array_parse(ray_direction, 2, 3, py_ray_direction, 
error_prefix) == -1)
-       {
-               return NULL;
-       }
-       
-       normalize_v3(ray_direction);
-       
+
+       py_BVHTreeDerivedMesh_ray_cast_parse_args(args, error_prefix, co, 
direction, &max_dist);
+
        /* may fail if the mesh has no faces, in that case the ray-cast misses 
*/
        if (bmdata) {
                BMFace *hit_face;
                float hit_co[3], hit_dist;
                
-               hit_dist = ray_maxdist;
+               hit_dist = max_dist;
                
-               hit_face = BKE_bmbvh_ray_cast(bmdata, ray_point, ray_direction, 
0.0f, &hit_dist, hit_co, NULL);
-               if (hit_face && hit_dist <= ray_maxdist) {
+               hit_face = BKE_bmbvh_ray_cast(bmdata, co, direction, 0.0f, 
&hit_dist, hit_co, NULL);
+               if (hit_face && hit_dist <= max_dist) {
                        int ret_index = BM_elem_index_get(hit_face);
                        return bvhtree_ray_hit_to_py(hit_co, hit_face->no, 
ret_index, hit_dist);
                }
@@ -617,41 +629,26 @@ static PyObject *py_BVHTreeBMesh_ray_cast(PyBVHTree_BMesh 
*self, PyObject *args)
        return bvhtree_ray_hit_to_py(NULL, NULL, -1, 0.0f);
 }
 
-PyDoc_STRVAR(py_BVHTreeBMesh_find_nearest_doc,
-".. method:: find_nearest(co, max_dist=" PYBVH_MAX_DIST_STR ")\n"
-"\n"
-"   Find the nearest element to a point.\n"
-"\n"
-"   :arg co: Find nearest element to this point.\n"
-"   :type co: :class:`Vector`\n"
-PYBVH_FIND_GENERIC_MAX_DIST_DOC
-PYBVH_FIND_GENERIC_RETURN_DOC
-);
+/* use py_BVHTree_generic_find_nearest_doc */
 static PyObject *py_BVHTreeBMesh_find_nearest(PyBVHTree_BMesh *self, PyObject 
*args)
 {
        const char *error_prefix = "find_nearest";
+       float co[3];
+       f

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to