1 | #ifndef _VECTOR3_H_
|
---|
2 | #define _VECTOR3_H_
|
---|
3 |
|
---|
4 | #include "math.h"
|
---|
5 |
|
---|
6 | class Vector3
|
---|
7 | {
|
---|
8 | public:
|
---|
9 | float x, y, z;
|
---|
10 |
|
---|
11 | inline Vector3() {}
|
---|
12 |
|
---|
13 | inline Vector3( float nx, float ny, float nz ) : x(nx), y(ny), z(nz) {}
|
---|
14 |
|
---|
15 | inline Vector3( float v[3] ) : x(v[0]), y(v[1]), z(v[2]) {}
|
---|
16 |
|
---|
17 | inline Vector3( int v[3] ): x((float)v[0]), y((float)v[1]), z((float)v[2]) {}
|
---|
18 |
|
---|
19 | inline Vector3( const float* const v ) : x(v[0]), y(v[1]), z(v[2]) {}
|
---|
20 |
|
---|
21 | inline Vector3( const Vector3& v ) : x(v.x), y(v.y), z(v.z) {}
|
---|
22 |
|
---|
23 | inline float operator [] ( unsigned i ) const
|
---|
24 | {
|
---|
25 | return *(&x+i);
|
---|
26 | }
|
---|
27 |
|
---|
28 | inline float& operator [] ( unsigned i )
|
---|
29 | {
|
---|
30 | return *(&x+i);
|
---|
31 | }
|
---|
32 |
|
---|
33 | inline Vector3& operator = ( const Vector3& v )
|
---|
34 | {
|
---|
35 | x = v.x;
|
---|
36 | y = v.y;
|
---|
37 | z = v.z;
|
---|
38 |
|
---|
39 | return *this;
|
---|
40 | }
|
---|
41 |
|
---|
42 | inline bool operator == ( const Vector3& v ) const
|
---|
43 | {
|
---|
44 | return ( x == v.x && y == v.y && z == v.z );
|
---|
45 | }
|
---|
46 |
|
---|
47 | inline bool operator != ( const Vector3& v ) const
|
---|
48 | {
|
---|
49 | return ( x != v.x || y != v.y || z != v.z );
|
---|
50 | }
|
---|
51 |
|
---|
52 | // arithmetic operations
|
---|
53 | inline Vector3 operator + ( const Vector3& v ) const
|
---|
54 | {
|
---|
55 | return Vector3(x + v.x, y + v.y, z + v.z);
|
---|
56 | }
|
---|
57 |
|
---|
58 | inline Vector3 operator - ( const Vector3& v ) const
|
---|
59 | {
|
---|
60 | return Vector3(x - v.x, y - v.y, z - v.z);
|
---|
61 | }
|
---|
62 |
|
---|
63 | inline Vector3 operator * ( float f ) const
|
---|
64 | {
|
---|
65 | return Vector3(x * f, y * f, z * f);
|
---|
66 | }
|
---|
67 |
|
---|
68 | inline Vector3 operator * ( const Vector3& v) const
|
---|
69 | {
|
---|
70 | return Vector3(x * v.x, y * v.y, z * v.z);
|
---|
71 | }
|
---|
72 |
|
---|
73 | inline Vector3 operator / ( float f ) const
|
---|
74 | {
|
---|
75 | f = 1.0f / f;
|
---|
76 | return Vector3(x * f, y * f, z * f);
|
---|
77 | }
|
---|
78 |
|
---|
79 | inline Vector3 operator - () const
|
---|
80 | {
|
---|
81 | return Vector3( -x, -y, -z);
|
---|
82 | }
|
---|
83 |
|
---|
84 | inline friend Vector3 operator * ( float f, const Vector3& v )
|
---|
85 | {
|
---|
86 | return Vector3(f * v.x, f * v.y, f * v.z);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // arithmetic updates
|
---|
90 | inline Vector3& operator += ( const Vector3& v )
|
---|
91 | {
|
---|
92 | x += v.x;
|
---|
93 | y += v.y;
|
---|
94 | z += v.z;
|
---|
95 |
|
---|
96 | return *this;
|
---|
97 | }
|
---|
98 |
|
---|
99 | inline Vector3& operator -= ( const Vector3& v )
|
---|
100 | {
|
---|
101 | x -= v.x;
|
---|
102 | y -= v.y;
|
---|
103 | z -= v.z;
|
---|
104 |
|
---|
105 | return *this;
|
---|
106 | }
|
---|
107 |
|
---|
108 | inline Vector3& operator *= ( float f )
|
---|
109 | {
|
---|
110 | x *= f;
|
---|
111 | y *= f;
|
---|
112 | z *= f;
|
---|
113 | return *this;
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline Vector3& operator /= ( float f )
|
---|
117 | {
|
---|
118 | f = 1.0f / f;
|
---|
119 |
|
---|
120 | x *= f;
|
---|
121 | y *= f;
|
---|
122 | z *= f;
|
---|
123 |
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inline float length () const
|
---|
128 | {
|
---|
129 | return (float)sqrt( x * x + y * y + z * z );
|
---|
130 | }
|
---|
131 |
|
---|
132 | inline float squaredLength () const
|
---|
133 | {
|
---|
134 | return x * x + y * y + z * z;
|
---|
135 | }
|
---|
136 |
|
---|
137 | inline float dotProduct(const Vector3& v) const
|
---|
138 | {
|
---|
139 | return x * v.x + y * v.y + z * v.z;
|
---|
140 | }
|
---|
141 |
|
---|
142 | inline Vector3 & normalise()
|
---|
143 | {
|
---|
144 | float f = (float)sqrt( x * x + y * y + z * z );
|
---|
145 |
|
---|
146 | // Will also work for zero-sized vectors, but will change nothing
|
---|
147 | if ( f > 1e-06f )
|
---|
148 | {
|
---|
149 | f = 1.0f / f;
|
---|
150 | x *= f;
|
---|
151 | y *= f;
|
---|
152 | z *= f;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return *this;
|
---|
156 | }
|
---|
157 |
|
---|
158 | inline Vector3 crossProduct( const Vector3& v ) const
|
---|
159 | {
|
---|
160 | return Vector3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
|
---|
161 | }
|
---|
162 |
|
---|
163 | inline Vector3 midPoint( const Vector3& v ) const
|
---|
164 | {
|
---|
165 | return Vector3( ( x + v.x ) * 0.5f, ( y + v.y ) * 0.5f, ( z + v.z ) * 0.5f );
|
---|
166 | }
|
---|
167 |
|
---|
168 | inline bool operator < ( const Vector3& v ) const
|
---|
169 | {
|
---|
170 | return ( x < v.x && y < v.y && z < v.z );
|
---|
171 | }
|
---|
172 |
|
---|
173 | inline bool operator > ( const Vector3& v ) const
|
---|
174 | {
|
---|
175 | return ( x > v.x && y > v.y && z > v.z );
|
---|
176 | }
|
---|
177 |
|
---|
178 | inline void makeFloor( const Vector3& v )
|
---|
179 | {
|
---|
180 | if( v.x < x ) x = v.x;
|
---|
181 | if( v.y < y ) y = v.y;
|
---|
182 | if( v.z < z ) z = v.z;
|
---|
183 | }
|
---|
184 |
|
---|
185 | inline void makeCeil( const Vector3& v )
|
---|
186 | {
|
---|
187 | if( v.x > x ) x = v.x;
|
---|
188 | if( v.y > y ) y = v.y;
|
---|
189 | if( v.z > z ) z = v.z;
|
---|
190 | }
|
---|
191 |
|
---|
192 | inline Vector3 perpendicular(void)
|
---|
193 | {
|
---|
194 | static float fSquareZero = 1e-06f * 1e-06f;
|
---|
195 |
|
---|
196 | Vector3 perp = this->crossProduct( Vector3::UNIT_X );
|
---|
197 |
|
---|
198 | // Check length
|
---|
199 | if( perp.squaredLength() < fSquareZero )
|
---|
200 | {
|
---|
201 | /* This vector is the Y axis multiplied by a scalar, so we have
|
---|
202 | to use another axis.
|
---|
203 | */
|
---|
204 | perp = this->crossProduct( Vector3::UNIT_Y );
|
---|
205 | }
|
---|
206 |
|
---|
207 | return perp;
|
---|
208 | }
|
---|
209 |
|
---|
210 | // special points
|
---|
211 | static const Vector3 ZERO;
|
---|
212 | static const Vector3 UNIT_X;
|
---|
213 | static const Vector3 UNIT_Y;
|
---|
214 | static const Vector3 UNIT_Z;
|
---|
215 | static const Vector3 UNIT_SCALE;
|
---|
216 | };
|
---|
217 |
|
---|
218 | #endif // _VECTOR3_H_
|
---|
219 |
|
---|