vector.hpp 8.8 KB

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