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.
sl.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2005,2009 Tom Drummond (twd20@cam.ac.uk),
4 // Gerhard Reitmayr (gr281@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 #ifndef TOON_INCLUDE_SL_H
32 #define TOON_INCLUDE_SL_H
33 
34 #include "TooN.h"
35 #include "helpers.h"
36 #include "gaussian_elimination.h"
37 #include "determinant.h"
38 #include <cassert>
39 
40 namespace TooN {
41 
42 template <int N, typename P> class SL;
43 template <int N, typename P> std::istream & operator>>(std::istream &, SL<N, P> &);
44 
58 template <int N, typename Precision = double>
59 class SL {
60  friend std::istream & operator>> <N,Precision>(std::istream &, SL &);
61 public:
62  static const int size = N;
63  static const int dim = N*N - 1;
64 
67 
69  template <int S, typename P, typename B>
70  SL( const Vector<S,P,B> & v ) { *this = exp(v); }
71 
73  template <int R, int C, typename P, typename A>
74  SL(const Matrix<R,C,P,A>& M) : my_matrix(M) { coerce(); }
75 
77  const Matrix<N,N,Precision> & get_matrix() const { return my_matrix; }
79  SL inverse() const { return SL(*this, Invert()); }
80 
82  SL operator*( const SL & rhs) const { return SL(*this, rhs); }
84  SL operator*=( const SL & rhs) { *this = *this*rhs; return *this; }
85 
88  template <int S, typename P, typename B>
89  static inline SL exp( const Vector<S,P,B> &);
90 
94  static inline Matrix<N,N,Precision> generator(int);
95 
96 private:
97  struct Invert {};
98  SL( const SL & from, struct Invert ) {
99  Matrix<N> id = Identity;
101  }
102  SL( const SL & a, const SL & b) : my_matrix(a.get_matrix() * b.get_matrix()) {}
103 
104  void coerce(){
105  using std::abs;
106  Precision det = determinant(my_matrix);
107  assert(abs(det) > 0);
108  using std::pow;
109  my_matrix /= pow(det, 1.0/N);
110  }
111 
115  static const int COUNT_DIAG = N - 1;
116  static const int COUNT_SYMM = (dim - COUNT_DIAG)/2;
117  static const int COUNT_ASYMM = COUNT_SYMM;
118  static const int DIAG_LIMIT = COUNT_DIAG;
119  static const int SYMM_LIMIT = COUNT_SYMM + DIAG_LIMIT;
121 
123 };
124 
125 template <int N, typename Precision>
126 template <int S, typename P, typename B>
128  SizeMismatch<S,dim>::test(v.size(), dim);
130  for(int i = 0; i < dim; ++i)
131  t += generator(i) * v[i];
132  SL<N, Precision> result;
133  result.my_matrix = TooN::exp(t);
134  return result;
135 }
136 
137 template <int N, typename Precision>
139  assert( i > -1 && i < dim );
141  if(i < DIAG_LIMIT) { // first ones are the diagonal ones
142  result(i,i) = 1;
143  result(i+1,i+1) = -1;
144  } else if(i < SYMM_LIMIT){ // then the symmetric ones
145  int row = 0, col = i - DIAG_LIMIT + 1;
146  while(col > (N - row - 1)){
147  col -= (N - row - 1);
148  ++row;
149  }
150  col += row;
151  result(row, col) = result(col, row) = 1;
152  } else { // finally the antisymmetric ones
153  int row = 0, col = i - SYMM_LIMIT + 1;
154  while(col > N - row - 1){
155  col -= N - row - 1;
156  ++row;
157  }
158  col += row;
159  result(row, col) = -1;
160  result(col, row) = 1;
161  }
162  return result;
163 }
164 
165 template <int S, typename PV, typename B, int N, typename P>
167  return lhs.get_matrix() * rhs;
168 }
169 
170 template <int S, typename PV, typename B, int N, typename P>
172  return lhs * rhs.get_matrix();
173 }
174 
175 template<int R, int C, typename PM, typename A, int N, typename P> inline
177  return lhs.get_matrix() * rhs;
178 }
179 
180 template<int R, int C, typename PM, typename A, int N, typename P> inline
182  return lhs * rhs.get_matrix();
183 }
184 
185 template <int N, typename P>
186 std::ostream & operator<<(std::ostream & out, const SL<N, P> & h){
187  out << h.get_matrix();
188  return out;
189 }
190 
191 template <int N, typename P>
192 std::istream & operator>>(std::istream & in, SL<N, P> & h){
193  in >> h.my_matrix;
194  h.coerce();
195  return in;
196 }
197 
198 };
199 
200 #endif
SL(const SL &from, struct Invert)
Definition: sl.h:98
static Operator< Internal::Identity< Internal::One > > Identity
Definition: objects.h:748
static const int COUNT_SYMM
Definition: sl.h:116
P determinant(const Matrix< R, C, P, B > &A)
Definition: determinant.h:149
static SL exp(const Vector< S, P, B > &)
SL(const Vector< S, P, B > &v)
exp constructor, creates element through exponentiation of Lie algebra vector. see SL::exp...
Definition: sl.h:70
Matrix< N, N, Precision > my_matrix
}
Definition: sl.h:122
Everything lives inside this namespace.
Definition: allocator.hh:48
static const int COUNT_DIAG
Definition: sl.h:115
static void test(int s1, int s2)
SL(const Matrix< R, C, P, A > &M)
copy constructor from a matrix, coerces matrix to be of determinant = 1
Definition: sl.h:74
Definition: sl.h:42
static const int size
size of the matrices represented by SL
Definition: sl.h:62
T abs(T t)
Definition: abs.h:30
void coerce()
Definition: sl.h:104
SL inverse() const
returns the inverse using LU
Definition: sl.h:79
SL operator*(const SL &rhs) const
multiplies to SLs together by multiplying the underlying matrices
Definition: sl.h:82
static Matrix< N, N, Precision > generator(int)
Definition: sl.h:138
static const int DIAG_LIMIT
Definition: sl.h:118
SL(const SL &a, const SL &b)
Definition: sl.h:102
static const int SYMM_LIMIT
Definition: sl.h:119
Matrix< R, C, P > exp(const Matrix< R, C, P, B > &m)
Definition: helpers.h:284
SL()
default constructor, creates identity element
Definition: sl.h:66
static const int dim
dimension of the vector space represented by SL
Definition: sl.h:63
Vector< N, Precision > gaussian_elimination(Matrix< N, N, Precision > A, Vector< N, Precision > b)
static const int COUNT_ASYMM
Definition: sl.h:117
std::istream & operator>>(std::istream &is, Vector< Size, Precision, Base > &v)
Definition: operators.hh:657
SL operator*=(const SL &rhs)
right multiplies this SL with another one
Definition: sl.h:84
Vector< Internal::Sizer< S1, S2 >::size, typename Internal::MultiplyType< P1, P2 >::type > operator*(const DiagonalMatrix< S1, P1, B1 > &d, const Vector< S2, P2, B2 > &v)
Definition: diagmatrix.h:111
static Operator< Internal::Zero > Zeros
Definition: objects.h:727
const Matrix< N, N, Precision > & get_matrix() const
returns the represented matrix
Definition: sl.h:77