VR-Forces 4.8 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtVector3.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
14 
15 #include <list>
16 #include <vector>
17 #include <limits>
18 #include <iostream>
19 
20 namespace makVrv
21 {
22 
25  template <class T>
26  class DtVector3
27  {
28  public:
30  DtVector3() : x(0), y(0), z(0) {}
32  DtVector3(T _x, T _y, T _z) : x(_x), y (_y), z (_z) {}
33 
36  template <class T1>
37  DtVector3(const T1& v) : x(v.x), y(v.y), z(v.z) {}
38 
41 
42  const void* ptr(void) const{return &x;}
43 
45  DtVector3 operator-(void) const {return DtVector3(-x,-y,-z);}
46 
47  bool isEqual(const DtVector3& rhs, T tolerance=std::numeric_limits<T>::min()) const;
48 
49  DtVector3 operator+(const DtVector3& rhs) const;
50  DtVector3 operator-(const DtVector3& rhs) const;
51 
52  DtVector3 operator+(T fScalar) const;
53  DtVector3 operator-(T fScalar) const;
54  DtVector3 operator*(T fScalar) const;
55  DtVector3 operator/(T fScalar) const;
56 
57  DtVector3& operator+=(T fScalar);
58  DtVector3& operator-=(T fScalar);
59  DtVector3& operator*=(T fScalar);
60  DtVector3& operator/=(T fScalar);
61 
63  bool operator>(const DtVector3& rhs) const;
65  bool operator<(const DtVector3& rhs) const;
66 
67  void normalize(void);
68  T length(void) const;
69  T squaredLength(void) const;
70  T getDistance(const DtVector3& rhs) const;
71 
72  DtVector3 crossProduct(const DtVector3& rhs) const;
73  T dotProduct(const DtVector3& rhs) const;
74  T absDotProduct(const DtVector3& rhs) const;
75 
76  DtVector3 midPoint(const DtVector3& rhs) const;
77 
79  void makeCeil(const DtVector3& rhs);
80 
82  void makeFloor(const DtVector3& rhs);
83 
84  T operator[](size_t index) const;
85  T& operator[](size_t index);
86 
87  T x, y, z;
88 
91  static DtVector3 X_AXIS;
92  static DtVector3 Y_AXIS;
93  static DtVector3 Z_AXIS;
94 
96 
97  static size_t sizeInBytes(void);
98  };
99 
101  typedef std::list<Vector3_32> Vector3_32_List;
102  typedef std::vector<Vector3_32> Vector3_32_Vector;
103 
104  template <class T>
106  template <class T>
108  template <class T>
110  template <class T>
112  template <class T>
114 
115  template <class T>
116  DtVector3<T> DtVector3<T>::POSITIVE_INFINITY = DtVector3<T>(std::numeric_limits<T>::max()
117  ,std::numeric_limits<T>::max()
118  ,std::numeric_limits<T>::max());
119 
120  template <class T>
122  {
123  T fLength = DtMath::Sqrt(x*x+y*y+z*z);
124  if (fLength>0)
125  {
126  T fInvLength = 1.0f/fLength;
127  x*= fInvLength;
128  y*= fInvLength;
129  z*= fInvLength;
130  }
131  }
132 
133  template <class T>
134  T DtVector3<T>::length(void) const
135  {
136  return DtMath::Sqrt(x*x+y*y+z*z);
137  }
138 
139  template <class T>
141  {
142  return x*x+y*y+z*z;
143  }
144 
145 
146  template <class T>
148  {
149  return (*this-rhs).length();
150  }
151 
152  template <class T>
154  {
155  return DtVector3<T>(x+rhs.x, y+rhs.y, z+rhs.z);
156  }
157  template <class T>
158  DtVector3<T> DtVector3<T>::operator-(const DtVector3<T>& rhs) const
159  {
160  return DtVector3<T>(x-rhs.x, y-rhs.y, z-rhs.z);
161  }
162 
163  template <class T>
165  {
166  return DtVector3<T>(x+fScalar, y+fScalar, z+fScalar);
167  }
168  template <class T>
170  {
171  return DtVector3<T>(x-fScalar, y-fScalar, z-fScalar);
172  }
173 
174  template <class T>
176  {
177  return DtVector3<T>(x*fScalar, y*fScalar, z*fScalar);
178  }
179 
180  template <class T>
182  {
183  T OneByfScalar = T(1.0)/fScalar;
184  return DtVector3<T>(x*OneByfScalar, y*OneByfScalar, z*OneByfScalar);
185  }
186 
187  template <class T>
188  bool DtVector3<T>::operator>(const DtVector3<T>& rhs) const
189  {
190  return x>rhs.x && y>rhs.y && z>rhs.z;
191  }
192 
193  template <class T>
194  bool DtVector3<T>::operator<(const DtVector3<T>& rhs) const
195  {
196  return x<rhs.x && y<rhs.y && z<rhs.z;
197  }
198 
199  template <class T>
200  bool DtVector3<T>::isEqual(const DtVector3<T>& rhs, T tolerance) const
201  {
202  return DtMath::isEqual(this->x, rhs.x,tolerance)
203  && DtMath::isEqual(this->y, rhs.y,tolerance)
204  && DtMath::isEqual(this->z, rhs.z,tolerance)
205  ;
206  }
207 
208  template <class T>
210  {
211  // i j k
212  // x y z
213  // rhs.x rhs.y rhs.z
214  return DtVector3<T>(
215  y*rhs.z - z*rhs.y
216  , z*rhs.x - x*rhs.z
217  , x*rhs.y - y*rhs.x
218  );
219  }
220 
221  template <class T>
223  {
224  return x*rhs.x + y*rhs.y + z*rhs.z;
225  }
226 
227  template <class T>
229  {
230  return DtMath::Abs(x*rhs.x) + DtMath::Abs(y*rhs.y) + DtMath::Abs(z*rhs.z);
231  }
232 
233  template <class T>
235  {
236  return DtVector3<T>( (x+rhs.x)*static_cast<T>(0.5)
237  , (y+rhs.y)*static_cast<T>(0.5)
238  , (z+rhs.z)*static_cast<T>(0.5)
239  );
240 
241  }
242 
243  template <class T>
244  T DtVector3<T>::operator[](size_t index) const
245  {
247  const T* ptrT = static_cast<const T*>(ptr());
248  return ptrT[index];
249  }
250 
251  template <class T>
252  T& DtVector3<T>::operator[](size_t index)
253  {
254  ASSERT_PREDICATE(index<=2);
255  T* ptrT = &x;
256  return ptrT[index];
257  }
258 
259  template <class T>
261  {
262  if( rhs.x > x ) x = rhs.x;
263  if( rhs.y > y ) y = rhs.y;
264  if( rhs.z > z ) z = rhs.z;
265  }
266  template <class T>
268  {
269  if( rhs.x < x ) x = rhs.x;
270  if( rhs.y < y ) y = rhs.y;
271  if( rhs.z < z ) z = rhs.z;
272  }
273 
274  template <class T>
276  {
277  x += fScalar;
278  y += fScalar;
279  z += fScalar;
280  return *this;
281  }
282 
283  template <class T>
285  {
286  x -= fScalar;
287  y -= fScalar;
288  z -= fScalar;
289  return *this;
290  }
291 
292  template <class T>
294  {
295  x *= fScalar;
296  y *= fScalar;
297  z *= fScalar;
298  return *this;
299  }
300 
301  template <class T>
303  {
304  return this->operator*=(T(1.0)/fScalar);
305  }
306 
307  template<class T>
308  inline std::ostream& operator <<( std::ostream& o, const DtVector3<T>& v )
309  {
310  o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
311  return o;
312  }
313 
314  template <class T>
316  {
317  return 3*sizeof(T);
318  }
319 
320 } //namespace makVrv
bool operator<(const DtVector3 &rhs) const
Return true if each component lessser than the corresponding component of rhs.
Definition: DtVector3.h:194
void makeCeil(const DtVector3 &rhs)
Each component = max(component, rhs.component)
Definition: DtVector3.h:260
DtVector3(T _x, T _y, T _z)
CTOR.
Definition: DtVector3.h:32
DtVector3 & operator-=(T fScalar)
Definition: DtVector3.h:284
DtVector3()
CTOR.
Definition: DtVector3.h:30
DtVector3 & operator*=(T fScalar)
Definition: DtVector3.h:293
DtVector3 crossProduct(const DtVector3 &rhs) const
Definition: DtVector3.h:209
T dotProduct(const DtVector3 &rhs) const
Definition: DtVector3.h:222
static DtVector3 X_AXIS
Definition: DtVector3.h:91
static T Sqrt(T val)
Standard math routines.
Definition: DtMath.h:54
DtVector3 operator*(T fScalar) const
Definition: DtVector3.h:175
DtVector3 operator/(T fScalar) const
Definition: DtVector3.h:181
std::list< Vector3_32 > Vector3_32_List
Definition: DtVector3.h:101
T z
Definition: DtVector3.h:87
DtVector3 midPoint(const DtVector3 &rhs) const
Definition: DtVector3.h:234
T x
Definition: DtVector3.h:87
DtVector3 operator+(const DtVector3 &rhs) const
static DtVector3 ZERO_VECTOR
Definition: DtVector3.h:89
DtVector3< float32 > Vector3_32
Definition: DtVector3.h:100
Math utilities.
static DtVector3 UNIT_SCALE
Definition: DtVector3.h:90
void makeFloor(const DtVector3 &rhs)
Each component = min(component, rhs.component)
Definition: DtVector3.h:267
T operator[](size_t index) const
Definition: DtVector3.h:244
static DtVector3 Z_AXIS
Definition: DtVector3.h:93
T y
Definition: DtVector3.h:87
static size_t sizeInBytes(void)
Definition: DtVector3.h:315
static DtVector3 Y_AXIS
Definition: DtVector3.h:92
static bool isEqual(int32 lhs, int32 rhs, int32 tolerance=std::numeric_limits< int32 >::epsilon())
Test equality with tolerance.
~DtVector3()
DTOR.
Definition: DtVector3.h:40
void normalize(void)
Definition: DtVector3.h:121
#define ASSERT_PREDICATE_RETURN_DEFAULT_CONSTRUCTOR(V, p)
Definition: DtAssert.h:45
DtVector3 & operator/=(T fScalar)
Definition: DtVector3.h:302
2 dimensional vector
Definition: DtVector3.h:26
T squaredLength(void) const
Definition: DtVector3.h:140
T absDotProduct(const DtVector3 &rhs) const
Definition: DtVector3.h:228
#define ASSERT_PREDICATE(p)
Definition: DtAssert.h:28
assert macros
std::vector< Vector3_32 > Vector3_32_Vector
Definition: DtVector3.h:102
DtVector3 & operator+=(T fScalar)
Definition: DtVector3.h:275
bool operator>(const DtVector3 &rhs) const
Return true if each component greater than the corresponding component of rhs.
Definition: DtVector3.h:188
T length(void) const
Definition: DtVector3.h:134
T getDistance(const DtVector3 &rhs) const
Definition: DtVector3.h:147
DtVector3(const T1 &v)
CTOR construct from type with constructable components.
Definition: DtVector3.h:37
DtVector3 operator-(void) const
Negate the vector.
Definition: DtVector3.h:45
bool isEqual(const DtVector3 &rhs, T tolerance=std::numeric_limits< T >::min()) const
Definition: DtVector3.h:200
static T Abs(T val)
Definition: DtMath.h:55
static DtVector3 POSITIVE_INFINITY
Definition: DtVector3.h:95
const void * ptr(void) const
Definition: DtVector3.h:42

Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)