Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
catchRotation.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 theta.u and quaternion multiplication.
32  */
33 
39 #include <visp3/core/vpConfig.h>
40 
41 #if defined(VISP_HAVE_CATCH2)
42 
43 #include <visp3/core/vpThetaUVector.h>
44 #include <visp3/core/vpUniRand.h>
45 
46 #include <catch_amalgamated.hpp>
47 
48 #ifdef ENABLE_VISP_NAMESPACE
49 using namespace VISP_NAMESPACE_NAME;
50 #endif
51 
52 namespace
53 {
54 vpThetaUVector generateThetaU(vpUniRand &rng)
55 {
56  return vpThetaUVector(
57  vpMath::rad(rng.uniform(-180.0, 180.0)) *
58  vpColVector({ rng.uniform(-1.0, 1.0), rng.uniform(-1.0, 1.0), rng.uniform(-1.0, 1.0) }).normalize());
59 }
60 
61 vpQuaternionVector generateQuat(vpUniRand &rng)
62 {
63  const double angle = vpMath::rad(rng.uniform(-180.0, 180.0));
64  const double ctheta = std::cos(angle);
65  const double stheta = std::sin(angle);
66  const double ax = rng.uniform(-1.0, 1.0);
67  const double ay = rng.uniform(-1.0, 1.0);
68  const double az = rng.uniform(-1.0, 1.0);
69  return vpQuaternionVector(stheta * ax, stheta * ay, stheta * az, ctheta);
70 }
71 } // namespace
72 
73 
74 bool test(const std::string &s, const vpArray2D<double> &v, const std::vector<double> &bench)
75 {
76  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
77  if (bench.size() != v.size()) {
78  std::cout << "Test fails: bad size wrt bench" << std::endl;
79  return false;
80  }
81  for (unsigned int i = 0; i < v.size(); i++) {
82  if (std::fabs(v.data[i] - bench[i]) > std::fabs(v.data[i]) * std::numeric_limits<double>::epsilon()) {
83  std::cout << "Test fails: bad content" << std::endl;
84  return false;
85  }
86  }
87 
88  return true;
89 }
90 
91 bool test(const std::string &s, const vpArray2D<double> &v, const vpColVector &bench)
92 {
93  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
94  if (bench.size() != v.size()) {
95  std::cout << "Test fails: bad size wrt bench" << std::endl;
96  return false;
97  }
98  for (unsigned int i = 0; i < v.size(); i++) {
99  if (std::fabs(v.data[i] - bench[i]) > std::fabs(v.data[i]) * std::numeric_limits<double>::epsilon()) {
100  std::cout << "Test fails: bad content" << std::endl;
101  return false;
102  }
103  }
104 
105  return true;
106 }
107 
108 bool test(const std::string &s, const vpRotationVector &v, const double &bench)
109 {
110  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
111  for (unsigned int i = 0; i < v.size(); i++) {
112  if (std::fabs(v[i] - bench) > std::fabs(v[i]) * std::numeric_limits<double>::epsilon()) {
113  std::cout << "Test fails: bad content" << std::endl;
114  return false;
115  }
116  }
117 
118  return true;
119 }
120 
121 bool test_matrix_equal(const vpHomogeneousMatrix &M1, const vpHomogeneousMatrix &M2, double epsilon = 1e-10)
122 {
123  for (unsigned int i = 0; i < 4; i++) {
124  for (unsigned int j = 0; j < 4; j++) {
125  if (!vpMath::equal(M1[i][j], M2[i][j], epsilon)) {
126  return false;
127  }
128  }
129  }
130  return true;
131 }
132 
133 TEST_CASE("Common rotation operations", "[rotation]")
134 {
135  SECTION("Theta u initialization")
136  {
138  std::vector<double> bench1(3, vpMath::rad(10));
139  vpColVector bench3(3, vpMath::rad(10));
140  CHECK(test("r1", r1, bench1));
141 
142  bench1.clear();
143  bench1 = r1.toStdVector();
144  CHECK(test("r1", r1, bench1));
145 
146  r1.buildFrom(bench3);
147  CHECK(test("r1", r1, bench3));
148 
149  vpThetaUVector r2 = r1;
150  CHECK(test("r2", r2, bench1));
151  CHECK(r2.data != r1.data);
152 
153  CHECK(test("r2", r2, vpMath::rad(10)));
154 
155  vpThetaUVector r3;
156  r3 = vpMath::rad(10);
157  CHECK(test("r3", r3, bench1));
158 
159  for (unsigned int i = 0; i < r3.size(); i++) {
160  CHECK(std::fabs(r3[i] - bench1[i]) < std::fabs(r3[i]) * std::numeric_limits<double>::epsilon());
161  }
162 
163  const vpColVector r4 = 0.5 * r1;
164  std::vector<double> bench2(3, vpMath::rad(5));
165  CHECK(test("r4", r4, bench2));
166 
167  const vpThetaUVector r5(r3);
168  CHECK(test("r5", r5, bench1));
169  }
170  SECTION("Rxyz initialization")
171  {
173  std::vector<double> bench1(3, vpMath::rad(10));
174  vpColVector bench3(3, vpMath::rad(10));
175  CHECK(test("r1", r1, bench1));
176 
177  bench1.clear();
178  bench1 = r1.toStdVector();
179  CHECK(test("r1", r1, bench1));
180 
181  r1.buildFrom(bench3);
182  CHECK(test("r1", r1, bench3));
183 
184  vpRxyzVector r2 = r1;
185  CHECK(test("r2", r2, bench1));
186 
187  CHECK(test("r2", r2, vpMath::rad(10)));
188 
189  vpRxyzVector r3;
190  r3 = vpMath::rad(10);
191  CHECK(test("r3", r3, bench1));
192 
193  for (unsigned int i = 0; i < r3.size(); i++) {
194  CHECK(std::fabs(r3[i] - bench1[i]) <= std::fabs(r3[i]) * std::numeric_limits<double>::epsilon());
195  }
196 
197  vpColVector r4 = 0.5 * r1;
198  std::vector<double> bench2(3, vpMath::rad(5));
199  CHECK(test("r4", r4, bench2));
200 
201  vpRxyzVector r5(r3);
202  CHECK(test("r5", r5, bench1));
203  }
204  SECTION("rzyx initialization")
205  {
207  std::vector<double> bench1(3, vpMath::rad(10));
208  vpColVector bench3(3, vpMath::rad(10));
209  CHECK(test("r1", r1, bench1));
210 
211  bench1.clear();
212  bench1 = r1.toStdVector();
213  CHECK(test("r1", r1, bench1));
214 
215  r1.buildFrom(bench3);
216  CHECK(test("r1", r1, bench3));
217 
218  vpRzyxVector r2 = r1;
219  CHECK(test("r2", r2, bench1));
220 
221  CHECK(test("r2", r2, vpMath::rad(10)));
222 
223  vpRzyxVector r3;
224  r3 = vpMath::rad(10);
225  CHECK(test("r3", r3, bench1));
226 
227  for (unsigned int i = 0; i < r3.size(); i++) {
228  CHECK(std::fabs(r3[i] - bench1[i]) <= std::fabs(r3[i]) * std::numeric_limits<double>::epsilon());
229  }
230 
231  vpColVector r4 = 0.5 * r1;
232  std::vector<double> bench2(3, vpMath::rad(5));
233  CHECK(test("r4", r4, bench2));
234 
235  vpRzyxVector r5(r3);
236  CHECK(test("r5", r5, bench1));
237  }
238  SECTION("rzyz initialiation")
239  {
241  std::vector<double> bench1(3, vpMath::rad(10));
242  vpColVector bench3(3, vpMath::rad(10));
243  CHECK(test("r1", r1, bench1));
244 
245  bench1.clear();
246  bench1 = r1.toStdVector();
247  CHECK(test("r1", r1, bench1));
248 
249  r1.buildFrom(bench3);
250  CHECK(test("r1", r1, bench3));
251 
252  vpRzyzVector r2 = r1;
253  CHECK(test("r2", r2, bench1));
254 
255  CHECK(test("r2", r2, vpMath::rad(10)));
256 
257  vpRzyzVector r3;
258  r3 = vpMath::rad(10);
259  CHECK(test("r3", r3, bench1));
260 
261  for (unsigned int i = 0; i < r3.size(); i++) {
262  CHECK(std::fabs(r3[i] - bench1[i]) <= std::fabs(r3[i]) * std::numeric_limits<double>::epsilon());
263  }
264 
265  vpColVector r4 = 0.5 * r1;
266  std::vector<double> bench2(3, vpMath::rad(5));
267  CHECK(test("r4", r4, bench2));
268 
269  vpRzyzVector r5(r3);
270  CHECK(test("r5", r5, bench1));
271  }
272  SECTION("Test quaternion initialization", "[quaternion]")
273  {
275  std::vector<double> bench1(4, vpMath::rad(10));
276  vpColVector bench3(4, vpMath::rad(10));
277  CHECK(test("r1", r1, bench1));
278 
279  bench1.clear();
280  bench1 = r1.toStdVector();
281  CHECK(test("r1", r1, bench1));
282 
283  r1.buildFrom(bench3);
284  CHECK(test("r1", r1, bench3));
285 
286  vpQuaternionVector r2 = r1;
287  CHECK(test("r2", r2, bench1));
288 
289  CHECK(test("r2", r2, vpMath::rad(10)));
290 
292  r3.set(vpMath::rad(10), vpMath::rad(10), vpMath::rad(10), vpMath::rad(10));
293  CHECK(test("r3", r3, bench1));
294 
295  for (unsigned int i = 0; i < r3.size(); i++) {
296  CHECK(std::fabs(r3[i] - bench1[i]) <= std::fabs(r3[i]) * std::numeric_limits<double>::epsilon());
297  }
298 
299  vpColVector r4 = 0.5 * r1;
300  std::vector<double> bench2(4, vpMath::rad(5));
301  CHECK(test("r4", r4, bench2));
302 
303  vpQuaternionVector r5(r3);
304  CHECK(test("r5", r5, bench1));
305  }
306  SECTION("Conversions")
307  {
309  for (int i = -10; i < 10; i++) {
310  for (int j = -10; j < 10; j++) {
311  vpThetaUVector tu(vpMath::rad(90 + i), vpMath::rad(170 + j), vpMath::rad(45));
312  tu.buildFrom(vpRotationMatrix(tu)); // put some coherence into rotation convention
313 
314  std::cout << "Initialization " << std::endl;
315 
316  double theta;
317  vpColVector u;
318  tu.extract(theta, u);
319 
320  std::cout << "theta=" << vpMath::deg(theta) << std::endl;
321  std::cout << "u=" << u << std::endl;
322 
323  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
324  R.buildFrom(tu);
325 
326  std::cout << "Matrix R";
327  CHECK(R.isARotationMatrix());
328 
329  std::cout << R << std::endl;
330 
331  std::cout << "From vpRotationMatrix to vpQuaternionVector " << std::endl;
332  vpQuaternionVector q(R);
333  CHECK(q.magnitude() == Catch::Approx(1.0).margin(1e-4));
334  std::cout << q << std::endl;
335 
336  R.buildFrom(q);
337  CHECK(R.isARotationMatrix());
338  std::cout << "From vpQuaternionVector to vpRotationMatrix " << std::endl;
339 
340  std::cout << "From vpRotationMatrix to vpRxyzVector " << std::endl;
341  vpRxyzVector RxyzbuildR(R);
342  std::cout << RxyzbuildR << std::endl;
343 
344  std::cout << "From vpRxyzVector to vpThetaUVector " << std::endl;
345  std::cout << " use From vpRxyzVector to vpRotationMatrix " << std::endl;
346  std::cout << " use From vpRotationMatrix to vpThetaUVector " << std::endl;
347 
348  vpThetaUVector tubuildEu;
349  tubuildEu.buildFrom(R);
350 
351  std::cout << std::endl;
352  std::cout << "result : should equivalent to the first one " << std::endl;
353 
354  double theta2;
355  vpColVector u2;
356 
357  tubuildEu.extract(theta2, u2);
358  std::cout << "theta=" << vpMath::deg(theta2) << std::endl;
359  std::cout << "u=" << u2 << std::endl;
360 
361  CHECK(vpMath::abs(theta2 - theta) < std::numeric_limits<double>::epsilon() * 1e10);
362  CHECK(vpMath::abs(u[0] - u2[0]) < std::numeric_limits<double>::epsilon() * 1e10);
363  CHECK(vpMath::abs(u[1] - u2[1]) < std::numeric_limits<double>::epsilon() * 1e10);
364  CHECK(vpMath::abs(u[2] - u2[2]) < std::numeric_limits<double>::epsilon() * 1e10);
365  }
366  }
367  SECTION("Conversion from and to rzyz vector")
368  {
369  vpRzyzVector rzyz(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45));
370  std::cout << "Initialization vpRzyzVector " << std::endl;
371  std::cout << rzyz << std::endl;
372  std::cout << "From vpRzyzVector to vpRotationMatrix " << std::endl;
373  R.buildFrom(rzyz);
374  CHECK(R.isARotationMatrix());
375  std::cout << "From vpRotationMatrix to vpRzyzVector " << std::endl;
376  vpRzyzVector rzyz_final;
377  rzyz_final.buildFrom(R);
378  CHECK(test("rzyz", rzyz_final, vpColVector(rzyz)));
379  std::cout << rzyz_final << std::endl;
380  }
381  SECTION("Conversion from and to rzyx vector")
382  {
383  vpRzyxVector rzyx(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45));
384  std::cout << "Initialization vpRzyxVector " << std::endl;
385  std::cout << rzyx << std::endl;
386  std::cout << "From vpRzyxVector to vpRotationMatrix " << std::endl;
387  R.buildFrom(rzyx);
388  CHECK(R.isARotationMatrix());
389  std::cout << R << std::endl;
390  std::cout << "From vpRotationMatrix to vpRzyxVector " << std::endl;
391  vpRzyxVector rzyx_final;
392  rzyx_final.buildFrom(R);
393  bool ret = test("rzyx", rzyx_final, vpColVector(rzyx));
394  if (ret == false) {
395  // Euler angle representation is not unique
396  std::cout << "Rzyx vector differ. Test rotation matrix..." << std::endl;
397  vpRotationMatrix RR(rzyx_final);
398  if (R == RR) {
399  std::cout << "Rzyx vector differ but rotation matrix is valid" << std::endl;
400  ret = true;
401  }
402  }
403  CHECK(ret);
404  std::cout << rzyx_final << std::endl;
405  }
406  }
407  SECTION("Rotation matrix extraction from homogeneous matrix and multiplication")
408  {
409  // Test rotation_matrix * homogeneous_matrix
410  vpHomogeneousMatrix _1_M_2_truth;
411  _1_M_2_truth[0][0] = 0.9835;
412  _1_M_2_truth[0][1] = -0.0581;
413  _1_M_2_truth[0][2] = 0.1716;
414  _1_M_2_truth[0][3] = 0;
415  _1_M_2_truth[1][0] = -0.0489;
416  _1_M_2_truth[1][1] = -0.9972;
417  _1_M_2_truth[1][2] = -0.0571;
418  _1_M_2_truth[1][3] = 0;
419  _1_M_2_truth[2][0] = 0.1744;
420  _1_M_2_truth[2][1] = 0.0478;
421  _1_M_2_truth[2][2] = -0.9835;
422  _1_M_2_truth[2][3] = 0;
423  vpHomogeneousMatrix _2_M_3_;
424  _2_M_3_[0][0] = 0.9835;
425  _2_M_3_[0][1] = -0.0581;
426  _2_M_3_[0][2] = 0.1716;
427  _2_M_3_[0][3] = 0.0072;
428  _2_M_3_[1][0] = -0.0489;
429  _2_M_3_[1][1] = -0.9972;
430  _2_M_3_[1][2] = -0.0571;
431  _2_M_3_[1][3] = 0.0352;
432  _2_M_3_[2][0] = 0.1744;
433  _2_M_3_[2][1] = 0.0478;
434  _2_M_3_[2][2] = -0.9835;
435  _2_M_3_[2][3] = 0.9470;
436 
437  vpRotationMatrix _1_R_2_ = _1_M_2_truth.getRotationMatrix();
438  vpHomogeneousMatrix _1_M_3_(_1_R_2_* _2_M_3_);
439  vpHomogeneousMatrix _1_M_3_truth(_1_M_2_truth * _2_M_3_);
440  CHECK(test_matrix_equal(_1_M_3_, _1_M_3_truth));
441  }
442 }
443 
444 TEST_CASE("Theta u multiplication", "[theta.u]")
445 {
446  const int nTrials = 100;
447  const uint64_t seed = 0x123456789;
448  vpUniRand rng(seed);
449  for (int iter = 0; iter < nTrials; iter++) {
450  const vpThetaUVector tu0 = generateThetaU(rng);
451  const vpThetaUVector tu1 = generateThetaU(rng);
452 
453  const vpRotationMatrix c1Rc2(tu0);
454  const vpRotationMatrix c2Rc3(tu1);
455  const vpRotationMatrix c1Rc3_ref = c1Rc2 * c2Rc3;
456  const vpThetaUVector c1_tu_c3 = tu0 * tu1;
457  // two rotation vectors can represent the same rotation,
458  // that is why we compare the rotation matrices
459  const vpRotationMatrix c1Rc3(c1_tu_c3);
460 
461  const double tolerance = 1e-9;
462  for (unsigned int i = 0; i < 3; i++) {
463  for (unsigned int j = 0; j < 3; j++) {
464  CHECK(c1Rc3_ref[i][j] == Catch::Approx(c1Rc3[i][j]).epsilon(0).margin(tolerance));
465  }
466  }
467  }
468 }
469 
470 TEST_CASE("Quaternion multiplication", "[quaternion]")
471 {
472  const int nTrials = 100;
473  const uint64_t seed = 0x123456789;
474  vpUniRand rng(seed);
475  for (int iter = 0; iter < nTrials; iter++) {
476  const vpQuaternionVector q0 = generateQuat(rng);
477  const vpQuaternionVector q1 = generateQuat(rng);
478 
479  const vpRotationMatrix c1Rc2(q0);
480  const vpRotationMatrix c2Rc3(q1);
481  const vpRotationMatrix c1Rc3_ref = c1Rc2 * c2Rc3;
482 
483  const vpQuaternionVector c1_q_c3 = q0 * q1;
484  // two quaternions of opposite sign can represent the same rotation,
485  // that is why we compare the rotation matrices
486  const vpRotationMatrix c1Rc3(c1_q_c3);
487 
488  const double tolerance = 1e-9;
489  for (unsigned int i = 0; i < 3; i++) {
490  for (unsigned int j = 0; j < 3; j++) {
491  CHECK(c1Rc3_ref[i][j] == Catch::Approx(c1Rc3[i][j]).epsilon(0).margin(tolerance));
492  }
493  }
494  }
495 }
496 
497 int main(int argc, char *argv[])
498 {
499  Catch::Session session;
500  session.applyCommandLine(argc, argv);
501  int numFailed = session.run();
502  return numFailed;
503 }
504 #else
505 #include <iostream>
506 
507 int main() { return EXIT_SUCCESS; }
508 #endif
unsigned int getCols() const
Definition: vpArray2D.h:337
Type * 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
unsigned int getRows() const
Definition: vpArray2D.h:347
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:405
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
static double rad(double deg)
Definition: vpMath.h:129
static Type abs(const Type &x)
Definition: vpMath.h:269
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:459
static double deg(double rad)
Definition: vpMath.h:119
Implementation of a rotation vector as quaternion angle minimal representation.
void set(double x, double y, double z, double w)
Implementation of a rotation matrix and operations on such kind of matrices.
bool isARotationMatrix(double threshold=1e-6) const
vpRotationMatrix & buildFrom(const vpHomogeneousMatrix &M)
Implementation of a generic rotation vector.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:184
vpRzyxVector & buildFrom(const vpRotationMatrix &R)
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyzVector.h:182
vpRzyzVector & buildFrom(const vpRotationMatrix &R)
Implementation of a rotation vector as axis-angle minimal representation.
void extract(double &theta, vpColVector &u) const
vpThetaUVector & buildFrom(const vpHomogeneousMatrix &M)
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:127
int uniform(int a, int b)
Definition: vpUniRand.cpp:159