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.
mbase.hh
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@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 namespace TooN {
32 
33 namespace Internal
34 {
35 // As usual, a positive integer means static and -1 means dynamic.
36 // The new case is that for strides, -2 means that the stride is
37 // the same as num_cols/num_rows, which must be dynamically sized.
38 
39 template<int, int, class, int, int, class> struct GenericMBase;
40 
42 //Closure used to acquire strides
43 //-1 means dynamic stride
44 //-2 means dynamic stride is tied to size for a normal matrix
45 template<int RowStride, int ColStride> struct Slice
46 {
47 
48  template<int Rows, int Cols, class Precision> struct MLayout: public GenericMBase<Rows, Cols, Precision, RowStride, ColStride, MatrixSlice<Rows, Cols, Precision> >
49  {
50  MLayout(Precision* p, int rows, int cols, int rowstride, int colstride)
51  :GenericMBase<Rows,Cols,Precision,RowStride,ColStride,MatrixSlice<Rows, Cols, Precision> >(p, rows, cols, rowstride, colstride)
52  {
53  }
54  };
55 };
56 
57 
58 template<int Rows, int Cols, bool D = (Rows == Dynamic || Cols == Dynamic)>
59 struct DiagSize
60 {
61  static const int size = Dynamic;
62 };
63 template<int Rows, int Cols>
64 struct DiagSize<Rows, Cols, 0>
65 {
66  static const int size = (Rows<Cols?Rows:Cols);
67 };
68 
69 template<int Rs, int Cs, bool D = (Rs == Dynamic || Cs == Dynamic)>
70 struct DiagStride
71 {
72  static const int stride = Dynamic;
73 };
74 template<int Rs, int Cs>
75 struct DiagStride<Rs, Cs, 0>
76 {
77  static const int stride = Rs + Cs;
78 };
79 
80 
81 template<int Rows, int Cols, class Precision, int RowStride, int ColStride, class Mem> struct GenericMBase
82  : public Mem,
83  RowStrideHolder<RowStride>,
84  ColStrideHolder<ColStride>
85 {
86  //Slices can never have tied strides
87  static const int SliceRowStride = RowStride == -2?-1: RowStride;
88  static const int SliceColStride = ColStride == -2?-1: ColStride;
89 
90  int rowstride() const {
91  if(RowStride == -2) { //Normal tied stride
92  return num_cols();
93  } else {
95  }
96  }
97 
98  int colstride() const {
99  if(ColStride == -2) { //Normal tied stride
100  return num_rows();
101  } else {
103  }
104  }
105 
106  //Optional constructors
108 
109  GenericMBase(Precision* p)
110  :Mem(p)
111  {}
112 
113 
114  GenericMBase(Precision* p, int r, int c, int rowstride, int colstride)
115  :Mem(p, r, c),
116  RowStrideHolder<RowStride>(rowstride),
117  ColStrideHolder<ColStride>(colstride)
118  {}
119 
120  GenericMBase(int r, int c)
121  :Mem(r, c) {}
122 
123  template<class Op>
125  : Mem(op),
126  RowStrideHolder<RowStride>(op),
127  ColStrideHolder<ColStride>(op)
128  {}
129 
130  using Mem::my_data;
131  using Mem::num_cols;
132  using Mem::num_rows;
133 
134  Precision& operator()(int r, int c){
135  Internal::check_index(num_rows(), r);
136  Internal::check_index(num_cols(), c);
137  return my_data[r*rowstride() + c*colstride()];
138  }
139 
140  const Precision& operator()(int r, int c) const {
141  Internal::check_index(num_rows(), r);
142  Internal::check_index(num_cols(), c);
143  return my_data[r*rowstride() + c*colstride()];
144  }
145 
146  Precision& operator[](const std::pair<int, int>& index) {
147  Internal::check_index(num_rows(), index.first);
148  Internal::check_index(num_cols(), index.second);
149  return (*this)(index.first, index.second);
150  }
151 
152  const Precision& operator[](const std::pair<int, int>& index) const {
153  Internal::check_index(num_rows(), index.first);
154  Internal::check_index(num_cols(), index.second);
155  return (*this)(index.first, index.second);
156  }
157 
158  // this is the type of vector obtained by [ ]
161 
162  Vec operator[](int r) {
163  Internal::check_index(num_rows(), r);
164  return Vec(my_data + rowstride()* r, num_cols(), colstride(), Slicing());
165  }
166 
167  const CVec operator[](int r) const {
168  Internal::check_index(num_rows(), r);
169  return CVec(my_data + rowstride()* r, num_cols(), colstride(), Slicing());
170  }
171 
172 
173  //Generic matrix slicing
174  template<int Rstart, int Cstart, int Rlength, int Clength>
178 
179  //Always pass the size and stride as a run-time parameter. It will be ignored
180  //by SliceHolder (above) if it is statically determined.
182  my_data+rowstride()*(Rstart==Dynamic?rs:Rstart) + colstride()*(Cstart==Dynamic?cs:Cstart),
183  Rlength==Dynamic?rl:Rlength,
184  Clength==Dynamic?cl:Clength,
185  rowstride(), colstride(), Slicing());
186  }
187 
188  template<int Rstart, int Cstart, int Rlength, int Clength>
192 
193  //Always pass the size and stride as a run-time parameter. It will be ignored
194  //by SliceHolder (above) if it is statically determined.
196  my_data+rowstride()*(Rstart==Dynamic?rs:Rstart) + colstride()*(Cstart==Dynamic?cs:Cstart),
197  Rlength==Dynamic?rl:Rlength,
198  Clength==Dynamic?cl:Clength,
199  rowstride(), colstride(), Slicing());
200  }
201 
202  //Special cases of slicing
203  template<int Rstart, int Cstart, int Rlength, int Clength>
205  {
206  //Extra checking in the static case
209  return slice<Rstart, Cstart, Rlength, Clength>(Rstart, Cstart, Rlength, Clength);
210  }
211 
212  template<int Rstart, int Cstart, int Rlength, int Clength>
214  {
217  return slice<Rstart, Cstart, Rlength, Clength>(Rstart, Cstart, Rlength, Clength);
218  }
219 
220  Matrix<-1, -1, Precision, Slice<SliceRowStride,SliceColStride> > slice(int rs, int cs, int rl, int cl){
221  return slice<Dynamic, Dynamic, Dynamic, Dynamic>(rs, cs, rl, cl);
222  }
223 
224  const Matrix<-1, -1, const Precision, Slice<SliceRowStride,SliceColStride> > slice(int rs, int cs, int rl, int cl) const {
225  return slice<Dynamic, Dynamic, Dynamic, Dynamic>(rs, cs, rl, cl);
226  }
227 
228  //Other slice related functions.
230  return Matrix<Cols, Rows, Precision, Slice<SliceColStride,SliceRowStride> >(my_data, num_cols(), num_rows(), colstride(), rowstride(), Slicing());
231  }
232 
235  }
236 
239 
241  {
242  return Vector<DiagSize, Precision, SliceVBase<DiagStride> >(my_data, std::min(num_cols(), num_rows()), rowstride() + colstride(), Slicing());
243  }
244 
246  {
247  return Vector<DiagSize, const Precision, SliceVBase<DiagStride> >(my_data, std::min(num_cols(), num_rows()), rowstride() + colstride(), Slicing());
248  }
249 };
250 
251 }
252 
254 //
255 // Classes for Matrices owning memory
256 //
257 //
258 struct RowMajor
259 {
260  template<int Rows, int Cols, class Precision> struct MLayout: public Internal::GenericMBase<Rows, Cols, Precision, (Cols==-1?-2:Cols), 1, Internal::MatrixAlloc<Rows, Cols, Precision> >
261  {
262  //Optional constructors.
263 
265 
266  MLayout(int rows, int cols)
267  :Internal::GenericMBase<Rows, Cols, Precision, (Cols == -1 ? -2 : Cols), 1, Internal::MatrixAlloc<Rows, Cols, Precision> >(rows, cols)
268  {}
269 
270  template<class Op>
272  :Internal::GenericMBase<Rows, Cols, Precision, (Cols == -1 ? -2 : Cols), 1, Internal::MatrixAlloc<Rows, Cols, Precision> >(op)
273  {}
274 
275  };
276 };
277 
278 struct ColMajor
279 {
280  template<int Rows, int Cols, class Precision> struct MLayout: public Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows==-1?-2:Rows), Internal::MatrixAlloc<Rows, Cols, Precision> >
281  {
282  //Optional constructors.
283 
285 
286  MLayout(int rows, int cols)
287  :Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows == -1 ? -2 : Rows), Internal::MatrixAlloc<Rows, Cols, Precision> >(rows, cols)
288  {}
289 
290  template<class Op>
292  :Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows == -1 ? -2 : Rows), Internal::MatrixAlloc<Rows, Cols, Precision> >(op)
293  {}
294 
295  };
296 };
297 
298 }
299 
MLayout(int rows, int cols)
Definition: mbase.hh:286
const Matrix<-1,-1, const Precision, Slice< SliceRowStride, SliceColStride > > slice(int rs, int cs, int rl, int cl) const
Definition: mbase.hh:224
Precision & operator[](const std::pair< int, int > &index)
Definition: mbase.hh:146
Matrix< Cols, Rows, Precision, Slice< SliceColStride, SliceRowStride > > T()
Definition: mbase.hh:229
Vector< Cols, const Precision, SliceVBase< SliceColStride > > CVec
Definition: mbase.hh:160
Vector< Cols, Precision, SliceVBase< SliceColStride > > Vec
Definition: mbase.hh:159
GenericMBase(Precision *p)
Definition: mbase.hh:109
const Matrix< Rlength, Clength, const Precision, Slice< SliceRowStride, SliceColStride > > slice() const
Definition: mbase.hh:213
Vector< DiagSize, Precision, SliceVBase< DiagStride > > diagonal_slice()
Definition: mbase.hh:240
Everything lives inside this namespace.
Definition: allocator.hh:48
Matrix< Rlength, Clength, Precision, Slice< SliceRowStride, SliceColStride > > slice()
Definition: mbase.hh:204
static const int Dynamic
Template size value used to indicate dynamically sized vectors and matrices.
Definition: TooN.h:272
const Matrix< Rlength, Clength, const Precision, Slice< SliceRowStride, SliceColStride > > slice(int rs, int cs, int rl, int cl) const
Definition: mbase.hh:189
GenericMBase(const Operator< Op > &op)
Definition: mbase.hh:124
MLayout(Precision *p, int rows, int cols, int rowstride, int colstride)
Definition: mbase.hh:50
static const int SliceColStride
Definition: mbase.hh:88
GenericMBase(int r, int c)
Definition: mbase.hh:120
static const int stride
Definition: mbase.hh:72
const CVec operator[](int r) const
Definition: mbase.hh:167
MLayout(int rows, int cols)
Definition: mbase.hh:266
MLayout(const Operator< Op > &op)
Definition: mbase.hh:291
Matrix< Rlength, Clength, Precision, Slice< SliceRowStride, SliceColStride > > slice(int rs, int cs, int rl, int cl)
Definition: mbase.hh:175
MLayout(const Operator< Op > &op)
Definition: mbase.hh:271
Vector< DiagSize, const Precision, SliceVBase< DiagStride > > diagonal_slice() const
Definition: mbase.hh:245
Precision & operator()(int r, int c)
Definition: mbase.hh:134
const Precision & operator()(int r, int c) const
Definition: mbase.hh:140
static void check_index(int, int)
Definition: debug.hh:28
Matrix<-1,-1, Precision, Slice< SliceRowStride, SliceColStride > > slice(int rs, int cs, int rl, int cl)
Definition: mbase.hh:220
GenericMBase(Precision *p, int r, int c, int rowstride, int colstride)
Definition: mbase.hh:114
static const int SliceRowStride
Definition: mbase.hh:87
const Precision & operator[](const std::pair< int, int > &index) const
Definition: mbase.hh:152
const Matrix< Cols, Rows, const Precision, Slice< SliceColStride, SliceRowStride > > T() const
Definition: mbase.hh:233
static const int size
Definition: mbase.hh:61