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