Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testRotation.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  * Tests transformation from various representations of rotation.
33  *
34 *****************************************************************************/
35 
41 #include <visp3/core/vpMath.h>
42 #include <visp3/core/vpQuaternionVector.h>
43 #include <visp3/core/vpRotationMatrix.h>
44 #include <visp3/io/vpParseArgv.h>
45 
46 #include <cassert>
47 #include <limits>
48 #include <stdio.h>
49 #include <stdlib.h>
50 
51 static unsigned int cpt = 0;
52 
53 bool test(const std::string &s, const vpArray2D<double> &v, const std::vector<double> &bench)
54 {
55  std::cout << "** Test " << ++cpt << std::endl;
56  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
57  if (bench.size() != v.size()) {
58  std::cout << "Test fails: bad size wrt bench" << std::endl;
59  return false;
60  }
61  for (unsigned int i = 0; i < v.size(); i++) {
62  if (std::fabs(v.data[i] - bench[i]) > std::fabs(v.data[i]) * std::numeric_limits<double>::epsilon()) {
63  std::cout << "Test fails: bad content" << std::endl;
64  return false;
65  }
66  }
67 
68  return true;
69 }
70 
71 bool test(const std::string &s, const vpArray2D<double> &v, const vpColVector &bench)
72 {
73  std::cout << "** Test " << ++cpt << std::endl;
74  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
75  if (bench.size() != v.size()) {
76  std::cout << "Test fails: bad size wrt bench" << std::endl;
77  return false;
78  }
79  for (unsigned int i = 0; i < v.size(); i++) {
80  if (std::fabs(v.data[i] - bench[i]) > std::fabs(v.data[i]) * std::numeric_limits<double>::epsilon()) {
81  std::cout << "Test fails: bad content" << std::endl;
82  return false;
83  }
84  }
85 
86  return true;
87 }
88 
89 bool test(const std::string &s, const vpRotationVector &v, const double &bench)
90 {
91  std::cout << "** Test " << ++cpt << std::endl;
92  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
93  for (unsigned int i = 0; i < v.size(); i++) {
94  if (std::fabs(v[i] - bench) > std::fabs(v[i]) * std::numeric_limits<double>::epsilon()) {
95  std::cout << "Test fails: bad content" << std::endl;
96  return false;
97  }
98  }
99 
100  return true;
101 }
102 
103 bool test_matrix_equal(const vpHomogeneousMatrix &M1, const vpHomogeneousMatrix &M2, double epsilon = 1e-10)
104 {
105  for (unsigned int i = 0; i < 4; i++) {
106  for (unsigned int j = 0; j < 4; j++) {
107  if (!vpMath::equal(M1[i][j], M2[i][j], epsilon)) {
108  return false;
109  }
110  }
111  }
112  return true;
113 }
114 
115 int main()
116 {
117  try {
118  {
120  std::vector<double> bench1(3, vpMath::rad(10));
121  vpColVector bench3(3, vpMath::rad(10));
122  if (test("r1", r1, bench1) == false)
123  return EXIT_FAILURE;
124 
125  bench1.clear();
126  bench1 = r1.toStdVector();
127  if (test("r1", r1, bench1) == false)
128  return EXIT_FAILURE;
129 
130  r1.buildFrom(bench3);
131  if (test("r1", r1, bench3) == false)
132  return EXIT_FAILURE;
133 
134  vpThetaUVector r2 = r1;
135  if (test("r2", r2, bench1) == false)
136  return EXIT_FAILURE;
137 
138  if (test("r2", r2, vpMath::rad(10)) == false)
139  return EXIT_FAILURE;
140 
141  vpThetaUVector r3;
142  r3 = vpMath::rad(10);
143  if (test("r3", r3, bench1) == false)
144  return EXIT_FAILURE;
145 
146  std::cout << "** Test " << ++cpt << std::endl;
147  for (unsigned int i = 0; i < r3.size(); i++) {
148  if (std::fabs(r3[i] - bench1[i]) > std::fabs(r3[i]) * std::numeric_limits<double>::epsilon()) {
149  std::cout << "Test fails: bad content" << std::endl;
150  return EXIT_FAILURE;
151  }
152  }
153 
154  vpColVector r4 = 0.5 * r1;
155  std::vector<double> bench2(3, vpMath::rad(5));
156  if (test("r4", r4, bench2) == false)
157  return EXIT_FAILURE;
158 
159  vpThetaUVector r5(r3);
160  if (test("r5", r5, bench1) == false)
161  return EXIT_FAILURE;
162  }
163  {
165  std::vector<double> bench1(3, vpMath::rad(10));
166  vpColVector bench3(3, vpMath::rad(10));
167  if (test("r1", r1, bench1) == false)
168  return EXIT_FAILURE;
169 
170  bench1.clear();
171  bench1 = r1.toStdVector();
172  if (test("r1", r1, bench1) == false)
173  return EXIT_FAILURE;
174 
175  r1.buildFrom(bench3);
176  if (test("r1", r1, bench3) == false)
177  return EXIT_FAILURE;
178 
179  vpRxyzVector r2 = r1;
180  if (test("r2", r2, bench1) == false)
181  return EXIT_FAILURE;
182 
183  if (test("r2", r2, vpMath::rad(10)) == false)
184  return EXIT_FAILURE;
185 
186  vpRxyzVector r3;
187  r3 = vpMath::rad(10);
188  if (test("r3", r3, bench1) == false)
189  return EXIT_FAILURE;
190 
191  std::cout << "** Test " << ++cpt << std::endl;
192  for (unsigned int i = 0; i < r3.size(); i++) {
193  if (std::fabs(r3[i] - bench1[i]) > std::fabs(r3[i]) * std::numeric_limits<double>::epsilon()) {
194  std::cout << "Test fails: bad content" << std::endl;
195  return EXIT_FAILURE;
196  }
197  }
198 
199  vpColVector r4 = 0.5 * r1;
200  std::vector<double> bench2(3, vpMath::rad(5));
201  if (test("r4", r4, bench2) == false)
202  return EXIT_FAILURE;
203 
204  vpRxyzVector r5(r3);
205  if (test("r5", r5, bench1) == false)
206  return EXIT_FAILURE;
207  }
208  {
210  std::vector<double> bench1(3, vpMath::rad(10));
211  vpColVector bench3(3, vpMath::rad(10));
212  if (test("r1", r1, bench1) == false)
213  return EXIT_FAILURE;
214 
215  bench1.clear();
216  bench1 = r1.toStdVector();
217  if (test("r1", r1, bench1) == false)
218  return EXIT_FAILURE;
219 
220  r1.buildFrom(bench3);
221  if (test("r1", r1, bench3) == false)
222  return EXIT_FAILURE;
223 
224  vpRzyxVector r2 = r1;
225  if (test("r2", r2, bench1) == false)
226  return EXIT_FAILURE;
227 
228  if (test("r2", r2, vpMath::rad(10)) == false)
229  return EXIT_FAILURE;
230 
231  vpRzyxVector r3;
232  r3 = vpMath::rad(10);
233  if (test("r3", r3, bench1) == false)
234  return EXIT_FAILURE;
235 
236  std::cout << "** Test " << ++cpt << std::endl;
237  for (unsigned int i = 0; i < r3.size(); i++) {
238  if (std::fabs(r3[i] - bench1[i]) > std::fabs(r3[i]) * std::numeric_limits<double>::epsilon()) {
239  std::cout << "Test fails: bad content" << std::endl;
240  return EXIT_FAILURE;
241  }
242  }
243 
244  vpColVector r4 = 0.5 * r1;
245  std::vector<double> bench2(3, vpMath::rad(5));
246  if (test("r4", r4, bench2) == false)
247  return EXIT_FAILURE;
248 
249  vpRzyxVector r5(r3);
250  if (test("r5", r5, bench1) == false)
251  return EXIT_FAILURE;
252  }
253  {
255  std::vector<double> bench1(3, vpMath::rad(10));
256  vpColVector bench3(3, vpMath::rad(10));
257  if (test("r1", r1, bench1) == false)
258  return EXIT_FAILURE;
259 
260  bench1.clear();
261  bench1 = r1.toStdVector();
262  if (test("r1", r1, bench1) == false)
263  return EXIT_FAILURE;
264 
265  r1.buildFrom(bench3);
266  if (test("r1", r1, bench3) == false)
267  return EXIT_FAILURE;
268 
269  vpRzyzVector r2 = r1;
270  if (test("r2", r2, bench1) == false)
271  return EXIT_FAILURE;
272 
273  if (test("r2", r2, vpMath::rad(10)) == false)
274  return EXIT_FAILURE;
275 
276  vpRzyzVector r3;
277  r3 = vpMath::rad(10);
278  if (test("r3", r3, bench1) == false)
279  return EXIT_FAILURE;
280 
281  std::cout << "** Test " << ++cpt << std::endl;
282  for (unsigned int i = 0; i < r3.size(); i++) {
283  if (std::fabs(r3[i] - bench1[i]) > std::fabs(r3[i]) * std::numeric_limits<double>::epsilon()) {
284  std::cout << "Test fails: bad content" << std::endl;
285  return EXIT_FAILURE;
286  }
287  }
288 
289  vpColVector r4 = 0.5 * r1;
290  std::vector<double> bench2(3, vpMath::rad(5));
291  if (test("r4", r4, bench2) == false)
292  return EXIT_FAILURE;
293 
294  vpRzyzVector r5(r3);
295  if (test("r5", r5, bench1) == false)
296  return EXIT_FAILURE;
297  }
298  {
300  std::vector<double> bench1(4, vpMath::rad(10));
301  vpColVector bench3(4, vpMath::rad(10));
302  if (test("r1", r1, bench1) == false)
303  return EXIT_FAILURE;
304 
305  bench1.clear();
306  bench1 = r1.toStdVector();
307  if (test("r1", r1, bench1) == false)
308  return EXIT_FAILURE;
309 
310  r1.buildFrom(bench3);
311  if (test("r1", r1, bench3) == false)
312  return EXIT_FAILURE;
313 
314  vpQuaternionVector r2 = r1;
315  if (test("r2", r2, bench1) == false)
316  return EXIT_FAILURE;
317 
318  if (test("r2", r2, vpMath::rad(10)) == false)
319  return EXIT_FAILURE;
320 
322  r3.set(vpMath::rad(10), vpMath::rad(10), vpMath::rad(10), vpMath::rad(10));
323  if (test("r3", r3, bench1) == false)
324  return EXIT_FAILURE;
325 
326  std::cout << "** Test " << ++cpt << std::endl;
327  for (unsigned int i = 0; i < r3.size(); i++) {
328  if (std::fabs(r3[i] - bench1[i]) > std::fabs(r3[i]) * std::numeric_limits<double>::epsilon()) {
329  std::cout << "Test fails: bad content" << std::endl;
330  return EXIT_FAILURE;
331  }
332  }
333 
334  vpColVector r4 = 0.5 * r1;
335  std::vector<double> bench2(4, vpMath::rad(5));
336  if (test("r4", r4, bench2) == false)
337  return EXIT_FAILURE;
338 
339  vpQuaternionVector r5(r3);
340  if (test("r5", r5, bench1) == false)
341  return EXIT_FAILURE;
342  }
343  {
345  for (int i = -10; i < 10; i++) {
346  for (int j = -10; j < 10; j++) {
347  vpThetaUVector tu(vpMath::rad(90 + i), vpMath::rad(170 + j), vpMath::rad(45));
348  tu.buildFrom(vpRotationMatrix(tu)); // put some coherence into rotation convention
349 
350  std::cout << "Initialization " << std::endl;
351 
352  double theta;
353  vpColVector u;
354  tu.extract(theta, u);
355 
356  std::cout << "theta=" << vpMath::deg(theta) << std::endl;
357  std::cout << "u=" << u << std::endl;
358 
359  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
360  R.buildFrom(tu);
361 
362  std::cout << "Matrix R";
363  if (R.isARotationMatrix() == 1)
364  std::cout << " is a rotation matrix " << std::endl;
365  else
366  std::cout << " is not a rotation matrix " << std::endl;
367 
368  std::cout << R << std::endl;
369 
370  std::cout << "From vpRotationMatrix to vpQuaternionVector " << std::endl;
371  vpQuaternionVector q(R);
372  std::cout << q << std::endl;
373 
374  R.buildFrom(q);
375  std::cout << "From vpQuaternionVector to vpRotationMatrix " << std::endl;
376 
377  std::cout << "From vpRotationMatrix to vpRxyzVector " << std::endl;
378  vpRxyzVector RxyzBuildFromR(R);
379  std::cout << RxyzBuildFromR << std::endl;
380 
381  std::cout << "From vpRxyzVector to vpThetaUVector " << std::endl;
382  std::cout << " use From vpRxyzVector to vpRotationMatrix " << std::endl;
383  std::cout << " use From vpRotationMatrix to vpThetaUVector " << std::endl;
384 
385  vpThetaUVector tuBuildFromEu;
386  tuBuildFromEu.buildFrom(R);
387 
388  std::cout << std::endl;
389  std::cout << "result : should equivalent to the first one " << std::endl;
390 
391  double theta2;
392  vpColVector u2;
393 
394  tuBuildFromEu.extract(theta2, u2);
395  std::cout << "theta=" << vpMath::deg(theta2) << std::endl;
396  std::cout << "u=" << u2 << std::endl;
397 
398  assert(vpMath::abs(theta2 - theta) < std::numeric_limits<double>::epsilon() * 1e10);
399  assert(vpMath::abs(u[0] - u2[0]) < std::numeric_limits<double>::epsilon() * 1e10);
400  assert(vpMath::abs(u[1] - u2[1]) < std::numeric_limits<double>::epsilon() * 1e10);
401  assert(vpMath::abs(u[2] - u2[2]) < std::numeric_limits<double>::epsilon() * 1e10);
402  }
403  vpRzyzVector rzyz(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45));
404  std::cout << "Initialization vpRzyzVector " << std::endl;
405  std::cout << rzyz << std::endl;
406  std::cout << "From vpRzyzVector to vpRotationMatrix " << std::endl;
407  R.buildFrom(rzyz);
408  std::cout << "From vpRotationMatrix to vpRzyzVector " << std::endl;
409  vpRzyzVector rzyz_final;
410  rzyz_final.buildFrom(R);
411  std::cout << rzyz_final << std::endl;
412 
413  vpRzyxVector rzyx(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45));
414  std::cout << "Initialization vpRzyxVector " << std::endl;
415  std::cout << rzyx << std::endl;
416  std::cout << "From vpRzyxVector to vpRotationMatrix " << std::endl;
417  R.buildFrom(rzyx);
418  std::cout << R << std::endl;
419  std::cout << "From vpRotationMatrix to vpRzyxVector " << std::endl;
420  vpRzyxVector rzyx_final;
421  rzyx_final.buildFrom(R);
422  std::cout << rzyx_final << std::endl;
423  }
424  }
425  {
426  // Test rotation_matrix * homogeneous_matrix
427  vpHomogeneousMatrix _1_M_2_truth;
428  _1_M_2_truth[0][0] = 0.9835;
429  _1_M_2_truth[0][1] = -0.0581;
430  _1_M_2_truth[0][2] = 0.1716;
431  _1_M_2_truth[0][3] = 0;
432  _1_M_2_truth[1][0] = -0.0489;
433  _1_M_2_truth[1][1] = -0.9972;
434  _1_M_2_truth[1][2] = -0.0571;
435  _1_M_2_truth[1][3] = 0;
436  _1_M_2_truth[2][0] = 0.1744;
437  _1_M_2_truth[2][1] = 0.0478;
438  _1_M_2_truth[2][2] = -0.9835;
439  _1_M_2_truth[2][3] = 0;
440  vpHomogeneousMatrix _2_M_3_;
441  _2_M_3_[0][0] = 0.9835;
442  _2_M_3_[0][1] = -0.0581;
443  _2_M_3_[0][2] = 0.1716;
444  _2_M_3_[0][3] = 0.0072;
445  _2_M_3_[1][0] = -0.0489;
446  _2_M_3_[1][1] = -0.9972;
447  _2_M_3_[1][2] = -0.0571;
448  _2_M_3_[1][3] = 0.0352;
449  _2_M_3_[2][0] = 0.1744;
450  _2_M_3_[2][1] = 0.0478;
451  _2_M_3_[2][2] = -0.9835;
452  _2_M_3_[2][3] = 0.9470;
453 
454  vpRotationMatrix _1_R_2_ = _1_M_2_truth.getRotationMatrix();
455  vpHomogeneousMatrix _1_M_3_(_1_R_2_* _2_M_3_);
456  vpHomogeneousMatrix _1_M_3_truth(_1_M_2_truth * _2_M_3_);
457  bool success = test_matrix_equal(_1_M_3_, _1_M_3_truth);
458  std::cout << "Test vpHomogeneousMatrix vpRotationMatrix::operator*(vpHomogeneousMatrix) " << (success ? "succeed" : "failed") << std::endl;
459  if (!success) {
460  return EXIT_FAILURE;
461  }
462  }
463  std::cout << "All tests succeed" << std::endl;
464  return EXIT_SUCCESS;
465  }
466  catch (const vpException &e) {
467  std::cout << "Catch an exception: " << e << std::endl;
468  return EXIT_FAILURE;
469  }
470 }
unsigned int getCols() const
Definition: vpArray2D.h:274
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:138
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:286
unsigned int getRows() const
Definition: vpArray2D.h:284
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:367
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
static double rad(double deg)
Definition: vpMath.h:127
static Type abs(const Type &x)
Definition: vpMath.h:267
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:449
static double deg(double rad)
Definition: vpMath.h:117
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:176
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:177
vpRzyxVector buildFrom(const vpRotationMatrix &R)
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyzVector.h:175
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)