Menu

[r193]: / trunk / include / small / dgematrix_small-specialized.hpp  Maximize  Restore  History

Download this file

175 lines (158 with data), 8.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//=============================================================================
/*! calculate inverse */
inline dgemat1 inv(const dgemat1& A)
{CPPL_VERBOSE_REPORT;
dgemat1 Ainv;
Ainv(0,0) =1./A(0,0);
return Ainv;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! calculate determinant */
inline double det(const dgemat2& A)
{CPPL_VERBOSE_REPORT;
return A(0,0)*A(1,1)-A(0,1)*A(1,0);
}
//=============================================================================
/*! calculate inverse */
inline dgemat2 inv(const dgemat2& A)
{CPPL_VERBOSE_REPORT;
const double Adet( det(A) );
dgemat2 Ainv;
Ainv(0,0)= A(1,1)/Adet; Ainv(0,1)=-A(0,1)/Adet;
Ainv(1,0)=-A(1,0)/Adet; Ainv(1,1)= A(0,0)/Adet;
return Ainv;
}
//=============================================================================
/*! return rotated tensor */
inline dgemat2 rotate(const dgemat2& m, const double& theta)
{CPPL_VERBOSE_REPORT;
//dgemat2 R(t2m(theta)); return R*m*t(R);//too slow
double c(std::cos(theta)), s(std::sin(theta));
double cc(c*c), cs(c*s), ss(s*s);
dgemat2 mat;
mat(0,0) =m(0,0)*cc -(m(0,1)+m(1,0))*cs +m(1,1)*ss;
mat(0,1) =m(0,1)*cc +(m(0,0)-m(1,1))*cs -m(1,0)*ss;
mat(1,0) =m(1,0)*cc +(m(0,0)-m(1,1))*cs -m(0,1)*ss;
mat(1,1) =m(1,1)*cc +(m(0,1)+m(1,0))*cs +m(0,0)*ss;
return mat;
}
//=============================================================================
/*! convert theta to 2x2 rotational matrix */
inline dgemat2 t2m(const double& theta)
{CPPL_VERBOSE_REPORT;
dgemat2 R;
R(0,0)=std::cos(theta); R(0,1)=-std::sin(theta);
R(1,0)=std::sin(theta); R(1,1)= std::cos(theta);
return R;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! calculate determinant */
inline double det(const dgemat3& A)
{CPPL_VERBOSE_REPORT;
return
+A(0,0)*A(1,1)*A(2,2) -A(0,0)*A(1,2)*A(2,1)
+A(0,1)*A(1,2)*A(2,0) -A(0,1)*A(1,0)*A(2,2)
+A(0,2)*A(1,0)*A(2,1) -A(0,2)*A(1,1)*A(2,0);
}
//=============================================================================
/*! calculate the 2nd invariant */
inline double II(const dgemat3& A)
{CPPL_VERBOSE_REPORT;
return
+A(0,0)*A(1,1) +A(1,1)*A(2,2) +A(2,2)*A(0,0)
-A(0,1)*A(1,0) -A(1,2)*A(2,1) -A(2,0)*A(0,2);
}
//=============================================================================
/*! calculate inverse */
inline dgemat3 inv(const dgemat3& A)
{CPPL_VERBOSE_REPORT;
const double Adet( det(A) );
dgemat3 Ainv;
Ainv(0,0) =(A(1,1)*A(2,2)-A(1,2)*A(2,1))/Adet;
Ainv(0,1) =(A(0,2)*A(2,1)-A(0,1)*A(2,2))/Adet;
Ainv(0,2) =(A(0,1)*A(1,2)-A(0,2)*A(1,1))/Adet;
Ainv(1,0) =(A(1,2)*A(2,0)-A(1,0)*A(2,2))/Adet;
Ainv(1,1) =(A(0,0)*A(2,2)-A(0,2)*A(2,0))/Adet;
Ainv(1,2) =(A(0,2)*A(1,0)-A(0,0)*A(1,2))/Adet;
Ainv(2,0) =(A(1,0)*A(2,1)-A(1,1)*A(2,0))/Adet;
Ainv(2,1) =(A(0,1)*A(2,0)-A(0,0)*A(2,1))/Adet;
Ainv(2,2) =(A(0,0)*A(1,1)-A(0,1)*A(1,0))/Adet;
return Ainv;
}
//=============================================================================
/*! rotate 3D matrix by quaternion */
inline dgemat3 rotate(const dgemat3& m, const dquater& q)
{CPPL_VERBOSE_REPORT;
dgemat3 R(q2m(q));
return R*m*t(R);
}
//=============================================================================
/*! convert 3D rotational matrix to quaternion */
inline dquater m2q(const dgemat3& m)
{CPPL_VERBOSE_REPORT;
dcovec3 v( m(2,1)-m(1,2), m(0,2)-m(2,0), m(1,0)-m(0,1) );
double t( std::acos(0.5*(m(0,0)+m(1,1)+m(2,2)-1.)) );
return vt2q(v,t);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! calculate determinant */
inline double det(const dgemat4& A)
{CPPL_VERBOSE_REPORT;
return
((A(0,0)*A(1,1)-A(0,1)*A(1,0))*A(2,2)+(A(0,2)*A(1,0)-A(0,0)*A(1,2))*A(2,1)+(A(0,1)*A(1,2)-A(0,2)*A(1,1))*A(2,0))*A(3,3)
+((A(0,1)*A(1,0)-A(0,0)*A(1,1))*A(2,3)+(A(0,0)*A(1,3)-A(0,3)*A(1,0))*A(2,1)+(A(0,3)*A(1,1)-A(0,1)*A(1,3))*A(2,0))*A(3,2)
+((A(0,0)*A(1,2)-A(0,2)*A(1,0))*A(2,3)+(A(0,3)*A(1,0)-A(0,0)*A(1,3))*A(2,2)+(A(0,2)*A(1,3)-A(0,3)*A(1,2))*A(2,0))*A(3,1)
+((A(0,2)*A(1,1)-A(0,1)*A(1,2))*A(2,3)+(A(0,1)*A(1,3)-A(0,3)*A(1,1))*A(2,2)+(A(0,3)*A(1,2)-A(0,2)*A(1,3))*A(2,1))*A(3,0);
}
//=============================================================================
/*! calculate inverse */
inline dgemat4 inv(const dgemat4& A)
{CPPL_VERBOSE_REPORT;
const double Adet =det(A);
dgemat4 Ainv;
Ainv(0,0) =(A(1,1)*A(2,2)-A(1,2)*A(2,1))*A(3,3)+(A(1,3)*A(2,1)-A(1,1)*A(2,3))*A(3,2)+(A(1,2)*A(2,3)-A(1,3)*A(2,2))*A(3,1);
Ainv(0,1) =(A(0,2)*A(2,1)-A(0,1)*A(2,2))*A(3,3)+(A(0,1)*A(2,3)-A(0,3)*A(2,1))*A(3,2)+(A(0,3)*A(2,2)-A(0,2)*A(2,3))*A(3,1);
Ainv(0,2) =(A(0,1)*A(1,2)-A(0,2)*A(1,1))*A(3,3)+(A(0,3)*A(1,1)-A(0,1)*A(1,3))*A(3,2)+(A(0,2)*A(1,3)-A(0,3)*A(1,2))*A(3,1);
Ainv(0,3) =(A(0,2)*A(1,1)-A(0,1)*A(1,2))*A(2,3)+(A(0,1)*A(1,3)-A(0,3)*A(1,1))*A(2,2)+(A(0,3)*A(1,2)-A(0,2)*A(1,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2,0))*A(3,1)+(A(1,2)*A(2,1)-A(1,1)*A(2,2))*A(3,0);
Ainv(3,1) =(A(0,0)*A(2,1)-A(0,1)*A(2,0))*A(3,2)+(A(0,2)*A(2,0)-A(0,0)*A(2,2))*A(3,1)+(A(0,1)*A(2,2)-A(0,2)*A(2,1))*A(3,0);
Ainv(3,2) =(A(0,1)*A(1,0)-A(0,0)*A(1,1))*A(3,2)+(A(0,0)*A(1,2)-A(0,2)*A(1,0))*A(3,1)+(A(0,2)*A(1,1)-A(0,1)*A(1,2))*A(3,0);
Ainv(3,3) =(A(0,0)*A(1,1)-A(0,1)*A(1,0))*A(2,2)+(A(0,2)*A(1,0)-A(0,0)*A(1,2))*A(2,1)+(A(0,1)*A(1,2)-A(0,2)*A(1,1))*A(2,0);
Ainv /=Adet;
return Ainv;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.