HatchitMath
ht_math.h
1 
20 #pragma once
21 
22 #include <ht_intrin.h>
23 #include <ht_malloc.h>
24 #include <cstdint>
25 #include <cmath>
26 #include <sstream>
27 #include <cstring>
28 
29 #ifdef _WIN32
30  //Visual C++ compiler warning C4251 disable
31  #ifdef _MSC_VER
32  #pragma warning(disable : 4251)
33  #endif
34 
35  #ifndef _MM_CALLCONV
36  #define _MM_CALLCONV __vectorcall
37  #endif
38 #else //Linux and MAC OSX
39  #if __GNUC__ >= 4
40  //GCC 4 has unique keywords for showing/hiding symbols
41  //the same keyword is used for both import and export
42  #define HT_API __attribute__((__visibility__("default")))
43 
44  //Define MSVC compatible __forceinline keyword
45  //for use with GCC compiler.
46  #ifndef __forceinline
47  #define __forceinline __attribute__((always_inline))
48  #endif
49 
50  #ifndef _MM_CALLCONV
51  #define _MM_CALLCONV
52  #endif
53  #endif
54 #endif
55 
56 namespace Hatchit {
57 
58  namespace Math {
59 
60  constexpr float Pi = 3.1415926535897932384626433832795f;
61  constexpr float HalfPi = Pi * .5f;
62  constexpr float QuarterPi = Pi * .25f;
63  constexpr float TwoPi = Pi * 2.0f;
64 
65  class Vector2;
66  class Vector3;
67  class Vector4;
68  class Matrix4;
69 
70  struct Float2
71  {
72  union
73  {
74  struct
75  {
76  float x;
77  float y;
78  };
79  float m_data[2];
80  };
81 
82  Float2() = default;
83  Float2(float _x, float _y) : x(_x), y(_y) {}
84  explicit Float2(const float *pArray) : x(pArray[0]), y(pArray[1]) {}
85  };
86 
87  struct Float3
88  {
89  union
90  {
91  struct
92  {
93  float x;
94  float y;
95  float z;
96  };
97  float m_data[3];
98  };
99 
100 
101  Float3() = default;
102  Float3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
103  explicit Float3(const float *pArray) : x(pArray[0]), y(pArray[1]), z(pArray[2]) {}
104  };
105 
106  struct Float4
107  {
108  union
109  {
110  struct
111  {
112  float x;
113  float y;
114  float z;
115  float w;
116  };
117  float m_data[4];
118  };
119 
120  Float4() = default;
121  Float4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w){}
122  explicit Float4(const float *pArray) : x(pArray[0]), y(pArray[1]), z(pArray[2]), w(pArray[3]) {}
123  };
124 
125  struct Float16
126  {
127  union
128  {
129  struct
130  {
131  float xx, xy, xz, xw;
132  float yx, yy, yz, yw;
133  float zx, zy, zz, zw;
134  float wx, wy, wz, ww;
135  };
136  float m_data[16];
137  };
138 
139  Float16() = default;
140  Float16(float _xx, float _xy, float _xz, float _xw,
141  float _yx, float _yy, float _yz, float _yw,
142  float _zx, float _zy, float _zz, float _zw,
143  float _wx, float _wy, float _wz, float _ww)
144  : xx(_xx), xy(_xy), xz(_xz), xw(_xw),
145  yx(_yx), yy(_yy), yz(_yz), yw(_yw),
146  zx(_zx), zy(_zy), zz(_zz), zw(_zw),
147  wx(_wx), wy(_wy), wz(_wz), ww(_ww) {}
148 
149  explicit Float16(const float* _array)
150  {
151  memcpy(m_data, _array, sizeof(float) * 16);
152  }
153  };
154 
155  class Matrix4
156  {
157  public:
158  /****************************************************
159  * Constructors
160  *****************************************************/
161 
162  Matrix4();
163  Matrix4(const float rawArray[]);
164  Matrix4(float xx, float xy, float xz, float xw,
165  float yx, float yy, float yz, float yw,
166  float zx, float zy, float zz, float zw,
167  float wx, float wy, float wz, float ww);
168  Matrix4(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d);
169  Matrix4(const Vector4& a, const Vector4& b, const Vector4& c, const Vector4& d);
170 
171  /****************************************************
172  * Operators
173  *****************************************************/
174 
175  Matrix4 operator* (const Matrix4& mat) const;
176  //matrix4 vector3 multiplication has been removed for pressing cerimonial reasons.
177  //Vector3 operator* (const Vector3& vec) const;
178  Vector4 operator* (const Vector4& vec) const;
179  float* operator[] (int row);
180 
181  Float16 ToFloat16() const;
182 
183  public:
184  union
185  {
186  __m128 m_rows[4];
187  struct
188  {
189  float xx, xy, xz, xw,
190  yx, yy, yz, yw,
191  zx, zy, zz, zw,
192  wx, wy, wz, ww;
193  };
194  float m_data[16];
195  };
196  };
197  std::ostream& operator<< (std::ostream& output, Matrix4& h);
198 
200  // Vector2 definition
202 
203  class _MM_ALIGN16 Vector2
204  {
205  public:
206  /****************************************************
207  * Constructors
208  *****************************************************/
209  Vector2();
210  Vector2(const float rawArray[]);
211  Vector2(float xy);
212  Vector2(float x, float y);
213  Vector2(const Vector2& other);
214  explicit Vector2(const __m128& vector);
215  explicit Vector2(__m128&& vector);
216 
217  /****************************************************
218  * Custom allocation/deallocation
219  *****************************************************/
220  void* operator new(size_t _size);
221  void operator delete(void* p);
222  void* operator new[](size_t size);
223  void operator delete[](void* p);
224 
225  /****************************************************
226  * Operators
227  *****************************************************/
228 
229  explicit operator __m128(void) const;
230  Vector2 operator+ (float s) const;
231  Vector2 operator- (float s) const;
232  Vector2 operator* (float s) const;
233  Vector2 operator/ (float s) const;
234  Vector2& operator+= (float s);
235  Vector2& operator-= (float s);
236  Vector2& operator*= (float s);
237  Vector2& operator/= (float s);
238  Vector2 operator+ (const Vector2& u) const;
239  Vector2 operator- (const Vector2& u) const;
240  Vector2 operator* (const Vector2& u) const;
241  Vector2 operator/ (const Vector2& u) const;
242  Vector2& operator+= (const Vector2& u);
243  Vector2& operator-= (const Vector2& u);
244  Vector2& operator*= (const Vector2& u);
245  Vector2& operator/= (const Vector2& u);
246 
247  bool operator== (const Vector2& u) const;
248  bool operator!= (const Vector2& u) const;
249  const float& operator[] (size_t i) const;
250  float& operator[] (size_t i);
251 
252  static float Dot(const Vector2& v, const Vector2& u);
253  static float Angle(const Vector2& v, const Vector2& u);
254  static float Distance(const Vector2& v, const Vector2& u);
255  float MagnitudeSquared() const;
256  float Magnitude() const;
257  Vector2 Normalized() const;
258  Vector2 Normalize();
259 
260  Float2 ToFloat2() const;
261 
262  public:
263  union
264  {
265  __m128 m_vector;
266  struct
267  {
268  float x, y;
269  };
270  float m_data[2];
271  };
272  };
273 
274  std::ostream& operator<< (std::ostream& output, const Vector2& v);
275 
277  // Vector3 definition
279 
280  class _MM_ALIGN16 Vector3
281  {
282  friend class Matrix4;
283  public:
284 
285  /****************************************************
286  * Constructors
287  *****************************************************/
288  Vector3();
289  Vector3(const float rawArray[]);
290  Vector3(float xyz);
291  Vector3(const Vector2& xy, float z);
292  Vector3(float x, float y, float z);
293  Vector3(const Vector3& other);
294 
295  /****************************************************
296  * Custom allocation/deallocation
297  *****************************************************/
298  void* operator new(size_t _size);
299  void operator delete(void* p);
300 
301  /****************************************************
302  * Operators
303  *****************************************************/
304 
305  explicit operator const __m128(void) const;
306  Vector3 operator+ (float s) const;
307  Vector3 operator- (float s) const;
308  Vector3 operator* (float s) const;
309  Vector3 operator/ (float s) const;
310  Vector3 operator+= (float s);
311  Vector3 operator-= (float s);
312  Vector3 operator*= (float s);
313  Vector3 operator/= (float s);
314  Vector3 operator+ (const Vector3& u) const;
315  Vector3 operator- (const Vector3& u) const;
316  Vector3 operator* (const Vector3& u) const;
317  Vector3 operator/ (const Vector3& u) const;
318  Vector3 operator+= (const Vector3& u);
319  Vector3 operator-= (const Vector3& u);
320  Vector3 operator*= (const Vector3& u);
321  Vector3 operator/= (const Vector3& u);
322 
323  bool operator== (const Vector3& u) const;
324  bool operator!= (const Vector3& u) const;
325  const float& operator[] (int i) const;
326  float& operator[] (int i);
327  operator Vector2() const;
328 
329  static Vector3 Cross(const Vector3& v, const Vector3& u);
330  static float Dot(const Vector3& v, const Vector3& u);
331  static float Angle(const Vector3& v, const Vector3& u);
332  static float Distance(const Vector3& v, const Vector3& u);
333  float MagnitudeSquared() const;
334  float Magnitude() const;
335  Vector3 Normalized() const;
336  Vector3 Normalize();
337 
338  Float3 ToFloat3() const;
339 
340  public:
341  union
342  {
343  struct
344  {
345  float x, y, z;
346  };
347  float m_data[3];
348  __m128 m_vector;
349  };
350  };
351 
352  std::ostream& operator<< (std::ostream& output, Vector3& h);
353 
355  // MM Vector4 Definition
357 
358  class _MM_ALIGN16 Vector4
359  {
360  public:
361 
362  /****************************************************
363  * Operators
364  *****************************************************/
365 
366  Vector4();
367  Vector4(const float rawArray[]);
368  Vector4(float xyzw);
369  Vector4(float x, float y, float z, float w);
370  Vector4(const Vector2& xy, float z, float w);
371  Vector4(const Vector3& xyz, float w);
372  Vector4(const Vector4& other);
373  explicit Vector4(__m128 v);
374 
375  /****************************************************
376  * Custom allocation/deallocation
377  *****************************************************/
378  void* operator new(size_t _size);
379  void operator delete(void* p);
380 
381 
382  /****************************************************
383  * Operators
384  *****************************************************/
385  explicit operator __m128(void) const;
386  Vector4 operator+ (float s) const;
387  Vector4 operator- (float s) const;
388  Vector4 operator* (float s) const;
389  Vector4 operator/ (float s) const;
390 
391  Vector4& operator+= (float s);
392  Vector4& operator-= (float s);
393  Vector4& operator*= (float s);
394  Vector4& operator/= (float s);
395 
396  bool operator== (const Vector4& u) const;
397  bool operator!= (const Vector4& u) const;
398 
399  Vector4 operator+ (const Vector4& u) const;
400  Vector4 operator- (const Vector4& u) const;
401  Vector4 operator* (const Vector4& u) const;
402  Vector4 operator/ (const Vector4& u) const;
403 
404  Vector4& operator+= (const Vector4& u);
405  Vector4& operator-= (const Vector4& u);
406  Vector4& operator*= (const Vector4& u);
407  Vector4& operator/= (const Vector4& u);
408 
409  float& operator[] (size_t i);
410  const float& operator[] (size_t i) const;
411  operator Vector3() const;
412  operator Vector2() const;
413 
414  /****************************************************
415  * Static Functions
416  *****************************************************/
417  static float Dot(const Vector4& v, const Vector4& u);
418  Vector4 Normalized() const;
419  Vector4 NormalizedEst() const;
420  float Magnitude() const;
421  float MagnitudeSqr() const;
422 
423  Float4 ToFloat4() const;
424 
425  public:
426  union
427  {
428  __m128 m_vector;
429  struct
430  {
431  float x, y, z, w;
432  };
433  float m_data[4];
434  };
435  };
436  std::ostream& operator<< (std::ostream& output, Vector4& h);
437 
439  // MM Quaternion Definition
441 
442  class _MM_ALIGN16 Quaternion
443  {
444  public:
445 
446  /****************************************************
447  * Constructors
448  *****************************************************/
449 
450  Quaternion();
451  Quaternion(const float rawArray[]);
452  Quaternion(const Vector3& axis, float angle);
453  Quaternion(float x, float y, float z, float w);
454  Quaternion(float roll, float pitch, float yaw);
455  explicit Quaternion(__m128 quatData);
456 
457  /****************************************************
458  * Custom allocation/deallocation
459  *****************************************************/
460 
461  void* operator new(size_t _size);
462  void operator delete(void* p);
463  void* operator new[](size_t size);
464  void operator delete[](void* p);
465 
466  /****************************************************
467  * Operators
468  *****************************************************/
469 
470  bool operator== (const Quaternion& p_rhs) const;
471  bool operator!= (const Quaternion& p_rhs) const;
472 
473  Quaternion operator+ (const Quaternion& p_rhs) const;
474  Quaternion operator- (const Quaternion& p_rhs) const;
475  Quaternion operator* (const Quaternion& p_rhs) const;
476 
477  Quaternion& operator+= (const Quaternion& p_rhs);
478  Quaternion& operator-= (const Quaternion& p_rhs);
479  Quaternion& operator*= (const Quaternion& p_rhs);
480 
481  explicit operator __m128() const;
482 
483  Float4 ToFloat4() const;
484 
485  public:
486  union
487  {
488  __m128 m_quaternion;
489  struct
490  {
491  float x, y, z, w;
492  };
493  float m_data[4];
494  };
495  };
496 
497 
499  // MM Instrinsic Functions
501 
502  __m128 _MM_CALLCONV MMVectorZero();
503  __m128 _MM_CALLCONV MMVectorSet(float x, float y, float z, float w);
504  __m128 _MM_CALLCONV MMVectorSetInt(uint32_t x, uint32_t y, uint32_t z, uint32_t w);
505 
506  float _MM_CALLCONV MMVectorGetX(__m128 v);
507  float _MM_CALLCONV MMVectorGetY(__m128 v);
508  float _MM_CALLCONV MMVectorGetZ(__m128 v);
509  float _MM_CALLCONV MMVectorGetW(__m128 v);
510 
511  void _MM_CALLCONV MMVectorGetXRaw(float* x, __m128 v);
512  void _MM_CALLCONV MMVectorGetYRaw(float* y, __m128 v);
513  void _MM_CALLCONV MMVectorGetZRaw(float* z, __m128 v);
514  void _MM_CALLCONV MMVectorGetWRaw(float* w, __m128 v);
515 
516  __m128 _MM_CALLCONV MMVectorSetX(__m128 v, float x);
517  __m128 _MM_CALLCONV MMVectorSetY(__m128 v, float y);
518  __m128 _MM_CALLCONV MMVectorSetZ(__m128 v, float z);
519  __m128 _MM_CALLCONV MMVectorSetW(__m128 v, float w);
520 
521  bool _MM_CALLCONV MMVectorEqual(__m128 v, __m128 u);
522 
523 
525  // MM Matrix Operations
527  Matrix4 _MM_CALLCONV MMMatrixTranslation(const Vector3& v);
528  Matrix4 _MM_CALLCONV MMMatrixRotationX(float r);
529  Matrix4 _MM_CALLCONV MMMatrixRotationY(float r);
530  Matrix4 _MM_CALLCONV MMMatrixRotationZ(float r);
531  Matrix4 _MM_CALLCONV MMMatrixRotationXYZ(const Vector3& r);
532  Matrix4 _MM_CALLCONV MMMatrixRotationQuaternion(const Quaternion& q);
533  Matrix4 _MM_CALLCONV MMMatrixScale(const Vector3& scale);
534  Matrix4 _MM_CALLCONV MMMatrixOrthoProj(float left, float right, float bottom, float top, float znear, float zfar);
535  Matrix4 _MM_CALLCONV MMMatrixPerspProj(float fov, float width, float height, float znear, float zfar);
536  Matrix4 _MM_CALLCONV MMMatrixLookAt(const Vector3& lookAt, const Vector3& center, const Vector3& up);
537  Matrix4 _MM_CALLCONV MMMatrixTranspose(const Matrix4& m);
538  Matrix4 _MM_CALLCONV MMMatrixInverse(const Matrix4& m);
539  Matrix4 _MM_CALLCONV MMMatrixInverseTranslation(const Vector3& v);
540  Matrix4 _MM_CALLCONV MMMatrixInverseTranslation(const Matrix4& m);
541  Matrix4 _MM_CALLCONV MMMatrixInverseRotation(const Vector3& v);
542  Matrix4 _MM_CALLCONV MMMatrixInverseRotation(const Matrix4& m);
543  Matrix4 _MM_CALLCONV MMMatrixInverseRotation(const Quaternion& q);
544  Matrix4 _MM_CALLCONV MMMatrixInverseScale(const Vector3& v);
545  Matrix4 _MM_CALLCONV MMMatrixInverseScale(const Matrix4& m);
546 
548  // MM Vector2 Operations
550  float _MM_CALLCONV MMVector2Dot(const Vector2& v, const Vector2& u);
551  float _MM_CALLCONV MMVector2Angle(const Vector2& v, const Vector2& u);
552  float _MM_CALLCONV MMVector2Distance(const Vector2& v, const Vector2& u);
553  float _MM_CALLCONV MMVector2MagnitudeSqr(const Vector2& v);
554  float _MM_CALLCONV MMVector2Magnitude(const Vector2& v);
555  Vector2 _MM_CALLCONV MMVector2Normalized(const Vector2& v);
556 
558  // MM Vector3 Operations
560 
561  Vector3 _MM_CALLCONV MMVector3Cross(const Vector3& v, const Vector3& u);
562  float _MM_CALLCONV MMVector3Dot(const Vector3& v, const Vector3& u);
563  float _MM_CALLCONV MMVector3Angle(const Vector3& v, const Vector3& u);
564  float _MM_CALLCONV MMVector3Distance(const Vector3& v, const Vector3& u);
565  float _MM_CALLCONV MMVector3MagnitudeSqr(const Vector3& v);
566  float _MM_CALLCONV MMVector3Magnitude(const Vector3& v);
567  Vector3 _MM_CALLCONV MMVector3Normalized(const Vector3& v);
568 
569 
571  // MM Vector4 Operations
573  float _MM_CALLCONV MMVector4Dot(const Vector4& v, const Vector4& u);
574  Vector4 _MM_CALLCONV MMVector4Normalize(const Vector4& v);
575  Vector4 _MM_CALLCONV MMVector4NormalizeEst(const Vector4& v);
576  float _MM_CALLCONV MMVector4Magnitude(const Vector4& v);
577  float _MM_CALLCONV MMVector4MagnitudeSqr(const Vector4& v);
578 
579 
581  // MM Quaternion Operations
583  float _MM_CALLCONV MMQuaternionDot(const Quaternion& q, const Quaternion& r);
584  Quaternion _MM_CALLCONV MMQuaternionNormalize(const Quaternion& q);
585  Quaternion _MM_CALLCONV MMQuaternionNormalizeEst(const Quaternion& q);
586  float _MM_CALLCONV MMQuaternionMagnitude(const Quaternion& q);
587  float _MM_CALLCONV MMQuaternionMagnitudeSqr(const Quaternion& q);
588  Quaternion _MM_CALLCONV MMQuaternionConjugate(const Quaternion& q);
589  }
590 }
591 
592 
593 #include <ht_math.inl>
594 #include <ht_mathmm.inl>
595 #include <ht_mathconvert.inl>
596 #include <ht_mathvector2.inl>
597 #include <ht_mathvector3.inl>
598 #include <ht_mathvector4.inl>
599 #include <ht_mathmatrix.inl>
600 #include <ht_mathquaternion.inl>
Definition: ht_math.h:125
Definition: ht_math.h:106
Definition: ht_math.h:70
Definition: ht_math.h:442
Hatchit Engine Copyright(c) 2015-2016 Third-Degree.
Definition: ht_intrin.h:33
Hatchit Engine Copyright(c) 2015 Third-Degree.
Hatchit Engine Copyright(c) 2015 Third-Degree.
Definition: ht_math.h:358
Definition: ht_math.h:203
Hatchit Engine Copyright(c) 2015 Third-Degree.
Definition: ht_math.h:280
Hatchit Engine Copyright(c) 2015 Third-Degree.
Definition: ht_math.h:87
Definition: ht_math.h:155