Matrix.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef PLAYRHO_COMMON_MATRIX_HPP
23 #define PLAYRHO_COMMON_MATRIX_HPP
24 
28 #include <PlayRho/Common/Real.hpp>
29 #include <PlayRho/Common/Units.hpp>
30 
31 namespace playrho {
32 
38 template <typename T, std::size_t M, std::size_t N>
40 
45 
55 template <typename>
56 struct IsMatrix: std::false_type {};
57 
67 template <typename T, std::size_t M, std::size_t N>
68 struct IsMatrix<Vector<Vector<T, N>, M>>: std::true_type {};
69 
80 template <typename>
81 struct IsSquareMatrix: std::false_type {};
82 
92 template <typename T, std::size_t M>
93 struct IsSquareMatrix<Vector<Vector<T, M>, M>>: std::true_type {};
94 
96 
100 template <typename T, std::size_t N>
101 PLAYRHO_CONSTEXPR inline
102 std::enable_if_t<!IsVector<T>::value, Matrix<T, N, N>> GetIdentityMatrix()
103 {
104  auto result = Matrix<Real, N, N>{};
105  for (auto i = std::size_t{0}; i < N; ++i)
106  {
107  result[i][i] = T{1};
108  }
109  return result;
110 }
111 
115 template <typename T>
116 PLAYRHO_CONSTEXPR inline std::enable_if_t<IsSquareMatrix<T>::value, T> GetIdentity()
117 {
118  return GetIdentityMatrix<typename T::value_type::value_type, std::tuple_size<T>::value>();
119 }
120 
122 template <typename T, std::size_t N>
123 PLAYRHO_CONSTEXPR inline
124 std::enable_if_t<!IsVector<T>::value, Vector<Vector<T, N>, 1>> GetRowMatrix(Vector<T, N> arg)
125 {
126  return Vector<Vector<T, N>, 1>{arg};
127 }
128 
130 template <typename T, std::size_t N>
131 PLAYRHO_CONSTEXPR inline
132 std::enable_if_t<!IsVector<T>::value, Vector<Vector<T, 1>, N>> GetColumnMatrix(Vector<T, N> arg)
133 {
134  auto result = Vector<Vector<T, 1>, N>{};
135  for (auto i = std::size_t{0}; i < N; ++i)
136  {
137  result[i][0] = arg[i];
138  }
139  return result;
140 }
141 
144 template <typename T, std::size_t M, std::size_t N>
145 PLAYRHO_CONSTEXPR inline
146 auto operator+ (const Matrix<T, M, N>& lhs, const Matrix<T, M, N>& rhs) noexcept
147 {
148  auto result = Matrix<T, M, N>{};
149  for (auto m = decltype(M){0}; m < M; ++m)
150  {
151  for (auto n = decltype(N){0}; n < N; ++n)
152  {
153  result[m][n] = lhs[m][n] + rhs[m][n];
154  }
155  }
156  return result;
157 }
158 
161 template <typename T, std::size_t M, std::size_t N>
162 PLAYRHO_CONSTEXPR inline
163 auto operator- (const Matrix<T, M, N>& lhs, const Matrix<T, M, N>& rhs) noexcept
164 {
165  auto result = Matrix<T, M, N>{};
166  for (auto m = decltype(M){0}; m < M; ++m)
167  {
168  for (auto n = decltype(N){0}; n < N; ++n)
169  {
170  result[m][n] = lhs[m][n] - rhs[m][n];
171  }
172  }
173  return result;
174 }
175 
177 template <typename T>
179 
181 template <typename T>
183 
186 
189 
192 
195 
197 template <>
198 PLAYRHO_CONSTEXPR inline bool IsValid(const Mat22& value) noexcept
199 {
200  return IsValid(get<0>(value)) && IsValid(get<1>(value));
201 }
202 
204 template <>
205 PLAYRHO_CONSTEXPR inline Mat22 GetInvalid() noexcept
206 {
207  return Mat22{GetInvalid<Vec2>(), GetInvalid<Vec2>()};
208 }
209 
210 } // namespace playrho
211 
212 #endif // PLAYRHO_COMMON_MATRIX_HPP