Visual Servoing Platform  version 3.0.0
vpExponentialMap.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Exponential map.
32  *
33  * Authors:
34  * Fabien Spindler
35  * Francois Chaumette
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpExponentialMap.h>
40 
41 
62 {
63  return vpExponentialMap::direct(v, 1.0);
64 }
65 
86 vpExponentialMap::direct(const vpColVector &v, const double &delta_t)
87 {
88  double theta,si,co,sinc,mcosc,msinc;
89  vpThetaUVector u ;
90  vpRotationMatrix rd ;
92 
93  vpColVector v_dt = v * delta_t;
94 
95  u[0] = v_dt[3];
96  u[1] = v_dt[4];
97  u[2] = v_dt[5];
98  rd.buildFrom(u);
99 
100  theta = sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
101  si = sin(theta);
102  co = cos(theta);
103  sinc = vpMath::sinc(si,theta);
104  mcosc = vpMath::mcosc(co,theta);
105  msinc = vpMath::msinc(si,theta);
106 
107  dt[0] = v_dt[0]*(sinc + u[0]*u[0]*msinc)
108  + v_dt[1]*(u[0]*u[1]*msinc - u[2]*mcosc)
109  + v_dt[2]*(u[0]*u[2]*msinc + u[1]*mcosc);
110 
111  dt[1] = v_dt[0]*(u[0]*u[1]*msinc + u[2]*mcosc)
112  + v_dt[1]*(sinc + u[1]*u[1]*msinc)
113  + v_dt[2]*(u[1]*u[2]*msinc - u[0]*mcosc);
114 
115  dt[2] = v_dt[0]*(u[0]*u[2]*msinc - u[1]*mcosc)
116  + v_dt[1]*(u[1]*u[2]*msinc + u[0]*mcosc)
117  + v_dt[2]*(sinc + u[2]*u[2]*msinc);
118 
119  vpHomogeneousMatrix Delta ;
120  Delta.insert(rd) ;
121  Delta.insert(dt) ;
122 
123 
124 
125  if (0) // test new version wrt old version
126  {
127  // old version
128  unsigned int i,j;
129 
130  double sinu,cosi,mcosi,s;
131  // double u[3];
132  // vpRotationMatrix rd ;
133  // vpTranslationVector dt ;
134 
135  s = sqrt(v_dt[3]*v_dt[3] + v_dt[4]*v_dt[4] + v_dt[5]*v_dt[5]);
136  if (s > 1.e-15)
137  {
138  for (i=0;i<3;i++) u[i] = v_dt[3+i]/s;
139  sinu = sin(s);
140  cosi = cos(s);
141  mcosi = 1-cosi;
142  rd[0][0] = cosi + mcosi*u[0]*u[0];
143  rd[0][1] = -sinu*u[2] + mcosi*u[0]*u[1];
144  rd[0][2] = sinu*u[1] + mcosi*u[0]*u[2];
145  rd[1][0] = sinu*u[2] + mcosi*u[1]*u[0];
146  rd[1][1] = cosi + mcosi*u[1]*u[1];
147  rd[1][2] = -sinu*u[0] + mcosi*u[1]*u[2];
148  rd[2][0] = -sinu*u[1] + mcosi*u[2]*u[0];
149  rd[2][1] = sinu*u[0] + mcosi*u[2]*u[1];
150  rd[2][2] = cosi + mcosi*u[2]*u[2];
151 
152  dt[0] = v_dt[0]*(sinu/s + u[0]*u[0]*(1-sinu/s))
153  + v_dt[1]*(u[0]*u[1]*(1-sinu/s)-u[2]*mcosi/s)
154  + v_dt[2]*(u[0]*u[2]*(1-sinu/s)+u[1]*mcosi/s);
155 
156  dt[1] = v_dt[0]*(u[0]*u[1]*(1-sinu/s)+u[2]*mcosi/s)
157  + v_dt[1]*(sinu/s + u[1]*u[1]*(1-sinu/s))
158  + v_dt[2]*(u[1]*u[2]*(1-sinu/s)-u[0]*mcosi/s);
159 
160  dt[2] = v_dt[0]*(u[0]*u[2]*(1-sinu/s)-u[1]*mcosi/s)
161  + v_dt[1]*(u[1]*u[2]*(1-sinu/s)+u[0]*mcosi/s)
162  + v_dt[2]*(sinu/s + u[2]*u[2]*(1-sinu/s));
163  }
164  else
165  {
166  for (i=0;i<3;i++)
167  {
168  for(j=0;j<3;j++) rd[i][j] = 0.0;
169  rd[i][i] = 1.0;
170  dt[i] = v_dt[i];
171  }
172  }
173  // end old version
174 
175  // Test of the new version
176  vpHomogeneousMatrix Delta_old ;
177  Delta_old.insert(rd) ;
178  Delta_old.insert(dt) ;
179 
180  int pb = 0;
181  for (i=0;i<4;i++)
182  {
183  for(j=0;j<4;j++)
184  if (fabs(Delta[i][j] - Delta_old[i][j]) > 1.e-5) pb = 1;
185  }
186  if (pb == 1)
187  {
188  printf("pb vpHomogeneousMatrix::expMap\n");
189  std::cout << " Delta : " << std::endl << Delta << std::endl;
190  std::cout << " Delta_old : " << std::endl << Delta_old << std::endl;
191  }
192  // end of the test
193  }
194 
195  return Delta ;
196 }
197 
215 {
216  return vpExponentialMap::inverse(M, 1.0);
217 }
218 
238 vpExponentialMap::inverse(const vpHomogeneousMatrix &M, const double &delta_t)
239 {
240  vpColVector v(6);
241  unsigned int i;
242  double theta,si,co,sinc,mcosc,msinc,det;
243  vpThetaUVector u ;
244  vpRotationMatrix Rd,a;
246 
247  M.extract(Rd);
248  u.buildFrom(Rd);
249  for (i=0;i<3;i++) v[3+i] = u[i];
250 
251  theta = sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
252  si = sin(theta);
253  co = cos(theta);
254  sinc = vpMath::sinc(si,theta);
255  mcosc = vpMath::mcosc(co,theta);
256  msinc = vpMath::msinc(si,theta);
257 
258  // a below is not a pure rotation matrix, even if not so far from
259  // the Rodrigues formula : sinc I + (1-sinc)/t^2 VV^T + (1-cos)/t^2 [V]_X
260  // with V = t.U
261 
262  a[0][0] = sinc + u[0]*u[0]*msinc;
263  a[0][1] = u[0]*u[1]*msinc - u[2]*mcosc;
264  a[0][2] = u[0]*u[2]*msinc + u[1]*mcosc;
265 
266  a[1][0] = u[0]*u[1]*msinc + u[2]*mcosc;
267  a[1][1] = sinc + u[1]*u[1]*msinc;
268  a[1][2] = u[1]*u[2]*msinc - u[0]*mcosc;
269 
270  a[2][0] = u[0]*u[2]*msinc - u[1]*mcosc;
271  a[2][1] = u[1]*u[2]*msinc + u[0]*mcosc;
272  a[2][2] = sinc + u[2]*u[2]*msinc;
273 
274  det = a[0][0]*a[1][1]*a[2][2] + a[1][0]*a[2][1]*a[0][2]
275  + a[0][1]*a[1][2]*a[2][0] - a[2][0]*a[1][1]*a[0][2]
276  - a[1][0]*a[0][1]*a[2][2] - a[0][0]*a[2][1]*a[1][2];
277 
278  if (fabs(det) > 1.e-5)
279  {
280  v[0] = (M[0][3]*a[1][1]*a[2][2]
281  + M[1][3]*a[2][1]*a[0][2]
282  + M[2][3]*a[0][1]*a[1][2]
283  - M[2][3]*a[1][1]*a[0][2]
284  - M[1][3]*a[0][1]*a[2][2]
285  - M[0][3]*a[2][1]*a[1][2])/det;
286  v[1] = (a[0][0]*M[1][3]*a[2][2]
287  + a[1][0]*M[2][3]*a[0][2]
288  + M[0][3]*a[1][2]*a[2][0]
289  - a[2][0]*M[1][3]*a[0][2]
290  - a[1][0]*M[0][3]*a[2][2]
291  - a[0][0]*M[2][3]*a[1][2])/det;
292  v[2] = (a[0][0]*a[1][1]*M[2][3]
293  + a[1][0]*a[2][1]*M[0][3]
294  + a[0][1]*M[1][3]*a[2][0]
295  - a[2][0]*a[1][1]*M[0][3]
296  - a[1][0]*a[0][1]*M[2][3]
297  - a[0][0]*a[2][1]*M[1][3])/det;
298  }
299  else
300  {
301  v[0] = M[0][3];
302  v[1] = M[1][3];
303  v[2] = M[2][3];
304  }
305 
306  // Apply the sampling time to the computed velocity
307  v /= delta_t;
308 
309  return(v);
310 }
311 
312 
313 /*
314  * Local variables:
315  * c-basic-offset: 2
316  * End:
317  */
static vpColVector inverse(const vpHomogeneousMatrix &M)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
static double sinc(double x)
Definition: vpMath.cpp:168
Implementation of a rotation matrix and operations on such kind of matrices.
void insert(const vpRotationMatrix &R)
static double mcosc(double cosx, double x)
Definition: vpMath.cpp:137
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
void extract(vpRotationMatrix &R) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static vpHomogeneousMatrix direct(const vpColVector &v)
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
static double msinc(double sinx, double x)
Definition: vpMath.cpp:153