[Bf-blender-cvs] [9d1293b] temp-python-bvh: temp

Campbell Barton noreply at git.blender.org
Fri Jul 24 03:18:29 CEST 2015


Commit: 9d1293b104fe90f12d9ae056f163fcb3d11f3563
Author: Campbell Barton
Date:   Wed Jul 15 21:04:15 2015 +1000
Branches: temp-python-bvh
https://developer.blender.org/rB9d1293b104fe90f12d9ae056f163fcb3d11f3563

temp

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

M	doc/python_api/sphinx_doc_gen.py
M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenkernel/intern/bvhutils.c
M	source/blender/python/mathutils/CMakeLists.txt
M	source/blender/python/mathutils/mathutils.c
A	source/blender/python/mathutils/mathutils_bvhtree.c
A	source/blender/python/mathutils/mathutils_bvhtree.h

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

diff --git a/doc/python_api/sphinx_doc_gen.py b/doc/python_api/sphinx_doc_gen.py
index 32776ef..47a7725 100644
--- a/doc/python_api/sphinx_doc_gen.py
+++ b/doc/python_api/sphinx_doc_gen.py
@@ -263,6 +263,7 @@ else:
         "gpu",
         "mathutils",
         "mathutils.geometry",
+        "mathutils.bvhtree",
         "mathutils.kdtree",
         "mathutils.noise",
         "freestyle",
@@ -1644,7 +1645,7 @@ def write_rst_contents(basepath):
 
     standalone_modules = (
         # mathutils
-        "mathutils", "mathutils.geometry", "mathutils.kdtree", "mathutils.noise",
+        "mathutils", "mathutils.geometry", "mathutils.bvhtree", "mathutils.kdtree", "mathutils.noise",
         # misc
         "freestyle", "bgl", "blf", "gpu", "aud", "bpy_extras",
         # bmesh, submodules are in own page
@@ -1796,6 +1797,7 @@ def write_rst_importable_modules(basepath):
         "bpy.props"            : "Property Definitions",
         "mathutils"            : "Math Types & Utilities",
         "mathutils.geometry"   : "Geometry Utilities",
+        "mathutils.bvhtree"    : "BVHTree Utilities",
         "mathutils.kdtree"     : "KDTree Utilities",
         "mathutils.noise"      : "Noise Utilities",
         "freestyle"            : "Freestyle Module",
diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index a360511..8109f46 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -101,6 +101,7 @@ void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
  * Math functions used by callbacks
  */
 float bvhtree_ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, const float v0[3], const float v1[3], const float v2[3]);
+float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray, float radius, const float m_dist, const float v0[3], const float v1[3], const float v2[3]);
 float nearest_point_in_tri_surface_squared(const float v0[3], const float v1[3], const float v2[3], const float p[3], int *v, int *e, float nearest[3]);
 
 /*
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c
index 1a4a4bd..2213869 100644
--- a/source/blender/blenkernel/intern/bvhutils.c
+++ b/source/blender/blenkernel/intern/bvhutils.c
@@ -60,7 +60,7 @@ float bvhtree_ray_tri_intersection(const BVHTreeRay *ray, const float UNUSED(m_d
 	return FLT_MAX;
 }
 
-static float sphereray_tri_intersection(const BVHTreeRay *ray, float radius, const float m_dist, const float v0[3], const float v1[3], const float v2[3])
+float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray, float radius, const float m_dist, const float v0[3], const float v1[3], const float v2[3])
 {
 	
 	float idist;
@@ -163,7 +163,7 @@ static void mesh_faces_spherecast(void *userdata, int index, const BVHTreeRay *r
 		if (data->sphere_radius == 0.0f)
 			dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
 		else
-			dist = sphereray_tri_intersection(ray, data->sphere_radius, hit->dist, t0, t1, t2);
+			dist = bvhtree_sphereray_tri_intersection(ray, data->sphere_radius, hit->dist, t0, t1, t2);
 
 		if (dist >= 0 && dist < hit->dist) {
 			hit->index = index;
@@ -200,7 +200,7 @@ static void editmesh_faces_spherecast(void *userdata, int index, const BVHTreeRa
 		if (data->sphere_radius == 0.0f)
 			dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
 		else
-			dist = sphereray_tri_intersection(ray, data->sphere_radius, hit->dist, t0, t1, t2);
+			dist = bvhtree_sphereray_tri_intersection(ray, data->sphere_radius, hit->dist, t0, t1, t2);
 
 		if (dist >= 0 && dist < hit->dist) {
 			hit->index = index;
diff --git a/source/blender/python/mathutils/CMakeLists.txt b/source/blender/python/mathutils/CMakeLists.txt
index ef6b090..f70f893 100644
--- a/source/blender/python/mathutils/CMakeLists.txt
+++ b/source/blender/python/mathutils/CMakeLists.txt
@@ -22,6 +22,7 @@ set(INC
 	.
 	../../blenlib
 	../../blenkernel
+	../../bmesh
 	../../makesdna
 	../../../../intern/guardedalloc
 )
@@ -37,6 +38,7 @@ set(SRC
 	mathutils_Matrix.c
 	mathutils_Quaternion.c
 	mathutils_Vector.c
+	mathutils_bvhtree.c
 	mathutils_geometry.c
 	mathutils_interpolate.c
 	mathutils_kdtree.c
@@ -48,6 +50,7 @@ set(SRC
 	mathutils_Matrix.h
 	mathutils_Quaternion.h
 	mathutils_Vector.h
+	mathutils_bvhtree.h
 	mathutils_geometry.h
 	mathutils_interpolate.h
 	mathutils_kdtree.h
diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c
index 8b18a02..09e67e1 100644
--- a/source/blender/python/mathutils/mathutils.c
+++ b/source/blender/python/mathutils/mathutils.c
@@ -596,6 +596,7 @@ static struct PyModuleDef M_Mathutils_module_def = {
 #include "mathutils_geometry.h"
 #include "mathutils_interpolate.h"
 #ifndef MATH_STANDALONE
+#  include "mathutils_bvhtree.h"
 #  include "mathutils_kdtree.h"
 #  include "mathutils_noise.h"
 #endif
@@ -649,6 +650,11 @@ PyMODINIT_FUNC PyInit_mathutils(void)
 	PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
 	Py_INCREF(submodule);
 
+	/* BVHTree submodule */
+	PyModule_AddObject(mod, "bvhtree", (submodule = PyInit_mathutils_bvhtree()));
+	PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
+	Py_INCREF(submodule);
+
 	/* KDTree submodule */
 	PyModule_AddObject(mod, "kdtree", (submodule = PyInit_mathutils_kdtree()));
 	PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
