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

114 lines
2.8 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. template <class T, std::size_t N>
  57. std::ostream& operator<<(std::ostream& os, const vector<T, N>& v)
  58. {
  59. for (std::size_t i = 0; i < N; ++i)
  60. {
  61. os << v[i];
  62. if (i < N - 1)
  63. {
  64. os << ' ';
  65. }
  66. }
  67. return os;
  68. }
  69. template <class T, std::size_t N, std::size_t M>
  70. std::ostream& operator<<(std::ostream& os, const matrix<T, N, M>& m)
  71. {
  72. for (std::size_t i = 0; i < N; ++i)
  73. {
  74. for (std::size_t j = 0; j < M; ++j)
  75. {
  76. os << m[i][j];
  77. if (i < N - 1 || j < M - 1)
  78. {
  79. os << ' ';
  80. }
  81. }
  82. }
  83. return os;
  84. }
  85. template <class T>
  86. std::ostream& operator<<(std::ostream& os, const quaternion<T>& q)
  87. {
  88. os << std::get<0>(q) << ' ' << std::get<1>(q)[0] << ' ' << std::get<1>(q)[1] << ' ' << std::get<1>(q)[2];
  89. return os;
  90. }
  91. /// @}
  92. } // namespace stream_operators
  93. } // namespace math
  94. // Bring stream operators into global namespace
  95. using namespace math::stream_operators;
  96. #endif // ANTKEEPER_MATH_STREAM_OPERATORS_HPP