Subversion Repositories gelsvn

Rev

Rev 112 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#include "ArithSqMat3x3Float.h"
2
#include "Mat3x3f.h"
113 jab 3
#include "Mat3x3d.h"
2 bj 4
 
5
namespace CGLA {
6
 
7
	template<class V, class M>
8
	M invert(const ArithSqMat3x3Float<V,M>& _a)
9
	{
10
		M a(_a[0], _a[1], _a[2]);
11
		M	b;
12
		b.identity();
13
 
45 jab 14
		unsigned int  i, j, i1;
2 bj 15
 
16
		for (j=0; j<3; j++) 
17
			{   
18
				i1 = j;                 // Row with largest pivot candidate
19
				for (i=j+1; i<3; i++)
20
					if (fabs(a[i][j]) > fabs(a[i1][j]))
21
						i1 = i;
22
 
23
				// Swap rows i1 and j in a and b to put pivot on diagonal
24
				V a_tmp = a[i1];
25
				a[i1] = a[j];
26
				a[j]  = a_tmp;
27
 
28
				V b_tmp = b[i1];
29
				b[i1] = b[j];
30
				b[j]  = b_tmp;
31
 
32
				// Scale row j to have a unit diagonal
5 jab 33
				if (a[j][j] == 0.0)
2 bj 34
					throw(Mat3x3fSingular("Tried to invert Singular matrix"));
35
 
36
				b[j] /= a[j][j];
37
				a[j] /= a[j][j];
38
 
39
				// Eliminate off-diagonal elems in col j of a, doing identical ops to b
40
				for (i=0; i<3; i++)
41
					if (i!=j) 
42
						{
43
							b[i] -= a[i][j] * b[j];
44
							a[i] -= a[i][j] * a[j];
45
						}
46
			}
47
		return b;
48
	}                                                                               
49
	template class ArithSqMat3x3Float<Vec3f,Mat3x3f>;
50
	template Mat3x3f invert(const ArithSqMat3x3Float<Vec3f,Mat3x3f>&);
112 jab 51
 
52
	template class ArithSqMat3x3Float<Vec3d,Mat3x3d>;
53
	template Mat3x3d invert(const ArithSqMat3x3Float<Vec3d,Mat3x3d>&);
2 bj 54
}