ViSP  2.9.0
testRotation.cpp
1 /****************************************************************************
2  *
3  * $Id: testRotation.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 
62 void usage(const char *name, const char *badparam);
63 bool getOptions(int argc, const char **argv);
64 
70 void usage(const char *name, const char *badparam)
71 {
72  fprintf(stdout, "\n\
73 Tests transformation within various representations of rotation.\n\
74 \n\
75 SYNOPSIS\n\
76  %s [-h]\n", name);
77 
78  fprintf(stdout, "\n\
79 OPTIONS: Default\n\
80  -h\n\
81  Print the help.\n");
82 
83  if (badparam)
84  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
85 }
93 bool getOptions(int argc, const char **argv)
94 {
95  const char *optarg_;
96  int c;
97  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
98 
99  switch (c) {
100  case 'h': usage(argv[0], NULL); return false; break;
101 
102  default:
103  usage(argv[0], optarg_);
104  return false; break;
105  }
106  }
107 
108  if ((c == 1) || (c == -1)) {
109  // standalone param or error
110  usage(argv[0], NULL);
111  std::cerr << "ERROR: " << std::endl;
112  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
113  return false;
114  }
115 
116  return true;
117 }
118 
119 
120 int
121 main(int argc, const char ** argv)
122 {
123  try {
124  // Read the command line options
125  if (getOptions(argc, argv) == false) {
126  exit (-1);
127  }
129  for(int i=-10;i<10;i++){
130  for(int j=-10;j<10;j++){
131  vpThetaUVector tu(vpMath::rad(90+i), vpMath::rad(170+j), vpMath::rad(45)) ;
132  tu.buildFrom(vpRotationMatrix(tu)); //put some coherence into rotation convention
133 
134  std::cout << "Initialization " <<std::endl ;
135 
136  double theta;
137  vpColVector u;
138  tu.extract(theta, u);
139 
140  std::cout << "theta=" << vpMath::deg(theta) << std::endl ;
141  std::cout << "u=" << u << std::endl ;
142 
143  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl ;
144  R.buildFrom(tu) ;
145 
146  std::cout << "Matrix R" ;
147  if (R.isARotationMatrix()==1) std::cout <<" is a rotation matrix " << std::endl ;
148  else std::cout <<" is not a rotation matrix " << std::endl ;
149 
150  std::cout << R << std::endl ;
151 
152  std::cout << "From vpRotationMatrix to vpQuaternionVector " << std::endl ;
153  vpQuaternionVector q(R);
154  std::cout << q <<std::endl ;
155 
156  R.buildFrom(q);
157  std::cout << "From vpQuaternionVector to vpRotationMatrix " << std::endl ;
158 
159  std::cout << "From vpRotationMatrix to vpRxyzVector " << std::endl ;
160  vpRxyzVector RxyzBuildFromR(R) ;
161  std::cout << RxyzBuildFromR <<std::endl ;
162 
163 
164  std::cout << "From vpRxyzVector to vpThetaUVector " << std::endl ;
165  std::cout << " use From vpRxyzVector to vpRotationMatrix " << std::endl ;
166  std::cout << " use From vpRotationMatrix to vpThetaUVector " << std::endl ;
167 
168 
169  vpThetaUVector tuBuildFromEu ;
170  tuBuildFromEu.buildFrom(R) ;
171 
172  std::cout << std::endl ;
173  std::cout << "result : should equivalent to the first one " << std::endl ;
174 
175 
176  double theta2;
177  vpColVector u2;
178 
179  tuBuildFromEu.extract(theta2, u2);
180  std::cout << "theta=" << vpMath::deg(theta2) << std::endl ;
181  std::cout << "u=" << u2 << std::endl ;
182 
183  assert(vpMath::abs(theta2-theta)<std::numeric_limits<double>::epsilon()*1e10);
184  assert(vpMath::abs(u[0]-u2[0])<std::numeric_limits<double>::epsilon()*1e10);
185  assert(vpMath::abs(u[1]-u2[1])<std::numeric_limits<double>::epsilon()*1e10);
186  assert(vpMath::abs(u[2]-u2[2])<std::numeric_limits<double>::epsilon()*1e10);
187  }
188  vpRzyzVector rzyz(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
189  std::cout << "Initialization vpRzyzVector " <<std::endl ;
190  std::cout << rzyz << std::endl ;
191  std::cout << "From vpRzyzVector to vpRotationMatrix " << std::endl ;
192  R.buildFrom(rzyz) ;
193  std::cout << "From vpRotationMatrix to vpRzyzVector " << std::endl ;
194  vpRzyzVector rzyz_final ;
195  rzyz_final.buildFrom(R) ;
196  std::cout << rzyz_final << std::endl ;
197 
198 
199  vpRzyxVector rzyx(vpMath::rad(180), vpMath::rad(120), vpMath::rad(45)) ;
200  std::cout << "Initialization vpRzyxVector " <<std::endl ;
201  std::cout << rzyx << std::endl ;
202  std::cout << "From vpRzyxVector to vpRotationMatrix " << std::endl ;
203  R.buildFrom(rzyx) ;
204  std::cout << R << std::endl ;
205  std::cout << "From vpRotationMatrix to vpRzyxVector " << std::endl ;
206  vpRzyxVector rzyx_final ;
207  rzyx_final.buildFrom(R) ;
208  std::cout << rzyx_final << std::endl ;
209  }
210  return 0;
211  }
212  catch(vpException e) {
213  std::cout << "Catch an exception: " << e << std::endl;
214  return 1;
215  }
216 }
error that can be emited by ViSP classes.
Definition: vpException.h:76
Class that consider the case of the Euler angle using the z-y-x convention, where are respectively ...
Definition: vpRzyxVector.h:151
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static Type abs(const Type &x)
Definition: vpMath.h:158
bool isARotationMatrix() const
test if the matrix is an rotation matrix
The vpRotationMatrix considers the particular case of a rotation matrix.
vpRotationMatrix buildFrom(const vpThetaUVector &v)
Transform a vector vpThetaUVector into an rotation matrix.
void extract(double &theta, vpColVector &u) const
Defines a quaternion and its basic operations.
void buildFrom(const double phi, const double theta, const double psi)
Definition: vpRzyxVector.h:183
static double rad(double deg)
Definition: vpMath.h:100
static double deg(double rad)
Definition: vpMath.h:93
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
Class that consider the case of the Euler angle using the x-y-z convention, where are respectively ...
Definition: vpRxyzVector.h:152
Class that consider the case of the Euler angles using the z-y-z convention, where are respectively...
Definition: vpRzyzVector.h:150
void buildFrom(const double phi, const double theta, const double psi)
Definition: vpRzyzVector.h:182
Class that consider the case of the parameterization for the rotation.