|
39 | 39 | import numpy.typing as npt |
40 | 40 | from ifcopenshell.util.shape_builder import np_to_4d |
41 | 41 | from mathutils import Matrix, Vector |
| 42 | +from mathutils.kdtree import KDTree |
42 | 43 |
|
43 | 44 | import bonsai.bim.import_ifc |
44 | 45 | import bonsai.core.tool |
@@ -562,7 +563,18 @@ def load_indexed_map(cls, index_map: ifcopenshell.entity_instance, mesh: bpy.typ |
562 | 563 |
|
563 | 564 | bm_verts = np.array([v.co for v in bm.verts]) |
564 | 565 | coords_scaled = np.array(faceset.Coordinates.CoordList) * si_conversion |
565 | | - coordinates_remap = [np.argmin(np.sum((bm_verts - co) ** 2, axis=1)) for co in coords_scaled] |
| 566 | + # See #2824. IfcIndexedColourMap is not natively handled by IfcOpenShell |
| 567 | + # As a result, we map IFC coords to Blender coords (highly wasteful but...) |
| 568 | + # coordinates_remap = [np.argmin(np.sum((bm_verts - co) ** 2, axis=1)) for co in coords_scaled] |
| 569 | + # Because this is O(N*M), here is a faster KDTree implementation. |
| 570 | + kd = KDTree(len(bm_verts)) |
| 571 | + for i, v in enumerate(bm_verts): |
| 572 | + kd.insert((float(v[0]), float(v[1]), float(v[2])), i) |
| 573 | + kd.balance() |
| 574 | + coordinates_remap = np.empty(len(coords_scaled), dtype=np.int32) |
| 575 | + for j, co in enumerate(coords_scaled): |
| 576 | + _co, index, _dist = kd.find((float(co[0]), float(co[1]), float(co[2]))) |
| 577 | + coordinates_remap[j] = index |
566 | 578 |
|
567 | 579 | # ifc indices start with 1 |
568 | 580 | remap_verts_to_blender = lambda ifc_verts: [coordinates_remap[i - 1] for i in ifc_verts] |
|
0 commit comments