Visual Servoing Platform  version 3.0.0
vpThetaUVector.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  * Theta U parameterization for the rotation.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
44 #include <cmath> // std::fabs
45 #include <limits> // numeric_limits
46 
47 #include <visp3/core/vpThetaUVector.h>
48 
49 const double vpThetaUVector::minimum = 0.0001;
50 
53  : vpRotationVector(3)
54 {}
57  : vpRotationVector(tu)
58 {}
59 
64  : vpRotationVector(3)
65 {
66  buildFrom(M) ;
67 }
72  : vpRotationVector(3)
73 {
74  buildFrom(p) ;
75 }
80  : vpRotationVector(3)
81 {
82  buildFrom(R) ;
83 }
84 
90  : vpRotationVector(3)
91 {
92  buildFrom(rzyx) ;
93 }
99  : vpRotationVector(3)
100 {
101  buildFrom(rzyz) ;
102 }
108  : vpRotationVector(3)
109 {
110  buildFrom(rxyz) ;
111 }
117  : vpRotationVector(4)
118 {
119  buildFrom(q) ;
120 }
121 
125 vpThetaUVector::vpThetaUVector(const double tux, const double tuy, const double tuz)
126  : vpRotationVector (3)
127 {
128  buildFrom(tux, tuy, tuz);
129 }
130 
136 {
138 
139  M.extract(R);
140  buildFrom(R);
141 
142  return *this ;
143 }
150 {
151  for(unsigned int i=0; i<3; i++)
152  data[i] = p[i+3];
153 
154  return *this ;
155 }
156 
162 {
163  double s,c,theta,sinc;
164 
165  s = (R[1][0]-R[0][1])*(R[1][0]-R[0][1])
166  + (R[2][0]-R[0][2])*(R[2][0]-R[0][2])
167  + (R[2][1]-R[1][2])*(R[2][1]-R[1][2]);
168  s = sqrt(s)/2.0;
169  c = (R[0][0]+R[1][1]+R[2][2]-1.0)/2.0;
170  theta=atan2(s,c); /* theta in [0, PI] since s > 0 */
171 
172  // General case when theta != pi. If theta=pi, c=-1
173  if ( (1+c) > minimum) // Since -1 <= c <= 1, no fabs(1+c) is required
174  {
175  sinc = vpMath::sinc(s,theta);
176 
177  data[0] = (R[2][1]-R[1][2])/(2*sinc);
178  data[1] = (R[0][2]-R[2][0])/(2*sinc);
179  data[2] = (R[1][0]-R[0][1])/(2*sinc);
180  }
181  else /* theta near PI */
182  {
183  if ( (R[0][0]-c) < std::numeric_limits<double>::epsilon() )
184  data[0] = 0.;
185  else
186  data[0] = theta*(sqrt((R[0][0]-c)/(1-c)));
187  if ((R[2][1]-R[1][2]) < 0) data[0] = -data[0];
188 
189  if ( (R[1][1]-c) < std::numeric_limits<double>::epsilon() )
190  data[1] = 0.;
191  else
192  data[1] = theta*(sqrt((R[1][1]-c)/(1-c)));
193 
194  if ((R[0][2]-R[2][0]) < 0) data[1] = -data[1];
195 
196  if ( (R[2][2]-c) < std::numeric_limits<double>::epsilon() )
197  data[2] = 0.;
198  else
199  data[2] = theta*(sqrt((R[2][2]-c)/(1-c)));
200 
201  if ((R[1][0]-R[0][1]) < 0) data[2] = -data[2];
202  }
203 
204  return *this ;
205 }
212 {
213  vpRotationMatrix R(rzyx) ;
214 
215  buildFrom(R) ;
216  return *this ;
217 }
224 {
225  vpRotationMatrix R(rzyz) ;
226 
227  buildFrom(R) ;
228  return *this ;
229 }
236 {
237  vpRotationMatrix R(rxyz) ;
238 
239  buildFrom(R) ;
240  return *this ;
241 }
242 
249 {
250  vpRotationMatrix R(q) ;
251 
252  buildFrom(R) ;
253  return *this ;
254 }
255 
278 {
279  for (unsigned int i=0; i< dsize; i++)
280  data[i] = v;
281 
282  return *this;
283 }
284 
296 void
297 vpThetaUVector::extract(double &theta, vpColVector &u) const
298 {
299  u.resize(3);
300 
301  theta = sqrt(data[0]*data[0] + data[1]*data[1] + data[2]*data[2]);
302  //if (theta == 0) {
303  if (std::fabs(theta) <= std::numeric_limits<double>::epsilon()) {
304  u = 0;
305  return;
306  }
307  for (unsigned int i=0 ; i < 3 ; i++)
308  u[i] = data[i] / theta ;
309 }
310 
314 void
315 vpThetaUVector::buildFrom(const double tux, const double tuy, const double tuz)
316 {
317  data[0] = tux;
318  data[1] = tuy;
319  data[2] = tuz;
320 }
Implementation of a generic rotation vector.
Implementation of an homogeneous matrix and operations on such kind of matrices.
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:152
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.
vpThetaUVector & operator=(double x)
void extract(double &theta, vpColVector &u) const
void extract(vpRotationMatrix &R) const
Implementation of a rotation vector as quaternion angle minimal representation.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:80
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyzVector.h:151
Implementation of a rotation vector as axis-angle minimal representation.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:217