1 /**************************************************************************/
2 /*  Copyright 2009 Tim Day                                                */
3 /*                                                                        */
4 /*  This file is part of Fracplanet                                       */
5 /*                                                                        */
6 /*  Fracplanet 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 /*  Fracplanet 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 Fracplanet.  If not, see <http://www.gnu.org/licenses/>.   */
18 /**************************************************************************/
19 
20 #include "matrix33.h"
21 
cofactor(uint row,uint col) const22 float Matrix33::cofactor(uint row,uint col) const
23 {
24   const uint row0=(row==0 ? 1 : 0);
25   const uint col0=(col==0 ? 1 : 0);
26 
27   const uint row1=(row==2 ? 1 : 2);
28   const uint col1=(col==2 ? 1 : 2);
29 
30   return
31     element(row0,col0)*element(row1,col1)
32     -
33     element(row0,col1)*element(row1,col0);
34 }
35 
determinant() const36 float Matrix33::determinant() const
37 {
38   return
39     element(0,0)*cofactor(0,0)
40     -element(0,1)*cofactor(0,1)
41     +element(0,2)*cofactor(0,2);
42 }
43 
inverted() const44 const Matrix33 Matrix33::inverted() const
45 {
46   Matrix33 ret;
47   for (uint row=0;row<3;row++)
48     for (uint col=0;col<3;col++)
49       {
50 	const float cf=cofactor(row,col);
51 	ret.element(col,row)=(((row+col)&1) ? -cf : cf);  // NB Transpose is deliberate
52       }
53 
54   return ret/determinant();
55 }
56