Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpExponentialMap.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
60 {
61  return vpExponentialMap::direct(v, 1.0);
62 }
63 
82 vpExponentialMap::direct(const vpColVector &v, const double &delta_t)
83 {
84  double theta,si,co,sinc,mcosc,msinc;
85  vpThetaUVector u ;
86  vpRotationMatrix rd ;
88 
89  vpColVector v_dt = v * delta_t;
90 
91  u[0] = v_dt[3];
92  u[1] = v_dt[4];
93  u[2] = v_dt[5];
94  rd.buildFrom(u);
95 
96  theta = sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
97  si = sin(theta);
98  co = cos(theta);
99  sinc = vpMath::sinc(si,theta);
100  mcosc = vpMath::mcosc(co,theta);
101  msinc = vpMath::msinc(si,theta);
102 
103  dt[0] = v_dt[0]*(sinc + u[0]*u[0]*msinc)
104  + v_dt[1]*(u[0]*u[1]*msinc - u[2]*mcosc)
105  + v_dt[2]*(u[0]*u[2]*msinc + u[1]*mcosc);
106 
107  dt[1] = v_dt[0]*(u[0]*u[1]*msinc + u[2]*mcosc)
108  + v_dt[1]*(sinc + u[1]*u[1]*msinc)
109  + v_dt[2]*(u[1]*u[2]*msinc - u[0]*mcosc);
110 
111  dt[2] = v_dt[0]*(u[0]*u[2]*msinc - u[1]*mcosc)
112  + v_dt[1]*(u[1]*u[2]*msinc + u[0]*mcosc)
113  + v_dt[2]*(sinc + u[2]*u[2]*msinc);
114 
115  vpHomogeneousMatrix Delta ;
116  Delta.insert(rd) ;
117  Delta.insert(dt) ;
118 
119  if (0) // test new version wrt old version
120  {
121  // old version
122  unsigned int i,j;
123 
124  double s;
125  // double u[3];
126  // vpRotationMatrix rd ;
127  // vpTranslationVector dt ;
128 
129  s = sqrt(v_dt[3]*v_dt[3] + v_dt[4]*v_dt[4] + v_dt[5]*v_dt[5]);
130  if (s > 1.e-15)
131  {
132  for (i=0;i<3;i++) u[i] = v_dt[3+i]/s;
133  double sinu = sin(s);
134  double cosi = cos(s);
135  double mcosi = 1-cosi;
136  rd[0][0] = cosi + mcosi*u[0]*u[0];
137  rd[0][1] = -sinu*u[2] + mcosi*u[0]*u[1];
138  rd[0][2] = sinu*u[1] + mcosi*u[0]*u[2];
139  rd[1][0] = sinu*u[2] + mcosi*u[1]*u[0];
140  rd[1][1] = cosi + mcosi*u[1]*u[1];
141  rd[1][2] = -sinu*u[0] + mcosi*u[1]*u[2];
142  rd[2][0] = -sinu*u[1] + mcosi*u[2]*u[0];
143  rd[2][1] = sinu*u[0] + mcosi*u[2]*u[1];
144  rd[2][2] = cosi + mcosi*u[2]*u[2];
145 
146  dt[0] = v_dt[0]*(sinu/s + u[0]*u[0]*(1-sinu/s))
147  + v_dt[1]*(u[0]*u[1]*(1-sinu/s)-u[2]*mcosi/s)
148  + v_dt[2]*(u[0]*u[2]*(1-sinu/s)+u[1]*mcosi/s);
149 
150  dt[1] = v_dt[0]*(u[0]*u[1]*(1-sinu/s)+u[2]*mcosi/s)
151  + v_dt[1]*(sinu/s + u[1]*u[1]*(1-sinu/s))
152  + v_dt[2]*(u[1]*u[2]*(1-sinu/s)-u[0]*mcosi/s);
153 
154  dt[2] = v_dt[0]*(u[0]*u[2]*(1-sinu/s)-u[1]*mcosi/s)
155  + v_dt[1]*(u[1]*u[2]*(1-sinu/s)+u[0]*mcosi/s)
156  + v_dt[2]*(sinu/s + u[2]*u[2]*(1-sinu/s));
157  }
158  else
159  {
160  for (i=0;i<3;i++)
161  {
162  for(j=0;j<3;j++) rd[i][j] = 0.0;
163  rd[i][i] = 1.0;
164  dt[i] = v_dt[i];
165  }
166  }
167  // end old version
168 
169  // Test of the new version
170  vpHomogeneousMatrix Delta_old ;
171  Delta_old.insert(rd) ;
172  Delta_old.insert(dt) ;
173 
174  int pb = 0;
175  for (i=0;i<4;i++)
176  {
177  for(j=0;j<4;j++)
178  if (fabs(Delta[i][j] - Delta_old[i][j]) > 1.e-5) pb = 1;
179  }
180  if (pb == 1)
181  {
182  printf("pb vpHomogeneousMatrix::expMap\n");
183  std::cout << " Delta : " << std::endl << Delta << std::endl;
184  std::cout << " Delta_old : " << std::endl << Delta_old << std::endl;
185  }
186  // end of the test
187  }
188 
189  return Delta ;
190 }
191 
208 {
209  return vpExponentialMap::inverse(M, 1.0);
210 }
211 
230 vpExponentialMap::inverse(const vpHomogeneousMatrix &M, const double &delta_t)
231 {
232  vpColVector v(6);
233  unsigned int i;
234  double theta,si,co,sinc,mcosc,msinc,det;
235  vpThetaUVector u ;
236  vpRotationMatrix Rd,a;
238 
239  M.extract(Rd);
240  u.buildFrom(Rd);
241  for (i=0;i<3;i++) v[3+i] = u[i];
242 
243  theta = sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
244  si = sin(theta);
245  co = cos(theta);
246  sinc = vpMath::sinc(si,theta);
247  mcosc = vpMath::mcosc(co,theta);
248  msinc = vpMath::msinc(si,theta);
249 
250  // a below is not a pure rotation matrix, even if not so far from
251  // the Rodrigues formula : sinc I + (1-sinc)/t^2 VV^T + (1-cos)/t^2 [V]_X
252  // with V = t.U
253 
254  a[0][0] = sinc + u[0]*u[0]*msinc;
255  a[0][1] = u[0]*u[1]*msinc - u[2]*mcosc;
256  a[0][2] = u[0]*u[2]*msinc + u[1]*mcosc;
257 
258  a[1][0] = u[0]*u[1]*msinc + u[2]*mcosc;
259  a[1][1] = sinc + u[1]*u[1]*msinc;
260  a[1][2] = u[1]*u[2]*msinc - u[0]*mcosc;
261 
262  a[2][0] = u[0]*u[2]*msinc - u[1]*mcosc;
263  a[2][1] = u[1]*u[2]*msinc + u[0]*mcosc;
264  a[2][2] = sinc + u[2]*u[2]*msinc;
265 
266  det = a[0][0]*a[1][1]*a[2][2] + a[1][0]*a[2][1]*a[0][2]
267  + a[0][1]*a[1][2]*a[2][0] - a[2][0]*a[1][1]*a[0][2]
268  - a[1][0]*a[0][1]*a[2][2] - a[0][0]*a[2][1]*a[1][2];
269 
270  if (fabs(det) > 1.e-5)
271  {
272  v[0] = (M[0][3]*a[1][1]*a[2][2]
273  + M[1][3]*a[2][1]*a[0][2]
274  + M[2][3]*a[0][1]*a[1][2]
275  - M[2][3]*a[1][1]*a[0][2]
276  - M[1][3]*a[0][1]*a[2][2]
277  - M[0][3]*a[2][1]*a[1][2])/det;
278  v[1] = (a[0][0]*M[1][3]*a[2][2]
279  + a[1][0]*M[2][3]*a[0][2]
280  + M[0][3]*a[1][2]*a[2][0]
281  - a[2][0]*M[1][3]*a[0][2]
282  - a[1][0]*M[0][3]*a[2][2]
283  - a[0][0]*M[2][3]*a[1][2])/det;
284  v[2] = (a[0][0]*a[1][1]*M[2][3]
285  + a[1][0]*a[2][1]*M[0][3]
286  + a[0][1]*M[1][3]*a[2][0]
287  - a[2][0]*a[1][1]*M[0][3]
288  - a[1][0]*a[0][1]*M[2][3]
289  - a[0][0]*a[2][1]*M[1][3])/det;
290  }
291  else
292  {
293  v[0] = M[0][3];
294  v[1] = M[1][3];
295  v[2] = M[2][3];
296  }
297 
298  // Apply the sampling time to the computed velocity
299  v /= delta_t;
300 
301  return(v);
302 }
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:169
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:138
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:154