Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testRotation.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Tests transformation from various representations of rotation.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpRotationMatrix.h>
48 #include <visp3/io/vpParseArgv.h>
49 #include <visp3/core/vpQuaternionVector.h>
50 
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <cassert>
54 #include <limits>
55 
56 static unsigned int cpt = 0;
57 
58 bool test(const std::string &s, const vpArray2D<double> &v, const std::vector<double> &bench)
59 {
60  std::cout << "** Test " << ++cpt << std::endl;
61  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
62  if(bench.size() != v.size()) {
63  std::cout << "Test fails: bad size wrt bench" << std::endl;
64  return false;
65  }
66  for (unsigned int i=0; i<v.size(); i++) {
67  if (std::fabs(v.data[i]-bench[i]) > std::fabs(v.data[i])*std::numeric_limits<double>::epsilon()) {
68  std::cout << "Test fails: bad content" << std::endl;
69  return false;
70  }
71  }
72 
73  return true;
74 }
75 
76 bool test(const std::string &s, const vpRotationVector &v, const double &bench)
77 {
78  std::cout << "** Test " << ++cpt << std::endl;
79  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
80  for (unsigned int i=0; i<v.size(); i++) {
81  if (std::fabs(v[i]-bench) > std::fabs(v[i])*std::numeric_limits<double>::epsilon()) {
82  std::cout << "Test fails: bad content" << std::endl;
83  return false;
84  }
85  }
86 
87  return true;
88 }
89 
90 int main()
91 {
92  try {
93  int err = 1;
94  {
96  std::vector<double> bench1(3, vpMath::rad(10));
97  if (test("r1", r1, bench1) == false)
98  return err;
99 
100  vpThetaUVector r2 = r1;
101  if (test("r2", r2, bench1) == false)
102  return err;
103 
104  if (test("r2", r2, vpMath::rad(10)) == false)
105  return err;
106 
107  vpThetaUVector r3;
108  r3 = vpMath::rad(10);
109  if (test("r3", r3, bench1) == false)
110  return err;
111 
112  std::cout << "** Test " << ++cpt << std::endl;
113  for(unsigned int i=0; i<r3.size(); i++) {
114  if (std::fabs(r3[i]-bench1[i]) > std::fabs(r3[i])*std::numeric_limits<double>::epsilon()) {
115  std::cout << "Test fails: bad content" << std::endl;
116  return false;
117  }
118  }
119 
120  vpColVector r4 = 0.5 * r1;
121  std::vector<double> bench2(3, vpMath::rad(5));
122  if (test("r4", r4, bench2) == false)
123  return err;
124 
125  vpThetaUVector r5(r3);
126  if (test("r5", r5, bench1) == false)
127  return err;
128  }
129  {
131  std::vector<double> bench1(3, vpMath::rad(10));
132  if (test("r1", r1, bench1) == false)
133  return err;
134 
135  vpRxyzVector r2 = r1;
136  if (test("r2", r2, bench1) == false)
137  return err;
138 
139  if (test("r2", r2, vpMath::rad(10)) == false)
140  return err;
141 
142  vpRxyzVector r3;
143  r3 = vpMath::rad(10);
144  if (test("r3", r3, bench1) == false)
145  return err;
146 
147  std::cout << "** Test " << ++cpt << std::endl;
148  for(unsigned int i=0; i<r3.size(); i++) {
149  if (std::fabs(r3[i]-bench1[i]) > std::fabs(r3[i])*std::numeric_limits<double>::epsilon()) {
150  std::cout << "Test fails: bad content" << std::endl;
151  return false;
152  }
153  }
154 
155  vpColVector r4 = 0.5 * r1;
156  std::vector<double> bench2(3, vpMath::rad(5));
157  if (test("r4", r4, bench2) == false)
158  return err;
159 
160  vpRxyzVector r5(r3);
161  if (test("r5", r5, bench1) == false)
162  return err;
163  }
164  {
166  std::vector<double> bench1(3, vpMath::rad(10));
167  if (test("r1", r1, bench1) == false)
168  return err;
169 
170  vpRzyxVector r2 = r1;
171  if (test("r2", r2, bench1) == false)
172  return err;
173 
174  if (test("r2", r2, vpMath::rad(10)) == false)
175  return err;
176 
177  vpRzyxVector r3;
178  r3 = vpMath::rad(10);
179  if (test("r3", r3, bench1) == false)
180  return err;
181 
182  std::cout << "** Test " << ++cpt << std::endl;
183  for(unsigned int i=0; i<r3.size(); i++) {
184  if (std::fabs(r3[i]-bench1[i]) > std::fabs(r3[i])*std::numeric_limits<double>::epsilon()) {
185  std::cout << "Test fails: bad content" << std::endl;
186  return false;
187  }
188  }
189 
190  vpColVector r4 = 0.5 * r1;
191  std::vector<double> bench2(3, vpMath::rad(5));
192  if (test("r4", r4, bench2) == false)
193  return err;
194 
195  vpRzyxVector r5(r3);
196  if (test("r5", r5, bench1) == false)
197  return err;
198  }
199  {
201  std::vector<double> bench1(3, vpMath::rad(10));
202  if (test("r1", r1, bench1) == false)
203  return err;
204 
205  vpRzyzVector r2 = r1;
206  if (test("r2", r2, bench1) == false)
207  return err;
208 
209  if (test("r2", r2, vpMath::rad(10)) == false)
210  return err;
211 
212  vpRzyzVector r3;
213  r3 = vpMath::rad(10);
214  if (test("r3", r3, bench1) == false)
215  return err;
216 
217  std::cout << "** Test " << ++cpt << std::endl;
218  for(unsigned int i=0; i<r3.size(); i++) {
219  if (std::fabs(r3[i]-bench1[i]) > std::fabs(r3[i])*std::numeric_limits<double>::epsilon()) {
220  std::cout << "Test fails: bad content" << std::endl;
221  return false;
222  }
223  }
224 
225  vpColVector r4 = 0.5 * r1;
226  std::vector<double> bench2(3, vpMath::rad(5));
227  if (test("r4", r4, bench2) == false)
228  return err;
229 
230  vpRzyzVector r5(r3);
231  if (test("r5", r5, bench1) == false)
232  return err;
233  }
234  {
236  std::vector<double> bench1(4, vpMath::rad(10));
237  if (test("r1", r1, bench1) == false)
238  return err;
239 
240  vpQuaternionVector r2 = r1;
241  if (test("r2", r2, bench1) == false)
242  return err;
243 
244  if (test("r2", r2, vpMath::rad(10)) == false)
245  return err;
246 
248  r3.set(vpMath::rad(10), vpMath::rad(10), vpMath::rad(10), vpMath::rad(10));
249  if (test("r3", r3, bench1) == false)
250  return err;
251 
252  std::cout << "** Test " << ++cpt << std::endl;
253  for(unsigned int i=0; i<r3.size(); i++) {
254  if (std::fabs(r3[i]-bench1[i]) > std::fabs(r3[i])*std::numeric_limits<double>::epsilon()) {
255  std::cout << "Test fails: bad content" << std::endl;
256  return false;
257  }
258  }
259 
260  vpColVector r4 = 0.5 * r1;
261  std::vector<double> bench2(4, vpMath::rad(5));
262  if (test("r4", r4, bench2) == false)
263  return err;
264 
265  vpQuaternionVector r5(r3);
266  if (test("r5", r5, bench1) == false)
267  return err;
268  }
269  {
271  for(int i=-10;i<10;i++){
272  for(int j=-10;j<10;j++){
273  vpThetaUVector tu(vpMath::rad(90+i), vpMath::rad(170+j), vpMath::rad(45)) ;
274  tu.buildFrom(vpRotationMatrix(tu)); //put some coherence into rotation convention
275 
276  std::cout << "Initialization " <<std::endl ;
277 
278  double theta;
279  vpColVector u;
280  tu.extract(theta, u);
281 
282  std::cout << "theta=" << vpMath::deg(theta) << std::endl ;
283  std::cout << "u=" << u << std::endl ;
284 
285  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl ;
286  R.buildFrom(tu) ;
287 
288  std::cout << "Matrix R" ;
289  if (R.isARotationMatrix()==1) std::cout <<" is a rotation matrix " << std::endl ;
290  else std::cout <<" is not a rotation matrix " << std::endl ;
291 
292  std::cout << R << std::endl ;
293 
294  std::cout << "From vpRotationMatrix to vpQuaternionVector " << std::endl ;
295  vpQuaternionVector q(R);
296  std::cout << q <<std::endl ;
297 
298  R.buildFrom(q);
299  std::cout << "From vpQuaternionVector to vpRotationMatrix " << std::endl ;
300 
301  std::cout << "From vpRotationMatrix to vpRxyzVector " << std::endl ;
302  vpRxyzVector RxyzBuildFromR(R) ;
303  std::cout << RxyzBuildFromR <<std::endl ;
304 
305 
306  std::cout << "From vpRxyzVector to vpThetaUVector " << std::endl ;
307  std::cout << " use From vpRxyzVector to vpRotationMatrix " << std::endl ;
308  std::cout << " use From vpRotationMatrix to vpThetaUVector " << std::endl ;
309 
310 
311  vpThetaUVector tuBuildFromEu ;
312  tuBuildFromEu.buildFrom(R) ;
313 
314  std::cout << std::endl ;
315  std::cout << "result : should equivalent to the first one " << std::endl ;
316 
317 
318  double theta2;
319  vpColVector u2;
320 
321  tuBuildFromEu.extract(theta2, u2);
322  std::cout << "theta=" << vpMath::deg(theta2) << std::endl ;
323  std::cout << "u=" << u2 << std::endl ;
324 
325  assert(vpMath::abs(theta2-theta)<std::numeric_limits<double>::epsilon()*1e10);
326  assert(vpMath::abs(u[0]-u2[0])<std::numeric_limits<double>::epsilon()*1e10);
327  assert(vpMath::abs(u[1]-u2[1])<std::numeric_limits<double>::epsilon()*1e10);
328  assert(vpMath::abs(u[2]-u2[2])<std::numeric_limits<double>::epsilon()*1e10);
329  }
330  vpRzyzVector rzyz(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
331  std::cout << "Initialization vpRzyzVector " <<std::endl ;
332  std::cout << rzyz << std::endl ;
333  std::cout << "From vpRzyzVector to vpRotationMatrix " << std::endl ;
334  R.buildFrom(rzyz) ;
335  std::cout << "From vpRotationMatrix to vpRzyzVector " << std::endl ;
336  vpRzyzVector rzyz_final ;
337  rzyz_final.buildFrom(R) ;
338  std::cout << rzyz_final << std::endl ;
339 
340  vpRzyxVector rzyx(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
341  std::cout << "Initialization vpRzyxVector " <<std::endl ;
342  std::cout << rzyx << std::endl ;
343  std::cout << "From vpRzyxVector to vpRotationMatrix " << std::endl ;
344  R.buildFrom(rzyx) ;
345  std::cout << R << std::endl ;
346  std::cout << "From vpRotationMatrix to vpRzyxVector " << std::endl ;
347  vpRzyxVector rzyx_final ;
348  rzyx_final.buildFrom(R) ;
349  std::cout << rzyx_final << std::endl ;
350  }
351  }
352  std::cout << "All tests succeed" << std::endl;
353  return 0;
354  }
355  catch(vpException &e) {
356  std::cout << "Catch an exception: " << e << std::endl;
357  return 1;
358  }
359 }
Implementation of a generic rotation vector.
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:149
void set(const double x, const double y, const double z, const double w)
vpRzyzVector buildFrom(const vpRotationMatrix &R)
error that can be emited by ViSP classes.
Definition: vpException.h:73
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:152
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
static Type abs(const Type &x)
Definition: vpMath.h:161
bool isARotationMatrix() const
Implementation of a rotation matrix and operations on such kind of matrices.
void extract(double &theta, vpColVector &u) const
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
Implementation of a rotation vector as quaternion angle minimal representation.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
vpRzyxVector buildFrom(const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:104
static double deg(double rad)
Definition: vpMath.h:97
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyzVector.h:151
Implementation of a rotation vector as axis-angle minimal representation.