vector.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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+(vector const & other) const {
  113. return vector{*this} += other;
  114. }
  115. vector& operator-=(vector const & other) {
  116. VECTOR_FOR_EACH(i) { _data[i] -= other[i]; }
  117. return *this;
  118. }
  119. vector operator-(vector const & other) const {
  120. return vector{*this} -= other;
  121. }
  122. vector operator-() const {
  123. return vector{} -= *this;
  124. }
  125. template <typename M>
  126. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(M c) {
  127. VECTOR_FOR_EACH(i) { _data[i] *= c; }
  128. return *this;
  129. }
  130. template <typename M>
  131. VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c) const {
  132. return vector<mul_t<M>, N>{*this} *= c;
  133. }
  134. template <typename M>
  135. friend VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c, vector<T, N> const & v) {
  136. return v * c;
  137. }
  138. template <typename M>
  139. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(vector<M, N> c) {
  140. VECTOR_FOR_EACH(i) { _data[i] *= c[i]; }
  141. return *this;
  142. }
  143. template <typename M>
  144. vector<mul_t<M>, N> operator*(vector<M, N> const & other) const {
  145. return vector<mul_t<M>, N>{*this} *= other;
  146. }
  147. template <typename M>
  148. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(M c) {
  149. expects(c != 0, std::domain_error, "divide by zero");
  150. VECTOR_FOR_EACH(i) { _data[i] /= c; }
  151. return *this;
  152. }
  153. template <typename M>
  154. VECTOR_DISABLE_IF_VECTOR(M, div_t<M>, N) operator/(M c) const {
  155. return vector<div_t<M>, N>{*this} /= c;
  156. }
  157. template <typename M>
  158. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(vector<M, N> c) {
  159. VECTOR_FOR_EACH(i) { expects(c[i] != 0, std::domain_error, "divide by zero"); }
  160. VECTOR_FOR_EACH(i) { _data[i] /= c[i]; }
  161. return *this;
  162. }
  163. template <typename M>
  164. vector<div_t<M>, N> operator/(vector<M, N> const & other) const {
  165. return vector<div_t<M>, N>{*this} /= other;
  166. }
  167. // Vector Operations
  168. value_type dot(vector const & other) const {
  169. value_type accum{};
  170. VECTOR_FOR_EACH(i) { accum += at(i) * other.at(i); }
  171. return accum;
  172. }
  173. mag_t magnitude() const {
  174. return std::sqrt(dot(*this));
  175. }
  176. vector<mag_t, N> unit() const {
  177. return *this / magnitude();
  178. }
  179. VECTOR_ENABLE_IF_EQ_N(3, T, N) cross(vector const & other) const {
  180. return {{
  181. y()*other.z() - z()*other.y(),
  182. z()*other.x() - x()*other.z(),
  183. x()*other.y() - y()*other.x()
  184. }};
  185. }
  186. VECTOR_ENABLE_IF_EQ_N(2, T, 3) cross(vector const & other) const {
  187. return {{ 0, 0, x()*other.y() - y()*other.x() }};
  188. }
  189. vector<mag_t, N> projection(vector const & other) const {
  190. vector<mag_t, N> b_p = other.unit();
  191. return b_p * vector<mag_t, N>{*this}.dot(b_p);
  192. }
  193. private:
  194. value_type _data[N] = {value_type()};
  195. };
  196. template <typename T, std::size_t N>
  197. vector<T, N> abs(vector<T, N> const & self) {
  198. vector<T, N> tmp(self);
  199. using std::abs;
  200. VECTOR_FOR_EACH(i) { tmp[i] = abs(tmp[i]); }
  201. return tmp;
  202. }
  203. template <typename T, std::size_t N>
  204. int compare(vector<T, N> const & lhs, vector<T, N> const & rhs) {
  205. VECTOR_FOR_EACH(i) {
  206. if (lhs[i] < rhs[i]) return -1;
  207. else if (lhs[i] > rhs[i]) return 1;
  208. }
  209. return 0;
  210. }
  211. template <typename T, std::size_t N>
  212. bool operator==(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) == 0; }
  213. template <typename T, std::size_t N>
  214. bool operator!=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) != 0; }
  215. template <typename T, std::size_t N>
  216. bool operator< (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) < 0; }
  217. template <typename T, std::size_t N>
  218. bool operator<=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) <= 0; }
  219. template <typename T, std::size_t N>
  220. bool operator> (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) > 0; }
  221. template <typename T, std::size_t N>
  222. bool operator>=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) >= 0; }
  223. } }
  224. template <typename... Ts>
  225. auto make_vector(Ts && ...elems) -> math::vector::vector<typename std::common_type<Ts...>::type, sizeof...(Ts)> {
  226. return {{elems...}};
  227. }
  228. using math::vector::abs;