vector.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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::initializer_list<T> && init) {
  54. expects(init.size() == N, "initializer size mismatch");
  55. std::size_t idx = 0;
  56. for ( auto it = init.begin(), end = init.end(); it != end && idx < N; ++it, ++idx ) {
  57. _data[idx] = *it;
  58. }
  59. }
  60. vector(std::array<T, N> const & init) {
  61. VECTOR_FOR_EACH(i) { _data[i] = init[i]; }
  62. }
  63. vector(vector const & other) {
  64. *this = other;
  65. }
  66. vector(vector && other) {
  67. *this = std::move(other);
  68. }
  69. // Conversion
  70. template <typename T2, std::size_t N2>
  71. explicit vector(vector<T2, N2> const & other) {
  72. VECTOR_FOR_EACH_RANGE(i, std::min(N, N2)) {
  73. _data[i] = static_cast<T>(other[i]);
  74. }
  75. }
  76. vector(T const & v, fill_t) {
  77. VECTOR_FOR_EACH(i) { _data[i] = v; }
  78. }
  79. // Assignment
  80. vector& operator=(vector const & other) {
  81. VECTOR_FOR_EACH(i) { _data[i] = other[i]; }
  82. return *this;
  83. }
  84. vector& operator=(vector && other) {
  85. VECTOR_FOR_EACH(i) { _data[i] = std::move(other._data[i]); }
  86. return *this;
  87. }
  88. // Named Accessors
  89. // - Numeric Vector Accessors
  90. VECTOR_ACCESS_FN(x, 0)
  91. VECTOR_ACCESS_FN(y, 1)
  92. VECTOR_ACCESS_FN(z, 2)
  93. VECTOR_ACCESS_FN(w, 3)
  94. // - Color Vector Accessors
  95. VECTOR_ACCESS_FN(r, 0)
  96. VECTOR_ACCESS_FN(g, 1)
  97. VECTOR_ACCESS_FN(b, 2)
  98. VECTOR_ACCESS_FN(a, 3)
  99. // Unnamed Accessors
  100. value_type const & operator[](std::size_t idx) const {
  101. return _data[idx];
  102. }
  103. value_type & operator[](std::size_t idx) {
  104. return _data[idx];
  105. }
  106. value_type const & at(std::size_t idx) const {
  107. expects(idx < N, std::out_of_range, "index out of range");
  108. return _data[idx];
  109. }
  110. value_type & at(std::size_t idx) {
  111. expects(idx < N, std::out_of_range, "index out of range");
  112. return _data[idx];
  113. }
  114. // Mathematical Operations
  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-=(vector const & other) {
  123. VECTOR_FOR_EACH(i) { _data[i] -= other[i]; }
  124. return *this;
  125. }
  126. vector operator-(vector const & other) const {
  127. return vector{*this} -= other;
  128. }
  129. vector operator-() const {
  130. return vector{} -= *this;
  131. }
  132. template <typename M>
  133. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(M c) {
  134. VECTOR_FOR_EACH(i) { _data[i] *= c; }
  135. return *this;
  136. }
  137. template <typename M>
  138. VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c) const {
  139. return vector<mul_t<M>, N>{*this} *= c;
  140. }
  141. template <typename M>
  142. friend VECTOR_DISABLE_IF_VECTOR(M, mul_t<M>, N) operator*(M c, vector<T, N> const & v) {
  143. return v * c;
  144. }
  145. template <typename M>
  146. VECTOR_ENABLE_IF_EQ_T(mul_t<M>, T, N)& operator*=(vector<M, N> c) {
  147. VECTOR_FOR_EACH(i) { _data[i] *= c[i]; }
  148. return *this;
  149. }
  150. template <typename M>
  151. vector<mul_t<M>, N> operator*(vector<M, N> const & other) const {
  152. return vector<mul_t<M>, N>{*this} *= other;
  153. }
  154. template <typename M>
  155. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(M c) {
  156. expects(c != 0, std::domain_error, "divide by zero");
  157. VECTOR_FOR_EACH(i) { _data[i] /= c; }
  158. return *this;
  159. }
  160. template <typename M>
  161. VECTOR_DISABLE_IF_VECTOR(M, div_t<M>, N) operator/(M c) const {
  162. return vector<div_t<M>, N>{*this} /= c;
  163. }
  164. template <typename M>
  165. VECTOR_ENABLE_IF_EQ_T(div_t<M>, T, N)& operator/=(vector<M, N> c) {
  166. VECTOR_FOR_EACH(i) { expects(c[i] != 0, std::domain_error, "divide by zero"); }
  167. VECTOR_FOR_EACH(i) { _data[i] /= c[i]; }
  168. return *this;
  169. }
  170. template <typename M>
  171. vector<div_t<M>, N> operator/(vector<M, N> const & other) const {
  172. return vector<div_t<M>, N>{*this} /= other;
  173. }
  174. // Vector Operations
  175. value_type dot(vector const & other) const {
  176. value_type accum{};
  177. VECTOR_FOR_EACH(i) { accum += at(i) * other.at(i); }
  178. return accum;
  179. }
  180. mag_t magnitude() const {
  181. return std::sqrt(dot(*this));
  182. }
  183. vector<mag_t, N> unit() const {
  184. return *this / magnitude();
  185. }
  186. VECTOR_ENABLE_IF_EQ_N(3, T, N) cross(vector const & other) const {
  187. return {
  188. y()*other.z() - z()*other.y(),
  189. z()*other.x() - x()*other.z(),
  190. x()*other.y() - y()*other.x()
  191. };
  192. }
  193. VECTOR_ENABLE_IF_EQ_N(2, T, 3) cross(vector const & other) const {
  194. return { 0, 0, x()*other.y() - y()*other.x() };
  195. }
  196. vector<mag_t, N> projection(vector const & other) const {
  197. vector<mag_t, N> b_p = other.unit();
  198. return b_p * vector<mag_t, N>{*this}.dot(b_p);
  199. }
  200. private:
  201. value_type _data[N] = {value_type()};
  202. };
  203. template <typename T, std::size_t N>
  204. vector<T, N> abs(vector<T, N> const & self) {
  205. vector<T, N> tmp(self);
  206. using std::abs;
  207. VECTOR_FOR_EACH(i) { tmp[i] = abs(tmp[i]); }
  208. return tmp;
  209. }
  210. template <typename T, std::size_t N>
  211. int compare(vector<T, N> const & lhs, vector<T, N> const & rhs) {
  212. VECTOR_FOR_EACH(i) {
  213. if (lhs[i] < rhs[i]) return -1;
  214. else if (lhs[i] > rhs[i]) return 1;
  215. }
  216. return 0;
  217. }
  218. template <typename T, std::size_t N>
  219. bool operator==(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) == 0; }
  220. template <typename T, std::size_t N>
  221. bool operator!=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) != 0; }
  222. template <typename T, std::size_t N>
  223. bool operator< (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) < 0; }
  224. template <typename T, std::size_t N>
  225. bool operator<=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) <= 0; }
  226. template <typename T, std::size_t N>
  227. bool operator> (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) > 0; }
  228. template <typename T, std::size_t N>
  229. bool operator>=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) >= 0; }
  230. } }
  231. using math::vector::abs;