ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testRotation.cpp
1 /****************************************************************************
2  *
3  * $Id: testRotation.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tests transformation from various representations of rotation.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
50 #include <visp/vpMath.h>
51 #include <visp/vpRotationMatrix.h>
52 #include <visp/vpParseArgv.h>
53 #include <visp/vpQuaternionVector.h>
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <cassert>
58 #include <limits>
59 // List of allowed command line options
60 #define GETOPTARGS "h"
61 
67 void usage(const char *name, const char *badparam)
68 {
69  fprintf(stdout, "\n\
70 Tests transformation within various representations of rotation.\n\
71 \n\
72 SYNOPSIS\n\
73  %s [-h]\n", name);
74 
75  fprintf(stdout, "\n\
76 OPTIONS: Default\n\
77  -h\n\
78  Print the help.\n");
79 
80  if (badparam)
81  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
82 }
90 bool getOptions(int argc, const char **argv)
91 {
92  const char *optarg;
93  int c;
94  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
95 
96  switch (c) {
97  case 'h': usage(argv[0], NULL); return false; break;
98 
99  default:
100  usage(argv[0], optarg);
101  return false; break;
102  }
103  }
104 
105  if ((c == 1) || (c == -1)) {
106  // standalone param or error
107  usage(argv[0], NULL);
108  std::cerr << "ERROR: " << std::endl;
109  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
110  return false;
111  }
112 
113  return true;
114 }
115 
116 
117 int
118 main(int argc, const char ** argv)
119 {
120  // Read the command line options
121  if (getOptions(argc, argv) == false) {
122  exit (-1);
123  }
125  for(int i=-10;i<10;i++){
126  for(int j=-10;j<10;j++){
127  vpThetaUVector tu(vpMath::rad(90+i), vpMath::rad(170+j), vpMath::rad(45)) ;
128  tu.buildFrom(vpRotationMatrix(tu)); //put some coherence into rotation convention
129 
130  std::cout << "Initialization " <<std::endl ;
131 
132  double theta;
133  vpColVector u;
134  tu.extract(theta, u);
135 
136  std::cout << "theta=" << vpMath::deg(theta) << std::endl ;
137  std::cout << "u=" << u << std::endl ;
138 
139  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl ;
140  R.buildFrom(tu) ;
141 
142  std::cout << "Matrix R" ;
143  if (R.isARotationMatrix()==1) std::cout <<" is a rotation matrix " << std::endl ;
144  else std::cout <<" is not a rotation matrix " << std::endl ;
145 
146  std::cout << R << std::endl ;
147 
148  std::cout << "From vpRotationMatrix to vpQuaternionVector " << std::endl ;
149  vpQuaternionVector q(R);
150  std::cout << q <<std::endl ;
151 
152  R.buildFrom(q);
153  std::cout << "From vpQuaternionVector to vpRotationMatrix " << std::endl ;
154 
155  std::cout << "From vpRotationMatrix to vpRxyzVector " << std::endl ;
156  vpRxyzVector RxyzBuildFromR(R) ;
157  std::cout << RxyzBuildFromR <<std::endl ;
158 
159 
160  std::cout << "From vpRxyzVector to vpThetaUVector " << std::endl ;
161  std::cout << " use From vpRxyzVector to vpRotationMatrix " << std::endl ;
162  std::cout << " use From vpRotationMatrix to vpThetaUVector " << std::endl ;
163 
164 
165  vpThetaUVector tuBuildFromEu ;
166  tuBuildFromEu.buildFrom(R) ;
167 
168  std::cout << std::endl ;
169  std::cout << "result : should equivalent to the first one " << std::endl ;
170 
171 
172  double theta2;
173  vpColVector u2;
174 
175  tuBuildFromEu.extract(theta2, u2);
176  std::cout << "theta=" << vpMath::deg(theta2) << std::endl ;
177  std::cout << "u=" << u2 << std::endl ;
178 
179  assert(vpMath::abs(theta2-theta)<std::numeric_limits<double>::epsilon()*1e10);
180  assert(vpMath::abs(u[0]-u2[0])<std::numeric_limits<double>::epsilon()*1e10);
181  assert(vpMath::abs(u[1]-u2[1])<std::numeric_limits<double>::epsilon()*1e10);
182  assert(vpMath::abs(u[2]-u2[2])<std::numeric_limits<double>::epsilon()*1e10);
183  }
184  vpRzyzVector rzyz(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
185  std::cout << "Initialization vpRzyzVector " <<std::endl ;
186  std::cout << rzyz << std::endl ;
187  std::cout << "From vpRzyzVector to vpRotationMatrix " << std::endl ;
188  R.buildFrom(rzyz) ;
189  std::cout << "From vpRotationMatrix to vpRzyzVector " << std::endl ;
190  vpRzyzVector rzyz_final ;
191  rzyz_final.buildFrom(R) ;
192  std::cout << rzyz_final << std::endl ;
193 
194 
195  vpRzyxVector rzyx(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
196  std::cout << "Initialization vpRzyxVector " <<std::endl ;
197  std::cout << rzyx << std::endl ;
198  std::cout << "From vpRzyxVector to vpRotationMatrix " << std::endl ;
199  R.buildFrom(rzyx) ;
200  std::cout << R << std::endl ;
201  std::cout << "From vpRotationMatrix to vpRzyxVector " << std::endl ;
202  vpRzyxVector rzyx_final ;
203  rzyx_final.buildFrom(R) ;
204  std::cout << rzyx_final << std::endl ;
205  }
206 }