💿🐜 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.

177 lines
4.1 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_STREAM_OPERATORS_HPP
  20. #define ANTKEEPER_MATH_STREAM_OPERATORS_HPP
  21. #include "math/vector-type.hpp"
  22. #include "math/matrix-type.hpp"
  23. #include "math/quaternion-type.hpp"
  24. #include <ostream>
  25. namespace math {
  26. namespace stream_operators {
  27. /// @addtogroup io
  28. /// @{
  29. /**
  30. * Writes the elements of a vector to an output stream, with each element delimeted by a space.
  31. *
  32. * @param os Output stream.
  33. * @param v Vector.
  34. * @return Output stream.
  35. */
  36. template <class T, std::size_t N>
  37. std::ostream& operator<<(std::ostream& os, const vector<T, N>& v);
  38. /**
  39. * Writes the elements of a matrix to an output stream, with each element delimeted by a space.
  40. *
  41. * @param os Output stream.
  42. * @param m Matrix.
  43. * @return Output stream.
  44. */
  45. template <class T, std::size_t N, std::size_t M>
  46. std::ostream& operator<<(std::ostream& os, const matrix<T, N, M>& m);
  47. /**
  48. * Writes the real and imaginary parts of a quaternion to an output stream, with each number delimeted by a space.
  49. *
  50. * @param os Output stream.
  51. * @param q Quaternion.
  52. * @return Output stream.
  53. */
  54. template <class T>
  55. std::ostream& operator<<(std::ostream& os, const quaternion<T>& q);
  56. /**
  57. * Reads the elements of a vector from an input stream, with each element delimeted by a space.
  58. *
  59. * @param is Input stream.
  60. * @param v Vector.
  61. * @return Input stream.
  62. */
  63. template <class T, std::size_t N>
  64. std::istream& operator>>(std::istream& is, vector<T, N>& v);
  65. /**
  66. * Reads the elements of a matrix from an input stream, with each element delimeted by a space.
  67. *
  68. * @param is Input stream.
  69. * @param m Matrix.
  70. * @return Input stream.
  71. */
  72. template <class T, std::size_t N, std::size_t M>
  73. std::istream& operator>>(std::istream& is, matrix<T, N, M>& m);
  74. /**
  75. * Reads the real and imaginary parts of a quaternion from an input stream, with each number delimeted by a space.
  76. *
  77. * @param is Input stream.
  78. * @param q Quaternion.
  79. * @return Input stream.
  80. */
  81. template <class T>
  82. std::istream& operator>>(std::istream& is, const quaternion<T>& q);
  83. template <class T, std::size_t N>
  84. std::ostream& operator<<(std::ostream& os, const vector<T, N>& v)
  85. {
  86. for (std::size_t i = 0; i < N; ++i)
  87. {
  88. os << v[i];
  89. if (i < N - 1)
  90. {
  91. os << ' ';
  92. }
  93. }
  94. return os;
  95. }
  96. template <class T, std::size_t N, std::size_t M>
  97. std::ostream& operator<<(std::ostream& os, const matrix<T, N, M>& m)
  98. {
  99. for (std::size_t i = 0; i < N; ++i)
  100. {
  101. for (std::size_t j = 0; j < M; ++j)
  102. {
  103. os << m[i][j];
  104. if (i < N - 1 || j < M - 1)
  105. {
  106. os << ' ';
  107. }
  108. }
  109. }
  110. return os;
  111. }
  112. template <class T>
  113. std::ostream& operator<<(std::ostream& os, const quaternion<T>& q)
  114. {
  115. os << q.w << ' ' << q.x << ' ' << q.y << ' ' << q.z;
  116. return os;
  117. }
  118. template <class T, std::size_t N>
  119. std::istream& operator>>(std::istream& is, vector<T, N>& v)
  120. {
  121. for (std::size_t i = 0; i < N; ++i)
  122. is >> v[i];
  123. return is;
  124. }
  125. template <class T, std::size_t N, std::size_t M>
  126. std::istream& operator>>(std::istream& is, matrix<T, N, M>& m)
  127. {
  128. for (std::size_t i = 0; i < N * M; ++i)
  129. {
  130. std::size_t j = i / M;
  131. std::size_t k = i % M;
  132. is >> m[j][k];
  133. }
  134. return is;
  135. }
  136. template <class T>
  137. std::istream& operator>>(std::istream& is, const quaternion<T>& q)
  138. {
  139. is >> q.w;
  140. is >> q.x;
  141. is >> q.y;
  142. is >> q.z;
  143. return is;
  144. }
  145. /// @}
  146. } // namespace stream_operators
  147. } // namespace math
  148. // Bring stream operators into global namespace
  149. using namespace math::stream_operators;
  150. #endif // ANTKEEPER_MATH_STREAM_OPERATORS_HPP