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.
wls.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2005,2009 Tom Drummond (twd20@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 #ifndef TOON_INCLUDE_WLS_H
31 #define TOON_INCLUDE_WLS_H
32 
33 #include "TooN.h"
34 #include "Cholesky.h"
35 #include "helpers.h"
36 
37 #include <cmath>
38 
39 namespace TooN {
40 
46  template <int Size=Dynamic, class Precision=DefaultPrecision,
47  template<int DecompSize, class DecompPrecision> class Decomposition = Cholesky>
48 class WLS {
49 public:
50 
52  WLS(int size=0) :
53  my_C_inv(size,size),
54  my_vector(size),
55  my_decomposition(size),
56  my_mu(size)
57  {
58  clear();
59  }
60 
62  void clear(){
63  my_C_inv = Zeros;
64  my_vector = Zeros;
65  }
66 
70  void add_prior(Precision val){
71  for(int i=0; i<my_C_inv.num_rows(); i++){
72  my_C_inv(i,i)+=val;
73  }
74  }
75 
79  template<class B2>
81  SizeMismatch<Size,Size>::test(my_C_inv.num_rows(), v.size());
82  for(int i=0; i<my_C_inv.num_rows(); i++){
83  my_C_inv(i,i)+=v[i];
84  }
85  }
86 
90  template<class B2>
92  my_C_inv+=m;
93  }
94 
99  template<class B2>
100  inline void add_mJ(Precision m, const Vector<Size, Precision, B2>& J, Precision weight = 1) {
101 
102  //Upper right triangle only, for speed
103  for(int r=0; r < my_C_inv.num_rows(); r++)
104  {
105  double Jw = weight * J[r];
106  my_vector[r] += m * Jw;
107  for(int c=r; c < my_C_inv.num_rows(); c++)
108  my_C_inv[r][c] += Jw * J[c];
109  }
110  }
111 
116  template<int N, class B1, class B2, class B3>
117  inline void add_mJ(const Vector<N,Precision,B1>& m,
119  const Matrix<N,N,Precision,B3>& invcov){
120  const Matrix<Size,N,Precision> temp = J * invcov;
121  my_C_inv += temp * J.T();
122  my_vector += temp * m;
123  }
124 
129  template<int N, class B1, class B2, class B3>
130  inline void add_mJ_rows(const Vector<N,Precision,B1>& m,
132  const Matrix<N,N,Precision,B3>& invcov){
133  const Matrix<Size,N,Precision> temp = J.T() * invcov;
134  my_C_inv += temp * J;
135  my_vector += temp * m;
136  }
137 
143  template<int N, int S1, class B1, class B2, class B3>
145  const Matrix<N,S1,Precision,B2>& J1, const int index1,
146  const Matrix<N,N,Precision,B3>& invcov){
147  const Matrix<S1,N,Precision> temp1 = J1.T() * invcov;
148  const int size1 = J1.num_cols();
149  my_C_inv.slice(index1, index1, size1, size1) += temp1 * J1;
150  my_vector.slice(index1, size1) += temp1 * m;
151  }
152 
160  template<int N, int S1, int S2, class B1, class B2, class B3, class B4>
162  const Matrix<N,S1,Precision,B2>& J1, const int index1,
163  const Matrix<N,S2,Precision,B3>& J2, const int index2,
164  const Matrix<N,N,Precision,B4>& invcov){
165  const Matrix<S1,N,Precision> temp1 = J1.T() * invcov;
166  const Matrix<S2,N,Precision> temp2 = J2.T() * invcov;
167  const Matrix<S1,S2,Precision> mixed = temp1 * J2;
168  const int size1 = J1.num_cols();
169  const int size2 = J2.num_cols();
170  my_C_inv.slice(index1, index1, size1, size1) += temp1 * J1;
171  my_C_inv.slice(index2, index2, size2, size2) += temp2 * J2;
172  my_C_inv.slice(index1, index2, size1, size2) += mixed;
173  my_C_inv.slice(index2, index1, size2, size1) += mixed.T();
174  my_vector.slice(index1, size1) += temp1 * m;
175  my_vector.slice(index2, size2) += temp2 * m;
176  }
177 
180  void compute(){
181 
182  //Copy the upper right triangle to the empty lower-left.
183  for(int r=1; r < my_C_inv.num_rows(); r++)
184  for(int c=0; c < r; c++)
185  my_C_inv[r][c] = my_C_inv[c][r];
186 
187  my_decomposition.compute(my_C_inv);
189  }
190 
193  void operator += (const WLS& meas){
194  my_vector+=meas.my_vector;
195  my_C_inv += meas.my_C_inv;
196  }
197 
203  const Vector<Size,Precision>& get_mu() const {return my_mu;}
205  const Vector<Size,Precision>& get_vector() const {return my_vector;}
206  Decomposition<Size,Precision>& get_decomposition(){return my_decomposition;}
207  const Decomposition<Size,Precision>& get_decomposition() const {return my_decomposition;}
208 
209 
210 private:
213  Decomposition<Size,Precision> my_decomposition;
215 
216  // comment out to allow bitwise copying
217  WLS( WLS& copyof );
218  int operator = ( WLS& copyof );
219 };
220 
221 }
222 
223 #endif
void clear()
Clear all the measurements and apply a constant regularisation term.
Definition: wls.h:62
void add_prior(const Matrix< Size, Size, Precision, B2 > &m)
Definition: wls.h:91
void add_mJ_rows(const Vector< N, Precision, B1 > &m, const Matrix< N, Size, Precision, B2 > &J, const Matrix< N, N, Precision, B3 > &invcov)
Definition: wls.h:130
void operator+=(const WLS &meas)
Definition: wls.h:193
Decomposition< Size, Precision > & get_decomposition()
Return the decomposition object used to compute .
Definition: wls.h:206
Everything lives inside this namespace.
Definition: allocator.hh:48
int operator=(WLS &copyof)
void add_sparse_mJ_rows(const Vector< N, Precision, B1 > &m, const Matrix< N, S1, Precision, B2 > &J1, const int index1, const Matrix< N, N, Precision, B3 > &invcov)
Definition: wls.h:144
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)
const Vector< Size, Precision > & get_vector() const
Returns the vector .
Definition: wls.h:205
void add_mJ(Precision m, const Vector< Size, Precision, B2 > &J, Precision weight=1)
Definition: wls.h:100
double DefaultPrecision
All TooN classes default to using this precision for computations and storage.
Definition: TooN.h:301
void add_prior(const Vector< Size, Precision, B2 > &v)
Definition: wls.h:80
const Decomposition< Size, Precision > & get_decomposition() const
Return the decomposition object used to compute .
Definition: wls.h:207
Vector< Size, Precision > & get_mu()
Returns the update. With no prior, this is the result of .
Definition: wls.h:202
Matrix< Size, Size, Precision > & get_C_inv()
Returns the inverse covariance matrix.
Definition: wls.h:199
Vector< Size, Precision > my_vector
Definition: wls.h:212
Vector< Size, Precision > & get_vector()
Returns the vector .
Definition: wls.h:204
WLS(int size=0)
Default constructor or construct with the number of dimensions for the Dynamic case.
Definition: wls.h:52
Vector< Size, Precision > my_mu
Definition: wls.h:214
void compute()
Definition: wls.h:180
const Vector< Size, Precision > & get_mu() const
Returns the update. With no prior, this is the result of .
Definition: wls.h:203
void add_mJ(const Vector< N, Precision, B1 > &m, const Matrix< Size, N, Precision, B2 > &J, const Matrix< N, N, Precision, B3 > &invcov)
Definition: wls.h:117
const Matrix< Size, Size, Precision > & get_C_inv() const
Returns the inverse covariance matrix.
Definition: wls.h:201
Decomposition< Size, Precision > my_decomposition
Definition: wls.h:213
void add_prior(Precision val)
Definition: wls.h:70
Matrix< Size, Size, Precision > my_C_inv
Definition: wls.h:211
static Operator< Internal::Zero > Zeros
Definition: objects.h:727
Definition: wls.h:48
void add_sparse_mJ_rows(const Vector< N, Precision, B1 > &m, const Matrix< N, S1, Precision, B2 > &J1, const int index1, const Matrix< N, S2, Precision, B3 > &J2, const int index2, const Matrix< N, N, Precision, B4 > &invcov)
Definition: wls.h:161