Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/mathutils/mathutils_bvhtree.c
| Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | |||||
| /** \name Docstring (snippets) | /** \name Docstring (snippets) | ||||
| * \{ */ | * \{ */ | ||||
| #define PYBVH_FIND_GENERIC_DISTANCE_DOC \ | #define PYBVH_FIND_GENERIC_DISTANCE_DOC \ | ||||
| " :arg distance: Maximum distance threshold.\n" \ | " :arg distance: Maximum distance threshold.\n" \ | ||||
| " :type distance: float\n" | " :type distance: float\n" | ||||
| #define PYBVH_FIND_GENERIC_RETURN_DOC \ | #define PYBVH_FIND_GENERIC_RETURN_DOC \ | ||||
| " :return: Returns a tuple\n" \ | " :return: Returns a namedtuple or None\n" \ | ||||
| " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \ | " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \ | ||||
| " Values will all be None if no hit is found.\n" \ | " :rtype: :class:`namedtuple`\n" | ||||
| " :rtype: :class:`tuple`\n" | |||||
| #define PYBVH_FIND_GENERIC_RETURN_LIST_DOC \ | #define PYBVH_FIND_GENERIC_RETURN_LIST_DOC \ | ||||
| " :return: Returns a list of tuples\n" \ | " :return: Returns a list of namedtuples\n" \ | ||||
| " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \ | " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \ | ||||
| " :rtype: :class:`list`\n" | " :rtype: :class:`list`\n" | ||||
| #define PYBVH_FROM_GENERIC_EPSILON_DOC \ | #define PYBVH_FROM_GENERIC_EPSILON_DOC \ | ||||
| " :arg epsilon: Increase the threshold for detecting overlap and raycast hits.\n" \ | " :arg epsilon: Increase the threshold for detecting overlap and raycast hits.\n" \ | ||||
| " :type epsilon: float\n" | " :type epsilon: float\n" | ||||
| /** \} */ | /** \} */ | ||||
| ▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | static PyObject *bvhtree_CreatePyObject( | ||||
| return (PyObject *)result; | return (PyObject *)result; | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name BVHTreeRayHit to Python utilities | /** \name BVHTreeRayHit/BVHTreeNearest to Python utilities | ||||
| * \{ */ | * \{ */ | ||||
| static void py_bvhtree_raycast_to_py_tuple(const BVHTreeRayHit *hit, PyObject *py_retval) | static PyTypeObject SurfacePoint_Type; | ||||
| { | |||||
| BLI_assert(hit->index >= 0); | |||||
| BLI_assert(PyTuple_GET_SIZE(py_retval) == 4); | |||||
| PyTuple_SET_ITEMS(py_retval, | static PyStructSequence_Field SurfacePoint_Fields[] = { | ||||
| Vector_CreatePyObject(hit->co, 3, NULL), | {(char *)"location", (char *)"Vector containing the position."}, | ||||
| Vector_CreatePyObject(hit->no, 3, NULL), | {(char *)"normal", (char *)"Vector containing the surface normal."}, | ||||
| PyLong_FromLong(hit->index), | {(char *)"index", (char *)"Index of the hit polygon."}, | ||||
| PyFloat_FromDouble(hit->dist)); | {(char *)"distance", (char *)"Distance to the hit position from ray origin."}, | ||||
| {NULL, NULL} | |||||
| }; | |||||
| } | PyStructSequence_Desc SurfacePoint_Desc = { | ||||
| (char *)"mathutils.bvhtree.SurfacePoint", | |||||
| (char *)"Contains information about a point on a polygon surface.", | |||||
| SurfacePoint_Fields, | |||||
| ARRAY_SIZE(SurfacePoint_Fields) - 1 | |||||
| }; | |||||
| static PyObject *py_bvhtree_raycast_to_py(const BVHTreeRayHit *hit) | static PyObject *py_bvhtree_raycast_to_py(const BVHTreeRayHit *hit) | ||||
| { | { | ||||
| PyObject *py_retval = PyTuple_New(4); | PyObject *py_retval = PyStructSequence_New((PyTypeObject *)&SurfacePoint_Type); | ||||
| PyStructSequence_SET_ITEM(py_retval, 0, Vector_CreatePyObject(hit->co, 3, NULL)); | |||||
| py_bvhtree_raycast_to_py_tuple(hit, py_retval); | PyStructSequence_SET_ITEM(py_retval, 1, Vector_CreatePyObject(hit->no, 3, NULL)); | ||||
| PyStructSequence_SET_ITEM(py_retval, 2, PyLong_FromLong(hit->index)); | |||||
| PyStructSequence_SET_ITEM(py_retval, 3, PyFloat_FromDouble(hit->dist)); | |||||
| return py_retval; | return py_retval; | ||||
| } | } | ||||
| static PyObject *py_bvhtree_raycast_to_py_none(void) | |||||
| { | |||||
| PyObject *py_retval = PyTuple_New(4); | |||||
| PyC_Tuple_Fill(py_retval, Py_None); | |||||
| return py_retval; | |||||
| } | |||||
| #if 0 | |||||
| static PyObject *py_bvhtree_raycast_to_py_and_check(const BVHTreeRayHit *hit) | |||||
| { | |||||
| PyObject *py_retval; | |||||
| py_retval = PyTuple_New(4); | |||||
| if (hit->index != -1) { | |||||
| py_bvhtree_raycast_to_py_tuple(hit, py_retval); | |||||
| } | |||||
| else { | |||||
| PyC_Tuple_Fill(py_retval, Py_None); | |||||
| } | |||||
| return py_retval; | |||||
| } | |||||
| #endif | |||||
| /** \} */ | |||||
| /* -------------------------------------------------------------------- */ | |||||
| /** \name BVHTreeNearest to Python utilities | |||||
| * \{ */ | |||||
| static void py_bvhtree_nearest_to_py_tuple(const BVHTreeNearest *nearest, PyObject *py_retval) | |||||
| { | |||||
| BLI_assert(nearest->index >= 0); | |||||
| BLI_assert(PyTuple_GET_SIZE(py_retval) == 4); | |||||
| PyTuple_SET_ITEMS(py_retval, | |||||
| Vector_CreatePyObject(nearest->co, 3, NULL), | |||||
| Vector_CreatePyObject(nearest->no, 3, NULL), | |||||
| PyLong_FromLong(nearest->index), | |||||
| PyFloat_FromDouble(sqrtf(nearest->dist_sq))); | |||||
| } | |||||
| static PyObject *py_bvhtree_nearest_to_py(const BVHTreeNearest *nearest) | static PyObject *py_bvhtree_nearest_to_py(const BVHTreeNearest *nearest) | ||||
| { | { | ||||
| PyObject *py_retval = PyTuple_New(4); | PyObject *py_retval = PyStructSequence_New((PyTypeObject *)&SurfacePoint_Type); | ||||
| PyStructSequence_SET_ITEM(py_retval, 0, Vector_CreatePyObject(nearest->co, 3, NULL)); | |||||
| py_bvhtree_nearest_to_py_tuple(nearest, py_retval); | PyStructSequence_SET_ITEM(py_retval, 1, Vector_CreatePyObject(nearest->no, 3, NULL)); | ||||
| PyStructSequence_SET_ITEM(py_retval, 2, PyLong_FromLong(nearest->index)); | |||||
| return py_retval; | PyStructSequence_SET_ITEM(py_retval, 3, PyFloat_FromDouble(sqrtf(nearest->dist_sq))); | ||||
| } | |||||
| static PyObject *py_bvhtree_nearest_to_py_none(void) | |||||
| { | |||||
| PyObject *py_retval = PyTuple_New(4); | |||||
| PyC_Tuple_Fill(py_retval, Py_None); | |||||
| return py_retval; | |||||
| } | |||||
| #if 0 | |||||
| static PyObject *py_bvhtree_nearest_to_py_and_check(const BVHTreeNearest *nearest) | |||||
| { | |||||
| PyObject *py_retval; | |||||
| py_retval = PyTuple_New(4); | |||||
| if (nearest->index != -1) { | |||||
| py_bvhtree_nearest_to_py_tuple(nearest, py_retval); | |||||
| } | |||||
| else { | |||||
| PyC_Tuple_Fill(py_retval, Py_None); | |||||
| } | |||||
| return py_retval; | return py_retval; | ||||
| } | } | ||||
| #endif | |||||
| /** \} */ | /** \} */ | ||||
| static void py_bvhtree__tp_dealloc(PyBVHTree *self) | static void py_bvhtree__tp_dealloc(PyBVHTree *self) | ||||
| { | { | ||||
| if (self->tree) { | if (self->tree) { | ||||
| BLI_bvhtree_free(self->tree); | BLI_bvhtree_free(self->tree); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | if (self->tree) { | ||||
| if (BLI_bvhtree_ray_cast( | if (BLI_bvhtree_ray_cast( | ||||
| self->tree, co, direction, 0.0f, &hit, | self->tree, co, direction, 0.0f, &hit, | ||||
| py_bvhtree_raycast_cb, self) != -1) | py_bvhtree_raycast_cb, self) != -1) | ||||
| { | { | ||||
| return py_bvhtree_raycast_to_py(&hit); | return py_bvhtree_raycast_to_py(&hit); | ||||
| } | } | ||||
| } | } | ||||
| return py_bvhtree_raycast_to_py_none(); | Py_RETURN_NONE; | ||||
| } | } | ||||
| PyDoc_STRVAR(py_bvhtree_find_nearest_doc, | PyDoc_STRVAR(py_bvhtree_find_nearest_doc, | ||||
| ".. method:: find_nearest(origin, distance=" PYBVH_MAX_DIST_STR ")\n" | ".. method:: find_nearest(origin, distance=" PYBVH_MAX_DIST_STR ")\n" | ||||
| "\n" | "\n" | ||||
| " Find the nearest element (typically face index) to a point.\n" | " Find the nearest element (typically face index) to a point.\n" | ||||
| "\n" | "\n" | ||||
| " :arg co: Find nearest element to this point.\n" | " :arg co: Find nearest element to this point.\n" | ||||
| Show All 33 Lines | if (self->tree) { | ||||
| if (BLI_bvhtree_find_nearest( | if (BLI_bvhtree_find_nearest( | ||||
| self->tree, co, &nearest, | self->tree, co, &nearest, | ||||
| py_bvhtree_nearest_point_cb, self) != -1) | py_bvhtree_nearest_point_cb, self) != -1) | ||||
| { | { | ||||
| return py_bvhtree_nearest_to_py(&nearest); | return py_bvhtree_nearest_to_py(&nearest); | ||||
| } | } | ||||
| } | } | ||||
| return py_bvhtree_nearest_to_py_none(); | Py_RETURN_NONE; | ||||
| } | } | ||||
| struct PyBVH_RangeData { | struct PyBVH_RangeData { | ||||
| PyBVHTree *self; | PyBVHTree *self; | ||||
| PyObject *result; | PyObject *result; | ||||
| float dist_sq; | float dist_sq; | ||||
| }; | }; | ||||
| ▲ Show 20 Lines • Show All 876 Lines • ▼ Show 20 Lines | if (m == NULL) { | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| /* Register classes */ | /* Register classes */ | ||||
| if (PyType_Ready(&PyBVHTree_Type) < 0) { | if (PyType_Ready(&PyBVHTree_Type) < 0) { | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| PyStructSequence_InitType(&SurfacePoint_Type, &SurfacePoint_Desc); | |||||
| PyModule_AddObject(m, "BVHTree", (PyObject *)&PyBVHTree_Type); | PyModule_AddObject(m, "BVHTree", (PyObject *)&PyBVHTree_Type); | ||||
| return m; | return m; | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||