47 int mxLUDecomp(
double** mat,
int n,
int* indx,
double* d)
49 int i, j, k, imax = 0;
52 std::vector<double> vv(n, 0);
55 for (i = 0; i < n; ++i)
58 for (j = 0; j < n; ++j)
59 if ((tmp = fabs(mat[i][j])) > big)
68 for (j = 0; j < n; ++j)
70 for (i = 0; i < j; ++i)
73 for (k = 0; k < i; ++k)
74 sum -= mat[i][k] * mat[k][j];
78 for (i = j; i < n; ++i)
81 for (k = 0; k < j; ++k)
82 sum -= mat[i][k] * mat[k][j];
84 if ((tmp = vv[i] * fabs(sum)) > big)
92 for (k = 0; k < n; ++k)
95 mat[imax][k] = mat[j][k];
102 if (mat[j][j] == 0.0)
106 tmp = 1.0 / mat[j][j];
107 for (i = j + 1; i < n; ++i)
122 int mxLUBcksub(
double** mat,
int n,
const int* indx,
double* b)
128 for (i = 0; i < n; ++i)
134 for (j = ii; j <= i - 1; ++j)
135 sum -= mat[i][j] * b[j];
141 for (i = n - 1; i >= 0; --i)
144 for (j = i + 1; j < n; ++j)
145 sum -= mat[i][j] * b[j];
146 b[i] = sum / mat[i][i];
160 double vv[3], big, tmp, sum;
166 for (i = 0; i < 3; ++i)
169 for (j = 0; j < 3; ++j)
170 if ((tmp = fabs(mat[i][j])) > big)
180 for (j = 0; j < 3; ++j)
182 for (i = 0; i < j; ++i)
185 for (k = 0; k < i; ++k)
186 sum -= mat[i][k] * mat[k][j];
190 for (i = j; i < 3; ++i)
193 for (k = 0; k < j; ++k)
194 sum -= mat[i][k] * mat[k][j];
196 if ((tmp = vv[i] * fabs(sum)) > big)
204 for (k = 0; k < 3; ++k)
207 mat[imax][k] = mat[j][k];
214 if (mat[j][j] == 0.0)
218 tmp = 1.0 / mat[j][j];
219 for (i = j + 1; i < 3; ++i)
231 void mxiLubksb3(
const double mat[3][3],
const int indx[3],
double b[3])
237 for (i = 0; i < 3; ++i)
243 for (j = ii; j <= i - 1; ++j)
244 sum -= mat[i][j] * b[j];
250 for (i = 2; i >= 0; --i)
253 for (j = i + 1; j < 3; ++j)
254 sum -= mat[i][j] * b[j];
255 b[i] = sum / mat[i][i];
276 if (fabs(A[0][0]) < 0.000000001)
278 x[0] = b[0] / A[0][0];
281 if (fabs(A[0][1]) >= fabs(A[0][0]))
283 if (fabs(A[0][1]) < 0.000000001)
285 d = A[1][1] / A[0][1];
286 x[0] = (b[1] - d * b[0]) / (A[1][0] - d * A[0][0]);
287 x[1] = (b[0] - A[0][0] * x[0]) / A[0][1];
291 d = A[1][0] / A[0][0];
292 x[1] = (b[1] - d * b[0]) / (A[1][1] - d * A[0][1]);
293 x[0] = (b[0] - A[0][1] * x[1]) / A[0][0];
297 indx = (
int*)malloc(n *
sizeof(
int));
303 for (i = 0; i < n; ++i)
328 int i, ib, m1, j, k, k1, j1, j2, mM, np1;
331 for (i = 0; i < numeqs; i++)
335 for (i = 1; i < numeqs; i++)
337 m1 = std::min(ib - 1, numeqs - i);
338 for (j = 0; j < m1; j++)
341 k1 = std::min(i, ib - j - 1);
342 for (k = 0; k < k1; k++)
344 if (fabs(a[i - k - 1][0]) < 1e-20)
348 if (fabs(a[i - k - 1][0]) < 1e-20)
350 sum += a[i - k - 1][k + 1] * a[i - k - 1][j + k + 1] / a[i - k - 1][0];
357 for (i = 1; i < numeqs; i++)
360 k1 = std::min(ib - 1, i);
361 for (k = 0; k < k1; k++)
363 if (fabs(a[i - k - 1][0] * b[i - k - 1]) < 1e-20)
365 sum += a[i - k - 1][k + 1] / a[i - k - 1][0] * b[i - k - 1];
370 if (fabs(a[numeqs - 1][0]) < 1e-20)
372 x[numeqs - 1] = b[numeqs - 1] / a[numeqs - 1][0];
374 for (k = 1; k < numeqs; k++)
378 j2 = std::min(numeqs, i + ib - 1);
380 for (j = j1; j <= j2; j++)
383 sum += x[j - 1] * a[i - 1][mM - 1];
385 if (fabs(a[i - 1][0]) < 1e-20)
387 x[i - 1] = (b[i - 1] - sum) / a[i - 1][0];
424 double tmp[4][4], scale[4];
430 for (i = 0; i < 4; i++)
432 double biggest = 0.0;
433 for (j = 0; j < 4; j++)
434 if ((dummy = fabs(tmp[i][j])) > biggest)
441 scale[i] = 1.0 / biggest;
444 int index_col[4], index_row[4], index_pivot[4];
450 int irow = 0, icol = 0;
451 for (i = 0; i < 4; i++)
454 double biggest = 0.0;
455 for (j = 0; j < 4; j++)
456 if (index_pivot[j] != 1)
457 for (k = 0; k < 4; k++)
459 if (index_pivot[k] == 0)
461 if ((dummy = scale[j] * fabs(tmp[j][k])) >= biggest)
468 else if (index_pivot[k] > 1)
474 ++(index_pivot[icol]);
478 double temp = tmp[irow][0];
479 tmp[irow][0] = tmp[icol][0];
482 tmp[irow][1] = tmp[icol][1];
485 tmp[irow][2] = tmp[icol][2];
488 tmp[irow][3] = tmp[icol][3];
491 scale[irow] = scale[icol];
496 if (tmp[icol][icol] == 0.0)
502 double pivot_inverse = 1.0 / tmp[icol][icol];
503 tmp[icol][icol] = 1.0;
504 tmp[icol][0] *= pivot_inverse;
505 tmp[icol][1] *= pivot_inverse;
506 tmp[icol][2] *= pivot_inverse;
507 tmp[icol][3] *= pivot_inverse;
509 for (l = 0; l < 4; l++)
512 dummy = tmp[l][icol];
514 tmp[l][0] -= tmp[icol][0] * dummy;
515 tmp[l][1] -= tmp[icol][1] * dummy;
516 tmp[l][2] -= tmp[icol][2] * dummy;
517 tmp[l][3] -= tmp[icol][3] * dummy;
522 for (l = 3; l >= 0; l--)
524 if (index_row[l] != index_col[l])
526 double temp = tmp[0][index_row[l]];
527 tmp[0][index_row[l]] = tmp[0][index_col[l]];
528 tmp[0][index_col[l]] = temp;
529 temp = tmp[1][index_row[l]];
530 tmp[1][index_row[l]] = tmp[1][index_col[l]];
531 tmp[1][index_col[l]] = temp;
532 temp = tmp[2][index_row[l]];
533 tmp[2][index_row[l]] = tmp[2][index_col[l]];
534 tmp[2][index_col[l]] = temp;
535 temp = tmp[3][index_row[l]];
536 tmp[3][index_row[l]] = tmp[3][index_col[l]];
537 tmp[3][index_col[l]] = temp;
554 pt->x = tx * ctm[0][0] + ty * ctm[0][1] + tz * ctm[0][2] + ctm[0][3];
555 pt->y = tx * ctm[1][0] + ty * ctm[1][1] + tz * ctm[1][2] + ctm[1][3];
556 pt->z = tx * ctm[2][0] + ty * ctm[2][1] + tz * ctm[2][2] + ctm[2][3];
563 void mxCopy4x4(
double copy[4][4],
const double matrix[4][4])
565 memcpy((
char*)&(copy[0][0]), &(matrix[0][0]), 16 *
sizeof(
double));
573 matrix[0][1] = matrix[0][2] = matrix[0][3] = 0.0;
574 matrix[1][0] = matrix[1][2] = matrix[1][3] = 0.0;
575 matrix[2][0] = matrix[2][1] = matrix[2][3] = 0.0;
576 matrix[3][0] = matrix[3][1] = matrix[3][2] = 0.0;
577 matrix[0][0] = matrix[1][1] = matrix[2][2] = matrix[3][3] = 1.0;
585 void mxMult4x4(
double product[4][4],
const double matrix1[4][4],
const double matrix2[4][4])
590 double temp1[4][4], temp2[4][4];
593 for (i = 3; i >= 0; i--)
595 for (j = 3; j >= 0; j--)
598 for (k = 3; k >= 0; k--)
599 product[i][j] += (temp1[i][k] * temp2[k][j]);
611 void mxRotate4x4(
double xrot,
double yrot,
double zrot,
double rMatt[4][4])
614 double cosX = cos(xrot *
XM_PI / 180);
615 double cosY = cos(yrot *
XM_PI / 180);
616 double cosZ = cos(zrot *
XM_PI / 180);
618 double sinX = sin(xrot *
XM_PI / 180);
619 double sinY = sin(yrot *
XM_PI / 180);
620 double sinZ = sin(zrot *
XM_PI / 180);
622 rMatt[0][0] = cosY * cosZ;
623 rMatt[1][0] = cosX * sinZ + sinX * sinY * cosZ;
624 rMatt[2][0] = sinX * sinZ - cosX * sinY * cosZ;
625 rMatt[0][1] = -cosY * sinZ;
626 rMatt[1][1] = cosX * cosZ - sinX * sinY * sinZ;
627 rMatt[2][1] = sinX * cosZ + cosX * sinY * sinZ;
629 rMatt[1][2] = -sinX * cosY;
630 rMatt[2][2] = cosX * cosY;
642 a_mx[0][3] = a_translation.x;
643 a_mx[1][3] = a_translation.y;
644 a_mx[2][3] = a_translation.z;
void mxRotate4x4(double xrot, double yrot, double zrot, double rMatt[4][4])
return a rotation matrix with specified x, y, z rotations z - y - x order
int mxLUBcksub(double **mat, int n, const int *indx, double *b)
solve [mat]{x} = {b} by back substitution (mat = LU of mat), from "Numerical Recipes in C" p 44...
void mxMult4x4(double product[4][4], const double matrix1[4][4], const double matrix2[4][4])
Multiplies two transformation matrices.
void mxIdent4x4(double matrix[4][4])
Sets a transformation matrix to identity.
int mxInvert4x4(const double matrix[4][4], double inv[4][4])
Compute the inverse of a transformation matrix. Checks for special case of Orthogonal 4x4 and does ea...
int mxLUDecomp(double **mat, int n, int *indx, double *d)
Decompose an n x n matrix into Upper & Lower trianglar (in place), from "Numerical Recipes in C" p 43...
routines for manipulating matrices
void mxCopy4x4(double copy[4][4], const double matrix[4][4])
Copy a transformation matrix. Simply uses memcpy.
bool mxSolveBandedEquations(double **a, double *x, double *b, int numeqs, int halfbandwidth)
Solves the matrix equation [a][x]=[b] using gauss elimination.
bool mxSolve3x3(double A[3][3], double x[3], double b[3])
Solves the matrix equation [A]*{x}={b} for {x} where [A] is 3x3 and {x} and {b} are 3x1 vectors...
void mxiLubksb3(const double mat[3][3], const int indx[3], double b[3])
solve [mat]{x} = {b} by back substitution (mat = LU of mat)
void mxPointMult(Pt3d *pt, const double ctm[4][4])
Multiplies a point by a transformation matrix.
void mxTranslate4x4(const Pt3d &a_translation, double a_mx[4][4])
return a translation matrix with specified translation z - y - x order
bool mxiLudcmp3(double mat[3][3], int indx[3], double *d)
Decomposes a 3 x 3 matrix into Upper & Lower trianglar(in place)
bool mxSolveNxN(double **A, double *x, double *b, int n)
Solves the matrix equation [A]*{x}={b} for {x} where [A] is NxN and {x} and {b} are Nx1 vectors...