Subversion Repositories gelsvn

Rev

Rev 113 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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