VR-Forces 5.0.1 Developer's Guide
 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 #include <iostream>
18 
19 namespace makVrv
20 {
21 
24  template <class T>
25  class DtVector2
26  {
27  public:
29  DtVector2() : x(0), y(0) {}
31  DtVector2(T _x, T _y) : x(_x), y (_y) {}
32 
35  template <class T1>
36  DtVector2(const T1& v) : x(v.x), y(v.y) {}
37 
40  {
41  //intentional nop
42  }
43  public:
45  const void* ptr(void) const
46  {
47  return &x;
48  }
49 
51  T operator[](size_t index) const;
52 
54  T& operator[](size_t index);
55 
57  T length(void) const;
59  T squaredLength(void) const;
60 
62  bool isEqual(const DtVector2& rhs, T tolerance) const;
63 
65  DtVector2 operator+(const DtVector2& rhs) const;
67  DtVector2 operator-(const DtVector2& rhs) const;
68 
70  DtVector2& operator+=(const DtVector2& rhs);
72  DtVector2& operator-=(const DtVector2& rhs);
73 
75  DtVector2 operator+(T fScalar) const;
77  DtVector2 operator-(T fScalar) const;
79  DtVector2 operator*(T fScalar) const;
81  DtVector2 operator/(T fScalar) const;
82 
84  DtVector2 operator/(const DtVector2& rhs) const;
85 
87  void normalize(void);
88 
90  DtVector2 getNormalizedCopy(void) const;
91 
93  T getDotProduct(const DtVector2& rhs) const;
94 
96  bool operator==(const DtVector2& rhs) const;
97 
99  bool operator!=(const DtVector2& rhs) const;
100 
101  public:
102  //Size of this class in bytes
103  static size_t sizeInBytes(void);
104  public:
105  T x, y;
106 
107  public:
108  static const DtVector2 theZeroVector;
109  static const DtVector2 theUnitScale;
110  };
111 
112  template <class T>
114  template <class T>
116 
117  template <class T>
119  {
120  return DtVector2<T>(x+rhs.x, y+rhs.y);
121  }
122 
123  template <class T>
124  DtVector2<T> DtVector2<T>::operator-(const DtVector2<T>& rhs) const
125  {
126  return DtVector2<T>(x-rhs.x, y-rhs.y);
127  }
128 
129  template <class T>
131  {
132  x += rhs.x;
133  y += rhs.y;
134  return *this;
135  }
136 
137  template <class T>
139  {
140  x -= rhs.x;
141  y -= rhs.y;
142  return *this;
143  }
144 
145  template <class T>
147  {
148  return DtVector2<T>(x+fScalar, y+fScalar);
149  }
150 
151  template <class T>
153  {
154  return DtVector2<T>(x-fScalar, y-fScalar);
155  }
156 
157  template <class T>
159  {
160  return DtVector2<T>(x*fScalar, y*fScalar);
161  }
162 
163  template <class T>
165  {
166  T OneByfScalar = T(1.0)/fScalar;
167  return DtVector2<T>(x*OneByfScalar, y*OneByfScalar);
168  }
169 
170  template <class T>
172  {
173  return DtVector2<T>(x/rhs.x, y/rhs.y);
174  }
175 
176  template <class T>
177  bool DtVector2<T>::isEqual(const DtVector2<T>& rhs, T tolerance) const
178  {
179  return DtMath::isEqual(this->x, rhs.x,tolerance)
180  && DtMath::isEqual(this->y, rhs.y,tolerance)
181  ;
182  }
183 
184 
185  template <class T>
187  {
188  return x*rhs.x + y*rhs.y;
189  }
190 
191  template <class T>
193  {
194  T fLength = DtMath::Sqrt(x * x + y * y);
195  if (fLength > 0)
196  {
197  T fInvLength = 1.0f / fLength;
198  x *= fInvLength;
199  y *= fInvLength;
200  }
201  }
202 
203  template <class T>
205  {
206  T vecLength = length();
207  if (vecLength<std::numeric_limits<T>::epsilon())
209 
210  T one_by_length = T(1.0)/vecLength;
211  return (DtVector2<T>(x,y)*one_by_length);
212  }
213 
214  template <class T>
215  T DtVector2<T>::operator[](size_t index) const
216  {
217  ASSERT_PREDICATE(index <= 1);
218  const T* ptrT = static_cast<const T*>(ptr());
219  return ptrT[index];
220  }
221 
222  template <class T>
223  T& DtVector2<T>::operator[](size_t index)
224  {
225  ASSERT_PREDICATE(index <= 1);
226  T* ptrT = &x;
227  return ptrT[index];
228  }
229 
230  template <class T>
231  T DtVector2<T>::length(void) const
232  {
233  return DtMath::Sqrt(x*x+y*y);
234  }
235 
236  template <class T>
238  {
239  return x*x+y*y;
240  }
241 
242  template <class T>
244  {
245  return 2*sizeof(T);
246  }
247 
248  template <class T>
249  bool DtVector2<T>::operator==(const DtVector2<T>& rhs) const
250  {
251  return x == rhs.x&&y == rhs.y;
252  }
253 
254  template <class T>
255  bool DtVector2<T>::operator!=(const DtVector2<T>& rhs) const
256  {
257  const DtVector2<T>& lhs = *this;
258  return !(lhs == rhs);
259  }
260 
261  template<class T>
262  inline std::ostream& operator <<(std::ostream& o, const DtVector2<T>& v)
263  {
264  o << "Vector2(" << v.x << ", " << v.y << ")";
265  return o;
266  }
267 
268  // 32 bit
270  typedef std::list<Vector2_32> Vector2_32_List;
271  typedef std::vector<Vector2_32> Vector2_32_Vector;
272 
275 
276  // 64 bit
278  typedef std::list<Vector2_64> Vector2_64_List;
279  typedef std::vector<Vector2_64> Vector2_64_Vector;
280 
281 } //namespace makVrv
282 
std::list< Vector2_32 > Vector2_32_List
Definition: DtVector2.h:270
bool isEqual(const DtVector2 &rhs, T tolerance) const
is rhs == to this vector within a specified tolerance/epsilon
Definition: DtVector2.h:177
DtVector2(T _x, T _y)
CTOR.
Definition: DtVector2.h:31
DtVector2 operator*(T fScalar) const
multiply a scalar to each component
Definition: DtVector2.h:158
static size_t sizeInBytes(void)
Definition: DtVector2.h:243
T squaredLength(void) const
get the length of the vector squared
Definition: DtVector2.h:237
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:186
std::list< Vector2_64 > Vector2_64_List
Definition: DtVector2.h:278
DtVector2 getNormalizedCopy(void) const
get a normalized copy of this vector
Definition: DtVector2.h:204
DtVector2 operator-(const DtVector2 &rhs) const
component wise subtract two vectors
static const DtVector2 theZeroVector
Definition: DtVector2.h:108
static const DtVector2 theUnitScale
Definition: DtVector2.h:109
DtVector2< int32 > Vector2_int
Definition: DtVector2.h:273
static T Sqrt(T val)
Standard math routines.
Definition: DtMath.h:58
T y
Definition: DtVector2.h:105
T length(void) const
get the length of the vector
Definition: DtVector2.h:231
DtVector2(const T1 &v)
CTOR construct from type with constructable components.
Definition: DtVector2.h:36
DtVector2 & operator+=(const DtVector2 &rhs)
component wise add rhs
Definition: DtVector2.h:130
DtVector2 operator/(T fScalar) const
divide each component by a scalar
Definition: DtVector2.h:164
~DtVector2()
DTOR.
Definition: DtVector2.h:39
Math utilities.
std::vector< Vector2_64 > Vector2_64_Vector
Definition: DtVector2.h:279
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:274
bool operator==(const DtVector2 &rhs) const
equality
Definition: DtVector2.h:249
void normalize(void)
normalize this vector in place
Definition: DtVector2.h:192
DtVector2()
CTOR.
Definition: DtVector2.h:29
bool operator!=(const DtVector2 &rhs) const
inequality
Definition: DtVector2.h:255
T x
Definition: DtVector2.h:105
T operator[](size_t index) const
access a component of the vector
Definition: DtVector2.h:215
DtVector2< float32 > Vector2_32
Definition: DtVector2.h:269
#define ASSERT_PREDICATE(p)
Definition: DtAssert.h:28
assert macros
DtVector2 & operator-=(const DtVector2 &rhs)
component wise subtract rhs
Definition: DtVector2.h:138
DtVector2< float64 > Vector2_64
Definition: DtVector2.h:277
std::vector< Vector2_32 > Vector2_32_Vector
Definition: DtVector2.h:271
const void * ptr(void) const
get a pointer to the data
Definition: DtVector2.h:45
2 dimensional vector
Definition: DtMath.h:20

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)