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