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.
irls.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 
31 #ifndef __IRLS_H
32 #define __IRLS_H
33 
34 #include "wls.h"
35 #include <cassert>
36 #include <cmath>
37 
38 namespace TooN {
39 
44  template<typename Precision>
45  struct RobustI {
46  void set_sd(Precision x){ sd_inlier = x;}
47  double sd_inlier;
48  inline Precision reweight(Precision x) {return 1/(sd_inlier+fabs(x));}
49  inline Precision true_scale(Precision x) {return reweight(x) - fabs(x)*reweight(x)*reweight(x);}
50  inline Precision objective(Precision x) {return fabs(x) + sd_inlier*log(sd_inlier*reweight(x));}
51  };
52 
57  template<typename Precision>
58  struct RobustII {
59  void set_sd(Precision x){ sd_inlier = x*x;}
60  Precision sd_inlier;
61  inline Precision reweight(Precision d){return 1/(sd_inlier+d*d);}
62  inline Precision true_scale(Precision d){return d - 2*d*reweight(d);}
63  inline Precision objective(Precision d){return 0.5 * log(1 + d*d/sd_inlier);}
64  };
65 
70  template<typename Precision>
71  struct ILinear {
72  void set_sd(Precision){}
73  inline Precision reweight(Precision d){return 1;}
74  inline Precision true_scale(Precision d){return 1;}
75  inline Precision objective(Precision d){return d*d;}
76  };
77 
83  template<typename Precision>
84  struct RobustIII {
85 
86  void set_sd(Precision x){ sd_inlier = x*x;}
87  Precision sd_inlier;
88  Precision reweight(Precision x) const
90  {
91  double d = (1 + x*x/sd_inlier);
92  return 1/(d*d);
93  }
95  Precision objective(Precision x) const
96  {
97  return x*x / (2*(1 + x*x/sd_inlier));
98  }
99  };
100 
106  template <int Size, typename Precision, template <typename Precision> class Reweight>
107  class IRLS
108  : public Reweight<Precision>,
109  public WLS<Size,Precision>
110  {
111  public:
112  IRLS(int size=Size):
113  WLS<Size,Precision>(size),
114  my_true_C_inv(Zeros(size))
115  {
116  my_residual=0;
117  }
118 
119  template<int Size2, typename Precision2, typename Base2>
120  inline void add_mJ(Precision m, const Vector<Size2,Precision2,Base2>& J) {
121  SizeMismatch<Size,Size2>::test(my_true_C_inv.num_rows(), J.size());
122 
123  Precision scale = Reweight<Precision>::reweight(m);
124  Precision ts = Reweight<Precision>::true_scale(m);
125  my_residual += Reweight<Precision>::objective(m);
126 
127  WLS<Size>::add_mJ(m,J,scale);
128 
129  Vector<Size,Precision> scaledm(m*ts);
130 
131  my_true_C_inv += scaledm.as_col() * scaledm.as_row();
132 
133  }
134 
135  void operator += (const IRLS& meas){
136  WLS<Size>::operator+=(meas);
138  }
139 
140 
143 
144  Precision get_residual() {return my_residual;}
145 
146  void clear(){
148  my_residual=0;
150  }
151 
152  private:
153 
154  Precision my_residual;
155 
157 
158  // comment out to allow bitwise copying
159  IRLS( IRLS& copyof );
160  int operator = ( IRLS& copyof );
161  };
162 
163 }
164 
165 #endif
Precision objective(Precision x) const
Definition: irls.h:95
Precision sd_inlier
Definition: irls.h:87
void clear()
Clear all the measurements and apply a constant regularisation term.
Definition: wls.h:62
Precision sd_inlier
The inlier standard deviation squared, .
Definition: irls.h:60
void set_sd(Precision)
Set the noise standard deviation (does nothing).
Definition: irls.h:72
Precision objective(Precision d)
Returns .
Definition: irls.h:63
Precision reweight(Precision d)
Returns .
Definition: irls.h:73
Precision reweight(Precision x)
Returns .
Definition: irls.h:48
void set_sd(Precision x)
Set the noise standard deviation.
Definition: irls.h:46
void set_sd(Precision x)
Set the noise standard deviation.
Definition: irls.h:86
Matrix< Size, Size, Precision > & get_true_C_inv()
Definition: irls.h:141
void operator+=(const WLS &meas)
Definition: wls.h:193
Matrix< Size, Size, Precision > my_true_C_inv
Definition: irls.h:156
Everything lives inside this namespace.
Definition: allocator.hh:48
static void test(int s1, int s2)
void add_mJ(Precision m, const Vector< Size, Precision, B2 > &J, Precision weight=1)
Definition: wls.h:100
Precision true_scale(Precision x)
Returns .
Definition: irls.h:49
int operator=(IRLS &copyof)
Precision objective(Precision d)
Returns .
Definition: irls.h:75
void add_mJ(Precision m, const Vector< Size2, Precision2, Base2 > &J)
Definition: irls.h:120
double sd_inlier
The inlier standard deviation, .
Definition: irls.h:47
Precision get_residual()
Definition: irls.h:144
const Matrix< Size, Size, Precision > & get_true_C_inv() const
Definition: irls.h:142
Precision true_scale(Precision d)
Returns .
Definition: irls.h:74
void clear()
Definition: irls.h:146
Precision true_scale(Precision d)
Returns .
Definition: irls.h:62
Precision reweight(Precision x) const
Returns .
Definition: irls.h:89
Precision objective(Precision x)
Returns .
Definition: irls.h:50
Precision my_residual
Definition: irls.h:154
void operator+=(const IRLS &meas)
Definition: irls.h:135
void set_sd(Precision x)
Set the noise standard deviation.
Definition: irls.h:59
static Operator< Internal::Zero > Zeros
Definition: objects.h:727
Definition: wls.h:48
IRLS(int size=Size)
Definition: irls.h:112
Precision reweight(Precision d)
Returns .
Definition: irls.h:61