vector.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // vector.hpp
  3. // vector
  4. //
  5. // Created by Sam Jaffe on 8/15/16.
  6. //
  7. #pragma once
  8. #include <cassert>
  9. #include <cmath>
  10. #include <cstddef>
  11. #include <array>
  12. #include <initializer_list>
  13. #include <stdexcept>
  14. #include <type_traits>
  15. #include "expect/expect.hpp"
  16. namespace math { namespace vector {
  17. #define VECTOR_ENABLE_IF_LT_N(index, expr) \
  18. template <bool _ = true> \
  19. typename std::enable_if<std::size_t(index) < N && _, expr>::type
  20. #define VECTOR_ENABLE_IF_EQ_N(index, t, n) \
  21. template <bool _ = true> \
  22. typename std::enable_if<std::size_t(index) == N && _, vector<t, n> >::type
  23. #define VECTOR_ENABLE_IF_EQ_T(_type, t, n) \
  24. typename std::enable_if<std::is_same<_type, t>::value, vector<t, n> >::type
  25. #define VECTOR_DISABLE_IF_VECTOR(_type, t, n) \
  26. typename std::enable_if<!is_vector<_type>::value, vector<t, n> >::type
  27. #define VECTOR_ACCESS_FN(name, i) \
  28. VECTOR_ENABLE_IF_LT_N(i, value_type const &) name() const { return _data[i]; } \
  29. VECTOR_ENABLE_IF_LT_N(i, value_type &) name() { return _data[i]; }
  30. #define VECTOR_FOR_EACH_RANGE(var, end) for (std::size_t var = 0; var < end; ++var)
  31. #define VECTOR_FOR_EACH(var) VECTOR_FOR_EACH_RANGE(var, N)
  32. struct {} fill;
  33. using fill_t = decltype(fill);
  34. template <typename T>
  35. struct is_vector { static const constexpr bool value = false; };
  36. template <typename T, std::size_t N>
  37. class vector;
  38. template <typename T, std::size_t N>
  39. struct is_vector<vector<T, N>> { static const constexpr bool value = true; };
  40. template <typename T, std::size_t N>
  41. class vector {
  42. public:
  43. using value_type = T;
  44. private:
  45. using mag_t = decltype(std::sqrt(std::declval<T>()));
  46. template <typename M>
  47. using mul_t = decltype(std::declval<T>()*std::declval<M>());
  48. template <typename M>
  49. using div_t = decltype(std::declval<T>()/std::declval<M>());
  50. public:
  51. // Constructors
  52. vector() = default;
  53. vector(std::array<T, N> const & init) {
  54. VECTOR_FOR_EACH(i) { _data[i] = init[i]; }
  55. }
  56. vector(vector const & other) {
  57. *this = other;
  58. }
  59. vector(vector && other) {
  60. *this = std::move(other);
  61. }
  62. // Conversion
  63. template <typename T2, std::size_t N2>
  64. explicit vector(vector<T2, N2> const & other) {
  65. VECTOR_FOR_EACH_RANGE(i, std::min(N, N2)) {
  66. _data[i] = static_cast<T>(other[i]);
  67. }
  68. }
  69. vector(T const & v, fill_t) {
  70. VECTOR_FOR_EACH(i) { _data[i] = v; }
  71. }
  72. // Assignment
  73. vector& operator=(vector const & other) {
  74. VECTOR_FOR_EACH(i) { _data[i] = other[i]; }
  75. return *this;
  76. }
  77. vector& operator=(vector && other) {
  78. VECTOR_FOR_EACH(i) { _data[i] = std::move(other._data[i]); }
  79. return *this;
  80. }
  81. // Named Accessors
  82. // - Numeric Vector Accessors
  83. VECTOR_ACCESS_FN(x, 0)
  84. VECTOR_ACCESS_FN(y, 1)
  85. VECTOR_ACCESS_FN(z, 2)
  86. VECTOR_ACCESS_FN(w, 3)
  87. // - Color Vector Accessors
  88. VECTOR_ACCESS_FN(r, 0)
  89. VECTOR_ACCESS_FN(g, 1)
  90. VECTOR_ACCESS_FN(b, 2)
  91. VECTOR_ACCESS_FN(a, 3)
  92. // Unnamed Accessors
  93. value_type const & operator[](std::size_t idx) const {
  94. return _data[idx];
  95. }
  96. value_type & operator[](std::size_t idx) {
  97. return _data[idx];
  98. }
  99. value_type const & at(std::size_t idx) const {
  100. expects(idx < N, std::out_of_range, "index out of range");
  101. return _data[idx];
  102. }
  103. value_type & at(std::size_t idx) {
  104. expects(idx < N, std::out_of_range, "index out of range");
  105. return _data[idx];
  106. }
  107. // Mathematical Operations
  108. vector& operator+=(vector const & other) {
  109. VECTOR_FOR_EACH(i) { _data[i] += other[i]; }
  110. return *this;
  111. }
  112. vector& operator+=(T const & other) {
  113. return operator+=(vector(other, fill));
  114. }
  115. vector operator+(vector const & other) const {
  116. return vector{*this} += other;
  117. }
  118. vector operator+(T const & other) const {
  119. return operator+(vector(other, fill));
  120. }
  121. friend vector operator+(T const & lhs, vector const & rhs) {
  122. return rhs + lhs;
  123. }
  124. vector& operator-=(vector const & other) {
  125. VECTOR_FOR_EACH(i) { _data[i] -= other[i]; }
  126. return *this;
  127. }
  128. vector& operator-=(T const & other) {
  129. return operator-=(vector(other, fill));
  130. }
  131. vector operator-(vector const & other) const {
  132. return vector{*this} -= other;
  133. }
  134. vector operator-(T const & other) const {
  135. return operator-(vector(other, fill));
  136. }
  137. friend vector operator-(T const & lhs, vector const & rhs) {
  138. return vector(lhs, fill) - rhs;
  139. }
  140. vector operator-() const {
  141. return vector{} -= *this;
  142. }
  143. template <typename M>
  144. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(M c) {
  145. VECTOR_FOR_EACH(i) { _data[i] *= c; }
  146. return *this;
  147. }
  148. template <typename M>
  149. VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c) const {
  150. return vector<mul_t<M>, N>{*this} *= c;
  151. }
  152. template <typename M>
  153. friend VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c, vector<T, N> const & v) {
  154. return v * c;
  155. }
  156. template <typename M>
  157. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(vector<M, N> c) {
  158. VECTOR_FOR_EACH(i) { _data[i] *= c[i]; }
  159. return *this;
  160. }
  161. template <typename M>
  162. vector<mul_t<M>, N> operator*(vector<M, N> const & other) const {
  163. return vector<mul_t<M>, N>{*this} *= other;
  164. }
  165. template <typename M>
  166. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(M c) {
  167. expects(c != 0, std::domain_error, "divide by zero");
  168. VECTOR_FOR_EACH(i) { _data[i] /= c; }
  169. return *this;
  170. }
  171. template <typename M>
  172. VECTOR_DISABLE_IF_VECTOR(M, div_t<M>, N) operator/(M c) const {
  173. return vector<div_t<M>, N>{*this} /= c;
  174. }
  175. template <typename M>
  176. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(vector<M, N> c) {
  177. VECTOR_FOR_EACH(i) { expects(c[i] != 0, std::domain_error, "divide by zero"); }
  178. VECTOR_FOR_EACH(i) { _data[i] /= c[i]; }
  179. return *this;
  180. }
  181. template <typename M>
  182. vector<div_t<M>, N> operator/(vector<M, N> const & other) const {
  183. return vector<div_t<M>, N>{*this} /= other;
  184. }
  185. // Vector Operations
  186. value_type dot(vector const & other) const {
  187. value_type accum{};
  188. VECTOR_FOR_EACH(i) { accum += at(i) * other.at(i); }
  189. return accum;
  190. }
  191. mag_t magnitude() const {
  192. return std::sqrt(dot(*this));
  193. }
  194. vector<mag_t, N> unit() const {
  195. return *this / magnitude();
  196. }
  197. VECTOR_ENABLE_IF_EQ_N(3, T, N) cross(vector const & other) const {
  198. return {{
  199. y()*other.z() - z()*other.y(),
  200. z()*other.x() - x()*other.z(),
  201. x()*other.y() - y()*other.x()
  202. }};
  203. }
  204. VECTOR_ENABLE_IF_EQ_N(2, T, 3) cross(vector const & other) const {
  205. return {{ 0, 0, x()*other.y() - y()*other.x() }};
  206. }
  207. vector<mag_t, N> projection(vector const & other) const {
  208. vector<mag_t, N> b_p = other.unit();
  209. return b_p * vector<mag_t, N>{*this}.dot(b_p);
  210. }
  211. private:
  212. value_type _data[N] = {value_type()};
  213. };
  214. template <typename T, std::size_t N>
  215. vector<T, N> abs(vector<T, N> const & self) {
  216. vector<T, N> tmp(self);
  217. using std::abs;
  218. VECTOR_FOR_EACH(i) { tmp[i] = abs(tmp[i]); }
  219. return tmp;
  220. }
  221. template <typename T, std::size_t N>
  222. int compare(vector<T, N> const & lhs, vector<T, N> const & rhs) {
  223. VECTOR_FOR_EACH(i) {
  224. if (lhs[i] < rhs[i]) return -1;
  225. else if (lhs[i] > rhs[i]) return 1;
  226. }
  227. return 0;
  228. }
  229. template <typename T, std::size_t N>
  230. bool operator==(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) == 0; }
  231. template <typename T, std::size_t N>
  232. bool operator!=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) != 0; }
  233. template <typename T, std::size_t N>
  234. bool operator< (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) < 0; }
  235. template <typename T, std::size_t N>
  236. bool operator<=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) <= 0; }
  237. template <typename T, std::size_t N>
  238. bool operator> (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) > 0; }
  239. template <typename T, std::size_t N>
  240. bool operator>=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) >= 0; }
  241. #undef VECTOR_FOR_EACH
  242. #undef VECTOR_FOR_EACH_RANGE
  243. #undef VECTOR_ACCESS_FN
  244. #undef VECTOR_DISABLE_IF_VECTOR
  245. #undef VECTOR_ENABLE_IF_EQ_T
  246. #undef VECTOR_ENABLE_IF_EQ_N
  247. #undef VECTOR_ENABLE_IF_LT_N
  248. } }
  249. template <typename... Ts>
  250. auto make_vector(Ts && ...elems) -> math::vector::vector<typename std::common_type<Ts...>::type, sizeof...(Ts)> {
  251. return {{elems...}};
  252. }
  253. using math::vector::abs;