Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpQuaternionVector.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
34 #include <algorithm>
35 #include <cassert>
36 #include <stdio.h>
37 #include <string.h>
38 #include <visp3/core/vpMath.h>
39 #include <visp3/core/vpQuaternionVector.h>
40 
41 BEGIN_VISP_NAMESPACE
42 // minimum value of sine
43 const double vpQuaternionVector::minimum = 0.0001;
44 
52 
55 
57 vpQuaternionVector::vpQuaternionVector(double x_, double y_, double z_, double w_) : vpRotationVector(4)
58 {
59  set(x_, y_, z_, w_);
60 }
61 
64 
66 vpQuaternionVector::vpQuaternionVector(const std::vector<double> &q) : vpRotationVector(4) { buildFrom(q); }
67 
74 
82 
90 void vpQuaternionVector::set(double qx, double qy, double qz, double qw)
91 {
92  const unsigned int index_0 = 0;
93  const unsigned int index_1 = 1;
94  const unsigned int index_2 = 2;
95  const unsigned int index_3 = 3;
96  data[index_0] = qx;
97  data[index_1] = qy;
98  data[index_2] = qz;
99  data[index_3] = qw;
100 }
101 
111 vpQuaternionVector &vpQuaternionVector::buildFrom(const double &qx, const double &qy, const double &qz, const double &qw)
112 {
113  set(qx, qy, qz, qw);
114  return *this;
115 }
116 
124 {
125  vpRotationMatrix R(tu);
126  buildFrom(R);
127 
128  return *this;
129 }
130 
135 {
136  if (q.size() != 4) {
138  "Cannot construct a quaternion vector from a %d-dimension col vector", q.size()));
139  }
140  const unsigned int val_4 = 4;
141  for (unsigned int i = 0; i < val_4; ++i) {
142  data[i] = q[i];
143  }
144 
145  return *this;
146 }
147 
151 vpQuaternionVector &vpQuaternionVector::buildFrom(const std::vector<double> &q)
152 {
153  if (q.size() != 4) {
155  "Cannot construct a quaternion vector from a %d-dimension std::vector", q.size()));
156  }
157 
158  const unsigned int val_4 = 4;
159  for (unsigned int i = 0; i < val_4; ++i) {
160  data[i] = q[i];
161  }
162 
163  return *this;
164 }
165 
172 {
173  vpThetaUVector tu(R);
174  vpColVector u;
175  double theta;
176  tu.extract(theta, u);
177 
178  theta *= 0.5;
179 
180  double sinTheta_2 = sin(theta);
181  const unsigned int index_0 = 0;
182  const unsigned int index_1 = 1;
183  const unsigned int index_2 = 2;
184  set(u[index_0] * sinTheta_2, u[index_1] * sinTheta_2, u[index_2] * sinTheta_2, cos(theta));
185  return *this;
186 }
187 
196 {
197  return vpQuaternionVector(x() + q.x(), y() + q.y(), z() + q.z(), w() + q.w());
198 }
207 {
208  return vpQuaternionVector(x() - q.x(), y() - q.y(), z() - q.z(), w() - q.w());
209 }
210 
213 {
214  return vpQuaternionVector(-x(), -y(), -z(), -w());
215 }
216 
219 {
220  return vpQuaternionVector(l * x(), l * y(), l * z(), l * w());
221 }
222 
225 {
226  return vpQuaternionVector(((w() * rq.x()) + (x() * rq.w()) + (y() * rq.z())) - (z() * rq.y()),
227  ((w() * rq.y()) + (y() * rq.w()) + (z() * rq.x())) - (x() * rq.z()),
228  ((w() * rq.z()) + (z() * rq.w()) + (x() * rq.y())) - (y() * rq.x()),
229  ((w() * rq.w()) - (x() * rq.x()) - (y() * rq.y())) - (z() * rq.z()));
230 }
231 
234 {
235  if (vpMath::nul(l, std::numeric_limits<double>::epsilon())) {
236  throw vpException(vpException::fatalError, "Division by scalar l==0 !");
237  }
238 
239  return vpQuaternionVector(x() / l, y() / l, z() / l, w() / l);
240 }
268 {
269  if (q.size() != 4) {
270  throw(vpException(vpException::dimensionError, "Cannot set a quaternion vector from a %d-dimension col vector",
271  q.size()));
272  }
273  const unsigned int val_4 = 4;
274  for (unsigned int i = 0; i < val_4; ++i) {
275  data[i] = q[i];
276  }
277 
278  return *this;
279 }
280 
281 
282 
289 
296 {
297  vpQuaternionVector q_inv;
298 
299  double mag_square = (w() * w()) + (x() * x()) + (y() * y()) + (z() * z());
300  if (!vpMath::nul(mag_square, std::numeric_limits<double>::epsilon())) {
301  q_inv = this->conjugate() / mag_square;
302  }
303  else {
304  std::cerr << "The current quaternion is null ! The inverse cannot be computed !" << std::endl;
305  }
306 
307  return q_inv;
308 }
309 
315 double vpQuaternionVector::magnitude() const { return sqrt((w() * w()) + (x() * x()) + (y() * y()) + (z() * z())); }
316 
321 {
322  double mag = magnitude();
323  if (!vpMath::nul(mag, std::numeric_limits<double>::epsilon())) {
324  set(x() / mag, y() / mag, z() / mag, w() / mag);
325  }
326 }
327 
337 {
338  return (q0.x() * q1.x()) + (q0.y() * q1.y()) + (q0.z() * q1.z()) + (q0.w() * q1.w());
339 }
340 
342 const double &vpQuaternionVector::x() const { const unsigned int index_0 = 0; return data[index_0]; }
344 const double &vpQuaternionVector::y() const { const unsigned int index_1 = 1; return data[index_1]; }
346 const double &vpQuaternionVector::z() const { const unsigned int index_2 = 2; return data[index_2]; }
348 const double &vpQuaternionVector::w() const { const unsigned int index_3 = 3; return data[index_3]; }
349 
351 double &vpQuaternionVector::x() { const unsigned int index_0 = 0; return data[index_0]; }
353 double &vpQuaternionVector::y() { const unsigned int index_1 = 1; return data[index_1]; }
355 double &vpQuaternionVector::z() { const unsigned int index_2 = 2; return data[index_2]; }
357 double &vpQuaternionVector::w() { const unsigned int index_3 = 3; return data[index_3]; }
358 
359 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
381 vpQuaternionVector &vpQuaternionVector::operator=(const std::initializer_list<double> &list)
382 {
383  if (list.size() > size()) {
384  throw(vpException(
386  "Cannot set quaternion vector out of bounds. It has only %d values while you try to initialize with %d values",
387  size(), list.size()));
388  }
389  std::copy(list.begin(), list.end(), data);
390  return *this;
391 }
392 #endif
393 
409 {
410  assert(t >= 0 && t <= 1);
411 
412  double cosHalfTheta = dot(q0, q1);
413  vpQuaternionVector q1_ = q1;
414  if (cosHalfTheta < 0) {
415  cosHalfTheta = -cosHalfTheta;
416  q1_ = -q1;
417  }
418 
419  vpQuaternionVector qLerp;
420  qLerp.x() = q0.x() - (t * (q0.x() - q1.x()));
421  qLerp.y() = q0.y() - (t * (q0.y() - q1.y()));
422  qLerp.z() = q0.z() - (t * (q0.z() - q1.z()));
423  qLerp.w() = q0.w() - (t * (q0.w() - q1.w()));
424 
425  return qLerp;
426 }
427 
443 {
444  assert(t >= 0 && t <= 1);
445 
446  vpQuaternionVector qLerp = lerp(q0, q1, t);
447  qLerp.normalize();
448 
449  return qLerp;
450 }
451 
467 {
468  assert(t >= 0 && t <= 1);
469  // Some additional references:
470  // https://splines.readthedocs.io/en/latest/rotation/slerp.html
471  // https://zeux.io/2015/07/23/approximating-slerp/
472  // https://github.com/eigenteam/eigen-git-mirror/blob/36b95962756c1fce8e29b1f8bc45967f30773c00/Eigen/src/Geometry/Quaternion.h#L753-L790
473  // https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
474  // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
475  // https://www.3dgep.com/understanding-quaternions/
476  // https://blog.magnum.graphics/backstage/the-unnecessarily-short-ways-to-do-a-quaternion-slerp/
477 
478  double cosHalfTheta = dot(q0, q1);
479  vpQuaternionVector q1_ = q1;
480  if (cosHalfTheta < 0) {
481  cosHalfTheta = -cosHalfTheta;
482  q1_ = -q1;
483  }
484 
485  double scale0 = 1 - t;
486  double scale1 = t;
487 
488  if ((1 - cosHalfTheta) > 0.1) {
489  double theta = std::acos(cosHalfTheta);
490  double invSinTheta = 1 / std::sin(theta);
491 
492  scale0 = std::sin((1 - t) * theta) * invSinTheta;
493  scale1 = std::sin(t * theta) * invSinTheta;
494  }
495 
496  vpQuaternionVector qSlerp;
497  qSlerp.x() = (scale0 * q0.x()) + (scale1 * q1_.x());
498  qSlerp.y() = (scale0 * q0.y()) + (scale1 * q1_.y());
499  qSlerp.z() = (scale0 * q0.z()) + (scale1 * q1_.z());
500  qSlerp.w() = (scale0 * q0.w()) + (scale1 * q1_.w());
501  qSlerp.normalize();
502 
503  return qSlerp;
504 }
505 END_VISP_NAMESPACE
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:148
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
@ fatalError
Fatal error.
Definition: vpException.h:72
static bool nul(double x, double threshold=0.001)
Definition: vpMath.h:450
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)
vpQuaternionVector & buildFrom(const double &qx, const double &qy, const double &qz, const double &qw)
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 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