Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpQuaternionVector.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  * Quaternion vector.
33  *
34  * Authors:
35  * Filip Novotny
36  *
37 *****************************************************************************/
38 
39 #include <algorithm>
40 #include <cassert>
41 #include <stdio.h>
42 #include <string.h>
43 #include <visp3/core/vpMath.h>
44 #include <visp3/core/vpQuaternionVector.h>
45 
46 // minimum value of sine
47 const double vpQuaternionVector::minimum = 0.0001;
48 
56 
59 
61 vpQuaternionVector::vpQuaternionVector(double x_, double y_, double z_, double w_) : vpRotationVector(4)
62 {
63  set(x_, y_, z_, w_);
64 }
65 
68 
70 vpQuaternionVector::vpQuaternionVector(const std::vector<double> &q) : vpRotationVector(4) { buildFrom(q); }
71 
78 
86 
94 void vpQuaternionVector::set(double qx, double qy, double qz, double qw)
95 {
96  data[0] = qx;
97  data[1] = qy;
98  data[2] = qz;
99  data[3] = qw;
100 }
110 vpQuaternionVector vpQuaternionVector::buildFrom(double qx, double qy, double qz, double qw)
111 {
112  set(qx, qy, qz, qw);
113  return *this;
114 }
115 
123 {
124  vpRotationMatrix R(tu);
125  buildFrom(R);
126 
127  return *this;
128 }
129 
134 {
135  if (q.size() != 4) {
137  "Cannot construct a quaternion vector from a %d-dimension col vector", q.size()));
138  }
139  for (unsigned int i = 0; i < 4; ++i) {
140  data[i] = q[i];
141  }
142 
143  return *this;
144 }
145 
150 {
151  if (q.size() != 4) {
153  "Cannot construct a quaternion vector from a %d-dimension std::vector", q.size()));
154  }
155  for (unsigned int i = 0; i < 4; ++i) {
156  data[i] = q[i];
157  }
158 
159  return *this;
160 }
161 
170 {
171  return vpQuaternionVector(x() + q.x(), y() + q.y(), z() + q.z(), w() + q.w());
172 }
181 {
182  return vpQuaternionVector(x() - q.x(), y() - q.y(), z() - q.z(), w() - q.w());
183 }
184 
187 
190 {
191  return vpQuaternionVector(l * x(), l * y(), l * z(), l * w());
192 }
193 
196 {
197  return vpQuaternionVector(((w() * rq.x()) + (x() * rq.w()) + (y() * rq.z())) - (z() * rq.y()),
198  ((w() * rq.y()) + (y() * rq.w()) + (z() * rq.x())) - (x() * rq.z()),
199  ((w() * rq.z()) + (z() * rq.w()) + (x() * rq.y())) - (y() * rq.x()),
200  ((w() * rq.w()) - (x() * rq.x()) - (y() * rq.y())) - (z() * rq.z()));
201 }
202 
205 {
206  if (vpMath::nul(l, std::numeric_limits<double>::epsilon())) {
207  throw vpException(vpException::fatalError, "Division by scalar l==0 !");
208  }
209 
210  return vpQuaternionVector(x() / l, y() / l, z() / l, w() / l);
211 }
237 {
238  if (q.size() != 4) {
239  throw(vpException(vpException::dimensionError, "Cannot set a quaternion vector from a %d-dimension col vector",
240  q.size()));
241  }
242  for (unsigned int i = 0; i < 4; ++i) {
243  data[i] = q[i];
244  }
245 
246  return *this;
247 }
248 
255 {
256  vpThetaUVector tu(R);
257  vpColVector u;
258  double theta;
259  tu.extract(theta, u);
260 
261  theta *= 0.5;
262 
263  double sinTheta_2 = sin(theta);
264  set(u[0] * sinTheta_2, u[1] * sinTheta_2, u[2] * sinTheta_2, cos(theta));
265  return *this;
266 }
267 
274 
281 {
282  vpQuaternionVector q_inv;
283 
284  double mag_square = (w() * w()) + (x() * x()) + (y() * y()) + (z() * z());
285  if (!vpMath::nul(mag_square, std::numeric_limits<double>::epsilon())) {
286  q_inv = this->conjugate() / mag_square;
287  }
288  else {
289  std::cerr << "The current quaternion is null ! The inverse cannot be computed !" << std::endl;
290  }
291 
292  return q_inv;
293 }
294 
300 double vpQuaternionVector::magnitude() const { return sqrt((w() * w()) + (x() * x()) + (y() * y()) + (z() * z())); }
301 
306 {
307  double mag = magnitude();
308  if (!vpMath::nul(mag, std::numeric_limits<double>::epsilon())) {
309  set(x() / mag, y() / mag, z() / mag, w() / mag);
310  }
311 }
312 
322 {
323  return (q0.x() * q1.x()) + (q0.y() * q1.y()) + (q0.z() * q1.z()) + (q0.w() * q1.w());
324 }
325 
327 const double &vpQuaternionVector::x() const { return data[0]; }
329 const double &vpQuaternionVector::y() const { return data[1]; }
331 const double &vpQuaternionVector::z() const { return data[2]; }
333 const double &vpQuaternionVector::w() const { return data[3]; }
334 
336 double &vpQuaternionVector::x() { return data[0]; }
338 double &vpQuaternionVector::y() { return data[1]; }
340 double &vpQuaternionVector::z() { return data[2]; }
342 double &vpQuaternionVector::w() { return data[3]; }
343 
344 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
362 vpQuaternionVector &vpQuaternionVector::operator=(const std::initializer_list<double> &list)
363 {
364  if (list.size() > size()) {
365  throw(vpException(
367  "Cannot set quaternion vector out of bounds. It has only %d values while you try to initialize with %d values",
368  size(), list.size()));
369  }
370  std::copy(list.begin(), list.end(), data);
371  return *this;
372 }
373 #endif
374 
390 {
391  assert(t >= 0 && t <= 1);
392 
393  double cosHalfTheta = dot(q0, q1);
394  vpQuaternionVector q1_ = q1;
395  if (cosHalfTheta < 0) {
396  cosHalfTheta = -cosHalfTheta;
397  q1_ = -q1;
398  }
399 
400  vpQuaternionVector qLerp;
401  qLerp.x() = q0.x() - (t * (q0.x() - q1.x()));
402  qLerp.y() = q0.y() - (t * (q0.y() - q1.y()));
403  qLerp.z() = q0.z() - (t * (q0.z() - q1.z()));
404  qLerp.w() = q0.w() - (t * (q0.w() - q1.w()));
405 
406  return qLerp;
407 }
408 
424 {
425  assert(t >= 0 && t <= 1);
426 
427  vpQuaternionVector qLerp = lerp(q0, q1, t);
428  qLerp.normalize();
429 
430  return qLerp;
431 }
432 
448 {
449  assert(t >= 0 && t <= 1);
450  // Some additional references:
451  // https://splines.readthedocs.io/en/latest/rotation/slerp.html
452  // https://zeux.io/2015/07/23/approximating-slerp/
453  // https://github.com/eigenteam/eigen-git-mirror/blob/36b95962756c1fce8e29b1f8bc45967f30773c00/Eigen/src/Geometry/Quaternion.h#L753-L790
454  // https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
455  // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
456  // https://www.3dgep.com/understanding-quaternions/
457  // https://blog.magnum.graphics/backstage/the-unnecessarily-short-ways-to-do-a-quaternion-slerp/
458 
459  double cosHalfTheta = dot(q0, q1);
460  vpQuaternionVector q1_ = q1;
461  if (cosHalfTheta < 0) {
462  cosHalfTheta = -cosHalfTheta;
463  q1_ = -q1;
464  }
465 
466  double scale0 = 1 - t;
467  double scale1 = t;
468 
469  if ((1 - cosHalfTheta) > 0.1) {
470  double theta = std::acos(cosHalfTheta);
471  double invSinTheta = 1 / std::sin(theta);
472 
473  scale0 = std::sin((1 - t) * theta) * invSinTheta;
474  scale1 = std::sin(t * theta) * invSinTheta;
475  }
476 
477  vpQuaternionVector qSlerp;
478  qSlerp.x() = (scale0 * q0.x()) + (scale1 * q1_.x());
479  qSlerp.y() = (scale0 * q0.y()) + (scale1 * q1_.y());
480  qSlerp.z() = (scale0 * q0.z()) + (scale1 * q1_.z());
481  qSlerp.w() = (scale0 * q0.w()) + (scale1 * q1_.w());
482  qSlerp.normalize();
483 
484  return qSlerp;
485 }
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
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
@ fatalError
Fatal error.
Definition: vpException.h:84
static bool nul(double x, double threshold=0.001)
Definition: vpMath.h:440
Implementation of a rotation vector as quaternion angle minimal representation.
vpQuaternionVector operator*(double l) const
Multiplication by scalar. Returns a quaternion defined by (lx,ly,lz,lw).
const double & z() const
Returns the z-component of the quaternion.
vpQuaternionVector conjugate() const
vpQuaternionVector inverse() const
vpQuaternionVector & operator=(const vpColVector &q)
void set(double x, double y, double z, double w)
static vpQuaternionVector slerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
static vpQuaternionVector nlerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
vpQuaternionVector operator-() const
Negate operator. Returns a quaternion defined by (-x,-y,-z-,-w).
const double & x() const
Returns the x-component of the quaternion.
static double dot(const vpQuaternionVector &q0, const vpQuaternionVector &q1)
const double & y() const
Returns the y-component of the quaternion.
const double & w() const
Returns the w-component of the quaternion.
vpQuaternionVector buildFrom(const double qx, const double qy, const double qz, const double qw)
vpQuaternionVector operator+(const vpQuaternionVector &q) const
static vpQuaternionVector lerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
vpQuaternionVector operator/(double l) const
Division by scalar. Returns a quaternion defined by (x/l,y/l,z/l,w/l).
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a generic rotation vector.
vpRowVector t() const
Implementation of a rotation vector as axis-angle minimal representation.
void extract(double &theta, vpColVector &u) const