My Project
ParaVector4.h
1 #pragma once
2 
3 #include "ParaVector3.h"
4 
5 namespace ParaEngine
6 {
7  class Matrix4;
10  class Vector4
11  {
12  public:
13  union {
14  struct{
15  float x, y, z, w;
16  };
17  float vector4_f32[4];
18  };
19  public:
20  inline Vector4()
21  {
22  }
23 
24  inline Vector4( const float fX, const float fY, const float fZ, const float fW )
25  : x( fX ), y( fY ), z( fZ ), w( fW)
26  {
27  }
28 
29  inline explicit Vector4( const float afCoordinate[4] )
30  : x( afCoordinate[0] ),
31  y( afCoordinate[1] ),
32  z( afCoordinate[2] ),
33  w( afCoordinate[3] )
34  {
35  }
36 
37  inline explicit Vector4( const int afCoordinate[4] )
38  {
39  x = (float)afCoordinate[0];
40  y = (float)afCoordinate[1];
41  z = (float)afCoordinate[2];
42  w = (float)afCoordinate[3];
43  }
44 
45  inline explicit Vector4( float* const r )
46  : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
47  {
48  }
49 
50  inline explicit Vector4( const float scaler )
51  : x( scaler )
52  , y( scaler )
53  , z( scaler )
54  , w( scaler )
55  {
56  }
57 
58  inline explicit Vector4(const Vector3& rhs, float w_ = 1.f)
59  : x(rhs.x), y(rhs.y), z(rhs.z), w(w_)
60  {
61  }
62 
63  inline explicit Vector4(const DeviceVector4& r)
64  {
65  operator=(reinterpret_cast<const Vector4&>(r));
66  }
67 
68 
69  inline float operator [] ( const size_t i ) const
70  {
71  assert( i < 4 );
72 
73  return *(&x+i);
74  }
75 
76  inline float& operator [] ( const size_t i )
77  {
78  assert( i < 4 );
79 
80  return *(&x+i);
81  }
82 
84  inline float* ptr()
85  {
86  return &x;
87  }
89  inline const float* ptr() const
90  {
91  return &x;
92  }
93 
98  inline Vector4& operator = ( const Vector4& rkVector )
99  {
100  x = rkVector.x;
101  y = rkVector.y;
102  z = rkVector.z;
103  w = rkVector.w;
104 
105  return *this;
106  }
107 
108  inline Vector4& operator = ( const float fScalar)
109  {
110  x = fScalar;
111  y = fScalar;
112  z = fScalar;
113  w = fScalar;
114  return *this;
115  }
116 
117  inline bool operator == ( const Vector4& rkVector ) const
118  {
119  return ( x == rkVector.x &&
120  y == rkVector.y &&
121  z == rkVector.z &&
122  w == rkVector.w );
123  }
124 
125  inline bool operator != ( const Vector4& rkVector ) const
126  {
127  return ( x != rkVector.x ||
128  y != rkVector.y ||
129  z != rkVector.z ||
130  w != rkVector.w );
131  }
132 
133  inline Vector4& operator = (const Vector3& rhs)
134  {
135  x = rhs.x;
136  y = rhs.y;
137  z = rhs.z;
138  w = 1.0f;
139  return *this;
140  }
141 
142  // arithmetic operations
143  inline Vector4 operator + ( const Vector4& rkVector ) const
144  {
145  return Vector4(
146  x + rkVector.x,
147  y + rkVector.y,
148  z + rkVector.z,
149  w + rkVector.w);
150  }
151 
152  inline Vector4 operator - ( const Vector4& rkVector ) const
153  {
154  return Vector4(
155  x - rkVector.x,
156  y - rkVector.y,
157  z - rkVector.z,
158  w - rkVector.w);
159  }
160 
161  inline Vector4 operator * ( const float fScalar ) const
162  {
163  return Vector4(
164  x * fScalar,
165  y * fScalar,
166  z * fScalar,
167  w * fScalar);
168  }
169 
170  inline Vector4 operator * ( const Vector4& rhs) const
171  {
172  return Vector4(
173  rhs.x * x,
174  rhs.y * y,
175  rhs.z * z,
176  rhs.w * w);
177  }
178 
179  inline Vector4 operator * (const Matrix4& mat) const;
180 
181  inline Vector4 operator / ( const float fScalar ) const
182  {
183  assert( fScalar != 0.0f );
184 
185  float fInv = 1.0f / fScalar;
186 
187  return Vector4(
188  x * fInv,
189  y * fInv,
190  z * fInv,
191  w * fInv);
192  }
193 
194  inline Vector4 operator / ( const Vector4& rhs) const
195  {
196  return Vector4(
197  x / rhs.x,
198  y / rhs.y,
199  z / rhs.z,
200  w / rhs.w);
201  }
202 
203  inline const Vector4& operator + () const
204  {
205  return *this;
206  }
207 
208  inline Vector4 operator - () const
209  {
210  return Vector4(-x, -y, -z, -w);
211  }
212 
213  inline friend Vector4 operator * ( const float fScalar, const Vector4& rkVector )
214  {
215  return Vector4(
216  fScalar * rkVector.x,
217  fScalar * rkVector.y,
218  fScalar * rkVector.z,
219  fScalar * rkVector.w);
220  }
221 
222  inline friend Vector4 operator / ( const float fScalar, const Vector4& rkVector )
223  {
224  return Vector4(
225  fScalar / rkVector.x,
226  fScalar / rkVector.y,
227  fScalar / rkVector.z,
228  fScalar / rkVector.w);
229  }
230 
231  inline friend Vector4 operator + (const Vector4& lhs, const float rhs)
232  {
233  return Vector4(
234  lhs.x + rhs,
235  lhs.y + rhs,
236  lhs.z + rhs,
237  lhs.w + rhs);
238  }
239 
240  inline friend Vector4 operator + (const float lhs, const Vector4& rhs)
241  {
242  return Vector4(
243  lhs + rhs.x,
244  lhs + rhs.y,
245  lhs + rhs.z,
246  lhs + rhs.w);
247  }
248 
249  inline friend Vector4 operator - (const Vector4& lhs, float rhs)
250  {
251  return Vector4(
252  lhs.x - rhs,
253  lhs.y - rhs,
254  lhs.z - rhs,
255  lhs.w - rhs);
256  }
257 
258  inline friend Vector4 operator - (const float lhs, const Vector4& rhs)
259  {
260  return Vector4(
261  lhs - rhs.x,
262  lhs - rhs.y,
263  lhs - rhs.z,
264  lhs - rhs.w);
265  }
266 
267  // arithmetic updates
268  inline Vector4& operator += ( const Vector4& rkVector )
269  {
270  x += rkVector.x;
271  y += rkVector.y;
272  z += rkVector.z;
273  w += rkVector.w;
274 
275  return *this;
276  }
277 
278  inline Vector4& operator -= ( const Vector4& rkVector )
279  {
280  x -= rkVector.x;
281  y -= rkVector.y;
282  z -= rkVector.z;
283  w -= rkVector.w;
284 
285  return *this;
286  }
287 
288  inline Vector4& operator *= ( const float fScalar )
289  {
290  x *= fScalar;
291  y *= fScalar;
292  z *= fScalar;
293  w *= fScalar;
294  return *this;
295  }
296 
297  inline Vector4& operator += ( const float fScalar )
298  {
299  x += fScalar;
300  y += fScalar;
301  z += fScalar;
302  w += fScalar;
303  return *this;
304  }
305 
306  inline Vector4& operator -= ( const float fScalar )
307  {
308  x -= fScalar;
309  y -= fScalar;
310  z -= fScalar;
311  w -= fScalar;
312  return *this;
313  }
314 
315  inline Vector4& operator *= ( const Vector4& rkVector )
316  {
317  x *= rkVector.x;
318  y *= rkVector.y;
319  z *= rkVector.z;
320  w *= rkVector.w;
321 
322  return *this;
323  }
324 
325  inline Vector4& operator /= ( const float fScalar )
326  {
327  assert( fScalar != 0.0f );
328 
329  float fInv = 1.0f / fScalar;
330 
331  x *= fInv;
332  y *= fInv;
333  z *= fInv;
334  w *= fInv;
335 
336  return *this;
337  }
338 
339  inline Vector4& operator /= ( const Vector4& rkVector )
340  {
341  x /= rkVector.x;
342  y /= rkVector.y;
343  z /= rkVector.z;
344  w /= rkVector.w;
345 
346  return *this;
347  }
348 
349  inline operator Vector3() const;
350 
358  inline float dotProduct(const Vector4& vec) const
359  {
360  return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
361  }
362 
367  inline Vector4 transformAffine(const Matrix4& M) const;
368 
371  inline friend std::ostream& operator <<
372  ( std::ostream& o, const Vector4& v )
373  {
374  o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
375  return o;
376  }
377  // special
378  static const Vector4 ZERO;
379  };
380 
381 }
float dotProduct(const Vector4 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: ParaVector4.h:358
float * ptr()
Pointer accessor for direct copying.
Definition: ParaVector4.h:84
4-dimensional homogeneous vector.
Definition: ParaVector4.h:10
const float * ptr() const
Pointer accessor for direct copying.
Definition: ParaVector4.h:89
different physics engine has different winding order.
Definition: EventBinding.h:32
Vector4 transformAffine(const Matrix4 &M) const
row vector: 4-D Vector transformation specially for affine matrix.
Definition: ParaVector4.inl:10
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Vector4 & operator=(const Vector4 &rkVector)
Assigns the value of the other vector.
Definition: ParaVector4.h:98
Definition: PEtypes.h:306