Skip to content

Fix multiple regressions from mathlib rewrite #703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions lib/vecmat.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ extern void vm_MakeZero(vector *v);
extern void vm_MakeZero(angvec *a);

// Rotates a vector thru a matrix
extern void vm_MatrixMulVector(vector *, vector *, matrix *);
extern void vm_MatrixMulVector(vector *, const vector *, const matrix *);

// Multiply a vector times the transpose of a matrix
void vm_VectorMulTMatrix(vector *result, vector *v, matrix *m);
void vm_VectorMulTMatrix(vector *result, const vector *v, const matrix *m);

// Multiplies 2 3x3 matrixes, returning the result in first argument
extern void vm_MatrixMul(matrix *, matrix *, matrix *);
extern void vm_MatrixMul(matrix *, const matrix *, const matrix *);

// Multiply a matrix times the transpose of a matrix
void vm_MatrixMulTMatrix(matrix *dest, matrix *src0, matrix *src1);
void vm_MatrixMulTMatrix(matrix *dest, const matrix *src0, const matrix *src1);

// Given a vector, returns the magnitude. Uses sqrt so it's slow
extern scalar vm_GetMagnitude(const vector *);
Expand All @@ -201,7 +201,7 @@ extern void vm_CrossProduct(vector *, const vector *, const vector *);
extern void vm_SubVectors(vector *, const vector *, const vector *);

// Returns adds two vectors, returns result in first arg
extern void vm_AddVectors(vector *, vector *, vector *);
extern void vm_AddVectors(vector *, const vector *, const vector *);

// Inits vector to 0,0,0
extern void vm_CenterVector(vector *);
Expand All @@ -214,13 +214,13 @@ extern void vm_AverageVector(vector *, int);
extern scalar vm_NormalizeVector(vector *);

// Scales second arg vector by 3rd arg, placing result in first arg
extern void vm_ScaleVector(vector *, vector *, scalar);
extern void vm_ScaleVector(vector *, const vector *, scalar);

// Scales all components of vector v by value s adds the result to p and stores result in vector d
extern void vm_ScaleAddVector(vector *d, vector *p, vector *v, scalar s);
extern void vm_ScaleAddVector(vector *d, const vector *p, const vector *v, scalar s);

// Divides second vector components by 3rd arg, placing result in first arg. Useful for parametric lines
extern void vm_DivVector(vector *, vector *, scalar);
extern void vm_DivVector(vector *, const vector *, scalar);

// Same as NormalizeVector, but uses approximation
extern scalar vm_NormalizeVectorFast(vector *);
Expand Down Expand Up @@ -277,49 +277,49 @@ extern scalar vm_VectorDistanceQuick(const vector *a, const vector *b);
// 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
scalar vm_GetNormalizedDir(vector *dest, vector *end, vector *start);
scalar vm_GetNormalizedDir(vector *dest, const vector *end, const vector *start);

// Returns a normalized direction vector between two points
// Uses sloppier magnitude, less precise
scalar vm_GetNormalizedDirFast(vector *dest, vector *end, vector *start);
scalar vm_GetNormalizedDirFast(vector *dest, const vector *end, const vector *start);

// extract angles from a matrix
angvec *vm_ExtractAnglesFromMatrix(angvec *a, matrix *m);
angvec *vm_ExtractAnglesFromMatrix(angvec *a, const matrix *m);

// returns the angle between two vectors and a forward vector
angle vm_DeltaAngVec(vector *v0, vector *v1, vector *fvec);
angle vm_DeltaAngVec(const vector *v0, const vector *v1, const vector *fvec);

// returns the angle between two normalized vectors and a forward vector
angle vm_DeltaAngVecNorm(vector *v0, vector *v1, vector *fvec);
angle vm_DeltaAngVecNorm(const vector *v0, const vector *v1, const vector *fvec);

// Computes the distance from a point to a plane.
// Parms: checkp - the point to check
// 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
scalar vm_DistToPlane(vector *checkp, vector *norm, vector *planep);
scalar vm_DistToPlane(const vector *checkp, const vector *norm, const vector *planep);

// returns the value of a determinant
scalar calc_det_value(matrix *det);
scalar calc_det_value(const matrix *det);

