TooN
fadbad.h
1 // -*- c++ -*-
2 
3 // Copyright (C) 2005,2009 Tom Drummond (twd20@cam.ac.uk)
4 
5 //All rights reserved.
6 //
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
9 //are met:
10 //1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
17 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
20 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 //POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef TOON_FADBAD_INTEGATION_H
29 #define TOON_FADBAD_INTEGATION_H
30 
31 #include <iostream>
32 
33 #include <TooN/TooN.h>
34 #include <TooN/se3.h>
35 #include <TooN/se2.h>
36 
37 #include <FADBAD++/fadiff.h>
38 
39 namespace fadbad {
40 
41 template <typename P, unsigned int N>
42 inline std::ostream & operator<<( std::ostream & out, const F<P, N> & val ){
43  return out << val.val();
44 }
45 
46 template <typename P, unsigned int N>
47 inline F<P, N> abs( const F<P, N> & val ){
48  return (val.val() < 0) ? -val : val;
49 }
50 
51 }
52 
53 namespace TooN {
54 
55 template<typename C, unsigned int N> struct IsField<fadbad::F<C, N> >
56 {
57  static const int value = numeric_limits<C>::is_specialized;
58 };
59 
60 template <int N, typename T, typename A, unsigned D>
61 inline Vector<N, T> get_value( const Vector<N, fadbad::F<T, D>, A> & v ){
62  Vector<N,T> result(v.size());
63  for(int i = 0; i < result.size(); ++i)
64  result[i] = v[i].val();
65  return result;
66 }
67 
68 template <typename P, int N, typename A>
69 inline Vector<N, fadbad::F<P> > make_fad_vector( const Vector<N, P, A> & val, const unsigned start = 0, const unsigned size = N ){
70  Vector<N, fadbad::F<P> > result = val;
71  for(unsigned i = 0, d = start; i < val.size() && d < size; ++i, ++d)
72  result[i].diff(d,size);
73  return result;
74 }
75 
76 template <unsigned D, typename P, int N, typename A>
77 inline Vector<N, fadbad::F<P,D> > make_fad_vector( const Vector<N, P, A> & val, const unsigned start = 0 ){
78  Vector<N, fadbad::F<P,D> > result = val;
79  for(unsigned i = 0, d = start; i < unsigned(val.size()) && d < D; ++i, ++d)
80  result[i].diff(d);
81  return result;
82 }
83 
84 template <unsigned D, typename P, int N, typename A>
85 inline Vector<N, P> get_derivative( const Vector<N, fadbad::F<P,D>, A> & val, const int dim ){
86  Vector<N, P> r;
87  for(int i = 0; i < N; ++i)
88  r[i] = val[i].deriv(dim);
89  return r;
90 }
91 
92 template <unsigned D, typename P, int N, typename A>
93 inline Matrix<N, D, P> get_jacobian( const Vector<N, fadbad::F<P, D>, A> & val ){
94  Matrix<N, D, P> result(N, val[0].size());
95  for(unsigned i = 0; i < val[0].size(); ++i)
96  result.T()[i] = get_derivative( val, i );
97  return result;
98 }
99 
100 template <int R, int C, typename P, unsigned D, typename A>
101 inline Matrix<R, C, P> get_derivative( const Matrix<R,C, fadbad::F<P, D>, A> & val, const int dim ){
102  Matrix<R, C, P> result;
103  for(int r = 0; r < R; ++r)
104  for(int c = 0; c < C; ++c)
105  result[r][c] = val[r][c].deriv(dim);
106  return result;
107 }
108 
109 template <typename P>
110 inline SO3<fadbad::F<P> > make_fad_so3(int start = 0, int size = 3){
111  // const Vector<3, fadbad::F<double> > p = make_fad_vector(makeVector(0.0, 0, 0), start, size);
112  // return SO3<fadbad::F<double> >(p);
113  SO3<fadbad::F<P> > r;
114  // this is a hack
115  Matrix<3,3,fadbad::F<P> > & m = const_cast<Matrix<3,3,fadbad::F<P> > &>(r.get_matrix());
116  m(2,1).diff(start, size);
117  m(1,2) = m(2,1) * -1;
118 
119  m(0,2).diff(start+1, size);
120  m(2,0) = m(0,2) * -1;
121 
122  m(1,0).diff(start+2, size);
123  m(0,1) = m(1,0) * -1;
124 
125  return r;
126 }
127 
128 template <typename P, unsigned D>
129 inline SO3<fadbad::F<P, D> > make_fad_so3(int start = 0){
130  // const Vector<3, fadbad::F<double> > p = make_fad_vector(makeVector(0.0, 0, 0), start, size);
131  // return SO3<fadbad::F<double> >(p);
133  // this is a hack
135  m(2,1).diff(start);
136  m(1,2) = m(2,1) * -1;
137 
138  m(0,2).diff(start+1);
139  m(2,0) = m(0,2) * -1;
140 
141  m(1,0).diff(start+2);
142  m(0,1) = m(1,0) * -1;
143 
144  return r;
145 }
146 
147 template <typename P>
148 inline SE3<fadbad::F<P> > make_fad_se3( int start = 0, int size = 6){
149  return SE3<fadbad::F<P> >(make_fad_so3<P>( start+3, size ), make_fad_vector(makeVector<P>(0.0, 0, 0), start, size));
150 }
151 
152 template <typename P, unsigned D>
153 inline SE3<fadbad::F<P, D> > make_fad_se3( int start = 0){
154  return SE3<fadbad::F<P, D> >(make_fad_so3<P, D>( start+3 ), make_fad_vector<D>(makeVector<P>(0.0, 0, 0), start));
155 }
156 
157 template <typename P>
158 inline SE2<fadbad::F<P> > make_fad_se2(int start = 0, int size = 3) {
159  return SE2<fadbad::F<P> >(make_fad_vector(makeVector<P>(0.0, 0, 0), start, size));
160 }
161 
162 template <typename P, unsigned D>
163 inline SE2<fadbad::F<P, D> > make_fad_se2(int start = 0) {
164  return SE2<fadbad::F<P, D> >(make_fad_vector<D>(makeVector<P>(0.0, 0, 0), start));
165 }
166 
167 template <typename P>
168 inline SO2<fadbad::F<P> > make_fad_so2(int start = 0, int size = 1) {
169  fadbad::F<P> r = 0;
170  r.diff(start,size) = 1;
171  return SO2<fadbad::F<P> >(r);
172 }
173 
174 template <typename P, unsigned D>
175 inline SO2<fadbad::F<P, D> > make_fad_so2(int start = 0) {
176  fadbad::F<P, D> r = 0;
177  r.diff(start) = 1;
178  return SO2<fadbad::F<P, D> >(r);
179 }
180 
181 template <typename P>
182 inline SO3<fadbad::F<P> > make_left_fad_so3( const SO3<P> & r, int start = 0, int size = 3 ){
183  return make_fad_so3<P>(start, size) * r;
184 }
185 
186 template <typename P, unsigned D>
187 inline SO3<fadbad::F<P, D> > make_left_fad_so3( const SO3<P> & r, int start = 0){
188  return make_fad_so3<P, D>(start) * r;
189 }
190 
191 template <typename P>
192 inline SE3<fadbad::F<P> > make_left_fad_se3( const SE3<P> & t, int start = 0, int size = 6 ){
193  return make_fad_se3<P>(start, size) * t;
194 }
195 
196 template <typename P, unsigned D>
197 inline SE3<fadbad::F<P, D> > make_left_fad_se3( const SE3<P> & t, int start = 0){
198  return make_fad_se3<P, D>(start) * t;
199 }
200 
201 template <typename P>
202 inline SO2<fadbad::F<P> > make_left_fad_so2( const SO2<P> & r, int start = 0, int size = 1 ){
203  return make_fad_so2<P>(start, size) * r;
204 }
205 
206 template <typename P, unsigned D>
207 inline SO2<fadbad::F<P, D> > make_left_fad_so2( const SO2<P> & r, int start = 0 ){
208  return make_fad_so2<P, D>(start) * r;
209 }
210 
211 template <typename P>
212 inline SE2<fadbad::F<P> > make_left_fad_se2( const SE2<P> & t, int start = 0, int size = 3 ){
213  return make_fad_se2<P>(start, size) * t;
214 }
215 
216 template <typename P, unsigned D>
217 inline SE2<fadbad::F<P, D> > make_left_fad_se2( const SE2<P> & t, int start = 0){
218  return make_fad_se2<P, D>(start) * t;
219 }
220 
221 }
222 
223 #endif // TOON_FADBAD_INTEGATION_H
224 
Represent a three-dimensional Euclidean transformation (a rotation and a translation).
Definition: se3.h:48
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
Is a number a field? i.e., +, -, *, / defined.
Definition: TooN.h:113
A matrix.
Definition: matrix.hh:105
Class to represent a two-dimensional rotation matrix.
Definition: so2.h:37
Definition: fadbad.h:39
Class to represent a three-dimensional rotation matrix.
Definition: so3.h:37
Represent a two-dimensional Euclidean transformation (a rotation and a translation).
Definition: se2.h:50
const Matrix< 3, 3, Precision > & get_matrix() const
Returns the SO3 as a Matrix<3>
Definition: so3.h:146