💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.6 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_VECTOR_OPERATORS_HPP
  20. #define ANTKEEPER_MATH_VECTOR_OPERATORS_HPP
  21. #include "math/vector-type.hpp"
  22. #include "math/vector-functions.hpp"
  23. namespace math {
  24. namespace vector_operators {
  25. /// @addtogroup vector
  26. /// @{
  27. /// @copydoc add(const vector<T, N>&, const vector<T, N>&)
  28. template <class T, std::size_t N>
  29. vector<T, N> operator+(const vector<T, N>& x, const vector<T, N>& y);
  30. /// @copydoc div(const vector<T, N>&, const vector<T, N>&)
  31. template <class T, std::size_t N>
  32. vector<T, N> operator/(const vector<T, N>& x, const vector<T, N>& y);
  33. /// @copydoc div(const vector<T, N>&, T)
  34. template <class T, std::size_t N>
  35. vector<T, N> operator/(const vector<T, N>& v, T s);
  36. /// @copydoc mul(const vector<T, N>&, const vector<T, N>&)
  37. template <class T, std::size_t N>
  38. vector<T, N> operator*(const vector<T, N>& x, const vector<T, N>& y);
  39. /// @copydoc mul(const vector<T, N>&, T)
  40. template <class T, std::size_t N>
  41. vector<T, N> operator*(const vector<T, N>& v, T s);
  42. /// @copydoc mul(const vector<T, N>&, T)
  43. template <class T, std::size_t N>
  44. vector<T, N> operator*(T s, const vector<T, N>& v);
  45. /// @copydoc negate(const vector<T, N>&)
  46. template <class T, std::size_t N>
  47. vector<T, N> operator-(const vector<T, N>& x);
  48. /// @copydoc sub(const vector<T, N>&, const vector<T, N>&)
  49. template <class T, std::size_t N>
  50. vector<T, N> operator-(const vector<T, N>& x, const vector<T, N>& y);
  51. /**
  52. * Adds two vectors and stores the result in the first vector.
  53. *
  54. * @param x First vector.
  55. * @param y Second vector.
  56. * @return Reference to the first vector.
  57. */
  58. template <class T, std::size_t N>
  59. vector<T, N>& operator+=(vector<T, N>& x, const vector<T, N>& y);
  60. /**
  61. * Subtracts two vectors and stores the result in the first vector.
  62. *
  63. * @param x First vector.
  64. * @param y Second vector.
  65. * @return Reference to the first vector.
  66. */
  67. template <class T, std::size_t N>
  68. vector<T, N>& operator-=(vector<T, N>& x, const vector<T, N>& y);
  69. /**
  70. * Multiplies two vectors and stores the result in the first vector.
  71. *
  72. * @param x First vector.
  73. * @param y Second vector.
  74. * @return Reference to the first vector.
  75. */
  76. template <class T, std::size_t N>
  77. vector<T, N>& operator*=(vector<T, N>& x, const vector<T, N>& y);
  78. /**
  79. * Multiplies a vector and a scalar and stores the result in the vector.
  80. *
  81. * @param v Vector.
  82. * @param s Scalar.
  83. * @return Reference to the vector.
  84. */
  85. template <class T, std::size_t N>
  86. vector<T, N>& operator*=(vector<T, N>& v, T s);
  87. /**
  88. * Divides the first vector by the second vector the result in the first vector.
  89. *
  90. * @param x First vector.
  91. * @param y Second vector.
  92. * @return Reference to the first vector.
  93. */
  94. template <class T, std::size_t N>
  95. vector<T, N>& operator/=(vector<T, N>& x, const vector<T, N>& y);
  96. /**
  97. * Divides a vector by a scalar and stores the result in the vector.
  98. *
  99. * @param v Vector.
  100. * @param s Scalar.
  101. * @return Reference to the vector.
  102. */
  103. template <class T, std::size_t N>
  104. vector<T, N>& operator/=(vector<T, N>& v, T s);
  105. template <class T, std::size_t N>
  106. inline vector<T, N> operator+(const vector<T, N>& x, const vector<T, N>& y)
  107. {
  108. return add(x, y);
  109. }
  110. template <class T, std::size_t N>
  111. inline vector<T, N> operator-(const vector<T, N>& x)
  112. {
  113. return negate(x);
  114. }
  115. template <class T, std::size_t N>
  116. inline vector<T, N> operator-(const vector<T, N>& x, const vector<T, N>& y)
  117. {
  118. return sub(x, y);
  119. }
  120. template <class T, std::size_t N>
  121. inline vector<T, N> operator*(const vector<T, N>& x, const vector<T, N>& y)
  122. {
  123. return mul(x, y);
  124. }
  125. template <class T, std::size_t N>
  126. inline vector<T, N> operator*(const vector<T, N>& v, T s)
  127. {
  128. return mul(v, s);
  129. }
  130. template <class T, std::size_t N>
  131. inline vector<T, N> operator*(T s, const vector<T, N>& v)
  132. {
  133. return mul(v, s);
  134. }
  135. template <class T, std::size_t N>
  136. inline vector<T, N> operator/(const vector<T, N>& x, const vector<T, N>& y)
  137. {
  138. return div(x, y);
  139. }
  140. template <class T, std::size_t N>
  141. inline vector<T, N> operator/(const vector<T, N>& v, T s)
  142. {
  143. return div(v, s);
  144. }
  145. template <class T, std::size_t N>
  146. inline vector<T, N>& operator+=(vector<T, N>& x, const vector<T, N>& y)
  147. {
  148. return (x = x + y);
  149. }
  150. template <class T, std::size_t N>
  151. inline vector<T, N>& operator-=(vector<T, N>& x, const vector<T, N>& y)
  152. {
  153. return (x = x - y);
  154. }
  155. template <class T, std::size_t N>
  156. inline vector<T, N>& operator*=(vector<T, N>& x, const vector<T, N>& y)
  157. {
  158. return (x = x * y);
  159. }
  160. template <class T, std::size_t N>
  161. inline vector<T, N>& operator*=(vector<T, N>& v, T s)
  162. {
  163. return (v = v * s);
  164. }
  165. template <class T, std::size_t N>
  166. inline vector<T, N>& operator/=(vector<T, N>& x, const vector<T, N>& y)
  167. {
  168. return (x = x * y);
  169. }
  170. template <class T, std::size_t N>
  171. inline vector<T, N>& operator/=(vector<T, N>& v, T s)
  172. {
  173. return (v = v / s);
  174. }
  175. /// @}
  176. } // namespace vector_operators
  177. } // namespace math
  178. // Bring vector operators into global namespace
  179. using namespace math::vector_operators;
  180. #endif // ANTKEEPER_MATH_VECTOR_OPERATORS_HPP