Monday, August 18, 2014

Invert 3x3 matrix and 4x4 matrix

Output:
M1, Translation(2.F,3.F,4.F)
| 1  0  0  2 |
| 0  1  0  3 |
| 0  0  1  4 |
| 0  0  0  1 |
M2, Inversion
| 1  0  0  -2 |
| 0  1  0  -3 |
| 0  0  1  -4 |
| 0  0  0   1 |
Identity = M1*M2
| 1  0  0  0 |
| 0  1  0  0 |
| 0  0  1  0 |
| 0  0  0  1 |
Identity = M2*M1
| 1  0  0  0 |
| 0  1  0  0 |
| 0  0  1  0 |
| 0  0  0  1 |


M1, Rotation(1.25 rad, normal(-2.F,-1.F,3.F)), then Translation(2.F,3.F,4.F)
|  0.511151     -0.663183     -0.54723       2           |
|  0.858884      0.364375      0.36058       3           |
| -0.0398737    -0.654132      0.755777      4           |
|  0             0             0             1           |
M2, Inversion
|  0.51082       0.858452     -0.0397004    -3.43819     |
| -0.662941      0.364187     -0.653764      2.84838     |
| -0.546831      0.360498      0.755209     -3.00867     |
|  0             0             0             1           |
Identity = M1*M2
|  1             1.49012e-08   0             2.38419e-07 |
|  0             1             0             4.76837e-07 |
| -2.98023e-08  -2.98023e-08   1             4.76837e-07 |
|  0             0             0             1           |
Identity = M2*M1
|  1             9.31323e-09  -1.11759e-08   2.38419e-07 |
|  1.86265e-08   1            -2.98023e-08  -2.38419e-07 |
| -7.45058e-09   0             1             0           |
|  0             0             0             1           |

Code:
Implementations of other functions and other operators are trivial.
/*Please refactor identifiers, as you like*/

#define eCG/*G++*/
/*#define eCV*//*VC++*/

/*Haters gonna hate ... */
typedef void v;
#define ig else if
#define ih else
#define rK noexcept
#define rU const noexcept
#define rY (*this)

#ifdef eCG
#define okN(p)__attribute__((aligned(p)))
#elif defined eCV
#define okN(p)__declspec(align(p))
#endif

#include<iostream>

typedef float tR;



struct okN(0x10) tR3x3{
tR m[0x10];/*Column-major, not row-major -- OpenGL FTW*/

#define o_(a,b)\
tR N##a##b()rU{return m[((b-1)*3)+(a-1)];}\
tR&N##a##b()rK{return m[((b-1)*3)+(a-1)];}
o_(1,1)o_(1,2)o_(1,3)
o_(2,1)o_(2,2)o_(2,3)
o_(3,1)o_(3,2)o_(3,3)
#undef o_

tR3x3()rK{}

tR3x3(
tR p11,tR p12,tR p13,
tR p21,tR p22,tR p23,
tR p31,tR p32,tR p33)rK{
#define o_(a,b)N##a##b()=p##a##b;
o_(1,1)o_(1,2)o_(1,3)
o_(2,1)o_(2,2)o_(2,3)
o_(3,1)o_(3,2)o_(3,3)
#undef o_
}

tR3x3&operator*=(tR const&)rK;
 
/*Determinant*/
tR kDnt()rU{return
N11()*(N22()*N33()-N23()*N32())-
N12()*(N21()*N33()-N23()*N31())+
N13()*(N21()*N32()-N22()*N31());}
 
tR3x3 kIv()rU{tR3x3 l;
l.N11()=N22()*N33()-N23()*N32();
l.N12()=-N12()*N33()+N13()*N32();
l.N13()=N12()*N23()-N13()*N22();
l.N21()=-N21()*N33()+N23()*N31();
l.N22()=N11()*N33()-N13()*N31();
l.N23()=-N11()*N23()+N13()*N21();
l.N31()=N21()*N32()-N22()*N31();
l.N32()=-N11()*N32()+N12()*N31();
l.N33()=N11()*N22()-N12()*N21();
l*=1.F/(N11()*l.N11()+N21()*l.N12()+N31()*l.N13());return l;}

friend std::ostream&operator<<(std::ostream&,tR3x3 const&)rK;};





