Visual Servoing Platform  version 3.1.0
vpThetaUVector.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 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 http://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  * Theta U parameterization for the rotation.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
45 #include <cmath> // std::fabs
46 #include <limits> // numeric_limits
47 
48 #include <visp3/core/vpThetaUVector.h>
49 
50 const double vpThetaUVector::minimum = 0.0001;
51 
58 {
59  if (tu.size() != 3) {
60  throw(vpException(vpException::dimensionError, "Cannot construct a theta-u vector from a %d-dimension col vector",
61  tu.size()));
62  }
63  for (unsigned int i = 0; i < 3; i++)
64  data[i] = tu[i];
65 }
66 
79 
100 
104 vpThetaUVector::vpThetaUVector(const double tux, const double tuy, const double tuz) : vpRotationVector(3)
105 {
106  buildFrom(tux, tuy, tuz);
107 }
108 
113 {
115 
116  M.extract(R);
117  buildFrom(R);
118 
119  return *this;
120 }
126 {
127  for (unsigned int i = 0; i < 3; i++)
128  data[i] = p[i + 3];
129 
130  return *this;
131 }
132 
137 {
138  double s, c, theta;
139 
140  s = (R[1][0] - R[0][1]) * (R[1][0] - R[0][1]) + (R[2][0] - R[0][2]) * (R[2][0] - R[0][2]) +
141  (R[2][1] - R[1][2]) * (R[2][1] - R[1][2]);
142  s = sqrt(s) / 2.0;
143  c = (R[0][0] + R[1][1] + R[2][2] - 1.0) / 2.0;
144  theta = atan2(s, c); /* theta in [0, PI] since s > 0 */
145 
146  // General case when theta != pi. If theta=pi, c=-1
147  if ((1 + c) > minimum) // Since -1 <= c <= 1, no fabs(1+c) is required
148  {
149  double sinc = vpMath::sinc(s, theta);
150 
151  data[0] = (R[2][1] - R[1][2]) / (2 * sinc);
152  data[1] = (R[0][2] - R[2][0]) / (2 * sinc);
153  data[2] = (R[1][0] - R[0][1]) / (2 * sinc);
154  } else /* theta near PI */
155  {
156  if ((R[0][0] - c) < std::numeric_limits<double>::epsilon())
157  data[0] = 0.;
158  else
159  data[0] = theta * (sqrt((R[0][0] - c) / (1 - c)));
160  if ((R[2][1] - R[1][2]) < 0)
161  data[0] = -data[0];
162 
163  if ((R[1][1] - c) < std::numeric_limits<double>::epsilon())
164  data[1] = 0.;
165  else
166  data[1] = theta * (sqrt((R[1][1] - c) / (1 - c)));
167 
168  if ((R[0][2] - R[2][0]) < 0)
169  data[1] = -data[1];
170 
171  if ((R[2][2] - c) < std::numeric_limits<double>::epsilon())
172  data[2] = 0.;
173  else
174  data[2] = theta * (sqrt((R[2][2] - c) / (1 - c)));
175 
176  if ((R[1][0] - R[0][1]) < 0)
177  data[2] = -data[2];
178  }
179 
180  return *this;
181 }
187 {
188  vpRotationMatrix R(rzyx);
189 
190  buildFrom(R);
191  return *this;
192 }
198 {
199  vpRotationMatrix R(rzyz);
200 
201  buildFrom(R);
202  return *this;
203 }
209 {
210  vpRotationMatrix R(rxyz);
211 
212  buildFrom(R);
213  return *this;
214 }
215 
221 {
222  vpRotationMatrix R(q);
223 
224  buildFrom(R);
225  return *this;
226 }
227 
250 {
251  for (unsigned int i = 0; i < dsize; i++)
252  data[i] = v;
253 
254  return *this;
255 }
256 
281 {
282  if (tu.size() != 3) {
283  throw(vpException(vpException::dimensionError, "Cannot set a theta-u vector from a %d-dimension col vector",
284  tu.size()));
285  }
286  for (unsigned int i = 0; i < 3; i++)
287  data[i] = tu[i];
288 
289  return *this;
290 }
291 
320 void vpThetaUVector::extract(double &theta, vpColVector &u) const
321 {
322  u.resize(3);
323 
324  theta = getTheta();
325  // if (theta == 0) {
326  if (std::fabs(theta) <= std::numeric_limits<double>::epsilon()) {
327  u = 0;
328  return;
329  }
330  for (unsigned int i = 0; i < 3; i++)
331  u[i] = data[i] / theta;
332 }
333 
356 double vpThetaUVector::getTheta() const { return sqrt(data[0] * data[0] + data[1] * data[1] + data[2] * data[2]); }
357 
382 {
383  vpColVector u(3);
384 
385  double theta = getTheta();
386  // if (theta == 0) {
387  if (std::fabs(theta) <= std::numeric_limits<double>::epsilon()) {
388  u = 0;
389  return u;
390  }
391  for (unsigned int i = 0; i < 3; i++)
392  u[i] = data[i] / theta;
393  return u;
394 }
395 
399 void vpThetaUVector::buildFrom(const double tux, const double tuy, const double tuz)
400 {
401  data[0] = tux;
402  data[1] = tuy;
403  data[2] = tuz;
404 }
vpThetaUVector & operator=(const vpColVector &tu)
Implementation of a generic rotation vector.
vpColVector getU() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(double &theta, vpColVector &u) const
error that can be emited by ViSP classes.
Definition: vpException.h:71
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:158
void extract(vpRotationMatrix &R) const
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
static double sinc(double x)
Definition: vpMath.cpp:170
Implementation of a rotation matrix and operations on such kind of matrices.
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:92
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:156
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:154
Implementation of a rotation vector as axis-angle minimal representation.
double getTheta() const
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241