1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #ifndef __Vector4_H__
|
---|
26 | #define __Vector4_H__
|
---|
27 |
|
---|
28 | #include "OgrePrerequisites.h"
|
---|
29 | #include "OgreVector3.h"
|
---|
30 |
|
---|
31 | namespace Ogre
|
---|
32 | {
|
---|
33 |
|
---|
34 | /** 4-dimensional homogenous vector.
|
---|
35 | */
|
---|
36 | class _OgreExport Vector4
|
---|
37 | {
|
---|
38 | public:
|
---|
39 | union {
|
---|
40 | struct {
|
---|
41 | Real x, y, z, w;
|
---|
42 | };
|
---|
43 | Real val[4];
|
---|
44 | };
|
---|
45 |
|
---|
46 | public:
|
---|
47 | inline Vector4()
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW )
|
---|
52 | : x( fX ), y( fY ), z( fZ ), w( fW)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | inline explicit Vector4( const Real afCoordinate[4] )
|
---|
57 | : x( afCoordinate[0] ),
|
---|
58 | y( afCoordinate[1] ),
|
---|
59 | z( afCoordinate[2] ),
|
---|
60 | w( afCoordinate[3] )
|
---|
61 | {
|
---|
62 | }
|
---|
63 |
|
---|
64 | inline explicit Vector4( const int afCoordinate[4] )
|
---|
65 | {
|
---|
66 | x = (Real)afCoordinate[0];
|
---|
67 | y = (Real)afCoordinate[1];
|
---|
68 | z = (Real)afCoordinate[2];
|
---|
69 | w = (Real)afCoordinate[3];
|
---|
70 | }
|
---|
71 |
|
---|
72 | inline explicit Vector4( Real* const r )
|
---|
73 | : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
|
---|
74 | {
|
---|
75 | }
|
---|
76 |
|
---|
77 | inline explicit Vector4( const Real scaler )
|
---|
78 | : x( scaler )
|
---|
79 | , y( scaler )
|
---|
80 | , z( scaler )
|
---|
81 | , w( scaler )
|
---|
82 | {
|
---|
83 | }
|
---|
84 |
|
---|
85 | inline explicit Vector4(const Vector3& rhs)
|
---|
86 | : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | inline Vector4( const Vector4& rkVector )
|
---|
91 | : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w)
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | inline Real operator [] ( const size_t i ) const
|
---|
96 | {
|
---|
97 | assert( i < 4 );
|
---|
98 |
|
---|
99 | return *(&x+i);
|
---|
100 | }
|
---|
101 |
|
---|
102 | inline Real& operator [] ( const size_t i )
|
---|
103 | {
|
---|
104 | assert( i < 4 );
|
---|
105 |
|
---|
106 | return *(&x+i);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Assigns the value of the other vector.
|
---|
110 | @param
|
---|
111 | rkVector The other vector
|
---|
112 | */
|
---|
113 | inline Vector4& operator = ( const Vector4& rkVector )
|
---|
114 | {
|
---|
115 | x = rkVector.x;
|
---|
116 | y = rkVector.y;
|
---|
117 | z = rkVector.z;
|
---|
118 | w = rkVector.w;
|
---|
119 |
|
---|
120 | return *this;
|
---|
121 | }
|
---|
122 |
|
---|
123 | inline Vector4& operator = ( const Real fScalar)
|
---|
124 | {
|
---|
125 | x = fScalar;
|
---|
126 | y = fScalar;
|
---|
127 | z = fScalar;
|
---|
128 | w = fScalar;
|
---|
129 | return *this;
|
---|
130 | }
|
---|
131 |
|
---|
132 | inline bool operator == ( const Vector4& rkVector ) const
|
---|
133 | {
|
---|
134 | return ( x == rkVector.x &&
|
---|
135 | y == rkVector.y &&
|
---|
136 | z == rkVector.z &&
|
---|
137 | w == rkVector.w );
|
---|
138 | }
|
---|
139 |
|
---|
140 | inline bool operator != ( const Vector4& rkVector ) const
|
---|
141 | {
|
---|
142 | return ( x != rkVector.x ||
|
---|
143 | y != rkVector.y ||
|
---|
144 | z != rkVector.z ||
|
---|
145 | w != rkVector.w );
|
---|
146 | }
|
---|
147 |
|
---|
148 | inline Vector4& operator = (const Vector3& rhs)
|
---|
149 | {
|
---|
150 | x = rhs.x;
|
---|
151 | y = rhs.y;
|
---|
152 | z = rhs.z;
|
---|
153 | w = 1.0f;
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // arithmetic operations
|
---|
158 | inline Vector4 operator + ( const Vector4& rkVector ) const
|
---|
159 | {
|
---|
160 | return Vector4(
|
---|
161 | x + rkVector.x,
|
---|
162 | y + rkVector.y,
|
---|
163 | z + rkVector.z,
|
---|
164 | w + rkVector.w);
|
---|
165 | }
|
---|
166 |
|
---|
167 | inline Vector4 operator - ( const Vector4& rkVector ) const
|
---|
168 | {
|
---|
169 | return Vector4(
|
---|
170 | x - rkVector.x,
|
---|
171 | y - rkVector.y,
|
---|
172 | z - rkVector.z,
|
---|
173 | w - rkVector.w);
|
---|
174 | }
|
---|
175 |
|
---|
176 | inline Vector4 operator * ( const Real fScalar ) const
|
---|
177 | {
|
---|
178 | return Vector4(
|
---|
179 | x * fScalar,
|
---|
180 | y * fScalar,
|
---|
181 | z * fScalar,
|
---|
182 | w * fScalar);
|
---|
183 | }
|
---|
184 |
|
---|
185 | inline Vector4 operator * ( const Vector4& rhs) const
|
---|
186 | {
|
---|
187 | return Vector4(
|
---|
188 | rhs.x * x,
|
---|
189 | rhs.y * y,
|
---|
190 | rhs.z * z,
|
---|
191 | rhs.w * w);
|
---|
192 | }
|
---|
193 |
|
---|
194 | inline Vector4 operator / ( const Real fScalar ) const
|
---|
195 | {
|
---|
196 | assert( fScalar != 0.0 );
|
---|
197 |
|
---|
198 | Real fInv = 1.0 / fScalar;
|
---|
199 |
|
---|
200 | return Vector4(
|
---|
201 | x * fInv,
|
---|
202 | y * fInv,
|
---|
203 | z * fInv,
|
---|
204 | w * fInv);
|
---|
205 | }
|
---|
206 |
|
---|
207 | inline Vector4 operator / ( const Vector4& rhs) const
|
---|
208 | {
|
---|
209 | return Vector4(
|
---|
210 | x / rhs.x,
|
---|
211 | y / rhs.y,
|
---|
212 | z / rhs.z,
|
---|
213 | w / rhs.w);
|
---|
214 | }
|
---|
215 |
|
---|
216 | inline const Vector4& operator + () const
|
---|
217 | {
|
---|
218 | return *this;
|
---|
219 | }
|
---|
220 |
|
---|
221 | inline Vector4 operator - () const
|
---|
222 | {
|
---|
223 | return Vector4(-x, -y, -z, -w);
|
---|
224 | }
|
---|
225 |
|
---|
226 | inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
|
---|
227 | {
|
---|
228 | return Vector4(
|
---|
229 | fScalar * rkVector.x,
|
---|
230 | fScalar * rkVector.y,
|
---|
231 | fScalar * rkVector.z,
|
---|
232 | fScalar * rkVector.w);
|
---|
233 | }
|
---|
234 |
|
---|
235 | inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector )
|
---|
236 | {
|
---|
237 | return Vector4(
|
---|
238 | fScalar / rkVector.x,
|
---|
239 | fScalar / rkVector.y,
|
---|
240 | fScalar / rkVector.z,
|
---|
241 | fScalar / rkVector.w);
|
---|
242 | }
|
---|
243 |
|
---|
244 | inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
|
---|
245 | {
|
---|
246 | return Vector4(
|
---|
247 | lhs.x + rhs,
|
---|
248 | lhs.y + rhs,
|
---|
249 | lhs.z + rhs,
|
---|
250 | lhs.w + rhs);
|
---|
251 | }
|
---|
252 |
|
---|
253 | inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
|
---|
254 | {
|
---|
255 | return Vector4(
|
---|
256 | lhs + rhs.x,
|
---|
257 | lhs + rhs.y,
|
---|
258 | lhs + rhs.z,
|
---|
259 | lhs + rhs.w);
|
---|
260 | }
|
---|
261 |
|
---|
262 | inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
|
---|
263 | {
|
---|
264 | return Vector4(
|
---|
265 | lhs.x - rhs,
|
---|
266 | lhs.y - rhs,
|
---|
267 | lhs.z - rhs,
|
---|
268 | lhs.w - rhs);
|
---|
269 | }
|
---|
270 |
|
---|
271 | inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
|
---|
272 | {
|
---|
273 | return Vector4(
|
---|
274 | lhs - rhs.x,
|
---|
275 | lhs - rhs.y,
|
---|
276 | lhs - rhs.z,
|
---|
277 | lhs - rhs.w);
|
---|
278 | }
|
---|
279 |
|
---|
280 | // arithmetic updates
|
---|
281 | inline Vector4& operator += ( const Vector4& rkVector )
|
---|
282 | {
|
---|
283 | x += rkVector.x;
|
---|
284 | y += rkVector.y;
|
---|
285 | z += rkVector.z;
|
---|
286 | w += rkVector.w;
|
---|
287 |
|
---|
288 | return *this;
|
---|
289 | }
|
---|
290 |
|
---|
291 | inline Vector4& operator -= ( const Vector4& rkVector )
|
---|
292 | {
|
---|
293 | x -= rkVector.x;
|
---|
294 | y -= rkVector.y;
|
---|
295 | z -= rkVector.z;
|
---|
296 | w -= rkVector.w;
|
---|
297 |
|
---|
298 | return *this;
|
---|
299 | }
|
---|
300 |
|
---|
301 | inline Vector4& operator *= ( const Real fScalar )
|
---|
302 | {
|
---|
303 | x *= fScalar;
|
---|
304 | y *= fScalar;
|
---|
305 | z *= fScalar;
|
---|
306 | w *= fScalar;
|
---|
307 | return *this;
|
---|
308 | }
|
---|
309 |
|
---|
310 | inline Vector4& operator += ( const Real fScalar )
|
---|
311 | {
|
---|
312 | x += fScalar;
|
---|
313 | y += fScalar;
|
---|
314 | z += fScalar;
|
---|
315 | w += fScalar;
|
---|
316 | return *this;
|
---|
317 | }
|
---|
318 |
|
---|
319 | inline Vector4& operator -= ( const Real fScalar )
|
---|
320 | {
|
---|
321 | x -= fScalar;
|
---|
322 | y -= fScalar;
|
---|
323 | z -= fScalar;
|
---|
324 | w -= fScalar;
|
---|
325 | return *this;
|
---|
326 | }
|
---|
327 |
|
---|
328 | inline Vector4& operator *= ( const Vector4& rkVector )
|
---|
329 | {
|
---|
330 | x *= rkVector.x;
|
---|
331 | y *= rkVector.y;
|
---|
332 | z *= rkVector.z;
|
---|
333 | w *= rkVector.w;
|
---|
334 |
|
---|
335 | return *this;
|
---|
336 | }
|
---|
337 |
|
---|
338 | inline Vector4& operator /= ( const Real fScalar )
|
---|
339 | {
|
---|
340 | assert( fScalar != 0.0 );
|
---|
341 |
|
---|
342 | Real fInv = 1.0 / fScalar;
|
---|
343 |
|
---|
344 | x *= fInv;
|
---|
345 | y *= fInv;
|
---|
346 | z *= fInv;
|
---|
347 | w *= fInv;
|
---|
348 |
|
---|
349 | return *this;
|
---|
350 | }
|
---|
351 |
|
---|
352 | inline Vector4& operator /= ( const Vector4& rkVector )
|
---|
353 | {
|
---|
354 | x /= rkVector.x;
|
---|
355 | y /= rkVector.y;
|
---|
356 | z /= rkVector.z;
|
---|
357 | w /= rkVector.w;
|
---|
358 |
|
---|
359 | return *this;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** Calculates the dot (scalar) product of this vector with another.
|
---|
363 | @param
|
---|
364 | vec Vector with which to calculate the dot product (together
|
---|
365 | with this one).
|
---|
366 | @returns
|
---|
367 | A float representing the dot product value.
|
---|
368 | */
|
---|
369 | inline Real dotProduct(const Vector4& vec) const
|
---|
370 | {
|
---|
371 | return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
|
---|
372 | }
|
---|
373 | /** Function for writing to a stream.
|
---|
374 | */
|
---|
375 | inline _OgreExport friend std::ostream& operator <<
|
---|
376 | ( std::ostream& o, const Vector4& v )
|
---|
377 | {
|
---|
378 | o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
|
---|
379 | return o;
|
---|
380 | }
|
---|
381 | // special
|
---|
382 | static const Vector4 ZERO;
|
---|
383 | };
|
---|
384 |
|
---|
385 | }
|
---|
386 | #endif
|
---|