~ubuntu-branches/ubuntu/trusty/hugin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/hugin_cpfind/localfeatures/MathStuff.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2011-01-06 14:28:24 UTC
  • mfrom: (1.1.9 upstream) (0.1.21 experimental)
  • Revision ID: james.westby@ubuntu.com-20110106142824-zn9lxylg5z44dynn
* Drop Cyril Brulebois from Uploaders. Thank you very much for your work.
* Bump package version. (rc3 was re-released as 2010.4.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (C) 2007-2008 Anael Orlinski
 
3
*
 
4
* This file is part of Panomatic.
 
5
*
 
6
* Panomatic is free software; you can redistribute it and/or modify
 
7
* it under the terms of the GNU General Public License as published by
 
8
* the Free Software Foundation; either version 2 of the License, or
 
9
* (at your option) any later version.
 
10
 
11
* Panomatic is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
* GNU General Public License for more details.
 
15
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with Panomatic; if not, write to the Free Software
 
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
*/
 
20
 
 
21
#include "MathStuff.h"
 
22
#include <math.h>
 
23
 
 
24
using namespace lfeat;
 
25
 
 
26
bool Math::SolveLinearSystem33(double *solution, double sq[3][3])
 
27
{
 
28
        const int size = 3;
 
29
        int row, col, c, pivot = 0, i;
 
30
        double maxc, coef, temp, mult, val;
 
31
 
 
32
        /* Triangularize the matrix. */
 
33
        for (col = 0; col < size - 1; col++) 
 
34
        {
 
35
                /* Pivot row with largest coefficient to top. */
 
36
                maxc = -1.0;
 
37
                for (row = col; row < size; row++) 
 
38
                {
 
39
                        coef = sq[row][col];
 
40
                        coef = (coef < 0.0 ? - coef : coef);
 
41
                        if (coef > maxc)
 
42
                        {
 
43
                                maxc = coef;
 
44
                                pivot = row;
 
45
                        }
 
46
                }
 
47
                if (pivot != col)
 
48
                {
 
49
                        /* Exchange "pivot" with "col" row (this is no less efficient
 
50
                        than having to perform all array accesses indirectly). */
 
51
                        for (i = 0; i < size; i++) 
 
52
                        {
 
53
                                temp = sq[pivot][i];
 
54
                                sq[pivot][i] = sq[col][i];
 
55
                                sq[col][i] = temp;
 
56
                        }
 
57
                        temp = solution[pivot];
 
58
                        solution[pivot] = solution[col];
 
59
                        solution[col] = temp;
 
60
                }
 
61
                /* Do reduction for this column. */
 
62
                for (row = col + 1; row < size; row++) 
 
63
                {
 
64
                        mult = sq[row][col] / sq[col][col];
 
65
                        for (c = col; c < size; c++)    /* Could start with c=col+1. */
 
66
                                sq[row][c] -= mult * sq[col][c];
 
67
                        solution[row] -= mult * solution[col];
 
68
                }
 
69
        }
 
70
 
 
71
        /* Do back substitution.  Pivoting does not affect solution order. */
 
72
        for (row = size - 1; row >= 0; row--) {
 
73
                val = solution[row];
 
74
                for (col = size - 1; col > row; col--)
 
75
                        val -= solution[col] * sq[row][col];
 
76
                solution[row] = val / sq[row][row];
 
77
        }
 
78
        return true;
 
79
}
 
80
 
 
81
bool Math::Normalize(double* iVec, int iLen)
 
82
{
 
83
 
 
84
        int i;
 
85
        double val, fac, sqlen = 0.0;
 
86
 
 
87
        for (i = 0; i < iLen; i++) {
 
88
                val = iVec[i];
 
89
                sqlen += val * val;
 
90
        }
 
91
        if (sqlen == 0.0)
 
92
                return false;
 
93
 
 
94
        fac = 1.0 / sqrt(sqlen);
 
95
        for (i = 0; i < iLen; i++)
 
96
                iVec[i] *= fac;
 
97
 
 
98
        return true;
 
99
}
 
100