diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-08-24 01:16:54 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-08-24 01:16:54 +0300 |
commit | 30e801eb06e47591b658a3faf6e3545b542ddf0d (patch) | |
tree | 43aa65300165a81921767c5d7ac659dad13c8fae | |
parent | da416e15140f8a897f2cd36511ee8d007afc1df2 (diff) |
3D Print Tools: use BVH for overlap check
Fixes T38241
-rw-r--r-- | object_print3d_utils/mesh_helpers.py | 59 |
1 files changed, 5 insertions, 54 deletions
diff --git a/object_print3d_utils/mesh_helpers.py b/object_print3d_utils/mesh_helpers.py index 450e2388..40659e2e 100644 --- a/object_print3d_utils/mesh_helpers.py +++ b/object_print3d_utils/mesh_helpers.py @@ -106,63 +106,14 @@ def bmesh_check_self_intersect_object(obj): if not obj.data.polygons: return array.array('i', ()) - # Heres what we do! - # - # * Take original Mesh. - # * Copy it and triangulate it (keeping list of original edge index values) - # * Move the BMesh into a temp Mesh. - # * Make a temp Object in the scene and assign the temp Mesh. - # * For every original edge - ray-cast on the object to find which intersect. - # * Report all edge intersections. - - # Triangulate bm = bmesh_copy_from_object(obj, transform=False, triangulate=False) - face_map_index_org = {f: i for i, f in enumerate(bm.faces)} - ret = bmesh.ops.triangulate(bm, faces=bm.faces) - face_map = ret["face_map"] - # map new index to original index - face_map_index = {i: face_map_index_org[face_map.get(f, f)] for i, f in enumerate(bm.faces)} - del face_map_index_org - del ret - - # Create a real mesh (lame!) - scene = bpy.context.scene - me_tmp = bpy.data.meshes.new(name="~temp~") - bm.to_mesh(me_tmp) - bm.free() - obj_tmp = bpy.data.objects.new(name=me_tmp.name, object_data=me_tmp) - scene.objects.link(obj_tmp) - scene.update() - ray_cast = obj_tmp.ray_cast - - faces_error = set() - - EPS_NORMAL = 0.000001 - EPS_CENTER = 0.01 # should always be bigger - - for ed in me_tmp.edges: - v1i, v2i = ed.vertices - v1 = me_tmp.vertices[v1i] - v2 = me_tmp.vertices[v2i] - - # setup the edge with an offset - co_1 = v1.co.copy() - co_2 = v2.co.copy() - co_mid = (co_1 + co_2) * 0.5 - no_mid = (v1.normal + v2.normal).normalized() * EPS_NORMAL - co_1 = co_1.lerp(co_mid, EPS_CENTER) + no_mid - co_2 = co_2.lerp(co_mid, EPS_CENTER) + no_mid - co, no, index = ray_cast(co_1, co_2) - if index != -1: - faces_error.add(face_map_index[index]) - - scene.objects.unlink(obj_tmp) - bpy.data.objects.remove(obj_tmp) - bpy.data.meshes.remove(me_tmp) - - scene.update() + import mathutils + tree = mathutils.bvhtree.BVHTree.FromBMesh(bm, epsilon=0.00001) + overlap = tree.overlap(tree) + faces_error = {i for i_pair in overlap for i in i_pair} + print(faces_error) return array.array('i', faces_error) |