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

154 lines
4.1 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_VECTOR_TYPE_HPP
  20. #define ANTKEEPER_MATH_VECTOR_TYPE_HPP
  21. #include <cstddef>
  22. namespace math {
  23. /**
  24. * An `N`-dimensional Euclidean vector.
  25. *
  26. * @tparam T Scalar type.
  27. * @tparam N Number of dimensions.
  28. */
  29. template <typename T, std::size_t N>
  30. struct vector
  31. {
  32. typedef T element_type;
  33. element_type elements[N];
  34. inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; }
  35. inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; }
  36. inline constexpr const element_type* data() const noexcept { return elements; };
  37. inline constexpr element_type* data() noexcept { return elements; };
  38. inline constexpr std::size_t size() const noexcept { return N; };
  39. };
  40. template <typename T>
  41. struct vector<T, 1>
  42. {
  43. typedef T element_type;
  44. union
  45. {
  46. element_type elements[1];
  47. struct
  48. {
  49. element_type x;
  50. };
  51. };
  52. inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; }
  53. inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; }
  54. inline constexpr const element_type* data() const noexcept { return elements; };
  55. inline constexpr element_type* data() noexcept { return elements; };
  56. inline constexpr std::size_t size() const noexcept { return 1; };
  57. };
  58. template <typename T>
  59. struct vector<T, 2>
  60. {
  61. typedef T element_type;
  62. union
  63. {
  64. element_type elements[2];
  65. struct
  66. {
  67. element_type x;
  68. element_type y;
  69. };
  70. };
  71. inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; }
  72. inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; }
  73. inline constexpr const element_type* data() const noexcept { return elements; };
  74. inline constexpr element_type* data() noexcept { return elements; };
  75. inline constexpr std::size_t size() const noexcept { return 2; };
  76. };
  77. template <typename T>
  78. struct vector<T, 3>
  79. {
  80. typedef T element_type;
  81. union
  82. {
  83. element_type elements[3];
  84. struct
  85. {
  86. element_type x;
  87. element_type y;
  88. element_type z;
  89. };
  90. };
  91. inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; }
  92. inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; }
  93. inline constexpr const element_type* data() const noexcept { return elements; };
  94. inline constexpr element_type* data() noexcept { return elements; };
  95. inline constexpr std::size_t size() const noexcept { return 3; };
  96. };
  97. template <typename T>
  98. struct vector<T, 4>
  99. {
  100. typedef T element_type;
  101. union
  102. {
  103. element_type elements[4];
  104. struct
  105. {
  106. element_type x;
  107. element_type y;
  108. element_type z;
  109. element_type w;
  110. };
  111. };
  112. inline constexpr element_type& operator[](std::size_t i) noexcept { return elements[i]; }
  113. inline constexpr const element_type& operator[](std::size_t i) const noexcept { return elements[i]; }
  114. inline constexpr const element_type* data() const noexcept { return elements; };
  115. inline constexpr element_type* data() noexcept { return elements; };
  116. inline constexpr std::size_t size() const noexcept { return 4; };
  117. };
  118. /// 2D vector.
  119. template <typename T>
  120. using vector2 = vector<T, 2>;
  121. /// 3D vector.
  122. template <typename T>
  123. using vector3 = vector<T, 3>;
  124. /// 4D vector.
  125. template <typename T>
  126. using vector4 = vector<T, 4>;
  127. } // namespace math
  128. #endif // ANTKEEPER_MATH_VECTOR_TYPE_HPP