File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,29 @@ mat_3x3 get_cross_matrix(const vec_3d *a)
191
191
return A ;
192
192
}
193
193
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
+
194
217
/** @} */
195
218
196
219
/**
@@ -223,6 +246,10 @@ static void test()
223
246
assert (fabsf (c .x - (-1.f )) < 0.01 );
224
247
assert (fabsf (c .y - (2.f )) < 0.01 );
225
248
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 );
226
253
}
227
254
228
255
/**
You can’t perform that action at this time.
0 commit comments