-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4PlCS.py
More file actions
1551 lines (1188 loc) · 60.7 KB
/
4PlCS.py
File metadata and controls
1551 lines (1188 loc) · 60.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This script implements the 4-Point Congruent Sets (4PlCS) algorithm for coarse registration of a point cloud (PCD) to an IFC model.
# The process is divided into four main steps:
# 1. Load a PCD, extract planar patches, and identify valid 4PlCSs.
# 2. Load an IFC model, extract planar patches, and identify valid 4PlCSs.
# 3. Match the 4PlCSs from the PCD and IFC to determine the most likely transformation.
# 4. Refine the transformation using the Iterative Closest Point (ICP) algorithm.
#
# Key assumptions:
# - The geometry of both the PCD and IFC models is in meters.
# - The Z-axis of the point cloud is oriented vertically upwards, similar to the IFC model.
# Import required libraries
import ifcopenshell
import ifcopenshell.geom
import ifcopenshell.util
import ifcopenshell.util.shape
import open3d as o3d
import numpy as np
import math
import os
import copy
import time
import re
import itertools
from typing import List, Tuple
from collections import defaultdict
from scipy.spatial.transform import Rotation as R
import argparse
np.set_printoptions(precision=2, suppress=True)
def run_4plcs(ifc_path, pcd_path):
# Set the type of PCD. This allows for different parameter sets.
PCDTYPE = "TLS"
#PCDTYPE = "Other"
print(f"PCDTYPE is set to '{PCDTYPE}'")
###############################################################################
# # 1. Extract PCD 4PlCSs
###############################################################################
# Read Input pointcloud
pcd = o3d.io.read_point_cloud(pcd_path)
print(f"Input pcd {pcd}")
# Get the base name of the pcd file for the output file name
pcd_filename = os.path.splitext(os.path.basename(pcd_path))[0]
# Downsample pcd
if PCDTYPE == "TLS":
pcd_voxel_sampling_size = 0.05
else:
pcd_voxel_sampling_size = 0.05
downpcd_tmp = pcd.voxel_down_sample(pcd_voxel_sampling_size)
print(f"Point pcd voxel sampling size: {pcd_voxel_sampling_size}.")
print(f"Input downsampled PCD: {downpcd_tmp}.")
sampling_ratio = float(len(downpcd_tmp.points)) / float(len(pcd.points))
sampling_count = int(round(1/sampling_ratio))
downpcd_before_transform = pcd.uniform_down_sample(sampling_count)
print(f"Point pcd sampling ratio: {sampling_ratio} (sampling cout = {sampling_count}).")
print(f"Input uniformly downsampled PCD: {downpcd_before_transform}.")
# Calculate pcd normals
if PCDTYPE == "TLS":
my_radius = 0.20
else:
my_radius = 0.20
downpcd_before_transform.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=my_radius, max_nn=30))
assert (downpcd_before_transform.has_normals()), "Point cloud should have normals, but doesn't"
# o3d.visualization.draw_geometries([downpcd_before_transform], window_name="Downsampled Point Cloud")
##########################################################################
# ## Known Expected Location:
# Do we know the expected location?
# If no, then set `KNOWN_TARGET_LOCATION` to `False`.
# If yes, then set `KNOWN_TARGET_LOCATION` to `True` and give the location coordinate to `pcd_origin_target`.
KNOWN_TARGET_LOCATION = False
print(f"KNOWN_TARGET_LOCATION is set to '{KNOWN_TARGET_LOCATION}'")
pcd_origin_init = [0.0, 0.0, 0.0]
print(f"pcd_origin_init: {pcd_origin_init}")
if KNOWN_TARGET_LOCATION:
pcd_origin_target = [-3.4854, 1.5203, 1.00]
print(f"pcd_origin_target: {pcd_origin_target}")
##########################################################################
# ## Apply a random transformation to the PCD.
# This is useful for testing purposes if the input data is already aligned.
# Set APPLY_RANDOM_TRANSFORM to False to disable.
APPLY_RANDOM_TRANSFORM = False
print(f"APPLY_RANDOM_TRANSFORM is set to '{APPLY_RANDOM_TRANSFORM}'")
random_transform = np.eye(4)
if APPLY_RANDOM_TRANSFORM == True:
random_euler_deg = [0.0, 0.0, 33.0]
random_translation = [32.0, 12.5, 4.4]
# Build the 4x4 transformation matrix
random_rotation = R.from_euler('xyz', random_euler_deg, degrees=True).as_matrix()
random_transform[:3, :3] = random_rotation
random_transform[:3, 3] = random_translation
# Visualize before and after random transformation with IFC mesh
original_pcd_viz = copy.deepcopy(downpcd_before_transform)
transformed_pcd_viz = copy.deepcopy(downpcd_before_transform)
transformed_pcd_viz.transform(random_transform)
# Color the point clouds for clarity
original_pcd_viz.paint_uniform_color([0, 0, 1]) # Blue: original
transformed_pcd_viz.paint_uniform_color([1, 0, 0]) # Red: transformed
o3d.visualization.draw_geometries([original_pcd_viz, transformed_pcd_viz])
print("Random Transform:")
print(random_transform)
downpcd = downpcd_before_transform.transform(random_transform)
# ## Extract planar patches from the PCD.
# This section detects planar patches in the point cloud using Open3D's `detect_planar_patches`.
# The detected patches are then filtered based on minimum area and maximum thickness.
# A subsampled version of each patch's point cloud is also stored.
stime_pcd_patches = time.time()
if PCDTYPE == "TLS":
extract_patch_area_min = 0.25 #m^2
point_area = pow(pcd_voxel_sampling_size, 2)
my_min_plane_edge_length = 0.4
normal_variance_threshold_deg = 10.0
coplanarity_deg = 87.0
outlier_ratio = 0.85
min_num_points = int(round(extract_patch_area_min / point_area))
else: # Other
extract_patch_area_min = 0.25 #m^2
point_area = pow(pcd_voxel_sampling_size, 2)
my_min_plane_edge_length = 0.4
normal_variance_threshold_deg = 10.0
coplanarity_deg = 87.0
outlier_ratio = 0.85
min_num_points = int(round(extract_patch_area_min / point_area))
oboxes = downpcd.detect_planar_patches(
normal_variance_threshold_deg = normal_variance_threshold_deg,
coplanarity_deg = coplanarity_deg,
outlier_ratio = outlier_ratio,
min_plane_edge_length = my_min_plane_edge_length,
min_num_points = min_num_points,
search_param = o3d.geometry.KDTreeSearchParamKNN(knn=30))
print(f"Number of patches: {len(oboxes)}.")
# Create a list of patches, filtering out those that are too small or too thick.
print("Create patches list with bounding boxes and point clouds.")
if PCDTYPE == "TLS":
patch_thickness_max = 0.10
patch_area_min = 1.0
else: # Other
patch_thickness_max = 0.10
patch_area_min = 1.0
print(f"Minimum patch area set to {patch_area_min} m^2, and minimum patch thickness set to {patch_thickness_max} m.")
patch_sampling_count = 10
patch_sampling_max = 1000
print(f"Patch sampling: Count={patch_sampling_count}; Max={patch_sampling_max}")
pcd_patches = []
pcd_patches_too_small = []
pcd_patches_too_thick = []
for idb, patch_obox in enumerate(oboxes):
mesh = o3d.geometry.TriangleMesh.create_from_oriented_bounding_box(patch_obox)
mesh.paint_uniform_color(patch_obox.color)
patch_indices = patch_obox.get_point_indices_within_bounding_box(downpcd.points)
patch_pcd = downpcd.select_by_index(patch_indices)
# Discard small patches
patch_area = len(patch_pcd.points) * point_area
if patch_area < patch_area_min:
pcd_patches_too_small.append(patch_obox)
continue
# Discard thick patches
if np.min(patch_obox.extent) > patch_thickness_max:
pcd_patches_too_thick.append(patch_obox)
continue
patch_pcd.paint_uniform_color(patch_obox.color)
patch_center = np.asarray(patch_pcd.points).mean(axis=0)
patch_normal = patch_obox.R[:,2]
d = -np.dot(patch_normal, patch_center)
a, b, c = patch_normal
patch_model = [a, b, c, d]
patch_downpcd = patch_pcd.uniform_down_sample(patch_sampling_count)
patch_downpcd_size = len(patch_downpcd.points)
if patch_downpcd_size > patch_sampling_max:
indices = np.random.choice(patch_downpcd_size, patch_sampling_max, replace=False)
patch_downpcd = patch_downpcd.select_by_index(indices)
pcd_patches.append({"obox": patch_obox,
"pcd": patch_pcd,
"downpcd": patch_downpcd,
"center": patch_center,
"normal": patch_normal,
"model": patch_model,
"area": patch_area})
# Sort all patches by area in descending order.
pcd_patches = sorted(pcd_patches, key=lambda p: p['area'], reverse=True)
etime_pcd_patches = time.time()
duration_pcd_patches = etime_pcd_patches - stime_pcd_patches
print(f"Number of patches extracted from pcd that are too small: {len(pcd_patches_too_small)}")
print(f"Number of patches extracted from pcd that are too thick: {len(pcd_patches_too_thick)}")
print(f"Number of patches extracted from pcd that are valid: {len(pcd_patches)}")
print(f"Time to extract patches: {duration_pcd_patches:.2f} s")
# Visualize the detected PCD patches.
print("Draw detected patches, horizontal and/or no-horizontal, with meshes and/or point clouds")
horizontal = False
not_horizontal = False # Set to False to speed up visualization
print(f"Horizontal planes displayed: {horizontal}; Non-horizontal patches displayed: {not_horizontal}")
display_obox = False
display_points = True
geometries_pcd = []
z_axis = [0, 0, 1.0]
for patch in pcd_patches:
patch_obox = patch["obox"]
patch_pcd = patch["downpcd"]
patch_normal = patch["normal"]
abs_dotproduct = np.absolute(np.dot(patch_normal, z_axis))
if not_horizontal == True and abs_dotproduct < 0.8 :
if display_obox == True:
geometries_pcd.append(patch_obox)
if display_points == True:
geometries_pcd.append(patch_pcd)
elif horizontal == True and abs_dotproduct > 0.8 :
if display_obox == True:
geometries_pcd.append(patch_obox)
if display_points == True:
geometries_pcd.append(patch_pcd)
# o3d.visualization.draw_geometries(geometries_pcd, window_name="PCD Patches")
# ## Extract 4-Point Congruent Sets (4PlCSs) from the PCD patches.
# A valid 4PlCS consists of four planar patches where:
# - Two patches are parallel to each other.
# - The other three patches are not pairwise parallel.
# This section filters the combinations of patches to find valid and useful 4PlCSs.
def angle_between_vectors_deg(n1: np.ndarray, n2: np.ndarray) -> float:
"""Returns angle in degrees between two normals."""
cos_theta = np.clip(np.dot(n1, n2) / (np.linalg.norm(n1) * np.linalg.norm(n2)), -1.0, 1.0)
angle = np.arccos(cos_theta)
return np.degrees(angle)
def are_normals_parallel(n1: np.ndarray, n2: np.ndarray, angle_thresh=5.0) -> bool:
angle = angle_between_vectors_deg(n1, n2)
return angle < angle_thresh or 180 - angle < angle_thresh
def are_normals_parallel(angle_deg: float, angle_thresh=5.0) -> bool:
return angle_deg < angle_thresh or (180 - angle_deg) < angle_thresh
def are_coplanar(plane1: np.ndarray, plane2: np.ndarray, angle_thresh=5.0, dist_thresh=0.05) -> bool:
"""
Checks if two planes are (nearly) coplanar: same normal direction and same offset from origin.
"""
n1 = plane1[:3]
n2 = plane2[:3]
if are_normals_parallel(n1, n2, angle_thresh):
# Use normalized offset to compare distances
d1 = plane1[3] / np.linalg.norm(n1)
d2 = plane2[3] / np.linalg.norm(n2)
dist_diff = abs(d1 - d2)
return dist_diff < dist_thresh
def intersect_planes(plane1: np.ndarray, plane2: np.ndarray, plane3: np.ndarray) -> np.ndarray:
"""
Solves for the intersection point of 3 planes using linear algebra.
"""
A = np.vstack([plane1[:3], plane2[:3], plane3[:3]])
b = -np.array([plane1[3], plane2[3], plane3[3]])
try:
return np.linalg.solve(A, b)
except np.linalg.LinAlgError:
return None # Planes do not intersect at a single point (e.g., parallel)
def find_valid_patch_combinations(patches: list, parallel_angle_thresh: float = 5.0,
coplanar_dist_thresh: float = 0.05, plane_dist_thresh: float = 3.0) -> List[Tuple[int, int, int, int]]:
"""
Finds all valid 4-plane combinations from the given list of patches.
Parameters:
- patches: List of patches.
- coplanar_threshold: Max allowed distance between parallel patches to consider them coplanar.
- parallel_threshold: Max angle difference (in radians) to consider normals parallel.
Returns:
- List of valid combinations (each as a tuple of indices).
"""
print(f"There are {len(patches)} patches.")
# First compute all pairwise angles and distances:
pairwise_angles = np.full((len(patches), len(patches)), -1.0, dtype=float)
pairwise_distances = np.full((len(patches), len(patches)), -1.0, dtype=float)
for pair in itertools.combinations(range(len(patches)), 2):
i, j = pair
patch_normal1 = patches[i]["normal"]
patch_center1 = patches[i]["center"]
patch_normal2 = patches[j]["normal"]
patch_center2 = patches[j]["center"]
pairwise_angles[i, j] = angle_between_vectors_deg(patch_normal1, patch_normal2)
pairwise_distances[i, j] = np.absolute(np.dot(patch_center2 - patch_center1, patch_normal1))
print(f"Pairwise angles and distances between all patches are pre-calculated.")
# Now, create all sets of valid combinations
valid_triplet = 0
valid_combinations = []
notideal_combinations = []
notvalid_combinations = []
combinations = itertools.combinations(range(len(patches)), 4)
print(f"There are {len(list(combinations))} combinations of patches.")
for comb in itertools.combinations(range(len(patches)), 4):
nonparallel_triplet_found = False
for triplet in itertools.combinations(comb, 3):
parallel_patch_found = False
for a, b in itertools.combinations(triplet, 2):
pairwise_angle = pairwise_angles[a, b]
if are_normals_parallel(pairwise_angle, parallel_angle_thresh):
parallel_patch_found = True
break
if parallel_patch_found == False:
nonparallel_triplet_found = True
break
if nonparallel_triplet_found == False:
notvalid_combinations.append(comb)
continue
valid_triplet += 1
# Check if two patches are nearly parallel and sufficiently distant or co-planar
coplanar_found = False
valid_parallel_pair_found = False
for pair in itertools.combinations(comb, 2):
i, j = pair
pairwise_angle_ij = pairwise_angles[i, j]
# Check if parallel:
if are_normals_parallel(pairwise_angle_ij, parallel_angle_thresh):
distance_ij = pairwise_distances[i, j]
# Check if co-planar:
if distance_ij < coplanar_dist_thresh:
coplanar_found = True
break
# Check if parallel planes are far enough from each other.
elif distance_ij > plane_dist_thresh:
valid_parallel_pair_found = True
break # No need to check other pairs
if coplanar_found:
notvalid_combinations.append(comb)
continue
if not valid_parallel_pair_found:
notideal_combinations.append(comb)
continue
# Calculate the all relative patch angles and patch orthogonal distance between i and j
sublist = [x for x in comb if x != i and x != j]
k, l = sublist
angle_ik = pairwise_angles[i, k]
angle_il = pairwise_angles[i, l]
angle_kl = pairwise_angles[k, l]
description = [angle_ik, angle_il, angle_kl, distance_ij]
area = patches[i]["area"] + patches[j]["area"] + patches[k]["area"] + patches[l]["area"]
comb_ordered = [i, k ,l, j]
# Add plane combination to list of valid plane combinations, i.e. is a 4PlCS
valid_combinations.append({"patch_ids": comb_ordered, "patch_geometry": description, "area": area})
print(f"There are {valid_triplet} valid patch triplets.")
print(f"There are {len(notvalid_combinations)} not valid 4PlCSs.")
print(f"There are {len(notideal_combinations)} not ideal 4PlCSs.")
print(f"There are {len(valid_combinations)} valid 4PlCSs.")
return valid_combinations
def find_largest_4PlCSs (a4PlCSs: list, patches: list, max_patches: int):
# Select the max_patches patches with the largest area (patches is already sorted according to "area")
patches_filtered = patches[:max_patches]
# Select the 4PlCSs that contain at least one of the largest patches
patches_filtered_ids = range(len(patches_filtered))
filtered_4PlCSs = [a4PlCS for a4PlCS in a4PlCSs if any(id_ in patches_filtered_ids for id_ in a4PlCS['patch_ids'])] # type: ignore
print (f"There are {len(filtered_4PlCSs)} filtered 4PlCSs.")
return filtered_4PlCSs
# Extract PCD 4PlCSs
stime_pcd_4PlCSs = time.time()
if PCDTYPE == "TLS":
coplanar_angle_thresh = 5.0
coplanar_dist_thresh = 0.05
plane_dist_thresh = 1.5
else:
coplanar_angle_thresh = 5.0
coplanar_dist_thresh = 0.05
plane_dist_thresh = 1.5
pcd_4PlCSs = find_valid_patch_combinations(pcd_patches, coplanar_angle_thresh, coplanar_dist_thresh, plane_dist_thresh)
# Select the top largest PCD 4PlCSs for matching.
# NOTE: This could be improved by using a minimum area threshold instead of a fixed number.
# --------------------------------
pcd_patches_max = 20
pcd_4PlCSs_largest = find_largest_4PlCSs(pcd_4PlCSs, pcd_patches, pcd_patches_max)
pcd_4PlCSs_largest = sorted(pcd_4PlCSs_largest , key=lambda p: p['area'], reverse=True)
# Filter to the top N 4PlCSs
pcd_4PlCSs_max = 100
pcd_4PlCSs_filtered = pcd_4PlCSs_largest[:pcd_4PlCSs_max]
print (f"There are {len(pcd_4PlCSs_filtered)} filtered 4PlCSs in the pcd.")
etime_pcd_4PlCSs = time.time()
duration_pcd_4PlCSs = etime_pcd_4PlCSs - stime_pcd_4PlCSs
print(f"Time to extract 4PlCSs: {duration_pcd_4PlCSs:.2f} s")
###############################################################################
# # 2. Loading and Processing IFC Model
###############################################################################
# ## Load the IFC file and convert its geometry to Open3D meshes.
# We load an IFC file and keep only the elements of certain classes (currently "IfcWall", "IfcSlab" and "IfcBeam").
def ifc_shape_to_open3d_mesh(shape):
vertices = np.array(shape.geometry.verts, dtype=np.float64).reshape(-1, 3)
triangles = np.array(shape.geometry.faces, dtype=np.int32).reshape(-1, 3)
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(vertices)
mesh.triangles = o3d.utility.Vector3iVector(triangles)
mesh.compute_triangle_normals()
return mesh
# Define the element types for the analysis
filtered_element_types = ["IfcWall","IfcSlab","IfcBeam"]
# Load ifc file
print(f"Input IFC model path: {ifc_path}")
if not os.path.exists(ifc_path):
print(f"Error: File not found at {ifc_path}")
else:
print(f"Opening file: {ifc_path}")
# Load the IFC model and filter objects by specified element type
ifc_file = ifcopenshell.open(ifc_path)
# Initialize geometry processing settings and iterator
tree = ifcopenshell.geom.tree()
settings = ifcopenshell.geom.settings()
settings.set(settings.USE_WORLD_COORDS, True)
#settings.set(settings.SEW_SHELLS, True)
# Initialize Open3D structures
elements = []
for type_name in filtered_element_types:
elements.extend(ifc_file.by_type(type_name))
# Convert elements to triangle meshes
all_triangles = []
all_meshes = []
for element in elements:
shape = ifcopenshell.geom.create_shape(settings, element)
element_mesh = ifc_shape_to_open3d_mesh(shape)
element_mesh.compute_triangle_normals()
all_meshes.append(element_mesh)
# Plot loaded IFC file
# o3d.visualization.draw_geometries(all_meshes, mesh_show_back_face=True, window_name="IFC Model")
# ## Extract planar patches from the IFC mesh.
# This section extracts planar patches from the IFC mesh using a region-growing approach based on triangle normal similarity.
# The detected patches are then filtered based on a minimum area.
def build_triangle_adjacency(mesh):
triangles = np.asarray(mesh.triangles)
edge_to_triangles = defaultdict(list)
# For each triangle, map its edges to the triangle index
for idx, tri in enumerate(triangles):
edges = [(tri[0], tri[1]), (tri[1], tri[2]), (tri[2], tri[0])]
for edge in edges:
edge_sorted = tuple(sorted(edge)) # order doesn't matter for undirected edges
edge_to_triangles[edge_sorted].append(idx)
# Build adjacency list
adjacency = [[] for _ in range(len(triangles))]
for edge, tris in edge_to_triangles.items():
if len(tris) == 2:
t1, t2 = tris
adjacency[t1].append(t2)
adjacency[t2].append(t1)
return adjacency
def extract_planar_patches(mesh, angle_threshold_deg=10):
triangles = np.asarray(mesh.triangles)
triangle_normals = np.asarray(mesh.triangle_normals)
# Build adjacency list
adjacency = build_triangle_adjacency(mesh)
angle_threshold_rad = math.radians(angle_threshold_deg)
visited = np.zeros(len(triangles), dtype=bool)
patches = []
for i in range(len(triangles)):
if visited[i]:
continue
# Start a new patch
patch = [i]
visited[i] = True
queue = [i]
while queue:
current = queue.pop()
current_normal = triangle_normals[current]
for neighbor in adjacency[current]:
if visited[neighbor]:
continue
neighbor_normal = triangle_normals[neighbor]
angle = np.arccos(
np.clip(np.dot(current_normal, neighbor_normal), -1.0, 1.0)
)
if angle < angle_threshold_rad:
visited[neighbor] = True
patch.append(neighbor)
queue.append(neighbor)
patches.append(patch)
return patches # List of lists of triangle indices
def triangles_to_mesh(triangles):
# Flatten triangle points into a list of points
all_points = np.vstack(triangles)
# Use numpy unique to remove duplicates and build index mapping
unique_points, inverse_indices = np.unique(all_points.round(decimals=8), axis=0, return_inverse=True)
# Rebuild triangles as indices
triangle_indices = []
for i in range(len(triangles)):
idx = inverse_indices[i * 3 : (i + 1) * 3]
triangle_indices.append(idx)
# Create Open3D mesh
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(unique_points)
mesh.triangles = o3d.utility.Vector3iVector(triangle_indices)
return mesh
def extract_submesh(mesh, triangle_indices):
triangles = np.asarray(mesh.triangles)
vertices = np.asarray(mesh.vertices)
selected_triangles = triangles[triangle_indices]
# Find the unique vertices used in the selected triangles
unique_vertex_indices, new_indices = np.unique(selected_triangles.flatten(), return_inverse=True)
# Map triangles to new indices
new_triangles = new_indices.reshape(-1, 3)
# Create new mesh
new_vertices = vertices[unique_vertex_indices]
submesh = o3d.geometry.TriangleMesh()
submesh.vertices = o3d.utility.Vector3dVector(new_vertices)
submesh.triangles = o3d.utility.Vector3iVector(new_triangles)
submesh.compute_vertex_normals()
submesh.compute_triangle_normals()
return submesh
def compute_patch_mesh_normal_center_model(mesh, patch_indices):
# Model
patch_mesh = extract_submesh(mesh, patch_indices)
# Normal and Center
triangles = np.asarray(mesh.triangles)
vertices = np.asarray(mesh.vertices)
triangle_normals = np.asarray(mesh.triangle_normals)
area_weighted_normal = np.zeros(3)
patch_centroid = np.zeros(3)
total_area = 0.0
for idx in patch_indices:
tri = triangles[idx]
v0, v1, v2 = vertices[tri[0]], vertices[tri[1]], vertices[tri[2]]
# Triangle area
area = 0.5 * np.linalg.norm(np.cross(v1 - v0, v2 - v0))
total_area += area
# Weighted normal
area_weighted_normal += triangle_normals[idx] * area
# Centroid contribution
tri_centroid = (v0 + v1 + v2) / 3.0
patch_centroid += tri_centroid * area
if total_area > 0:
patch_normal = area_weighted_normal / np.linalg.norm(area_weighted_normal)
patch_center = patch_centroid / total_area
else:
patch_normal = np.array([0.0, 0.0, 1.0]) # Fallback normal
patch_center = np.zeros(3)
# Plane model: ax + by + cz + d = 0
a, b, c = patch_normal
d = -np.dot(patch_normal, patch_center)
plane_model = (a, b, c, d)
return patch_mesh, patch_normal, patch_center, plane_model
def compute_patch_obox(mesh, thickness):
# Get vertices
vertices = np.asarray(mesh.vertices)
# Compute centroid
centroid = vertices.mean(axis=0)
# Subtract mean
centered = vertices - centroid
# PCA to get normal
cov = np.cov(centered.T)
eigvals, eigvecs = np.linalg.eigh(cov)
normal = eigvecs[:, 0] # Smallest eigenvector → plane normal
# In-plane basis vectors
v1 = eigvecs[:, 1]
v2 = eigvecs[:, 2]
# Project to 2D plane coordinate system
projections = np.dot(centered, np.stack([v1, v2], axis=1))
# 2D bounding box
min_proj = projections.min(axis=0)
max_proj = projections.max(axis=0)
# Reconstruct corners in 3D and add thickness along normal
corners = []
for dx in [0, 1]:
for dy in [0, 1]:
for dz in [-thickness / 2, thickness / 2]: # Add thickness
point2d = min_proj + (max_proj - min_proj) * [dx, dy]
corner3d = centroid + point2d[0] * v1 + point2d[1] * v2 + dz * normal
corners.append(corner3d)
# Create OBB from corners
obox = o3d.geometry.OrientedBoundingBox.create_from_points(o3d.utility.Vector3dVector(corners))
return obox
def compute_patch_area(mesh):
triangles = np.asarray(mesh.triangles)
vertices = np.asarray(mesh.vertices)
total_area = 0.0
for tri in triangles:
v0, v1, v2 = vertices[tri[0]], vertices[tri[1]], vertices[tri[2]]
area = 0.5 * np.linalg.norm(np.cross(v1 - v0, v2 - v0))
total_area += area
return total_area
# Extract IFC Patches
stime_ifc_patches = time.time()
if PCDTYPE == "TLS":
ifc_patch_obox_thickness = 0.10
else:
ifc_patch_obox_thickness = 0.10
ifc_patches = []
ifc_patches_too_small = []
ifc_patches_wrong_orientation = []
ifc_patches_total = 0
for mesh in all_meshes:
patches = extract_planar_patches(mesh, angle_threshold_deg=normal_variance_threshold_deg)
ifc_patches_total += len(patches)
for patch in patches:
patch_mesh, patch_normal, patch_center, patch_model = compute_patch_mesh_normal_center_model(mesh, patch)
patch_area = compute_patch_area(patch_mesh)
# Discard patches that are too small:
if patch_area < patch_area_min:
ifc_patches_too_small.append(patch_mesh)
continue
# Discard wrongly oriented patches (if known location):
if KNOWN_TARGET_LOCATION:
if np.dot(pcd_origin_target - patch_center, patch_normal) < 0.0:
ifc_patches_wrong_orientation.append(patch_mesh)
continue
patch_obox = compute_patch_obox(patch_mesh, thickness=ifc_patch_obox_thickness)
patch_obox.color = np.array([0.3, 0.3, 0.3], dtype=np.float32)
ifc_patches.append({"mesh": patch_mesh,
"center": patch_center,
"normal": patch_normal,
"model": patch_model,
"obox": patch_obox,
"area": patch_area})
# Sort all patches by area
ifc_patches = sorted(ifc_patches, key=lambda p: p['area'], reverse=True)
print(f"Found {ifc_patches_total} planar patches.")
print(f"Found {len(ifc_patches)} valid planar patches.")
if KNOWN_TARGET_LOCATION:
print(f"Found {len(ifc_patches_wrong_orientation)} wrongly oriented planar patches (because known pcd origin target location).")
print(f"Found {len(ifc_patches_too_small)} too small planar patches.")
etime_ifc_patches = time.time()
duration_ifc_patches = etime_ifc_patches - stime_ifc_patches
print(f"Time to extract ifc patches: {duration_ifc_patches:.2f} s")
# ## Extract 4PlCSs from the IFC patches.
# This section extracts valid and useful 4PlCSs from the IFC patches using the same method as for the point cloud patches.
# Extract IFC 4PlCSs
stime_ifc_4PlCSs = time.time()
ifc_4PlCSs = find_valid_patch_combinations(ifc_patches, coplanar_angle_thresh, coplanar_dist_thresh, plane_dist_thresh)
# Select Largest IFC 4PlCSs
ifc_patches_max = 50
ifc_4PlCSs_largest = find_largest_4PlCSs(ifc_4PlCSs, ifc_patches, ifc_patches_max)
ifc_4PlCSs_largest = sorted(ifc_4PlCSs_largest, key=lambda p: p['area'], reverse=True)
match_4PlCSs_max = 100000
ifc_4PlCSs_max = round(match_4PlCSs_max / pcd_4PlCSs_max)
ifc_4PlCSs_filtered = ifc_4PlCSs_largest[:ifc_4PlCSs_max]
print (f"There are {len(ifc_4PlCSs_filtered)} largest 4PlCSs in the ifc.")
etime_ifc_4PlCSs = time.time()
duration_ifc_4PlCSs = etime_ifc_4PlCSs - stime_ifc_4PlCSs
print(f"Time to extract ifc 4PlCSs: {duration_ifc_4PlCSs:.2f} s")
###########################################################################
# # 3. Match 4PlCSs
# ## Find the best transformation by matching 4PlCSs.
# For each pair of PCD and IFC 4PlCSs, this section:
# 1. Checks if their internal geometries match.
# 2. Calculates the possible rigid transformations between them.
# 3. Computes the support for each transformation (how many PCD points align with IFC patches).
# 4. Filters transformations based on minimum support and removes duplicates.
# Plot all pcd and ifc patches together for visualization
geom = []
for ifc_patch in ifc_patches:
ifc_patch_mesh = ifc_patch["obox"]
geom.append(ifc_patch_mesh)
for pcd_patch in pcd_patches:
pcd_patch_pcd = pcd_patch["downpcd"]
geom.append(pcd_patch_pcd)
# o3d.visualization.draw_geometries(geom, mesh_show_back_face=True)
def intersect_three_planes(plane1, plane2, plane3):
"""
Computes the intersection point of three planes given in (a, b, c, d) form.
"""
# Normals of the planes
n1 = np.array(plane1[:3])
n2 = np.array(plane2[:3])
n3 = np.array(plane3[:3])
# Coefficient matrix (normals stacked as rows)
A = np.vstack([n1, n2, n3])
# Right-hand side: -d terms
d = np.array([-plane1[3], -plane2[3], -plane3[3]])
# Solve the linear system
try:
point = np.linalg.solve(A, d)
except np.linalg.LinAlgError as e:
raise ValueError("Planes do not intersect at a single point (they may be parallel or coplanar).") from e
return point
def align_vectors(A, B):
"""
Aligns vector A to vector B using a rigid transformation (rotation + translation)
to minimize the MSE between corresponding points.
"""
assert A.shape == B.shape, "Vectors must have the same shape"
# Compute centroids
centroid_A = A.mean(axis=0)
centroid_B = B.mean(axis=0)
# Center the vectors
AA = A - centroid_A
BB = B - centroid_B
# Compute optimal rotation using SVD
H = AA.T @ BB
U, _, Vt = np.linalg.svd(H)
R = Vt.T @ U.T
# Fix improper rotation (reflection)
if np.linalg.det(R) < 0:
Vt[2, :] *= -1
R = Vt.T @ U.T
# Compute translation
t = centroid_B - R @ centroid_A
# Apply transformation
A_aligned = (R @ A.T).T + t
# Compute mean squared error
mse = np.mean(np.linalg.norm(A_aligned - B, axis=1)**2)
return {"R": R, "t": t, "mes": mse}
def transform_plane(plane, transformation):
"""
Applies rotation R and translation t to a plane equation (a, b, c, d).
"""
n = np.array(plane[:3]) # normal
d = plane[3]
R = transformation[:3, :3]
t = transformation[:3, 3]
# Rotate the normal
n_rot = R @ n
# Get a point on the original plane
p = -d * n / np.dot(n, n)
# Transform the point
p_transformed = R @ p + t
# Compute new d
d_new = -np.dot(n_rot, p_transformed)
return np.append(n_rot, d_new)
def find_rotation_angles_around_line(p0, v, planeA1, planeA2, planeB1, planeB2, angle_tol=0.05):
"""
Finds all rotation angles (in radians) around line (p0, v) that align planeA1,A2 with planeB1,B2.
Returns a list of (angle_rad, rotation_matrix_4x4) tuples.
"""
v = v / np.linalg.norm(v)
nA1 = np.array(planeA1[:3])
nA2 = np.array(planeA2[:3])
nB1 = np.array(planeB1[:3])
nB2 = np.array(planeB2[:3])
# Remove axial components (only perpendicular rotation matters)
def orth_proj(n): return n - np.dot(n, v) * v
nA1_proj = orth_proj(nA1)
nA2_proj = orth_proj(nA2)
nB1_proj = orth_proj(nB1)
nB2_proj = orth_proj(nB2)
# Normalize projected normals
nA1_proj /= np.linalg.norm(nA1_proj)
nA2_proj /= np.linalg.norm(nA2_proj)
nB1_proj /= np.linalg.norm(nB1_proj)
nB2_proj /= np.linalg.norm(nB2_proj)
def angle_between(u, v):
cross = np.cross(u, v)
dot = np.dot(u, v)
angle = np.arctan2(np.linalg.norm(cross), dot)
return angle
def rotate_around_line(point, axis, angle_rad):
"""
Returns a 4x4 transformation matrix rotating around a line defined by (point, axis).
"""
axis = axis / np.linalg.norm(axis)
rot = R.from_rotvec(angle_rad * axis).as_matrix()
Transformation = np.eye(4)
Transformation[:3, :3] = rot
Transformation[:3, 3] = point - rot @ point
return Transformation
# Try two angle candidates: θ1 from A1→B1, θ2 from A1→-B1
candidate_angles = []
for sign1 in [+1, -1]:
θ1 = angle_between(sign1 * nA1_proj, nB1_proj)
R1 = R.from_rotvec(θ1 * v).as_matrix()
nA2_rotated = R1 @ nA2_proj
for sign2 in [+1, -1]:
angle_diff = angle_between(nA2_rotated, sign2 * nB2_proj)
if angle_diff < angle_tol:
# Valid alignment found
T1 = rotate_around_line(p0, v, θ1)
if not any(np.isclose(θ1, v["angle"], atol=0.02) for v in candidate_angles):
candidate_angles.append({"angle": θ1, "transformation": T1, "angle_diff": angle_diff})
θ2 = θ1 + np.pi
T2 = rotate_around_line(p0, v, θ2)
if not any(np.isclose(θ2, v["angle"], atol=0.02) for v in candidate_angles):
candidate_angles.append({"angle": θ2, "transformation": T2, "angle_diff": angle_diff})
return candidate_angles
def angle_sets_equal(set1: np.ndarray, set2: np.ndarray, tol:float=1.0) -> bool:
unmatched_set2 = list(set2)
for val_1 in set1:
found_match = False
for i, val_2 in enumerate(unmatched_set2):
if math.isclose(val_1, val_2, abs_tol=tol):
unmatched_set2.pop(i)
found_match = True
break
if not found_match:
return False
return True
def match_geometry(pcd_geometry, ifc_geometry, diff_distance_max = 0.2, diff_angle_max = 4.0):