My Project
ParaVector3.h
1 #pragma once
2 #include "ParaMath.h"
3 #include "ParaQuaternion.h"
4 
5 namespace ParaEngine
6 {
7  class Matrix4;
8 
16  class Vector3
17  {
18  public:
19  float x, y, z;
20 
21  public:
22  inline Vector3()
23  {
24  }
25 
26  inline Vector3( const float fX, const float fY, const float fZ )
27  : x( fX ), y( fY ), z( fZ )
28  {
29  }
30 
31  inline explicit Vector3( const float afCoordinate[3] )
32  : x( afCoordinate[0] ),
33  y( afCoordinate[1] ),
34  z( afCoordinate[2] )
35  {
36  }
37 
38  inline explicit Vector3( const int afCoordinate[3] )
39  {
40  x = (float)afCoordinate[0];
41  y = (float)afCoordinate[1];
42  z = (float)afCoordinate[2];
43  }
44 
45  inline explicit Vector3( float* const r )
46  : x( r[0] ), y( r[1] ), z( r[2] )
47  {
48  }
49 
50  inline explicit Vector3( const float scaler )
51  : x( scaler )
52  , y( scaler )
53  , z( scaler )
54  {
55  }
56 
57 
58  inline float operator [] ( const size_t i ) const
59  {
60  assert( i < 3 );
61 
62  return *(&x+i);
63  }
64 
65  inline float& operator [] ( const size_t i )
66  {
67  assert( i < 3 );
68 
69  return *(&x+i);
70  }
72  inline float* ptr()
73  {
74  return &x;
75  }
77  inline const float* ptr() const
78  {
79  return &x;
80  }
81  /*inline operator float* () { return &x; };
82  inline operator const float* () const { return &x; };*/
83 
88  inline Vector3& operator = ( const Vector3& rkVector )
89  {
90  x = rkVector.x;
91  y = rkVector.y;
92  z = rkVector.z;
93 
94  return *this;
95  }
96 
97 #ifdef __D3DX9_H__
98  Vector3(const D3DXVECTOR3& v);
99  operator D3DXVECTOR3& ();
100  operator const D3DXVECTOR3& () const;
101 #else
102  inline explicit Vector3(const DeviceVector3& v)
103  : x(v.x), y(v.y), z(v.z)
104  {
105  }
106 #endif
107 
108  inline Vector3& operator = (const DeviceVector3& rkVector)
109  {
110  x = rkVector.x;
111  y = rkVector.y;
112  z = rkVector.z;
113  return *this;
114  }
115 
116  inline Vector3& operator = ( const float fScaler )
117  {
118  x = fScaler;
119  y = fScaler;
120  z = fScaler;
121 
122  return *this;
123  }
124 
125  inline bool operator == ( const Vector3& rkVector ) const
126  {
127  return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
128  }
129 
130  inline bool operator != ( const Vector3& rkVector ) const
131  {
132  return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
133  }
134 
135  // arithmetic operations
136  inline Vector3 operator + ( const Vector3& rkVector ) const
137  {
138  return Vector3(
139  x + rkVector.x,
140  y + rkVector.y,
141  z + rkVector.z);
142  }
143 
144  inline Vector3 operator - ( const Vector3& rkVector ) const
145  {
146  return Vector3(
147  x - rkVector.x,
148  y - rkVector.y,
149  z - rkVector.z);
150  }
151 
152  inline Vector3 operator * ( const float fScalar ) const
153  {
154  return Vector3(
155  x * fScalar,
156  y * fScalar,
157  z * fScalar);
158  }
159 
161  inline Vector3 operator % (const Vector3& rhs) const
162  {
163  return crossProduct(rhs);
164  }
165 
166  inline Vector3 operator * ( const Vector3& rhs) const
167  {
168  return Vector3(
169  x * rhs.x,
170  y * rhs.y,
171  z * rhs.z);
172  }
173 
174  inline Vector3 operator / ( const float fScalar ) const
175  {
176  assert( fScalar != 0.0f );
177 
178  float fInv = 1.0f / fScalar;
179 
180  return Vector3(
181  x * fInv,
182  y * fInv,
183  z * fInv);
184  }
185 
186  inline Vector3 operator / ( const Vector3& rhs) const
187  {
188  return Vector3(
189  x / rhs.x,
190  y / rhs.y,
191  z / rhs.z);
192  }
193 
194  inline const Vector3& operator + () const
195  {
196  return *this;
197  }
198 
199  inline Vector3 operator - () const
200  {
201  return Vector3(-x, -y, -z);
202  }
203 
204  // overloaded operators to help Vector3
205  inline friend Vector3 operator * ( const float fScalar, const Vector3& rkVector )
206  {
207  return Vector3(
208  fScalar * rkVector.x,
209  fScalar * rkVector.y,
210  fScalar * rkVector.z);
211  }
212 
213  inline friend Vector3 operator / ( const float fScalar, const Vector3& rkVector )
214  {
215  return Vector3(
216  fScalar / rkVector.x,
217  fScalar / rkVector.y,
218  fScalar / rkVector.z);
219  }
220 
221  inline friend Vector3 operator + (const Vector3& lhs, const float rhs)
222  {
223  return Vector3(
224  lhs.x + rhs,
225  lhs.y + rhs,
226  lhs.z + rhs);
227  }
228 
229  inline friend Vector3 operator + (const float lhs, const Vector3& rhs)
230  {
231  return Vector3(
232  lhs + rhs.x,
233  lhs + rhs.y,
234  lhs + rhs.z);
235  }
236 
237  inline friend Vector3 operator - (const Vector3& lhs, const float rhs)
238  {
239  return Vector3(
240  lhs.x - rhs,
241  lhs.y - rhs,
242  lhs.z - rhs);
243  }
244 
245  inline friend Vector3 operator - (const float lhs, const Vector3& rhs)
246  {
247  return Vector3(
248  lhs - rhs.x,
249  lhs - rhs.y,
250  lhs - rhs.z);
251  }
252 
253  // arithmetic updates
254  inline Vector3& operator += ( const Vector3& rkVector )
255  {
256  x += rkVector.x;
257  y += rkVector.y;
258  z += rkVector.z;
259 
260  return *this;
261  }
262 
263  inline Vector3& operator += ( const float fScalar )
264  {
265  x += fScalar;
266  y += fScalar;
267  z += fScalar;
268  return *this;
269  }
270 
271  inline Vector3& operator -= ( const Vector3& rkVector )
272  {
273  x -= rkVector.x;
274  y -= rkVector.y;
275  z -= rkVector.z;
276 
277  return *this;
278  }
279 
280  inline Vector3& operator -= ( const float fScalar )
281  {
282  x -= fScalar;
283  y -= fScalar;
284  z -= fScalar;
285  return *this;
286  }
287 
288  inline Vector3& operator *= ( const float fScalar )
289  {
290  x *= fScalar;
291  y *= fScalar;
292  z *= fScalar;
293  return *this;
294  }
295 
296  inline Vector3& operator *= ( const Vector3& rkVector )
297  {
298  x *= rkVector.x;
299  y *= rkVector.y;
300  z *= rkVector.z;
301 
302  return *this;
303  }
304 
306  inline Vector3 operator * (const Matrix4& mat) const;
307 
308  inline Vector3& operator /= ( const float fScalar )
309  {
310  assert( fScalar != 0.0f );
311 
312  float fInv = 1.0f / fScalar;
313 
314  x *= fInv;
315  y *= fInv;
316  z *= fInv;
317 
318  return *this;
319  }
320 
321  inline Vector3& operator /= ( const Vector3& rkVector )
322  {
323  x /= rkVector.x;
324  y /= rkVector.y;
325  z /= rkVector.z;
326 
327  return *this;
328  }
329 
331  int dominantAxis() const;
332 
340  inline float length () const
341  {
342  return Math::Sqrt( x * x + y * y + z * z );
343  }
344 
355  inline float squaredLength () const
356  {
357  return x * x + y * y + z * z;
358  }
359 
367  inline float distance(const Vector3& rhs) const
368  {
369  return (*this - rhs).length();
370  }
371 
382  inline float squaredDistance(const Vector3& rhs) const
383  {
384  return (*this - rhs).squaredLength();
385  }
386 
401  inline float dotProduct(const Vector3& vec) const
402  {
403  return x * vec.x + y * vec.y + z * vec.z;
404  }
405 
416  inline float absDotProduct(const Vector3& vec) const
417  {
418  return Math::Abs(x * vec.x) + Math::Abs(y * vec.y) + Math::Abs(z * vec.z);
419  }
420 
430  inline float normalise()
431  {
432  float fLength = Math::Sqrt( x * x + y * y + z * z );
433 
434  // Will also work for zero-sized vectors, but will change nothing
435  if ( fLength > 1e-08 )
436  {
437  float fInvLength = 1.0f / fLength;
438  x *= fInvLength;
439  y *= fInvLength;
440  z *= fInvLength;
441  }
442 
443  return fLength;
444  }
445 
446  inline Vector3 InvertYCopy() const
447  {
448  return Vector3(x, -y, z);
449  }
450 
451  inline Vector3& InvertY()
452  {
453  y = -y;
454  return *this;
455  }
456 
485  inline Vector3 crossProduct( const Vector3& rkVector ) const
486  {
487  return Vector3(
488  y * rkVector.z - z * rkVector.y,
489  z * rkVector.x - x * rkVector.z,
490  x * rkVector.y - y * rkVector.x);
491  }
492 
496  inline Vector3 midPoint( const Vector3& vec ) const
497  {
498  return Vector3(
499  ( x + vec.x ) * 0.5f,
500  ( y + vec.y ) * 0.5f,
501  ( z + vec.z ) * 0.5f );
502  }
503 
505  Vector3 TransformNormal(const Matrix4& m) const;
506 
510  Vector3 TransformCoord(const Matrix4& m) const;
511 
512 
516  inline bool operator < ( const Vector3& rhs ) const
517  {
518  if( x < rhs.x && y < rhs.y && z < rhs.z )
519  return true;
520  return false;
521  }
522 
526  inline bool operator > ( const Vector3& rhs ) const
527  {
528  if( x > rhs.x && y > rhs.y && z > rhs.z )
529  return true;
530  return false;
531  }
532 
540  inline void makeFloor( const Vector3& cmp )
541  {
542  if( cmp.x < x ) x = cmp.x;
543  if( cmp.y < y ) y = cmp.y;
544  if( cmp.z < z ) z = cmp.z;
545  }
546 
554  inline void makeCeil( const Vector3& cmp )
555  {
556  if( cmp.x > x ) x = cmp.x;
557  if( cmp.y > y ) y = cmp.y;
558  if( cmp.z > z ) z = cmp.z;
559  }
560 
568  Vector3 perpendicular(void) const;
588  Vector3 randomDeviant(
589  const Radian& angle,
590  const Vector3& up = Vector3::ZERO ) const;
591 
596  Radian angleBetween(const Vector3& dest);
605  Quaternion getRotationTo(const Vector3& dest,
606  const Vector3& fallbackAxis = Vector3::ZERO) const;
607 
609  inline bool isZeroLength(void) const
610  {
611  float sqlen = (x * x) + (y * y) + (z * z);
612  return (sqlen < (1e-06 * 1e-06));
613 
614  }
615 
618  inline Vector3 normalisedCopy(void) const
619  {
620  Vector3 ret = *this;
621  ret.normalise();
622  return ret;
623  }
624 
628  inline Vector3 reflect(const Vector3& normal) const
629  {
630  return Vector3( *this - ( 2 * this->dotProduct(normal) * normal ) );
631  }
632 
639  inline bool positionEquals(const Vector3& rhs, float tolerance = 1e-03) const
640  {
641  return Math::RealEqual(x, rhs.x, tolerance) &&
642  Math::RealEqual(y, rhs.y, tolerance) &&
643  Math::RealEqual(z, rhs.z, tolerance);
644 
645  }
646 
653  inline bool positionCloses(const Vector3& rhs, float tolerance = 1e-03f) const
654  {
655  return squaredDistance(rhs) <=
656  (squaredLength() + rhs.squaredLength()) * tolerance;
657  }
658 
666  inline bool directionEquals(const Vector3& rhs,
667  const Radian& tolerance) const
668  {
669  float dot = dotProduct(rhs);
670  Radian angle = Math::ACos(dot);
671 
672  return Math::Abs(angle.valueRadians()) <= tolerance.valueRadians();
673 
674  }
675 
676  // special points
677  static const Vector3 ZERO;
678  static const Vector3 UNIT_X;
679  static const Vector3 UNIT_Y;
680  static const Vector3 UNIT_Z;
681  static const Vector3 NEGATIVE_UNIT_X;
682  static const Vector3 NEGATIVE_UNIT_Y;
683  static const Vector3 NEGATIVE_UNIT_Z;
684  static const Vector3 UNIT_SCALE;
685 
688  inline friend std::ostream& operator <<
689  ( std::ostream& o, const Vector3& v )
690  {
691  o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
692  return o;
693  }
694 
695  DeviceVector3_ptr GetPointer(){ return (DeviceVector3_ptr)this; }
696  };
697 
698 }
bool directionEquals(const Vector3 &rhs, const Radian &tolerance) const
Returns whether this vector is within a directional tolerance of another vector.
Definition: ParaVector3.h:666
Wrapper class which indicates a given angle value is in Radians.
Definition: ParaAngle.h:10
const float * ptr() const
Pointer accessor for direct copying.
Definition: ParaVector3.h:77
bool positionEquals(const Vector3 &rhs, float tolerance=1e-03) const
Returns whether this vector is within a positional tolerance of another vector.
Definition: ParaVector3.h:639
bool operator<(const Vector3 &rhs) const
Returns true if the vector&#39;s scalar components are all greater that the ones of the vector it is comp...
Definition: ParaVector3.h:516
void makeCeil(const Vector3 &cmp)
Sets this vector&#39;s components to the maximum of its own and the ones of the passed in vector...
Definition: ParaVector3.h:554
float absDotProduct(const Vector3 &vec) const
Calculates the absolute dot (scalar) product of this vector with another.
Definition: ParaVector3.h:416
float squaredDistance(const Vector3 &rhs) const
Returns the square of the distance to another vector.
Definition: ParaVector3.h:382
different physics engine has different winding order.
Definition: EventBinding.h:32
bool operator>(const Vector3 &rhs) const
Returns true if the vector&#39;s scalar components are all smaller that the ones of the vector it is comp...
Definition: ParaVector3.h:526
Vector3 TransformCoord(const Matrix4 &m) const
only use this function.
static bool RealEqual(float a, float b, float tolerance=std::numeric_limits< float >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
Definition: ParaMath.cpp:307
Implementation of a Quaternion, i.e.
Definition: ParaQuaternion.h:10
Vector3 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalised vector is returned as a copy...
Definition: ParaVector3.h:618
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
bool positionCloses(const Vector3 &rhs, float tolerance=1e-03f) const
Returns whether this vector is within a positional tolerance of another vector, also take scale of th...
Definition: ParaVector3.h:653
float distance(const Vector3 &rhs) const
Returns the distance to another vector.
Definition: ParaVector3.h:367
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: ParaVector3.h:609
Vector3 midPoint(const Vector3 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: ParaVector3.h:496
Vector3 & operator=(const Vector3 &rkVector)
Assigns the value of the other vector.
Definition: ParaVector3.h:88
Vector3 randomDeviant(const Radian &angle, const Vector3 &up=Vector3::ZERO) const
Generates a new random vector which deviates from this vector by a given angle in a random direction...
Definition: ParaVector3.cpp:112
Vector3 TransformNormal(const Matrix4 &m) const
ignored translation(row 3).
Definition: ParaVector3.cpp:40
float normalise()
Normalises the vector.
Definition: ParaVector3.h:430
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Quaternion getRotationTo(const Vector3 &dest, const Vector3 &fallbackAxis=Vector3::ZERO) const
Gets the shortest arc quaternion to rotate this vector to the destination vector. ...
Definition: ParaVector3.cpp:49
Vector3 operator%(const Vector3 &rhs) const
special cross product
Definition: ParaVector3.h:161
float length() const
Returns the length (magnitude) of the vector.
Definition: ParaVector3.h:340
Vector3 crossProduct(const Vector3 &rkVector) const
Calculates the cross-product of 2 vectors, i.e.
Definition: ParaVector3.h:485
Vector3 reflect(const Vector3 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: ParaVector3.h:628
float * ptr()
Pointer accessor for direct copying.
Definition: ParaVector3.h:72
Definition: PEtypes.h:298
float squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: ParaVector3.h:355
float dotProduct(const Vector3 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: ParaVector3.h:401
Radian angleBetween(const Vector3 &dest)
Gets the angle between 2 vectors.
Definition: ParaVector3.cpp:98
void makeFloor(const Vector3 &cmp)
Sets this vector&#39;s components to the minimum of its own and the ones of the passed in vector...
Definition: ParaVector3.h:540
Vector3 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an &#39;up&#39; vector).
Definition: ParaVector3.cpp:136
int dominantAxis() const
Returns the axis along which this vector is dominant.
Definition: ParaVector3.cpp:155