📄 vector.h
字号:
// ------------------------------------
// Vector class for handling vectors
// ------------------------------------
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <cmath>
#include "point.h"
// Make it easy to switch betwen var types (you only want to use one type in the same project
// so templates is both unnceccessary and is harder to optimize)
typedef float vectType;
class GcVector2
{
public:
/* Everyting is declared in the header (for inlining) since */
/* speed is vital to vector calculation */
// Default constructor
GcVector2(): x(0), y(0){}
// Overloaded constructor allowing initialazion
GcVector2(vectType sX, vectType sY): x(sX), y(sY){}
// Destructor
~GcVector2() {}
vectType & operator[](int i)
{
return array[i];
}
const vectType operator[](int i) const
{
return array[i];
}
operator float*()
{
return (float *)array;
}
/* Overload the standard math operators to work with the vector */
// Negation
const GcVector2 operator-()
{
return GcVector2(-x, -y);
}
// Addition (vect1 + vect2)
const GcVector2 operator+(const GcVector2 vect)
{
return GcVector2(x + vect.x, y + vect.y);
}
// Addition (vect1 += vect2)
void operator+=(const GcVector2 vect)
{
x += vect.x;
y += vect.y;
}
// Substraction (vector1 - vector2)
const GcVector2 operator-(const GcVector2 vect)
{
return GcVector2(x - vect.x, y - vect.y);
}
const GcVector2 operator-(const GcVector2 vect) const
{
return GcVector2(x - vect.x, y - vect.y);
}
// Substraction (vector1 -= vector2)
void operator-=(const GcVector2 vect)
{
x -= vect.x;
y -= vect.y;
}
// Scalar multiplication (vect * scal)
const GcVector2 operator*(vectType scalar)
{
return GcVector2(x * scalar, y * scalar);
}
// Scalar multiplication (vect *= scal)
void operator*=(vectType scalar)
{
x *= scalar;
y *= scalar;
}
// Scalar division (vect / scal)
const GcVector2 operator/(vectType scalar)
{
return GcVector2(x / scalar, y / scalar);
}
// Scalar division (vect /= scal)
void operator/=(vectType scalar)
{
x /= scalar;
y /= scalar;
}
/* Overload the standard comperation operator */
// Equal too
bool operator==(const GcVector2 vect)
{
return ((x == vect.x) && (y == vect.y));
}
/* Common vector calculations */
// Return the lenght of the vector (in other words return |z|)
const vectType Length()
{
return vectType(sqrt(x*x + y*y));
}
// Calculate the dot product
const vectType DotProduct(const GcVector2 vect)
{
return (x * vect.x + y * vect.y);
}
// Calcualte the angle betwen two vectors (in radians)
const vectType AngleBetwen(GcVector2 vect)
{
return vectType(acos(this->DotProduct(vect) / (this->Length() * vect.Length())));
}
// Normalize the vector
void Normalize()
{
vectType length;
// Clacluate the lenght of the crossproduct vector
length = this->Length();
// Normalize the
x /= length;
y /= length;
}
/* The variables are public for easier access */
//vectType x, y;
union
{
struct
{
vectType x, y;
};
vectType array[2];
};
};
class GcVector3
{
public:
/* Everyting is declared in the header (for inlining) since */
/* speed is vital to vector calculation */
// Default constructor
GcVector3(): x(0), y(0), z(0) {}
// Overloaded constructor allowing initialazion
GcVector3(vectType sX, vectType sY, vectType sZ): x(sX), y(sY), z(sZ) {}
// Destructor
~GcVector3() {}
vectType & operator[](int i)
{
return array[i];
}
const vectType operator[](int i) const
{
return array[i];
}
operator float*()
{
return (float *)array;
}
/* Overload the standard math operators to work with the vector */
// Negation
GcVector3 operator-()
{
return GcVector3(-x, -y, -z);
}
// Addition (vect1 + vect2)
GcVector3 operator+(const GcVector3 vect)
{
return GcVector3(x + vect.x, y + vect.y, z + vect.z);
}
// Addition (vect1 += vect2)
void operator+=(const GcVector3 vect)
{
x += vect.x;
y += vect.y;
z += vect.z;
}
// Substraction (vector1 - vector2)
GcVector3 operator-(const GcVector3 vect)
{
return GcVector3(x - vect.x, y - vect.y, z - vect.z);
}
const GcVector3 operator-(const GcVector3 vect) const
{
return GcVector3(x - vect.x, y - vect.y, z - vect.z);
}
// Substraction (vector1 -= vector2)
void operator-=(const GcVector3 vect)
{
x -= vect.x;
y -= vect.y;
z -= vect.z;
}
// Scalar multiplication (vect * scal)
GcVector3 operator*(vectType scalar)
{
return GcVector3(x * scalar, y * scalar, z * scalar);
}
// Scalar multiplication (vect *= scal)
void operator*=(vectType scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
}
// Scalar division (vect / scal)
GcVector3 operator/(vectType scalar)
{
return GcVector3(x / scalar, y / scalar, z / scalar);
}
// Scalar division (vect /= scal)
void operator/=(vectType scalar)
{
x /= scalar;
y /= scalar;
z /= scalar;
}
/* Overload the standard comperation operator */
// Equal too
bool operator==(const GcVector3 vect)
{
return ((x == vect.x) && (y == vect.y) && (z == vect.z));
}
/* Allow points to be converted into vectors */
GcVector3 operator=(GcPoint3 point)
{
x = point.x;
y = point.y;
z = point.z;
return *this;
}
/* Common vector calculations */
// Return the lenght of the vector (in other words return |z|)
const vectType Length()
{
return vectType(sqrt(x*x + y*y + z*z));
}
// Calculate the dot product
const vectType DotProduct(const GcVector3 vect) const
{
return (x * vect.x + y * vect.y + z * vect.z);
}
// Calculate the dot product
vectType operator*(const GcVector3 & vect)
{
return (x * vect.x + y * vect.y + z * vect.z);
}
// Calculate the dot product
vectType operator*(const GcVector3 & vect) const
{
return (x * vect.x + y * vect.y + z * vect.z);
}
// Calcualte the angle betwen two vectors (in radians)
const vectType AngleBetwen(GcVector3 vect)
{
return vectType(acos(this->DotProduct(vect) / (this->Length() * vect.Length())));
}
// Return the cross product
const GcVector3 CrossProduct(const GcVector3 vect)
{
return GcVector3((y * vect.z - z * vect.y), (z * vect.x - x * vect.z), (x * vect.y - y * vect.x));
}
// Normalize the vector against another vector
void Normalize(const GcVector3 vect)
{
vectType length;
// Clacluate the cross product
*this = this->CrossProduct(vect);
// Clacluate the lenght of the crossproduct vector
length = this->Length();
// Normalize the
x /= length;
y /= length;
z /= length;
}
// Normalize the vector
void Normalize()
{
vectType length;
// Clacluate the lenght of the crossproduct vector
length = this->Length();
// Normalize the
x /= length;
y /= length;
z /= length;
}
/* The variables are public for easier access */
//vectType x, y, z;
union
{
struct
{
vectType x, y, z;
};
vectType array[3];
};
};
// Vector4 class
class GcVector4
{
public:
/* Everyting is declared in the header (for inlining) since */
/* speed is vital to vector calculation */
// Default constructor
GcVector4() : x(0), y(0), z(0), w(0) {}
// Overloaded constructor allowing initialazion
GcVector4(vectType sX, vectType sY, vectType sZ, vectType sW) : x(sX), y(sY), z(sZ), w(sW) {}
// Destructor
~GcVector4() {}
vectType & operator[](int i)
{
return array[i];
}
const vectType operator[](int i) const
{
return array[i];
}
operator float*()
{
return (float *)array;
}
/* Overload the standard math operators to work with the vector */
// Negation
const GcVector4 operator-()
{
return GcVector4(-x, -y, -z, -w);
}
// Addition (vect1 + vect2)
const GcVector4 operator+(const GcVector4 vect)
{
return GcVector4(x + vect.x, y + vect.y, z + vect.z, w + vect.w);
}
// Addition (vect1 += vect2)
void operator+=(const GcVector4 vect)
{
x += vect.x;
y += vect.y;
z += vect.z;
w += vect.w;
}
// Substraction (vector1 - vector2)
const GcVector4 operator-(const GcVector4 vect)
{
return GcVector4(x - vect.x, y - vect.y, z - vect.z, w - vect.w);
}
const GcVector4 operator-(const GcVector4 vect) const
{
return GcVector4(x - vect.x, y - vect.y, z - vect.z, w - vect.w);
}
// Substraction (vector1 -= vector2)
void operator-=(const GcVector4 vect)
{
x -= vect.x;
y -= vect.y;
z -= vect.z;
w -= vect.w;
}
// Scalar multiplication (vect * scal)
const GcVector4 operator*(vectType scalar)
{
return GcVector4(x * scalar, y * scalar, z * scalar, w * scalar);
}
// Scalar multiplication (vect *= scal)
void operator*=(vectType scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
}
// Scalar division (vect / scal)
const GcVector4 operator/(vectType scalar)
{
return GcVector4(x / scalar, y / scalar, z / scalar, w / scalar);
}
// Scalar division (vect /= scal)
void operator/=(vectType scalar)
{
x /= scalar;
y /= scalar;
z /= scalar;
w /= scalar;
}
/* Overload the standard comperation operator */
// Equal too
bool operator==(const GcVector4 vect)
{
return ((x == vect.x) && (y == vect.y) && (z == vect.z) && (w == vect.w));
}
/* Common vector calculations */
// Return the length of the vector (in other words return |z|)
const vectType Length()
{
return vectType(sqrt(x*x + y*y + z*z + w*w));
//return vectType(sqrt(x*x + y*y + z*z));
}
// Calculate the dot product
const vectType DotProduct(const GcVector4 vect)
{
return (x * vect.x + y * vect.y + z * vect.z + w * vect.w);
}
// Calculate the dot product
vectType operator*(const GcVector4 & vect)
{
return (x * vect.x + y * vect.y + z * vect.z + w * vect.w);
}
// Calculate the dot product
vectType operator*(const GcVector4 & vect) const
{
return (x * vect.x + y * vect.y + z * vect.z + w * vect.w);
}
// Calcualte the angle betwen two vectors (in radians)
const vectType AngleBetween(GcVector4 vect)
{
return vectType(acos(this->DotProduct(vect) / (this->Length() * vect.Length())));
}
// Return the cross product
const GcVector4 CrossProduct(const GcVector4 vect)
{
GcVector4 v;
v.x = y * vect.z - z * vect.y;
v.y = z * vect.x - x * vect.z;
v.z = x * vect.y - y * vect.z;
v.w = 1.0;
return v;
}
// Normalize the vector against another vector
void Normalize(const GcVector4 vect)
{
vectType length;
// Clacluate the cross product
*this = this->CrossProduct(vect);
// Clacluate the length of the crossproduct vector
length = this->Length();
// Normalize the
x /= length;
y /= length;
z /= length;
w /= length;
}
// Normalize the vector
void Normalize()
{
vectType length;
// Clacluate the lenght of the crossproduct vector
length = this->Length();
// Normalize the
x /= length;
y /= length;
z /= length;
w /= length;
}
// Normalize the plane (leave the distance alone)
void NormalizePlane()
{
vectType length;
// Clacluate the lenght of the crossproduct vector
length = (float)sqrt(x*x + y*y + z*z);
// Normalize the
x /= length;
y /= length;
z /= length;
w /= length;
}
GcVector4 & Homogenize()
{
if (w == 0)
w = 1.0;
x /= w;
y /= w;
z /= w;
w = 1.0;
return *this;
}
/* The variables are public for easier access */
union
{
struct
{
vectType x, y, z, w;
};
vectType array[4];
};
// vectType x, y, z, w;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -