📄 mtxlib.cpp
字号:
x = xIn;
y = yIn;
z = zIn;
w = wIn;
}
// Get length of a vector4
float vector4::length() const
{
return (float) sqrt(x*x + y*y + z*z + w*w);
}
// Get squared length of a vector4
float vector4::lengthSqr() const
{
return (x*x + y*y + z*z + w*w);
}
// Does vector4 equal (0, 0, 0, 0)?
bool vector4::isZero() const
{
return ((x == 0.0F) && (y == 0.0F) && (z == 0.0F) && (w == 0.0F));
}
// Normalize a vector4
vector4 &vector4::normalize()
{
float m = length();
if (m > 0.0F)
m = 1.0F / m;
else
m = 0.0F;
x *= m;
y *= m;
z *= m;
w *= m;
return *this;
}
////////////////////////////////////////////////////////////
// Miscellaneous vector functions
//
// Return Normal of vector2's
vector2 Normalized(const vector2 &a)
{
vector2 ret(a);
return ret.normalize();
}
// Return Normal of vector3's
vector3 Normalized(const vector3 &a)
{
vector3 ret(a);
return ret.normalize();
}
// Return Normal of vector4's
vector4 Normalized(const vector4 &a)
{
vector4 ret(a);
return ret.normalize();
}
// Dot product of two vector2's
float DotProduct(const vector2 &a, const vector2 &b)
{
return a.x*b.x + a.y*b.y;
}
// Dot product of two vector3's
float DotProduct(const vector3 &a, const vector3 &b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}
// Dot product of two vector4's
float DotProduct(const vector4 &a, const vector4 &b)
{
return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
}
// Swap two vector2's
void SwapVec(vector2 &a, vector2 &b)
{
vector2 tmp(a);
a = b;
b = tmp;
}
// Swap two vector3's
void SwapVec(vector3 &a, vector3 &b)
{
vector3 tmp(a);
a = b;
b = tmp;
}
// Swap two vector4's
void SwapVec(vector4 &a, vector4 &b)
{
vector4 tmp(a);
a = b;
b = tmp;
}
// Cross product of two vector3's
vector3 CrossProduct(const vector3 &a, const vector3 &b)
{
return vector3(a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x);
}
// Are these two vector2's nearly equal?
bool NearlyEquals( const vector2& a, const vector2& b, float r )
{
vector2 diff = a - b; // difference
return (DotProduct(diff, diff) < r*r); // radius
}
// Are these two vector3's nearly equal?
bool NearlyEquals( const vector3& a, const vector3& b, float r )
{
vector3 diff = a - b; // difference
return (DotProduct(diff, diff) < r*r); // radius
}
// Are these two vector4's nearly equal?
bool NearlyEquals( const vector4& a, const vector4& b, float r )
{
vector4 diff = a - b; // difference
return (DotProduct(diff, diff) < r*r); // radius
}
////////////////////////////////////////////////////////////
// matrix33 class
//
// Constructor with initializing value
matrix33::matrix33(float v)
{
col[0].set(v, v, v);
col[1].set(v, v, v);
col[2].set(v, v, v);
}
// Constructor with initializing matrix33
matrix33::matrix33(const matrix33 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
}
// Constructor with initializing vector3's
matrix33::matrix33(const vector3 &v0, const vector3 &v1, const vector3 &v2)
{
col[0] = v0;
col[1] = v1;
col[2] = v2;
}
// Array indexing
vector3 &matrix33::operator [] (unsigned int i)
{
assert (i<3);
return (vector3&)col[i];
}
// Array indexing
const vector3 &matrix33::operator [] (unsigned int i) const
{
assert (i<3);
return (vector3&)col[i];
}
// Assign
matrix33 &matrix33::operator = (const matrix33 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
return *this;
}
// Add a matrix33 to this one
matrix33 &matrix33::operator += (const matrix33 &m)
{
col[0] += m[0];
col[1] += m[1];
col[2] += m[2];
return *this;
}
// Subtract a matrix33 from this one
matrix33 &matrix33::operator -= (const matrix33 &m)
{
col[0] -= m[0];
col[1] -= m[1];
col[2] -= m[2];
return *this;
}
// Multiply the matrix33 by another matrix33
matrix33 &matrix33::operator *= (const matrix33 &m)
{
matrix33 t;
for (unsigned int r = 0; r < 3; r++)
{
for (unsigned int c = 0; c < 3; c++)
{
float f = 0;
f += col[0][r] * m[c][0];
f += col[1][r] * m[c][1];
f += col[2][r] * m[c][2];
t[c][r] = f;
}
}
*this = t;
return *this;
}
// Multiply the matrix33 by a float
matrix33 &matrix33::operator *= (float f)
{
col[0] *= f;
col[1] *= f;
col[2] *= f;
return *this;
}
// Are these two matrix33's equal?
bool operator == (const matrix33 &a, const matrix33 &b)
{
return ((a[0] == b[0]) && (a[1] == b[1]) && (a[2] == b[2]));
}
// Are these two matrix33's not equal?
bool operator != (const matrix33 &a, const matrix33 &b)
{
return ((a[0] != b[0]) || (a[1] != b[1]) || (a[2] != b[2]));
}
// Add two matrix33's
matrix33 operator + (const matrix33 &a, const matrix33 &b)
{
matrix33 ret(a);
ret += b;
return ret;
}
// Subtract one matrix33 from another
matrix33 operator - (const matrix33 &a, const matrix33 &b)
{
matrix33 ret(a);
ret -= b;
return ret;
}
// Multiply matrix33 by another matrix33
matrix33 operator * (const matrix33 &a, const matrix33 &b)
{
matrix33 ret(a);
ret *= b;
return ret;
}
// Multiply a vector3 by this matrix33
vector3 operator * (const matrix33 &m, const vector3 &v)
{
vector3 ret;
ret.x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0];
ret.y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1];
ret.z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2];
return ret;
}
// Multiply a vector3 by this matrix33
vector3 operator * (const vector3 &v, const matrix33 &m)
{
vector3 ret;
ret.x = DotProduct(m[0], v);
ret.y = DotProduct(m[1], v);
ret.z = DotProduct(m[2], v);
return ret;
}
// Multiply matrix33 by a float
matrix33 operator * (float f, const matrix33 &m)
{
matrix33 ret(m);
ret *= f;
return ret;
}
// Multiply matrix33 by a float
matrix33 operator * (const matrix33 &m, float f)
{
matrix33 ret(m);
ret *= f;
return ret;
}
// Set matrix33 to the identity matrix
matrix33 &matrix33::identity()
{
for (unsigned int c = 0; c < 3; c++)
{
for (unsigned int r = 0; r < 3; r++)
{
if (c == r)
col[c][r] = 1.0F;
else
col[c][r] = 0.0F;
}
}
return *this;
}
// Transpose the matrix33
matrix33 &matrix33::transpose()
{
float t;
for (unsigned int c = 0; c < 3; c++)
{
for (unsigned int r = c + 1; r < 3; r++)
{
t = col[c][r];
col[c][r] = col[r][c];
col[r][c] = t;
}
}
return *this;
}
// Invert the matrix33
matrix33 &matrix33::invert()
{
matrix33 a(*this);
matrix33 b(IdentityMatrix33());
unsigned int c, r;
unsigned int cc;
unsigned int rowMax; // Points to max abs value row in this column
unsigned int row;
float tmp;
// Go through columns
for (c=0; c<3; c++)
{
// Find the row with max value in this column
rowMax = c;
for (r=c+1; r<3; r++)
{
if (fabs(a[c][r]) > fabs(a[c][rowMax]))
{
rowMax = r;
}
}
// If the max value here is 0, we can't invert. Return identity.
if (a[rowMax][c] == 0.0F)
return (identity());
// Swap row "rowMax" with row "c"
for (cc=0; cc<3; cc++)
{
tmp = a[cc][c];
a[cc][c] = a[cc][rowMax];
a[cc][rowMax] = tmp;
tmp = b[cc][c];
b[cc][c] = b[cc][rowMax];
b[cc][rowMax] = tmp;
}
// Now everything we do is on row "c".
// Set the max cell to 1 by dividing the entire row by that value
tmp = a[c][c];
for (cc=0; cc<3; cc++)
{
a[cc][c] /= tmp;
b[cc][c] /= tmp;
}
// Now do the other rows, so that this column only has a 1 and 0's
for (row = 0; row < 3; row++)
{
if (row != c)
{
tmp = a[c][row];
for (cc=0; cc<3; cc++)
{
a[cc][row] -= a[cc][c] * tmp;
b[cc][row] -= b[cc][c] * tmp;
}
}
}
}
*this = b;
return *this;
}
// Return a matrix33 set to the identity matrix
matrix33 IdentityMatrix33()
{
matrix33 ret;
return ret.identity();
}
// Return the transpose of the matrix33
matrix33 TransposeMatrix33(const matrix33 &m)
{
matrix33 ret(m);
return ret.transpose();
}
// Return the inverted matrix33
matrix33 InvertMatrix33(const matrix33 &m)
{
matrix33 ret(m);
return ret.invert();
}
// Return a 2D rotation matrix33
matrix33 RotateRadMatrix33(float rad)
{
matrix33 ret;
float sinA, cosA;
sinA = (float)sin(rad);
cosA = (float)cos(rad);
ret[0][0] = cosA; ret[1][0] = -sinA; ret[2][0] = 0.0F;
ret[0][1] = sinA; ret[1][1] = cosA; ret[2][1] = 0.0F;
ret[0][2] = 0.0F; ret[1][2] = 0.0F; ret[2][2] = 1.0F;
return ret;
}
// Return a 2D translation matrix33
matrix33 TranslateMatrix33(float x, float y)
{
matrix33 ret;
ret.identity();
ret[2][0] = x;
ret[2][1] = y;
return ret;
}
// Return a 2D/3D scale matrix33
matrix33 ScaleMatrix33(float x, float y, float z)
{
matrix33 ret;
ret.identity();
ret[0][0] = x;
ret[1][1] = y;
ret[2][2] = z;
return ret;
}
////////////////////////////////////////////////////////////
// matrix44 class
//
// Constructor with initializing value
matrix44::matrix44(float v)
{
col[0].set(v, v, v, v);
col[1].set(v, v, v, v);
col[2].set(v, v, v, v);
col[3].set(v, v, v, v);
}
// Constructor with initializing matrix44
matrix44::matrix44(const matrix44 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
col[3] = m[3];
}
// Constructor with initializing matrix33
matrix44::matrix44(const matrix33 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
col[3] = vector4(0.0, 0.0, 0.0, 1.0);
}
// Constructor with initializing vector4's
matrix44::matrix44(const vector4 &v0, const vector4 &v1,
const vector4 &v2, const vector4 &v3)
{
col[0] = v0;
col[1] = v1;
col[2] = v2;
col[3] = v3;
}
// Array indexing
vector4 &matrix44::operator [] (unsigned int i)
{
assert (i<4);
return (vector4&) col[i];
}
// Array indexing
const vector4 &matrix44::operator [] (unsigned int i) const
{
assert (i<4);
return (vector4&) col[i];
}
// Assign
matrix44 &matrix44::operator = (const matrix44 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
col[3] = m[3];
return *this;
}
// Assign a matrix33 to the matrix44
matrix44 &matrix44::operator = (const matrix33 &m)
{
col[0] = m[0];
col[1] = m[1];
col[2] = m[2];
col[3] = vector4(0.0, 0.0, 0.0, 1.0);
return *this;
}
// Add a matrix44 to this one
matrix44 &matrix44::operator += (const matrix44 &m)
{
col[0] += m[0];
col[1] += m[1];
col[2] += m[2];
col[3] += m[3];
return *this;
}
// Subtract a matrix44 from this one
matrix44 &matrix44::operator -= (const matrix44 &m)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -