1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
2 | // VECTOR3D.h
|
---|
3 | // Class declaration for a 3d 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 VECTOR3D_H
|
---|
15 | #define VECTOR3D_H
|
---|
16 |
|
---|
17 | class VECTOR3D
|
---|
18 | {
|
---|
19 | public:
|
---|
20 | //constructors
|
---|
21 | VECTOR3D(void) : x(0.0f), y(0.0f), z(0.0f)
|
---|
22 | {}
|
---|
23 |
|
---|
24 | VECTOR3D(float newX, float newY, float newZ) : x(newX), y(newY), z(newZ)
|
---|
25 | {}
|
---|
26 |
|
---|
27 | VECTOR3D(const float * rhs) : x(*rhs), y(*(rhs+1)), z(*(rhs+2))
|
---|
28 | {}
|
---|
29 |
|
---|
30 | VECTOR3D(const VECTOR3D & rhs) : x(rhs.x), y(rhs.y), z(rhs.z)
|
---|
31 | {}
|
---|
32 |
|
---|
33 | ~VECTOR3D() {} //empty
|
---|
34 |
|
---|
35 | void Set(float newX, float newY, float newZ)
|
---|
36 | { x=newX; y=newY; z=newZ; }
|
---|
37 |
|
---|
38 | //Accessors kept for compatibility
|
---|
39 | void SetX(float newX) {x = newX;}
|
---|
40 | void SetY(float newY) {y = newY;}
|
---|
41 | void SetZ(float newZ) {z = newZ;}
|
---|
42 |
|
---|
43 | float GetX() const {return x;} //public accessor functions
|
---|
44 | float GetY() const {return y;} //inline, const
|
---|
45 | float GetZ() const {return z;}
|
---|
46 |
|
---|
47 | void LoadZero(void)
|
---|
48 | { x=y=z=0.0f; }
|
---|
49 | void LoadOne(void)
|
---|
50 | { x=y=z=1.0f; }
|
---|
51 |
|
---|
52 | //vector algebra
|
---|
53 | VECTOR3D CrossProduct(const VECTOR3D & rhs) const
|
---|
54 | { return VECTOR3D(y*rhs.z - z*rhs.y, z*rhs.x - x*rhs.z, x*rhs.y - y*rhs.x); }
|
---|
55 |
|
---|
56 | float DotProduct(const VECTOR3D & rhs) const
|
---|
57 | { return x*rhs.x + y*rhs.y + z*rhs.z; }
|
---|
58 |
|
---|
59 | void Normalize();
|
---|
60 | VECTOR3D GetNormalized() const;
|
---|
61 |
|
---|
62 | float GetLength() const
|
---|
63 | { return (float)sqrt((x*x)+(y*y)+(z*z)); }
|
---|
64 |
|
---|
65 | float GetSquaredLength() const
|
---|
66 | { return (x*x)+(y*y)+(z*z); }
|
---|
67 |
|
---|
68 | //rotations
|
---|
69 | void RotateX(double angle);
|
---|
70 | VECTOR3D GetRotatedX(double angle) const;
|
---|
71 | void RotateY(double angle);
|
---|
72 | VECTOR3D GetRotatedY(double angle) const;
|
---|
73 | void RotateZ(double angle);
|
---|
74 | VECTOR3D GetRotatedZ(double angle) const;
|
---|
75 | void RotateAxis(double angle, const VECTOR3D & axis);
|
---|
76 | VECTOR3D GetRotatedAxis(double angle, const VECTOR3D & axis) const;
|
---|
77 |
|
---|
78 | //pack to [0,1] for color
|
---|
79 | void PackTo01();
|
---|
80 | VECTOR3D GetPackedTo01() const;
|
---|
81 |
|
---|
82 | //linear interpolate
|
---|
83 | VECTOR3D lerp(const VECTOR3D & v2, float factor) const
|
---|
84 | { return (*this)*(1.0f-factor) + v2*factor; }
|
---|
85 |
|
---|
86 | VECTOR3D QuadraticInterpolate(const VECTOR3D & v2, const VECTOR3D & v3, float factor) const
|
---|
87 | { return (*this)*(1.0f-factor)*(1.0f-factor) + 2*v2*factor*(1.0f-factor) + v3*factor*factor;}
|
---|
88 |
|
---|
89 |
|
---|
90 | //overloaded operators
|
---|
91 | //binary operators
|
---|
92 | VECTOR3D operator+(const VECTOR3D & rhs) const
|
---|
93 | { return VECTOR3D(x + rhs.x, y + rhs.y, z + rhs.z); }
|
---|
94 |
|
---|
95 | VECTOR3D operator-(const VECTOR3D & rhs) const
|
---|
96 | { return VECTOR3D(x - rhs.x, y - rhs.y, z - rhs.z); }
|
---|
97 |
|
---|
98 | VECTOR3D operator*(const float rhs) const
|
---|
99 | { return VECTOR3D(x*rhs, y*rhs, z*rhs); }
|
---|
100 |
|
---|
101 | VECTOR3D operator/(const float rhs) const
|
---|
102 | { return (rhs==0.0f) ? VECTOR3D(0.0f, 0.0f, 0.0f) : VECTOR3D(x / rhs, y / rhs, z / rhs); }
|
---|
103 |
|
---|
104 | //multiply by a float, eg 3*v
|
---|
105 | friend VECTOR3D operator*(float scaleFactor, const VECTOR3D & rhs);
|
---|
106 |
|
---|
107 | //Add, subtract etc, saving the construction of a temporary
|
---|
108 | void Add(const VECTOR3D & v2, VECTOR3D & result)
|
---|
109 | {
|
---|
110 | result.x=x+v2.x;
|
---|
111 | result.y=y+v2.y;
|
---|
112 | result.z=z+v2.z;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void Subtract(const VECTOR3D & v2, VECTOR3D & result)
|
---|
116 | {
|
---|
117 | result.x=x-v2.x;
|
---|
118 | result.y=y-v2.y;
|
---|
119 | result.z=z-v2.z;
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool operator==(const VECTOR3D & rhs) const;
|
---|
123 | bool operator!=(const VECTOR3D & rhs) const
|
---|
124 | { return !((*this)==rhs); }
|
---|
125 |
|
---|
126 | //self-add etc
|
---|
127 | void operator+=(const VECTOR3D & rhs)
|
---|
128 | { x+=rhs.x; y+=rhs.y; z+=rhs.z; }
|
---|
129 |
|
---|
130 | void operator-=(const VECTOR3D & rhs)
|
---|
131 | { x-=rhs.x; y-=rhs.y; z-=rhs.z; }
|
---|
132 |
|
---|
133 | void operator*=(const float rhs)
|
---|
134 | { x*=rhs; y*=rhs; z*=rhs; }
|
---|
135 |
|
---|
136 | void operator/=(const float rhs)
|
---|
137 | { if(rhs==0.0f)
|
---|
138 | return;
|
---|
139 | else
|
---|
140 | { x/=rhs; y/=rhs; z/=rhs; }
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | //unary operators
|
---|
145 | VECTOR3D operator-(void) const {return VECTOR3D(-x, -y, -z);}
|
---|
146 | VECTOR3D operator+(void) const {return *this;}
|
---|
147 |
|
---|
148 | //cast to pointer to a (float *) for glVertex3fv etc
|
---|
149 | operator float* () const {return (float*) this;}
|
---|
150 | operator const float* () const {return (const float*) this;}
|
---|
151 |
|
---|
152 | //member variables
|
---|
153 | float x;
|
---|
154 | float y;
|
---|
155 | float z;
|
---|
156 | };
|
---|
157 |
|
---|
158 | #endif //VECTOR3D_H |
---|