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 Vector4( const Vector4& rkVector )
|
---|
86 | : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | inline Real operator [] ( const size_t i ) const
|
---|
91 | {
|
---|
92 | assert( i < 4 );
|
---|
93 |
|
---|
94 | return *(&x+i);
|
---|
95 | }
|
---|
96 |
|
---|
97 | inline Real& operator [] ( const size_t i )
|
---|
98 | {
|
---|
99 | assert( i < 4 );
|
---|
100 |
|
---|
101 | return *(&x+i);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Assigns the value of the other vector.
|
---|
105 | @param
|
---|
106 | rkVector The other vector
|
---|
107 | */
|
---|
108 | inline Vector4& operator = ( const Vector4& rkVector )
|
---|
109 | {
|
---|
110 | x = rkVector.x;
|
---|
111 | y = rkVector.y;
|
---|
112 | z = rkVector.z;
|
---|
113 | w = rkVector.w;
|
---|
114 |
|
---|
115 | return *this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | inline Vector4& operator = ( const Real fScalar)
|
---|
119 | {
|
---|
120 | x = fScalar;
|
---|
121 | y = fScalar;
|
---|
122 | z = fScalar;
|
---|
123 | w = fScalar;
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inline bool operator == ( const Vector4& rkVector ) const
|
---|
128 | {
|
---|
129 | return ( x == rkVector.x &&
|
---|
130 | y == rkVector.y &&
|
---|
131 | z == rkVector.z &&
|
---|
132 | w == rkVector.w );
|
---|
133 | }
|
---|
134 |
|
---|
135 | inline bool operator != ( const Vector4& rkVector ) const
|
---|
136 | {
|
---|
137 | return ( x != rkVector.x ||
|
---|
138 | y != rkVector.y ||
|
---|
139 | z != rkVector.z ||
|
---|
140 | w != rkVector.w );
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline Vector4& operator = (const Vector3& rhs)
|
---|
144 | {
|
---|
145 | x = rhs.x;
|
---|
146 | y = rhs.y;
|
---|
147 | z = rhs.z;
|
---|
148 | w = 1.0f;
|
---|
149 | return *this;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // arithmetic operations
|
---|
153 | inline Vector4 operator + ( const Vector4& rkVector ) const
|
---|
154 | {
|
---|
155 | Vector4 kSum;
|
---|
156 |
|
---|
157 | kSum.x = x + rkVector.x;
|
---|
158 | kSum.y = y + rkVector.y;
|
---|
159 | kSum.z = z + rkVector.z;
|
---|
160 | kSum.w = w + rkVector.w;
|
---|
161 |
|
---|
162 | return kSum;
|
---|
163 | }
|
---|
164 |
|
---|
165 | inline Vector4 operator - ( const Vector4& rkVector ) const
|
---|
166 | {
|
---|
167 | Vector4 kDiff;
|
---|
168 |
|
---|
169 | kDiff.x = x - rkVector.x;
|
---|
170 | kDiff.y = y - rkVector.y;
|
---|
171 | kDiff.z = z - rkVector.z;
|
---|
172 | kDiff.w = w - rkVector.w;
|
---|
173 |
|
---|
174 | return kDiff;
|
---|
175 | }
|
---|
176 |
|
---|
177 | inline Vector4 operator * ( const Real fScalar ) const
|
---|
178 | {
|
---|
179 | Vector4 kProd;
|
---|
180 |
|
---|
181 | kProd.x = fScalar*x;
|
---|
182 | kProd.y = fScalar*y;
|
---|
183 | kProd.z = fScalar*z;
|
---|
184 | kProd.w = fScalar*w;
|
---|
185 |
|
---|
186 | return kProd;
|
---|
187 | }
|
---|
188 |
|
---|
189 | inline Vector4 operator * ( const Vector4& rhs) const
|
---|
190 | {
|
---|
191 | Vector4 kProd;
|
---|
192 |
|
---|
193 | kProd.x = rhs.x * x;
|
---|
194 | kProd.y = rhs.y * y;
|
---|
195 | kProd.z = rhs.z * z;
|
---|
196 | kProd.w = rhs.w * w;
|
---|
197 |
|
---|
198 | return kProd;
|
---|
199 | }
|
---|
200 |
|
---|
201 | inline Vector4 operator / ( const Real fScalar ) const
|
---|
202 | {
|
---|
203 | assert( fScalar != 0.0 );
|
---|
204 |
|
---|
205 | Vector4 kDiv;
|
---|
206 |
|
---|
207 | Real fInv = 1.0 / fScalar;
|
---|
208 | kDiv.x = x * fInv;
|
---|
209 | kDiv.y = y * fInv;
|
---|
210 | kDiv.z = z * fInv;
|
---|
211 | kDiv.w = w * fInv;
|
---|
212 |
|
---|
213 | return kDiv;
|
---|
214 | }
|
---|
215 |
|
---|
216 | inline Vector4 operator / ( const Vector4& rhs) const
|
---|
217 | {
|
---|
218 | Vector4 kDiv;
|
---|
219 |
|
---|
220 | kDiv.x = x / rhs.x;
|
---|
221 | kDiv.y = y / rhs.y;
|
---|
222 | kDiv.z = z / rhs.z;
|
---|
223 | kDiv.w = w / rhs.w;
|
---|
224 |
|
---|
225 | return kDiv;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | inline Vector4 operator - () const
|
---|
230 | {
|
---|
231 | Vector4 kNeg;
|
---|
232 |
|
---|
233 | kNeg.x = -x;
|
---|
234 | kNeg.y = -y;
|
---|
235 | kNeg.z = -z;
|
---|
236 | kNeg.w = -w;
|
---|
237 |
|
---|
238 | return kNeg;
|
---|
239 | }
|
---|
240 |
|
---|
241 | inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
|
---|
242 | {
|
---|
243 | Vector4 kProd;
|
---|
244 |
|
---|
245 | kProd.x = fScalar * rkVector.x;
|
---|
246 | kProd.y = fScalar * rkVector.y;
|
---|
247 | kProd.z = fScalar * rkVector.z;
|
---|
248 | kProd.w = fScalar * rkVector.w;
|
---|
249 |
|
---|
250 | return kProd;
|
---|
251 | }
|
---|
252 |
|
---|
253 | inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
|
---|
254 | {
|
---|
255 | Vector4 ret;
|
---|
256 | ret = rhs;
|
---|
257 | return ret += lhs;
|
---|
258 | }
|
---|
259 |
|
---|
260 | inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
|
---|
261 | {
|
---|
262 | Vector4 ret;
|
---|
263 | ret = lhs;
|
---|
264 | return ret += rhs;
|
---|
265 | }
|
---|
266 |
|
---|
267 | inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
|
---|
268 | {
|
---|
269 | Vector4 ret;
|
---|
270 | ret = lhs;
|
---|
271 | return ret -= rhs;
|
---|
272 | }
|
---|
273 |
|
---|
274 | inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
|
---|
275 | {
|
---|
276 | Vector4 ret;
|
---|
277 | ret = lhs;
|
---|
278 | return ret -= rhs;
|
---|
279 | }
|
---|
280 |
|
---|
281 | // arithmetic updates
|
---|
282 | inline Vector4& operator += ( const Vector4& rkVector )
|
---|
283 | {
|
---|
284 | x += rkVector.x;
|
---|
285 | y += rkVector.y;
|
---|
286 | z += rkVector.z;
|
---|
287 | w += rkVector.w;
|
---|
288 |
|
---|
289 | return *this;
|
---|
290 | }
|
---|
291 |
|
---|
292 | inline Vector4& operator -= ( const Vector4& rkVector )
|
---|
293 | {
|
---|
294 | x -= rkVector.x;
|
---|
295 | y -= rkVector.y;
|
---|
296 | z -= rkVector.z;
|
---|
297 | w -= rkVector.w;
|
---|
298 |
|
---|
299 | return *this;
|
---|
300 | }
|
---|
301 |
|
---|
302 | inline Vector4& operator *= ( const Real fScalar )
|
---|
303 | {
|
---|
304 | x *= fScalar;
|
---|
305 | y *= fScalar;
|
---|
306 | z *= fScalar;
|
---|
307 | w *= fScalar;
|
---|
308 | return *this;
|
---|
309 | }
|
---|
310 |
|
---|
311 | inline Vector4& operator += ( const Real fScalar )
|
---|
312 | {
|
---|
313 | x += fScalar;
|
---|
314 | y += fScalar;
|
---|
315 | z += fScalar;
|
---|
316 | w += fScalar;
|
---|
317 | return *this;
|
---|
318 | }
|
---|
319 |
|
---|
320 | inline Vector4& operator -= ( const Real fScalar )
|
---|
321 | {
|
---|
322 | x -= fScalar;
|
---|
323 | y -= fScalar;
|
---|
324 | z -= fScalar;
|
---|
325 | w -= fScalar;
|
---|
326 | return *this;
|
---|
327 | }
|
---|
328 |
|
---|
329 | inline Vector4& operator *= ( const Vector4& rkVector )
|
---|
330 | {
|
---|
331 | x *= rkVector.x;
|
---|
332 | y *= rkVector.y;
|
---|
333 | z *= rkVector.z;
|
---|
334 | w *= rkVector.w;
|
---|
335 |
|
---|
336 | return *this;
|
---|
337 | }
|
---|
338 |
|
---|
339 | inline Vector4& operator /= ( const Real fScalar )
|
---|
340 | {
|
---|
341 | assert( fScalar != 0.0 );
|
---|
342 |
|
---|
343 | Real fInv = 1.0 / fScalar;
|
---|
344 |
|
---|
345 | x *= fInv;
|
---|
346 | y *= fInv;
|
---|
347 | z *= fInv;
|
---|
348 | w *= fInv;
|
---|
349 |
|
---|
350 | return *this;
|
---|
351 | }
|
---|
352 |
|
---|
353 | inline Vector4& operator /= ( const Vector4& rkVector )
|
---|
354 | {
|
---|
355 | x /= rkVector.x;
|
---|
356 | y /= rkVector.y;
|
---|
357 | z /= rkVector.z;
|
---|
358 | w /= rkVector.w;
|
---|
359 |
|
---|
360 | return *this;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Calculates the dot (scalar) product of this vector with another.
|
---|
364 | @param
|
---|
365 | vec Vector with which to calculate the dot product (together
|
---|
366 | with this one).
|
---|
367 | @returns
|
---|
368 | A float representing the dot product value.
|
---|
369 | */
|
---|
370 | inline Real dotProduct(const Vector4& vec) const
|
---|
371 | {
|
---|
372 | return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
|
---|
373 | }
|
---|
374 | /** Function for writing to a stream.
|
---|
375 | */
|
---|
376 | inline _OgreExport friend std::ostream& operator <<
|
---|
377 | ( std::ostream& o, const Vector4& v )
|
---|
378 | {
|
---|
379 | o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
|
---|
380 | return o;
|
---|
381 | }
|
---|
382 | // special
|
---|
383 | static const Vector4 ZERO;
|
---|
384 | };
|
---|
385 |
|
---|
386 | }
|
---|
387 | #endif
|
---|