Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

145 строки
3.4 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 OPERATORS(elems) \
  43. inline T& operator[](int n) { return *(&x + n); } \
  44. inline T const& operator[](int n) const { return *(&x + n); } \
  45. \
  46. VECTOR_OP(elems, -) \
  47. VECTOR_OP(elems, +) \
  48. VECTOR_OP(elems, *) \
  49. VECTOR_OP(elems, /) \
  50. \
  51. BOOL_OP(elems, ==, false) \
  52. BOOL_OP(elems, !=, true) \
  53. \
  54. SCALAR_OP(elems, -) \
  55. SCALAR_OP(elems, +) \
  56. SCALAR_OP(elems, *) \
  57. SCALAR_OP(elems, /) \
  58. \
  59. template<typename U> \
  60. inline operator Vec##elems<U>() const \
  61. { \
  62. Vec##elems<U> ret; \
  63. for (int n = 0; n < elems; n++) \
  64. ret[n] = static_cast<U>((*this)[n]); \
  65. return ret; \
  66. } \
  67. \
  68. inline T sqlen() const \
  69. { \
  70. T acc = 0; \
  71. for (int n = 0; n < elems; n++) \
  72. acc += (*this)[n] * (*this)[n]; \
  73. return acc; \
  74. } \
  75. \
  76. inline float len() const \
  77. { \
  78. return sqrtf((float)sqlen()); \
  79. }
  80. template <typename T> struct Vec2
  81. {
  82. inline Vec2() { x = y = 0; }
  83. inline Vec2(T val) { x = y = val; }
  84. inline Vec2(T _x, T _y) { x = _x; y = _y; }
  85. OPERATORS(2)
  86. union { T x; T a; T i; };
  87. union { T y; T b; T j; };
  88. };
  89. typedef Vec2<float> Float2;
  90. typedef Vec2<int> Int2;
  91. template <typename T> struct Vec3
  92. {
  93. inline Vec3() { x = y = z = 0; }
  94. inline Vec3(T val) { x = y = z = val; }
  95. inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
  96. OPERATORS(3)
  97. union { T x; T a; T i; };
  98. union { T y; T b; T j; };
  99. union { T z; T c; T k; };
  100. };
  101. typedef Vec3<float> Float3;
  102. typedef Vec3<int> Int3;
  103. #define SCALAR_GLOBAL(elems, op, U) \
  104. template<typename T> \
  105. static inline Vec##elems<U> operator op(U const &val, \
  106. Vec##elems<T> const &that) \
  107. { \
  108. Vec##elems<U> ret; \
  109. for (int n = 0; n < elems; n++) \
  110. ret[n] = val op that[n]; \
  111. return ret; \
  112. }
  113. #define SCALAR_GLOBAL2(elems, op) \
  114. SCALAR_GLOBAL(elems, op, int) \
  115. SCALAR_GLOBAL(elems, op, float)
  116. #define GLOBALS(elems) \
  117. SCALAR_GLOBAL2(elems, -) \
  118. SCALAR_GLOBAL2(elems, +) \
  119. SCALAR_GLOBAL2(elems, *) \
  120. SCALAR_GLOBAL2(elems, /)
  121. GLOBALS(2)
  122. GLOBALS(3)
  123. #endif // __DH_MATRIX_H__