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.
gaussian_elimination.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2008,2009 Ethan Eade, Tom Drummond (twd20@cam.ac.uk)
4 // and Ed Rosten (er258@cam.ac.uk)
5 //
6 // This file is part of the TooN Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30 
31 
32 #ifndef GAUSSIAN_ELIMINATION_H
33 #define GAUSSIAN_ELIMINATION_H
34 
35 #include <utility>
36 #include <cmath>
37 #include "TooN.h"
38 
39 namespace TooN {
44  template<int N, typename Precision>
46  using std::swap;
47  using std::abs;
48 
49  int size=b.size();
50 
51  for (int i=0; i<size; ++i) {
52  int argmax = i;
53  Precision maxval = abs(A[i][i]);
54 
55  for (int ii=i+1; ii<size; ++ii) {
56  double v = abs(A[ii][i]);
57  if (v > maxval) {
58  maxval = v;
59  argmax = ii;
60  }
61  }
62  Precision pivot = A[argmax][i];
63  //assert(abs(pivot) > 1e-16);
64  Precision inv_pivot = static_cast<Precision>(1)/pivot;
65  if (argmax != i) {
66  for (int j=i; j<size; ++j)
67  swap(A[i][j], A[argmax][j]);
68  swap(b[i], b[argmax]);
69  }
70  //A[i][i] = 1;
71  for (int j=i+1; j<size; ++j)
72  A[i][j] *= inv_pivot;
73  b[i] *= inv_pivot;
74 
75  for (int u=i+1; u<size; ++u) {
76  double factor = A[u][i];
77  //A[u][i] = 0;
78  for (int j=i+1; j<size; ++j)
79  A[u][j] -= factor * A[i][j];
80  b[u] -= factor * b[i];
81  }
82  }
83 
84  Vector<N,Precision> x(size);
85  for (int i=size-1; i>=0; --i) {
86  x[i] = b[i];
87  for (int j=i+1; j<size; ++j)
88  x[i] -= A[i][j] * x[j];
89  }
90  return x;
91  }
92 
93  namespace Internal
94  {
95  template<int i, int j, int k> struct Size3
96  {
97  static const int s=(i!= -1)?i:(j!=-1?j:k);
98  };
99 
100  };
101 
106  template<int R1, int C1, int R2, int C2, typename Precision>
108  using std::swap;
109  using std::abs;
110  SizeMismatch<R1, C1>::test(A.num_rows(), A.num_cols());
111  SizeMismatch<R1, R2>::test(A.num_rows(), b.num_rows());
112 
113  int size=A.num_rows();
114 
115  for (int i=0; i<size; ++i) {
116  int argmax = i;
117  Precision maxval = abs(A[i][i]);
118 
119  for (int ii=i+1; ii<size; ++ii) {
120  double v = abs(A[ii][i]);
121  if (v > maxval) {
122  maxval = v;
123  argmax = ii;
124  }
125  }
126  Precision pivot = A[argmax][i];
127  //assert(abs(pivot) > 1e-16);
128  Precision inv_pivot = static_cast<Precision>(1)/pivot;
129  if (argmax != i) {
130  for (int j=i; j<size; ++j)
131  swap(A[i][j], A[argmax][j]);
132 
133  for(int j=0; j < b.num_cols(); j++)
134  swap(b[i][j], b[argmax][j]);
135  }
136  //A[i][i] = 1;
137  for (int j=i+1; j<size; ++j)
138  A[i][j] *= inv_pivot;
139  b[i] *= inv_pivot;
140 
141  for (int u=i+1; u<size; ++u) {
142  double factor = A[u][i];
143  //A[u][i] = 0;
144  for (int j=i+1; j<size; ++j)
145  A[u][j] -= factor * A[i][j];
146  b[u] -= factor * b[i];
147  }
148  }
149 
150  Matrix<Internal::Size3<R1, C1, R2>::s,C2,Precision> x(b.num_rows(), b.num_cols());
151  for (int i=size-1; i>=0; --i) {
152  for(int k=0; k <b.num_cols(); k++)
153  {
154  x[i][k] = b[i][k];
155  for (int j=i+1; j<size; ++j)
156  x[i][k] -= A[i][j] * x[j][k];
157  }
158  }
159  return x;
160  }
161 }
162 #endif
Everything lives inside this namespace.
Definition: allocator.hh:48
static void test(int s1, int s2)
T abs(T t)
Definition: abs.h:30
Vector< N, Precision > gaussian_elimination(Matrix< N, N, Precision > A, Vector< N, Precision > b)