Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "MathStuff.h"
00025 #include <math.h>
00026
00027 using namespace parallelsurf;
00028
00029 bool Math::SolveLinearSystem33(double *solution, double sq[3][3])
00030 {
00031 const int size = 3;
00032 int row, col, c, pivot = 0, i;
00033 double maxc, coef, temp, mult, val;
00034
00035
00036 for (col = 0; col < size - 1; col++)
00037 {
00038
00039 maxc = -1.0;
00040 for (row = col; row < size; row++)
00041 {
00042 coef = sq[row][col];
00043 coef = (coef < 0.0 ? - coef : coef);
00044 if (coef > maxc)
00045 {
00046 maxc = coef;
00047 pivot = row;
00048 }
00049 }
00050 if (pivot != col)
00051 {
00052
00053
00054 for (i = 0; i < size; i++)
00055 {
00056 temp = sq[pivot][i];
00057 sq[pivot][i] = sq[col][i];
00058 sq[col][i] = temp;
00059 }
00060 temp = solution[pivot];
00061 solution[pivot] = solution[col];
00062 solution[col] = temp;
00063 }
00064
00065 for (row = col + 1; row < size; row++)
00066 {
00067 mult = sq[row][col] / sq[col][col];
00068 for (c = col; c < size; c++)
00069 sq[row][c] -= mult * sq[col][c];
00070 solution[row] -= mult * solution[col];
00071 }
00072 }
00073
00074
00075 for (row = size - 1; row >= 0; row--) {
00076 val = solution[row];
00077 for (col = size - 1; col > row; col--)
00078 val -= solution[col] * sq[row][col];
00079 solution[row] = val / sq[row][row];
00080 }
00081 return true;
00082 }
00083
00084 bool Math::Normalize( std::vector<double> &iVec )
00085 {
00086 int i;
00087 double val, fac, sqlen = 0.0;
00088
00089 for (i = 0; i < iVec.size(); i++) {
00090 val = iVec[i];
00091 sqlen += val * val;
00092 }
00093 if (sqlen == 0.0)
00094 return false;
00095
00096 fac = 1.0 / sqrt(sqlen);
00097 for (i = 0; i < iVec.size(); i++)
00098 iVec[i] *= fac;
00099
00100 return true;
00101 }
00102