Skip to content

Commit 31ced82

Browse files
lasgelPanquesito7
andauthored
tests: added get_angle test function in geometry/vector3d (#838)
* feat: add get_angle algorithm * docs: added documentation for get_angle algorithm * test: add test for get_angle algorithm * fix: Fixed indentation * Test: Changed //printf to // printf * fix: Changed variable description for norm_a and norm_b * fix: changed // to /// in the comment as suggested * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: David Leal <[email protected]>
1 parent 08157c4 commit 31ced82

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

geometry/vectors_3d.c

+27
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,29 @@ mat_3x3 get_cross_matrix(const vec_3d *a)
191191
return A;
192192
}
193193

194+
/**
195+
* Obtain the angle between two given vectors.
196+
* @f[\alpha=acos\left(\frac{\vec{a} \cdot \vec{b}}{\lVert\vec{a}\rVert \cdot \lVert\vec{b}\rVert}\right)@f]
197+
* @param[in] a first input vector
198+
* @param[in] b second input vector
199+
* @returns angle between @f$\vec{a}@f$ and @f$\vec{b}@f$ in radians
200+
*/
201+
202+
double get_angle(const vec_3d *a, const vec_3d *b)
203+
{
204+
double alpha, cos_alpha;
205+
float norm_a = vector_norm(a); ///< The norm of vector a
206+
float norm_b = vector_norm(b); ///< The norm of vector b
207+
if (fabsf(norm_a) < EPSILON || fabsf(norm_b) < EPSILON) /// detect possible division by 0 - the angle is not defined in this case
208+
{
209+
return NAN;
210+
}
211+
212+
cos_alpha = dot_prod(a, b) / (norm_a * norm_b);
213+
alpha = acos(cos_alpha); // delivers the radian
214+
return alpha; // in range from -1 to 1
215+
}
216+
194217
/** @} */
195218

196219
/**
@@ -223,6 +246,10 @@ static void test()
223246
assert(fabsf(c.x - (-1.f)) < 0.01);
224247
assert(fabsf(c.y - (2.f)) < 0.01);
225248
assert(fabsf(c.z - (-1.f)) < 0.01);
249+
250+
double alpha = get_angle(&a, &b);
251+
// printf("The angle is %f\n", alpha);
252+
assert(fabsf(alpha - 0.387597) < 0.01);
226253
}
227254

228255
/**

0 commit comments

Comments
 (0)