diff --git a/source/blender/python/mathutils/mathutils_bvhtree.c b/source/blender/python/mathutils/mathutils_bvhtree.c
new file mode 100644
index 0000000..310ec41
--- /dev/null
+++ b/source/blender/python/mathutils/mathutils_bvhtree.c
@@ -0,0 +1,1119 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Lukas Toenne
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/mathutils/mathutils_bvhtree.c
+ *  \ingroup mathutils
+ *
+ * This file defines the 'mathutils.bvhtree' module, a general purpose module to access
+ * blenders bvhtree for mesh surface nearest-element search and ray casting.
+ */
+
+#include <Python.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_kdopbvh.h"
+#include "BLI_math.h"
+
+#include "DNA_object_types.h"
+
+#include "BKE_bvhutils.h"
+#include "BKE_customdata.h"
+#include "BKE_DerivedMesh.h"
+#include "BKE_editmesh_bvh.h"
+
+#include "bmesh.h"
+
+#include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
+#include "../bmesh/bmesh_py_types.h"
+
+#include "mathutils.h"
+#include "mathutils_bvhtree.h"  /* own include */
+
+#include "BLI_strict_flags.h"
+
+typedef struct {
+	PyObject_HEAD
+} PyBVHTree;
+
+typedef struct {
+	PyBVHTree base;
+	/* Object DerivedMesh data */
+	Object *ob;
+	BVHTreeFromMesh meshdata;
+	bool use_poly_index;
+} PyBVHTree_DerivedMesh;
+
+typedef struct {
+	PyBVHTree base;
+	/* BMesh data */
+	BMBVHTree *bmdata;
+	BMLoop *(*bmlooptris)[3];
+	int bmtotlooptris;
+} PyBVHTree_BMesh;
+
+typedef struct BVHVertex {
+	float co[3];
+} BVHVertex;
+
+typedef struct BVHTriangle {
+	int v1, v2, v3;
+} BVHTriangle;
+
+typedef struct PyBVHTree_Custom {
+	PyBVHTree base;
+	/* geometry data */
+	struct BVHTree *tree;
+	
+	struct BVHVertex *vert;
+	struct BVHTriangle *tri;
+	int totvert, tottri;
+	
+	float epsilon;
+} PyBVHTree_Custom;
+
+/* -------------------------------------------------------------------- */
+/* Utility helper functions */
+
+/* return various derived meshes based on requested settings */
+static DerivedMesh *bvh_get_derived_mesh(const char *funcname, struct Scene *scene, Object *ob, bool use_deform, bool use_render, bool use_cage)
+{
+	/* we only need minimum mesh data for topology and vertex locations */
+	CustomDataMask mask = CD_MASK_BAREMESH;
+	
+	/* Write the display mesh into the dummy mesh */
+	if (use_deform) {
+		if (use_render) {
+			if (use_cage) {
+				PyErr_Format(PyExc_ValueError,
+				             "%s(...): cage arg is unsupported when (render=True)", funcname);
+				return NULL;
+			}
+			else {
+				return mesh_create_derived_render(scene, ob, mask);
+			}
+		}
+		else {
+			if (use_cage) {
+				return mesh_get_derived_deform(scene, ob, mask);  /* ob->derivedDeform */
+			}
+			else {
+				return mesh_get_derived_final(scene, ob, mask);  /* ob->derivedFinal */
+			}
+		}
+	}
+	else {
+		/* !use_deform */
+		if (use_render) {
+			if (use_cage) {
+				PyErr_Format(PyExc_ValueError,
+				             "%s(...): cage arg is unsupported when (render=True)", funcname);
+				return NULL;
+			}
+			else {
+				return mesh_create_derived_no_deform_render(scene, ob, NULL, mask);
+			}
+		}
+		else {
+			if (use_cage) {
+				PyErr_Format(PyExc_ValueError,
+				             "%s(...): cage arg is unsupported when (deform=False, render=False)", funcname);
+				return NULL;
+			}
+			else {
+				return mesh_create_derived_no_deform(scene, ob, NULL, mask);
+			}
+		}
+	}
+}
+
+static int dm_tessface_to_poly_index(DerivedMesh *dm, int tessface_index)
+{
+	if (tessface_index != ORIGINDEX_NONE && tessface_index < dm->getNumTessFaces(dm)) {
+		/* double lookup */
+		const int *index_mf_to_mpoly;
+		if ((index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX))) {
+			const int *index_mp_to_orig = dm->getPolyDataArray(dm, CD_ORIGINDEX);
+			return DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, tessface_index);
+		}
+	}
+
+	return ORIGINDEX_NONE;
+}
+
+static PyObject *bvhtree_ray_hit_to_py

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list