vector.hpp 7.3 KB

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