vector_old.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // vector.h
  3. //
  4. //
  5. // Created by Sam Jaffe on 8/21/15.
  6. //
  7. //
  8. #pragma once
  9. namespace math { namespace vector {
  10. template <size_t, typename> class raw_vector;
  11. } }
  12. template <size_t D, typename T>
  13. math::vector::raw_vector<D, T> operator +(T const&, math::vector::raw_vector<D, T> const&);
  14. template <size_t D, typename T>
  15. math::vector::raw_vector<D, T> operator -(T const&, math::vector::raw_vector<D, T> const&);
  16. template <size_t D, typename T>
  17. math::vector::raw_vector<D, T> operator *(T const&, math::vector::raw_vector<D, T> const&);
  18. template <size_t D, typename T>
  19. math::vector::raw_vector<D, T> operator /(T const&, math::vector::raw_vector<D, T> const&);
  20. namespace math { namespace vector {
  21. struct fill_t {} fill;
  22. template <size_t D, typename T>
  23. class raw_vector {
  24. public:
  25. typedef T value_type;
  26. typedef T& reference;
  27. typedef const T& const_reference;
  28. typedef raw_vector<D, T> this_type;
  29. typedef decltype(std::sqrt(std::declval<T>())) sqrt_type; // double or float
  30. T& operator [](size_t index) { return data[index]; }
  31. inline const T& operator [](size_t index) const { return data[index]; }
  32. T& at(size_t index) {
  33. if (index >= D) throw std::out_of_range(std::to_string(index) + " is out of range [0, " + std::to_string(D) + ")");
  34. return operator [](index);
  35. }
  36. const T& at(size_t index) const {
  37. if (index >= D) throw std::out_of_range(std::to_string(index) + " is out of range [0, " + std::to_string(D) + ")");
  38. return operator [](index);
  39. }
  40. template <std::size_t Idx, typename = typename std::enable_if<Idx < D>::type>
  41. inline const_reference get() const { return data[Idx]; }
  42. template <std::size_t Idx, typename = typename std::enable_if<Idx < D>::type>
  43. inline reference get() { return data[Idx]; }
  44. reference x() { return get<0>(); }
  45. const_reference x() const { return get<0>(); }
  46. reference y() { return get<1>(); }
  47. const_reference y() const { return get<1>(); }
  48. reference z() { return get<2>(); }
  49. const_reference z() const { return get<2>(); }
  50. reference w() { return get<3>(); }
  51. const_reference w() const { return get<3>(); }
  52. reference r() { return get<0>(); }
  53. const_reference r() const { return get<0>(); }
  54. reference g() { return get<1>(); }
  55. const_reference g() const { return get<1>(); }
  56. reference b() { return get<2>(); }
  57. const_reference b() const { return get<2>(); }
  58. reference a() { return get<3>(); }
  59. const_reference a() const { return get<3>(); }
  60. this_type& operator+=(const this_type& other) {
  61. for (size_t i = 0; i < D; ++i) at(i) += other.at(i);
  62. return *this;
  63. }
  64. this_type& operator-=(const this_type& other) {
  65. for (size_t i = 0; i < D; ++i) at(i) -= other.at(i);
  66. return *this;
  67. }
  68. this_type& operator*=(const this_type& other) {
  69. for (size_t i = 0; i < D; ++i) at(i) *= other.at(i);
  70. return *this;
  71. }
  72. this_type& operator/=(const this_type& other) {
  73. for (size_t i = 0; i < D; ++i) at(i) /= other.at(i);
  74. return *this;
  75. }
  76. this_type& operator+=(const T& c) {
  77. return operator +=(this_type{c, fill});
  78. }
  79. this_type& operator -=(const T& c) {
  80. return operator -=(this_type{c, fill});
  81. }
  82. this_type& operator *=(const T& c) {
  83. return operator *=(this_type{c, fill});
  84. }
  85. this_type& operator /=(const T& c) {
  86. return operator /=(this_type{c, fill});
  87. }
  88. this_type operator +(const this_type& other) const {
  89. return this_type(*this) += other;
  90. }
  91. this_type operator -(const this_type& other) const {
  92. return this_type(*this) -= other;
  93. }
  94. this_type operator *(const this_type& other) const {
  95. return this_type(*this) *= other;
  96. }
  97. this_type operator /(const this_type& other) const {
  98. return this_type(*this) /= other;
  99. }
  100. this_type operator +(const T& c) const {
  101. return operator +(this_type{c, fill});
  102. }
  103. this_type operator -(const T& c) const {
  104. return operator -(this_type{c, fill});
  105. }
  106. this_type operator *(const T& c) const {
  107. return operator *(this_type{c, fill});
  108. }
  109. this_type operator /(const T& c) const {
  110. return operator /(this_type{c, fill});
  111. }
  112. this_type operator -() const { return -1 * (*this); }
  113. bool operator ==(const this_type& other) const {
  114. return !memcmp(data, other.data, D * sizeof(T));
  115. } // technically wrong on float/double
  116. bool operator !=(const this_type& other) const {
  117. return !operator==(other);
  118. }
  119. bool operator <=(const this_type& other) const {
  120. for (size_t i = 0; i < D; ++i) { if (at(i) > other.at(i)) return false; }
  121. return true;
  122. }
  123. bool operator <(const this_type& other) const {
  124. for (size_t i = 0; i < D; ++i) { if (at(i) >= other.at(i)) return false; }
  125. return true;
  126. }
  127. bool operator >=(const this_type& other) const {
  128. for (size_t i = 0; i < D; ++i) { if (at(i) < other.at(i)) return false; }
  129. return true;
  130. }
  131. bool operator >(const this_type& other) const {
  132. for (size_t i = 0; i < D; ++i) { if (at(i) <= other.at(i)) return false; }
  133. return true;
  134. }
  135. template <size_t D2>
  136. typename std::enable_if<(D == 2 || D == 3) && D == D2, raw_vector<3, T> >::type cross(const raw_vector<D2, T>& v) const {
  137. return D == 2
  138. ? raw_vector<3, T>{0, 0, at(0)*v.at(1) - at(1)*v.at(0)}
  139. : raw_vector<3, T>{
  140. at(1)*v.at(2) - at(2)*v.at(1),
  141. at(2)*v.at(0) - at(0)*v.at(2),
  142. at(0)*v.at(1) - at(1)*v.at(0)
  143. };
  144. }
  145. T dot(const this_type& v) const {
  146. T accum{};
  147. for (size_t i = 0; i < D; ++i) accum += data[i] * v[i];
  148. return accum;
  149. }
  150. T lengthSquared() const {
  151. return dot(*this);
  152. }
  153. sqrt_type length() const {
  154. return std::sqrt(lengthSquared());
  155. }
  156. T distanceSquared(const this_type& other) const {
  157. return operator -(other).lengthSquared();
  158. }
  159. sqrt_type distance(const this_type& other) const {
  160. return std::sqrt(distanceSquared(other));
  161. }
  162. raw_vector<D, sqrt_type> unit() const { return raw_vector<D, sqrt_type>(*this)/length(); }
  163. void swap(raw_vector& that) {
  164. using std::swap;
  165. for (size_t i = 0; i < D; ++i) swap(data[i], that.data[i]);
  166. }
  167. constexpr raw_vector() = default;
  168. raw_vector(const this_type& that) {
  169. for (size_t i = 0; i < D; ++i) data[i] = that.data[i];
  170. }
  171. raw_vector(this_type&& that) {
  172. for (size_t i = 0; i < D; ++i) data[i] = that.data[i];
  173. }
  174. raw_vector& operator=(const this_type& other) {
  175. for (size_t i = 0; i < D; ++i) data[i] = other.data[i];
  176. return *this;
  177. }
  178. raw_vector& operator=(this_type&& other) {
  179. for (size_t i = 0; i < D; ++i) data[i] = other.data[i];
  180. return *this;
  181. }
  182. raw_vector(std::initializer_list<T> vals) {
  183. const size_t D2 = vals.size();
  184. const T* ptr = vals.begin();
  185. for (size_t i = 0; i < std::min(D, D2); ++i) data[i] = *ptr++;
  186. for (size_t j = std::min(D, D2); j < std::max(D, D2); ++j) data[j] = T();
  187. }
  188. template <std::size_t D2, typename T2,
  189. typename = typename std::enable_if<D2 != D || !std::is_same<T, T2>::value>::type>
  190. explicit raw_vector(const raw_vector<D2, T2>& that) {
  191. for (size_t i = 0; i < std::min(D, D2); ++i) data[i] = static_cast<T>(that[i]);
  192. for (size_t j = std::min(D, D2); j < std::max(D, D2); ++j) data[j] = T();
  193. }
  194. raw_vector(T const& t, fill_t) {
  195. for (size_t i = 0; i < D; ++i) data[i] = t;
  196. }
  197. private:
  198. value_type data[D] = {0};
  199. };
  200. } }
  201. template <size_t D, typename T>
  202. math::vector::raw_vector<D, T> operator +(T const& lhs, math::vector::raw_vector<D, T> const& rhs) {
  203. return rhs + lhs;
  204. }
  205. template <size_t D, typename T>
  206. math::vector::raw_vector<D, T> operator -(T const& lhs, math::vector::raw_vector<D, T> const& rhs) {
  207. return math::vector::raw_vector<D, T>(lhs, math::vector::fill) - rhs;
  208. }
  209. template <size_t D, typename T>
  210. math::vector::raw_vector<D, T> operator *(T const& lhs, math::vector::raw_vector<D, T> const& rhs) {
  211. return rhs * lhs;
  212. }
  213. template <size_t D, typename T>
  214. math::vector::raw_vector<D, T> operator /(T const& lhs, math::vector::raw_vector<D, T> const& rhs) {
  215. return math::vector::raw_vector<D, T>(lhs, math::vector::fill) / rhs;
  216. }
  217. template <typename CharT, typename Traits, size_t N, typename T>
  218. std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, const math::vector::raw_vector<N, T>& v) {
  219. out << '(' << v[0];
  220. for (size_t i = 1; i < N; ++i) { out << ',' << v[i]; }
  221. out << ')';
  222. return out;
  223. }
  224. namespace std {
  225. template <size_t D, typename T>
  226. math::vector::raw_vector<D, T> abs(math::vector::raw_vector<D, T> const& data) {
  227. math::vector::raw_vector<D, T> rval;
  228. for (size_t i = 0; i < D; ++i) rval[i] = std::abs(data[i]);
  229. return rval;
  230. }
  231. }