Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfMatrix.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_TYPES_TFMATRIX_H_
21#define _SOURCE_TYPES_TFMATRIX_H_
22
23#include <tfError.h>
24
25#include "tfVector.h"
26
27#include <Magnum/Magnum.h>
28#include <Magnum/Math/Matrix.h>
29
30#include <ostream>
31
32
34
35
36 template<std::size_t size, class T> using MatrixBase = Magnum::Math::Matrix<size, T>;
37
38 template<std::size_t size, class T>
39 class TMatrixS : public MatrixBase<size, T> {
40 public:
41 constexpr TMatrixS() noexcept: MatrixBase<size, T>() {}
42
43 template<class ...U> constexpr TMatrixS(const TVectorS<size, T>& first, const U&... next) noexcept: MatrixBase<size, T>(first, next...) {}
44
45 constexpr explicit TMatrixS(T value) noexcept: MatrixBase<size, T>(value) {}
46
47 template<class U> constexpr explicit TMatrixS(const TMatrixS<size, U>& other) noexcept: MatrixBase<size, T>((MatrixBase<size, U>)other) {}
48
49 template<std::size_t otherSize> constexpr explicit TMatrixS(const TMatrixS<otherSize, T>& other) noexcept: MatrixBase<size, T>((MatrixBase<otherSize, T>)other) {}
50
51 constexpr /*implicit*/ TMatrixS(const TMatrixS<size, T>& other) noexcept: MatrixBase<size, T>((MatrixBase<size, T>)other) {}
52
54 bool isOrthogonal() const { return MatrixBase<size, T>::isOrthogonal(); };
55
57 T trace() const { return MatrixBase<size, T>::trace(); }
58
60 TMatrixS<size-1, T> ij(std::size_t skipCol, std::size_t skipRow) const { return (TMatrixS<size-1, T>)MatrixBase<size-1, T>::ij(skipCol, skipRow); }
61
63 T cofactor(std::size_t col, std::size_t row) const { return MatrixBase<size, T>::cofactor(col, row); }
64
66 TMatrixS<size, T> comatrix() const { return (TMatrixS<size, T>)MatrixBase<size, T>::comatrix(); }
67
69 TMatrixS<size, T> adjugate() const { return (TMatrixS<size, T>)MatrixBase<size, T>::adjugate(); }
70
72 T determinant() const { return MatrixBase<size, T>::determinant(); }
73
75 TMatrixS<size, T> inverted() const { return (TMatrixS<size, T>)MatrixBase<size, T>::inverted(); }
76
78 TMatrixS<size, T> invertedOrthogonal() const { return (TMatrixS<size, T>)MatrixBase<size, T>::invertedOrthogonal(); }
79
80 /* Reimplementation of functions to return correct type */
81
82 TMatrixS<size, T> operator*(const TMatrixS<size, T>& other) const { return (TMatrixS<size, T>)MatrixBase<size, T>::operator*((MatrixBase<size, T>)other); }
83 TVectorS<size, T> operator*(const TVectorS<size, T>& other) const { return (TVectorS<size, T>)MatrixBase<size, T>::operator*(other); }
84
86 TMatrixS<size, T> transposed() const { return (TMatrixS<size, T>)MatrixBase<size, T>::transposed(); }
87
89 static TMatrixS<size, T>& from(T* data) { return (TMatrixS<size, T>)MatrixBase<size, T>::from(data); }
90
92 static const TMatrixS<size, T>& from(const T* data) { return (TMatrixS<size, T>)MatrixBase<size, T>::from(data); }
93
94 TMatrixS<size, T> operator-() const {
95 return (TMatrixS<size, T>)MatrixBase<size, T>::operator-();
96 }
97 TMatrixS<size, T>& operator+=(const TMatrixS<size, T>& other) {
98 MatrixBase<size, T>::operator+=((MatrixBase<size, T>)other);
99 return *this;
100 }
101 TMatrixS<size, T> operator+(const TMatrixS<size, T>& other) const {
102 return (TMatrixS<size, T>)MatrixBase<size, T>::operator+((MatrixBase<size, T>)other);
103 }
104 TMatrixS<size, T>& operator-=(const TMatrixS<size, T>& other) {
105 MatrixBase<size, T>::operator-=((MatrixBase<size, T>)other);
106 return *this;
107 }
108 TMatrixS<size, T> operator-(const TMatrixS<size, T>& other) const {
109 return (TMatrixS<size, T>)MatrixBase<size, T>::operator-((MatrixBase<size, T>)other);
110 }
111 TMatrixS<size, T>& operator*=(T number) {
112 MatrixBase<size, T>::operator*=(number);
113 return *this;
114 }
115 TMatrixS<size, T> operator*(T number) const {
116 return (TMatrixS<size, T>)MatrixBase<size, T>::operator*(number);
117 }
118 TMatrixS<size, T>& operator/=(T number) {
119 MatrixBase<size, T>::operator/=(number);
120 return *this;
121 }
122 TMatrixS<size, T> operator/(T number) const {
123 return (TMatrixS<size, T>)MatrixBase<size, T>::operator/(number);
124 }
125
127 constexpr TMatrixS<size, T> flippedCols() const {
128 return (TMatrixS<size, T>)MatrixBase<size, T>::flippedCols();
129 }
130
132 constexpr TMatrixS<size, T> flippedRows() const {
133 return (TMatrixS<size, T>)MatrixBase<size, T>::flippedRows();
134 }
135
136 };
137
138 #define REVISED_MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(size, Type, MagnumImplType, VectorType) \
139 \
140 static Type<T>& from(T* data) { \
141 return *reinterpret_cast<Type<T>*>(&MagnumImplType<T>::from(data)); \
142 } \
143 \
144 Type<T> operator-() const { \
145 return (Type<T>)MagnumImplType<T>::operator-(); \
146 } \
147 Type<T>& operator+=(const Type<T>& other) { \
148 MagnumImplType<T>::operator+=((MagnumImplType<T>)other); \
149 return *this; \
150 } \
151 Type<T> operator+(const Type<T>& other) const { \
152 return (Type<T>)MagnumImplType<T>::operator+((MagnumImplType<T>)other); \
153 } \
154 Type<T>& operator-=(const Type<T>& other) { \
155 MagnumImplType<T>::operator-=((MagnumImplType<T>)other); \
156 return *this; \
157 } \
158 Type<T> operator-(const Type<T>& other) const { \
159 return (Type<T>)MagnumImplType<T>::operator-((MagnumImplType<T>)other); \
160 } \
161 Type<T>& operator*=(T number) { \
162 MagnumImplType<T>::operator*=(number); \
163 return *this; \
164 } \
165 Type<T> operator*(T number) const { \
166 return (Type<T>)MagnumImplType<T>::operator*(number); \
167 } \
168 Type<T>& operator/=(T number) { \
169 MagnumImplType<T>::operator/=(number); \
170 return *this; \
171 } \
172 Type<T> operator/(T number) const { \
173 return (Type<T>)MagnumImplType<T>::operator/(number); \
174 } \
175 \
176 \
177 constexpr Type<T> flippedCols() const { \
178 return (Type<T>)MagnumImplType<T>::flippedCols(); \
179 } \
180 \
181 \
182 constexpr Type<T> flippedRows() const { \
183 return (Type<T>)MagnumImplType<T>::flippedRows(); \
184 } \
185 VectorType<T>& operator[](std::size_t col) { \
186 return static_cast<VectorType<T>&>(MagnumImplType<T>::operator[](col)); \
187 } \
188 constexpr const VectorType<T> operator[](std::size_t col) const { \
189 return VectorType<T>(MagnumImplType<T>::operator[](col)); \
190 } \
191 \
192 \
193 VectorType<T> row(std::size_t row) const { \
194 return VectorType<T>(MagnumImplType<T>::row(row)); \
195 } \
196 \
197 Type<T> operator*(const Type<T>& other) const { \
198 return MagnumImplType<T>::operator*(other); \
199 } \
200 VectorType<T> operator*(const TVectorS<size, T>& other) const { \
201 return (VectorType<T>)MatrixBase<size, T>::operator*(other); \
202 } \
203 \
204 \
205 Type<T> transposed() const { return MatrixBase<size, T>::transposed(); } \
206 constexpr VectorType<T> diagonal() const { \
207 return (VectorType<T>)MatrixBase<size, T>::diagonal(); \
208 } \
209 \
210 \
211 Type<T> inverted() const { return MatrixBase<size, T>::inverted(); } \
212 \
213 \
214 Type<T> invertedOrthogonal() const { \
215 return MatrixBase<size, T>::invertedOrthogonal(); \
216 } \
217 \
218 \
219 T* data() { return MagnumImplType<T>::data(); } \
220 \
221 \
222 constexpr const T* data() const { return MagnumImplType<T>::data(); } \
223
224 #define SWIGPYTHON_MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(size, Type, VectorType) \
225 VectorType<T>& _getitem(int i) { \
226 if(i < 0) return _getitem(size - 1 + i); \
227 return this->operator[](i); \
228 } \
229 void _setitem(int i, const VectorType<T> &val) { \
230 if(i < 0) return _setitem(size - 1 + i, val); \
231 VectorType<T> &item = this->operator[](i); \
232 item = val; \
233 } \
234 int __len__() { return size; } \
235 std::vector<std::vector<T> >& asVectors() { \
236 std::vector<std::vector<T> > *result = new std::vector<std::vector<T> >(size); \
237 for(int i = 0; i < size; ++i) { \
238 std::vector<T> &item = (*result)[i]; \
239 item = this->operator[](i); \
240 } \
241 return *result; \
242 } \
243
244 #define MAGNUM_BASE_MATRIX_CAST_METHODS(size, Type, MagnumImplType) \
245 template<class U = T> \
246 Type(const MagnumImplType<U>& other) : MagnumImplType<T>(other) {} \
247 template<class U = T> \
248 operator MagnumImplType<U>*() { return static_cast<MagnumImplType<U>*>(this); } \
249 template<class U = T> \
250 operator const MagnumImplType<U>*() { \
251 return static_cast<const MagnumImplType<U>*>(this); \
252 } \
253 \
254 template<class U = T> \
255 Type(const MatrixBase<size, U>& other) : MagnumImplType<T>(other) {} \
256 \
257 operator std::vector<std::vector<T> >&() const { \
258 std::vector<T> *result = new std::vector<T>(size); \
259 for(int i = 0; i < size; ++i) { \
260 std::vector<T> *c = new std::vector<T>(this->operator[](i)); \
261 *result[i] = *c; \
262 } \
263 return *result; \
264 } \
265
266 }
267
268template<std::size_t size, class T>
269inline std::ostream& operator<<(std::ostream& os, const TissueForge::types::TMatrixS<size, T>& m)
270{
271 os << std::string("{");
272 for(int i = 0; i < size; ++i) os << m.row(i) << std::string(",") << std::endl;
273 os << std::string("}");
274 return os;
275}
276
277#define TF_MATRIX_IMPL_OSTREAM(type) \
278 template<class T> \
279 inline std::ostream& operator<<(std::ostream& os, const type<T>& m) \
280 { \
281 os << std::string("{"); \
282 for(int i = 0; i < m.Size; ++i) os << m.row(i) << std::string(",") << std::endl;\
283 os << std::string("}"); \
284 return os; \
285 } \
286
287#endif // _SOURCE_TYPES_TFMATRIX_H_
Definition tfMatrix.h:39
T determinant() const
Definition tfMatrix.h:72
TMatrixS< size, T > inverted() const
Definition tfMatrix.h:75
static TMatrixS< size, T > & from(T *data)
Definition tfMatrix.h:89
TMatrixS< size, T > comatrix() const
Definition tfMatrix.h:66
constexpr TMatrixS< size, T > flippedRows() const
Definition tfMatrix.h:132
TMatrixS< size, T > invertedOrthogonal() const
Definition tfMatrix.h:78
TMatrixS< size-1, T > ij(std::size_t skipCol, std::size_t skipRow) const
Definition tfMatrix.h:60
constexpr TMatrixS< size, T > flippedCols() const
Definition tfMatrix.h:127
bool isOrthogonal() const
Definition tfMatrix.h:54
static const TMatrixS< size, T > & from(const T *data)
Definition tfMatrix.h:92
TMatrixS< size, T > adjugate() const
Definition tfMatrix.h:69
T cofactor(std::size_t col, std::size_t row) const
Definition tfMatrix.h:63
TMatrixS< size, T > transposed() const
Definition tfMatrix.h:86
T trace() const
Definition tfMatrix.h:57
Definition tfVector.h:38
Native Tissue Forge type definitions.
Definition tfMatrix.h:33