Visual Servoing Platform  version 3.0.0
vpQuaternionVector.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  * 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 
78  : vpRotationVector(4)
79 {
80  buildFrom(R);
81 }
82 
90  : vpRotationVector (4)
91 {
92  buildFrom(tu) ;
93 }
94 
95 
103 void vpQuaternionVector::set(const double qx, const double qy,
104  const double qz, const double qw)
105 {
106  data[0]=qx;
107  data[1]=qy;
108  data[2]=qz;
109  data[3]=qw;
110 }
121 vpQuaternionVector::buildFrom(const double qx, const double qy,
122  const double qz, const double qw)
123 {
124  set(qx, qy, qz, qw);
125  return *this;
126 }
127 
136 {
137  vpRotationMatrix R(tu) ;
138  buildFrom(R) ;
139 
140  return *this ;
141 }
150 {
151  return vpQuaternionVector(x()+q.x(), y()+q.y(), z()+q.z(), w()+q.w());
152 }
161 {
162  return vpQuaternionVector(x()-q.x(), y()-q.y(), z()-q.z(), w()-q.w());
163 }
164 
167 {
168  return vpQuaternionVector(-x(), -y(), -z(), -w());
169 }
170 
173 {
174  return vpQuaternionVector(l*x(),l*y(),l*z(),l*w());
175 }
176 
179  return vpQuaternionVector(w() * rq.x() + x() * rq.w() + y() * rq.z() - z() * rq.y(),
180  w() * rq.y() + y() * rq.w() + z() * rq.x() - x() * rq.z(),
181  w() * rq.z() + z() * rq.w() + x() * rq.y() - y() * rq.x(),
182  w() * rq.w() - x() * rq.x() - y() * rq.y() - z() * rq.z());
183 }
184 
187 {
188  if(vpMath::nul(l, std::numeric_limits<double>::epsilon())) {
189  throw vpException(vpException::fatalError, "Division by scalar l==0 !");
190  }
191 
192  return vpQuaternionVector(x()/l,y()/l,z()/l,w()/l);
193 }
194 
202 {
203  vpThetaUVector tu(R);
204  vpColVector u;
205  double theta;
206  tu.extract(theta, u);
207 
208  theta *= 0.5;
209 
210  double sinTheta_2 = sin(theta);
211  set( u[0] * sinTheta_2, u[1] * sinTheta_2, u[2] * sinTheta_2, cos(theta) );
212  return *this;
213 }
214 
221  return vpQuaternionVector( -x(), -y(), -z(), w() );
222 }
223 
230  vpQuaternionVector q_inv;
231 
232  double mag_square = w()*w() + x()*x() + y()*y() + z()*z();
233  if(!vpMath::nul(mag_square, std::numeric_limits<double>::epsilon())) {
234  q_inv = this->conjugate() / mag_square;
235  } else {
236  std::cerr << "The current quaternion is null ! The inverse cannot be computed !" << std::endl;
237  }
238 
239  return q_inv;
240 }
241 
248  return sqrt( w()*w() + x()*x() + y()*y() + z()*z() );
249 }
250 
255  double mag = magnitude();
256  if(!vpMath::nul(mag, std::numeric_limits<double>::epsilon())) {
257  set( x()/mag, y()/mag, z()/mag, w()/mag );
258  }
259 }
260 
262 double vpQuaternionVector::x() const {return data[0];}
264 double vpQuaternionVector::y() const {return data[1];}
266 double vpQuaternionVector::z() const {return data[2];}
268 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)
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:285
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
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a rotation vector as axis-angle minimal representation.