My Project
ParaDVector3.h
1 #pragma once
2 #include "ParaMath.h"
3 #include "ParaQuaternion.h"
4 #include "ParaVector3.h"
5 
6 namespace ParaEngine
7 {
8  class Matrix4;
9 
17  class DVector3
18  {
19  public:
20  double x, y, z;
21 
22  public:
23  inline DVector3()
24  {
25  }
26 
27  inline DVector3(const double fX, const double fY, const double fZ)
28  : x( fX ), y( fY ), z( fZ )
29  {
30  }
31 
32  inline explicit DVector3(const double afCoordinate[3])
33  : x( afCoordinate[0] ), y( afCoordinate[1] ), z( afCoordinate[2] )
34  {
35  }
36 
37  inline explicit DVector3( const int afCoordinate[3] )
38  {
39  x = (double)afCoordinate[0];
40  y = (double)afCoordinate[1];
41  z = (double)afCoordinate[2];
42  }
43 
44  inline explicit DVector3( double* const r )
45  : x( r[0] ), y( r[1] ), z( r[2] )
46  {
47  }
48 
49  inline explicit DVector3( const double scaler )
50  : x( scaler ), y( scaler ) , z( scaler )
51  {
52  }
53 
54  inline explicit DVector3(const Vector3& vFrom)
55  : x(vFrom.x), y(vFrom.y), z(vFrom.z)
56  {
57  }
58 
59  inline double operator [] ( const size_t i ) const
60  {
61  assert( i < 3 );
62 
63  return *(&x+i);
64  }
65 
66  inline double& operator [] ( const size_t i )
67  {
68  assert( i < 3 );
69 
70  return *(&x+i);
71  }
73  inline double* ptr()
74  {
75  return &x;
76  }
78  inline const double* ptr() const
79  {
80  return &x;
81  }
82 
84  operator Vector3 () const {
85  return Vector3((float)x, (float)y, (float)z);
86  };
87 
88  /*inline operator double* () { return &x; };
89  inline operator const double* () const { return &x; };*/
90 
95  inline DVector3& operator = ( const DVector3& rkVector )
96  {
97  x = rkVector.x;
98  y = rkVector.y;
99  z = rkVector.z;
100 
101  return *this;
102  }
103 
104  inline DVector3& operator = (const Vector3& rkVector)
105  {
106  x = rkVector.x;
107  y = rkVector.y;
108  z = rkVector.z;
109  return *this;
110  }
111 
112  inline DVector3& operator = ( const double fScaler )
113  {
114  x = fScaler;
115  y = fScaler;
116  z = fScaler;
117 
118  return *this;
119  }
120 
121  inline bool operator == ( const DVector3& rkVector ) const
122  {
123  return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
124  }
125 
126  inline bool operator != ( const DVector3& rkVector ) const
127  {
128  return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
129  }
130 
131  // arithmetic operations
132  inline DVector3 operator + ( const DVector3& rkVector ) const
133  {
134  return DVector3(
135  x + rkVector.x,
136  y + rkVector.y,
137  z + rkVector.z);
138  }
139  inline DVector3 operator + (const Vector3& rkVector) const
140  {
141  return DVector3(
142  x + rkVector.x,
143  y + rkVector.y,
144  z + rkVector.z);
145  }
146 
147  inline DVector3 operator - ( const DVector3& rkVector ) const
148  {
149  return DVector3(
150  x - rkVector.x,
151  y - rkVector.y,
152  z - rkVector.z);
153  }
154 
155  inline DVector3 operator - (const Vector3& rkVector) const
156  {
157  return DVector3(
158  x - rkVector.x,
159  y - rkVector.y,
160  z - rkVector.z);
161  }
162 
163  inline DVector3 operator * ( const double fScalar ) const
164  {
165  return DVector3(
166  x * fScalar,
167  y * fScalar,
168  z * fScalar);
169  }
170 
172  inline DVector3 operator % (const DVector3& rhs) const
173  {
174  return crossProduct(rhs);
175  }
176 
177  inline DVector3 operator * ( const DVector3& rhs) const
178  {
179  return DVector3(
180  x * rhs.x,
181  y * rhs.y,
182  z * rhs.z);
183  }
184 
185  inline DVector3 operator / ( const double fScalar ) const
186  {
187  assert( fScalar != 0.0f );
188 
189  double fInv = 1.0f / fScalar;
190 
191  return DVector3(
192  x * fInv,
193  y * fInv,
194  z * fInv);
195  }
196 
197  inline DVector3 operator / ( const DVector3& rhs) const
198  {
199  return DVector3(
200  x / rhs.x,
201  y / rhs.y,
202  z / rhs.z);
203  }
204 
205  inline const DVector3& operator + () const
206  {
207  return *this;
208  }
209 
210  inline DVector3 operator - () const
211  {
212  return DVector3(-x, -y, -z);
213  }
214 
215  // overloaded operators to help DVector3
216  inline friend DVector3 operator * ( const double fScalar, const DVector3& rkVector )
217  {
218  return DVector3(
219  fScalar * rkVector.x,
220  fScalar * rkVector.y,
221  fScalar * rkVector.z);
222  }
223 
224  inline friend DVector3 operator / ( const double fScalar, const DVector3& rkVector )
225  {
226  return DVector3(
227  fScalar / rkVector.x,
228  fScalar / rkVector.y,
229  fScalar / rkVector.z);
230  }
231 
232  inline friend DVector3 operator + (const DVector3& lhs, const double rhs)
233  {
234  return DVector3(
235  lhs.x + rhs,
236  lhs.y + rhs,
237  lhs.z + rhs);
238  }
239 
240  inline friend DVector3 operator + (const double lhs, const DVector3& rhs)
241  {
242  return DVector3(
243  lhs + rhs.x,
244  lhs + rhs.y,
245  lhs + rhs.z);
246  }
247 
248  inline friend DVector3 operator - (const DVector3& lhs, const double rhs)
249  {
250  return DVector3(
251  lhs.x - rhs,
252  lhs.y - rhs,
253  lhs.z - rhs);
254  }
255 
256  inline friend DVector3 operator - (const double lhs, const DVector3& rhs)
257  {
258  return DVector3(
259  lhs - rhs.x,
260  lhs - rhs.y,
261  lhs - rhs.z);
262  }
263 
264  // arithmetic updates
265  inline DVector3& operator += ( const DVector3& rkVector )
266  {
267  x += rkVector.x;
268  y += rkVector.y;
269  z += rkVector.z;
270 
271  return *this;
272  }
273 
274  inline DVector3& operator += (const Vector3& rkVector)
275  {
276  x += rkVector.x;
277  y += rkVector.y;
278  z += rkVector.z;
279  return *this;
280  }
281 
282  inline DVector3& operator += ( const double fScalar )
283  {
284  x += fScalar;
285  y += fScalar;
286  z += fScalar;
287  return *this;
288  }
289 
290  inline DVector3& operator -= ( const DVector3& rkVector )
291  {
292  x -= rkVector.x;
293  y -= rkVector.y;
294  z -= rkVector.z;
295 
296  return *this;
297  }
298 
299  inline DVector3& operator -= (const Vector3& rkVector)
300  {
301  x -= rkVector.x;
302  y -= rkVector.y;
303  z -= rkVector.z;
304 
305  return *this;
306  }
307 
308  inline DVector3& operator -= ( const double fScalar )
309  {
310  x -= fScalar;
311  y -= fScalar;
312  z -= fScalar;
313  return *this;
314  }
315 
316  inline DVector3& operator *= ( const double fScalar )
317  {
318  x *= fScalar;
319  y *= fScalar;
320  z *= fScalar;
321  return *this;
322  }
323 
324  inline DVector3& operator *= ( const DVector3& rkVector )
325  {
326  x *= rkVector.x;
327  y *= rkVector.y;
328  z *= rkVector.z;
329 
330  return *this;
331  }
332 
333  inline DVector3& operator /= ( const double fScalar )
334  {
335  assert( fScalar != 0.0f );
336 
337  double fInv = 1.0f / fScalar;
338 
339  x *= fInv;
340  y *= fInv;
341  z *= fInv;
342 
343  return *this;
344  }
345 
346  inline DVector3& operator /= ( const DVector3& rkVector )
347  {
348  x /= rkVector.x;
349  y /= rkVector.y;
350  z /= rkVector.z;
351 
352  return *this;
353  }
354 
355 
363  inline double length () const
364  {
365  return Math::Sqrt( x * x + y * y + z * z );
366  }
367 
378  inline double squaredLength () const
379  {
380  return x * x + y * y + z * z;
381  }
382 
390  inline double distance(const DVector3& rhs) const
391  {
392  return (*this - rhs).length();
393  }
394 
405  inline double squaredDistance(const DVector3& rhs) const
406  {
407  return (*this - rhs).squaredLength();
408  }
409 
424  inline double dotProduct(const DVector3& vec) const
425  {
426  return x * vec.x + y * vec.y + z * vec.z;
427  }
428 
439  inline double absDotProduct(const DVector3& vec) const
440  {
441  return Math::Abs(x * vec.x) + Math::Abs(y * vec.y) + Math::Abs(z * vec.z);
442  }
443 
453  inline double normalise()
454  {
455  double fLength = Math::Sqrt( x * x + y * y + z * z );
456 
457  // Will also work for zero-sized vectors, but will change nothing
458  if ( fLength > 1e-08 )
459  {
460  double fInvLength = 1.0f / fLength;
461  x *= fInvLength;
462  y *= fInvLength;
463  z *= fInvLength;
464  }
465 
466  return fLength;
467  }
468 
497  inline DVector3 crossProduct( const DVector3& rkVector ) const
498  {
499  return DVector3(
500  y * rkVector.z - z * rkVector.y,
501  z * rkVector.x - x * rkVector.z,
502  x * rkVector.y - y * rkVector.x);
503  }
504 
508  inline DVector3 midPoint( const DVector3& vec ) const
509  {
510  return DVector3(
511  ( x + vec.x ) * 0.5f,
512  ( y + vec.y ) * 0.5f,
513  ( z + vec.z ) * 0.5f );
514  }
515 
519  inline bool operator < ( const DVector3& rhs ) const
520  {
521  if( x < rhs.x && y < rhs.y && z < rhs.z )
522  return true;
523  return false;
524  }
525 
529  inline bool operator > ( const DVector3& rhs ) const
530  {
531  if( x > rhs.x && y > rhs.y && z > rhs.z )
532  return true;
533  return false;
534  }
535 
543  inline void makeFloor( const DVector3& cmp )
544  {
545  if( cmp.x < x ) x = cmp.x;
546  if( cmp.y < y ) y = cmp.y;
547  if( cmp.z < z ) z = cmp.z;
548  }
549 
557  inline void makeCeil( const DVector3& cmp )
558  {
559  if( cmp.x > x ) x = cmp.x;
560  if( cmp.y > y ) y = cmp.y;
561  if( cmp.z > z ) z = cmp.z;
562  }
563 
565  inline bool isZeroLength(void) const
566  {
567  double sqlen = (x * x) + (y * y) + (z * z);
568  return (sqlen < (1e-06 * 1e-06));
569 
570  }
571 
574  inline DVector3 normalisedCopy(void) const
575  {
576  DVector3 ret = *this;
577  ret.normalise();
578  return ret;
579  }
580 
584  inline DVector3 reflect(const DVector3& normal) const
585  {
586  return DVector3( *this - ( 2 * this->dotProduct(normal) * normal ) );
587  }
588 
595  inline bool positionEquals(const Vector3& rhs, float tolerance = 1e-03) const
596  {
597  return Math::RealEqual((float)x, rhs.x, tolerance) &&
598  Math::RealEqual((float)y, rhs.y, tolerance) &&
599  Math::RealEqual((float)z, rhs.z, tolerance);
600  }
601 
608  inline bool positionCloses(const DVector3& rhs, float tolerance = 1e-03f) const
609  {
610  return squaredDistance(rhs) <=
611  (squaredLength() + rhs.squaredLength()) * tolerance;
612  }
613 
621  inline bool directionEquals(const DVector3& rhs,
622  const Radian& tolerance) const
623  {
624  double dot = dotProduct(rhs);
625  Radian angle = Math::ACos((float)dot);
626 
627  return Math::Abs(angle.valueRadians()) <= tolerance.valueRadians();
628  }
629 
632  inline friend std::ostream& operator <<
633  ( std::ostream& o, const DVector3& v )
634  {
635  o << "DVector3(" << v.x << ", " << v.y << ", " << v.z << ")";
636  return o;
637  }
638  };
639 
640 }
double normalise()
Normalises the vector.
Definition: ParaDVector3.h:453
Wrapper class which indicates a given angle value is in Radians.
Definition: ParaAngle.h:10
DVector3 operator%(const DVector3 &rhs) const
special cross product
Definition: ParaDVector3.h:172
void makeCeil(const DVector3 &cmp)
Sets this vector&#39;s components to the maximum of its own and the ones of the passed in vector...
Definition: ParaDVector3.h:557
3-dimensional vector with double precision.
Definition: ParaDVector3.h:17
bool positionEquals(const Vector3 &rhs, float tolerance=1e-03) const
Returns whether this vector is within a positional tolerance of another floating precision vector...
Definition: ParaDVector3.h:595
double squaredDistance(const DVector3 &rhs) const
Returns the square of the distance to another vector.
Definition: ParaDVector3.h:405
double length() const
Returns the length (magnitude) of the vector.
Definition: ParaDVector3.h:363
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: ParaDVector3.h:565
different physics engine has different winding order.
Definition: EventBinding.h:32
double * ptr()
Pointer accessor for direct copying.
Definition: ParaDVector3.h:73
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
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
bool operator<(const DVector3 &rhs) const
Returns true if the vector&#39;s scalar components are all greater that the ones of the vector it is comp...
Definition: ParaDVector3.h:519
DVector3 & operator=(const DVector3 &rkVector)
Assigns the value of the other vector.
Definition: ParaDVector3.h:95
const double * ptr() const
Pointer accessor for direct copying.
Definition: ParaDVector3.h:78
bool positionCloses(const DVector3 &rhs, float tolerance=1e-03f) const
Returns whether this vector is within a positional tolerance of another vector, also take scale of th...
Definition: ParaDVector3.h:608
double distance(const DVector3 &rhs) const
Returns the distance to another vector.
Definition: ParaDVector3.h:390
double dotProduct(const DVector3 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: ParaDVector3.h:424
void makeFloor(const DVector3 &cmp)
Sets this vector&#39;s components to the minimum of its own and the ones of the passed in vector...
Definition: ParaDVector3.h:543
DVector3 midPoint(const DVector3 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: ParaDVector3.h:508
bool directionEquals(const DVector3 &rhs, const Radian &tolerance) const
Returns whether this vector is within a directional tolerance of another vector.
Definition: ParaDVector3.h:621
DVector3 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalized vector is returned as a copy...
Definition: ParaDVector3.h:574
DVector3 reflect(const DVector3 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: ParaDVector3.h:584
bool operator>(const DVector3 &rhs) const
Returns true if the vector&#39;s scalar components are all smaller that the ones of the vector it is comp...
Definition: ParaDVector3.h:529
DVector3 crossProduct(const DVector3 &rkVector) const
Calculates the cross-product of 2 vectors, i.e.
Definition: ParaDVector3.h:497
double squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: ParaDVector3.h:378
double absDotProduct(const DVector3 &vec) const
Calculates the absolute dot (scalar) product of this vector with another.
Definition: ParaDVector3.h:439