VR-Forces 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 
11 #include <vrvMath/DtCommonTypes.h>
12 #include <vrvMath/DtAssert.h>
13 #include <vrvMath/DtMath.h>
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 
131  void set(T x, T y, T z);
132  public:
133  static size_t sizeInBytes(void);
134  public:
135  T x, y, z;
136  public:
137  static const DtVector3 theZeroVector;
138  static const DtVector3 theUnitScale;
139  static const DtVector3 X_AXIS;
140  static const DtVector3 Y_AXIS;
141  static const DtVector3 Z_AXIS;
142 
144  };
145 
147  typedef std::list<Vector3_32> Vector3_32_List;
148  typedef std::vector<Vector3_32> Vector3_32_Vector;
149 
150  template <class T>
152  template <class T>
154  template <class T>
156  template <class T>
158  template <class T>
160 
161  template <class T>
162  DtVector3<T> DtVector3<T>::POSITIVE_INFINITY = DtVector3<T>(std::numeric_limits<T>::max()
163  , std::numeric_limits<T>::max()
164  , std::numeric_limits<T>::max());
165 
166  template <class T>
168  {
169  T fLength = DtMath::Sqrt(x*x + y * y + z * z);
170  if (fLength > 0)
171  {
172  T fInvLength = 1.0f / fLength;
173  x *= fInvLength;
174  y *= fInvLength;
175  z *= fInvLength;
176  }
177  }
178 
179  template <class T>
180  T DtVector3<T>::length(void) const
181  {
182  return DtMath::Sqrt(x*x + y * y + z * z);
183  }
184 
185  template <class T>
187  {
188  return x * x + y * y + z * z;
189  }
190 
191 
192  template <class T>
194  {
195  return (*this - rhs).length();
196  }
197 
198  template <class T>
200  {
201  return DtVector3<T>(x + rhs.x, y + rhs.y, z + rhs.z);
202  }
203  template <class T>
204  DtVector3<T> DtVector3<T>::operator-(const DtVector3<T>& rhs) const
205  {
206  return DtVector3<T>(x - rhs.x, y - rhs.y, z - rhs.z);
207  }
208 
209  template <class T>
211  {
212  return DtVector3<T>(x + fScalar, y + fScalar, z + fScalar);
213  }
214  template <class T>
216  {
217  return DtVector3<T>(x - fScalar, y - fScalar, z - fScalar);
218  }
219 
220  template <class T>
222  {
223  return DtVector3<T>(x*fScalar, y*fScalar, z*fScalar);
224  }
225 
226  template <class T>
228  {
229  T OneByfScalar = T(1.0) / fScalar;
230  return DtVector3<T>(x*OneByfScalar, y*OneByfScalar, z*OneByfScalar);
231  }
232 
233  template <class T>
234  bool DtVector3<T>::operator>(const DtVector3<T>& rhs) const
235  {
236  return x > rhs.x && y > rhs.y && z > rhs.z;
237  }
238 
239  template <class T>
240  bool DtVector3<T>::operator<(const DtVector3<T>& rhs) const
241  {
242  return x < rhs.x && y < rhs.y && z < rhs.z;
243  }
244 
245  template <class T>
246  bool DtVector3<T>::isEqual(const DtVector3<T>& rhs, T tolerance) const
247  {
248  return DtMath::isEqual(this->x, rhs.x, tolerance)
249  && DtMath::isEqual(this->y, rhs.y, tolerance)
250  && DtMath::isEqual(this->z, rhs.z, tolerance)
251  ;
252  }
253 
254  template <class T>
256  {
257  // i j k
258  // x y z
259  // rhs.x rhs.y rhs.z
260  return DtVector3<T>(
261  y*rhs.z - z * rhs.y
262  , z*rhs.x - x * rhs.z
263  , x*rhs.y - y * rhs.x
264  );
265  }
266 
267  template <class T>
269  {
270  return x * rhs.x + y * rhs.y + z * rhs.z;
271  }
272 
273  template <class T>
275  {
276  return DtMath::Abs(x*rhs.x) + DtMath::Abs(y*rhs.y) + DtMath::Abs(z*rhs.z);
277  }
278 
279  template <class T>
281  {
282  return DtVector3<T>((x + rhs.x)*static_cast<T>(0.5)
283  , (y + rhs.y)*static_cast<T>(0.5)
284  , (z + rhs.z)*static_cast<T>(0.5)
285  );
286 
287  }
288 
289  template <class T>
290  T DtVector3<T>::operator[](size_t index) const
291  {
292  ASSERT_PREDICATE(index <= 2);
293  const T* ptrT = static_cast<const T*>(ptr());
294  return ptrT[index];
295  }
296 
297  template <class T>
298  T& DtVector3<T>::operator[](size_t index)
299  {
300  ASSERT_PREDICATE(index <= 2);
301  T* ptrT = &x;
302  return ptrT[index];
303  }
304 
305  template <class T>
307  {
308  if (rhs.x > x) x = rhs.x;
309  if (rhs.y > y) y = rhs.y;
310  if (rhs.z > z) z = rhs.z;
311  }
312  template <class T>
314  {
315  if (rhs.x < x) x = rhs.x;
316  if (rhs.y < y) y = rhs.y;
317  if (rhs.z < z) z = rhs.z;
318  }
319 
320  template <class T>
322  {
323  x += fScalar;
324  y += fScalar;
325  z += fScalar;
326  return *this;
327  }
328 
329  template <class T>
331  {
332  x -= fScalar;
333  y -= fScalar;
334  z -= fScalar;
335  return *this;
336  }
337 
338  template <class T>
340  {
341  x += rhs.x;
342  y += rhs.y;
343  z += rhs.z;
344  return *this;
345  }
346 
347  template <class T>
348  DtVector3<T>& DtVector3<T>::operator-=(const DtVector3<T>& rhs)
349  {
350  x -= rhs.x;
351  y -= rhs.y;
352  z -= rhs.z;
353  return *this;
354  }
355 
356  template <class T>
358  {
359  x *= fScalar;
360  y *= fScalar;
361  z *= fScalar;
362  return *this;
363  }
364 
365  template <class T>
367  {
368  return this->operator*=(T(1.0) / fScalar);
369  }
370 
371  template <class T>
372  bool DtVector3<T>::operator==(const DtVector3<T>& rhs) const
373  {
374  return x == rhs.x&&y == rhs.y&&z == rhs.z;
375  }
376 
377  template <class T>
378  bool DtVector3<T>::operator!=(const DtVector3<T>& rhs) const
379  {
380  const DtVector3<T>& lhs = *this;
381  return !(lhs == rhs);
382  }
383 
384  template<class T>
385  inline std::ostream& operator <<(std::ostream& o, const DtVector3<T>& v)
386  {
387  o << "Vector3(" << v.x << " " << v.y << " " << v.z << ")";
388  return o;
389  }
390 
391  template <class T>
393  {
394  return 3 * sizeof(T);
395  }
396 
397  template <class T>
398  void DtVector3<T>::set(T _x, T _y, T _z)
399  {
400  x = _x;
401  y = _y;
402  z = _z;
403  }
404 
408  typedef std::list<Vector3_32> Vector3_32_List;
409  typedef std::vector<Vector3_32> Vector3_32_Vector;
410 
411 
413  typedef std::list<Vector3_64> Vector3_64_List;
414  typedef std::vector<Vector3_64> Vector3_64_Vector;
415 
416 
417 } //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:240
void makeCeil(const DtVector3 &rhs)
Each component = max(component, rhs.component)
Definition: DtVector3.h:306
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:357
DtVector3 crossProduct(const DtVector3 &rhs) const
get the cross product of this vector and rhs
Definition: DtVector3.h:255
bool operator!=(const DtVector3 &rhs) const
inequality
Definition: DtVector3.h:378
DtVector3< float64 > Vector3_64
Definition: DtVector3.h:412
static const DtVector3 theUnitScale
Definition: DtVector3.h:138
T dotProduct(const DtVector3 &rhs) const
get the dot product of this vector and rhs
Definition: DtVector3.h:268
static const DtVector3 Z_AXIS
Definition: DtVector3.h:141
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:221
DtVector3 operator/(T fScalar) const
divide each component by a scalar
Definition: DtVector3.h:227
std::list< Vector3_32 > Vector3_32_List
Definition: DtVector3.h:147
T z
Definition: DtVector3.h:135
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:280
static const DtVector3 X_AXIS
Definition: DtVector3.h:139
T x
Definition: DtVector3.h:135
DtVector3 operator+(const DtVector3 &rhs) const
component wise add two vectors together
std::list< Vector3_64 > Vector3_64_List
Definition: DtVector3.h:413
bool operator==(const DtVector3 &rhs) const
equality
Definition: DtVector3.h:372
DtVector3< float32 > Vector3_32
Definition: DtVector3.h:146
Math utilities.
void makeFloor(const DtVector3 &rhs)
Each component = min(component, rhs.component)
Definition: DtVector3.h:313
T operator[](size_t index) const
access a component of this vector
Definition: DtVector3.h:290
static const DtVector3 Y_AXIS
Definition: DtVector3.h:140
DtVector3< int32 > Vector3_int
Definition: DtVector3.h:405
T y
Definition: DtVector3.h:135
static size_t sizeInBytes(void)
Definition: DtVector3.h:392
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:167
DtVector3< uint32 > Vector3_uint32
Definition: DtVector3.h:407
void set(T x, T y, T z)
set values
Definition: DtVector3.h:398
DtVector3 & operator/=(T fScalar)
divide each component by a scalar
Definition: DtVector3.h:366
2 dimensional vector
Definition: DtMath.h:21
std::vector< Vector3_64 > Vector3_64_Vector
Definition: DtVector3.h:414
T squaredLength(void) const
get the length squared of this vector
Definition: DtVector3.h:186
static const DtVector3 theZeroVector
Definition: DtVector3.h:137
T absDotProduct(const DtVector3 &rhs) const
get the abs dot product of this vector and rhs
Definition: DtVector3.h:274
#define ASSERT_PREDICATE(p)
Definition: DtAssert.h:28
assert macros
std::vector< Vector3_32 > Vector3_32_Vector
Definition: DtVector3.h:148
bool operator>(const DtVector3 &rhs) const
Return true if each component greater than the corresponding component of rhs.
Definition: DtVector3.h:234
T length(void) const
get the length of this vector
Definition: DtVector3.h:180
T getDistance(const DtVector3 &rhs) const
get the distance between this vector and rhs
Definition: DtVector3.h:193
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:246
static T Abs(T val)
Standard math routines.
Definition: DtMath.h:59
static DtVector3 POSITIVE_INFINITY
Definition: DtVector3.h:143
const void * ptr(void) const
Definition: DtVector3.h:47

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)