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

163 lines
4.0 KiB

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