[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).
This commit is contained in:
Bill Currie 2022-01-02 01:15:17 +09:00
parent 937f36384b
commit 5fb28d7c38
4 changed files with 37 additions and 47 deletions

View file

@ -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]); \

View file

@ -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

View file

@ -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]))

View file

@ -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++)