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.
objects.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 // Copyright (C) 2005,2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk), Gerhard Reitmayr (gr281@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  // dummy structs that are used in 0-ary operators
35  struct Zero;
36  struct SizedZero;
37  struct RCZero;
38  template<class P> struct Identity;
39  template<class P> struct SizedIdentity;
40 
41  template<int S, class P, class B, class Ps> class ScalarsVector;
42  template<int R, int C, class P, class B, class Ps> class ScalarsMatrix;
43  template<int R, int C, class P, class B, class Ps> class AddIdentity;
44  template<class P> class Scalars;
45  template<class P> class SizedScalars;
46  template<class P> class RCScalars;
47 
51  struct One{
52 
53  One(){}
54  template<class C> operator C() const
56  {
57  return 1;
58  }
59  };
60  template<class Rhs> Rhs operator*(One, const Rhs& v){return v;}
61  template<class Lhs> Lhs operator*(const Lhs& v, One){return v;}
62  template<class Rhs> Rhs operator+(One, const Rhs& v){return 1+v;}
63  template<class Lhs> Lhs operator+(const Lhs& v, One){return v+1;}
64  template<class Rhs> Rhs operator-(One, const Rhs& v){return 1-v;}
65  template<class Lhs> Lhs operator-(const Lhs& v, One){return v-1;}
66 
68  inline int operator-(const One&)
69  {
70  return -1;
71  }
72 
77  template<class C> struct NegType
78  {
79  typedef C Type;
80  };
81 
86  template<> struct NegType<One>
87  {
90  typedef int Type;
91  };
92 
96  template<class Rhs> struct Field<One, Rhs>
97  {
100  static const int is = IsField<Rhs>::value;
101  };
102 
106  template<class Lhs> struct Field<Lhs, One>
107  {
110  static const int is = IsField<Lhs>::value;
111  };
112 
113 }
114 
116 // Zero
118 
119 
120 
121 template<> struct Operator<Internal::SizedZero>;
122 template<> struct Operator<Internal::RCZero>;
123 
124 
128 template<> struct Operator<Internal::Zero> {
131  template<int Size, class Precision, class Base>
133  for(int i=0; i < v.size(); i++) {
134  v[i]= 0;
135  }
136  }
137 
138  template<int R, int C, class P, class B>
139  void eval(Matrix<R,C,P,B>& m) const {
140  for(int r=0; r<m.num_rows(); r++){
141  for(int c=0; c<m.num_cols(); c++){
142  m(r,c)=0;
143  }
144  }
145  }
147 
149  Operator<Internal::SizedZero> operator()(int s);
151  Operator<Internal::RCZero> operator()(int r, int c);
152 
153 };
154 
158 template<> struct Operator<Internal::RCZero> : public Operator<Internal::Zero> {
159 
162  Operator(int r, int c) : my_rows(r), my_cols(c) {}
163 
164  const int my_rows;
165  const int my_cols;
166 
167  int num_rows() const {return my_rows;}
168  int num_cols() const {return my_cols;}
170 };
171 
175 template<> struct Operator<Internal::SizedZero> : public Operator<Internal::Zero> {
176 
179  Operator(int s) : my_size(s) {}
180 
181  const int my_size;
182 
183  int size() const {return my_size;}
184  int num_rows() const {return my_size;}
185  int num_cols() const {return my_size;}
187 };
188 
191 }
192 
194  return Operator<Internal::RCZero>(r,c);
195 }
196 
197 
199 // Identity
201 
205 template<int R, int C, class P, class B, class Precision> struct Operator<Internal::AddIdentity<R,C,P,B,Precision> >
206 {
207  const Precision s;
209  bool invert_m;
210 
213  Operator(Precision s_, const Matrix<R,C,P,B>& m_, bool b)
214  :s(s_),m(m_),invert_m(b){}
216 
219  template<int R1, int C1, class P1, class B1>
220  void eval(Matrix<R1,C1,P1,B1>& mm) const{
221  for(int r=0; r < m.num_rows(); r++)
222  for(int c=0; c < m.num_cols(); c++)
223  if(invert_m)
224  mm[r][c] = -m[r][c];
225  else
226  mm[r][c] = m[r][c];
227 
228  for(int i=0; i < m.num_rows(); i++)
229  mm[i][i] += (P)s;
230  }
232 
235  int num_rows() const
236  {
237  return m.num_rows();
238  }
239  int num_cols() const
240  {
241  return m.num_cols();
242  }
244 };
245 
249 template<class Pr> struct Operator<Internal::Identity<Pr> > {
250 
253 
254  typedef Pr Precision;
255  template<class Pout, class Pmult> Operator<Internal::Identity<Pout> > scale_me(const Pmult& m) const
256  {
257  return Operator<Internal::Identity<Pout> >(val*m);
258  }
260 
262  const Precision val;
263 
266  Operator(const Precision& v)
267  :val(v)
268  {}
269 
271  {}
273 
276  template<int R, int C, class P, class B>
277  void eval(Matrix<R,C,P,B>& m) const {
278  SizeMismatch<R, C>::test(m.num_rows(), m.num_cols());
279 
280  for(int r=0; r<m.num_rows(); r++){
281  for(int c=0; c<m.num_cols(); c++){
282  m(r,c)=0;
283  }
284  }
285 
286  for(int r=0; r < m.num_rows(); r++) {
287  m(r,r) = (P)val;
288  }
289  }
290 
291  template<int Rows, int Cols, typename P, typename B>
293  {
294  SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
295  for(int i=0; i < m.num_rows(); i++)
296  m[i][i] += (P)val;
297  }
298 
299  template <int Rows, int Cols, typename P1, typename B1>
301  {
302  SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
304  }
305 
306  template <int Rows, int Cols, typename P1, typename B1>
308  {
309  SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
311  }
312 
313  template <int Rows, int Cols, typename P1, typename B1>
315  {
316  SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
318  }
320 
325  }
327 };
328 
332 template<class Precision> struct Operator<Internal::SizedIdentity<Precision> >
333  : public Operator<Internal::Identity<Precision> > {
334 
336 
339  Operator(int s, const Precision& v)
340  :Operator<Internal::Identity<Precision> > (v), my_size(s)
341  {}
343 
346  const int my_size;
347  int num_rows() const {return my_size;}
348  int num_cols() const {return my_size;}
350 
353  template<class Pout, class Pmult> Operator<Internal::SizedIdentity<Pout> > scale_me(const Pmult& m) const
354  {
355  return Operator<Internal::SizedIdentity<Pout> >(my_size, val*m);
356  }
358 };
360 //
361 // Addition of scalars to vectors and matrices
362 //
363 
364 
368 template<int S, class P, class B, class Precision> struct Operator<Internal::ScalarsVector<S,P,B,Precision> >
369 {
370  const Precision s;
371  const Vector<S,P,B>& v;
372  const bool invert_v;
373 
376  Operator(Precision s_, const Vector<S,P,B>& v_, bool inv)
377  :s(s_),v(v_),invert_v(inv){}
379 
382  template<int S1, class P1, class B1>
383  void eval(Vector<S1,P1,B1>& vv) const{
384  for(int i=0; i < v.size(); i++)
385  if(invert_v)
386  vv[i] = s - v[i];
387  else
388  vv[i] = s + v[i];
389  }
391 
394  int size() const
395  {
396  return v.size();
397  }
399 };
400 
404 template<int R, int C, class P, class B, class Precision> struct Operator<Internal::ScalarsMatrix<R,C,P,B,Precision> >
405 {
406  const Precision s;
408  const bool invert_m;
409  Operator(Precision s_, const Matrix<R,C,P,B>& m_, bool inv)
412  :s(s_),m(m_),invert_m(inv){}
413  template<int R1, int C1, class P1, class B1>
414  void eval(Matrix<R1,C1,P1,B1>& mm) const{
415  for(int r=0; r < m.num_rows(); r++)
416  for(int c=0; c < m.num_cols(); c++)
417  if(invert_m)
418  mm[r][c] = s - m[r][c];
419  else
420  mm[r][c] = s + m[r][c];
421  }
423 
426  int num_rows() const
427  {
428  return m.num_rows();
429  }
430  int num_cols() const
431  {
432  return m.num_cols();
433  }
435 };
436 
441 template<class P> struct Operator<Internal::Scalars<P> >
442 {
445  typedef P Precision;
447 
448  const Precision s;
449 
452  Operator(Precision s_)
453  :s(s_){}
454 
456  {}
458 
460  //
461  // All applications for vector
462  //
465 
466  template <int Size, typename P1, typename B1>
467  void eval(Vector<Size, P1, B1>& v) const
468  {
469  for(int i=0; i < v.size(); i++)
470  v[i] = (P1)s;
471  }
472 
473  template <int Size, typename P1, typename B1>
475  {
476  for(int i=0; i < v.size(); i++)
477  v[i] += (P1)s;
478  }
479 
480  template <int Size, typename P1, typename B1>
482  {
483  for(int i=0; i < v.size(); ++i)
484  v[i] -= (P1)s;
485  }
486 
487  template <int Size, typename P1, typename B1>
489  {
491  }
492 
493  template <int Size, typename P1, typename B1>
495  {
497  }
498 
499  template <int Size, typename P1, typename B1>
501  {
503  }
504 
506  //
507  // All applications for matrix
508  //
509 
510  template <int Rows, int Cols, typename P1, typename B1>
512  {
513  for(int r=0; r < m.num_rows(); r++)
514  for(int c=0; c < m.num_cols(); c++)
515  m[r][c] = s;
516  }
517 
518  template <int Rows, int Cols, typename P1, typename B1>
520  {
521  for(int r=0; r < m.num_rows(); r++)
522  for(int c=0; c < m.num_cols(); c++)
523  m[r][c] += (P1)s;
524  }
525 
526  template <int Rows, int Cols, typename P1, typename B1>
528  {
529  for(int r=0; r < m.num_rows(); r++)
530  for(int c=0; c < m.num_cols(); c++)
531  m[r][c] -= (P1)s;
532  }
533 
534  template <int Rows, int Cols, typename P1, typename B1>
536  {
538  }
539 
540 
541  template <int Rows, int Cols, typename P1, typename B1>
543  {
545  }
546 
547  template <int Rows, int Cols, typename P1, typename B1>
549  {
551  }
554  //
555  // Create sized versions for initialization
556  //
557 
560 
562  {
564  }
565 
567  {
569  }
571 
574  template<class Pout, class Pmult> Operator<Internal::Scalars<Pout> > scale_me(const Pmult& m) const
575  {
576  return Operator<Internal::Scalars<Pout> >(s*m);
577  }
579 };
580 
584 template<class P> struct Operator<Internal::SizedScalars<P> >: public Operator<Internal::Scalars<P> >
585 {
589  const int my_size;
590  int size() const {
591  return my_size;
592  }
593  int num_rows() const {
594  return my_size;
595  }
596  int num_cols() const {
597  return my_size;
598  }
600 
603  Operator(P s, int sz)
604  :Operator<Internal::Scalars<P> >(s),my_size(sz){}
606 
609  template<class Pout, class Pmult> Operator<Internal::SizedScalars<Pout> > scale_me(const Pmult& m) const
610  {
611  return Operator<Internal::SizedScalars<Pout> >(s*m, my_size);
612  }
614 
615 private:
616  void operator()(int);
617  void operator()(int,int);
618 };
619 
620 
624 template<class P> struct Operator<Internal::RCScalars<P> >: public Operator<Internal::Scalars<P> >
625 {
627 
630  const int my_rows, my_cols;
631  int num_rows() const {
632  return my_rows;
633  }
634  int num_cols() const {
635  return my_cols;
636  }
637 
638  Operator(P s, int r, int c)
639  :Operator<Internal::Scalars<P> >(s),my_rows(r),my_cols(c)
640  {}
641 
642  template<class Pout, class Pmult> Operator<Internal::RCScalars<Pout> > scale_me(const Pmult& m) const
643  {
644  return Operator<Internal::RCScalars<Pout> >(s*m, my_rows, my_cols);
645  }
646 
648 private:
649  void operator()(int);
650  void operator()(int,int);
651 };
652 
653 
655 //
656 // How to scale scalable operators
657 //
658 
659 template<template<class> class Op, class Pl, class Pr>
660 Operator<Op<typename Internal::MultiplyType<Pl, Pr>::type > >
661 operator*(const Pl& l, const Operator<Op<Pr> >& r)
662 {
663  return r.template scale_me<typename Internal::MultiplyType<Pl, Pr>::type, Pl>(l);
664 }
665 
666 template<template<class> class Op, class Pl, class Pr>
667 Operator<Op<typename Internal::MultiplyType<Pl, Pr>::type > >
668 operator*(const Operator<Op<Pl> >& l, const Pr& r)
669 {
670  return l.template scale_me<typename Internal::MultiplyType<Pl, Pr>::type>(r);
671 }
672 
673 template<template<class> class Op, class Pl, class Pr>
674 Operator<Op<typename Internal::DivideType<Pl, Pr>::type > >
675 operator/(const Operator<Op<Pl> >& l, const Pr& r)
676 {
677  return l.template scale_me<typename Internal::MultiplyType<Pl, Pr>::type, Pl>(static_cast<typename Internal::DivideType<Pl,Pr>::type>(1)/r);
678 }
679 
680 
681 template<class Op>
683 {
684  return o.template scale_me<typename Operator<Op>::Precision>(-1);
685 }
686 
687 //Special case for negating One
688 template<template<class>class Op>
689 Operator<Op<DefaultPrecision> > operator-(const Operator<Op<Internal::One> >& o)
690 {
691  return o.template scale_me<DefaultPrecision>(-1);
692 }
693 
714 
715 
728 
749 
750 }
751 
static const int is
Definition: operators.hh:52
bool invert_m
Whether the identity should be added to + or - m.
Definition: objects.h:209
Operator< Op< typename Internal::DivideType< Pl, Pr >::type > > operator/(const Operator< Op< Pl > > &l, const Pr &r)
Definition: objects.h:675
Operator< Internal::ScalarsVector< Size, P1, B1, Precision > > add(const Vector< Size, P1, B1 > &v) const
Definition: objects.h:488
Operator< Internal::AddIdentity< Rows, Cols, P1, B1, Precision > > rsubtract(const Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:307
static Operator< Internal::Identity< Internal::One > > Identity
Definition: objects.h:748
void eval(Vector< Size, Precision, Base > &v) const
Definition: objects.h:132
Operator< Internal::SizedScalars< Precision > > operator()(int size) const
Definition: objects.h:561
Everything lives inside this namespace.
Definition: allocator.hh:48
Rhs operator-(One, const Rhs &v)
Subtracts something from One.
Definition: objects.h:64
Rhs operator*(One, const Rhs &v)
Multiplies One by something.
Definition: objects.h:60
static void test(int s1, int s2)
void plusequals(Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:519
Operator< Internal::RCScalars< Pout > > scale_me(const Pmult &m) const
Definition: objects.h:642
static const Operator< Internal::Scalars< Internal::One > > Ones
Definition: objects.h:713
Rhs operator+(One, const Rhs &v)
Adds something to One.
Definition: objects.h:62
void eval(Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:511
void eval(Vector< Size, P1, B1 > &v) const
Definition: objects.h:467
Operator< Internal::Identity< Pout > > scale_me(const Pmult &m) const
Definition: objects.h:255
Operator< Internal::ScalarsMatrix< Rows, Cols, P1, B1, Precision > > add(const Matrix< Rows, Cols, P1, B1 > &v) const
Definition: objects.h:535
void minusequals(Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:527
Operator< Internal::SizedScalars< Pout > > scale_me(const Pmult &m) const
Definition: objects.h:609
const Precision s
Scale of the identity matrix.
Definition: objects.h:207
Operator< Internal::AddIdentity< Rows, Cols, P1, B1, Precision > > lsubtract(const Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:314
Operator< Internal::ScalarsVector< Size, P1, B1, Precision > > rsubtract(const Vector< Size, P1, B1 > &v) const
Definition: objects.h:494
void plusequals(Vector< Size, P1, B1 > &v) const
Definition: objects.h:474
Operator< Internal::ScalarsMatrix< Rows, Cols, P1, B1, typename Internal::NegType< P >::Type > > rsubtract(const Matrix< Rows, Cols, P1, B1 > &v) const
Definition: objects.h:542
Operator< Internal::ScalarsMatrix< Rows, Cols, P1, B1, Precision > > lsubtract(const Matrix< Rows, Cols, P1, B1 > &v) const
Definition: objects.h:548
Operator(Precision s_, const Vector< S, P, B > &v_, bool inv)
Definition: objects.h:376
const Vector< S, P, B > & v
Vector to be added to.
Definition: objects.h:371
Operator< Op > operator-(const Operator< Op > &o)
Definition: objects.h:682
void eval(Matrix< R, C, P, B > &m) const
Definition: objects.h:277
Operator< Internal::SizedIdentity< Precision > > operator()(int s)
Definition: objects.h:323
void eval(Matrix< R1, C1, P1, B1 > &mm) const
Definition: objects.h:220
const Precision s
Value of the scalar being represented.
Definition: objects.h:448
void eval(Matrix< R, C, P, B > &m) const
Definition: objects.h:139
void plusequals(Matrix< Rows, Cols, P, B > &m) const
Definition: objects.h:292
Operator(Precision s_, const Matrix< R, C, P, B > &m_, bool b)
Definition: objects.h:213
Operator< Internal::Scalars< Pout > > scale_me(const Pmult &m) const
Definition: objects.h:574
void minusequals(Vector< Size, P1, B1 > &v) const
Definition: objects.h:481
Operator< Internal::ScalarsVector< Size, P1, B1, Precision > > lsubtract(const Vector< Size, P1, B1 > &v) const
Definition: objects.h:500
Operator< Internal::RCScalars< Precision > > operator()(int r, int c) const
Definition: objects.h:566
Operator< Internal::SizedIdentity< Pout > > scale_me(const Pmult &m) const
Definition: objects.h:353
const Matrix< R, C, P, B > & m
matrix to which the identity should be added
Definition: objects.h:208
C Type
The type of -C.
Definition: objects.h:79
Operator< Internal::AddIdentity< Rows, Cols, P1, B1, Precision > > add(const Matrix< Rows, Cols, P1, B1 > &m) const
Definition: objects.h:300
void eval(Matrix< R1, C1, P1, B1 > &mm) const
Definition: objects.h:414
Vector< Internal::Sizer< S1, S2 >::size, typename Internal::MultiplyType< P1, P2 >::type > operator*(const DiagonalMatrix< S1, P1, B1 > &d, const Vector< S2, P2, B2 > &v)
Definition: diagmatrix.h:111
const Matrix< R, C, P, B > & m
Vector to be added to.
Definition: objects.h:407
static Operator< Internal::Zero > Zeros
Definition: objects.h:727