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.
golden_section.h
Go to the documentation of this file.
1 #ifndef TOON_GOLDEN_SECTION_H
2 #define TOON_GOLDEN_SECTION_H
3 #include "TooN.h"
4 #include <limits>
5 #include <cmath>
6 #include <cstdlib>
7 #include <iomanip>
8 
9 namespace TooN
10 {
11  using std::numeric_limits;
12 
26  template<class Functor, class Precision> Vector<2, Precision> golden_section_search(Precision a, Precision b, Precision c, Precision fb, const Functor& func, int maxiterations, Precision tol = sqrt(numeric_limits<Precision>::epsilon()))
27  {
28  using std::abs;
29  //The golden ratio:
30  const Precision g = (3.0 - sqrt(5))/2;
31 
32  Precision x1, x2, fx1, fx2;
33 
34  //Perform an initial iteration, to get a 4 point
35  //bracketing. This is rather more convenient than
36  //a 3 point bracketing.
37  if(abs(b-a) > abs(c-b))
38  {
39  x1 = b - g*(b-a);
40  x2 = b;
41 
42  fx1 = func(x1);
43  fx2 = fb;
44  }
45  else
46  {
47  x2 = b + g * (c-b);
48  x1 = b;
49 
50  fx1 = fb;
51  fx2 = func(x2);
52  }
53 
54  //We now have an ordered list of points a x1 x2 c
55 
56  //Termination condition from NR in C
57  int itnum = 1; //We've already done one iteration.
58  while(abs(c-a) > tol * (abs(x2)+abs(x1)) && itnum < maxiterations)
59  {
60  if(fx1 > fx2)
61  {
62  // Bracketing does:
63  // a x1 x2 c
64  // a x1 x2 c
65  a = x1;
66  x1 = x2;
67  x2 = x1 + g * (c-x1);
68 
69  fx1 = fx2;
70  fx2 = func(x2);
71  }
72  else
73  {
74  // Bracketing does:
75  // a x1 x2 c
76  // a x1 x2 c
77  c = x2;
78  x2 = x1;
79  x1= x2 - g * (x2 - a);
80 
81  fx2 = fx1;
82  fx1 = func(x1);
83  }
84  }
85 
86 
87  if(fx1 < fx2)
88  return makeVector<Precision>(x1, fx1);
89  else
90  return makeVector<Precision>(x2, fx2);
91  }
92 
105  template<class Functor, class Precision> Vector<2, Precision> golden_section_search(Precision a, Precision b, Precision c, const Functor& func, int maxiterations, Precision tol = sqrt(numeric_limits<Precision>::epsilon()))
106  {
107  return golden_section_search(a, b, c, func(b), func, maxiterations, tol);
108  }
109 
110 }
111 #endif
Everything lives inside this namespace.
Definition: allocator.hh:48
Vector< 2, Precision > golden_section_search(Precision a, Precision b, Precision c, Precision fb, const Functor &func, int maxiterations, Precision tol=sqrt(numeric_limits< Precision >::epsilon()))
T abs(T t)
Definition: abs.h:30