struct okN(0x10) tR4x4{
tR m[0x10];/*Column-major, not row-major -- OpenGL FTW*/

#define o_(a,b)\
tR N##a##b()rU{return m[((b-1)<<2)+(a-1)];}\
tR&N##a##b()rK{return m[((b-1)<<2)+(a-1)];}
o_(1,1)o_(1,2)o_(1,3)o_(1,4)
o_(2,1)o_(2,2)o_(2,3)o_(2,4)
o_(3,1)o_(3,2)o_(3,3)o_(3,4)
o_(4,1)o_(4,2)o_(4,3)o_(4,4)
#undef o_

tR4x4()rK{}

tR4x4(
tR p11,tR p12,tR p13,tR p14,
tR p21,tR p22,tR p23,tR p24,
tR p31,tR p32,tR p33,tR p34,
tR p41,tR p42,tR p43,tR p44)rK{
#define o_(a,b)N##a##b()=p##a##b;
o_(1,1)o_(1,2)o_(1,3)o_(1,4)
o_(2,1)o_(2,2)o_(2,3)o_(2,4)
o_(3,1)o_(3,2)o_(3,3)o_(3,4)
o_(4,1)o_(4,2)o_(4,3)o_(4,4)
#undef o_
}

tR4x4&operator*=(tR const&)rK;
 
/*Determinant*/
tR kDnt()rU{return
N11()*(N22()*(N33()*N44()-N43()*N34())-N23()*(N32()*N44()-N42()*N34())+N24()*(N32()*N43()-N42()*N33()))-
N12()*(N21()*(N33()*N44()-N43()*N34())-N23()*(N31()*N44()-N41()*N34())+N24()*(N31()*N43()-N41()*N33()))+
N13()*(N21()*(N32()*N44()-N42()*N34())-N22()*(N31()*N44()-N41()*N34())+N24()*(N31()*N42()-N41()*N32()))-
N14()*(N21()*(N32()*N43()-N42()*N33())-N22()*(N31()*N43()-N41()*N33())+N23()*(N31()*N42()-N41()*N32()));}
 
tR4x4 kIv()rU{tR4x4 l;
l.N11()=N22()*N33()*N44()-N22()*N43()*N34()-N23()*N32()*N44()+N23()*N42()*N34()+N24()*N32()*N43()-N24()*N42()*N33();
l.N12()=-N12()*N33()*N44()+N12()*N43()*N34()+N13()*N32()*N44()-N13()*N42()*N34()-N14()*N32()*N43()+N14()*N42()*N33();
l.N13()=N12()*N23()*N44()-N12()*N43()*N24()-N13()*N22()*N44()+N13()*N42()*N24()+N14()*N22()*N43()-N14()*N42()*N23();
l.N14()=-N12()*N23()*N34()+N12()*N33()*N24()+N13()*N22()*N34()-N13()*N32()*N24()-N14()*N22()*N33()+N14()*N32()*N23();
l.N21()=-N21()*N33()*N44()+N21()*N43()*N34()+N23()*N31()*N44()-N23()*N41()*N34()-N24()*N31()*N43()+N24()*N41()*N33();
l.N22()=N11()*N33()*N44()-N11()*N43()*N34()-N13()*N31()*N44()+N13()*N41()*N34()+N14()*N31()*N43()-N14()*N41()*N33();
l.N23()=-N11()*N23()*N44()+N11()*N43()*N24()+N13()*N21()*N44()-N13()*N41()*N24()-N14()*N21()*N43()+N14()*N41()*N23();
l.N24()=N11()*N23()*N34()-N11()*N33()*N24()-N13()*N21()*N34()+N13()*N31()*N24()+N14()*N21()*N33()-N14()*N31()*N23();
l.N31()=N21()*N32()*N44()-N21()*N42()*N34()-N22()*N31()*N44()+N22()*N41()*N34()+N24()*N31()*N42()-N24()*N41()*N32();
l.N32()=-N11()*N32()*N44()+N11()*N42()*N34()+N12()*N31()*N44()-N12()*N41()*N34()-N14()*N31()*N42()+N14()*N41()*N32();
l.N33()=N11()*N22()*N44()-N11()*N42()*N24()-N12()*N21()*N44()+N12()*N41()*N24()+N14()*N21()*N42()-N14()*N41()*N22();
l.N34()=-N11()*N22()*N34()+N11()*N32()*N24()+N12()*N21()*N34()-N12()*N31()*N24()-N14()*N21()*N32()+N14()*N31()*N22();
l.N41()=-N21()*N32()*N43()+N21()*N42()*N33()+N22()*N31()*N43()-N22()*N41()*N33()-N23()*N31()*N42()+N23()*N41()*N32();
l.N42()=N11()*N32()*N43()-N11()*N42()*N33()-N12()*N31()*N43()+N12()*N41()*N33()+N13()*N31()*N42()-N13()*N41()*N32();
l.N43()=-N11()*N22()*N43()+N11()*N42()*N23()+N12()*N21()*N43()-N12()*N41()*N23()-N13()*N21()*N42()+N13()*N41()*N22();
l.N44()=N11()*N22()*N33()-N11()*N32()*N23()-N12()*N21()*N33()+N12()*N31()*N23()+N13()*N21()*N32()-N13()*N31()*N22();
l*=1.F/(N11()*l.N11()+N21()*l.N12()+N31()*l.N13()+N41()*l.N14());return l;}

friend std::ostream&operator<<(std::ostream&,tR4x4 const&)rK;};





std::ostream&operator<<(std::ostream&q,tR3x3 const&p)rK{q
<<"| "<<p.N11()<<"  "<<p.N12()<<"  "<<p.N13()<<" |\n"
<<"| "<<p.N21()<<"  "<<p.N22()<<"  "<<p.N23()<<" |\n"
<<"| "<<p.N31()<<"  "<<p.N32()<<"  "<<p.N33()<<" |\n";return q;}

std::ostream&operator<<(std::ostream&q,tR4x4 const&p)rK{q
<<"| "<<p.N11()<<"  "<<p.N12()<<"  "<<p.N13()<<"  "<<p.N14()<<" |\n"
<<"| "<<p.N21()<<"  "<<p.N22()<<"  "<<p.N23()<<"  "<<p.N24()<<" |\n"
<<"| "<<p.N31()<<"  "<<p.N32()<<"  "<<p.N33()<<"  "<<p.N34()<<" |\n"
<<"| "<<p.N41()<<"  "<<p.N42()<<"  "<<p.N43()<<"  "<<p.N44()<<" |\n";return q;}


No comments:

Post a Comment