Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
testQuaternion.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2024 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  * Test quaternion interpolation.
33  */
34 
40 #include <visp3/core/vpConfig.h>
41 
42 #ifdef VISP_HAVE_CATCH2
43 
44 #include <visp3/core/vpQuaternionVector.h>
45 
46 #define CATCH_CONFIG_RUNNER
47 #include <catch.hpp>
48 
49 TEST_CASE("Quaternion interpolation", "[quaternion]")
50 {
51  const double angle0 = vpMath::rad(-37.14);
52  const double angle1 = vpMath::rad(57.96);
53  vpColVector axis({ 1.2, 6.4, -3.7 });
54  axis.normalize();
55  const vpThetaUVector tu0(angle0 * axis);
56  const vpThetaUVector tu1(angle1 * axis);
57  const vpQuaternionVector q0(tu0);
58  const vpQuaternionVector q1(tu1);
59  const double t = 0.5;
60 
61  const double ref_angle_middle = t * (angle0 + angle1);
62  const double margin = 1e-3;
63  const double marginLerp = 1e-1;
64 
65  // From:
66  // https://github.com/google/mathfu/blob/a75f852f2d76f6f14d5697e0d09ce509a2e3bfc6/unit_tests/quaternion_test/quaternion_test.cpp#L319-L329
67  // This will verify that interpolating two quaternions corresponds to interpolating the angle.
68  SECTION("LERP")
69  {
71  CHECK(vpThetaUVector(qLerp).getTheta() == Approx(ref_angle_middle).margin(marginLerp));
72  }
73 
74  SECTION("NLERP")
75  {
77  CHECK(vpThetaUVector(qNlerp).getTheta() == Approx(ref_angle_middle).margin(margin));
78  }
79 
80  SECTION("SERP")
81  {
83  CHECK(vpThetaUVector(qSlerp).getTheta() == Approx(ref_angle_middle).margin(margin));
84  }
85 }
86 
87 TEST_CASE("Quaternion operators", "[quaternion]")
88 {
89 
90  SECTION("Addition and subtraction")
91  {
92  const vpQuaternionVector q1(2.1, -1, -3.7, 1.5);
93  const vpQuaternionVector q2(0.5, 1.4, 0.7, 2.5);
94  const vpQuaternionVector q3 = q1 + q2;
95  const double margin = std::numeric_limits<double>::epsilon();
96  std::cout << "q3=" << q3 << std::endl;
97  CHECK(q3.x() == Approx(2.6).margin(margin));
98  CHECK(q3.y() == Approx(0.4).margin(margin));
99  CHECK(q3.z() == Approx(-3.0).margin(margin));
100  CHECK(q3.w() == Approx(4.0).margin(margin));
101 
102 
103  // Test subtraction of two quaternions
104  const vpQuaternionVector q4 = q3 - q1;
105  std::cout << "q4=" << q4 << std::endl;
106  CHECK(q4.x() == Approx(q2.x()).margin(margin));
107  CHECK(q4.y() == Approx(q2.y()).margin(margin));
108  CHECK(q4.z() == Approx(q2.z()).margin(margin));
109  CHECK(q4.w() == Approx(q2.w()).margin(margin));
110  }
111 
112  SECTION("Multiplication")
113  {
115  const vpQuaternionVector q1(3.0, 4.0, 3.0, -sin(M_PI));
116  const vpQuaternionVector q2(3.9, -1.0, -3.0, 4.0);
117  const vpQuaternionVector q3 = q1 * q2;
118  const double margin = std::numeric_limits<double>::epsilon() * 1e4;
119  CHECK(q3.x() == Approx(3.0).margin(margin));
120  CHECK(q3.y() == Approx(36.7).margin(margin));
121  CHECK(q3.z() == Approx(-6.6).margin(margin));
122  CHECK(q3.w() == Approx(1.3).margin(margin));
123  }
124 
125  SECTION("Conjugate")
126  {
127  const vpQuaternionVector q1(3.0, 36.7, -6.6, 1.3);
128  const vpQuaternionVector q1_conj = q1.conjugate();
129  const double margin = std::numeric_limits<double>::epsilon();
130  CHECK(q1_conj.x() == Approx(-q1.x()).margin(margin));
131  CHECK(q1_conj.y() == Approx(-q1.y()).margin(margin));
132  CHECK(q1_conj.z() == Approx(-q1.z()).margin(margin));
133  CHECK(q1_conj.w() == Approx(q1.w()).margin(margin));
134  }
135 
136  SECTION("Inverse")
137  {
138  const vpQuaternionVector q1(3.0, 36.7, -6.6, 1.3);
139  const vpQuaternionVector q1_inv = q1.inverse();
140  const double margin = 1e-6;
141  CHECK(q1_inv.x() == Approx(-0.00214111).margin(margin));
142  CHECK(q1_inv.y() == Approx(-0.026193).margin(margin));
143  CHECK(q1_inv.z() == Approx(0.00471045).margin(margin));
144  CHECK(q1_inv.w() == Approx(0.000927816).margin(margin));
145  }
146 
147  SECTION("Norm")
148  {
149  const vpQuaternionVector q1(3.0, 36.7, -6.6, 1.3);
150  const double norm = q1.magnitude();
151  CHECK(norm == Approx(37.4318).margin(1e-4));
152  }
153 
154  SECTION("Normalization")
155  {
156  vpQuaternionVector q1(3.0, 36.7, -6.6, 1.3);
157  q1.normalize();
158  const double margin = 1e-6;
159  const double norm = q1.magnitude();
160  CHECK(norm == Approx(1.0).margin(1e-4));
161  CHECK(q1.x() == Approx(0.0801457).margin(margin));
162  CHECK(q1.y() == Approx(0.98045).margin(margin));
163  CHECK(q1.z() == Approx(-0.176321).margin(margin));
164  CHECK(q1.w() == Approx(0.0347298).margin(margin));
165  }
166 
167  SECTION("Copy constructor")
168  {
169  vpQuaternionVector q_copy1 = vpQuaternionVector(0, 0, 1, 1);
170  std::cout << "q_copy1=" << q_copy1 << std::endl;
171  const vpQuaternionVector q_copy2 = q_copy1;
172  CHECK_FALSE((!vpMath::equal(q_copy2.x(), q_copy1.x()) || !vpMath::equal(q_copy2.y(), q_copy1.y()) ||
173  !vpMath::equal(q_copy2.z(), q_copy1.z()) || !vpMath::equal(q_copy2.w(), q_copy1.w())));
174 
175  // compare data pointers: verify that they're not the same
176  CHECK(q_copy2.data != q_copy1.data);
177  q_copy1.set(1, 0, 1, 10);
178  CHECK((vpMath::equal(q_copy2.x(), q_copy1.x()) || vpMath::equal(q_copy2.y(), q_copy1.y()) ||
179  vpMath::equal(q_copy2.z(), q_copy1.z()) || vpMath::equal(q_copy2.w(), q_copy1.w())));
180  std::cout << "q_copy1 after set = " << q_copy1 << std::endl;
181  std::cout << "q_copy2=" << q_copy2 << std::endl;
182  }
183 
184  SECTION("operator=")
185  {
186  const vpQuaternionVector q1 = vpQuaternionVector(0, 0, 1, 1);
187  vpQuaternionVector q_same(10, 10, 10, 10);
188  q_same = q1;
189 
190  CHECK_FALSE((!vpMath::equal(q_same.x(), q1.x()) || !vpMath::equal(q_same.y(), q1.y()) ||
191  !vpMath::equal(q_same.z(), q1.z()) || !vpMath::equal(q_same.w(), q1.w())));
192  // compare data pointers: verify that they're not the same
193  CHECK(q_same.data != q1.data);
194  }
195 
196 }
197 
198 int main(int argc, char *argv[])
199 {
200  Catch::Session session; // There must be exactly one instance
201 
202  // Let Catch (using Clara) parse the command line
203  session.applyCommandLine(argc, argv);
204 
205  int numFailed = session.run();
206 
207  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
208  // This clamping has already been applied, so just return it here
209  // You can also do any post run clean-up here
210  return numFailed;
211 }
212 #else
213 #include <iostream>
214 
215 int main() { return EXIT_SUCCESS; }
216 #endif
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector & normalize()
static double rad(double deg)
Definition: vpMath.h:127
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:449
Implementation of a rotation vector as quaternion angle minimal representation.
const double & z() const
Returns the z-component of the quaternion.
vpQuaternionVector conjugate() const
vpQuaternionVector inverse() const
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)
const double & x() const
Returns the x-component of the quaternion.
const double & y() const
Returns the y-component of the quaternion.
const double & w() const
Returns the w-component of the quaternion.
static vpQuaternionVector lerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
Implementation of a rotation vector as axis-angle minimal representation.