void vm_MakeInverseMatrix(matrix *dest);
void vm_SinCosToMatrix(matrix *m, scalar sinp, scalar cosp, scalar sinb, scalar cosb, scalar sinh, scalar cosh);

// Gets the real center of a polygon
scalar vm_GetCentroid(vector *centroid, vector *src, int nv);
scalar vm_GetCentroid(vector *centroid, const vector *src, int nv);

// retrieves a random vector in values -RAND_MAX/2 to RAND_MAX/2
void vm_MakeRandomVector(vector *vec);

// Given a set of points, computes the minimum bounding sphere of those points
scalar vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts);
scalar vm_ComputeBoundingSphere(vector *center, const vector *vecs, int num_verts);

// Gets the real center of a polygon, but uses fast magnitude calculation
// Returns the size of the passed in stuff
scalar vm_GetCentroidFast(vector *centroid, vector *src, int nv);
scalar vm_GetCentroidFast(vector *centroid, const vector *src, int nv);

// Here are the C++ operator overloads -- they do as expected
extern matrix operator*(matrix src0, matrix src1);
extern matrix operator*=(matrix &src0, matrix src1);
extern matrix operator*(const matrix &src0, const matrix &src1);
extern matrix operator*=(matrix &src0, const matrix &src1);

#endif
64 changes: 32 additions & 32 deletions lib/vecmat_external.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,60 +150,60 @@ constexpr inline vec<T,3,A> operator-() const { vec<T,N,A> neg = {}; neg.fil

/* arithmetic vector */
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive, typename T_DST = decltype((T)1 + (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator+(const vec<T_OTHER,N_OTHER,A_OTHER> other) const
constexpr inline vec<T_DST,N,A> operator+(const vec<T_OTHER,N_OTHER,A_OTHER> &other) const
{
vec<T_DST,N,A> dst = {};
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), dst.begin(), std::plus<>{});
return dst;
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive, typename T_DST = decltype((T)1 - (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator-(const vec<T_OTHER,N_OTHER,A_OTHER> other) const
constexpr inline vec<T_DST,N,A> operator-(const vec<T_OTHER,N_OTHER,A_OTHER> &other) const
{
vec<T_DST,N,A> dst = {};
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), dst.begin(), std::minus<>{});
return dst;
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive, typename T_DST = decltype((T)1 * (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator*(const vec<T_OTHER,N_OTHER,A_OTHER> other) const
constexpr inline vec<T_DST,N,A> operator*(const vec<T_OTHER,N_OTHER,A_OTHER> &other) const
{
vec<T_DST,N,A> dst = {};
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), dst.begin(), std::multiplies<>{});
return dst;
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive, typename T_DST = decltype((T)1 / (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator/(const vec<T_OTHER,N_OTHER,A_OTHER> other) const
constexpr inline vec<T_DST,N,A> operator/(const vec<T_OTHER,N_OTHER,A_OTHER> &other) const
{
vec<T_DST,N,A> dst = {};
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), dst.begin(), [](const T& a, const T& b) { return (T)a*(T)1/b; });
return dst;
}
/* arithmetic scalar */
template<typename T_OTHER, typename T_DST = decltype((T)1 + (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator+(const T_OTHER other) const
constexpr inline vec<T_DST,N,A> operator+(const T_OTHER &other) const
{
vec<T_DST,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), dst.begin(), std::plus<>{});
return dst;
}
template<typename T_OTHER, typename T_DST = decltype((T)1 - (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator-(const T_OTHER other) const
constexpr inline vec<T_DST,N,A> operator-(const T_OTHER &other) const
{
vec<T_DST,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), dst.begin(), std::minus<>{});
return dst;
}
template<typename T_OTHER, typename T_DST = decltype((T)1 * (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator*(const T_OTHER other) const
constexpr inline vec<T_DST,N,A> operator*(const T_OTHER &other) const
{
vec<T_DST,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), dst.begin(), std::multiplies<>{});
return dst;
}
template<typename T_OTHER, typename T_DST = decltype((T)1 / (T_OTHER)1)>
constexpr inline vec<T_DST,N,A> operator/(const T_OTHER other) const
constexpr inline vec<T_DST,N,A> operator/(const T_OTHER &other) const
{
vec<T_DST,N,A> dst = {};
dst.fill((T)1/other);
Expand All @@ -213,56 +213,56 @@ constexpr inline vec<T_DST,N,A> operator/(const T_OTHER other) const

/* arithmetic assign vector */
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive>
constexpr inline vec<T,N,A> operator+=(const vec<T_OTHER,N_OTHER,A_OTHER> other)
constexpr inline vec<T,N,A> operator+=(const vec<T_OTHER,N_OTHER,A_OTHER> &other)
{
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), (*this).begin(), std::plus<>{});
return (*this);
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive>
constexpr inline vec<T,N,A> operator-=(const vec<T_OTHER,N_OTHER,A_OTHER> other)
constexpr inline vec<T,N,A> operator-=(const vec<T_OTHER,N_OTHER,A_OTHER> &other)
{
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), (*this).begin(), std::minus<>{});
return (*this);
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive>
constexpr inline vec<T,N,A> operator*=(const vec<T_OTHER,N_OTHER,A_OTHER> other)
constexpr inline vec<T,N,A> operator*=(const vec<T_OTHER,N_OTHER,A_OTHER> &other)
{
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), (*this).begin(), std::multiplies<>{});
return (*this);
}
template<typename T_OTHER, size_t N_OTHER, enum align A_OTHER = align::adaptive>
constexpr inline vec<T,N,A> operator/=(const vec<T_OTHER,N_OTHER,A_OTHER> other)
constexpr inline vec<T,N,A> operator/=(const vec<T_OTHER,N_OTHER,A_OTHER> &other)
{
std::transform((*this).cbegin(), (*this).cbegin() + std::min<size_t>(N_OTHER,N), other.cbegin(), (*this).begin(), [](const T& a, const T& b) { return (T)a*(T)1/b; });
return (*this);
}
/* arithmetic assign scalar */
template<typename T_OTHER>
constexpr inline vec<T,N,A> operator+=(const T_OTHER other)
constexpr inline vec<T,N,A> operator+=(const T_OTHER &other)
{
vec<T,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), (*this).begin(), std::plus<>{});
return (*this);
}
template<typename T_OTHER>
constexpr inline vec<T,N,A> operator-=(const T_OTHER other)
constexpr inline vec<T,N,A> operator-=(const T_OTHER &other)
{
vec<T,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), (*this).begin(), std::minus<>{});
return (*this);
}
template<typename T_OTHER>
constexpr inline vec<T,N,A> operator*=(const T_OTHER other)
constexpr inline vec<T,N,A> operator*=(const T_OTHER &other)
{
vec<T,N,A> dst = {};
dst.fill((T)other);
std::transform((*this).cbegin(), (*this).cend(), dst.cbegin(), (*this).begin(), std::multiplies<>{});
return (*this);
}
template<typename T_OTHER>
constexpr inline vec<T,N,A> operator/=(const T_OTHER other)
constexpr inline vec<T,N,A> operator/=(const T_OTHER &other)
{
vec<T,N,A> dst = {};
dst.fill((T)1/other);
Expand All @@ -271,24 +271,24 @@ constexpr inline vec<T,N,A> operator/=(const T_OTHER other)
}

