Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

180 righe
4.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Matrix classes
  12. // ------------------
  13. //
  14. #if !defined __DH_MATRIX_H__
  15. #define __DH_MATRIX_H__
  16. #include <cmath>
  17. #define VECTOR_OP(elems, op) \
  18. template<typename U> \
  19. inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
  20. { \
  21. Vec##elems<T> ret; \
  22. for (int n = 0; n < elems; n++) \
  23. ret[n] = (*this)[n] op val[n]; \
  24. return ret; \
  25. }
  26. #define BOOL_OP(elems, op, ret) \
  27. inline bool operator op(Vec##elems<T> const &val) const \
  28. { \
  29. for (int n = 0; n < elems; n++) \
  30. if ((*this)[n] != val[n]) \
  31. return ret; \
  32. return !ret; \
  33. }
  34. #define SCALAR_OP(elems, op) \
  35. inline Vec##elems<T> operator op(T const &val) const \
  36. { \
  37. Vec##elems<T> ret; \
  38. for (int n = 0; n < elems; n++) \
  39. ret[n] = (*this)[n] op val; \
  40. return ret; \
  41. }
  42. #define CAST_OP(elems, dest) \
  43. inline operator Vec##dest<T>() const \
  44. { \
  45. Vec##dest<T> ret; \
  46. for (int n = 0; n < elems && n < dest; n++) \
  47. ret[n] = (*this)[n]; \
  48. return ret; \
  49. }
  50. #define OPERATORS(elems) \
  51. inline T& operator[](int n) { return *(&x + n); } \
  52. inline T const& operator[](int n) const { return *(&x + n); } \
  53. \
  54. VECTOR_OP(elems, -) \
  55. VECTOR_OP(elems, +) \
  56. VECTOR_OP(elems, *) \
  57. VECTOR_OP(elems, /) \
  58. \
  59. BOOL_OP(elems, ==, false) \
  60. BOOL_OP(elems, !=, true) \
  61. \
  62. SCALAR_OP(elems, -) \
  63. SCALAR_OP(elems, +) \
  64. SCALAR_OP(elems, *) \
  65. SCALAR_OP(elems, /) \
  66. \
  67. CAST_OP(elems, 2) \
  68. CAST_OP(elems, 3) \
  69. CAST_OP(elems, 4) \
  70. \
  71. template<typename U> \
  72. inline operator Vec##elems<U>() const \
  73. { \
  74. Vec##elems<U> ret; \
  75. for (int n = 0; n < elems; n++) \
  76. ret[n] = static_cast<U>((*this)[n]); \
  77. return ret; \
  78. } \
  79. \
  80. inline T sqlen() const \
  81. { \
  82. T acc = 0; \
  83. for (int n = 0; n < elems; n++) \
  84. acc += (*this)[n] * (*this)[n]; \
  85. return acc; \
  86. } \
  87. \
  88. inline float len() const \
  89. { \
  90. return sqrtf((float)sqlen()); \
  91. }
  92. template <typename T> struct Vec2;
  93. template <typename T> struct Vec3;
  94. template <typename T> struct Vec4;
  95. template <typename T> struct Vec2
  96. {
  97. inline Vec2() { x = y = 0; }
  98. inline Vec2(T val) { x = y = val; }
  99. inline Vec2(T _x, T _y) { x = _x; y = _y; }
  100. OPERATORS(2)
  101. union { T x; T a; T i; };
  102. union { T y; T b; T j; };
  103. };
  104. typedef Vec2<float> float2;
  105. typedef Vec2<int> int2;
  106. template <typename T> struct Vec3
  107. {
  108. inline Vec3() { x = y = z = 0; }
  109. inline Vec3(T val) { x = y = z = val; }
  110. inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
  111. OPERATORS(3)
  112. union { T x; T a; T i; };
  113. union { T y; T b; T j; };
  114. union { T z; T c; T k; };
  115. };
  116. typedef Vec3<float> float3;
  117. typedef Vec3<int> int3;
  118. template <typename T> struct Vec4
  119. {
  120. inline Vec4() { x = y = z = w = 0; }
  121. inline Vec4(T val) { x = y = z = w = val; }
  122. inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; }
  123. OPERATORS(4)
  124. union { T x; T a; T i; };
  125. union { T y; T b; T j; };
  126. union { T z; T c; T k; };
  127. union { T w; T d; T l; };
  128. };
  129. typedef Vec4<float> float4;
  130. typedef Vec4<int> int4;
  131. #define SCALAR_GLOBAL(elems, op, U) \
  132. template<typename T> \
  133. static inline Vec##elems<U> operator op(U const &val, \
  134. Vec##elems<T> const &that) \
  135. { \
  136. Vec##elems<U> ret; \
  137. for (int n = 0; n < elems; n++) \
  138. ret[n] = val op that[n]; \
  139. return ret; \
  140. }
  141. #define SCALAR_GLOBAL2(elems, op) \
  142. SCALAR_GLOBAL(elems, op, int) \
  143. SCALAR_GLOBAL(elems, op, float)
  144. #define GLOBALS(elems) \
  145. SCALAR_GLOBAL2(elems, -) \
  146. SCALAR_GLOBAL2(elems, +) \
  147. SCALAR_GLOBAL2(elems, *) \
  148. SCALAR_GLOBAL2(elems, /)
  149. GLOBALS(2)
  150. GLOBALS(3)
  151. GLOBALS(4)
  152. #endif // __DH_MATRIX_H__