Skip to content

Commit 78231f7

Browse files
feat: slight refactor of df_poses module
1 parent 13c4759 commit 78231f7

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/gh/diffCheck/diffCheck/df_poses.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ class DFPose:
1515
class DFPosesBeam:
1616
"""
1717
This class contains the poses of a single beam, at different times in the assembly process.
18+
It also contains the number of faces detected for this element, based on which the poses are calculated.
1819
"""
1920
poses_dictionnary: dict
21+
n_faces: int = 3
2022

2123
def add_pose(self, pose: DFPose, step_number: int):
2224
"""
2325
Add a pose to the dictionary of poses.
2426
"""
2527
self.poses_dictionnary[f"pose_{step_number}"] = pose
2628

29+
def set_n_faces(self, n_faces: int):
30+
"""
31+
Set the number of faces detected for this element.
32+
"""
33+
self.n_faces = n_faces
34+
2735
@dataclass
2836
class DFPosesAssembly:
2937
n_step: int = 0
@@ -44,7 +52,7 @@ def __post_init__(self):
4452
def add_step(self, new_poses: list[DFPose]):
4553
for i, pose in enumerate(new_poses):
4654
if f"element_{i}" not in self.poses_per_element_dictionary:
47-
self.poses_per_element_dictionary[f"element_{i}"] = DFPosesBeam({})
55+
self.poses_per_element_dictionary[f"element_{i}"] = DFPosesBeam({}, 4)
4856
self.poses_per_element_dictionary[f"element_{i}"].add_pose(pose, self.n_step + 1)
4957
self.n_step += 1
5058

@@ -72,3 +80,37 @@ def save(self, file_path: str):
7280
"""
7381
with open(file_path, 'w') as f:
7482
json.dump(self.poses_per_element_dictionary, f, default=lambda o: o.__dict__, indent=4)
83+
84+
85+
def compute_dot_product(v1, v2):
86+
"""
87+
Compute the dot product of two vectors.
88+
"""
89+
return (v1.X * v2.X) + (v1.Y * v2.Y) + (v1.Z * v2.Z)
90+
91+
92+
def select_vectors(vectors, previous_xDirection, previous_yDirection):
93+
"""
94+
Select the vectors that are aligned with the xDirection and yDirection.
95+
"""
96+
if previous_xDirection is not None and previous_yDirection is not None:
97+
sorted_vectors_by_alignment = sorted(vectors, key=lambda v: compute_dot_product(v, previous_xDirection), reverse=True)
98+
new_xDirection = sorted_vectors_by_alignment[0]
99+
else:
100+
new_xDirection = vectors[0]
101+
102+
condidates_for_yDirection = []
103+
for v in vectors:
104+
if compute_dot_product(v, new_xDirection) ** 2 < 0.5:
105+
condidates_for_yDirection.append(v)
106+
if previous_xDirection is not None and previous_yDirection is not None:
107+
sorted_vectors_by_perpendicularity = sorted(condidates_for_yDirection, key=lambda v: compute_dot_product(v, previous_yDirection), reverse=True)
108+
new_xDirection = sorted_vectors_by_alignment[0]
109+
new_yDirection = sorted_vectors_by_perpendicularity[0] - compute_dot_product(sorted_vectors_by_perpendicularity[0], new_xDirection) * new_xDirection
110+
new_yDirection.Unitize()
111+
else:
112+
new_xDirection = vectors[0]
113+
sorted_vectors = sorted(vectors[1:], key=lambda v: compute_dot_product(v, new_xDirection)**2)
114+
new_yDirection = sorted_vectors[0] - compute_dot_product(vectors[1], new_xDirection) * new_xDirection
115+
new_yDirection.Unitize()
116+
return new_xDirection, new_yDirection

0 commit comments

Comments
 (0)