Skip to content

Commit fdf23ec

Browse files
committed
Fix #7632. See #2824. Optimise vertex matching trick for IfcIndexedColourMap.
1 parent 1445f89 commit fdf23ec

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/bonsai/bonsai/tool/loader.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import numpy.typing as npt
4040
from ifcopenshell.util.shape_builder import np_to_4d
4141
from mathutils import Matrix, Vector
42+
from mathutils.kdtree import KDTree
4243

4344
import bonsai.bim.import_ifc
4445
import bonsai.core.tool
@@ -562,7 +563,18 @@ def load_indexed_map(cls, index_map: ifcopenshell.entity_instance, mesh: bpy.typ
562563

563564
bm_verts = np.array([v.co for v in bm.verts])
564565
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
566578

567579
# ifc indices start with 1
568580
remap_verts_to_blender = lambda ifc_verts: [coordinates_remap[i - 1] for i in ifc_verts]

0 commit comments

Comments
 (0)