[930] | 1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 2 | // VECTOR4D.h
|
---|
| 3 | // Class declaration for a 4d vector
|
---|
| 4 | // You may use this code however you wish, but if you do, please credit me and
|
---|
| 5 | // provide a link to my website in a readme file or similar
|
---|
| 6 | // Downloaded from: www.paulsprojects.net
|
---|
| 7 | // Created: 20th July 2002
|
---|
| 8 | // Modified: 8th November 2002 - Changed Constructor layout
|
---|
| 9 | // - Some speed Improvements
|
---|
| 10 | // - Corrected Lerp
|
---|
| 11 | // 7th January 2003 - Added QuadraticInterpolate
|
---|
| 12 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 13 |
|
---|
| 14 | #ifndef VECTOR4D_H
|
---|
| 15 | #define VECTOR4D_H
|
---|
| 16 |
|
---|
| 17 | class VECTOR4D
|
---|
| 18 | {
|
---|
| 19 | public:
|
---|
| 20 | //constructors
|
---|
| 21 | VECTOR4D(void) : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
|
---|
| 22 | {}
|
---|
| 23 |
|
---|
| 24 | VECTOR4D(float newX, float newY, float newZ, float newW)
|
---|
| 25 | : x(newX), y(newY), z(newZ), w(newW)
|
---|
| 26 | {}
|
---|
| 27 |
|
---|
| 28 | VECTOR4D(const float * rhs) : x(*rhs), y(*(rhs+1)), z(*(rhs+2)), w(*(rhs+3))
|
---|
| 29 | {}
|
---|
| 30 |
|
---|
| 31 | VECTOR4D(const VECTOR4D & rhs): x(rhs.x), y(rhs.y), z(rhs.z), w(rhs.w)
|
---|
| 32 | {}
|
---|
| 33 |
|
---|
| 34 | //convert v3d to v4d
|
---|
| 35 | VECTOR4D(const VECTOR3D & rhs): x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
|
---|
| 36 | {}
|
---|
| 37 |
|
---|
| 38 | ~VECTOR4D() {} //empty
|
---|
| 39 |
|
---|
| 40 | void Set(float newX, float newY, float newZ, float newW)
|
---|
| 41 | { x=newX; y=newY; z=newZ; w=newW; }
|
---|
| 42 |
|
---|
| 43 | //accessors kept for compatability
|
---|
| 44 | void SetX(float newX) {x = newX;}
|
---|
| 45 | void SetY(float newY) {y = newY;}
|
---|
| 46 | void SetZ(float newZ) {z = newZ;}
|
---|
| 47 | void SetW(float newW) {w = newW;}
|
---|
| 48 |
|
---|
| 49 | float GetX() const {return x;} //public accessor functions
|
---|
| 50 | float GetY() const {return y;} //inline, const
|
---|
| 51 | float GetZ() const {return z;}
|
---|
| 52 | float GetW() const {return w;}
|
---|
| 53 |
|
---|
| 54 | void LoadZero(void)
|
---|
| 55 | { x=0.0f; y=0.0f; z=0.0f; w=0.0f; }
|
---|
| 56 |
|
---|
| 57 | void LoadOne(void)
|
---|
| 58 | { x=1.0f; y=1.0f; z=1.0f; w=1.0f; }
|
---|
| 59 |
|
---|
| 60 | //vector algebra
|
---|
| 61 | float DotProduct(const VECTOR4D & rhs)
|
---|
| 62 | { return x*rhs.x + y*rhs.y + z*rhs.z + w*rhs.w; }
|
---|
| 63 |
|
---|
| 64 | //rotations
|
---|
| 65 | void RotateX(double angle);
|
---|
| 66 | VECTOR4D GetRotatedX(double angle) const;
|
---|
| 67 | void RotateY(double angle);
|
---|
| 68 | VECTOR4D GetRotatedY(double angle) const;
|
---|
| 69 | void RotateZ(double angle);
|
---|
| 70 | VECTOR4D GetRotatedZ(double angle) const;
|
---|
| 71 | void RotateAxis(double angle, const VECTOR3D & axis);
|
---|
| 72 | VECTOR4D GetRotatedAxis(double angle, const VECTOR3D & axis) const;
|
---|
| 73 |
|
---|
| 74 | VECTOR4D lerp(const VECTOR4D & v2, float factor) const
|
---|
| 75 | { return (*this)*(1.0f-factor)+v2*factor; }
|
---|
| 76 |
|
---|
| 77 | VECTOR4D QuadraticInterpolate(const VECTOR4D & v2, const VECTOR4D & v3, float factor) const
|
---|
| 78 | { return (*this)*(1.0f-factor)*(1.0f-factor) + 2*v2*factor*(1.0f-factor) + v3*factor*factor;}
|
---|
| 79 |
|
---|
| 80 | //binary operators
|
---|
| 81 | VECTOR4D operator+(const VECTOR4D & rhs) const
|
---|
| 82 | { return VECTOR4D(x+rhs.x, y+rhs.y, z+rhs.z, w+rhs.w); }
|
---|
| 83 |
|
---|
| 84 | VECTOR4D operator-(const VECTOR4D & rhs) const
|
---|
| 85 | { return VECTOR4D(x-rhs.x, y-rhs.y, z-rhs.z, w-rhs.w); }
|
---|
| 86 |
|
---|
| 87 | VECTOR4D operator*(const float rhs) const
|
---|
| 88 | { return VECTOR4D(x*rhs, y*rhs, z*rhs, w*rhs); }
|
---|
| 89 |
|
---|
| 90 | VECTOR4D operator/(const float rhs) const
|
---|
| 91 | { return rhs==0.0f ? VECTOR4D(0.0f, 0.0f, 0.0f, 0.0f)
|
---|
| 92 | : VECTOR4D(x/rhs, y/rhs, z/rhs, w/rhs); }
|
---|
| 93 |
|
---|
| 94 | //multiply by a float, eg 3*v
|
---|
| 95 | friend VECTOR4D operator*(float scaleFactor, const VECTOR4D & rhs);
|
---|
| 96 |
|
---|
| 97 | bool operator==(const VECTOR4D & rhs) const;
|
---|
| 98 | bool operator!=(const VECTOR4D & rhs) const
|
---|
| 99 | { return !((*this)==rhs); }
|
---|
| 100 |
|
---|
| 101 | //self-add etc
|
---|
| 102 | void operator+=(const VECTOR4D & rhs)
|
---|
| 103 | { x+=rhs.x; y+=rhs.y; z+=rhs.z; w+=rhs.w; }
|
---|
| 104 |
|
---|
| 105 | void operator-=(const VECTOR4D & rhs)
|
---|
| 106 | { x-=rhs.x; y-=rhs.y; z-=rhs.z; w-=rhs.w; }
|
---|
| 107 |
|
---|
| 108 | void operator*=(const float rhs)
|
---|
| 109 | { x*=rhs; y*=rhs; z*=rhs; w*=rhs; }
|
---|
| 110 |
|
---|
| 111 | void operator/=(const float rhs)
|
---|
| 112 | { if(rhs==0.0f)
|
---|
| 113 | return;
|
---|
| 114 | else
|
---|
| 115 | { x/=rhs; y/=rhs; z/=rhs; w/=rhs; }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | //unary operators
|
---|
| 119 | VECTOR4D operator-(void) const {return VECTOR4D(-x, -y, -z, -w);}
|
---|
| 120 | VECTOR4D operator+(void) const {return (*this);}
|
---|
| 121 |
|
---|
| 122 | //cast to pointer to float for glVertex4fv etc
|
---|
| 123 | operator float* () const {return (float*) this;}
|
---|
| 124 | operator const float* () const {return (const float*) this;}
|
---|
| 125 |
|
---|
| 126 | operator VECTOR3D(); //convert v4d to v3d
|
---|
| 127 |
|
---|
| 128 | //member variables
|
---|
| 129 | float x;
|
---|
| 130 | float y;
|
---|
| 131 | float z;
|
---|
| 132 | float w;
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | #endif //VECTOR3D_H |
---|