|
1 | 1 | #! python3 |
2 | 2 |
|
3 | 3 | import Rhino |
| 4 | + |
4 | 5 | import diffCheck |
5 | 6 | from diffCheck import diffcheck_bindings |
6 | 7 | from diffCheck import df_cvt_bindings as df_cvt |
7 | 8 | import diffCheck.df_util |
8 | | -import scriptcontext as sc |
9 | 9 |
|
10 | | -ABSTOL = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance |
| 10 | +from ghpythonlib.componentbase import executingcomponent as component |
11 | 11 |
|
12 | | -def main(i_clusters, i_joints, i_joint_ids): |
| 12 | +ABSTOL = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance |
13 | 13 |
|
14 | | - if len(i_joints) != len(i_joint_ids): |
15 | | - raise ValueError("The number of joints and joint ids must be the same.") |
16 | | - |
17 | | - if len(i_clusters) == 0: |
18 | | - raise ValueError("No clusters given.") |
| 14 | +class DFJointSegmentator(component): |
| 15 | + def RunScript(self, |
| 16 | + i_clusters: Rhino.Geometry.PointCloud, |
| 17 | + i_joints: Rhino.Geometry.Mesh, |
| 18 | + i_joint_ids: int, |
| 19 | + i_angle_threshold: float, |
| 20 | + i_distance_threshold: float): |
| 21 | + """ |
| 22 | + Amongst clusters, associates the clusters to the individual joints, |
| 23 | + creates a reference point cloud for each joint, |
| 24 | + and returns the joint segments, the reference point clouds, and the ICP transformations from the first to the second. |
| 25 | +
|
| 26 | + :param i_clusters: The clusters to be associated to the joints. |
| 27 | + :param i_joints: The joints to which the clusters will be associated. These are meshes. |
| 28 | + :param i_joint_ids: The joint ids of the joints. |
| 29 | + :param i_angle_threshold: The angle threshold for the association of the clusters to the joints. |
| 30 | + :param i_distance_threshold: The distance threshold for the association of the clusters to the joints. |
| 31 | + """ |
| 32 | + if i_angle_threshold is None : i_angle_threshold = 0.1 |
| 33 | + if i_distance_threshold is None : i_distance_threshold = 0.1 |
| 34 | + |
| 35 | + if len(i_joints) != len(i_joint_ids): |
| 36 | + raise ValueError("The number of joints and joint ids must be the same.") |
| 37 | + |
| 38 | + if len(i_clusters) == 0: |
| 39 | + raise ValueError("No clusters given.") |
19 | 40 |
|
20 | | - if not isinstance(i_clusters[0], Rhino.Geometry.PointCloud): |
21 | | - raise ValueError("The input clusters must be PointClouds.") |
22 | | - |
23 | | - if not isinstance(i_joints[0], Rhino.Geometry.Mesh): |
24 | | - raise ValueError("The input joints must be convertible to Meshes.") |
| 41 | + if not isinstance(i_clusters[0], Rhino.Geometry.PointCloud): |
| 42 | + raise ValueError("The input clusters must be PointClouds.") |
25 | 43 |
|
| 44 | + if not isinstance(i_joints[0], Rhino.Geometry.Mesh): |
| 45 | + raise ValueError("The input joints must be convertible to Meshes.") |
| 46 | + |
26 | 47 |
|
27 | | - # prepping the reference meshes |
28 | | - n_joints = max(i_joint_ids) + 1 |
29 | | - joints = [ [] for i in range(n_joints) ] |
30 | | - for face, id in zip(i_joints, i_joint_ids): |
31 | | - face.Subdivide() |
32 | | - face.Faces.ConvertQuadsToTriangles() |
33 | | - joints[id].append(df_cvt.cvt_rhmesh_2_dfmesh(face)) |
| 48 | + # prepping the reference meshes |
| 49 | + n_joints = max(i_joint_ids) + 1 |
| 50 | + joints = [ [] for i in range(n_joints) ] |
| 51 | + for face, id in zip(i_joints, i_joint_ids): |
| 52 | + face.Subdivide() |
| 53 | + face.Faces.ConvertQuadsToTriangles() |
| 54 | + joints[id].append(df_cvt.cvt_rhmesh_2_dfmesh(face)) |
34 | 55 |
|
35 | | - joint_clouds = [] |
36 | | - registrations = [] |
37 | | - joint_segments = [] |
38 | | - df_clouds = [df_cvt.cvt_rhcloud_2_dfcloud(cluster) for cluster in i_clusters] |
| 56 | + joint_clouds = [] |
| 57 | + registrations = [] |
| 58 | + joint_segments = [] |
| 59 | + df_clouds = [df_cvt.cvt_rhcloud_2_dfcloud(cluster) for cluster in i_clusters] |
39 | 60 |
|
40 | | - # for each joint, find the corresponding clusters and merge them, generate a reference point cloud, and register the merged clusters to the reference point cloud |
41 | | - for joint in joints: |
| 61 | + # for each joint, find the corresponding clusters and merge them, generate a reference point cloud, and register the merged clusters to the reference point cloud |
| 62 | + for joint in joints: |
42 | 63 |
|
43 | | - # create the reference point cloud |
44 | | - joint_cloud = diffcheck_bindings.dfb_geometry.DFPointCloud() |
| 64 | + # create the reference point cloud |
| 65 | + joint_cloud = diffcheck_bindings.dfb_geometry.DFPointCloud() |
45 | 66 |
|
46 | | - for face in joint: |
47 | | - face_cloud = face.sample_points_uniformly(1000) |
48 | | - joint_cloud.add_points(face_cloud) |
| 67 | + for face in joint: |
| 68 | + face_cloud = face.sample_points_uniformly(1000) |
| 69 | + joint_cloud.add_points(face_cloud) |
49 | 70 |
|
50 | | - joint_clouds.append(df_cvt.cvt_dfcloud_2_rhcloud(joint_cloud)) |
| 71 | + joint_clouds.append(df_cvt.cvt_dfcloud_2_rhcloud(joint_cloud)) |
51 | 72 |
|
52 | | - # find the corresponding clusters and merge them |
53 | | - segment = diffcheck_bindings.dfb_segmentation.DFSegmentation.associate_clusters(joint, df_clouds ) |
54 | | - diffcheck_bindings.dfb_segmentation.DFSegmentation.clean_unassociated_clusters(df_clouds, [segment], [joint], 0.1, 0.02) |
| 73 | + # find the corresponding clusters and merge them |
| 74 | + segment = diffcheck_bindings.dfb_segmentation.DFSegmentation.associate_clusters(joint, df_clouds, i_angle_threshold, i_distance_threshold) |
| 75 | + diffcheck_bindings.dfb_segmentation.DFSegmentation.clean_unassociated_clusters(df_clouds, [segment], [joint], i_angle_threshold ,i_distance_threshold) |
55 | 76 |
|
56 | | - # register the merged clusters to the reference point cloud |
57 | | - registration = diffcheck_bindings.dfb_registrations.DFRefinedRegistration.O3DICP(segment, joint_cloud) |
58 | | - res = registration.transformation_matrix |
| 77 | + # register the merged clusters to the reference point cloud |
| 78 | + registration = diffcheck_bindings.dfb_registrations.DFRefinedRegistration.O3DICP(segment, joint_cloud) |
| 79 | + res = registration.transformation_matrix |
59 | 80 |
|
60 | | - registrations.append(df_cvt.cvt_ndarray_2_rh_transform(res)) |
61 | | - joint_segments.append(df_cvt.cvt_dfcloud_2_rhcloud(segment)) |
| 81 | + registrations.append(df_cvt.cvt_ndarray_2_rh_transform(res)) |
| 82 | + joint_segments.append(df_cvt.cvt_dfcloud_2_rhcloud(segment)) |
62 | 83 |
|
63 | | - return joint_segments, joint_clouds, registrations |
| 84 | + return joint_segments, registrations, joint_clouds |
64 | 85 |
|
65 | | -if __name__ == "__main__": |
66 | | - o_joint_segments, o_reference_point_clouds, o_transforms = main(i_clusters, i_joints, i_joint_ids) |
| 86 | +# if __name__ == "__main__": |
| 87 | +# o_joint_segments, o_reference_point_clouds, o_transforms = main(i_clusters, i_joints, i_joint_ids) |
67 | 88 |
|
68 | | - for i in range(len(o_joint_segments)): |
69 | | - o_joint_segments[i].Transform(o_transforms[i]) |
| 89 | +# for i in range(len(o_joint_segments)): |
| 90 | +# o_joint_segments[i].Transform(o_transforms[i]) |
0 commit comments