Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testQuaternion2.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  * Test quaternion interpolation.
33  *
34 *****************************************************************************/
35 
41 #include <visp3/core/vpConfig.h>
42 
43 #ifdef VISP_HAVE_CATCH2
44 
45 #include <visp3/core/vpQuaternionVector.h>
46 
47 #define CATCH_CONFIG_RUNNER
48 #include <catch.hpp>
49 
50 TEST_CASE("Quaternion interpolation", "[quaternion]")
51 {
52  const double angle0 = vpMath::rad(-37.14);
53  const double angle1 = vpMath::rad(57.96);
54  vpColVector axis({1.2, 6.4, -3.7});
55  axis.normalize();
56  const vpThetaUVector tu0(angle0 * axis);
57  const vpThetaUVector tu1(angle1 * axis);
58  const vpQuaternionVector q0(tu0);
59  const vpQuaternionVector q1(tu1);
60  const double t = 0.5;
61 
62  const double ref_angle_middle = t * (angle0 + angle1);
63  const double margin = 1e-3;
64  const double marginLerp = 1e-1;
65 
66  // From:
67  // https://github.com/google/mathfu/blob/a75f852f2d76f6f14d5697e0d09ce509a2e3bfc6/unit_tests/quaternion_test/quaternion_test.cpp#L319-L329
68  // This will verify that interpolating two quaternions corresponds to interpolating the angle.
69  SECTION("LERP")
70  {
72  CHECK(vpThetaUVector(qLerp).getTheta() == Approx(ref_angle_middle).margin(marginLerp));
73  }
74 
75  SECTION("NLERP")
76  {
78  CHECK(vpThetaUVector(qNlerp).getTheta() == Approx(ref_angle_middle).margin(margin));
79  }
80 
81  SECTION("SERP")
82  {
84  CHECK(vpThetaUVector(qSlerp).getTheta() == Approx(ref_angle_middle).margin(margin));
85  }
86 }
87 
88 int main(int argc, char *argv[])
89 {
90  Catch::Session session; // There must be exactly one instance
91 
92  // Let Catch (using Clara) parse the command line
93  session.applyCommandLine(argc, argv);
94 
95  int numFailed = session.run();
96 
97  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
98  // This clamping has already been applied, so just return it here
99  // You can also do any post run clean-up here
100  return numFailed;
101 }
102 #else
103 #include <iostream>
104 
105 int main() { return EXIT_SUCCESS; }
106 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector & normalize()
static double rad(double deg)
Definition: vpMath.h:127
Implementation of a rotation vector as quaternion angle minimal representation.
static vpQuaternionVector slerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
static vpQuaternionVector nlerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
static vpQuaternionVector lerp(const vpQuaternionVector &q0, const vpQuaternionVector &q1, double t)
Implementation of a rotation vector as axis-angle minimal representation.