MathStuff.cpp
Go to the documentation of this file.
00001 /*
00002 * This file is part of Parallel SURF, which implements the SURF algorithm
00003 * using multi-threading.
00004 *
00005 * Copyright (C) 2010 David Gossow
00006 *
00007 * It is based on the SURF implementation included in Pan-o-matic 0.9.4, 
00008 * written by Anael Orlinski.
00009 * 
00010 * Parallel SURF is free software; you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation; either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * Parallel SURF is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 * 
00020 * You should have received a copy of the GNU General Public License
00021 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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         /* Triangularize the matrix. */
00036         for (col = 0; col < size - 1; col++) 
00037         {
00038                 /* Pivot row with largest coefficient to top. */
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                         /* Exchange "pivot" with "col" row (this is no less efficient
00053                         than having to perform all array accesses indirectly). */
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                 /* Do reduction for this column. */
00065                 for (row = col + 1; row < size; row++) 
00066                 {
00067                         mult = sq[row][col] / sq[col][col];
00068                         for (c = col; c < size; c++)    /* Could start with c=col+1. */
00069                                 sq[row][c] -= mult * sq[col][c];
00070                         solution[row] -= mult * solution[col];
00071                 }
00072         }
00073 
00074         /* Do back substitution.  Pivoting does not affect solution order. */
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 


or_libs
Author(s): raphael
autogenerated on Mon Oct 6 2014 02:53:18