TooN Algorithm Library - tag  0.2
helpers.h
Go to the documentation of this file.
1 #ifndef TAG_HELPERS_H
2 #define TAG_HELPERS_H
3 
4 #include <TooN/se3.h>
5 #include <TooN/wls.h>
6 #include <cassert>
7 
8 namespace tag {
9 
14 
15 template <class T> inline const typename T::first_type& first_point(const T& t) { return t.first; }
16 template <class T> inline const typename T::second_type& second_point(const T& t) { return t.second; }
17 template <class T> inline double noise(const T& t) { return 1.0; }
18 
32 
33 template <class It> void getProjectiveHomography(It begin, It end, TooN::Matrix<3>& H){
34  assert(std::distance(begin,end) >= 4);
35 
36  TooN::WLS<8> wls;
37  for (It it=begin; it!=end; it++) {
38  const TooN::Vector<2>& a = first_point(*it);
39  const TooN::Vector<2>& b = second_point(*it);
40  const TooN::Vector<8> J1 = TooN::makeVector(a[0], a[1], 1, 0, 0, 0, -b[0]*a[0], -b[0]*a[1]);
41  const TooN::Vector<8> J2 = TooN::makeVector(0, 0, 0, a[0], a[1], 1, -b[1]*a[0], -b[1]*a[1]);
42  wls.add_mJ(b[0], J1, noise(*it));
43  wls.add_mJ(b[1], J2, noise(*it));
44  }
45  wls.compute();
46  TooN::Vector<8> h = wls.get_mu();
47  H[0] = h.template slice<0,3>();
48  H[1] = h.template slice<3,3>();
49  H[2][0] = h[6];
50  H[2][1] = h[7];
51  H[2][2] = 1;
52 }
53 
54 
68 template <class It> TooN::Matrix<3> getProjectiveHomography(It begin, It end){
69  TooN::Matrix<3> H;
70  getProjectiveHomography(begin, end, H);
71  return H;
72 }
73 
78 template<class V, class M > inline void getCrossProductMatrix( const V & vec, M & result ){
79  assert(vec.size() == 3);
80  assert(result.num_cols() == 3 && result.num_rows() == 3);
81  result(0,0) = 0; result(0,1) = -vec[2]; result(0,2) = vec[1];
82  result(1,0) = vec[2]; result(1,1) = 0; result(1,2) = -vec[0];
83  result(2,0) = -vec[1]; result(2,1) = vec[0]; result(2,2) = 0;
84 }
85 
90 template<class V> inline TooN::Matrix<3> getCrossProductMatrix( const V & vec ){
91  TooN::Matrix<3> result;
92  getCrossProductMatrix(vec, result);
93  return result;
94 }
95 
100 template<class M> inline void getEssentialMatrix(const TooN::SE3<> & transform , M & E){
101  //assert(E.num_cols() == 3 && E.num_rows() == 3);
102  const TooN::Vector<3> & t = transform.get_translation();
103  const TooN::Matrix<3> & r = transform.get_rotation().get_matrix();
104  E[0] = t[1] * r[2] - t[2] * r[1];
105  E[1] = t[2] * r[0] - t[0] * r[2];
106  E[2] = t[0] * r[1] - t[1] * r[0];
107 }
108 
113 inline TooN::Matrix<3> getEssentialMatrix(const TooN::SE3<> & transform ){
114  TooN::Matrix<3> E;
115  getEssentialMatrix(transform, E);
116  return E;
117 }
118 
119 } // namespace tag
120 
121 #endif // TAG_HELPERS_H