📄 mtxlib.cpp
字号:
/* Copyright (C) Steven Woodcock, 2001.
* All rights reserved worldwide.
*
* This software is provided "as is" without express or implied
* warranties. You may freely copy and compile this source into
* applications you distribute provided that the copyright text
* below is included in the resulting source code, for example:
* "Portions Copyright (C) Steven Woodcock, 2001"
*/
//==========================================================
// C++ Matrix Library
// Version: 2.5
// Date: April 22, 2000
// Authors: Dante Treglia II and Mark A. DeLoura
// Thanks to: Miguel Gomez, Stan Melax, Pete Isensee,
// Gabor Nagy, Scott Bilas, James Boer, Eric Lengyel
//==========================================================
#include "mtxlib.h"
#include <cmath>
#include <cassert>
////////////////////////////////////////////////////////////
// vector2 class
//
// Constructor with initializing float values
vector2::vector2(float inX, float inY)
{
x = inX;
y = inY;
}
// Constructor with initializing vector2
vector2::vector2(const vector2 &v)
{
x = v.x;
y = v.y;
}
// Array indexing
float &vector2::operator [] (unsigned int i)
{
assert (i<2);
return *(&x+i);
}
// Array indexing
const float &vector2::operator [] (unsigned int i) const
{
assert (i<2);
return *(&x+i);
}
// Assign
vector2 &vector2::operator = (const vector2 &v)
{
x = v.x;
y = v.y;
return *this;
}
// Add a vector2 to this one
vector2 &vector2::operator += (const vector2 &v)
{
x += v.x;
y += v.y;
return *this;
}
// Subtract a vector2 from this one
vector2 &vector2::operator -= (const vector2 &v)
{
x -= v.x;
y -= v.y;
return *this;
}
// Multiply the vector2 by a float
vector2 &vector2::operator *= (float f)
{
x *= f;
y *= f;
return *this;
}
// Divide the vector2 by a float
vector2 &vector2::operator /= (float f)
{
x /= f;
y /= f;
return *this;
}
// Are these two vector2's equal?
bool operator == (const vector2 &a, const vector2 &b)
{
return ((a.x == b.x) && (a.y == b.y));
}
// Are these two vector2's not equal?
bool operator != (const vector2 &a, const vector2 &b)
{
return ((a.x != b.x) || (a.y != b.y));
}
// Add two vector2's
vector2 operator + (const vector2 &a, const vector2 &b)
{
vector2 ret(a);
ret += b;
return ret;
}
// Subtract one vector2 from another
vector2 operator - (const vector2 &a, const vector2 &b)
{
vector2 ret(a);
ret -= b;
return ret;
}
// Multiply vector2 by a float
vector2 operator * (const vector2 &v, float f)
{
return vector2(f * v.x, f * v.y);
}
// Multiply vector2 by a float
vector2 operator * (float f, const vector2 &v)
{
return vector2(f * v.x, f * v.y);
}
// Divide vector2 by a float
vector2 operator / (const vector2 &v, float f)
{
return vector2(v.x / f, v.y / f);
}
// Negate a vector2
vector2 operator - (const vector2 &a)
{
return vector2(-a.x, -a.y);
}
// Set Values
void vector2::set(float xIn, float yIn)
{
x = xIn;
y = yIn;
}
// Get length of a vector2
float vector2::length() const
{
return (float) sqrt(x*x + y*y);
}
// Get squared length of a vector2
float vector2::lengthSqr() const
{
return (x*x + y*y);
}
// Does vector2 equal (0, 0)?
bool vector2::isZero() const
{
return ((x == 0.0F) && (y == 0.0F));
}
// Normalize a vector2
vector2 &vector2::normalize()
{
float m = length();
if (m > 0.0F)
m = 1.0F / m;
else
m = 0.0F;
x *= m;
y *= m;
return *this;
}
////////////////////////////////////////////////////////////
// vector3 class
//
// Constructor with initializing float values
vector3::vector3(float inX, float inY, float inZ)
{
x = inX;
y = inY;
z = inZ;
}
// Constructor with initializing vector3
vector3::vector3(const vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
}
// Constructor with initializing vector2
vector3::vector3(const vector2 &v)
{
x = v.x;
y = v.y;
z = 0.0F;
}
// Constructor with initializing vector4
vector3::vector3(const vector4 &v)
{
x = v.x;
y = v.y;
z = v.z;
}
// Array indexing
float &vector3::operator [] (unsigned int i)
{
assert (i<3);
return *(&x+i);
}
// Array indexing
const float &vector3::operator [] (unsigned int i) const
{
assert (i<3);
return *(&x+i);
}
// Assign
vector3 &vector3::operator = (const vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
// Assign from a vector2
vector3 &vector3::operator = (const vector2 &v)
{
x = v.x;
y = v.y;
z = 0.0F;
return *this;
}
// Add a vector3 to this one
vector3 &vector3::operator += (const vector3 &v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
// Subtract a vector3 from this one
vector3 &vector3::operator -= (const vector3 &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
// Multiply the vector3 by a float
vector3 &vector3::operator *= (float f)
{
x *= f;
y *= f;
z *= f;
return *this;
}
// Divide the vector3 by a float
vector3 &vector3::operator /= (float f)
{
x /= f;
y /= f;
z /= f;
return *this;
}
// Are these two vector3's equal?
bool operator == (const vector3 &a, const vector3 &b)
{
return ((a.x == b.x) && (a.y == b.y) && (a.z == b.z));
}
// Are these two vector3's not equal?
bool operator != (const vector3 &a, const vector3 &b)
{
return ((a.x != b.x) || (a.y != b.y) || (a.z != b.z));
}
// Add two vector3's
vector3 operator + (const vector3 &a, const vector3 &b)
{
vector3 ret(a);
ret += b;
return ret;
}
// Subtract one vector3 from another
vector3 operator - (const vector3 &a, const vector3 &b)
{
vector3 ret(a);
ret -= b;
return ret;
}
// Multiply vector3 by a float
vector3 operator * (const vector3 &v, float f)
{
return vector3(f * v.x, f * v.y, f * v.z);
}
// Multiply vector3 by a float
vector3 operator * (float f, const vector3 &v)
{
return vector3(f * v.x, f * v.y, f * v.z);
}
// Divide vector3 by a float
vector3 operator / (const vector3 &v, float f)
{
return vector3(v.x / f, v.y / f, v.z / f);
}
// Negate a vector3
vector3 operator - (const vector3 &a)
{
return vector3(-a.x, -a.y, -a.z);
}
// Set Values
void vector3::set(float xIn, float yIn, float zIn)
{
x = xIn;
y = yIn;
z = zIn;
}
// Get length of a vector3
float vector3::length() const
{
return (float) sqrt(x*x + y*y + z*z);
}
// Get squared length of a vector3
float vector3::lengthSqr() const
{
return (x*x + y*y + z*z);
}
// Does vector3 equal (0, 0, 0)?
bool vector3::isZero() const
{
return ((x == 0.0F) && (y == 0.0F) && (z == 0.0F));
}
// Normalize a vector3
vector3 &vector3::normalize()
{
float m = length();
if (m > 0.0F)
m = 1.0F / m;
else
m = 0.0F;
x *= m;
y *= m;
z *= m;
return *this;
}
////////////////////////////////////////////////////////////
// vector4 class
//
// Constructor with initializing float values
vector4::vector4(float inX, float inY, float inZ, float inW)
{
x = inX;
y = inY;
z = inZ;
w = inW;
}
// Constructor with initializing vector4
vector4::vector4(const vector4 &v)
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
// Constructor with initializing vector3
vector4::vector4(const vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
w = 0.0F;
}
// Constructor with initializing vector2
vector4::vector4(const vector2 &v)
{
x = v.x;
y = v.y;
z = 0.0F;
w = 0.0F;
}
// Array indexing
float &vector4::operator [] (unsigned int i)
{
assert (i<4);
return *(&x+i);
}
// Array indexing
const float &vector4::operator [] (unsigned int i) const
{
assert (i<4);
return *(&x+i);
}
// Assign
vector4 &vector4::operator = (const vector4 &v)
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
return *this;
}
// Assign from a vector3
vector4 &vector4::operator = (const vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
w = 0.0F;
return *this;
}
// Assign from a vector2
vector4 &vector4::operator = (const vector2 &v)
{
x = v.x;
y = v.y;
z = 0.0F;
w = 0.0F;
return *this;
}
// Add a vector4 to this one
vector4 &vector4::operator += (const vector4 &v)
{
x += v.x;
y += v.y;
z += v.z;
w += v.w;
return *this;
}
// Subtract a vector4 from this one
vector4 &vector4::operator -= (const vector4 &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
w -= v.w;
return *this;
}
// Multiply the vector4 by a float
vector4 &vector4::operator *= (float f)
{
x *= f;
y *= f;
z *= f;
w *= f;
return *this;
}
// Divide the vector4 by a float
vector4 &vector4::operator /= (float f)
{
x /= f;
y /= f;
z /= f;
w /= f;
return *this;
}
// Are these two vector4's equal?
bool operator == (const vector4 &a, const vector4 &b)
{
return ((a.x == b.x) && (a.y == b.y) &&
(a.z == b.z) && (a.w == b.w));
}
// Are these two vector4's not equal?
bool operator != (const vector4 &a, const vector4 &b)
{
return ((a.x != b.x) || (a.y != b.y) ||
(a.z != b.z) || (a.w != b.w));
}
// Add two vector4's
vector4 operator + (const vector4 &a, const vector4 &b)
{
vector4 ret(a);
ret += b;
return ret;
}
// Subtract one vector4 from another
vector4 operator - (const vector4 &a, const vector4 &b)
{
vector4 ret(a);
ret -= b;
return ret;
}
// Multiply vector4 by a float
vector4 operator * (const vector4 &v, float f)
{
return vector4(f * v.x, f * v.y, f * v.z, f * v.w);
}
// Multiply vector4 by a float
vector4 operator * (float f, const vector4 &v)
{
return vector4(f * v.x, f * v.y, f * v.z, f * v.w);
}
// Divide vector4 by a float
vector4 operator / (const vector4 &v, float f)
{
return vector4(v.x / f, v.y / f, v.z / f, v.w / f);
}
// Negate a vector4
vector4 operator - (const vector4 &a)
{
return vector4(-a.x, -a.y, -a.z, -a.w);
}
// Set Values
void vector4::set(float xIn, float yIn, float zIn, float wIn)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -