77import Rhino
88
99from ghpythonlib .componentbase import executingcomponent as component
10- # from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
1110
1211import System
1312
1413def compute_dot_product (v1 , v2 ):
1514 """
1615 Compute the dot product of two vectors.
1716 """
18- return v1 .X * v2 .X + v1 .Y * v2 .Y + v1 .Z * v2 .Z
17+ return ( v1 .X * v2 .X ) + ( v1 .Y * v2 .Y ) + ( v1 .Z * v2 .Z )
1918
2019class DFMainPCAxes (component ):
2120 def RunScript (self ,
2221 i_clouds : System .Collections .Generic .List [Rhino .Geometry .PointCloud ],
23- i_file_name : str ,
24- reset : bool ) -> System .Collections .Generic .IList [Rhino .Geometry .Vector3d ]:
22+ i_reset : bool ):
2523
2624 planes = []
2725 all_poses_in_time = df_poses .DFPosesAssembly ()
28- if reset :
26+ if i_reset :
2927 all_poses_in_time .reset ()
3028 return None , None
3129
@@ -37,7 +35,6 @@ def RunScript(self,
3735 return None , None
3836 df_cloud .estimate_normals (True , 12 )
3937
40- # hint = df_cvt_bindings.cvt_dfcloud_2_rhcloud(df_cloud)
4138 df_points = df_cloud .get_axis_aligned_bounding_box ()
4239 df_point = (df_points [0 ] + df_points [1 ]) / 2
4340 rh_point = Rhino .Geometry .Point3d (df_point [0 ], df_point [1 ], df_point [2 ])
@@ -47,25 +44,17 @@ def RunScript(self,
4744 if previous_pose :
4845 rh_previous_xDirection = Rhino .Geometry .Vector3d (previous_pose .xDirection [0 ], previous_pose .xDirection [1 ], previous_pose .xDirection [2 ])
4946 rh_previous_yDirection = Rhino .Geometry .Vector3d (previous_pose .yDirection [0 ], previous_pose .yDirection [1 ], previous_pose .yDirection [2 ])
50- n_faces = len (diffcheck_bindings .dfb_segmentation .DFSegmentation .segment_by_normal (df_cloud , 12 , int (len (df_cloud .points )/ 20 ), True , int (len (df_cloud .points )/ 200 ), 1 ))
47+ n_faces = all_poses_in_time .poses_per_element_dictionary [f"element_{ i } " ].n_faces
48+ else :
49+ rh_previous_xDirection = None
50+ rh_previous_yDirection = None
51+ n_faces = len (diffcheck_bindings .dfb_segmentation .DFSegmentation .segment_by_normal (df_cloud , 12 , int (len (df_cloud .points )/ 20 ), True , int (len (df_cloud .points )/ 200 ), 1 ))
52+
5153 axes = df_cloud .get_principal_axes (n_faces )
5254 for axe in axes :
5355 vectors .append (Rhino .Geometry .Vector3d (axe [0 ], axe [1 ], axe [2 ]))
54- if previous_pose :
55- # Sort the vectors by their alignment with the previous xDirection and yDirection
56- sorted_vectors_by_alignment = sorted (vectors , key = lambda v : compute_dot_product (v , rh_previous_xDirection ), reverse = True )
57- sorted_vectors_by_perpendicularity = sorted (vectors , key = lambda v : compute_dot_product (v , rh_previous_yDirection ), reverse = True )
58- new_xDirection = sorted_vectors_by_alignment [0 ]
59- new_yDirection = sorted_vectors_by_perpendicularity [0 ] #- compute_dot_product(sorted_vectors_by_perpendicularity[0], new_xDirection) * new_xDirection
60- new_yDirection .Unitize ()
61- else :
62- # If no previous pose, just use the first two vectors as x and y directions
63- new_xDirection = vectors [0 ]
64- new_yDirection = vectors [1 ] #- compute_dot_product(vectors[1], new_xDirection) * new_xDirection
65- new_yDirection .Unitize ()
6656
67- print (new_xDirection )
68- print (new_yDirection )
57+ new_xDirection , new_yDirection = df_poses .select_vectors (vectors , rh_previous_xDirection , rh_previous_yDirection )
6958
7059 pose = df_poses .DFPose (
7160 origin = [rh_point .X , rh_point .Y , rh_point .Z ],
@@ -77,13 +66,4 @@ def RunScript(self,
7766
7867 all_poses_in_time .add_step (all_poses_this_time )
7968
80- return planes , all_poses_in_time .poses_per_element_dictionary
81-
82-
83- # if __name__ == "__main__":
84- # i_file_name = "C:/Users/localuser/test_file_poses.json"
85- # component = DFMainPCAxes()
86- # if reset == None: # noqa: E711
87- # reset = False
88-
89- # a, dico = component.RunScript(x, i_file_name, reset) # noqa: F821
69+ return [planes , all_poses_in_time ]
0 commit comments