| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGMatrix33.h |
| 4 | Author: Tony Peden, Jon Berndt, Mathias Frolich |
| 5 | Date started: Unknown |
| 6 | |
| 7 | ------------- Copyright (C) 2001 Jon S. Berndt ([email protected]) ------------- |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it under |
| 10 | the terms of the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | ??/??/?? TP Created |
| 29 | 03/16/2000 JSB Added exception throwing |
| 30 | 03/06/2004 MF Rework of the code to make it a bit compiler friendlier |
| 31 | |
| 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 33 | SENTRY |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 35 | |
| 36 | #ifndef FGMATRIX33_H |
| 37 | #define FGMATRIX33_H |
| 38 | |
| 39 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | INCLUDES |
| 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 42 | |
| 43 | #include <string> |
| 44 | #include <iosfwd> |
| 45 | |
| 46 | #include "FGJSBBase.h" |
| 47 | #include "FGColumnVector3.h" |
| 48 | |
| 49 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 50 | FORWARD DECLARATIONS |
| 51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 52 | |
| 53 | namespace JSBSim { |
| 54 | |
| 55 | class FGQuaternion; |
| 56 | |
| 57 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 58 | CLASS DOCUMENTATION |
| 59 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 60 | |
| 61 | /** Handles matrix math operations. |
| 62 | @author Tony Peden, Jon Berndt, Mathias Froelich |
| 63 | */ |
| 64 | |
| 65 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 | DECLARATION: FGMatrix33 |
| 67 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 68 | |
| 69 | class JSBSIM_API FGMatrix33 |
| 70 | { |
| 71 | public: |
| 72 | |
| 73 | enum { |
| 74 | eRows = 3, |
| 75 | eColumns = 3 |
| 76 | }; |
| 77 | |
| 78 | /** Default initializer. |
| 79 | |
| 80 | Create a zero matrix. |
| 81 | */ |
| 82 | FGMatrix33(void); |
| 83 | |
| 84 | /** Copy constructor. |
| 85 | |
| 86 | @param M Matrix which is used for initialization. |
| 87 | |
| 88 | Create copy of the matrix given in the argument. |
| 89 | */ |
| 90 | FGMatrix33(const FGMatrix33& M) |
| 91 | { |
| 92 | data[0] = M.data[0]; |
| 93 | data[1] = M.data[1]; |
| 94 | data[2] = M.data[2]; |
| 95 | data[3] = M.data[3]; |
| 96 | data[4] = M.data[4]; |
| 97 | data[5] = M.data[5]; |
| 98 | data[6] = M.data[6]; |
| 99 | data[7] = M.data[7]; |
| 100 | data[8] = M.data[8]; |
| 101 | } |
| 102 | |
| 103 | /** Initialization by given values. |
| 104 | |
| 105 | @param m11 value of the 1,1 Matrix element. |
| 106 | @param m12 value of the 1,2 Matrix element. |
| 107 | @param m13 value of the 1,3 Matrix element. |
| 108 | @param m21 value of the 2,1 Matrix element. |
| 109 | @param m22 value of the 2,2 Matrix element. |
| 110 | @param m23 value of the 2,3 Matrix element. |
| 111 | @param m31 value of the 3,1 Matrix element. |
| 112 | @param m32 value of the 3,2 Matrix element. |
| 113 | @param m33 value of the 3,3 Matrix element. |
| 114 | |
| 115 | Create a matrix from the doubles given in the arguments. |
| 116 | */ |
| 117 | FGMatrix33(const double m11, const double m12, const double m13, |
| 118 | const double m21, const double m22, const double m23, |
| 119 | const double m31, const double m32, const double m33) |
| 120 | { |
| 121 | data[0] = m11; |
| 122 | data[1] = m21; |
| 123 | data[2] = m31; |
| 124 | data[3] = m12; |
| 125 | data[4] = m22; |
| 126 | data[5] = m32; |
| 127 | data[6] = m13; |
| 128 | data[7] = m23; |
| 129 | data[8] = m33; |
| 130 | } |
| 131 | |
| 132 | /** Destructor. |
| 133 | */ |
| 134 | ~FGMatrix33(void) {} |
| 135 | |
| 136 | /** Prints the contents of the matrix. |
| 137 | @param delimeter the item separator (tab or comma) |
| 138 | @return a string with the delimeter-separated contents of the matrix */ |
| 139 | std::string Dump(const std::string& delimeter) const; |
| 140 | |
| 141 | /** Prints the contents of the matrix. |
| 142 | @param delimeter the item separator (tab or comma, etc.) |
| 143 | @param prefix an additional prefix that is used to indent the 3X3 matrix |
| 144 | printout |
| 145 | @return a string with the delimeter-separated contents of the matrix */ |
| 146 | std::string Dump(const std::string& delimiter, const std::string& prefix) const; |
| 147 | |
| 148 | /** Read access the entries of the matrix. |
| 149 | @param row Row index. |
| 150 | @param col Column index. |
| 151 | |
| 152 | @return the value of the matrix entry at the given row and |
| 153 | column indices. Indices are counted starting with 1. |
| 154 | */ |
| 155 | double operator()(unsigned int row, unsigned int col) const { |
| 156 | return data[(col-1)*eRows+row-1]; |
| 157 | } |
| 158 | |
| 159 | /** Write access the entries of the matrix. |
| 160 | Note that the indices given in the arguments are unchecked. |
| 161 | |
| 162 | @param row Row index. |
| 163 | @param col Column index. |
| 164 | |
| 165 | @return a reference to the matrix entry at the given row and |
| 166 | column indices. Indices are counted starting with 1. |
| 167 | */ |
| 168 | double& operator()(unsigned int row, unsigned int col) { |
| 169 | return data[(col-1)*eRows+row-1]; |
| 170 | } |
| 171 | |
| 172 | /** Read access the entries of the matrix. |
| 173 | This function is just a shortcut for the <tt>double& |
| 174 | operator()(unsigned int row, unsigned int col)</tt> function. It is |
| 175 | used internally to access the elements in a more convenient way. |
| 176 | |
| 177 | Note that the indices given in the arguments are unchecked. |
| 178 | |
| 179 | @param row Row index. |
| 180 | @param col Column index. |
| 181 | |
| 182 | @return the value of the matrix entry at the given row and |
| 183 | column indices. Indices are counted starting with 1. |
| 184 | */ |
| 185 | double Entry(unsigned int row, unsigned int col) const { |
| 186 | return data[(col-1)*eRows+row-1]; |
| 187 | } |
| 188 | |
| 189 | /** Write access the entries of the matrix. |
| 190 | This function is just a shortcut for the <tt>double& |
| 191 | operator()(unsigned int row, unsigned int col)</tt> function. It is |
| 192 | used internally to access the elements in a more convenient way. |
| 193 | |
| 194 | Note that the indices given in the arguments are unchecked. |
| 195 | |
| 196 | @param row Row index. |
| 197 | @param col Column index. |
| 198 | |
| 199 | @return a reference to the matrix entry at the given row and |
| 200 | column indices. Indices are counted starting with 1. |
| 201 | */ |
| 202 | double& Entry(unsigned int row, unsigned int col) { |
| 203 | return data[(col-1)*eRows+row-1]; |
| 204 | } |
| 205 | |
| 206 | /** Number of rows in the matrix. |
| 207 | @return the number of rows in the matrix. |
| 208 | */ |
| 209 | unsigned int Rows(void) const { return eRows; } |
| 210 | |
| 211 | /** Number of cloumns in the matrix. |
| 212 | @return the number of columns in the matrix. |
| 213 | */ |
| 214 | unsigned int Cols(void) const { return eColumns; } |
| 215 | |
| 216 | /** Transposed matrix. |
| 217 | This function only returns the transpose of this matrix. This matrix |
| 218 | itself remains unchanged. |
| 219 | @return the transposed matrix. |
| 220 | */ |
| 221 | FGMatrix33 Transposed(void) const { |
| 222 | return FGMatrix33( data[0], data[1], data[2], |
| 223 | data[3], data[4], data[5], |
| 224 | data[6], data[7], data[8] ); |
| 225 | } |
| 226 | |
| 227 | /** Transposes this matrix. |
| 228 | This function only transposes this matrix. Nothing is returned. |
| 229 | */ |
| 230 | void T(void); |
| 231 | |
| 232 | /** Initialize the matrix. |
| 233 | This function initializes a matrix to all 0.0. |
| 234 | */ |
| 235 | void InitMatrix(void); |
| 236 | |
| 237 | /** Initialize the matrix. |
| 238 | This function initializes a matrix to user specified values. |
| 239 | */ |
| 240 | void InitMatrix(const double m11, const double m12, const double m13, |
| 241 | const double m21, const double m22, const double m23, |
| 242 | const double m31, const double m32, const double m33) |
| 243 | { |
| 244 | data[0] = m11; |
| 245 | data[1] = m21; |
| 246 | data[2] = m31; |
| 247 | data[3] = m12; |
| 248 | data[4] = m22; |
| 249 | data[5] = m32; |
| 250 | data[6] = m13; |
| 251 | data[7] = m23; |
| 252 | data[8] = m33; |
| 253 | } |
| 254 | |
| 255 | /** Returns the quaternion associated with this direction cosine (rotation) matrix. |
| 256 | */ |
| 257 | FGQuaternion GetQuaternion(void) const; |
| 258 | |
| 259 | /** Returns the Euler angle column vector associated with this matrix. |
| 260 | */ |
| 261 | FGColumnVector3 GetEuler() const; |
| 262 | |
| 263 | /** Determinant of the matrix. |
| 264 | @return the determinant of the matrix. |
| 265 | */ |
| 266 | double Determinant(void) const; |
| 267 | |
| 268 | /** Return if the matrix is invertible. |
| 269 | Checks and returns if the matrix is nonsingular and thus |
| 270 | invertible. This is done by simply computing the determinant and |
| 271 | check if it is zero. Note that this test does not cover any |
| 272 | instabilities caused by nearly singular matirces using finite |
| 273 | arithmetics. It only checks exact singularity. |
| 274 | */ |
| 275 | bool Invertible(void) const { return 0.0 != Determinant(); } |
| 276 | |
| 277 | /** Return the inverse of the matrix. |
| 278 | Computes and returns if the inverse of the matrix. It is computed |
| 279 | by Cramers Rule. Also there are no checks performed if the matrix |
| 280 | is invertible. If you are not sure that it really is check this |
| 281 | with the @ref Invertible() call before. |
| 282 | */ |
| 283 | FGMatrix33 Inverse(void) const; |
| 284 | |
| 285 | /** Assignment operator. |
| 286 | |
| 287 | @param A source matrix. |
| 288 | |
| 289 | Copy the content of the matrix given in the argument into *this. |
| 290 | */ |
| 291 | FGMatrix33& operator=(const FGMatrix33& A) |
| 292 | { |
| 293 | data[0] = A.data[0]; |
| 294 | data[1] = A.data[1]; |
| 295 | data[2] = A.data[2]; |
| 296 | data[3] = A.data[3]; |
| 297 | data[4] = A.data[4]; |
| 298 | data[5] = A.data[5]; |
| 299 | data[6] = A.data[6]; |
| 300 | data[7] = A.data[7]; |
| 301 | data[8] = A.data[8]; |
| 302 | return *this; |
| 303 | } |
| 304 | |
| 305 | /** Assignment operator. |
| 306 | |
| 307 | @param lv initializer list of at most 9 values. |
| 308 | |
| 309 | Copy the content of the list into *this. */ |
| 310 | FGMatrix33& operator=(std::initializer_list<double> lv) |
| 311 | { |
| 312 | double *v = data; |
| 313 | for(auto& x: lv) { |
| 314 | *v = x; |
| 315 | v += 3; |
| 316 | if (v-data > 8) |
| 317 | v -= 8; |
| 318 | } |
| 319 | |
| 320 | return *this; |
| 321 | } |
| 322 | |
| 323 | /** Matrix vector multiplication. |
| 324 | |
| 325 | @param v vector to multiply with. |
| 326 | @return matric vector product. |
| 327 | |
| 328 | Compute and return the product of the current matrix with the |
| 329 | vector given in the argument. |
| 330 | */ |
| 331 | FGColumnVector3 operator*(const FGColumnVector3& v) const; |
| 332 | |
| 333 | /** Matrix subtraction. |
| 334 | |
| 335 | @param B matrix to add to. |
| 336 | @return difference of the matrices. |
| 337 | |
| 338 | Compute and return the sum of the current matrix and the matrix |
| 339 | B given in the argument. |
| 340 | */ |
| 341 | FGMatrix33 operator-(const FGMatrix33& B) const; |
| 342 | |
| 343 | /** Matrix addition. |
| 344 | |
| 345 | @param B matrix to add to. |
| 346 | @return sum of the matrices. |
| 347 | |
| 348 | Compute and return the sum of the current matrix and the matrix |
| 349 | B given in the argument. |
| 350 | */ |
| 351 | FGMatrix33 operator+(const FGMatrix33& B) const; |
| 352 | |
| 353 | /** Matrix product. |
| 354 | |
| 355 | @param B matrix to add to. |
| 356 | @return product of the matrices. |
| 357 | |
| 358 | Compute and return the product of the current matrix and the matrix |
| 359 | B given in the argument. |
| 360 | */ |
| 361 | FGMatrix33 operator*(const FGMatrix33& B) const; |
| 362 | |
| 363 | /** Multiply the matrix with a scalar. |
| 364 | |
| 365 | @param scalar scalar factor to multiply with. |
| 366 | @return scaled matrix. |
| 367 | |
| 368 | Compute and return the product of the current matrix with the |
| 369 | scalar value scalar given in the argument. |
| 370 | */ |
| 371 | FGMatrix33 operator*(const double scalar) const; |
| 372 | |
| 373 | /** Multiply the matrix with 1.0/scalar. |
| 374 | |
| 375 | @param scalar scalar factor to divide through. |
| 376 | @return scaled matrix. |
| 377 | |
| 378 | Compute and return the product of the current matrix with the |
| 379 | scalar value 1.0/scalar, where scalar is given in the argument. |
| 380 | */ |
| 381 | FGMatrix33 operator/(const double scalar) const; |
| 382 | |
| 383 | /** In place matrix subtraction. |
| 384 | |
| 385 | @param B matrix to subtract. |
| 386 | @return reference to the current matrix. |
| 387 | |
| 388 | Compute the diffence from the current matrix and the matrix B |
| 389 | given in the argument. |
| 390 | */ |
| 391 | FGMatrix33& operator-=(const FGMatrix33 &B); |
| 392 | |
| 393 | /** In place matrix addition. |
| 394 | |
| 395 | @param B matrix to add. |
| 396 | @return reference to the current matrix. |
| 397 | |
| 398 | Compute the sum of the current matrix and the matrix B |
| 399 | given in the argument. |
| 400 | */ |
| 401 | FGMatrix33& operator+=(const FGMatrix33 &B); |
| 402 | |
| 403 | /** In place matrix multiplication. |
| 404 | |
| 405 | @param B matrix to multiply with. |
| 406 | @return reference to the current matrix. |
| 407 | |
| 408 | Compute the product of the current matrix and the matrix B |
| 409 | given in the argument. |
| 410 | */ |
| 411 | FGMatrix33& operator*=(const FGMatrix33 &B); |
| 412 | |
| 413 | /** In place matrix scale. |
| 414 | |
| 415 | @param scalar scalar value to multiply with. |
| 416 | @return reference to the current matrix. |
| 417 | |
| 418 | Compute the product of the current matrix and the scalar value scalar |
| 419 | given in the argument. |
| 420 | */ |
| 421 | FGMatrix33& operator*=(const double scalar); |
| 422 | |
| 423 | /** In place matrix scale. |
| 424 | |
| 425 | @param scalar scalar value to divide through. |
| 426 | @return reference to the current matrix. |
| 427 | |
| 428 | Compute the product of the current matrix and the scalar value |
| 429 | 1.0/scalar, where scalar is given in the argument. |
| 430 | */ |
| 431 | FGMatrix33& operator/=(const double scalar); |
| 432 | |
| 433 | private: |
| 434 | double data[eRows*eColumns]; |
| 435 | }; |
| 436 | |
| 437 | /** Scalar multiplication. |
| 438 | |
| 439 | @param scalar scalar value to multiply with. |
| 440 | @param A Matrix to multiply. |
| 441 | |
| 442 | Multiply the Matrix with a scalar value. |
| 443 | */ |
| 444 | inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) { |
| 445 | // use already defined operation. |
| 446 | return A*scalar; |
| 447 | } |
| 448 | |
| 449 | /** Write matrix to a stream. |
| 450 | |
| 451 | @param os Stream to write to. |
| 452 | @param M Matrix to write. |
| 453 | |
| 454 | Write the matrix to a stream. |
| 455 | */ |
| 456 | JSBSIM_API std::ostream& operator<<(std::ostream& os, const FGMatrix33& M); |
| 457 | |
| 458 | /** Read matrix from a stream. |
| 459 | |
| 460 | @param os Stream to read from. |
| 461 | @param M Matrix to initialize with the values from the stream. |
| 462 | |
| 463 | Read matrix from a stream. |
| 464 | */ |
| 465 | JSBSIM_API std::istream& operator>>(std::istream& is, FGMatrix33& M); |
| 466 | |
| 467 | } // namespace JSBSim |
| 468 | #endif |