VR-Forces 4.10 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtVector2.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 
18 namespace makVrv
19 {
20 
23  template <class T>
24  class DtVector2
25  {
26  public:
28  DtVector2() : x(0), y(0) {}
30  DtVector2(T _x, T _y) : x(_x), y (_y) {}
31 
34  template <class T1>
35  DtVector2(const T1& v) : x(v.x), y(v.y) {}
36 
39  {
40  //intentional nop
41  }
42  public:
44  const void* ptr(void) const
45  {
46  return &x;
47  }
48 
50  T operator[](size_t index) const;
51 
53  T length(void) const;
55  T squaredLength(void) const;
56 
58  bool isEqual(const DtVector2& rhs, T tolerance) const;
59 
61  DtVector2 operator+(const DtVector2& rhs) const;
63  DtVector2 operator-(const DtVector2& rhs) const;
64 
66  DtVector2& operator+=(const DtVector2& rhs);
68  DtVector2& operator-=(const DtVector2& rhs);
69 
71  DtVector2 operator+(T fScalar) const;
73  DtVector2 operator-(T fScalar) const;
75  DtVector2 operator*(T fScalar) const;
77  DtVector2 operator/(T fScalar) const;
78 
80  DtVector2 operator/(const DtVector2& rhs) const;
81 
83  void normalize(void);
84 
86  DtVector2 getNormalizedCopy(void) const;
87 
89  T getDotProduct(const DtVector2& rhs) const;
90 
92  bool operator==(const DtVector2& rhs) const;
93 
95  bool operator!=(const DtVector2& rhs) const;
96 
97  public:
98  //Size of this class in bytes
99  static size_t sizeInBytes(void);
100  public:
101  T x, y;
102 
103  public:
104  static const DtVector2 theZeroVector;
105  static const DtVector2 theUnitScale;
106  };
107 
108  template <class T>
110  template <class T>
112 
113  template <class T>
115  {
116  return DtVector2<T>(x+rhs.x, y+rhs.y);
117  }
118 
119  template <class T>
120  DtVector2<T> DtVector2<T>::operator-(const DtVector2<T>& rhs) const
121  {
122  return DtVector2<T>(x-rhs.x, y-rhs.y);
123  }
124 
125  template <class T>
127  {
128  x += rhs.x;
129  y += rhs.y;
130  return *this;
131  }
132 
133  template <class T>
135  {
136  x -= rhs.x;
137  y -= rhs.y;
138  return *this;
139  }
140 
141  template <class T>
143  {
144  return DtVector2<T>(x+fScalar, y+fScalar);
145  }
146 
147  template <class T>
149  {
150  return DtVector2<T>(x-fScalar, y-fScalar);
151  }
152 
153  template <class T>
155  {
156  return DtVector2<T>(x*fScalar, y*fScalar);
157  }
158 
159  template <class T>
161  {
162  T OneByfScalar = T(1.0)/fScalar;
163  return DtVector2<T>(x*OneByfScalar, y*OneByfScalar);
164  }
165 
166  template <class T>
168  {
169  return DtVector2<T>(x/rhs.x, y/rhs.y);
170  }
171 
172  template <class T>
173  bool DtVector2<T>::isEqual(const DtVector2<T>& rhs, T tolerance) const
174  {
175  return DtMath::isEqual(this->x, rhs.x,tolerance)
176  && DtMath::isEqual(this->y, rhs.y,tolerance)
177  ;
178  }
179 
180 
181  template <class T>
183  {
184  return x*rhs.x + y*rhs.y;
185  }
186 
187  template <class T>
189  {
190  T fLength = DtMath::Sqrt(x * x + y * y);
191  if (fLength > 0)
192  {
193  T fInvLength = 1.0f / fLength;
194  x *= fInvLength;
195  y *= fInvLength;
196  }
197  }
198 
199  template <class T>
201  {
202  T vecLength = length();
203  if (vecLength<std::numeric_limits<T>::epsilon())
205 
206  T one_by_length = T(1.0)/vecLength;
207  return (DtVector2<T>(x,y)*one_by_length);
208  }
209 
210  template <class T>
211  T DtVector2<T>::operator[](size_t index) const
212  {
214  const T* ptrT = static_cast<const T*>(ptr());
215  return ptrT[index];
216  }
217 
218  template <class T>
219  T DtVector2<T>::length(void) const
220  {
221  return DtMath::Sqrt(x*x+y*y);
222  }
223 
224  template <class T>
226  {
227  return x*x+y*y;
228  }
229 
230  template <class T>
232  {
233  return 2*sizeof(T);
234  }
235 
236  template <class T>
237  bool DtVector2<T>::operator==(const DtVector2<T>& rhs) const
238  {
239  return x == rhs.x&&y == rhs.y;
240  }
241 
242  template <class T>
243  bool DtVector2<T>::operator!=(const DtVector2<T>& rhs) const
244  {
245  const DtVector2<T>& lhs = *this;
246  return !(lhs == rhs);
247  }
248 
249  // 32 bit
251  typedef std::list<Vector2_32> Vector2_32_List;
252  typedef std::vector<Vector2_32> Vector2_32_Vector;
253 
256 
257  // 64 bit
259  typedef std::list<Vector2_64> Vector2_64_List;
260  typedef std::vector<Vector2_64> Vector2_64_Vector;
261 
262 } //namespace makVrv
263 
std::list< Vector2_32 > Vector2_32_List
Definition: DtVector2.h:251
bool isEqual(const DtVector2 &rhs, T tolerance) const
is rhs == to this vector within a specified tolerance/epsilon
Definition: DtVector2.h:173
DtVector2(T _x, T _y)
CTOR.
Definition: DtVector2.h:30
DtVector2 operator*(T fScalar) const
multiply a scalar to each component
Definition: DtVector2.h:154
static size_t sizeInBytes(void)
Definition: DtVector2.h:231
T squaredLength(void) const
get the length of the vector squared
Definition: DtVector2.h:225
DtVector2 operator+(const DtVector2 &rhs) const
component wise add two vectors together
T getDotProduct(const DtVector2 &rhs) const
get the dot product of this vector with rhs
Definition: DtVector2.h:182
std::list< Vector2_64 > Vector2_64_List
Definition: DtVector2.h:259
DtVector2 getNormalizedCopy(void) const
get a normalized copy of this vector
Definition: DtVector2.h:200
DtVector2 operator-(const DtVector2 &rhs) const
component wise subtract two vectors
static const DtVector2 theZeroVector
Definition: DtVector2.h:104
static const DtVector2 theUnitScale
Definition: DtVector2.h:105
DtVector2< int32 > Vector2_int
Definition: DtVector2.h:254
static T Sqrt(T val)
Standard math routines.
Definition: DtMath.h:58
T y
Definition: DtVector2.h:101
T length(void) const
get the length of the vector
Definition: DtVector2.h:219
DtVector2(const T1 &v)
CTOR construct from type with constructable components.
Definition: DtVector2.h:35
DtVector2 & operator+=(const DtVector2 &rhs)
component wise add rhs
Definition: DtVector2.h:126
DtVector2 operator/(T fScalar) const
divide each component by a scalar
Definition: DtVector2.h:160
~DtVector2()
DTOR.
Definition: DtVector2.h:38
Math utilities.
std::vector< Vector2_64 > Vector2_64_Vector
Definition: DtVector2.h:260
static bool isEqual(int32 lhs, int32 rhs, int32 tolerance=std::numeric_limits< int32 >::epsilon())
Test equality with tolerance.
DtVector2< uint32 > Vector2_uint32
Definition: DtVector2.h:255
bool operator==(const DtVector2 &rhs) const
equality
Definition: DtVector2.h:237
#define ASSERT_PREDICATE_RETURN_DEFAULT_CONSTRUCTOR(V, p)
Definition: DtAssert.h:45
void normalize(void)
normalize this vector in place
Definition: DtVector2.h:188
DtVector2()
CTOR.
Definition: DtVector2.h:28
bool operator!=(const DtVector2 &rhs) const
inequality
Definition: DtVector2.h:243
T x
Definition: DtVector2.h:101
T operator[](size_t index) const
access a component of the vector
Definition: DtVector2.h:211
DtVector2< float32 > Vector2_32
Definition: DtVector2.h:250
assert macros
DtVector2 & operator-=(const DtVector2 &rhs)
component wise subtract rhs
Definition: DtVector2.h:134
DtVector2< float64 > Vector2_64
Definition: DtVector2.h:258
std::vector< Vector2_32 > Vector2_32_Vector
Definition: DtVector2.h:252
const void * ptr(void) const
get a pointer to the data
Definition: DtVector2.h:44
2 dimensional vector
Definition: DtMath.h:20

Document ID: Generated on Tue Sep 21 17:42:52 EDT 2021 from SVN revision 234861
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)