/*
* Vector3.h
* Jonathan Boldiga
* 08/30/03
*
* Description: Vector class. Allows manipulation of vectors via addition,
* subtraction, division, multiplication, normals, etc.
*
*/
#ifndef __Vector3_H
#define __Vector3_H
#include
#define PI (3.14159265359f)
#define DEG2RAD(a) (PI/180*(a))
#define RAD2DEG(a) (180/PI*(a))
class Vector3{
public:
// x,y,z coordinates
float x;
float y;
float z;
public:
Vector3(float a = 0, float b = 0, float c = 0) : x(a), y(b), z(c) {}
Vector3(const Vector3 &vector) : x(vector.x), y(vector.y), z(vector.z) {}
// vector index
float &operator[](const long index){
return *((&x)+index);
}
// vector assignment
const Vector3 &operator=(const Vector3 &vector){
x = vector.x;
y = vector.y;
z = vector.z;
return *this;
}
// vectorector equality
const bool operator==(const Vector3 &vector) const{
return ((x == vector.x) && (y == vector.y) && (z == vector.z));
}
// vectorector inequality
const bool operator!=(const Vector3 &vector) const{
return !(*this == vector);
}
// vector add
const Vector3 operator+(const Vector3 &vector) const{
return Vector3(x + vector.x, y + vector.y, z + vector.z);
}
// vector add (opposite of negation)
const Vector3 operator+() const{
return Vector3(*this);
}
// vector increment
const Vector3& operator+=(const Vector3& vector){
x += vector.x;
y += vector.y;
z += vector.z;
return *this;
}
// vector subtraction
const Vector3 operator-(const Vector3& vector) const{
return Vector3(x - vector.x, y - vector.y, z - vector.z);
}
// vector negation
const Vector3 operator-() const{
return Vector3(-x, -y, -z);
}
// vector decrement
const Vector3 &operator-=(const Vector3& vector){
x -= vector.x;
y -= vector.y;
z -= vector.z;
return *this;
}
// scalar self-multiply
const Vector3 &operator*=(const float &s){
x *= s;
y *= s;
z *= s;
return *this;
}
// scalar self-divectoride
const Vector3 &operator/=(const float &s){
const float recip = 1/s; // for speed, one divectorision
x *= recip;
y *= recip;
z *= recip;
return *this;
}
// post multiply by scalar
const Vector3 operator*(const float &s) const{
return Vector3(x*s, y*s, z*s);
}
// pre multiply by scalar
friend inline const Vector3 operator*(const float &s, const Vector3 &vector){
return vector*s;
}
const Vector3 operator*(const Vector3& vector) const{
return Vector3(x*vector.x, y*vector.y, z*vector.z);
}
// divide by scalar
const Vector3 operator/(float s) const{
s = 1/s;
return Vector3(s*x, s*y, s*z);
}
// cross product
const Vector3 crossProduct(const Vector3 &vector) const{
return Vector3(y*vector.z - z*vector.y, z*vector.x - x*vector.z, x*vector.y - y*vector.x);
}
// cross product
const Vector3 operator^(const Vector3 &vector) const{
return Vector3(y*vector.z - z*vector.y, z*vector.x - x*vector.z, x*vector.y - y*vector.x);
}
// dot product
const float dotProduct(const Vector3 &vector) const{
return x*vector.x + y*vector.x + z*vector.z;
}
// dot product
const float operator%(const Vector3 &vector) const{
return x*vector.x + y*vector.x + z*vector.z;
}
// length of vector
const float length() const{
return (float)sqrt((double)(x*x + y*y + z*z));
}
// return the unit vector
const Vector3 unitVector() const{
return (*this) / length();
}
// normalize this vector
void normalize(){
(*this) /= length();
}
const float operator!() const{
return sqrtf(x*x + y*y + z*z);
}
// return vector with specified length
const Vector3 operator | (const float length) const{
return *this * (length / !(*this));
}
// set length of vector equal to length
const Vector3& operator |= (const float length){
return *this = *this | length;
}
// return angle between two vectors
const float inline Angle(const Vector3& normal) const{
return acosf(*this % normal);
}
// reflect this vector off surface with normal vector
const Vector3 inline reflection(const Vector3& normal) const{
const Vector3 vector(*this | 1); // normalize this vector
return (vector - normal * 2.0 * (vector % normal)) * !*this;
}
// rotate angle degrees about a normal
const Vector3 inline rotate(const float angle, const Vector3& normal) const{
const float cosine = cosf(angle);
const float sine = sinf(angle);
return Vector3(*this * cosine + ((normal * *this) * (1.0f - cosine)) *
normal + (*this ^ normal) * sine);
}
};
#endif