/* commutative operators */
friend inline vec<T,N,A> operator+(const scalar s, vec<T,N,A> rhs) { return rhs + s; }
friend inline vec<T,N,A> operator-(const scalar s, vec<T,N,A> rhs) { return -rhs + s; }
friend inline vec<T,N,A> operator*(const scalar s, vec<T,N,A> rhs) { return rhs * s; }
friend inline vec<T,N,A> operator/(const scalar s, vec<T,N,A> rhs) { vec<T,N,A> tmp = {}; tmp.fill(s); return tmp/rhs; }
friend inline vec<T,N,A> operator+(const scalar s, const vec<T,N,A> &rhs) { return rhs + s; }
friend inline vec<T,N,A> operator-(const scalar s, const vec<T,N,A> &rhs) { return -rhs + s; }
friend inline vec<T,N,A> operator*(const scalar s, const vec<T,N,A> &rhs) { return rhs * s; }
friend inline vec<T,N,A> operator/(const scalar s, const vec<T,N,A> &rhs) { vec<T,N,A> tmp = {}; tmp.fill(s); return tmp/rhs; }

template<size_t N_A = 3, size_t N_B = 3, enum align A_A = align::adaptive, enum align A_B = align::adaptive>
constexpr static inline scalar dot(vec<T,N_A,A_A> a, vec<T,N_B,A_B> b) { return (a * b).sum(); }
constexpr static inline scalar dot(const vec<T,N_A,A_A> &a, const vec<T,N_B,A_B> &b) { return (a * b).sum(); }

