SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
gauss_jordan.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Ed Rosten (er258@cam.ac.uk)
4 //
5 // This file is part of the TooN Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 
11 // This library 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 along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29 
30 
31 #ifndef TOON_INC_GAUSS_JORDAN_H
32 #define TOON_INC_GAUSS_JORDAN_H
33 
34 #include <utility>
35 #include <cmath>
36 #include "TooN.h"
37 
38 namespace TooN
39 {
50 template<int R, int C, class Precision, class Base> void gauss_jordan(Matrix<R, C, Precision, Base>& m)
51 {
52  using std::swap;
53 
54  //Loop over columns to reduce.
55  for(int col=0; col < m.num_rows(); col++)
56  {
57  //Reduce the current column to a single element
58 
59 
60  //Search down the current column in the lower triangle for the largest
61  //absolute element (pivot). Then swap the pivot row, so that the pivot
62  //element is on the diagonal. The benchmarks show that it is actually
63  //faster to swap whole rows than it is to access the rows via indirection
64  //and swap the indirection element. This holds for both pointer indirection
65  //and using a permutation vector over rows.
66  {
67  using std::abs;
68  int pivotpos = col;
69  double pivotval = abs(m[pivotpos][col]);
70  for(int p=col+1; p <m.num_rows(); p++)
71  if(abs(m[p][col]) > pivotval)
72  {
73  pivotpos = p;
74  pivotval = abs(m[pivotpos][col]);
75  }
76 
77  if(col != pivotpos)
78  for(int c=0; c < m.num_cols(); c++)
79  swap(m[col][c], m[pivotpos][c]);
80  }
81 
82  //Reduce the current column in every row to zero, excluding elements on
83  //the leading diagonal.
84  for(int row = 0; row < m.num_rows(); row++)
85  {
86  if(row != col)
87  {
88  double multiple = m[row][col] / m[col][col];
89 
90  //We could eliminate some of the computations in the augmented
91  //matrix, if the augmented half is the identity. In general, it
92  //is not.
93 
94  //Subtract the pivot row from all other rows, to make
95  //column col zero.
96  m[row][col] = 0;
97  for(int c=col+1; c < m.num_cols(); c++)
98  m[row][c] = m[row][c] - m[col][c] * multiple;
99  }
100  }
101  }
102 
103  //Final pass to make diagonal elements one. Performing this in a final
104  //pass allows us to avoid any significant computations on the left-hand
105  //square matrix, since it is diagonal, and ends up as the identity.
106  for(int row=0;row < m.num_rows(); row++)
107  {
108  double mul = 1/m[row][row];
109 
110  m[row][row] = 1;
111 
112  for(int col=m.num_rows(); col < m.num_cols(); col++)
113  m[row][col] *= mul;
114  }
115 }
116 
117 }
118 #endif
Everything lives inside this namespace.
Definition: allocator.hh:48
T abs(T t)
Definition: abs.h:30
void gauss_jordan(Matrix< R, C, Precision, Base > &m)
Definition: gauss_jordan.h:50