From 4631dd44685b84634455e1bfb5f4fee8177468ea Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Fri, 27 Mar 2020 17:20:21 +0100 Subject: Add files via upload --- c_lib/cvec3.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/c_lib/cvec3.h b/c_lib/cvec3.h index 1197998f..c70ab894 100644 --- a/c_lib/cvec3.h +++ b/c_lib/cvec3.h @@ -27,8 +27,7 @@ #include #include -#define cvec3_initializer {0, 0, 0} -#define cvec3_data(v) (&(v).x) +#define cvec3_data(v) (&(v).x) #define declare_CVec3(tag, T) \ typedef struct CVec3##tag { T x, y, z; } CVec3##tag; \ @@ -61,23 +60,23 @@ return _cvec3_DOT(v, v); \ } \ static inline CVec3##tag cvec3##tag##_plus(CVec3##tag u, CVec3##tag v) { \ - CVec3##tag w = {u.x + v.x, u.y + v.y, u.z + v.z}; return w; \ + u.x += v.x, u.y += v.y, u.z += v.z; return u; \ } \ static inline CVec3##tag cvec3##tag##_minus(CVec3##tag u, CVec3##tag v) { \ - CVec3##tag w = {u.x - v.x, u.y - v.y, u.z - v.z}; return w; \ + u.x -= v.x, u.y -= v.y, u.z -= v.z; return u; \ } \ static inline CVec3##tag cvec3##tag##_mult(CVec3##tag v, double s) { \ - CVec3##tag w = {(T)(v.x*s), (T)(v.y*s), (T)(v.z*s)}; return w; \ + v.x = (T)(s*v.x), v.y = (T)(s*v.y), v.z = (T)(s*v.z); return v; \ + } \ + static inline CVec3##tag cvec3##tag##_multInverse(CVec3##tag v, double s) { \ + v.x = (T)(s/v.x), v.y = (T)(s/v.y), v.z = (T)(s/v.z); return v; \ } \ static inline CVec3##tag cvec3##tag##_neg(CVec3##tag v) { \ - CVec3##tag w = {-v.x, -v.y, -v.z}; return w; \ + v.x = -v.x, v.y = -v.y, v.z = -v.z; return v; \ } \ static inline CVec3##tag cvec3##tag##_unit(CVec3##tag v) { \ - double s = 1.0 / cvec3##tag##_length(v); \ - CVec3##tag w = {(T)(v.x*s), (T)(v.y*s), (T)(v.z*s)}; return w; \ - } \ - static inline CVec3##tag cvec3##tag##_multInverse(CVec3##tag v, double s) { \ - CVec3##tag w = {(T)(s/v.x), (T)(s/v.y), (T)(s/v.z)}; return w; \ + double s = 1.0 / sqrt(_cvec3_DOT(v, v)); \ + v.x = (T)(v.x*s), v.y = (T)(v.y*s), v.z = (T)(v.z*s); return v; \ } \ static inline double cvec3##tag##_dot(CVec3##tag u, CVec3##tag v) { \ return _cvec3_DOT(u, v); \ -- cgit v1.2.3