source: OGRE/trunk/ogrenew/OgreMain/include/OgreVector4.h @ 657

Revision 657, 8.2 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://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
31namespace 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( Real fX, Real fY, Real fZ, Real fW )
52            : x( fX ), y( fY ), z( fZ ), w( fW)
53        {
54        }
55
56        inline Vector4( Real afCoordinate[4] )
57            : x( afCoordinate[0] ),
58              y( afCoordinate[1] ),
59              z( afCoordinate[2] ),
60              w (afCoordinate[3] )
61        {
62        }
63
64        inline Vector4( 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 Vector4( const Real* const r )
73            : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
74        {
75        }
76
77        inline Vector4( const Vector4& rkVector )
78            : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w)
79        {
80        }
81
82                inline Real operator [] ( size_t i ) const
83        {
84            assert( i < 4 );
85
86            return *(&x+i);
87        }
88
89                inline Real& operator [] ( size_t i )
90        {
91            assert( i < 4 );
92
93            return *(&x+i);
94        }
95
96        /** Assigns the value of the other vector.
97            @param
98                rkVector The other vector
99        */
100        inline Vector4& operator = ( const Vector4& rkVector )
101        {
102            x = rkVector.x;
103            y = rkVector.y;
104            z = rkVector.z;           
105            w = rkVector.w;           
106
107            return *this;
108        }
109
110        inline bool operator == ( const Vector4& rkVector ) const
111        {
112            return ( x == rkVector.x &&
113                y == rkVector.y &&
114                z == rkVector.z &&
115                w == rkVector.w );
116        }
117
118        inline bool operator != ( const Vector4& rkVector ) const
119        {
120            return ( x != rkVector.x ||
121                y != rkVector.y ||
122                z != rkVector.z ||
123                w != rkVector.w );
124        }
125
126        inline Vector4& operator = (const Vector3& rhs)
127        {
128            x = rhs.x;
129            y = rhs.y;
130            z = rhs.z;
131            w = 1.0f;
132            return *this;
133        }
134
135        // arithmetic operations
136        inline Vector4 operator + ( const Vector4& rkVector ) const
137        {
138            Vector4 kSum;
139
140            kSum.x = x + rkVector.x;
141            kSum.y = y + rkVector.y;
142            kSum.z = z + rkVector.z;
143            kSum.w = w + rkVector.w;
144
145            return kSum;
146        }
147
148        inline Vector4 operator - ( const Vector4& rkVector ) const
149        {
150            Vector4 kDiff;
151
152            kDiff.x = x - rkVector.x;
153            kDiff.y = y - rkVector.y;
154            kDiff.z = z - rkVector.z;
155            kDiff.w = w - rkVector.w;
156
157            return kDiff;
158        }
159
160        inline Vector4 operator * ( Real fScalar ) const
161        {
162            Vector4 kProd;
163
164            kProd.x = fScalar*x;
165            kProd.y = fScalar*y;
166            kProd.z = fScalar*z;
167            kProd.w = fScalar*w;
168
169            return kProd;
170        }
171
172        inline Vector4 operator * ( const Vector4& rhs) const
173        {
174            Vector4 kProd;
175
176            kProd.x = rhs.x * x;
177            kProd.y = rhs.y * y;
178            kProd.z = rhs.z * z;
179            kProd.w = rhs.w * w;
180
181            return kProd;
182        }
183
184        inline Vector4 operator / ( Real fScalar ) const
185        {
186            assert( fScalar != 0.0 );
187
188            Vector4 kDiv;
189
190            Real fInv = 1.0 / fScalar;
191            kDiv.x = x * fInv;
192            kDiv.y = y * fInv;
193            kDiv.z = z * fInv;
194            kDiv.w = w * fInv;
195
196            return kDiv;
197        }
198
199        inline Vector4 operator / ( const Vector4& rhs) const
200        {
201            Vector4 kDiv;
202
203            kDiv.x = x / rhs.x;
204            kDiv.y = y / rhs.y;
205            kDiv.z = z / rhs.z;
206            kDiv.w = w / rhs.w;
207
208            return kDiv;
209        }
210
211
212        inline Vector4 operator - () const
213        {
214            Vector4 kNeg;
215
216            kNeg.x = -x;
217            kNeg.y = -y;
218            kNeg.z = -z;
219            kNeg.w = -w;
220
221            return kNeg;
222        }
223
224        inline friend Vector4 operator * ( Real fScalar, const Vector4& rkVector )
225        {
226            Vector4 kProd;
227
228            kProd.x = fScalar * rkVector.x;
229            kProd.y = fScalar * rkVector.y;
230            kProd.z = fScalar * rkVector.z;
231            kProd.w = fScalar * rkVector.w;
232
233            return kProd;
234        }
235
236        // arithmetic updates
237        inline Vector4& operator += ( const Vector4& rkVector )
238        {
239            x += rkVector.x;
240            y += rkVector.y;
241            z += rkVector.z;
242            w += rkVector.w;
243
244            return *this;
245        }
246
247        inline Vector4& operator -= ( const Vector4& rkVector )
248        {
249            x -= rkVector.x;
250            y -= rkVector.y;
251            z -= rkVector.z;
252            w -= rkVector.w;
253
254            return *this;
255        }
256
257        inline Vector4& operator *= ( Real fScalar )
258        {
259            x *= fScalar;
260            y *= fScalar;
261            z *= fScalar;
262            w *= fScalar;
263            return *this;
264        }
265
266        inline Vector4& operator *= ( const Vector4& rkVector )
267        {
268            x *= rkVector.x;
269            y *= rkVector.y;
270            z *= rkVector.z;
271            w *= rkVector.w;
272
273            return *this;
274        }
275
276        inline Vector4& operator /= ( Real fScalar )
277        {
278            assert( fScalar != 0.0 );
279
280            Real fInv = 1.0 / fScalar;
281
282            x *= fInv;
283            y *= fInv;
284            z *= fInv;
285            w *= fInv;
286
287            return *this;
288        }
289
290        inline Vector4& operator /= ( const Vector4& rkVector )
291        {
292            x /= rkVector.x;
293            y /= rkVector.y;
294            z /= rkVector.z;
295            w /= rkVector.w;
296
297            return *this;
298        }
299
300        /** Calculates the dot (scalar) product of this vector with another.
301            @param
302                vec Vector with which to calculate the dot product (together
303                with this one).
304            @returns
305                A float representing the dot product value.
306        */
307        inline Real dotProduct(const Vector4& vec) const
308        {
309            return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
310        }
311        /** Function for writing to a stream.
312        */
313        inline _OgreExport friend std::ostream& operator <<
314            ( std::ostream& o, const Vector4& v )
315        {
316            o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
317            return o;
318        }
319    };
320
321}
322#endif
Note: See TracBrowser for help on using the repository browser.