Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpQuaternionVector.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  * Quaternion vector.
32  *
33  * Authors:
34  * Filip Novotny
35  *
36  *****************************************************************************/
37 
38 
39 #include <visp3/core/vpQuaternionVector.h>
40 #include <visp3/core/vpMath.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <algorithm>
44 
45 
46 // minimum value of sine
47 const double vpQuaternionVector::minimum = 0.0001;
48 
56  : vpRotationVector(4)
57 {}
58 
61  : vpRotationVector(q)
62 {}
63 
65 vpQuaternionVector::vpQuaternionVector(const double x_, const double y_,
66  const double z_,const double w_)
67  : vpRotationVector(4)
68 {
69  set(x_, y_, z_, w_);
70 }
71 
74  : vpRotationVector(4)
75 {
76  if (q.size() != 4) {
77  throw(vpException(vpException::dimensionError, "Cannot construct a quaternion vector from a %d-dimension col vector", q.size()));
78  }
79  for (unsigned int i=0; i<4; i++)
80  data[i] = q[i];
81 }
82 
89  : vpRotationVector(4)
90 {
91  buildFrom(R);
92 }
93 
101  : vpRotationVector (4)
102 {
103  buildFrom(tu) ;
104 }
105 
106 
114 void vpQuaternionVector::set(const double qx, const double qy,
115  const double qz, const double qw)
116 {
117  data[0]=qx;
118  data[1]=qy;
119  data[2]=qz;
120  data[3]=qw;
121 }
132 vpQuaternionVector::buildFrom(const double qx, const double qy,
133  const double qz, const double qw)
134 {
135  set(qx, qy, qz, qw);
136  return *this;
137 }
138 
147 {
148  vpRotationMatrix R(tu) ;
149  buildFrom(R) ;
150 
151  return *this ;
152 }
161 {
162  return vpQuaternionVector(x()+q.x(), y()+q.y(), z()+q.z(), w()+q.w());
163 }
172 {
173  return vpQuaternionVector(x()-q.x(), y()-q.y(), z()-q.z(), w()-q.w());
174 }
175 
178 {
179  return vpQuaternionVector(-x(), -y(), -z(), -w());
180 }
181 
184 {
185  return vpQuaternionVector(l*x(),l*y(),l*z(),l*w());
186 }
187 
190  return vpQuaternionVector(w() * rq.x() + x() * rq.w() + y() * rq.z() - z() * rq.y(),
191  w() * rq.y() + y() * rq.w() + z() * rq.x() - x() * rq.z(),
192  w() * rq.z() + z() * rq.w() + x() * rq.y() - y() * rq.x(),
193  w() * rq.w() - x() * rq.x() - y() * rq.y() - z() * rq.z());
194 }
195 
198 {
199  if(vpMath::nul(l, std::numeric_limits<double>::epsilon())) {
200  throw vpException(vpException::fatalError, "Division by scalar l==0 !");
201  }
202 
203  return vpQuaternionVector(x()/l,y()/l,z()/l,w()/l);
204 }
228 {
229  if (q.size() != 4) {
230  throw(vpException(vpException::dimensionError, "Cannot set a quaternion vector from a %d-dimension col vector", q.size()));
231  }
232  for (unsigned int i=0; i< 4; i++)
233  data[i] = q[i];
234 
235  return *this;
236 }
237 
245 {
246  vpThetaUVector tu(R);
247  vpColVector u;
248  double theta;
249  tu.extract(theta, u);
250 
251  theta *= 0.5;
252 
253  double sinTheta_2 = sin(theta);
254  set( u[0] * sinTheta_2, u[1] * sinTheta_2, u[2] * sinTheta_2, cos(theta) );
255  return *this;
256 }
257 
264  return vpQuaternionVector( -x(), -y(), -z(), w() );
265 }
266 
273  vpQuaternionVector q_inv;
274 
275  double mag_square = w()*w() + x()*x() + y()*y() + z()*z();
276  if(!vpMath::nul(mag_square, std::numeric_limits<double>::epsilon())) {
277  q_inv = this->conjugate() / mag_square;
278  } else {
279  std::cerr << "The current quaternion is null ! The inverse cannot be computed !" << std::endl;
280  }
281 
282  return q_inv;
283 }
284 
291  return sqrt( w()*w() + x()*x() + y()*y() + z()*z() );
292 }
293 
298  double mag = magnitude();
299  if(!vpMath::nul(mag, std::numeric_limits<double>::epsilon())) {
300  set( x()/mag, y()/mag, z()/mag, w()/mag );
301  }
302 }
303 
305 double vpQuaternionVector::x() const {return data[0];}
307 double vpQuaternionVector::y() const {return data[1];}
309 double vpQuaternionVector::z() const {return data[2];}
311 double vpQuaternionVector::w() const {return data[3];}
vpQuaternionVector operator+(const vpQuaternionVector &q) const
vpQuaternionVector operator*(const double l) const
Multiplication by scalar. Returns a quaternion defined by (lx,ly,lz,lw).
Implementation of a generic rotation vector.
void set(const double x, const double y, const double z, const double w)
vpQuaternionVector operator/(const double l) const
Division by scalar. Returns a quaternion defined by (x/l,y/l,z/l,w/l).
vpQuaternionVector inverse() const
error that can be emited by ViSP classes.
Definition: vpException.h:73
double y() const
Returns y-component of the quaternion.
vpQuaternionVector operator-() const
Negate operator. Returns a quaternion defined by (-x,-y,-z-,-w).
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
vpQuaternionVector buildFrom(const double qx, const double qy, const double qz, const double qw)
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
vpQuaternionVector conjugate() const
Implementation of a rotation matrix and operations on such kind of matrices.
double w() const
Returns w-component of the quaternion.
static bool nul(double x, double s=0.001)
Definition: vpMath.h:294
void extract(double &theta, vpColVector &u) const
double z() const
Returns z-component of the quaternion.
double x() const
Returns x-component of the quaternion.
Implementation of a rotation vector as quaternion angle minimal representation.
double magnitude() const
vpQuaternionVector & operator=(const vpColVector &q)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a rotation vector as axis-angle minimal representation.