Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
42// minimum value of sine
43const double vpQuaternionVector::minimum = 0.0001;
44const unsigned int vpQuaternionVector::constr_val_4 = 4;
45
50
53 : vpRotationVector(constr_val_4)
54{
55 w() = 1.0;
56}
57
59vpQuaternionVector::vpQuaternionVector(double x_, double y_, double z_, double w_) : vpRotationVector(constr_val_4)
60{
61 set(x_, y_, z_, w_);
62}
63
66
68vpQuaternionVector::vpQuaternionVector(const std::vector<double> &q) : vpRotationVector(constr_val_4) { buildFrom(q); }
69
76
84
92void vpQuaternionVector::set(double qx, double qy, double qz, double qw)
93{
94 const unsigned int index_0 = 0;
95 const unsigned int index_1 = 1;
96 const unsigned int index_2 = 2;
97 const unsigned int index_3 = 3;
98 data[index_0] = qx;
99 data[index_1] = qy;
100 data[index_2] = qz;
101 data[index_3] = qw;
102}
103
113vpQuaternionVector &vpQuaternionVector::buildFrom(const double &qx, const double &qy, const double &qz, const double &qw)
114{
115 set(qx, qy, qz, qw);
116 return *this;
117}
118
126{
127 vpRotationMatrix R(tu);
128 buildFrom(R);
129
130 return *this;
131}
132
137{
138 const unsigned int val_4 = 4;
139 if (q.size() != val_4) {
141 "Cannot construct a quaternion vector from a %d-dimension col vector", q.size()));
142 }
143 for (unsigned int i = 0; i < val_4; ++i) {
144 data[i] = q[i];
145 }
146
147 return *this;
148}
149
154{
155 const unsigned int val_4 = 4;
156 if (q.size() != val_4) {
158 "Cannot construct a quaternion vector from a %d-dimension std::vector", q.size()));
159 }
160
161 for (unsigned int i = 0; i < val_4; ++i) {
162 data[i] = q[i];
163 }
164
165 return *this;
166}
167
174{
175 vpThetaUVector tu(R);
176 vpColVector u;
177 double theta;
178 tu.extract(theta, u);
179
180 theta *= 0.5;
181
182 double sinTheta_2 = sin(theta);
183 const unsigned int index_0 = 0;
184 const unsigned int index_1 = 1;
185 const unsigned int index_2 = 2;
186 set(u[index_0] * sinTheta_2, u[index_1] * sinTheta_2, u[index_2] * sinTheta_2, cos(theta));
187 return *this;
188}
189
198{
199 return vpQuaternionVector(x() + q.x(), y() + q.y(), z() + q.z(), w() + q.w());
200}
201
209{
210 return vpQuaternionVector(x() - q.x(), y() - q.y(), z() - q.z(), w() - q.w());
211}
212
215{
216 return vpQuaternionVector(-x(), -y(), -z(), -w());
217}
218
221{
222 return vpQuaternionVector(l * x(), l * y(), l * z(), l * w());
223}
224
227{
228 return vpQuaternionVector(((w() * rq.x()) + (x() * rq.w()) + (y() * rq.z())) - (z() * rq.y()),
229 ((w() * rq.y()) + (y() * rq.w()) + (z() * rq.x())) - (x() * rq.z()),
230 ((w() * rq.z()) + (z() * rq.w()) + (x() * rq.y())) - (y() * rq.x()),
231 ((w() * rq.w()) - (x() * rq.x()) - (y() * rq.y())) - (z() * rq.z()));
232}
233
236{
237 if (vpMath::nul(l, std::numeric_limits<double>::epsilon())) {
238 throw vpException(vpException::fatalError, "Division by scalar l==0 !");
239 }
240
241 return vpQuaternionVector(x() / l, y() / l, z() / l, w() / l);
242}
243
270{
271 const unsigned int val_4 = 4;
272 if (q.size() != val_4) {
273 throw(vpException(vpException::dimensionError, "Cannot set a quaternion vector from a %d-dimension col vector",
274 q.size()));
275 }
276 for (unsigned int i = 0; i < val_4; ++i) {
277 data[i] = q[i];
278 }
279
280 return *this;
281}
282
283
284
291
298{
299 vpQuaternionVector q_inv;
300
301 double mag_square = (w() * w()) + (x() * x()) + (y() * y()) + (z() * z());
302 if (!vpMath::nul(mag_square, std::numeric_limits<double>::epsilon())) {
303 q_inv = this->conjugate() / mag_square;
304 }
305 else {
306 std::cerr << "The current quaternion is null ! The inverse cannot be computed !" << std::endl;
307 }
308
309 return q_inv;
310}
311
317double vpQuaternionVector::magnitude() const { return sqrt((w() * w()) + (x() * x()) + (y() * y()) + (z() * z())); }
318
323{
324 double mag = magnitude();
325 if (!vpMath::nul(mag, std::numeric_limits<double>::epsilon())) {
326 set(x() / mag, y() / mag, z() / mag, w() / mag);
327 }
328}
329
339{
340 return (q0.x() * q1.x()) + (q0.y() * q1.y()) + (q0.z() * q1.z()) + (q0.w() * q1.w());
341}
342
344const double &vpQuaternionVector::x() const { const unsigned int index_0 = 0; return data[index_0]; }
346const double &vpQuaternionVector::y() const { const unsigned int index_1 = 1; return data[index_1]; }
348const double &vpQuaternionVector::z() const { const unsigned int index_2 = 2; return data[index_2]; }
350const double &vpQuaternionVector::w() const { const unsigned int index_3 = 3; return data[index_3]; }
351
353double &vpQuaternionVector::x() { const unsigned int index_0 = 0; return data[index_0]; }
355double &vpQuaternionVector::y() { const unsigned int index_1 = 1; return data[index_1]; }
357double &vpQuaternionVector::z() { const unsigned int index_2 = 2; return data[index_2]; }
359double &vpQuaternionVector::w() { const unsigned int index_3 = 3; return data[index_3]; }
360
361#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
383vpQuaternionVector &vpQuaternionVector::operator=(const std::initializer_list<double> &list)
384{
385 if (list.size() > size()) {
386 throw(vpException(
388 "Cannot set quaternion vector out of bounds. It has only %d values while you try to initialize with %d values",
389 size(), list.size()));
390 }
391 std::copy(list.begin(), list.end(), data);
392 return *this;
393}
394#endif
395
411{
412 assert(t >= 0 && t <= 1);
413
414 double cosHalfTheta = dot(q0, q1);
415 vpQuaternionVector q1_ = q1;
416 if (cosHalfTheta < 0) {
417 cosHalfTheta = -cosHalfTheta;
418 q1_ = -q1;
419 }
420
421 vpQuaternionVector qLerp;
422 qLerp.x() = q0.x() - (t * (q0.x() - q1.x()));
423 qLerp.y() = q0.y() - (t * (q0.y() - q1.y()));
424 qLerp.z() = q0.z() - (t * (q0.z() - q1.z()));
425 qLerp.w() = q0.w() - (t * (q0.w() - q1.w()));
426
427 return qLerp;
428}
429
445{
446 assert(t >= 0 && t <= 1);
447
448 vpQuaternionVector qLerp = lerp(q0, q1, t);
449 qLerp.normalize();
450
451 return qLerp;
452}
453
469{
470 assert(t >= 0 && t <= 1);
471 // Some additional references:
472 // https://splines.readthedocs.io/en/latest/rotation/slerp.html
473 // https://zeux.io/2015/07/23/approximating-slerp/
474 // https://github.com/eigenteam/eigen-git-mirror/blob/36b95962756c1fce8e29b1f8bc45967f30773c00/Eigen/src/Geometry/Quaternion.h#L753-L790
475 // https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
476 // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
477 // https://www.3dgep.com/understanding-quaternions/
478 // https://blog.magnum.graphics/backstage/the-unnecessarily-short-ways-to-do-a-quaternion-slerp/
479
480 double cosHalfTheta = dot(q0, q1);
481 vpQuaternionVector q1_ = q1;
482 if (cosHalfTheta < 0) {
483 cosHalfTheta = -cosHalfTheta;
484 q1_ = -q1;
485 }
486
487 double scale0 = 1 - t;
488 double scale1 = t;
489
490 if ((1 - cosHalfTheta) > 0.1) {
491 double theta = std::acos(cosHalfTheta);
492 double invSinTheta = 1 / std::sin(theta);
493
494 scale0 = std::sin((1 - t) * theta) * invSinTheta;
495 scale1 = std::sin(t * theta) * invSinTheta;
496 }
497
498 vpQuaternionVector qSlerp;
499 qSlerp.x() = (scale0 * q0.x()) + (scale1 * q1_.x());
500 qSlerp.y() = (scale0 * q0.y()) + (scale1 * q1_.y());
501 qSlerp.z() = (scale0 * q0.z()) + (scale1 * q1_.z());
502 qSlerp.w() = (scale0 * q0.w()) + (scale1 * q1_.w());
503 qSlerp.normalize();
504
505 return qSlerp;
506}
507END_VISP_NAMESPACE
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
Implementation of column vector and the associated operations.
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:453
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.
vpRotationVector()
Constructor that constructs a 0-size rotation vector.
vpRowVector t() const
Implementation of a rotation vector as axis-angle minimal representation.