Visual Servoing Platform  version 3.6.1 under development (2024-05-26)
vpRzyxVector.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  * Rzyx angle parameterization for the rotation.
33  * Rzyx(phi,theta,psi) = Rot(z,phi)Rot(y,theta)Rot(x,psi)
34  *
35 *****************************************************************************/
36 
37 #include <math.h>
38 #include <visp3/core/vpRzyxVector.h>
48 
51 
58 vpRzyxVector::vpRzyxVector(double phi, double theta, double psi) : vpRotationVector(3) { build(phi, theta, psi); }
59 
66 
74 
77 
79 vpRzyxVector::vpRzyxVector(const std::vector<double> &rzyx) : vpRotationVector(3) { build(rzyx); }
80 
81 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
94 {
95  build(R);
96  return *this;
97 }
98 
107 {
108  build(tu);
109  return *this;
110 }
111 
119 void vpRzyxVector::buildFrom(double phi, double theta, double psi)
120 {
121  build(phi, theta, psi);
122 }
123 
129 {
130  build(rzyx);
131  return *this;
132 }
133 
138 vpRzyxVector vpRzyxVector::buildFrom(const std::vector<double> &rzyx)
139 {
140  build(rzyx);
141  return *this;
142 }
143 #endif
144 
156 {
157  double nx = R[0][0];
158  double ny = R[1][0];
159 
160  double COEF_MIN_ROT = 1e-6;
161  double phi;
162 
163  if ((fabs(nx) < COEF_MIN_ROT) && (fabs(ny) < COEF_MIN_ROT)) {
164  phi = 0;
165  }
166  else {
167  phi = atan2(ny, nx);
168  }
169  double si = sin(phi);
170  double co = cos(phi);
171 
172  double nz = R[2][0];
173  double theta = atan2(-nz, (co * nx) + (si * ny));
174 
175  double ax = R[0][2];
176  double ay = R[1][2];
177  double ox = R[0][1];
178  double oy = R[1][1];
179 
180  double psi = atan2((si * ax) - (co * ay), (-si * ox) + (co * oy));
181 
182  build(phi, theta, psi);
183 
184  return *this;
185 }
186 
194 {
196  R.build(tu);
197  build(R);
198 
199  return *this;
200 }
201 
208 vpRzyxVector &vpRzyxVector::build(const double &phi, const double &theta, const double &psi)
209 {
210  data[0] = phi;
211  data[1] = theta;
212  data[2] = psi;
213  return *this;
214 }
215 
220 {
221  if (rzyx.size() != 3) {
222  throw(vpException(vpException::dimensionError, "Cannot construct a R-zyx vector from a %d-dimension col vector",
223  rzyx.size()));
224  }
225  for (unsigned int i = 0; i < 3; ++i) {
226  data[i] = rzyx[i];
227  }
228 
229  return *this;
230 }
231 
235 vpRzyxVector &vpRzyxVector::build(const std::vector<double> &rzyx)
236 {
237  if (rzyx.size() != 3) {
238  throw(vpException(vpException::dimensionError, "Cannot construct a R-zyx vector from a %d-dimension std::vector",
239  rzyx.size()));
240  }
241  for (unsigned int i = 0; i < 3; ++i) {
242  data[i] = rzyx[i];
243  }
244 
245  return *this;
246 }
247 
268 {
269  for (unsigned int i = 0; i < dsize; ++i) {
270  data[i] = v;
271  }
272 
273  return *this;
274 }
275 
299 {
300  if (rzyx.size() != 3) {
301  throw(vpException(vpException::dimensionError, "Cannot set a R-zyx vector from a %d-dimension col vector",
302  rzyx.size()));
303  }
304  for (unsigned int i = 0; i < 3; ++i) {
305  data[i] = rzyx[i];
306  }
307 
308  return *this;
309 }
310 
311 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
329 vpRzyxVector &vpRzyxVector::operator=(const std::initializer_list<double> &list)
330 {
331  if (list.size() > size()) {
332  throw(vpException(
334  "Cannot set Euler x-y-z vector out of bounds. It has only %d values while you try to initialize with %d values",
335  size(), list.size()));
336  }
337  std::copy(list.begin(), list.end(), data);
338  return *this;
339 }
340 #endif
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:135
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:341
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:70
Implementation of a rotation matrix and operations on such kind of matrices.
vpRotationMatrix & build(const vpHomogeneousMatrix &M)
Implementation of a generic rotation vector.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRzyxVector.h:178
vpRzyxVector & operator=(const vpColVector &rzyx)
vp_deprecated vpRzyxVector buildFrom(const vpRotationMatrix &R)
vpRzyxVector & build(const vpRotationMatrix &R)
Implementation of a rotation vector as axis-angle minimal representation.