Vec3.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <cassert>
  18. template<class T>
  19. class Vec3
  20. {
  21. public:
  22. Vec3() = default;
  23. Vec3(T x, T y, T z) : v{ x, y, z }
  24. {
  25. }
  26. Vec3(T v_[3]) : v{ v_[0], v_[1], v_[2] }
  27. {
  28. }
  29. T& operator[](unsigned int i) {
  30. assert(i < 3);
  31. return v[i];
  32. }
  33. const T& operator[](unsigned int i) const {
  34. assert(i < 3);
  35. return v[i];
  36. }
  37. inline T dot(const Vec3& other) const {
  38. return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2];
  39. }
  40. inline T length() const
  41. {
  42. return sqrt(squaredLength());
  43. }
  44. inline T squaredLength() const
  45. {
  46. return dot(*this);
  47. }
  48. inline Vec3& normalize()
  49. {
  50. T invL = T(1) / length();
  51. v[0] *= invL;
  52. v[1] *= invL;
  53. v[2] *= invL;
  54. return *this;
  55. }
  56. inline Vec3& negate()
  57. {
  58. v[0] = -v[0];
  59. v[1] = -v[1];
  60. v[2] = -v[2];
  61. return *this;
  62. }
  63. inline Vec3& inverse()
  64. {
  65. v[0] = T(1) / v[0];
  66. v[1] = T(1) / v[1];
  67. v[2] = T(1) / v[2];
  68. return *this;
  69. }
  70. inline Vec3& minimum(const Vec3& v2)
  71. {
  72. v[0] = min(v[0], v2[0]);
  73. v[1] = min(v[1], v2[1]);
  74. v[2] = min(v[2], v2[2]);
  75. return *this;
  76. }
  77. inline Vec3& maximum(const Vec3& v2)
  78. {
  79. v[0] = max(v[0], v2[0]);
  80. v[1] = max(v[1], v2[1]);
  81. v[2] = max(v[2], v2[2]);
  82. return *this;
  83. }
  84. inline Vec3& operator *=(const Vec3& v2) {
  85. v[0] *= v2[0];
  86. v[1] *= v2[1];
  87. v[2] *= v2[2];
  88. return *this;
  89. }
  90. inline Vec3& operator *=(const T scale) {
  91. v[0] *= scale;
  92. v[1] *= scale;
  93. v[2] *= scale;
  94. return *this;
  95. }
  96. inline Vec3& operator /=(const Vec3& v2) {
  97. v[0] /= v2[0];
  98. v[1] /= v2[1];
  99. v[2] /= v2[2];
  100. return *this;
  101. }
  102. inline Vec3& operator /=(const T scale) {
  103. v[0] /= scale;
  104. v[1] /= scale;
  105. v[2] /= scale;
  106. return *this;
  107. }
  108. inline Vec3& operator +=(const Vec3& other) {
  109. v[0] += other.v[0];
  110. v[1] += other.v[1];
  111. v[2] += other.v[2];
  112. return *this;
  113. }
  114. inline Vec3& operator -=(const Vec3& other) {
  115. v[0] -= other.v[0];
  116. v[1] -= other.v[1];
  117. v[2] -= other.v[2];
  118. return *this;
  119. }
  120. inline bool equals(const Vec3& other, T epsilon=T(0.001))
  121. {
  122. return fabs(v[0] - other.v[0]) <= epsilon && fabs(v[1] - other.v[1]) <= epsilon && fabs(v[2] - other.v[2]) <= epsilon;
  123. }
  124. T x() const { return v[0]; }
  125. T y() const { return v[1]; }
  126. T z() const { return v[2]; }
  127. T v[3] = { 0, 0, 0 };
  128. };
  129. template< class T>
  130. inline Vec3<T> operator+(const Vec3<T>& l, const Vec3<T>& r)
  131. {
  132. auto res = l;
  133. res += r;
  134. return res;
  135. }
  136. template< class T>
  137. inline Vec3<T> operator-(const Vec3<T>& l, const Vec3<T>& r)
  138. {
  139. auto res = l;
  140. res -= r;
  141. return res;
  142. }
  143. template< class T>
  144. inline Vec3<T> normalize(Vec3<T> v)
  145. {
  146. return v.normalize();
  147. }
  148. template< class T>
  149. inline Vec3<T> negate(Vec3<T> v)
  150. {
  151. return v.negate();
  152. }
  153. template< class T>
  154. inline Vec3<T> inverse(Vec3<T> v)
  155. {
  156. return v.inverse();
  157. }
  158. template< class T>
  159. inline Vec3<T> minimum(Vec3<T> v, const Vec3<T>& v2)
  160. {
  161. v.minimum(v2);
  162. return v;
  163. }
  164. template< class T>
  165. inline Vec3<T> maximum(Vec3<T> v, const Vec3<T>& v2)
  166. {
  167. v.maximum(v2);
  168. return v;
  169. }
  170. template< class T>
  171. inline Vec3<T> operator /(Vec3<T> v, T scale)
  172. {
  173. v /= scale;
  174. return v;
  175. }
  176. template< class T>
  177. inline Vec3<T> operator *(Vec3<T> v, Vec3<T> v2)
  178. {
  179. v *= v2;
  180. return v;
  181. }
  182. template< class T>
  183. inline Vec3<T> operator /(Vec3<T> v, Vec3<T> v2)
  184. {
  185. v /= v2;
  186. return v;
  187. }
  188. template< class T>
  189. inline Vec3<T> operator *(Vec3<T> v, T scale)
  190. {
  191. v *= scale;
  192. return v;
  193. }
  194. typedef Vec3<float> Vec3f;
  195. template<class T>
  196. class Matrix3_4
  197. {
  198. public:
  199. Matrix3_4(const T (&m_)[3][4])
  200. {
  201. for (int row = 0; row < 3; ++row)
  202. {
  203. for (int col = 0; col < 4; ++col)
  204. {
  205. m[row][col] = m_[row][col];
  206. }
  207. }
  208. }
  209. Matrix3_4(const T* m_)
  210. {
  211. for (int row = 0; row < 3; ++row)
  212. {
  213. for (int col = 0; col < 4; ++col)
  214. {
  215. m[row][col] = m_[row * 4 + col];
  216. }
  217. }
  218. }
  219. Vec3<T> operator *(const Vec3<T> v) const
  220. {
  221. auto x = v[0];
  222. auto y = v[1];
  223. auto z = v[2];
  224. return Vec3<T>(
  225. x * m[0][0] + y * m[0][1] + z * m[0][2] + m[0][3],
  226. x * m[1][0] + y * m[1][1] + z * m[1][2] + m[1][3],
  227. x * m[2][0] + y * m[2][1] + z * m[2][2] + m[2][3]
  228. );
  229. }
  230. Matrix3_4& scaledColumn(unsigned int iColumn, T scale)
  231. {
  232. assert(iColumn < 4);
  233. m[0][iColumn] *= scale;
  234. m[1][iColumn] *= scale;
  235. m[2][iColumn] *= scale;
  236. return *this;
  237. }
  238. Matrix3_4 scaleColumn(unsigned int iColumn, T scale) const
  239. {
  240. assert(iColumn < 4);
  241. Matrix3_4 copy(*this);
  242. copy.m[0][iColumn] *= scale;
  243. copy.m[1][iColumn] *= scale;
  244. copy.m[2][iColumn] *= scale;
  245. return copy;
  246. }
  247. Matrix3_4& setColumn(unsigned int iColumn, const Vec3<T>& v)
  248. {
  249. assert(iColumn < 4);
  250. m[0][iColumn] = v[0];
  251. m[1][iColumn] = v[1];
  252. m[2][iColumn] = v[2];
  253. return *this;
  254. }
  255. Vec3f getColumn(unsigned int iColumn) const
  256. {
  257. assert(iColumn < 4);
  258. return Vec3f(m[0][iColumn], m[1][iColumn], m[2][iColumn]);
  259. }
  260. static Matrix3_4 translation(const Vec3<T> v)
  261. {
  262. return Matrix3_4({ {1, 0, 0, v.x()}, {0, 1, 0, v.y()}, {0, 0, 1, v.z()}});
  263. }
  264. static Matrix3_4 scale(const Vec3<T> v)
  265. {
  266. return Matrix3_4({ {v.x(), 0, 0, 0}, {0, v.y(), 0, 0}, {0, 0, v.z(), 0}});
  267. }
  268. public:
  269. T m[3][4] = { {1, 0, 0, 1}, {0, 1, 0, 1}, {0, 0, 1, 1}};
  270. };
  271. typedef Matrix3_4<float> Matrix3_4f;