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.
determinant.h
Go to the documentation of this file.
1 #ifndef TOON_INCLUDE_DETERMINANT_H
2 #define TOON_INCLUDE_DETERMINANT_H
3 #include "TooN.h"
4 #include <cstdlib>
5 #include <utility>
6 #ifdef TOON_DETERMINANT_LAPACK
7  #include "LU.h"
8 #endif
9 
10 namespace TooN
11 {
12  namespace Internal
13  {
20  template<int R, int C> struct Square
21  {
22  };
23 
24 
28  template<int R> struct Square<R, R>
29  {
30  static const int Size = R;
31  };
32 
37  template<int R> struct Square<R, Dynamic>
38  {
39  static const int Size = R;
40  };
45  template<int C> struct Square<Dynamic, C>
46  {
47  static const int Size = C;
48  };
53  template<> struct Square<Dynamic, Dynamic>
54  {
55  static const int Size = Dynamic;
56  };
57  };
58 
59 
65  template<int R, int C, typename Precision, typename Base>
67  {
69  TooN::SizeMismatch<R, C>::test(A.num_rows(), A.num_cols());
70  using std::swap;
71  using std::abs;
72 
73  int size=A.num_rows();
74 
75  //If row operations of the form row_a += alpha * row_b
76  //then the determinant is unaffected. However, if a row
77  //is scaled, then the determinant is scaled by the same
78  //amount.
79  Precision determinant=1;
80 
81  for (int i=0; i<size; ++i) {
82 
83  //Find the pivot
84  int argmax = i;
85  Precision maxval = abs(A[i][i]);
86 
87  for (int ii=i+1; ii<size; ++ii) {
88  double v = abs(A[ii][i]);
89  if (v > maxval) {
90  maxval = v;
91  argmax = ii;
92  }
93  }
94  Precision pivot = A[argmax][i];
95 
96  //assert(abs(pivot) > 1e-16);
97 
98  //Swap the current row with the pivot row if necessary.
99  //A row swap negates the determinant.
100  if (argmax != i) {
101  determinant*=-1;
102  for (int j=i; j<size; ++j)
103  swap(A[i][j], A[argmax][j]);
104  }
105 
106  determinant *= A[i][i];
107 
108  if(determinant == 0)
109  return 0;
110 
111  for (int u=i+1; u<size; ++u) {
112  //Do not multiply out the usual 1/pivot term
113  //to avoid division. It causes poor scaling.
114  double factor = A[u][i]/pivot;
115 
116  for (int j=i+1; j<size; ++j)
117  A[u][j] = A[u][j] - factor * A[i][j];
118  }
119  }
120 
121  return determinant;
122  }
123 
124  #ifdef TOON_DETERMINANT_LAPACK
125 
130  template<int R, int C, class P, class B>
131  P determinant_LU(const Matrix<R, C, P, B>& A)
132  {
133  TooN::SizeMismatch<R, C>::test(A.num_rows(), A.num_cols());
134  LU<Internal::Square<R,C>::Size, P> lu(A);
135  return lu.determinant();
136  }
137  #endif
138 
148  template<int R, int C, class P, class B>
150  {
151  TooN::SizeMismatch<R, C>::test(A.num_rows(), A.num_cols());
152  if(A.num_rows() == 2)
153  return A[0][0]*A[1][1] - A[1][0]*A[0][1];
154  #if defined TOON_DETERMINANT_LAPACK && TOON_DETERMINANT_LAPACK != -1
155  else if(A.num_rows() >= TOON_DETERMINANT_LAPACK)
156  return determinant_LU(A);
157  #endif
158  else
160  }
161 }
162 
163 #endif
P determinant(const Matrix< R, C, P, B > &A)
Definition: determinant.h:149
Everything lives inside this namespace.
Definition: allocator.hh:48
Precision determinant_gaussian_elimination(const Matrix< R, C, Precision, Base > &A_)
Definition: determinant.h:66
static const int Dynamic
Template size value used to indicate dynamically sized vectors and matrices.
Definition: TooN.h:272
static void test(int s1, int s2)
T abs(T t)
Definition: abs.h:30