1+ #! python3
2+
3+ import System
4+ import typing
5+
6+ import Rhino
7+ import Rhino .Geometry as rg
8+ from ghpythonlib .componentbase import executingcomponent as component
9+
10+ import Grasshopper as gh
11+ from Grasshopper .Kernel import GH_RuntimeMessageLevel as RML
12+
13+ import diffCheck
14+ from diffCheck import diffcheck_bindings
15+ from diffCheck import df_cvt_bindings
16+ import diffCheck .df_util
17+
18+
19+ class ICPRegistration (component ):
20+ def RunScript (self ,
21+ i_cloud_source : rg .PointCloud ,
22+ i_cloud_target : rg .PointCloud ,
23+
24+ i_use_generalized_icp : bool ,
25+
26+ i_max_corrspondence_dist : float ,
27+ i_max_iteration : int ,
28+
29+ is_t_estimate_pt2pt : bool , # valid only for 03dicp
30+ i_use_point_to_plane : bool # valid only for 03dicp
31+ ) -> rg .Transform :
32+ """
33+ The global registration component aligns two point clouds in a rough way.
34+
35+ :param i_cloud_source: source point cloud
36+ :param i_cloud_target: target point cloud to align to
37+ :param i_use_generalized_icp: if true, it uses the generalized ICP algorithm
38+ :param i_max_corrspondence_dist: maximum correspondence distance
39+ :param i_max_iteration: maximum number of iterations
40+ :param is_t_estimate_pt2pt: (valid only for 03dicp) if true, it deforms the point cloud
41+ :param i_use_point_to_plane: (valid only for 03dicp) if true, it uses point to plane registration
42+
43+ :return o_x_form : transformation matrix
44+ """
45+ if i_cloud_source is None or i_cloud_target is None :
46+ ghenv .Component .AddRuntimeMessage (RML .Warning , "Please provide both objects of type point clouds to align" )
47+ return None
48+
49+ # set default values
50+ if i_use_generalized_icp is None : i_use_generalized_icp = True
51+ if i_max_corrspondence_dist is None : i_max_corrspondence_dist = 5
52+ if i_max_iteration is None : i_max_iteration = 50
53+
54+ # get the working unit of the Rhino document, if other than meters, set a multiplier factor
55+ scalef = diffCheck .df_util .get_doc_2_meters_unitf ()
56+ i_max_corrspondence_dist *= scalef
57+
58+ # conversion
59+ df_cloud_source = df_cvt_bindings .cvt_rhcloud_2_dfcloud (i_cloud_source )
60+ df_cloud_target = df_cvt_bindings .cvt_rhcloud_2_dfcloud (i_cloud_target )
61+
62+ # fast registration
63+ # these are the only hardcoded values since it will get the best result
64+ RELATIVE_FITNESS = 1e-6
65+ RELATIVE_RMSE = 1e-6
66+
67+ df_xform = None
68+ if i_use_generalized_icp :
69+ df_xform = diffcheck_bindings .dfb_registrations .DFRefinedRegistration .O3DGeneralizedICP (
70+ source = df_cloud_source ,
71+ target = df_cloud_target ,
72+ max_correspondence_distance = i_max_corrspondence_dist ,
73+ max_iteration = i_max_iteration ,
74+ relative_fitness = RELATIVE_FITNESS ,
75+ relative_rmse = RELATIVE_RMSE
76+ )
77+ else :
78+ df_xform = diffcheck_bindings .dfb_registrations .DFRefinedRegistration .O3DICP (
79+ source = df_cloud_source ,
80+ target = df_cloud_target ,
81+ max_correspondence_distance = i_max_corrspondence_dist ,
82+ is_t_estimate_pt2pt = is_t_estimate_pt2pt ,
83+ relative_fitness = RELATIVE_FITNESS ,
84+ relative_rmse = RELATIVE_RMSE ,
85+ max_iteration = i_max_iteration ,
86+ use_point_to_plane = i_use_point_to_plane
87+ )
88+ print ("-------------------" )
89+ print ("Estimated transformation matrix:" )
90+ print (df_xform .transformation_matrix )
91+ print ("-------------------" )
92+
93+ # cvt df xform to rhino xform
94+ df_xform_matrix = df_xform .transformation_matrix
95+ rh_form = rg .Transform ()
96+ for i in range (4 ):
97+ for j in range (4 ):
98+ rh_form [i , j ] = df_xform_matrix [i , j ]
99+ if rh_form == rg .Transform .Identity :
100+ ghenv .Component .AddRuntimeMessage (RML .Warning , "The transformation matrix is identity, no transformation is applied" )
101+ return None
102+
103+ o_x_form = rh_form
104+
105+ return o_x_form
106+
107+
108+ if __name__ == "__main__" :
109+ com = ICPRegistration ()
110+ o_x_form = com .RunScript (
111+ i_cloud_source ,
112+ i_cloud_target ,
113+
114+ i_use_generalized_icp ,
115+
116+ i_max_corrspondence_dist ,
117+ i_max_iteration ,
118+
119+ is_t_estimate_pt2pt ,
120+ i_use_point_to_plane
121+ )
0 commit comments