4 mat3d::mat3d(
double a00,
double a01,
double a02,
double a10,
double a11,
double a12,
double a20,
double a21,
double a22)
6 m_data[0][0] = a00; m_data[0][1] = a01; m_data[0][2] = a02;
7 m_data[1][0] = a10; m_data[1][1] = a11; m_data[1][2] = a12;
8 m_data[2][0] = a20; m_data[2][1] = a21; m_data[2][2] = a22;
12 double mat3d::Invert()
16 det += m_data[0][0]*m_data[1][1]*m_data[2][2];
17 det += m_data[0][1]*m_data[1][2]*m_data[2][0];
18 det += m_data[0][2]*m_data[1][0]*m_data[2][1];
19 det -= m_data[0][2]*m_data[1][1]*m_data[2][0];
20 det -= m_data[0][1]*m_data[1][0]*m_data[2][2];
21 det -= m_data[0][0]*m_data[1][2]*m_data[2][1];
25 double deti = 1 / det;
30 mi[0][0] = (m_data[1][1]*m_data[2][2] - m_data[1][2]*m_data[2][1]);
31 mi[0][1] = -(m_data[1][0]*m_data[2][2] - m_data[1][2]*m_data[2][0]);
32 mi[0][2] = (m_data[1][0]*m_data[2][1] - m_data[1][1]*m_data[2][0]);
34 mi[1][0] = -(m_data[0][1]*m_data[2][2] - m_data[0][2]*m_data[2][1]);
35 mi[1][1] = (m_data[0][0]*m_data[2][2] - m_data[0][2]*m_data[2][0]);
36 mi[1][2] = -(m_data[0][0]*m_data[2][1] - m_data[0][1]*m_data[2][0]);
38 mi[2][0] = (m_data[0][1]*m_data[1][2] - m_data[0][2]*m_data[1][1]);
39 mi[2][1] = -(m_data[0][0]*m_data[1][2] - m_data[0][2]*m_data[1][0]);
40 mi[2][2] = (m_data[0][0]*m_data[1][1] - m_data[0][1]*m_data[1][0]);
43 m_data[0][0] = mi[0][0]*deti; m_data[1][0] = mi[0][1]*deti; m_data[2][0] = mi[0][2]*deti;
44 m_data[0][1] = mi[1][0]*deti; m_data[1][1] = mi[1][1]*deti; m_data[2][1] = mi[1][2]*deti;
45 m_data[0][2] = mi[2][0]*deti; m_data[1][2] = mi[2][1]*deti; m_data[2][2] = mi[2][2]*deti;
55 double dot = quatd::dot(q1, q2);
68 double angle = acos(dot);
69 return (q1*sin(angle*(1-t)) + q3*sin(angle*t))/sin(angle);
71 return quatd::lerp(q1,q3,t);
76 bool matrix::solve(vector<double>& x, vector<double>& b)
79 if (m_nr != m_nc)
return false;
80 if (((
int)x.size() != m_nr) || ((
int)b.size() != m_nr))
return false;
84 const double TINY = 1.0e-20;
86 double big, dum, sum, temp;
100 if ((temp=fabs(a(i,j))) > big) big = temp;
101 if (big == 0)
return false;
110 for (k=0; k<i; ++k) sum -= a(i,k)*a(k,j);
118 for (k=0; k<j; ++k) sum -= a(i,k)*a(k,j);
120 if ((dum=vv[i]*fabs(sum))>=big)
139 if (a(j,j) == 0) a(j,j) = TINY;
143 for (i=j+1;i<n; ++i) a(i,j) *= dum;
159 for (j=ii-1;j<i;++j) sum -= a(i,j)*x[j];
165 for (i=n-1; i>=0; --i)
168 for (j=i+1; j<n; ++j) sum -= a(i,j)*x[j];
175 void matrix::mult_transpose_self(
matrix& AAt)
180 for (
int i=0; i<N; ++i)
181 for (
int j=0; j<N; ++j)
183 double& aij = AAt[i][j];
185 for (
int k=0; k<R; ++k) aij += A[k][i]*A[k][j];
189 matrix::matrix(
int r,
int c) : m_nr(r), m_nc(c)
192 if (m_ne > 0) d =
new double[m_ne];
199 for (
int i=0; i<m_ne; ++i) d[i] = 0.0;
203 void matrix::mult_transpose(vector<double>& x, vector<double>& y)
205 for (
int i=0; i<m_nc; ++i) y[i] = 0.0;
207 for (
int i=0; i<m_nr; ++i)
209 double* di = d + i*m_nc;
210 for (
int j=0; j<m_nc; ++j) y[j] += di[j]*x[i];
215 bool matrix::lsq_solve(vector<double>& x, vector<double>& b)
217 if ((
int) x.size() != m_nc)
return false;
218 if ((
int) b.size() != m_nr)
return false;
220 vector<double> y(m_nc);
221 mult_transpose(b, y);
224 mult_transpose_self(AA);
231 #define ROTATE(a, i, j, k, l) g=a[i][j]; h=a[k][l];a[i][j]=g-s*(h+g*tau); a[k][l] = h + s*(g - h*tau); 233 bool matrix::eigen_vectors(
matrix&
Eigen, vector<double>& eigen_values){
238 double sm, tresh, g, h, t, c, tau, s, th;
239 const double eps = 0;
243 for(
int i = 0; i< R ; i++)
245 for(
int j = 0;j<N;j++) Eigen[i][j] = 0;
254 for(
int i = 0; i<R;i++)
256 b.push_back(A[i][i]);
257 eigen_values.push_back(A[i][i]);
262 for (n=0; n<NMAX; ++n)
266 for(
int i = 0; i<N-1;i++){
267 for(
int j = i+1; j<N; j++)
274 if (n < 3) tresh = 0.2*sm/(R*R);
else tresh = 0.0;
277 for(
int i = 0; i<N-1;i++){
278 for(
int j = i+1; j<N; j++){
280 g = 100.0*fabs(A[i][j]);
282 if ((n > 3) && ((fabs(eigen_values[i])+g) == fabs(eigen_values[i])) && ((fabs(eigen_values[j])+g) == fabs(eigen_values[j])))
286 else if (fabs(A[i][j]) > tresh){
287 h = eigen_values[j] - eigen_values[i];
288 if ((fabs(h)+g) == fabs(h))
293 t = 1.0/(fabs(th) + sqrt(1+th*th));
294 if (th < 0.0) t = -t;
296 c = 1.0/sqrt(1.0 + t*t);
302 eigen_values[i] -= h;
303 eigen_values[j] += h;
305 for (k= 0; k<=i-1; ++k) { ROTATE(A, k, i, k, j) }
306 for (k=i+1; k<=j-1; ++k) { ROTATE(A, i, k, k, j) }
307 for (k=j+1; k<N; ++k) { ROTATE(A, i, k, j, k) }
308 for (k= 0; k<N; ++k) { ROTATE(Eigen, k, i, k, j) }
315 for (
int i=0; i<R; ++i)
318 eigen_values[i] = b[i];
328 mat3d mat3d::transpose()
330 return mat3d(m_data[0][0], m_data[1][0], m_data[2][0], m_data[0][1], m_data[1][1], m_data[2][1], m_data[0][2], m_data[1][2], m_data[2][2]);