VR-Forces 4.5 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtVector3.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
11 #include "DtCommonTypes.h"
12 #include "DtAssert.h"
13 #include "DtMath.h"
14 
15 #include <list>
16 #include <vector>
17 #include <limits>
18 #include <iostream>
19 
20 namespace makVrv {
21 
24  template <class T>
25  class DtVector3
26  {
27  public:
31  DtVector3(T _x, T _y, T _z): x(_x), y (_y), z (_z){}
32 
35 
36  const void* ptr(void) const{return &x;}
37 
39  DtVector3 operator-(void) const {return DtVector3(-x,-y,-z);}
40 
41  bool isEqual(const DtVector3& rhs, T tolerance=std::numeric_limits<T>::min()) const;
42 
43  DtVector3 operator+(const DtVector3& rhs) const;
44  DtVector3 operator-(const DtVector3& rhs) const;
45 
46  DtVector3 operator+(T fScalar) const;
47  DtVector3 operator-(T fScalar) const;
48  DtVector3 operator*(T fScalar) const;
49  DtVector3 operator/(T fScalar) const;
50 
51  DtVector3& operator+=(T fScalar);
52  DtVector3& operator-=(T fScalar);
53  DtVector3& operator*=(T fScalar);
54  DtVector3& operator/=(T fScalar);
55 
57  bool operator>(const DtVector3& rhs) const;
59  bool operator<(const DtVector3& rhs) const;
60 
61  void normalize(void);
62  T length(void) const;
63  T squaredLength(void) const;
64  T getDistance(const DtVector3& rhs) const;
65 
66  DtVector3 crossProduct(const DtVector3& rhs) const;
67  T dotProduct(const DtVector3& rhs) const;
68  T absDotProduct(const DtVector3& rhs) const;
69 
70  DtVector3 midPoint(const DtVector3& rhs) const;
71 
73  void makeCeil(const DtVector3& rhs);
74 
76  void makeFloor(const DtVector3& rhs);
77 
78  T operator[](size_t index) const;
79  T& operator[](size_t index);
80 
81  T x, y, z;
82 
85  static DtVector3 X_AXIS;
86  static DtVector3 Y_AXIS;
87  static DtVector3 Z_AXIS;
88 
90 
91  static size_t sizeInBytes(void);
92  };
93 
95  typedef std::list<Vector3_32> Vector3_32_List;
96  typedef std::vector<Vector3_32> Vector3_32_Vector;
97 
98  template <class T>
100  template <class T>
102  template <class T>
104  template <class T>
106  template <class T>
108 
109  template <class T>
110  DtVector3<T> DtVector3<T>::POSITIVE_INFINITY = DtVector3<T>(std::numeric_limits<T>::max()
111  ,std::numeric_limits<T>::max()
112  ,std::numeric_limits<T>::max());
113 
114  template <class T>
116  {
117  T fLength = DtMath::Sqrt(x*x+y*y+z*z);
118  if (fLength>0)
119  {
120  T fInvLength = 1.0f/fLength;
121  x*= fInvLength;
122  y*= fInvLength;
123  z*= fInvLength;
124  }
125  }
126 
127  template <class T>
128  T DtVector3<T>::length(void) const
129  {
130  return DtMath::Sqrt(x*x+y*y+z*z);
131  }
132 
133  template <class T>
135  {
136  return x*x+y*y+z*z;
137  }
138 
139 
140  template <class T>
142  {
143  return (*this-rhs).length();
144  }
145 
146  template <class T>
148  {
149  return DtVector3<T>(x+rhs.x, y+rhs.y, z+rhs.z);
150  }
151  template <class T>
152  DtVector3<T> DtVector3<T>::operator-(const DtVector3<T>& rhs) const
153  {
154  return DtVector3<T>(x-rhs.x, y-rhs.y, z-rhs.z);
155  }
156 
157  template <class T>
159  {
160  return DtVector3<T>(x+fScalar, y+fScalar, z+fScalar);
161  }
162  template <class T>
164  {
165  return DtVector3<T>(x-fScalar, y-fScalar, z-fScalar);
166  }
167 
168  template <class T>
170  {
171  return DtVector3<T>(x*fScalar, y*fScalar, z*fScalar);
172  }
173 
174  template <class T>
176  {
177  T OneByfScalar = T(1.0)/fScalar;
178  return DtVector3<T>(x*OneByfScalar, y*OneByfScalar, z*OneByfScalar);
179  }
180 
181  template <class T>
182  bool DtVector3<T>::operator>(const DtVector3<T>& rhs) const
183  {
184  return x>rhs.x && y>rhs.y && z>rhs.z;
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>::isEqual(const DtVector3<T>& rhs, T tolerance) const
195  {
196  return DtMath::isEqual(this->x, rhs.x,tolerance)
197  && DtMath::isEqual(this->y, rhs.y,tolerance)
198  && DtMath::isEqual(this->z, rhs.z,tolerance)
199  ;
200  }
201 
202  template <class T>
204  {
205  // i j k
206  // x y z
207  // rhs.x rhs.y rhs.z
208  return DtVector3<T>(
209  y*rhs.z - z*rhs.y
210  , z*rhs.x - x*rhs.z
211  , x*rhs.y - y*rhs.x
212  );
213  }
214 
215  template <class T>
217  {
218  return x*rhs.x + y*rhs.y + z*rhs.z;
219  }
220 
221  template <class T>
223  {
224  return DtMath::Abs(x*rhs.x) + DtMath::Abs(y*rhs.y) + DtMath::Abs(z*rhs.z);
225  }
226 
227  template <class T>
229  {
230  return DtVector3<T>( (x+rhs.x)*static_cast<T>(0.5)
231  , (y+rhs.y)*static_cast<T>(0.5)
232  , (z+rhs.z)*static_cast<T>(0.5)
233  );
234 
235  }
236 
237  template <class T>
238  T DtVector3<T>::operator[](size_t index) const
239  {
241  const T* ptrT = static_cast<const T*>(ptr());
242  return ptrT[index];
243  }
244 
245  template <class T>
246  T& DtVector3<T>::operator[](size_t index)
247  {
248  ASSERT_PREDICATE(index<=2);
249  T* ptrT = &x;
250  return ptrT[index];
251  }
252 
253  template <class T>
255  {
256  if( rhs.x > x ) x = rhs.x;
257  if( rhs.y > y ) y = rhs.y;
258  if( rhs.z > z ) z = rhs.z;
259  }
260  template <class T>
262  {
263  if( rhs.x < x ) x = rhs.x;
264  if( rhs.y < y ) y = rhs.y;
265  if( rhs.z < z ) z = rhs.z;
266  }
267 
268  template <class T>
270  {
271  x += fScalar;
272  y += fScalar;
273  z += fScalar;
274  return *this;
275  }
276 
277  template <class T>
279  {
280  x -= fScalar;
281  y -= fScalar;
282  z -= fScalar;
283  return *this;
284  }
285 
286  template <class T>
288  {
289  x *= fScalar;
290  y *= fScalar;
291  z *= fScalar;
292  return *this;
293  }
294 
295  template <class T>
297  {
298  return this->operator*=(T(1.0)/fScalar);
299  }
300 
301  template<class T>
302  inline std::ostream& operator <<
303  ( std::ostream& o, const DtVector3<T>& v )
304  {
305  o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
306  return o;
307  }
308 
309  template <class T>
311  {
312  return 3*sizeof(T);
313  }
314 
315 } //namespace makVrv

Document ID: Generated on Thu Mar 23 18:54:12 EDT 2017 from SVN revision 174804
Copyright © 2005-2017 VT MÄK. All Rights Reserved (www.mak.com)