From 5fb28d7c38106a45894659b84cf6c34717066e17 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 2 Jan 2022 01:15:17 +0900 Subject: [PATCH] [math] Clean up vector component operations And add a unary op macro. Having VectorCompOp makes it easy to write macros that work for multiple data widths, which is why it and its users now use (dst, ...) instead of (..., dst) as in the past. I'll sort out the other macros later now that I know the compiler handily gives messages about the switched order (uninitialized vars etc). --- include/QF/math/vector.h | 50 +++++++++++++++----------------------- libs/models/trace.c | 22 ++++++++--------- libs/util/test/test-mat3.c | 6 ++--- libs/util/test/test-mat4.c | 6 ++--- 4 files changed, 37 insertions(+), 47 deletions(-) diff --git a/include/QF/math/vector.h b/include/QF/math/vector.h index 100a98bd0..f45893f50 100644 --- a/include/QF/math/vector.h +++ b/include/QF/math/vector.h @@ -37,25 +37,27 @@ extern const vec_t *const vec3_origin; +#define VectorCompUop(b, op, a) \ + do { \ + (b)[0] = op ((a)[0]); \ + (b)[1] = op ((a)[1]); \ + (b)[2] = op ((a)[2]); \ + } while (0) +#define VectorCompOp(c, a, op, b) \ + do { \ + (c)[0] = (a)[0] op (b)[0]; \ + (c)[1] = (a)[1] op (b)[1]; \ + (c)[2] = (a)[2] op (b)[2]; \ + } while (0) +#define VectorCompAdd(c,a,b) VectorCompOp (c, a, +, b) +#define VectorCompSub(c,a,b) VectorCompOp (c, a, -, b) +#define VectorCompMult(c,a,b) VectorCompOp (c, a, *, b) +#define VectorCompDiv(c,a,b) VectorCompOp (c, a, /, b) + #define DotProduct(a,b) ((a)[0] * (b)[0] + (a)[1] * (b)[1] + (a)[2] * (b)[2]) -#define VectorSubtract(a,b,c) \ - do { \ - (c)[0] = (a)[0] - (b)[0]; \ - (c)[1] = (a)[1] - (b)[1]; \ - (c)[2] = (a)[2] - (b)[2]; \ - } while (0) -#define VectorNegate(a,b) \ - do { \ - (b)[0] = -(a)[0]; \ - (b)[1] = -(a)[1]; \ - (b)[2] = -(a)[2]; \ - } while (0) -#define VectorAdd(a,b,c) \ - do { \ - (c)[0] = (a)[0] + (b)[0]; \ - (c)[1] = (a)[1] + (b)[1]; \ - (c)[2] = (a)[2] + (b)[2]; \ - } while (0) +#define VectorSubtract(a,b,c) VectorCompSub (c, a, b) +#define VectorNegate(a,b) VectorCompUop (b, -, a) +#define VectorAdd(a,b,c) VectorCompAdd (c, a, b) #define VectorCopy(a,b) \ do { \ (b)[0] = (a)[0]; \ @@ -119,18 +121,6 @@ extern const vec_t *const vec3_origin; (c)[1] = (b)[1] - (b)[0] * (a)[0]; \ (c)[0] = (b)[0]; \ } while (0) -#define VectorCompMult(a,b,c) \ - do { \ - (c)[0] = (a)[0] * (b)[0]; \ - (c)[1] = (a)[1] * (b)[1]; \ - (c)[2] = (a)[2] * (b)[2]; \ - } while (0) -#define VectorCompDiv(a,b,c) \ - do { \ - (c)[0] = (a)[0] / (b)[0]; \ - (c)[1] = (a)[1] / (b)[1]; \ - (c)[2] = (a)[2] / (b)[2]; \ - } while (0) #define VectorCompCompare(c, m, a, op, b) \ do { \ (c)[0] = m((a)[0] op (b)[0]); \ diff --git a/libs/models/trace.c b/libs/models/trace.c index 1fcecb47b..d65a06c9a 100644 --- a/libs/models/trace.c +++ b/libs/models/trace.c @@ -120,7 +120,7 @@ init_box (const trace_t *trace, clipbox_t *box, const vec3_t vel) //FIXME rotated box for (i = 0; i < 3; i++) u[i] = (vel[i] >= 0 ? 1 : -1); - VectorCompMult (u, trace->extents, p); + VectorCompMult (p, u, trace->extents); for (i = 0; i < 3; i++) { box->portals[i].planenum = i; box->portals[i].next[0] = 0; @@ -153,17 +153,17 @@ init_box (const trace_t *trace, clipbox_t *box, const vec3_t vel) box->edges[i].points[j][a] = s[k] * u[i] * box->edges[i].points[j - 1][b]; } - VectorCompMult (box->points[i].points[j - 1], trace->extents, - box->points[i].points[j - 1]); - VectorCompMult (box->edges[i].points[j - 1], trace->extents, - box->edges[i].points[j - 1]); + VectorCompMult (box->points[i].points[j - 1], + box->points[i].points[j - 1], trace->extents); + VectorCompMult (box->edges[i].points[j - 1], + box->edges[i].points[j - 1], trace->extents); VectorScale (box->edges[i].points[j - 1], 2, box->edges[i].points[j - 1]); } - VectorCompMult (box->points[i].points[3], trace->extents, - box->points[i].points[3]); - VectorCompMult (box->edges[i].points[3], trace->extents, - box->edges[i].points[3]); + VectorCompMult (box->points[i].points[3], + box->points[i].points[3], trace->extents); + VectorCompMult (box->edges[i].points[3], + box->edges[i].points[3], trace->extents); VectorScale (box->edges[i].points[3], 2, box->edges[i].points[3]); } @@ -566,8 +566,8 @@ portal_intersect (trace_t *trace, clipport_t *portal, plane_t *plane, vec3_t p1, p2, imp, dist; vec_t t1, t2, frac; - VectorCompMult (trace->extents, verts[i][0], p1); - VectorCompMult (trace->extents, verts[i][1], p2); + VectorCompMult (p1, trace->extents, verts[i][0]); + VectorCompMult (p2, trace->extents, verts[i][1]); t1 = PlaneDiff (p1, plane) + o_n; t2 = PlaneDiff (p2, plane) + o_n; // if both ends of the box edge are on the same side (or touching) the diff --git a/libs/util/test/test-mat3.c b/libs/util/test/test-mat3.c index 903c4de9f..9134fb9c2 100644 --- a/libs/util/test/test-mat3.c +++ b/libs/util/test/test-mat3.c @@ -99,7 +99,7 @@ test_transform (const vec3_t angles, const vec3_t scale) VectorCopy (v, x); AngleQuat (angles, rotation); - VectorCompMult (scale, x, x); + VectorCompMult (x, scale, x); QuatMultVec (rotation, x, x); Mat3Init (rotation, scale, mat); @@ -132,7 +132,7 @@ test_transform2 (const vec3_t angles, const vec3_t scale) VectorCopy (v, x); AngleQuat (angles, rotation); - VectorCompMult (scale, x, x); + VectorCompMult (x, scale, x); QuatMultVec (rotation, x, x); Mat3Init (rotation, scale, mat); @@ -141,7 +141,7 @@ test_transform2 (const vec3_t angles, const vec3_t scale) VectorCopy (v, y); QuatMultVec (rot, y, y); VectorShear (sh, y, y); - VectorCompMult (sc, y, y);//scale + VectorCompMult (y, sc, y);//scale for (i = 0; i < 3; i++) if (!compare (x[i], y[i])) diff --git a/libs/util/test/test-mat4.c b/libs/util/test/test-mat4.c index 1bd7afcc9..cd30c0990 100644 --- a/libs/util/test/test-mat4.c +++ b/libs/util/test/test-mat4.c @@ -114,7 +114,7 @@ test_transform (const vec3_t angles, const vec3_t scale, VectorCopy (v, x); AngleQuat (angles, rotation); - VectorCompMult (scale, x, x); + VectorCompMult (x, scale, x); QuatMultVec (rotation, x, x); VectorAdd (x, translation, x); @@ -148,7 +148,7 @@ test_transform2 (const vec3_t angles, const vec3_t scale, VectorCopy (v, x); AngleQuat (angles, rotation); - VectorCompMult (scale, x, x); + VectorCompMult (x, scale, x); QuatMultVec (rotation, x, x); VectorAdd (translation, x, x); @@ -158,7 +158,7 @@ test_transform2 (const vec3_t angles, const vec3_t scale, VectorCopy (v, y); QuatMultVec (rot, y, y); VectorShear (sh, y, y); - VectorCompMult (sc, y, y);//scale + VectorCompMult (y, sc, y);//scale VectorAdd (tr, y, y); for (i = 0; i < 3; i++)