From 31ced8233e2ae55cff5149e760f92a3fca1d09cf Mon Sep 17 00:00:00 2001 From: lasgel Date: Fri, 9 Jul 2021 20:52:29 +0200 Subject: [PATCH] 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 --- geometry/vectors_3d.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/geometry/vectors_3d.c b/geometry/vectors_3d.c index c1ff0242..90ea1f00 100644 --- a/geometry/vectors_3d.c +++ b/geometry/vectors_3d.c @@ -191,6 +191,29 @@ mat_3x3 get_cross_matrix(const vec_3d *a) return A; } +/** + * Obtain the angle between two given vectors. + * @f[\alpha=acos\left(\frac{\vec{a} \cdot \vec{b}}{\lVert\vec{a}\rVert \cdot \lVert\vec{b}\rVert}\right)@f] + * @param[in] a first input vector + * @param[in] b second input vector + * @returns angle between @f$\vec{a}@f$ and @f$\vec{b}@f$ in radians + */ + +double get_angle(const vec_3d *a, const vec_3d *b) +{ + double alpha, cos_alpha; + float norm_a = vector_norm(a); ///< The norm of vector a + float norm_b = vector_norm(b); ///< The norm of vector b + if (fabsf(norm_a) < EPSILON || fabsf(norm_b) < EPSILON) /// detect possible division by 0 - the angle is not defined in this case + { + return NAN; + } + + cos_alpha = dot_prod(a, b) / (norm_a * norm_b); + alpha = acos(cos_alpha); // delivers the radian + return alpha; // in range from -1 to 1 +} + /** @} */ /** @@ -223,6 +246,10 @@ static void test() assert(fabsf(c.x - (-1.f)) < 0.01); assert(fabsf(c.y - (2.f)) < 0.01); assert(fabsf(c.z - (-1.f)) < 0.01); + + double alpha = get_angle(&a, &b); + // printf("The angle is %f\n", alpha); + assert(fabsf(alpha - 0.387597) < 0.01); } /**