VR-Forces 5.0.2 Developer's Guide
 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) 2021 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  //intentional nop
43  }
44 
45  public:
46  //pointer to the data directly
47  const void* ptr(void) const
48  {
49  return &x;
50  }
51 
53  const T* typePtr(void) const { return &x; }
54 
56  DtVector3 operator-(void) const { return DtVector3(-x, -y, -z); }
57 
59  bool operator==(const DtVector3& rhs) const;
60 
62  bool operator!=(const DtVector3& rhs) const;
63 
65  bool isEqual(const DtVector3& rhs, T tolerance = std::numeric_limits<T>::min()) const;
66 
68  DtVector3 operator+(const DtVector3& rhs) const;
70  DtVector3 operator-(const DtVector3& rhs) const;
71 
73  DtVector3& operator+=(const DtVector3& rhs);
75  DtVector3& operator-=(const DtVector3& rhs);
76 
78  DtVector3 operator+(T fScalar) const;
80  DtVector3 operator-(T fScalar) const;
82  DtVector3 operator*(T fScalar) const;
84  DtVector3 operator/(T fScalar) const;
85 
87  DtVector3& operator+=(T fScalar);
89  DtVector3& operator-=(T fScalar);
91  DtVector3& operator*=(T fScalar);
93  DtVector3& operator/=(T fScalar);
94 
96  bool operator>(const DtVector3& rhs) const;
98  bool operator<(const DtVector3& rhs) const;
99 
101  void normalize(void);
103  T length(void) const;
105  T squaredLength(void) const;
107  T getDistance(const DtVector3& rhs) const;
108 
110  DtVector3 crossProduct(const DtVector3& rhs) const;
112  T dotProduct(const DtVector3& rhs) const;
114  T absDotProduct(const DtVector3& rhs) const;
115 
117  DtVector3 midPoint(const DtVector3& rhs) const;
118 
120  void makeCeil(const DtVector3& rhs);
121 
123  void makeFloor(const DtVector3& rhs);
124 
126  T operator[](size_t index) const;
128  T& operator[](size_t index);
129  public:
130  static size_t sizeInBytes(void);
131  public:
132  T x, y, z;
133  public:
134  static const DtVector3 theZeroVector;
135  static const DtVector3 theUnitScale;
136  static const DtVector3 X_AXIS;
137  static const DtVector3 Y_AXIS;
138  static const DtVector3 Z_AXIS;
139 
141  };
142 
144  typedef std::list<Vector3_32> Vector3_32_List;
145  typedef std::vector<Vector3_32> Vector3_32_Vector;
146 
147  template <class T>
149  template <class T>
151  template <class T>
153  template <class T>
155  template <class T>
157 
158  template <class T>
159  DtVector3<T> DtVector3<T>::POSITIVE_INFINITY = DtVector3<T>(std::numeric_limits<T>::max()
160  , std::numeric_limits<T>::max()
161  , std::numeric_limits<T>::max());
162 
163  template <class T>
165  {
166  T fLength = DtMath::Sqrt(x*x + y * y + z * z);
167  if (fLength > 0)
168  {
169  T fInvLength = 1.0f / fLength;
170  x *= fInvLength;
171  y *= fInvLength;
172  z *= fInvLength;
173  }
174  }
175 
176  template <class T>
177  T DtVector3<T>::length(void) const
178  {
179  return DtMath::Sqrt(x*x + y * y + z * z);
180  }
181 
182  template <class T>
184  {
185  return x * x + y * y + z * z;
186  }
187 
188 
189  template <class T>
191  {
192  return (*this - rhs).length();
193  }
194 
195  template <class T>
197  {
198  return DtVector3<T>(x + rhs.x, y + rhs.y, z + rhs.z);
199  }
200  template <class T>
201  DtVector3<T> DtVector3<T>::operator-(const DtVector3<T>& rhs) const
202  {
203  return DtVector3<T>(x - rhs.x, y - rhs.y, z - rhs.z);
204  }
205 
206  template <class T>
208  {
209  return DtVector3<T>(x + fScalar, y + fScalar, z + fScalar);
210  }
211  template <class T>
213  {
214  return DtVector3<T>(x - fScalar, y - fScalar, z - fScalar);
215  }
216 
217  template <class T>
219  {
220  return DtVector3<T>(x*fScalar, y*fScalar, z*fScalar);
221  }
222 
223  template <class T>
225  {
226  T OneByfScalar = T(1.0) / fScalar;
227  return DtVector3<T>(x*OneByfScalar, y*OneByfScalar, z*OneByfScalar);
228  }
229 
230  template <class T>
231  bool DtVector3<T>::operator>(const DtVector3<T>& rhs) const
232  {
233  return x > rhs.x && y > rhs.y && z > rhs.z;
234  }
235 
236  template <class T>
237  bool DtVector3<T>::operator<(const DtVector3<T>& rhs) const
238  {
239  return x < rhs.x && y < rhs.y && z < rhs.z;
240  }
241 
242  template <class T>
243  bool DtVector3<T>::isEqual(const DtVector3<T>& rhs, T tolerance) const
244  {
245  return DtMath::isEqual(this->x, rhs.x, tolerance)
246  && DtMath::isEqual(this->y, rhs.y, tolerance)
247  && DtMath::isEqual(this->z, rhs.z, tolerance)
248  ;
249  }
250 
251  template <class T>
253  {
254  // i j k
255  // x y z
256  // rhs.x rhs.y rhs.z
257  return DtVector3<T>(
258  y*rhs.z - z * rhs.y
259  , z*rhs.x - x * rhs.z
260  , x*rhs.y - y * rhs.x
261  );
262  }
263 
264  template <class T>
266  {
267  return x * rhs.x + y * rhs.y + z * rhs.z;
268  }
269 
270  template <class T>
272  {
273  return DtMath::Abs(x*rhs.x) + DtMath::Abs(y*rhs.y) + DtMath::Abs(z*rhs.z);
274  }
275 
276  template <class T>
278  {
279  return DtVector3<T>((x + rhs.x)*static_cast<T>(0.5)
280  , (y + rhs.y)*static_cast<T>(0.5)
281  , (z + rhs.z)*static_cast<T>(0.5)
282  );
283 
284  }
285 
286  template <class T>
287  T DtVector3<T>::operator[](size_t index) const
288  {
289  ASSERT_PREDICATE(index <= 2);
290  const T* ptrT = static_cast<const T*>(ptr());
291  return ptrT[index];
292  }
293 
294  template <class T>
295  T& DtVector3<T>::operator[](size_t index)
296  {
297  ASSERT_PREDICATE(index <= 2);
298  T* ptrT = &x;
299  return ptrT[index];
300  }
301 
302  template <class T>
304  {
305  if (rhs.x > x) x = rhs.x;
306  if (rhs.y > y) y = rhs.y;
307  if (rhs.z > z) z = rhs.z;
308  }
309  template <class T>
311  {
312  if (rhs.x < x) x = rhs.x;
313  if (rhs.y < y) y = rhs.y;
314  if (rhs.z < z) z = rhs.z;
315  }
316 
317  template <class T>
319  {
320  x += fScalar;
321  y += fScalar;
322  z += fScalar;
323  return *this;
324  }
325 
326  template <class T>
328  {
329  x -= fScalar;
330  y -= fScalar;
331  z -= fScalar;
332  return *this;
333  }
334 
335  template <class T>
337  {
338  x += rhs.x;
339  y += rhs.y;
340  z += rhs.z;
341  return *this;
342  }
343 
344  template <class T>
345  DtVector3<T>& DtVector3<T>::operator-=(const DtVector3<T>& rhs)
346  {
347  x -= rhs.x;
348  y -= rhs.y;
349  z -= rhs.z;
350  return *this;
351  }
352 
353  template <class T>
355  {
356  x *= fScalar;
357  y *= fScalar;
358  z *= fScalar;
359  return *this;
360  }
361 
362  template <class T>
364  {
365  return this->operator*=(T(1.0) / fScalar);
366  }
367 
368  template <class T>
369  bool DtVector3<T>::operator==(const DtVector3<T>& rhs) const
370  {
371  return x == rhs.x&&y == rhs.y&&z == rhs.z;
372  }
373 
374  template <class T>
375  bool DtVector3<T>::operator!=(const DtVector3<T>& rhs) const
376  {
377  const DtVector3<T>& lhs = *this;
378  return !(lhs == rhs);
379  }
380 
381  template<class T>
382  inline std::ostream& operator <<(std::ostream& o, const DtVector3<T>& v)
383  {
384  o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
385  return o;
386  }
387 
388  template <class T>
390  {
391  return 3 * sizeof(T);
392  }
393 
397  typedef std::list<Vector3_32> Vector3_32_List;
398  typedef std::vector<Vector3_32> Vector3_32_Vector;
399 
400 
402  typedef std::list<Vector3_64> Vector3_64_List;
403  typedef std::vector<Vector3_64> Vector3_64_Vector;
404 
405 
406 } //namespace makVrv
DtVector3 & operator-=(const DtVector3 &rhs)
component wise subtract rhs
bool operator<(const DtVector3 &rhs) const
Return true if each component lessser than the corresponding component of rhs.
Definition: DtVector3.h:237
void makeCeil(const DtVector3 &rhs)
Each component = max(component, rhs.component)
Definition: DtVector3.h:303
DtVector3(T _x, T _y, T _z)
CTOR.
Definition: DtVector3.h:32
DtVector3()
CTOR.
Definition: DtVector3.h:30
DtVector3 & operator+=(const DtVector3 &rhs)
component wise add rhs
DtVector3 & operator*=(T fScalar)
multiply a scalar to each component
Definition: DtVector3.h:354
DtVector3 crossProduct(const DtVector3 &rhs) const
get the cross product of this vector and rhs
Definition: DtVector3.h:252
bool operator!=(const DtVector3 &rhs) const
inequality
Definition: DtVector3.h:375
DtVector3< float64 > Vector3_64
Definition: DtVector3.h:401
static const DtVector3 theUnitScale
Definition: DtVector3.h:135
T dotProduct(const DtVector3 &rhs) const
get the dot product of this vector and rhs
Definition: DtVector3.h:265
static const DtVector3 Z_AXIS
Definition: DtVector3.h:138
static T Sqrt(T val)
Standard math routines.
Definition: DtMath.h:58
DtVector3 operator*(T fScalar) const
multiply a scalar to each component
Definition: DtVector3.h:218
DtVector3 operator/(T fScalar) const
divide each component by a scalar
Definition: DtVector3.h:224
std::list< Vector3_32 > Vector3_32_List
Definition: DtVector3.h:144
T z
Definition: DtVector3.h:132
const T * typePtr(void) const
access the pointer to the data, with the type of the data
Definition: DtVector3.h:53
DtVector3 midPoint(const DtVector3 &rhs) const
get the midpoint between this vector and rhs
Definition: DtVector3.h:277
static const DtVector3 X_AXIS
Definition: DtVector3.h:136
T x
Definition: DtVector3.h:132
DtVector3 operator+(const DtVector3 &rhs) const
component wise add two vectors together
std::list< Vector3_64 > Vector3_64_List
Definition: DtVector3.h:402
bool operator==(const DtVector3 &rhs) const
equality
Definition: DtVector3.h:369
DtVector3< float32 > Vector3_32
Definition: DtVector3.h:143
Math utilities.
void makeFloor(const DtVector3 &rhs)
Each component = min(component, rhs.component)
Definition: DtVector3.h:310
T operator[](size_t index) const
access a component of this vector
Definition: DtVector3.h:287
static const DtVector3 Y_AXIS
Definition: DtVector3.h:137
DtVector3< int32 > Vector3_int
Definition: DtVector3.h:394
T y
Definition: DtVector3.h:132
static size_t sizeInBytes(void)
Definition: DtVector3.h:389
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)
normalize this vector in place
Definition: DtVector3.h:164
DtVector3< uint32 > Vector3_uint32
Definition: DtVector3.h:396
DtVector3 & operator/=(T fScalar)
divide each component by a scalar
Definition: DtVector3.h:363
2 dimensional vector
Definition: DtMath.h:21
std::vector< Vector3_64 > Vector3_64_Vector
Definition: DtVector3.h:403
T squaredLength(void) const
get the length squared of this vector
Definition: DtVector3.h:183
static const DtVector3 theZeroVector
Definition: DtVector3.h:134
T absDotProduct(const DtVector3 &rhs) const
get the abs dot product of this vector and rhs
Definition: DtVector3.h:271
#define ASSERT_PREDICATE(p)
Definition: DtAssert.h:28
assert macros
std::vector< Vector3_32 > Vector3_32_Vector
Definition: DtVector3.h:145
bool operator>(const DtVector3 &rhs) const
Return true if each component greater than the corresponding component of rhs.
Definition: DtVector3.h:231
T length(void) const
get the length of this vector
Definition: DtVector3.h:177
T getDistance(const DtVector3 &rhs) const
get the distance between this vector and rhs
Definition: DtVector3.h:190
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:56
bool isEqual(const DtVector3 &rhs, T tolerance=std::numeric_limits< T >::min()) const
is rhs == to this vector within a specified tolerance/epsilon
Definition: DtVector3.h:243
static T Abs(T val)
Standard math routines.
Definition: DtMath.h:59
static DtVector3 POSITIVE_INFINITY
Definition: DtVector3.h:140
const void * ptr(void) const
Definition: DtVector3.h:47

Document ID: Generated on Sun Dec 4 20:22:03 EST 2022 from SVN revision 249613
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)