Skip to content

Commit 8bcc20d

Browse files
update _angle_of_2_vectors to return degree in 0~360
1 parent 1da71fc commit 8bcc20d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

test/test_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ def test_angle_of_2_vectors_180_degree(self):
247247
])
248248

249249
res = _angle_of_2_vectors( GCR1_cart[0], GCR1_cart[1])
250-
pass
250+
251+
# The angle between the two vectors should be 181 degree
252+
self.assertAlmostEqual(res, np.deg2rad(181.0), places=8)
251253

252254

253255
class TestFaceEdgeConnectivityHelper(TestCase):

uxarray/grid/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# TODO: makes the function return the angle between 0 to 360 degrees
1111
def _angle_of_2_vectors(u, v):
1212
"""Calculate the angle between two 3D vectors u and v in radians. Can be
13-
used to calcualte the span of a GCR.
13+
used to calcualte the span of a GCR, it will return the degree between 0 to 360.
1414
1515
Parameters
1616
----------
@@ -24,11 +24,22 @@ def _angle_of_2_vectors(u, v):
2424
float
2525
The angle between u and v in radians.
2626
"""
27+
# First calculate the direction of the normal, which can determine if the angle is greater than 180 degrees
28+
# The direction of the normal is determined by the cross product of the two vectors
29+
normal = np.cross(u, v)
30+
31+
# Calculate the angle between the two vectors,between 0 to 180 degrees
2732
v_norm_times_u = np.linalg.norm(v) * u
2833
u_norm_times_v = np.linalg.norm(u) * v
2934
vec_minus = v_norm_times_u - u_norm_times_v
3035
vec_sum = v_norm_times_u + u_norm_times_v
3136
angle_u_v_rad = 2 * np.arctan2(np.linalg.norm(vec_minus), np.linalg.norm(vec_sum))
37+
38+
# If the normal is positive, the angle is less than 180 degrees (counter-clockwise)
39+
if np.dot(normal, np.array([0.0, 0.0, 1.0])) >= 0:
40+
return angle_u_v_rad
41+
else:
42+
return 2 * np.pi - angle_u_v_rad
3243
return angle_u_v_rad
3344

3445

0 commit comments

Comments
 (0)