Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cVector.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cVector_H
8 #define _INC_cVector_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "MathDX.h"
14 #include "Calc.h"
15 #include "cVecT.h"
16 #include "GrayCore/include/cHeap.h"
17 
18 namespace GrayLib
19 {
20  UNITTEST2_PREDEF(cVector);
21 
23 
24  enum AXIS_TYPE
25  {
28  AXIS_X = 0,
32  AXIS_QTY
33  };
34 
35  class GRAYLIB_LINK cVector2f : public cVecT2<DVALUEDEF_t>
36  {
39 
40  typedef cVecT2<DVALUE_t> SUPER_t;
41  typedef cVector2f THIS_t;
42 
43  public:
44  cVector2f() noexcept
45  {
46  // ASSUME XMFLOAT2 is un-init
47  }
48  cVector2f(const SUPER_t& v2) noexcept
49  : cVecT2<DVALUE_t>(v2)
50  {}
51  cVector2f(DVALUE_t _x, DVALUE_t _y) noexcept
52  : cVecT2<DVALUE_t>(_x, _y)
53  {}
54 
55 #ifdef _WIN32
56  // Cast directly to the DirectX equiv type.
57  cVector2f(const XMFLOAT2& v) noexcept
58  : cVecT2<DVALUE_t>(*reinterpret_cast<const SUPER_t*>(&v))
59  {}
60 
61  inline operator XMFLOAT2&() noexcept
62  {
63  return *reinterpret_cast<XMFLOAT2*>(this);
64  }
65  inline operator const XMFLOAT2&() const noexcept
66  {
67  return *reinterpret_cast<const XMFLOAT2*>(this);
68  }
69 
70  inline operator XMFLOAT2*() noexcept
71  {
72  return (XMFLOAT2*) this;
73  }
74  inline operator const XMFLOAT2*() const noexcept
75  {
76  return (const XMFLOAT2*) this;
77  }
78 
79  const XMFLOAT4* get_X4() const noexcept
80  {
81  return (const XMFLOAT4*) this; // DANGER to SMALL!!
82  }
83 #endif
84 
85  THIS_t GetProj(const cMatrix4x4f& M) const;
86 
87  static const cVector2f k_vZero;
88  static const cVector2f k_vOne;
89  };
90 
92 
93  class GRAYLIB_LINK cVector3f : public cVecT3<DVALUEDEF_t>
94  {
100 
101  typedef cVecT3<DVALUE_t> SUPER_t;
102  typedef cVector3f THIS_t;
103 
104  public:
105 
106  static const cVector3f k_vZero;
107  static const cVector3f k_vOne;
108  static const cVector3f k_vXAxis;
109  static const cVector3f k_vYAxis;
110  static const cVector3f k_vZAxis;
111 
112  cVector3f() noexcept
113  {
114  // ASSUME XMFLOAT3 is un-init
115  }
116  cVector3f(const SUPER_t& v) noexcept
117  : cVecT3<DVALUE_t>(v)
118  {}
119  cVector3f(DVALUE_t _x, DVALUE_t _y, DVALUE_t _z) noexcept
120  : cVecT3<DVALUE_t>(_x, _y, _z)
121  {}
122 
123 #ifdef _WIN32
124  // Cast directly to the DirectX equiv type.
125  cVector3f(const XMFLOAT3& v)
126  : cVecT3<DVALUE_t>(*(const SUPER_t*)&v)
127  {}
128 
129  inline operator XMFLOAT3&()
130  {
131  return *((XMFLOAT3*)this);
132  }
133  inline operator const XMFLOAT3&() const
134  {
135  return *((const XMFLOAT3*)this);
136  }
137 
138  inline operator XMFLOAT3*()
139  {
140  return (XMFLOAT3*) this;
141  }
142  inline operator const XMFLOAT3*() const
143  {
144  return (const XMFLOAT3*) this;
145  }
146 
147  const XMFLOAT4* get_X4() const
148  {
150  return (const XMFLOAT4*) this; // DANGER to SMALL!!
151  }
152 
153  inline DVALUE_t operator[] (int i) const // should be a UINT version ??
154  {
156  return GetElem(i);
157  }
158  inline DVALUE_t& operator[] (int i) // should be a UINT version ??
159  {
161  return RefElem(i);
162  }
163 #endif
164 #ifdef USE_DXM
165  cVector3f(const XMVECTOR& v)
166  {
167  ::XMStoreFloat3((XMFLOAT3*)this, v);
168  }
169  inline void Set(const XMVECTOR& v)
170  {
171  ::XMStoreFloat3((XMFLOAT3*)this, v);
172  }
173  inline operator XMVECTOR () const
174  {
175  return ::XMLoadFloat3((const XMFLOAT3*)this);
176  }
177 #endif
178 
179  // const tests.
180 
181  DVALUE_t GetMinAxis(const THIS_t& v2, DVALUE_t fDistMin = k_FLT_MIN2) const
182  {
185  DVALUE_t fd = CalcI::Abs(x - v2.x);
186  if (fd > fDistMin)
187  return fd;
188  fd = CalcI::Abs(y - v2.y);
189  if (fd > fDistMin)
190  return fd;
191  fd = CalcI::Abs(z - v2.z);
192  if (fd > fDistMin)
193  return fd;
194  return 0;
195  }
196 
197  bool IsNearHeight(const THIS_t& v2, DVALUE_t fDist = k_FLT_MIN2) const
198  {
199  if (!CalcI::IsNear(y, v2.y, fDist))
200  return false;
201  return true;
202  }
203  bool IsNearXZ(const THIS_t& v2, DVALUE_t fDist = k_FLT_MIN2) const
204  {
205  if (!CalcI::IsNear(x, v2.x, fDist))
206  return false;
207  if (!CalcI::IsNear(z, v2.z, fDist))
208  return false;
209  return true;
210  }
211  DVALUE_t GetDistXZ(const THIS_t& v2) const
212  {
213  return Calc::Sqrt(Calc::Sqr(v2.x) + Calc::Sqr(v2.z));
214  }
215 
216  // ----------------------------- HLSL - conformal math goes there
217  // Named modifiers (e.g. normalize) here as methods only for MODIFIERS
218  // non-modifier named operators (Normalized) implemented as global funcs later
219  // that is due to better equation readability and consistency
220 
221  THIS_t GetProj(const cMatrix4x4f& M) const;
222  THIS_t GetProj2(const cMatrix4x4f& M) const;
223  THIS_t GetTransNorm(const cMatrix4x4f& M) const;
224  THIS_t GetTransNorm2(const cMatrix4x4f& M) const;
225 
226  THIS_t GetVectorToTarget(const cVector3f& vTarget) const
227  {
229  THIS_t v = vTarget - *this;
230  v.SetNormalized();
231  return v;
232  }
233 
234  DVALUE_t GetDistSeg(const THIS_t& v1, const THIS_t& v2) const;
235  RADIANf_t GetEulerHeading(const THIS_t& v2) const;
236 
237  // Change this operators.
238 
239  void SetMin()
240  {
241  m_x = -k_FLT_MAX2; m_y = -k_FLT_MAX2; m_z = -k_FLT_MAX2;
242  }
243  void SetMax()
244  {
245  m_x = k_FLT_MAX2; m_y = k_FLT_MAX2; m_z = k_FLT_MAX2;
246  }
247 
248  // aliases (most are legacy HLSL)
249 
250  void SetEulerWrapSigned();
251 
252  static RADIANf_t GRAYCALL GetVectorAngle(const THIS_t& v1, const THIS_t& v2);
253  static RADIANf_t GRAYCALL GetVectorAngleN(const THIS_t& lv1, const THIS_t& lv2); // assumed normalized
254 
255  UNITTEST2_PREDEF(cVector );
256  };
257 
259 
260  class cVector4f : public cVecT4<float>
261  {
265 
266  typedef cVector4f THIS_t;
267  typedef cVecT4<DVALUE_t> SUPER_t;
268  public:
270  {
272  }
273  cVector4f(const DVALUE_t* pVals) : SUPER_t(pVals)
274  {}
275  cVector4f(const SUPER_t& v)
276  : SUPER_t(v)
277  {}
278  cVector4f(DVALUE_t _x, DVALUE_t _y, DVALUE_t _z = 0, DVALUE_t _w = 0) noexcept
279  : SUPER_t(_x, _y, _z, _w)
280  {}
282  : SUPER_t(v, w)
283  {}
284 
285 #if defined(USE_DXM) || defined(USE_DX) // ASSUME DVALUE_t = float !!!
286  // Cast directly to the DirectX equiv type.
287  cVector4f(const XMFLOAT4& v) : SUPER_t(*(const SUPER_t*)&v)
288  {}
289 
290  inline operator XMFLOAT4&()
291  {
292  return *((XMFLOAT4*)this);
293  }
294  inline operator const XMFLOAT4&() const
295  {
296  return *((const XMFLOAT4*)this);
297  }
298  inline operator XMFLOAT4*()
299  {
300  return (XMFLOAT4*) this;
301  }
302  inline operator const XMFLOAT4*() const
303  {
304  return (const XMFLOAT4*) this;
305  }
306 #endif
307 
308 #ifdef USE_DXM
309  cVector4f(const XMVECTOR& v)
310  {
311  ::XMStoreFloat4((XMFLOAT4*)this, v);
312  }
313  inline void Set(const XMVECTOR& v)
314  {
315  ::XMStoreFloat4((XMFLOAT4*)this, v);
316  }
317  inline operator XMVECTOR () const
318  {
319  return ::XMLoadFloat4((const XMFLOAT4*)this);
320  }
321 #endif
322 
323  inline void InitCross4(const cVector4f& v1, const cVector4f& v2, const cVector4f& v3)
324  {
326 #ifdef USE_DXM
327  *this = ::XMVector4Cross((XMVECTOR)v1, (XMVECTOR)v2, (XMVECTOR)v3);
328 #else
329  SUPER_t::InitCross(v1, v2, v3);
330 #endif
331  }
332  };
333 
334 #ifdef _WIN32
335  // Freely convert from one type to another.
336  inline cVector3f& Cvt(XMFLOAT3& v)
337  {
338  return *((cVector3f*)&v);
339  }
340  inline const cVector3f& Cvt(const XMFLOAT3& v)
341  {
342  return *((const cVector3f*)&v);
343  }
344 #endif
345 
346 }
347 #endif // _INC_cVector_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cMatrix.h:194
Definition: cVecT.h:473
Definition: cVecT.h:530
TYPE y
Definition: cVecT.h:545
TYPE z
Definition: cVecT.h:545
TYPE x
Definition: cVecT.h:545
Definition: cVecT.h:663
void InitCross(const THIS_t &v1, const THIS_t &v2, const THIS_t &v3)
Definition: cVecT.h:733
float w
Definition: cVecT.h:676
DVALUEDEF_t DVALUE_t
Dimension value type.
Definition: cVecT.h:48
void Set(const THIS_t &v)
Definition: cVecT.h:322
TYPE SetNormalized(void) noexcept
Definition: cVecT.h:334
Definition: cVector.h:36
cVector2f(const SUPER_t &v2) noexcept
Definition: cVector.h:48
static const cVector2f k_vOne
Definition: cVector.h:88
cVector2f(DVALUE_t _x, DVALUE_t _y) noexcept
Definition: cVector.h:51
cVector2f() noexcept
Definition: cVector.h:44
static const cVector2f k_vZero
Definition: cVector.h:87
Definition: cVector.h:94
static const cVector3f k_vYAxis
Definition: cVector.h:109
bool IsNearXZ(const THIS_t &v2, DVALUE_t fDist=k_FLT_MIN2) const
Definition: cVector.h:203
DVALUE_t GetMinAxis(const THIS_t &v2, DVALUE_t fDistMin=k_FLT_MIN2) const
Definition: cVector.h:181
bool IsNearHeight(const THIS_t &v2, DVALUE_t fDist=k_FLT_MIN2) const
Definition: cVector.h:197
cVector3f(DVALUE_t _x, DVALUE_t _y, DVALUE_t _z) noexcept
Definition: cVector.h:119
static const cVector3f k_vXAxis
Definition: cVector.h:108
static const cVector3f k_vZero
Definition: cVector.h:106
DVALUE_t GetDistXZ(const THIS_t &v2) const
Definition: cVector.h:211
cVector3f() noexcept
Definition: cVector.h:112
void SetMin()
Definition: cVector.h:239
THIS_t GetVectorToTarget(const cVector3f &vTarget) const
Definition: cVector.h:226
static const cVector3f k_vZAxis
Definition: cVector.h:110
static const cVector3f k_vOne
Definition: cVector.h:107
UNITTEST2_PREDEF(cVector)
void SetMax()
Definition: cVector.h:243
cVector3f(const SUPER_t &v) noexcept
Definition: cVector.h:116
Definition: cVector.h:261
cVector4f(const DVALUE_t *pVals)
Definition: cVector.h:273
cVector4f(DVALUE_t _x, DVALUE_t _y, DVALUE_t _z=0, DVALUE_t _w=0) noexcept
Definition: cVector.h:278
cVector4f(const cVecT3< DVALUE_t > &v, DVALUE_t w)
Definition: cVector.h:281
cVector4f()
Definition: cVector.h:269
cVector4f(const SUPER_t &v)
Definition: cVector.h:275
void InitCross4(const cVector4f &v1, const cVector4f &v2, const cVector4f &v3)
Definition: cVector.h:323
Definition: cMesh.h:22
class __DECL_IMPORT cMatrix4x4f
Definition: cVector.h:22
UNITTEST2_PREDEF(cQuadtree)
cVecT3< double > cVector3d
Definition: cVector.h:258
cVecT2< double > cVector2d
Definition: cVector.h:91
float RADIANf_t
type is float radians
Definition: Calc.h:27
AXIS_TYPE
Definition: cVector.h:25
@ AXIS_Trans
position offset.
Definition: cVector.h:31
@ AXIS_Y
Definition: cVector.h:29
@ AXIS_X
Definition: cVector.h:28
@ AXIS_Z
Definition: cVector.h:30
@ AXIS_QTY
Definition: cVector.h:32
static TYPE Abs(TYPE a) noexcept
similar to ABS(n) macro. Does nothing for unsigned types.
static bool IsNear(TYPE n1, TYPE n2, TYPE nDiff=(TYPE) k_FLT_MIN2) noexcept
Definition: Calc.h:135
static TYPE Sqr(TYPE a) noexcept
Definition: Calc.h:141
static TYPE Sqrt(TYPE a)
Definition: Calc.h:430
Definition: MathDX.h:37
Definition: MathDX.h:58
Definition: MathDX.h:106