adaptive aligned std::array based vector types (#686)

This commit is contained in:
Jon Daniel
2025-05-08 12:36:35 -07:00
committed by GitHub
parent 19762b9282
commit adda1c1f15
131 changed files with 3396 additions and 3592 deletions

View File

@@ -21,62 +21,35 @@
void vm_AverageVector(vector *a, int num) {
// Averages a vector. ie divides each component of vector a by num
// assert (num!=0);
a->x = a->x / (float)num;
a->y = a->y / (float)num;
a->z = a->z / (float)num;
*a /= (scalar)num;
}
void vm_AddVectors(vector *result, vector *a, vector *b) {
void vm_AddVectors(vector *result, const vector *a, const vector *b) {
// Adds two vectors. Either source can equal dest
result->x = a->x + b->x;
result->y = a->y + b->y;
result->z = a->z + b->z;
*result = *a + *b;
}
void vm_SubVectors(vector *result, const vector *a, const vector *b) {
// Subtracts second vector from first. Either source can equal dest
result->x = a->x - b->x;
result->y = a->y - b->y;
result->z = a->z - b->z;
*result = *a - *b;
}
float vm_VectorDistance(const vector *a, const vector *b) {
scalar vm_VectorDistance(const vector *a, const vector *b) {
// Given two vectors, returns the distance between them
vector dest;
float dist;
vm_SubVectors(&dest, a, b);
dist = vm_GetMagnitude(&dest);
return dist;
return (*a - *b).mag();
}
float vm_VectorDistanceQuick(vector *a, vector *b) {
scalar vm_VectorDistanceQuick(const vector *a, const vector *b) {
// Given two vectors, returns the distance between them
vector dest;
float dist;
vm_SubVectors(&dest, a, b);
dist = vm_GetMagnitudeFast(&dest);
return dist;
return (*a - *b).mag();
}
// Calculates the perpendicular vector given three points
// Parms: n - the computed perp vector (filled in)
// v0,v1,v2 - three clockwise vertices
void vm_GetPerp(vector *n, vector *a, vector *b, vector *c) {
void vm_GetPerp(vector *n, const vector *a, const vector *b, const vector *c) {
// Given 3 vertices, return the surface normal in n
// IMPORTANT: B must be the 'corner' vertex
vector x, y;
vm_SubVectors(&x, b, a);
vm_SubVectors(&y, c, b);
vm_CrossProduct(n, &x, &y);
*n = vector::cross3(*b - *a, *c - *b);
}
// Calculates the (normalized) surface normal give three points
@@ -84,123 +57,100 @@ void vm_GetPerp(vector *n, vector *a, vector *b, vector *c) {
// v0,v1,v2 - three clockwise vertices
// Returns the magnitude of the normal before it was normalized.
// The bigger this value, the better the normal.
float vm_GetNormal(vector *n, vector *v0, vector *v1, vector *v2) {
scalar vm_GetNormal(vector *n, const vector *v0, const vector *v1, const vector *v2) {
vm_GetPerp(n, v0, v1, v2);
return vm_VectorNormalize(n);
}
// Does a simple dot product calculation
float vm_DotProduct(vector *u, vector *v) { return (u->x * v->x) + (u->y * v->y) + (u->z * v->z); }
scalar vm_DotProduct(const vector *u, const vector *v) { return vector::dot(*u, *v); }
// Scales all components of vector v by value s and stores result in vector d
// dest can equal source
void vm_ScaleVector(vector *d, vector *v, float s) {
d->x = (v->x * s);
d->y = (v->y * s);
d->z = (v->z * s);
void vm_ScaleVector(vector *d, const vector *v, const scalar s) {
*d = *v * s;
}
void vm_ScaleAddVector(vector *d, vector *p, vector *v, float s) {
void vm_ScaleAddVector(vector *d, const vector *p, const vector *v, const scalar s) {
// Scales all components of vector v by value s
// adds the result to p and stores result in vector d
// dest can equal source
d->x = p->x + (v->x * s);
d->y = p->y + (v->y * s);
d->z = p->z + (v->z * s);
*d = *p + *v * s;
}
void vm_DivVector(vector *dest, vector *src, float n) {
void vm_DivVector(vector *dest, vector *src, const scalar n) {
// Divides a vector into n portions
// Dest can equal src
// assert (n!=0);
dest->x = src->x / n;
dest->y = src->y / n;
dest->z = src->z / n;
*dest = *src / n;
}
void vm_CrossProduct(vector *dest, vector *u, vector *v) {
void vm_CrossProduct(vector *dest, const vector *u, const vector *v) {
// Computes a cross product between u and v, returns the result
// in Normal. Dest cannot equal source.
dest->x = (u->y * v->z) - (u->z * v->y);
dest->y = (u->z * v->x) - (u->x * v->z);
dest->z = (u->x * v->y) - (u->y * v->x);
*dest = vector::cross3(*u, *v);
}
// Normalize a vector.
// Returns: the magnitude before normalization
float vm_VectorNormalize(vector *a) {
float mag;
mag = vm_GetMagnitude(a);
scalar vm_VectorNormalize(vector *a) {
scalar mag = a->mag();
if (mag > 0)
*a /= mag;
else {
*a = Zero_vector;
a->x = 1.0;
*a = vector::id(0);
mag = 0.0f;
}
return mag;
}
float vm_GetMagnitude(vector *a) {
float f;
f = (a->x * a->x) + (a->y * a->y) + (a->z * a->z);
return (sqrt(f));
scalar vm_GetMagnitude(const vector *a) {
return a->mag();
}
void vm_ClearMatrix(matrix *dest) { memset(dest, 0, sizeof(matrix)); }
void vm_MakeIdentity(matrix *dest) {
memset(dest, 0, sizeof(matrix));
dest->rvec.x = dest->uvec.y = dest->fvec.z = 1.0;
*dest = { vector::id(0), vector::id(1), vector::id(2) };
}
void vm_MakeInverseMatrix(matrix *dest) {
memset((void *)dest, 0, sizeof(matrix));
dest->rvec.x = dest->uvec.y = dest->fvec.z = -1.0;
*dest = { -vector::id(0), -vector::id(1), -vector::id(2) };
}
void vm_TransposeMatrix(matrix *m) {
// Transposes a matrix in place
float t;
scalar t;
t = m->uvec.x;
m->uvec.x = m->rvec.y;
m->rvec.y = t;
t = m->fvec.x;
m->fvec.x = m->rvec.z;
m->rvec.z = t;
t = m->fvec.y;
m->fvec.y = m->uvec.z;
m->uvec.z = t;
t = m->uvec.x();
m->uvec.x() = m->rvec.y();
m->rvec.y() = t;
t = m->fvec.x();
m->fvec.x() = m->rvec.z();
m->rvec.z() = t;
t = m->fvec.y();
m->fvec.y() = m->uvec.z();
m->uvec.z() = t;
}
void vm_MatrixMulVector(vector *result, vector *v, matrix *m) {
void vm_MatrixMulVector(vector *result, const vector *v, const matrix *m) {
// Rotates a vector thru a matrix
// assert(result != v);
result->x = *v * m->rvec;
result->y = *v * m->uvec;
result->z = *v * m->fvec;
*result = vector{ vector::dot(*v, m->rvec), vector::dot(*v, m->uvec), vector::dot(*v, m->fvec) };
}
// Multiply a vector times the transpose of a matrix
void vm_VectorMulTMatrix(vector *result, vector *v, matrix *m) {
// assert(result != v);
result->x = vm_Dot3Vector(m->rvec.x, m->uvec.x, m->fvec.x, v);
result->y = vm_Dot3Vector(m->rvec.y, m->uvec.y, m->fvec.y, v);
result->z = vm_Dot3Vector(m->rvec.z, m->uvec.z, m->fvec.z, v);
*result = { vm_Dot3Vector(m->rvec.x(), m->uvec.x(), m->fvec.x(), v),
vm_Dot3Vector(m->rvec.y(), m->uvec.y(), m->fvec.y(), v),
vm_Dot3Vector(m->rvec.z(), m->uvec.z(), m->fvec.z(), v) };
}
void vm_MatrixMul(matrix *dest, matrix *src0, matrix *src1) {
@@ -208,17 +158,17 @@ void vm_MatrixMul(matrix *dest, matrix *src0, matrix *src1) {
// assert((dest != src0) && (dest != src1));
dest->rvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->rvec);
dest->uvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->uvec);
dest->fvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->fvec);
dest->rvec.x() = vm_Dot3Vector(src0->rvec.x(), src0->uvec.x(), src0->fvec.x(), &src1->rvec);
dest->uvec.x() = vm_Dot3Vector(src0->rvec.x(), src0->uvec.x(), src0->fvec.x(), &src1->uvec);
dest->fvec.x() = vm_Dot3Vector(src0->rvec.x(), src0->uvec.x(), src0->fvec.x(), &src1->fvec);
dest->rvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->rvec);
dest->uvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->uvec);
dest->fvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->fvec);
dest->rvec.y() = vm_Dot3Vector(src0->rvec.y(), src0->uvec.y(), src0->fvec.y(), &src1->rvec);
dest->uvec.y() = vm_Dot3Vector(src0->rvec.y(), src0->uvec.y(), src0->fvec.y(), &src1->uvec);
dest->fvec.y() = vm_Dot3Vector(src0->rvec.y(), src0->uvec.y(), src0->fvec.y(), &src1->fvec);
dest->rvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->rvec);
dest->uvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->uvec);
dest->fvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->fvec);
dest->rvec.z() = vm_Dot3Vector(src0->rvec.z(), src0->uvec.z(), src0->fvec.z(), &src1->rvec);
dest->uvec.z() = vm_Dot3Vector(src0->rvec.z(), src0->uvec.z(), src0->fvec.z(), &src1->uvec);
dest->fvec.z() = vm_Dot3Vector(src0->rvec.z(), src0->uvec.z(), src0->fvec.z(), &src1->fvec);
}
// Multiply a matrix times the transpose of a matrix
@@ -227,34 +177,34 @@ void vm_MatrixMulTMatrix(matrix *dest, matrix *src0, matrix *src1) {
// assert((dest != src0) && (dest != src1));
dest->rvec.x = src0->rvec.x * src1->rvec.x + src0->uvec.x * src1->uvec.x + src0->fvec.x * src1->fvec.x;
dest->uvec.x = src0->rvec.x * src1->rvec.y + src0->uvec.x * src1->uvec.y + src0->fvec.x * src1->fvec.y;
dest->fvec.x = src0->rvec.x * src1->rvec.z + src0->uvec.x * src1->uvec.z + src0->fvec.x * src1->fvec.z;
dest->rvec.x() = src0->rvec.x() * src1->rvec.x() + src0->uvec.x() * src1->uvec.x() + src0->fvec.x() * src1->fvec.x();
dest->uvec.x() = src0->rvec.x() * src1->rvec.y() + src0->uvec.x() * src1->uvec.y() + src0->fvec.x() * src1->fvec.y();
dest->fvec.x() = src0->rvec.x() * src1->rvec.z() + src0->uvec.x() * src1->uvec.z() + src0->fvec.x() * src1->fvec.z();
dest->rvec.y = src0->rvec.y * src1->rvec.x + src0->uvec.y * src1->uvec.x + src0->fvec.y * src1->fvec.x;
dest->uvec.y = src0->rvec.y * src1->rvec.y + src0->uvec.y * src1->uvec.y + src0->fvec.y * src1->fvec.y;
dest->fvec.y = src0->rvec.y * src1->rvec.z + src0->uvec.y * src1->uvec.z + src0->fvec.y * src1->fvec.z;
dest->rvec.y() = src0->rvec.y() * src1->rvec.x() + src0->uvec.y() * src1->uvec.x() + src0->fvec.y() * src1->fvec.x();
dest->uvec.y() = src0->rvec.y() * src1->rvec.y() + src0->uvec.y() * src1->uvec.y() + src0->fvec.y() * src1->fvec.y();
dest->fvec.y() = src0->rvec.y() * src1->rvec.z() + src0->uvec.y() * src1->uvec.z() + src0->fvec.y() * src1->fvec.z();
dest->rvec.z = src0->rvec.z * src1->rvec.x + src0->uvec.z * src1->uvec.x + src0->fvec.z * src1->fvec.x;
dest->uvec.z = src0->rvec.z * src1->rvec.y + src0->uvec.z * src1->uvec.y + src0->fvec.z * src1->fvec.y;
dest->fvec.z = src0->rvec.z * src1->rvec.z + src0->uvec.z * src1->uvec.z + src0->fvec.z * src1->fvec.z;
dest->rvec.z() = src0->rvec.z() * src1->rvec.x() + src0->uvec.z() * src1->uvec.x() + src0->fvec.z() * src1->fvec.x();
dest->uvec.z() = src0->rvec.z() * src1->rvec.y() + src0->uvec.z() * src1->uvec.y() + src0->fvec.z() * src1->fvec.y();
dest->fvec.z() = src0->rvec.z() * src1->rvec.z() + src0->uvec.z() * src1->uvec.z() + src0->fvec.z() * src1->fvec.z();
}
matrix operator*(matrix src0, matrix src1) {
// For multiplying two 3x3 matrices together
matrix dest;
dest.rvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.rvec);
dest.uvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.uvec);
dest.fvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.fvec);
dest.rvec.x() = vm_Dot3Vector(src0.rvec.x(), src0.uvec.x(), src0.fvec.x(), &src1.rvec);
dest.uvec.x() = vm_Dot3Vector(src0.rvec.x(), src0.uvec.x(), src0.fvec.x(), &src1.uvec);
dest.fvec.x() = vm_Dot3Vector(src0.rvec.x(), src0.uvec.x(), src0.fvec.x(), &src1.fvec);
dest.rvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.rvec);
dest.uvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.uvec);
dest.fvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.fvec);
dest.rvec.y() = vm_Dot3Vector(src0.rvec.y(), src0.uvec.y(), src0.fvec.y(), &src1.rvec);
dest.uvec.y() = vm_Dot3Vector(src0.rvec.y(), src0.uvec.y(), src0.fvec.y(), &src1.uvec);
dest.fvec.y() = vm_Dot3Vector(src0.rvec.y(), src0.uvec.y(), src0.fvec.y(), &src1.fvec);
dest.rvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.rvec);
dest.uvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.uvec);
dest.fvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.fvec);
dest.rvec.z() = vm_Dot3Vector(src0.rvec.z(), src0.uvec.z(), src0.fvec.z(), &src1.rvec);
dest.uvec.z() = vm_Dot3Vector(src0.rvec.z(), src0.uvec.z(), src0.fvec.z(), &src1.uvec);
dest.fvec.z() = vm_Dot3Vector(src0.rvec.z(), src0.uvec.z(), src0.fvec.z(), &src1.fvec);
return dest;
}
@@ -265,7 +215,7 @@ matrix operator*=(matrix &src0, matrix src1) { return (src0 = src0 * src1); }
// Parameters: dest - filled in with the normalized direction vector
// start,end - the start and end points used to calculate the vector
// Returns: the distance between the two input points
float vm_GetNormalizedDir(vector *dest, vector *end, vector *start) {
scalar vm_GetNormalizedDir(vector *dest, const vector *end, const vector *start) {
vm_SubVectors(dest, end, start);
return vm_VectorNormalize(dest);
}
@@ -275,31 +225,31 @@ float vm_GetNormalizedDir(vector *dest, vector *end, vector *start) {
// Parameters: dest - filled in with the normalized direction vector
// start,end - the start and end points used to calculate the vector
// Returns: the distance between the two input points
float vm_GetNormalizedDirFast(vector *dest, vector *end, vector *start) {
scalar vm_GetNormalizedDirFast(vector *dest, const vector *end, const vector *start) {
vm_SubVectors(dest, end, start);
return vm_VectorNormalizeFast(dest);
}
float vm_GetMagnitudeFast(vector *v) {
float a, b, c, bc;
scalar vm_GetMagnitudeFast(const vector *v) {
scalar a, b, c, bc;
a = fabs(v->x);
b = fabs(v->y);
c = fabs(v->z);
a = fabs(v->x());
b = fabs(v->y());
c = fabs(v->z());
if (a < b) {
float t = a;
scalar t = a;
a = b;
b = t;
}
if (b < c) {
float t = b;
scalar t = b;
b = c;
c = t;
if (a < b) {
float t = a;
scalar t = a;
a = b;
b = t;
}
@@ -312,19 +262,16 @@ float vm_GetMagnitudeFast(vector *v) {
// Normalize a vector using an approximation of the magnitude
// Returns: the magnitude before normalization
float vm_VectorNormalizeFast(vector *a) {
float mag;
scalar vm_VectorNormalizeFast(vector *a) {
scalar mag;
mag = vm_GetMagnitudeFast(a);
if (mag == 0.0) {
a->x = a->y = a->z = 0.0;
*a = Zero_vector;
return 0;
}
a->x = (a->x / mag);
a->y = (a->y / mag);
a->z = (a->z / mag);
*a /= mag;
return mag;
}
@@ -334,17 +281,13 @@ float vm_VectorNormalizeFast(vector *a) {
// Parms: norm - the (normalized) surface normal of the plane
// planep - a point on the plane
// Returns: The signed distance from the plane; negative dist is on the back of the plane
float vm_DistToPlane(vector *checkp, vector *norm, vector *planep) {
vector t;
t = *checkp - *planep;
return t * *norm;
scalar vm_DistToPlane(const vector *checkp, const vector *norm, const vector *planep) {
return vector::dot(*checkp - *planep, *norm);
}
float vm_GetSlope(float x1, float y1, float x2, float y2) {
scalar vm_GetSlope(scalar x1, scalar y1, scalar x2, scalar y2) {
// returns the slope of a line
float r;
scalar r;
if (y2 - y1 == 0)
return (0.0);
@@ -353,30 +296,30 @@ float vm_GetSlope(float x1, float y1, float x2, float y2) {
return (r);
}
void vm_SinCosToMatrix(matrix *m, float sinp, float cosp, float sinb, float cosb, float sinh, float cosh) {
float sbsh, cbch, cbsh, sbch;
void vm_SinCosToMatrix(matrix *m, scalar sinp, scalar cosp, scalar sinb, scalar cosb, scalar sinh, scalar cosh) {
scalar sbsh, cbch, cbsh, sbch;
sbsh = (sinb * sinh);
cbch = (cosb * cosh);
cbsh = (cosb * sinh);
sbch = (sinb * cosh);
m->rvec.x = cbch + (sinp * sbsh); // m1
m->uvec.z = sbsh + (sinp * cbch); // m8
m->rvec.x() = cbch + (sinp * sbsh); // m1
m->uvec.z() = sbsh + (sinp * cbch); // m8
m->uvec.x = (sinp * cbsh) - sbch; // m2
m->rvec.z = (sinp * sbch) - cbsh; // m7
m->uvec.x() = (sinp * cbsh) - sbch; // m2
m->rvec.z() = (sinp * sbch) - cbsh; // m7
m->fvec.x = (sinh * cosp); // m3
m->rvec.y = (sinb * cosp); // m4
m->uvec.y = (cosb * cosp); // m5
m->fvec.z = (cosh * cosp); // m9
m->fvec.x() = (sinh * cosp); // m3
m->rvec.y() = (sinb * cosp); // m4
m->uvec.y() = (cosb * cosp); // m5
m->fvec.z() = (cosh * cosp); // m9
m->fvec.y = -sinp; // m6
m->fvec.y() = -sinp; // m6
}
void vm_AnglesToMatrix(matrix *m, angle p, angle h, angle b) {
float sinp, cosp, sinb, cosb, sinh, cosh;
scalar sinp, cosp, sinb, cosb, sinh, cosh;
sinp = FixSin(p);
cosp = FixCos(p);
@@ -393,17 +336,17 @@ void vm_AnglesToMatrix(matrix *m, angle p, angle h, angle b) {
// v - the forward vector of the new matrix
// a - the angle of rotation around the forward vector
void vm_VectorAngleToMatrix(matrix *m, vector *v, angle a) {
float sinb, cosb, sinp, cosp, sinh, cosh;
scalar sinb, cosb, sinp, cosp, sinh, cosh;
sinb = FixSin(a);
cosb = FixCos(a);
sinp = -v->y;
sinp = -v->y();
cosp = sqrt(1.0 - (sinp * sinp));
if (cosp != 0.0) {
sinh = v->x / cosp;
cosh = v->z / cosp;
sinh = v->x() / cosp;
cosh = v->z() / cosp;
} else {
sinh = 0;
cosh = 1.0;
@@ -420,7 +363,7 @@ void vm_Orthogonalize(matrix *m) {
}
// Generate right vector from forward and up vectors
m->rvec = m->uvec ^ m->fvec;
m->rvec = vector::cross3(m->uvec, m->fvec);
// Normaize new right vector
if (vm_VectorNormalize(&m->rvec) == 0) {
@@ -429,7 +372,7 @@ void vm_Orthogonalize(matrix *m) {
}
// Recompute up vector, in case it wasn't entirely perpendiclar
m->uvec = m->fvec ^ m->rvec;
m->uvec = vector::cross3(m->fvec, m->rvec);
}
// do the math for vm_VectorToMatrix()
@@ -449,21 +392,19 @@ void DoVectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) {
bad_vector2:;
if (zvec->x == 0 && zvec->z == 0) { // forward vec is straight up or down
if (zvec->x() == 0 && zvec->z() == 0) { // forward vec is straight up or down
m->rvec.x = 1.0;
m->uvec.z = (zvec->y < 0) ? 1.0 : -1.0;
m->rvec.x() = 1.0;
m->uvec.z() = (zvec->y() < 0) ? 1.0 : -1.0;
m->rvec.y = m->rvec.z = m->uvec.x = m->uvec.y = 0;
m->rvec.y() = m->rvec.z() = m->uvec.x() = m->uvec.y() = 0;
} else { // not straight up or down
xvec->x = zvec->z;
xvec->y = 0;
xvec->z = -zvec->x;
*xvec = { zvec->z(),0, -zvec->x() };
vm_VectorNormalize(xvec);
*yvec = *zvec ^ *xvec;
*yvec = vector::cross3(*zvec, *xvec);
}
} else { // use right vec
@@ -472,14 +413,14 @@ void DoVectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) {
if (vm_VectorNormalize(xvec) == 0)
goto bad_vector2;
*yvec = *zvec ^ *xvec;
*yvec = vector::cross3(*zvec, *xvec);
// normalize new perpendicular vector
if (vm_VectorNormalize(yvec) == 0)
goto bad_vector2;
// now recompute right vector, in case it wasn't entirely perpendiclar
*xvec = *yvec ^ *zvec;
*xvec = vector::cross3(*yvec, *zvec);
}
} else { // use up vec
@@ -487,14 +428,14 @@ void DoVectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) {
if (vm_VectorNormalize(yvec) == 0)
goto bad_vector2;
*xvec = *yvec ^ *zvec;
*xvec = vector::cross3(*yvec, *zvec);
// normalize new perpendicular vector
if (vm_VectorNormalize(xvec) == 0)
goto bad_vector2;
// now recompute up vector, in case it wasn't entirely perpendiclar
*yvec = *zvec ^ *xvec;
*yvec = vector::cross3(*zvec, *xvec);
}
}
@@ -526,7 +467,7 @@ void vm_VectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) {
}
}
void vm_SinCos(uint16_t a, float *s, float *c) {
void vm_SinCos(uint16_t a, scalar *s, scalar *c) {
if (s)
*s = FixSin(a);
if (c)
@@ -535,50 +476,50 @@ void vm_SinCos(uint16_t a, float *s, float *c) {
// extract angles from a matrix
angvec *vm_ExtractAnglesFromMatrix(angvec *a, matrix *m) {
float sinh, cosh, cosp;
scalar sinh, cosh, cosp;
if (m->fvec.x == 0 && m->fvec.z == 0) // zero head
a->h = 0;
if (m->fvec.x() == 0 && m->fvec.z() == 0) // zero head
a->h() = 0;
else
a->h = FixAtan2(m->fvec.z, m->fvec.x);
a->h() = FixAtan2(m->fvec.z(), m->fvec.x());
sinh = FixSin(a->h);
cosh = FixCos(a->h);
sinh = FixSin(a->h());
cosh = FixCos(a->h());
if (fabs(sinh) > fabs(cosh)) // sine is larger, so use it
cosp = (m->fvec.x / sinh);
cosp = (m->fvec.x() / sinh);
else // cosine is larger, so use it
cosp = (m->fvec.z / cosh);
cosp = (m->fvec.z() / cosh);
if (cosp == 0 && m->fvec.y == 0)
a->p = 0;
if (cosp == 0 && m->fvec.y() == 0)
a->p() = 0;
else
a->p = FixAtan2(cosp, -m->fvec.y);
a->p() = FixAtan2(cosp, -m->fvec.y());
if (cosp == 0) // the cosine of pitch is zero. we're pitched straight up. say no bank
a->b = 0;
a->b() = 0;
else {
float sinb, cosb;
scalar sinb, cosb;
sinb = (m->rvec.y / cosp);
cosb = (m->uvec.y / cosp);
sinb = (m->rvec.y() / cosp);
cosb = (m->uvec.y() / cosp);
if (sinb == 0 && cosb == 0)
a->b = 0;
a->b() = 0;
else
a->b = FixAtan2(cosb, sinb);
a->b() = FixAtan2(cosb, sinb);
}
return a;
}
// returns the value of a determinant
float calc_det_value(matrix *det) {
return det->rvec.x * det->uvec.y * det->fvec.z - det->rvec.x * det->uvec.z * det->fvec.y -
det->rvec.y * det->uvec.x * det->fvec.z + det->rvec.y * det->uvec.z * det->fvec.x +
det->rvec.z * det->uvec.x * det->fvec.y - det->rvec.z * det->uvec.y * det->fvec.x;
scalar calc_det_value(matrix *det) {
return det->rvec.x() * det->uvec.y() * det->fvec.z() - det->rvec.x() * det->uvec.z() * det->fvec.y() -
det->rvec.y() * det->uvec.x() * det->fvec.z() + det->rvec.y() * det->uvec.z() * det->fvec.x() +
det->rvec.z() * det->uvec.x() * det->fvec.y() - det->rvec.z() * det->uvec.y() * det->fvec.x();
}
// computes the delta angle between two vectors.
@@ -618,10 +559,10 @@ angle vm_DeltaAngVecNorm(vector *v0, vector *v1, vector *fvec) {
// Gets the real center of a polygon
// Returns the size of the passed in stuff
float vm_GetCentroid(vector *centroid, vector *src, int nv) {
scalar vm_GetCentroid(vector *centroid, vector *src, int nv) {
// ASSERT (nv>2);
vector normal;
float area, total_area;
scalar area, total_area;
int i;
vector tmp_center;
@@ -726,19 +667,20 @@ float vm_GetCentroidFast(vector *centroid, vector *src, int nv) {
// creates a completely random, non-normalized vector with a range of values from -1023 to +1024 values)
void vm_MakeRandomVector(vector *vec) {
vec->x = rand() - RAND_MAX / 2;
vec->y = rand() - RAND_MAX / 2;
vec->z = rand() - RAND_MAX / 2;
vec->x() = rand();
vec->y() = rand();
vec->z() = rand();
*vec -= RAND_MAX / 2;
}
// Given a set of points, computes the minimum bounding sphere of those points
float vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts) {
scalar vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts) {
// This algorithm is from Graphics Gems I. There's a better algorithm in Graphics Gems III that
// we should probably implement sometime.
vector *min_x, *max_x, *min_y, *max_y, *min_z, *max_z, *vp;
float dx, dy, dz;
float rad, rad2;
scalar dx, dy, dz;
scalar rad, rad2;
int i;
// Initialize min, max vars
@@ -747,22 +689,22 @@ float vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts) {
// First, find the points with the min & max x,y, & z coordinates
for (i = 0, vp = vecs; i < num_verts; i++, vp++) {
if (vp->x < min_x->x)
if (vp->x() < min_x->x())
min_x = vp;
if (vp->x > max_x->x)
if (vp->x() > max_x->x())
max_x = vp;
if (vp->y < min_y->y)
if (vp->y() < min_y->y())
min_y = vp;
if (vp->y > max_y->y)
if (vp->y() > max_y->y())
max_y = vp;
if (vp->z < min_z->z)
if (vp->z() < min_z->z())
min_z = vp;
if (vp->z > max_z->z)
if (vp->z() > max_z->z())
max_z = vp;
}
@@ -792,14 +734,14 @@ float vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts) {
rad2 = rad * rad;
for (i = 0, vp = vecs; i < num_verts; i++, vp++) {
vector delta;
float t2;
scalar t2;
delta = *vp - *center;
t2 = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z;
t2 = delta.x() * delta.x() + delta.y() * delta.y() + delta.z() * delta.z();
// If point outside, make the sphere bigger
if (t2 > rad2) {
float t;
scalar t;
t = sqrt(t2);
rad = (rad + t) / 2;