From: jpreiss Date: Fri, 21 Feb 2020 23:11:45 +0000 (-0800) Subject: Vector indexing function. X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;h=94dfe78cd4c8c3d1f3edb3415ddf02f908b655ad;p=forks%2Fcmath3d.git Vector indexing function. Could use a union to give direct array access. May implement later. --- diff --git a/math3d.h b/math3d.h index 465f7f5..0671d7b 100644 --- a/math3d.h +++ b/math3d.h @@ -235,7 +235,7 @@ static inline struct vec vsub2(struct vec a, struct vec b, struct vec c) { } // -// conversion to/from raw float and double arrays. +// conversion to/from raw float and double arrays; array-like access. // // load a vector from a double array. @@ -254,6 +254,10 @@ static inline struct vec vloadf(float const *f) { static inline void vstoref(struct vec v, float *f) { f[0] = v.x; f[1] = v.y; f[2] = v.z; } +// index a vector like a 3-element array. +static inline float vindex(struct vec v, int i) { + return ((float const *)&v.x)[i]; +} // ---------------------------- 3x3 matrices ------------------------------ diff --git a/test.c b/test.c index 1565737..626587a 100644 --- a/test.c +++ b/test.c @@ -59,6 +59,16 @@ do { \ // tests - when adding new tests, make sure to add to test_fns array // +void test_vec_basic() +{ + struct vec v = mkvec(1.0f, 2.0f, 3.0f); + assert(vindex(v, 0) == 1.0f); + assert(vindex(v, 1) == 2.0f); + assert(vindex(v, 2) == 3.0f); + + printf("%s passed\n", __func__); +} + void test_mat_axisangle() { srand(0); // deterministic @@ -147,6 +157,7 @@ void test_qvectovec() // micro test framework typedef void (*voidvoid_fn)(void); voidvoid_fn test_fns[] = { + test_vec_basic, test_mat_axisangle, test_quat_rpy_conversions, test_quat_mat_conversions,