Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpExponentialMap.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Exponential map.
33  *
34  * Authors:
35  * Francois Chaumette
36  *
37 *****************************************************************************/
38 
39 #include <visp3/core/vpExponentialMap.h>
40 
59 
79 {
80  if (v.size() != 6) {
82  "Cannot compute direct exponential map from a %d-dim velocity vector. Should be 6-dim.",
83  v.size()));
84  }
85  double theta, si, co, sinc, mcosc, msinc;
89 
90  vpColVector v_dt = v * delta_t;
91 
92  u[0] = v_dt[3];
93  u[1] = v_dt[4];
94  u[2] = v_dt[5];
95  rd.buildFrom(u);
96 
97  theta = sqrt((u[0] * u[0]) + (u[1] * u[1]) + (u[2] * u[2]));
98  si = sin(theta);
99  co = cos(theta);
100  sinc = vpMath::sinc(si, theta);
101  mcosc = vpMath::mcosc(co, theta);
102  msinc = vpMath::msinc(si, theta);
103 
104  dt[0] = ((v_dt[0] * (sinc + (u[0] * u[0] * msinc))) +
105  (v_dt[1] * ((u[0] * u[1] * msinc) - (u[2] * mcosc)))) +
106  (v_dt[2] * ((u[0] * u[2] * msinc) + (u[1] * mcosc)));
107 
108  dt[1] = ((v_dt[0] * ((u[0] * u[1] * msinc) + (u[2] * mcosc))) +
109  (v_dt[1] * (sinc + (u[1] * u[1] * msinc)))) +
110  (v_dt[2] * ((u[1] * u[2] * msinc) - (u[0] * mcosc)));
111 
112  dt[2] = ((v_dt[0] * ((u[0] * u[2] * msinc) - (u[1] * mcosc))) +
113  (v_dt[1] * ((u[1] * u[2] * msinc) + (u[0] * mcosc)))) +
114  (v_dt[2] * (sinc + (u[2] * u[2] * msinc)));
115 
116  vpHomogeneousMatrix Delta;
117  Delta.insert(rd);
118  Delta.insert(dt);
119 
120  if (0) // test new version wrt old version
121  {
122  // old version
123  unsigned int i, j;
124 
125  double s;
126 
127  s = sqrt((v_dt[3] * v_dt[3]) + (v_dt[4] * v_dt[4]) + (v_dt[5] * v_dt[5]));
128  if (s > 1.e-15) {
129  for (i = 0; i < 3; ++i) {
130  u[i] = v_dt[3 + i] / s;
131  }
132  double sinu = sin(s);
133  double cosi = cos(s);
134  double mcosi = 1 - cosi;
135  rd[0][0] = cosi + (mcosi * u[0] * u[0]);
136  rd[0][1] = (-sinu * u[2]) + (mcosi * u[0] * u[1]);
137  rd[0][2] = (sinu * u[1]) + (mcosi * u[0] * u[2]);
138  rd[1][0] = (sinu * u[2]) + (mcosi * u[1] * u[0]);
139  rd[1][1] = cosi + (mcosi * u[1] * u[1]);
140  rd[1][2] = (-sinu * u[0]) + (mcosi * u[1] * u[2]);
141  rd[2][0] = (-sinu * u[1]) + (mcosi * u[2] * u[0]);
142  rd[2][1] = (sinu * u[0]) + (mcosi * u[2] * u[1]);
143  rd[2][2] = cosi + (mcosi * u[2] * u[2]);
144 
145  dt[0] = (v_dt[0] * ((sinu / s) + (u[0] * u[0] * (1 - (sinu / s))))) +
146  (v_dt[1] * ((u[0] * u[1] * (1 - (sinu / s))) - ((u[2] * mcosi) / s))) +
147  (v_dt[2] * ((u[0] * u[2] * (1 - (sinu / s))) + ((u[1] * mcosi) / s)));
148 
149  dt[1] = (v_dt[0] * ((u[0] * u[1] * (1 - (sinu / s))) + ((u[2] * mcosi) / s))) +
150  (v_dt[1] * ((sinu / s) + (u[1] * u[1] * (1 - (sinu / s))))) +
151  (v_dt[2] * ((u[1] * u[2] * (1 - (sinu / s))) - ((u[0] * mcosi) / s)));
152 
153  dt[2] = (v_dt[0] * ((u[0] * u[2] * (1 - (sinu / s))) - ((u[1] * mcosi) / s))) +
154  (v_dt[1] * ((u[1] * u[2] * (1 - (sinu / s))) + ((u[0] * mcosi) / s))) +
155  (v_dt[2] * ((sinu / s) + (u[2] * u[2] * (1 - (sinu / s)))));
156  }
157  else {
158  for (i = 0; i < 3; ++i) {
159  for (j = 0; j < 3; ++j) {
160  rd[i][j] = 0.0;
161  }
162  rd[i][i] = 1.0;
163  dt[i] = v_dt[i];
164  }
165  }
166  // end old version
167 
168  // Test of the new version
169  vpHomogeneousMatrix Delta_old;
170  Delta_old.insert(rd);
171  Delta_old.insert(dt);
172 
173  int pb = 0;
174  for (i = 0; i < 4; ++i) {
175  for (j = 0; j < 4; ++j) {
176  if (fabs(Delta[i][j] - Delta_old[i][j]) > 1.e-5) {
177  pb = 1;
178  }
179  }
180  }
181  if (pb == 1) {
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 
207 
226 {
227  vpColVector v(6);
228  unsigned int i;
229  double theta, si, co, sinc, mcosc, msinc, det;
230  vpThetaUVector u;
231  vpRotationMatrix Rd, a;
233 
234  M.extract(Rd);
235  u.buildFrom(Rd);
236  for (i = 0; i < 3; ++i) {
237  v[3 + i] = u[i];
238  }
239 
240  theta = sqrt((u[0] * u[0]) + (u[1] * u[1]) + (u[2] * u[2]));
241  si = sin(theta);
242  co = cos(theta);
243  sinc = vpMath::sinc(si, theta);
244  mcosc = vpMath::mcosc(co, theta);
245  msinc = vpMath::msinc(si, theta);
246 
247  // a below is not a pure rotation matrix, even if not so far from
248  // the Rodrigues formula : sinc I + (1-sinc)/t^2 VV^T + (1-cos)/t^2 [V]_X
249  // with V = t.U
250 
251  a[0][0] = sinc + (u[0] * u[0] * msinc);
252  a[0][1] = (u[0] * u[1] * msinc) - (u[2] * mcosc);
253  a[0][2] = (u[0] * u[2] * msinc) + (u[1] * mcosc);
254 
255  a[1][0] = (u[0] * u[1] * msinc) + (u[2] * mcosc);
256  a[1][1] = sinc + (u[1] * u[1] * msinc);
257  a[1][2] = (u[1] * u[2] * msinc) - (u[0] * mcosc);
258 
259  a[2][0] = (u[0] * u[2] * msinc) - (u[1] * mcosc);
260  a[2][1] = (u[1] * u[2] * msinc) + (u[0] * mcosc);
261  a[2][2] = sinc + (u[2] * u[2] * msinc);
262 
263  det = (((((a[0][0] * a[1][1] * a[2][2]) + (a[1][0] * a[2][1] * a[0][2])) + (a[0][1] * a[1][2] * a[2][0])) -
264  (a[2][0] * a[1][1] * a[0][2])) - (a[1][0] * a[0][1] * a[2][2])) - (a[0][0] * a[2][1] * a[1][2]);
265 
266  if (fabs(det) > 1.e-5) {
267  v[0] = ((((((M[0][3] * a[1][1] * a[2][2]) + (M[1][3] * a[2][1] * a[0][2])) + (M[2][3] * a[0][1] * a[1][2])) -
268  (M[2][3] * a[1][1] * a[0][2])) - (M[1][3] * a[0][1] * a[2][2])) - (M[0][3] * a[2][1] * a[1][2])) /
269  det;
270  v[1] = ((((((a[0][0] * M[1][3] * a[2][2]) + (a[1][0] * M[2][3] * a[0][2])) + (M[0][3] * a[1][2] * a[2][0])) -
271  (a[2][0] * M[1][3] * a[0][2])) - (a[1][0] * M[0][3] * a[2][2])) - (a[0][0] * M[2][3] * a[1][2])) /
272  det;
273  v[2] = ((((((a[0][0] * a[1][1] * M[2][3]) + (a[1][0] * a[2][1] * M[0][3])) + (a[0][1] * M[1][3] * a[2][0])) -
274  (a[2][0] * a[1][1] * M[0][3])) - (a[1][0] * a[0][1] * M[2][3])) - (a[0][0] * a[2][1] * M[1][3])) /
275  det;
276  }
277  else {
278  v[0] = M[0][3];
279  v[1] = M[1][3];
280  v[2] = M[2][3];
281  }
282 
283  // Apply the sampling time to the computed velocity
284  v /= delta_t;
285 
286  return v;
287 }
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
static vpHomogeneousMatrix direct(const vpColVector &v)
static vpColVector inverse(const vpHomogeneousMatrix &M)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
void insert(const vpRotationMatrix &R)
static double msinc(double sinx, double x)
Definition: vpMath.cpp:252
static double sinc(double x)
Definition: vpMath.cpp:269
static double mcosc(double cosx, double x)
Definition: vpMath.cpp:234
Implementation of a rotation matrix and operations on such kind of matrices.
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
Implementation of a rotation vector as axis-angle minimal representation.
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
Class that consider the case of a translation vector.