OGRE  1.8
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreVector4.h
Go to the documentation of this file.
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-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Vector4_H__
29 #define __Vector4_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreVector3.h"
33 
34 namespace Ogre
35 {
36 
46  {
47  public:
48  Real x, y, z, w;
49 
50  public:
51  inline Vector4()
52  {
53  }
54 
55  inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW )
56  : x( fX ), y( fY ), z( fZ ), w( fW)
57  {
58  }
59 
60  inline explicit Vector4( const Real afCoordinate[4] )
61  : x( afCoordinate[0] ),
62  y( afCoordinate[1] ),
63  z( afCoordinate[2] ),
64  w( afCoordinate[3] )
65  {
66  }
67 
68  inline explicit Vector4( const int afCoordinate[4] )
69  {
70  x = (Real)afCoordinate[0];
71  y = (Real)afCoordinate[1];
72  z = (Real)afCoordinate[2];
73  w = (Real)afCoordinate[3];
74  }
75 
76  inline explicit Vector4( Real* const r )
77  : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
78  {
79  }
80 
81  inline explicit Vector4( const Real scaler )
82  : x( scaler )
83  , y( scaler )
84  , z( scaler )
85  , w( scaler )
86  {
87  }
88 
89  inline explicit Vector4(const Vector3& rhs)
90  : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
91  {
92  }
93 
96  inline void swap(Vector4& other)
97  {
98  std::swap(x, other.x);
99  std::swap(y, other.y);
100  std::swap(z, other.z);
101  std::swap(w, other.w);
102  }
103 
104  inline Real operator [] ( const size_t i ) const
105  {
106  assert( i < 4 );
107 
108  return *(&x+i);
109  }
110 
111  inline Real& operator [] ( const size_t i )
112  {
113  assert( i < 4 );
114 
115  return *(&x+i);
116  }
117 
119  inline Real* ptr()
120  {
121  return &x;
122  }
124  inline const Real* ptr() const
125  {
126  return &x;
127  }
128 
133  inline Vector4& operator = ( const Vector4& rkVector )
134  {
135  x = rkVector.x;
136  y = rkVector.y;
137  z = rkVector.z;
138  w = rkVector.w;
139 
140  return *this;
141  }
142 
143  inline Vector4& operator = ( const Real fScalar)
144  {
145  x = fScalar;
146  y = fScalar;
147  z = fScalar;
148  w = fScalar;
149  return *this;
150  }
151 
152  inline bool operator == ( const Vector4& rkVector ) const
153  {
154  return ( x == rkVector.x &&
155  y == rkVector.y &&
156  z == rkVector.z &&
157  w == rkVector.w );
158  }
159 
160  inline bool operator != ( const Vector4& rkVector ) const
161  {
162  return ( x != rkVector.x ||
163  y != rkVector.y ||
164  z != rkVector.z ||
165  w != rkVector.w );
166  }
167 
168  inline Vector4& operator = (const Vector3& rhs)
169  {
170  x = rhs.x;
171  y = rhs.y;
172  z = rhs.z;
173  w = 1.0f;
174  return *this;
175  }
176 
177  // arithmetic operations
178  inline Vector4 operator + ( const Vector4& rkVector ) const
179  {
180  return Vector4(
181  x + rkVector.x,
182  y + rkVector.y,
183  z + rkVector.z,
184  w + rkVector.w);
185  }
186 
187  inline Vector4 operator - ( const Vector4& rkVector ) const
188  {
189  return Vector4(
190  x - rkVector.x,
191  y - rkVector.y,
192  z - rkVector.z,
193  w - rkVector.w);
194  }
195 
196  inline Vector4 operator * ( const Real fScalar ) const
197  {
198  return Vector4(
199  x * fScalar,
200  y * fScalar,
201  z * fScalar,
202  w * fScalar);
203  }
204 
205  inline Vector4 operator * ( const Vector4& rhs) const
206  {
207  return Vector4(
208  rhs.x * x,
209  rhs.y * y,
210  rhs.z * z,
211  rhs.w * w);
212  }
213 
214  inline Vector4 operator / ( const Real fScalar ) const
215  {
216  assert( fScalar != 0.0 );
217 
218  Real fInv = 1.0f / fScalar;
219 
220  return Vector4(
221  x * fInv,
222  y * fInv,
223  z * fInv,
224  w * fInv);
225  }
226 
227  inline Vector4 operator / ( const Vector4& rhs) const
228  {
229  return Vector4(
230  x / rhs.x,
231  y / rhs.y,
232  z / rhs.z,
233  w / rhs.w);
234  }
235 
236  inline const Vector4& operator + () const
237  {
238  return *this;
239  }
240 
241  inline Vector4 operator - () const
242  {
243  return Vector4(-x, -y, -z, -w);
244  }
245 
246  inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
247  {
248  return Vector4(
249  fScalar * rkVector.x,
250  fScalar * rkVector.y,
251  fScalar * rkVector.z,
252  fScalar * rkVector.w);
253  }
254 
255  inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector )
256  {
257  return Vector4(
258  fScalar / rkVector.x,
259  fScalar / rkVector.y,
260  fScalar / rkVector.z,
261  fScalar / rkVector.w);
262  }
263 
264  inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
265  {
266  return Vector4(
267  lhs.x + rhs,
268  lhs.y + rhs,
269  lhs.z + rhs,
270  lhs.w + rhs);
271  }
272 
273  inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
274  {
275  return Vector4(
276  lhs + rhs.x,
277  lhs + rhs.y,
278  lhs + rhs.z,
279  lhs + rhs.w);
280  }
281 
282  inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
283  {
284  return Vector4(
285  lhs.x - rhs,
286  lhs.y - rhs,
287  lhs.z - rhs,
288  lhs.w - rhs);
289  }
290 
291  inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
292  {
293  return Vector4(
294  lhs - rhs.x,
295  lhs - rhs.y,
296  lhs - rhs.z,
297  lhs - rhs.w);
298  }
299 
300  // arithmetic updates
301  inline Vector4& operator += ( const Vector4& rkVector )
302  {
303  x += rkVector.x;
304  y += rkVector.y;
305  z += rkVector.z;
306  w += rkVector.w;
307 
308  return *this;
309  }
310 
311  inline Vector4& operator -= ( const Vector4& rkVector )
312  {
313  x -= rkVector.x;
314  y -= rkVector.y;
315  z -= rkVector.z;
316  w -= rkVector.w;
317 
318  return *this;
319  }
320 
321  inline Vector4& operator *= ( const Real fScalar )
322  {
323  x *= fScalar;
324  y *= fScalar;
325  z *= fScalar;
326  w *= fScalar;
327  return *this;
328  }
329 
330  inline Vector4& operator += ( const Real fScalar )
331  {
332  x += fScalar;
333  y += fScalar;
334  z += fScalar;
335  w += fScalar;
336  return *this;
337  }
338 
339  inline Vector4& operator -= ( const Real fScalar )
340  {
341  x -= fScalar;
342  y -= fScalar;
343  z -= fScalar;
344  w -= fScalar;
345  return *this;
346  }
347 
348  inline Vector4& operator *= ( const Vector4& rkVector )
349  {
350  x *= rkVector.x;
351  y *= rkVector.y;
352  z *= rkVector.z;
353  w *= rkVector.w;
354 
355  return *this;
356  }
357 
358  inline Vector4& operator /= ( const Real fScalar )
359  {
360  assert( fScalar != 0.0 );
361 
362  Real fInv = 1.0f / fScalar;
363 
364  x *= fInv;
365  y *= fInv;
366  z *= fInv;
367  w *= fInv;
368 
369  return *this;
370  }
371 
372  inline Vector4& operator /= ( const Vector4& rkVector )
373  {
374  x /= rkVector.x;
375  y /= rkVector.y;
376  z /= rkVector.z;
377  w /= rkVector.w;
378 
379  return *this;
380  }
381 
389  inline Real dotProduct(const Vector4& vec) const
390  {
391  return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
392  }
394  inline bool isNaN() const
395  {
396  return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
397  }
400  inline _OgreExport friend std::ostream& operator <<
401  ( std::ostream& o, const Vector4& v )
402  {
403  o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
404  return o;
405  }
406  // special
407  static const Vector4 ZERO;
408  };
412 }
413 #endif
414 
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:233
Vector4(const Real fX, const Real fY, const Real fZ, const Real fW)
Definition: OgreVector4.h:55
Vector4(const Real afCoordinate[4])
Definition: OgreVector4.h:60
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:720
Radian operator/(Real a, const Radian &b)
Definition: OgreMath.h:725
Vector4(const int afCoordinate[4])
Definition: OgreVector4.h:68
Real * ptr()
Pointer accessor for direct copying.
Definition: OgreVector4.h:119
Vector4(Real *const r)
Definition: OgreVector4.h:76
const Real * ptr() const
Pointer accessor for direct copying.
Definition: OgreVector4.h:124
Vector4(const Vector3 &rhs)
Definition: OgreVector4.h:89
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Real dotProduct(const Vector4 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: OgreVector4.h:389
void swap(Vector4 &other)
Exchange the contents of this vector with another.
Definition: OgreVector4.h:96
Vector4(const Real scaler)
Definition: OgreVector4.h:81
static bool isNaN(Real f)
Definition: OgreMath.h:290
static const Vector4 ZERO
Definition: OgreVector4.h:407
4-dimensional homogeneous vector.
Definition: OgreVector4.h:45
bool isNaN() const
Check whether this vector contains valid values.
Definition: OgreVector4.h:394
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)