TooN
so2.h
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 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef TOON_INCLUDE_SO2_H
30 #define TOON_INCLUDE_SO2_H
31 
32 #include <TooN/TooN.h>
33 #include <TooN/helpers.h>
34 
35 namespace TooN {
36 
37 template <typename Precision> class SO2;
38 template <typename Precision> class SE2;
39 template <typename Precision> class SIM2;
40 
41 template<typename Precision> inline std::istream & operator>>(std::istream &, SO2<Precision> & );
42 template<typename Precision> inline std::istream & operator>>(std::istream &, SE2<Precision> & );
43 template<typename Precision> inline std::istream & operator>>(std::istream &, SIM2<Precision> & );
44 
49 template<typename Precision = DefaultPrecision>
50 class SO2 {
51  friend std::istream& operator>> <Precision>(std::istream&, SO2& );
52  friend std::istream& operator>> <Precision>(std::istream&, SE2<Precision>& );
53  friend std::istream& operator>> <Precision>(std::istream&, SIM2<Precision>& );
54 
55 public:
58  :my_matrix(Identity)
59  {}
60 
61  SO2() : my_matrix(Identity) {}
62 
64  SO2(const Matrix<2,2,Precision>& rhs) {
65  *this = rhs;
66  coerce();
67  }
68 
70  explicit SO2(const Precision l) { *this = exp(l); }
71 
74  template <int R, int C, typename P, typename A>
76  my_matrix = rhs;
77  coerce();
78  return *this;
79  }
80 
82  void coerce(){
83  my_matrix[0] = unit(my_matrix[0]);
84  my_matrix[1] -= my_matrix[0] * (my_matrix[0]*my_matrix[1]);
85  my_matrix[1] = unit(my_matrix[1]);
86  }
87 
89  inline static SO2 exp(const Precision & d){
90  SO2<Precision> result;
91  result.my_matrix[0][0] = result.my_matrix[1][1] = cos(d);
92  result.my_matrix[1][0] = sin(d);
93  result.my_matrix[0][1] = -result.my_matrix[1][0];
94  return result;
95  }
96 
98  Precision ln() const { return atan2(my_matrix[1][0], my_matrix[0][0]); }
99 
101  SO2 inverse() const { return SO2(*this, Invert()); }
102 
104  template <typename P>
105  SO2& operator *=(const SO2<P>& rhs){
106  my_matrix=my_matrix*rhs.get_matrix();
107  return *this;
108  }
109 
111  template <typename P>
114  }
115 
117  const Matrix<2,2,Precision>& get_matrix() const {return my_matrix;}
118 
121  Matrix<2,2,Precision> result;
122  result[0] = makeVector(0,-1);
123  result[1] = makeVector(1,0);
124  return result;
125  }
126 
127 private:
128  struct Invert {};
129  inline SO2(const SO2& so2, const Invert&) : my_matrix(so2.my_matrix.T()) {}
130  template <typename PA, typename PB>
131  inline SO2(const SO2<PA>& a, const SO2<PB>& b) : my_matrix(a.get_matrix()*b.get_matrix()) {}
132 
133  Matrix<2,2,Precision> my_matrix;
134 };
135 
138 template <typename Precision>
139 inline std::ostream& operator<< (std::ostream& os, const SO2<Precision> & rhs){
140  return os << rhs.get_matrix();
141 }
142 
145 template <typename Precision>
146 inline std::istream& operator>>(std::istream& is, SO2<Precision>& rhs){
147  is >> rhs.my_matrix;
148  rhs.coerce();
149  return is;
150 }
151 
154 template<int D, typename P1, typename PV, typename Accessor>
156  return lhs.get_matrix() * rhs;
157 }
158 
161 template<int D, typename P1, typename PV, typename Accessor>
163  return lhs * rhs.get_matrix();
164 }
165 
168 template <int R, int C, typename P1, typename P2, typename Accessor>
170  return lhs.get_matrix() * rhs;
171 }
172 
175 template <int R, int C, typename P1, typename P2, typename Accessor>
177  return lhs * rhs.get_matrix();
178 }
179 
180 }
181 
182 #endif
std::istream & operator>>(std::istream &is, SO2< Precision > &rhs)
Read from SO2 to a stream.
Definition: so2.h:146
const Matrix< 2, 2, Precision > & get_matrix() const
Returns the SO2 as a Matrix<2>
Definition: so2.h:117
Vector< 2, typename Internal::MultiplyType< PV, P1 >::type > operator*(const Vector< D, PV, Accessor > &lhs, const SO2< P1 > &rhs)
Left-multiply by a Vector.
Definition: so2.h:162
Matrix< 2, C, typename Internal::MultiplyType< P1, P2 >::type > operator*(const SO2< P1 > &lhs, const Matrix< R, C, P2, Accessor > &rhs)
Right-multiply by a Matrix.
Definition: so2.h:169
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
SO2(const Operator< Internal::Identity< Internal::One > > &)
Default constructor. Initialises the matrix to the identity (no rotation)
Definition: so2.h:57
Class to represent a two-dimensional rotation matrix.
Definition: so2.h:37
SO2< typename Internal::MultiplyType< Precision, P >::type > operator*(const SO2< P > &rhs) const
Right-multiply by another rotation matrix.
Definition: so2.h:112
static SO2 exp(const Precision &d)
Exponentiate an angle in the Lie algebra to generate a new SO2.
Definition: so2.h:89
void coerce()
Modifies the matrix to make sure it is a valid rotation matrix.
Definition: so2.h:82
SO2 & operator=(const Matrix< R, C, P, A > &rhs)
Assigment operator from a general matrix.
Definition: so2.h:75
Matrix< R, 2, typename Internal::MultiplyType< P1, P2 >::type > operator*(const Matrix< R, C, P1, Accessor > &lhs, const SO2< P2 > &rhs)
Left-multiply by a Matrix.
Definition: so2.h:176
Vector< 2, typename Internal::MultiplyType< P1, PV >::type > operator*(const SO2< P1 > &lhs, const Vector< D, PV, Accessor > &rhs)
Right-multiply by a Vector.
Definition: so2.h:155
Definition: operators.hh:119
SO2 inverse() const
Returns the inverse of this matrix (=the transpose, so this is a fast operation)
Definition: so2.h:101
static Matrix< 2, 2, Precision > generator()
returns generator matrix
Definition: so2.h:120
SO2 & operator*=(const SO2< P > &rhs)
Self right-multiply by another rotation matrix.
Definition: so2.h:105
Represent a two-dimensional Similarity transformation (a rotation, a uniform scale and a translation)...
Definition: sim2.h:50
Definition: objects.h:36
SO2(const Matrix< 2, 2, Precision > &rhs)
Construct from a rotation matrix.
Definition: so2.h:64
Represent a two-dimensional Euclidean transformation (a rotation and a translation).
Definition: se2.h:50
SO2(const Precision l)
Construct from an angle.
Definition: so2.h:70
Precision ln() const
extracts the rotation angle from the SO2
Definition: so2.h:98
Vector< Size, Precision > unit(const Vector< Size, Precision, Base > &v)
Compute a the unit vector .
Definition: helpers.h:153