template<size_t N_A = 3, size_t N_B = 3, enum align A_A = align::adaptive, enum align A_B = align::adaptive>
constexpr static inline vec<T,3> cross3(vec<T,N_A,A_A> a, vec<T,N_B,A_B> b)
constexpr static inline vec<T,3> cross3(const vec<T,N_A,A_A> &a, const vec<T,N_B,A_B> &b)
{
return vec<T,3,align::vector>{a.y()*b.z(), a.z()*b.x(), a.x()*b.y()}
- vec<T,3,align::vector>{b.y()*a.z(), b.z()*a.x(), b.x()*a.y()};
}
constexpr inline scalar mag() const { return (scalar)sqrt(dot((*this),(*this))); }

template<size_t N_A = 3, size_t N_B = 3, enum align A_A = align::adaptive, enum align A_B = align::adaptive>
static constexpr inline scalar distance(vec<T,N_A,A_A> a, vec<T,N_B,A_B> b) { return (a - b).mag(); }
static constexpr inline scalar distance(const vec<T,N_A,A_A> &a, const vec<T,N_B,A_B> &b) { return (a - b).mag(); }


};
Expand Down Expand Up @@ -364,7 +364,7 @@ constexpr static inline const matrix4 ne()
};

// Adds 2 matrices
static inline matrix operator+(matrix a, matrix b) {
static inline matrix operator+(matrix a, const matrix &b) {
// Adds two 3x3 matrixs.

a.rvec += b.rvec;
Expand All @@ -375,9 +375,9 @@ static inline matrix operator+(matrix a, matrix b) {
}

// Adds 2 matrices
static inline matrix operator+=(matrix &a, matrix b) { return (a = a + b); }
static inline matrix operator+=(matrix &a, const matrix &b) { return (a = a + b); }
// Subtracts 2 matrices
static inline matrix operator-(matrix a, matrix b) {
static inline matrix operator-(matrix a, const matrix &b) {
// subtracts two 3x3 matrices

a.rvec = a.rvec - b.rvec;
Expand All @@ -388,10 +388,10 @@ static inline matrix operator-(matrix a, matrix b) {
}

// Subtracts 2 matrices
static inline matrix operator-=(matrix &a, matrix b) { return (a = a - b); }
static inline matrix operator-=(matrix &a, const matrix &b) { return (a = a - b); }

// Does a simple dot product calculation
//static inline float operator*(vector u, vector v) { return (u.x * v.x) + (u.y * v.y) + (u.z * v.z); }
//static inline float operator*(const vector &u, const vector &v) { return (u.x * v.x) + (u.y * v.y) + (u.z * v.z); }

// Scalar multiplication
static inline matrix operator*(float s, matrix m) {
Expand All @@ -417,10 +417,10 @@ static inline matrix operator/(matrix src, float n) {
return src;
}

inline scalar vm_Dot3Product(const vector a, const vector b) { return vector::dot(a,b); }
static inline scalar vm_Dot3Vector(scalar x, scalar y, scalar z, vector *v) { return vector::dot(aligned_vector{x,y,z}, *(aligned_vector*)v); }
inline scalar vm_Dot3Product(const vector &a, const vector &b) { return vector::dot(a,b); }
static inline scalar vm_Dot3Vector(scalar x, scalar y, scalar z, const vector *v) { return vector::dot(aligned_vector{x,y,z}, *v); }

inline vector vm_Cross3Product(vector u, vector v) {
inline vector vm_Cross3Product(const vector &u, const vector &v) {
return vector::cross3(u,v);
}

Expand All @@ -447,6 +447,6 @@ static inline matrix operator~(matrix m) {
}

// Apply a matrix to a vector
static inline vector operator*(vector v, matrix m) {
static inline vector operator*(const vector &v, const matrix &m) {
return { vector::dot(v,m.rvec), vector::dot(v,m.uvec), vector::dot(v,m.fvec) };
}
Loading
Loading