From 068c46513aac5ecfb06489191fad9fd60e5d750c Mon Sep 17 00:00:00 2001 From: Gwendal Grignou Date: Wed, 19 Dec 2018 09:08:12 -0800 Subject: math: Add/expose some vector operation Expose dot procdut and cros product for vector operations. BUG=b:120346412 BRANCH=none TEST=compile, check unit tests. Change-Id: Ief50f31aa6105dc2f0d92caf8b7473a7e141eb45 Signed-off-by: Gwendal Grignou Reviewed-on: https://chromium-review.googlesource.com/1387923 Commit-Ready: ChromeOS CL Exonerator Bot --- common/math_util.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'common/math_util.c') diff --git a/common/math_util.c b/common/math_util.c index 5e45f9efad..1a29c63d49 100644 --- a/common/math_util.c +++ b/common/math_util.c @@ -148,6 +148,28 @@ int vector_magnitude(const intv3_t v) return int_sqrtf(sum); } +/* cross_product only works if the vectors magnitudes are around 1<<16. */ +void cross_product(const intv3_t v1, const intv3_t v2, intv3_t v) +{ + v[X] = (fp_inter_t)v1[Y] * v2[Z] - (fp_inter_t)v1[Z] * v2[Y]; + v[Y] = (fp_inter_t)v1[Z] * v2[X] - (fp_inter_t)v1[X] * v2[Z]; + v[Z] = (fp_inter_t)v1[X] * v2[Y] - (fp_inter_t)v1[Y] * v2[X]; +} + +fp_inter_t dot_product(const intv3_t v1, const intv3_t v2) +{ + return (fp_inter_t)v1[X] * v2[X] + + (fp_inter_t)v1[Y] * v2[Y] + + (fp_inter_t)v1[Z] * v2[Z]; +} + +void vector_scale(intv3_t v, fp_t s) +{ + v[X] = fp_mul(v[X], s); + v[Y] = fp_mul(v[Y], s); + v[Z] = fp_mul(v[Z], s); +} + fp_t cosine_of_angle_diff(const intv3_t v1, const intv3_t v2) { fp_inter_t dotproduct; @@ -157,10 +179,7 @@ fp_t cosine_of_angle_diff(const intv3_t v1, const intv3_t v2) * Angle between two vectors is acos(A dot B / |A|*|B|). To return * cosine of angle between vectors, then don't do acos operation. */ - - dotproduct = (fp_inter_t)v1[0] * v2[0] + - (fp_inter_t)v1[1] * v2[1] + - (fp_inter_t)v1[2] * v2[2]; + dotproduct = dot_product(v1, v2); denominator = (fp_inter_t)vector_magnitude(v1) * vector_magnitude(v2); -- cgit v1.2.1